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_set_position(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_set_position(c->buffer, LDNS_HEADER_SIZE); 1167 sldns_buffer_flip(c->buffer); 1168 return 1; 1169 } 1170 1171 if (!query_dname_len(c->buffer)) { 1172 LDNS_QDCOUNT_SET(sldns_buffer_begin(c->buffer), 0); 1173 LDNS_ANCOUNT_SET(sldns_buffer_begin(c->buffer), 0); 1174 LDNS_NSCOUNT_SET(sldns_buffer_begin(c->buffer), 0); 1175 LDNS_ARCOUNT_SET(sldns_buffer_begin(c->buffer), 0); 1176 LDNS_QR_SET(sldns_buffer_begin(c->buffer)); 1177 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), 1178 LDNS_RCODE_FORMERR); 1179 sldns_buffer_set_position(c->buffer, LDNS_HEADER_SIZE); 1180 sldns_buffer_flip(c->buffer); 1181 return 1; 1182 } 1183 /* space available for query type and class? */ 1184 if (sldns_buffer_remaining(c->buffer) < 2 * sizeof(uint16_t)) { 1185 LDNS_QR_SET(sldns_buffer_begin(c->buffer)); 1186 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), 1187 LDNS_RCODE_FORMERR); 1188 LDNS_QDCOUNT_SET(sldns_buffer_begin(c->buffer), 0); 1189 LDNS_ANCOUNT_SET(sldns_buffer_begin(c->buffer), 0); 1190 LDNS_NSCOUNT_SET(sldns_buffer_begin(c->buffer), 0); 1191 LDNS_ARCOUNT_SET(sldns_buffer_begin(c->buffer), 0); 1192 sldns_buffer_set_position(c->buffer, LDNS_HEADER_SIZE); 1193 sldns_buffer_flip(c->buffer); 1194 return 1; 1195 } 1196 LDNS_QR_SET(sldns_buffer_begin(c->buffer)); 1197 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), 1198 LDNS_RCODE_REFUSED); 1199 1200 sldns_buffer_skip(c->buffer, (ssize_t)sizeof(uint16_t)); /* skip qtype */ 1201 1202 sldns_buffer_skip(c->buffer, (ssize_t)sizeof(uint16_t)); /* skip qclass */ 1203 1204 /* The OPT RR to be returned should come directly after 1205 * the query, so mark this spot. 1206 */ 1207 opt_rr_mark = sldns_buffer_position(c->buffer); 1208 1209 /* Skip through the RR records */ 1210 if(LDNS_ANCOUNT(sldns_buffer_begin(c->buffer)) != 0 || 1211 LDNS_NSCOUNT(sldns_buffer_begin(c->buffer)) != 0) { 1212 if(!skip_pkt_rrs(c->buffer, 1213 ((int)LDNS_ANCOUNT(sldns_buffer_begin(c->buffer)))+ 1214 ((int)LDNS_NSCOUNT(sldns_buffer_begin(c->buffer))))) { 1215 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), 1216 LDNS_RCODE_FORMERR); 1217 LDNS_ANCOUNT_SET(sldns_buffer_begin(c->buffer), 0); 1218 LDNS_NSCOUNT_SET(sldns_buffer_begin(c->buffer), 0); 1219 LDNS_ARCOUNT_SET(sldns_buffer_begin(c->buffer), 0); 1220 sldns_buffer_set_position(c->buffer, opt_rr_mark); 1221 sldns_buffer_flip(c->buffer); 1222 return 1; 1223 } 1224 } 1225 /* Do we have a valid OPT RR here? If not return REFUSED (could be a valid TSIG or something so no FORMERR) */ 1226 /* domain name must be the root of length 1. */ 1227 if(sldns_buffer_remaining(c->buffer) < 1 || *sldns_buffer_current(c->buffer) != 0) { 1228 LDNS_ANCOUNT_SET(sldns_buffer_begin(c->buffer), 0); 1229 LDNS_NSCOUNT_SET(sldns_buffer_begin(c->buffer), 0); 1230 LDNS_ARCOUNT_SET(sldns_buffer_begin(c->buffer), 0); 1231 sldns_buffer_set_position(c->buffer, opt_rr_mark); 1232 sldns_buffer_flip(c->buffer); 1233 return 1; 1234 } else { 1235 sldns_buffer_skip(c->buffer, 1); /* skip root label */ 1236 } 1237 if(sldns_buffer_remaining(c->buffer) < 2 || 1238 sldns_buffer_read_u16(c->buffer) != LDNS_RR_TYPE_OPT) { 1239 LDNS_ANCOUNT_SET(sldns_buffer_begin(c->buffer), 0); 1240 LDNS_NSCOUNT_SET(sldns_buffer_begin(c->buffer), 0); 1241 LDNS_ARCOUNT_SET(sldns_buffer_begin(c->buffer), 0); 1242 sldns_buffer_set_position(c->buffer, opt_rr_mark); 1243 sldns_buffer_flip(c->buffer); 1244 return 1; 1245 } 1246 /* Write OPT RR directly after the query, 1247 * so without the (possibly skipped) Answer and NS RRs 1248 */ 1249 LDNS_ANCOUNT_SET(sldns_buffer_begin(c->buffer), 0); 1250 LDNS_NSCOUNT_SET(sldns_buffer_begin(c->buffer), 0); 1251 sldns_buffer_clear(c->buffer); /* reset write limit */ 1252 sldns_buffer_set_position(c->buffer, opt_rr_mark); 1253 1254 /* Check if OPT record can be written 1255 * 17 == root label (1) + RR type (2) + UDP Size (2) 1256 * + Fields (4) + rdata len (2) + EDE Option code (2) 1257 * + EDE Option length (2) + EDE info-code (2) 1258 */ 1259 if (sldns_buffer_available(c->buffer, 17) == 0) { 1260 LDNS_ARCOUNT_SET(sldns_buffer_begin(c->buffer), 0); 1261 sldns_buffer_flip(c->buffer); 1262 return 1; 1263 } 1264 1265 LDNS_ARCOUNT_SET(sldns_buffer_begin(c->buffer), 1); 1266 1267 /* root label */ 1268 sldns_buffer_write_u8(c->buffer, 0); 1269 sldns_buffer_write_u16(c->buffer, LDNS_RR_TYPE_OPT); 1270 sldns_buffer_write_u16(c->buffer, EDNS_ADVERTISED_SIZE); 1271 1272 /* write OPT Record TTL Field */ 1273 sldns_buffer_write_u32(c->buffer, 0); 1274 1275 /* write rdata len: EDE option + length + info-code */ 1276 sldns_buffer_write_u16(c->buffer, 6); 1277 1278 /* write OPTIONS; add EDE option code */ 1279 sldns_buffer_write_u16(c->buffer, LDNS_EDNS_EDE); 1280 1281 /* write single EDE option length (for just 1 info-code) */ 1282 sldns_buffer_write_u16(c->buffer, 2); 1283 1284 /* write single EDE info-code */ 1285 sldns_buffer_write_u16(c->buffer, LDNS_EDE_PROHIBITED); 1286 1287 sldns_buffer_flip(c->buffer); 1288 1289 verbose(VERB_ALGO, "attached EDE code: %d", LDNS_EDE_PROHIBITED); 1290 1291 return 1; 1292 1293 } 1294 1295 return -1; 1296 } 1297 1298 static int 1299 deny_refuse_all(struct comm_point* c, enum acl_access* acl, 1300 struct worker* worker, struct comm_reply* repinfo, 1301 struct acl_addr** acladdr, int ede, int check_proxy, 1302 struct check_request_result* check_result) 1303 { 1304 if(check_proxy) { 1305 *acladdr = acl_addr_lookup(worker->daemon->acl, 1306 &repinfo->remote_addr, repinfo->remote_addrlen); 1307 } else { 1308 *acladdr = acl_addr_lookup(worker->daemon->acl, 1309 &repinfo->client_addr, repinfo->client_addrlen); 1310 } 1311 /* If there is no ACL based on client IP use the interface ACL. */ 1312 if(!(*acladdr) && c->socket) { 1313 *acladdr = c->socket->acl; 1314 } 1315 *acl = acl_get_control(*acladdr); 1316 return deny_refuse(c, *acl, acl_deny, acl_refuse, worker, repinfo, 1317 *acladdr, ede, check_result); 1318 } 1319 1320 static int 1321 deny_refuse_non_local(struct comm_point* c, enum acl_access acl, 1322 struct worker* worker, struct comm_reply* repinfo, 1323 struct acl_addr* acladdr, int ede, 1324 struct check_request_result* check_result) 1325 { 1326 return deny_refuse(c, acl, acl_deny_non_local, acl_refuse_non_local, 1327 worker, repinfo, acladdr, ede, check_result); 1328 } 1329 1330 /* Check if the query is blocked by source IP rate limiting. 1331 * Returns 1 if it passes the check, 0 otherwise. */ 1332 static int 1333 check_ip_ratelimit(struct worker* worker, struct sockaddr_storage* addr, 1334 socklen_t addrlen, int has_cookie, sldns_buffer* pkt) 1335 { 1336 if(!infra_ip_ratelimit_inc(worker->env.infra_cache, addr, addrlen, 1337 *worker->env.now, has_cookie, 1338 worker->env.cfg->ip_ratelimit_backoff, pkt)) { 1339 /* See if we can pass through with slip factor */ 1340 if(!has_cookie && worker->env.cfg->ip_ratelimit_factor != 0 && 1341 ub_random_max(worker->env.rnd, 1342 worker->env.cfg->ip_ratelimit_factor) == 0) { 1343 char addrbuf[128]; 1344 addr_to_str(addr, addrlen, addrbuf, sizeof(addrbuf)); 1345 verbose(VERB_QUERY, "ip_ratelimit allowed through for " 1346 "ip address %s because of slip in " 1347 "ip_ratelimit_factor", addrbuf); 1348 return 1; 1349 } 1350 return 0; 1351 } 1352 return 1; 1353 } 1354 1355 int 1356 worker_handle_request(struct comm_point* c, void* arg, int error, 1357 struct comm_reply* repinfo) 1358 { 1359 struct worker* worker = (struct worker*)arg; 1360 int ret; 1361 hashvalue_type h; 1362 struct lruhash_entry* e; 1363 struct query_info qinfo; 1364 struct edns_data edns; 1365 struct edns_option* original_edns_list = NULL; 1366 enum acl_access acl; 1367 struct acl_addr* acladdr; 1368 int pre_edns_ip_ratelimit = 1; 1369 int rc = 0; 1370 int need_drop = 0; 1371 int is_expired_answer = 0; 1372 int is_secure_answer = 0; 1373 int rpz_passthru = 0; 1374 long long wait_queue_time = 0; 1375 /* We might have to chase a CNAME chain internally, in which case 1376 * we'll have up to two replies and combine them to build a complete 1377 * answer. These variables control this case. */ 1378 struct ub_packed_rrset_key* alias_rrset = NULL; 1379 struct reply_info* partial_rep = NULL; 1380 struct query_info* lookup_qinfo = &qinfo; 1381 struct query_info qinfo_tmp; /* placeholder for lookup_qinfo */ 1382 struct respip_client_info* cinfo = NULL, cinfo_tmp; 1383 struct timeval wait_time; 1384 struct check_request_result check_result = {0,0}; 1385 memset(&qinfo, 0, sizeof(qinfo)); 1386 1387 if((error != NETEVENT_NOERROR && error != NETEVENT_DONE)|| !repinfo) { 1388 /* some bad tcp query DNS formats give these error calls */ 1389 verbose(VERB_ALGO, "handle request called with err=%d", error); 1390 return 0; 1391 } 1392 1393 if (worker->env.cfg->sock_queue_timeout && timeval_isset(&c->recv_tv)) { 1394 timeval_subtract(&wait_time, worker->env.now_tv, &c->recv_tv); 1395 wait_queue_time = wait_time.tv_sec * 1000000 + wait_time.tv_usec; 1396 if (worker->stats.max_query_time_us < wait_queue_time) 1397 worker->stats.max_query_time_us = wait_queue_time; 1398 if(wait_queue_time > 1399 (long long)(worker->env.cfg->sock_queue_timeout * 1000000)) { 1400 /* count and drop queries that were sitting in the socket queue too long */ 1401 worker->stats.num_queries_timed_out++; 1402 return 0; 1403 } 1404 } 1405 1406 #ifdef USE_DNSCRYPT 1407 repinfo->max_udp_size = worker->daemon->cfg->max_udp_size; 1408 if(!dnsc_handle_curved_request(worker->daemon->dnscenv, repinfo)) { 1409 worker->stats.num_query_dnscrypt_crypted_malformed++; 1410 return 0; 1411 } 1412 if(c->dnscrypt && !repinfo->is_dnscrypted) { 1413 char buf[LDNS_MAX_DOMAINLEN+1]; 1414 /* Check if this is unencrypted and asking for certs */ 1415 worker_check_request(c->buffer, worker, &check_result); 1416 if(check_result.value != 0) { 1417 verbose(VERB_ALGO, 1418 "dnscrypt: worker check request: bad query."); 1419 log_addr(VERB_CLIENT,"from",&repinfo->client_addr, 1420 repinfo->client_addrlen); 1421 comm_point_drop_reply(repinfo); 1422 return 0; 1423 } 1424 if(!query_info_parse(&qinfo, c->buffer)) { 1425 verbose(VERB_ALGO, 1426 "dnscrypt: worker parse request: formerror."); 1427 log_addr(VERB_CLIENT, "from", &repinfo->client_addr, 1428 repinfo->client_addrlen); 1429 comm_point_drop_reply(repinfo); 1430 return 0; 1431 } 1432 dname_str(qinfo.qname, buf); 1433 if(!(qinfo.qtype == LDNS_RR_TYPE_TXT && 1434 strcasecmp(buf, 1435 worker->daemon->dnscenv->provider_name) == 0)) { 1436 verbose(VERB_ALGO, 1437 "dnscrypt: not TXT \"%s\". Received: %s \"%s\"", 1438 worker->daemon->dnscenv->provider_name, 1439 sldns_rr_descript(qinfo.qtype)->_name, 1440 buf); 1441 comm_point_drop_reply(repinfo); 1442 worker->stats.num_query_dnscrypt_cleartext++; 1443 return 0; 1444 } 1445 worker->stats.num_query_dnscrypt_cert++; 1446 sldns_buffer_rewind(c->buffer); 1447 } else if(c->dnscrypt && repinfo->is_dnscrypted) { 1448 worker->stats.num_query_dnscrypt_crypted++; 1449 } 1450 #endif 1451 #ifdef USE_DNSTAP 1452 /* 1453 * sending src (client)/dst (local service) addresses over DNSTAP from incoming request handler 1454 */ 1455 if(worker->dtenv.log_client_query_messages) { 1456 log_addr(VERB_ALGO, "request from client", &repinfo->client_addr, repinfo->client_addrlen); 1457 log_addr(VERB_ALGO, "to local addr", (void*)repinfo->c->socket->addr->ai_addr, repinfo->c->socket->addr->ai_addrlen); 1458 dt_msg_send_client_query(&worker->dtenv, &repinfo->client_addr, (void*)repinfo->c->socket->addr->ai_addr, c->type, c->ssl, c->buffer, 1459 ((worker->env.cfg->sock_queue_timeout && timeval_isset(&c->recv_tv))?&c->recv_tv:NULL)); 1460 } 1461 #endif 1462 /* Check deny/refuse ACLs */ 1463 if(repinfo->is_proxied) { 1464 if((ret=deny_refuse_all(c, &acl, worker, repinfo, &acladdr, 1465 worker->env.cfg->ede, 1, &check_result)) != -1) { 1466 if(ret == 1) 1467 goto send_reply; 1468 return ret; 1469 } 1470 } 1471 if((ret=deny_refuse_all(c, &acl, worker, repinfo, &acladdr, 1472 worker->env.cfg->ede, 0, &check_result)) != -1) { 1473 if(ret == 1) 1474 goto send_reply; 1475 return ret; 1476 } 1477 1478 worker_check_request(c->buffer, worker, &check_result); 1479 if(check_result.value != 0) { 1480 verbose(VERB_ALGO, "worker check request: bad query."); 1481 log_addr(VERB_CLIENT,"from",&repinfo->client_addr, repinfo->client_addrlen); 1482 if(check_result.value != -1) { 1483 LDNS_QR_SET(sldns_buffer_begin(c->buffer)); 1484 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), 1485 check_result.value); 1486 return 1; 1487 } 1488 comm_point_drop_reply(repinfo); 1489 return 0; 1490 } 1491 1492 worker->stats.num_queries++; 1493 pre_edns_ip_ratelimit = !worker->env.cfg->do_answer_cookie 1494 || sldns_buffer_limit(c->buffer) < LDNS_HEADER_SIZE 1495 || LDNS_ARCOUNT(sldns_buffer_begin(c->buffer)) == 0; 1496 1497 /* If the IP rate limiting check needs extra EDNS information (e.g., 1498 * DNS Cookies) postpone the check until after EDNS is parsed. */ 1499 if(pre_edns_ip_ratelimit) { 1500 /* NOTE: we always check the repinfo->client_address. 1501 * IP ratelimiting is implicitly disabled for proxies. */ 1502 if(!check_ip_ratelimit(worker, &repinfo->client_addr, 1503 repinfo->client_addrlen, 0, c->buffer)) { 1504 worker->stats.num_queries_ip_ratelimited++; 1505 comm_point_drop_reply(repinfo); 1506 return 0; 1507 } 1508 } 1509 1510 if(!query_info_parse(&qinfo, c->buffer)) { 1511 verbose(VERB_ALGO, "worker parse request: formerror."); 1512 log_addr(VERB_CLIENT, "from", &repinfo->client_addr, 1513 repinfo->client_addrlen); 1514 memset(&qinfo, 0, sizeof(qinfo)); /* zero qinfo.qname */ 1515 if(worker_err_ratelimit(worker, LDNS_RCODE_FORMERR) == -1) { 1516 comm_point_drop_reply(repinfo); 1517 return 0; 1518 } 1519 sldns_buffer_rewind(c->buffer); 1520 LDNS_QR_SET(sldns_buffer_begin(c->buffer)); 1521 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), 1522 LDNS_RCODE_FORMERR); 1523 goto send_reply; 1524 } 1525 if(worker->env.cfg->log_queries) { 1526 char ip[128]; 1527 addr_to_str(&repinfo->client_addr, repinfo->client_addrlen, ip, sizeof(ip)); 1528 log_query_in(ip, qinfo.qname, qinfo.qtype, qinfo.qclass); 1529 } 1530 if(qinfo.qtype == LDNS_RR_TYPE_AXFR || 1531 qinfo.qtype == LDNS_RR_TYPE_IXFR) { 1532 verbose(VERB_ALGO, "worker request: refused zone transfer."); 1533 log_addr(VERB_CLIENT, "from", &repinfo->client_addr, 1534 repinfo->client_addrlen); 1535 sldns_buffer_rewind(c->buffer); 1536 LDNS_QR_SET(sldns_buffer_begin(c->buffer)); 1537 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), 1538 LDNS_RCODE_REFUSED); 1539 if(worker->stats.extended) { 1540 worker->stats.qtype[qinfo.qtype]++; 1541 } 1542 goto send_reply; 1543 } 1544 if(qinfo.qtype == LDNS_RR_TYPE_OPT || 1545 qinfo.qtype == LDNS_RR_TYPE_TSIG || 1546 qinfo.qtype == LDNS_RR_TYPE_TKEY || 1547 qinfo.qtype == LDNS_RR_TYPE_MAILA || 1548 qinfo.qtype == LDNS_RR_TYPE_MAILB || 1549 (qinfo.qtype >= 128 && qinfo.qtype <= 248)) { 1550 verbose(VERB_ALGO, "worker request: formerror for meta-type."); 1551 log_addr(VERB_CLIENT, "from", &repinfo->client_addr, 1552 repinfo->client_addrlen); 1553 if(worker_err_ratelimit(worker, LDNS_RCODE_FORMERR) == -1) { 1554 comm_point_drop_reply(repinfo); 1555 return 0; 1556 } 1557 sldns_buffer_rewind(c->buffer); 1558 LDNS_QR_SET(sldns_buffer_begin(c->buffer)); 1559 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), 1560 LDNS_RCODE_FORMERR); 1561 if(worker->stats.extended) { 1562 worker->stats.qtype[qinfo.qtype]++; 1563 } 1564 goto send_reply; 1565 } 1566 if((ret=parse_edns_from_query_pkt( 1567 c->buffer, &edns, worker->env.cfg, c, repinfo, 1568 (worker->env.now ? *worker->env.now : time(NULL)), 1569 worker->scratchpad)) != 0) { 1570 struct edns_data reply_edns; 1571 verbose(VERB_ALGO, "worker parse edns: formerror."); 1572 log_addr(VERB_CLIENT, "from", &repinfo->client_addr, 1573 repinfo->client_addrlen); 1574 memset(&reply_edns, 0, sizeof(reply_edns)); 1575 reply_edns.edns_present = 1; 1576 error_encode(c->buffer, ret, &qinfo, 1577 *(uint16_t*)(void *)sldns_buffer_begin(c->buffer), 1578 sldns_buffer_read_u16_at(c->buffer, 2), &reply_edns); 1579 regional_free_all(worker->scratchpad); 1580 goto send_reply; 1581 } 1582 if(edns.edns_present) { 1583 if(edns.edns_version != 0) { 1584 edns.opt_list_in = NULL; 1585 edns.opt_list_out = NULL; 1586 edns.opt_list_inplace_cb_out = NULL; 1587 verbose(VERB_ALGO, "query with bad edns version."); 1588 log_addr(VERB_CLIENT, "from", &repinfo->client_addr, 1589 repinfo->client_addrlen); 1590 extended_error_encode(c->buffer, EDNS_RCODE_BADVERS, &qinfo, 1591 *(uint16_t*)(void *)sldns_buffer_begin(c->buffer), 1592 sldns_buffer_read_u16_at(c->buffer, 2), 0, &edns); 1593 regional_free_all(worker->scratchpad); 1594 goto send_reply; 1595 } 1596 if(edns.udp_size < NORMAL_UDP_SIZE && 1597 worker->daemon->cfg->harden_short_bufsize) { 1598 verbose(VERB_QUERY, "worker request: EDNS bufsize %d ignored", 1599 (int)edns.udp_size); 1600 log_addr(VERB_CLIENT, "from", &repinfo->client_addr, 1601 repinfo->client_addrlen); 1602 edns.udp_size = NORMAL_UDP_SIZE; 1603 } 1604 } 1605 1606 /* Get stats for cookies */ 1607 server_stats_downstream_cookie(&worker->stats, &edns); 1608 1609 /* If the IP rate limiting check was postponed, check now. */ 1610 if(!pre_edns_ip_ratelimit) { 1611 /* NOTE: we always check the repinfo->client_address. 1612 * IP ratelimiting is implicitly disabled for proxies. */ 1613 if(!check_ip_ratelimit(worker, &repinfo->client_addr, 1614 repinfo->client_addrlen, edns.cookie_valid, 1615 c->buffer)) { 1616 worker->stats.num_queries_ip_ratelimited++; 1617 comm_point_drop_reply(repinfo); 1618 return 0; 1619 } 1620 } 1621 1622 /* "if, else if" sequence below deals with downstream DNS Cookies */ 1623 if(acl != acl_allow_cookie) 1624 ; /* pass; No cookie downstream processing whatsoever */ 1625 1626 else if(edns.cookie_valid) 1627 ; /* pass; Valid cookie is good! */ 1628 1629 else if(c->type != comm_udp) 1630 ; /* pass; Stateful transport */ 1631 1632 else if(edns.cookie_present) { 1633 /* Cookie present, but not valid: Cookie was bad! */ 1634 extended_error_encode(c->buffer, 1635 LDNS_EXT_RCODE_BADCOOKIE, &qinfo, 1636 *(uint16_t*)(void *) 1637 sldns_buffer_begin(c->buffer), 1638 sldns_buffer_read_u16_at(c->buffer, 2), 1639 0, &edns); 1640 regional_free_all(worker->scratchpad); 1641 goto send_reply; 1642 } else { 1643 /* Cookie required, but no cookie present on UDP */ 1644 verbose(VERB_ALGO, "worker request: " 1645 "need cookie or stateful transport"); 1646 log_addr(VERB_ALGO, "from",&repinfo->remote_addr 1647 , repinfo->remote_addrlen); 1648 EDNS_OPT_LIST_APPEND_EDE(&edns.opt_list_out, 1649 worker->scratchpad, LDNS_EDE_OTHER, 1650 "DNS Cookie needed for UDP replies"); 1651 error_encode(c->buffer, 1652 (LDNS_RCODE_REFUSED|BIT_TC), &qinfo, 1653 *(uint16_t*)(void *) 1654 sldns_buffer_begin(c->buffer), 1655 sldns_buffer_read_u16_at(c->buffer, 2), 1656 &edns); 1657 regional_free_all(worker->scratchpad); 1658 goto send_reply; 1659 } 1660 1661 if(edns.udp_size > worker->daemon->cfg->max_udp_size && 1662 c->type == comm_udp) { 1663 verbose(VERB_QUERY, 1664 "worker request: max UDP reply size modified" 1665 " (%d to max-udp-size)", (int)edns.udp_size); 1666 log_addr(VERB_CLIENT, "from", &repinfo->client_addr, 1667 repinfo->client_addrlen); 1668 edns.udp_size = worker->daemon->cfg->max_udp_size; 1669 } 1670 if(edns.udp_size < LDNS_HEADER_SIZE) { 1671 verbose(VERB_ALGO, "worker request: edns is too small."); 1672 log_addr(VERB_CLIENT, "from", &repinfo->client_addr, 1673 repinfo->client_addrlen); 1674 LDNS_QR_SET(sldns_buffer_begin(c->buffer)); 1675 LDNS_TC_SET(sldns_buffer_begin(c->buffer)); 1676 LDNS_RCODE_SET(sldns_buffer_begin(c->buffer), 1677 LDNS_RCODE_SERVFAIL); 1678 sldns_buffer_set_position(c->buffer, LDNS_HEADER_SIZE); 1679 sldns_buffer_write_at(c->buffer, 4, 1680 (uint8_t*)"\0\0\0\0\0\0\0\0", 8); 1681 sldns_buffer_flip(c->buffer); 1682 regional_free_all(worker->scratchpad); 1683 goto send_reply; 1684 } 1685 if(worker->stats.extended) 1686 server_stats_insquery(&worker->stats, c, qinfo.qtype, 1687 qinfo.qclass, &edns, repinfo); 1688 if(c->type != comm_udp) 1689 edns.udp_size = 65535; /* max size for TCP replies */ 1690 if(qinfo.qclass == LDNS_RR_CLASS_CH && answer_chaos(worker, &qinfo, 1691 &edns, repinfo, c->buffer)) { 1692 regional_free_all(worker->scratchpad); 1693 goto send_reply; 1694 } 1695 if(LDNS_OPCODE_WIRE(sldns_buffer_begin(c->buffer)) == 1696 LDNS_PACKET_NOTIFY) { 1697 answer_notify(worker, &qinfo, &edns, c->buffer, 1698 &repinfo->client_addr, repinfo->client_addrlen); 1699 regional_free_all(worker->scratchpad); 1700 goto send_reply; 1701 } 1702 if(local_zones_answer(worker->daemon->local_zones, &worker->env, &qinfo, 1703 &edns, c->buffer, worker->scratchpad, repinfo, acladdr->taglist, 1704 acladdr->taglen, acladdr->tag_actions, 1705 acladdr->tag_actions_size, acladdr->tag_datas, 1706 acladdr->tag_datas_size, worker->daemon->cfg->tagname, 1707 worker->daemon->cfg->num_tags, acladdr->view)) { 1708 regional_free_all(worker->scratchpad); 1709 if(sldns_buffer_limit(c->buffer) == 0) { 1710 comm_point_drop_reply(repinfo); 1711 return 0; 1712 } 1713 goto send_reply; 1714 } 1715 if(worker->env.auth_zones && 1716 rpz_callback_from_worker_request(worker->env.auth_zones, 1717 &worker->env, &qinfo, &edns, c->buffer, worker->scratchpad, 1718 repinfo, acladdr->taglist, acladdr->taglen, &worker->stats, 1719 &rpz_passthru)) { 1720 regional_free_all(worker->scratchpad); 1721 if(sldns_buffer_limit(c->buffer) == 0) { 1722 comm_point_drop_reply(repinfo); 1723 return 0; 1724 } 1725 goto send_reply; 1726 } 1727 if(worker->env.auth_zones && 1728 auth_zones_answer(worker->env.auth_zones, &worker->env, 1729 &qinfo, &edns, repinfo, c->buffer, worker->scratchpad)) { 1730 regional_free_all(worker->scratchpad); 1731 if(sldns_buffer_limit(c->buffer) == 0) { 1732 comm_point_drop_reply(repinfo); 1733 return 0; 1734 } 1735 /* set RA for everyone that can have recursion (based on 1736 * access control list) */ 1737 if(LDNS_RD_WIRE(sldns_buffer_begin(c->buffer)) && 1738 acl != acl_deny_non_local && acl != acl_refuse_non_local) 1739 LDNS_RA_SET(sldns_buffer_begin(c->buffer)); 1740 goto send_reply; 1741 } 1742 1743 /* We've looked in our local zones. If the answer isn't there, we 1744 * might need to bail out based on ACLs now. */ 1745 if((ret=deny_refuse_non_local(c, acl, worker, repinfo, acladdr, 1746 worker->env.cfg->ede, &check_result)) != -1) 1747 { 1748 regional_free_all(worker->scratchpad); 1749 if(ret == 1) 1750 goto send_reply; 1751 return ret; 1752 } 1753 1754 /* If this request does not have the recursion bit set, verify 1755 * ACLs allow the recursion bit to be treated as set. */ 1756 if(!(LDNS_RD_WIRE(sldns_buffer_begin(c->buffer))) && 1757 acl == acl_allow_setrd ) { 1758 LDNS_RD_SET(sldns_buffer_begin(c->buffer)); 1759 } 1760 1761 /* If this request does not have the recursion bit set, verify 1762 * ACLs allow the snooping. */ 1763 if(!(LDNS_RD_WIRE(sldns_buffer_begin(c->buffer))) && 1764 acl != acl_allow_snoop ) { 1765 if(worker->env.cfg->ede) { 1766 EDNS_OPT_LIST_APPEND_EDE(&edns.opt_list_out, 1767 worker->scratchpad, LDNS_EDE_NOT_AUTHORITATIVE, ""); 1768 } 1769 error_encode(c->buffer, LDNS_RCODE_REFUSED, &qinfo, 1770 *(uint16_t*)(void *)sldns_buffer_begin(c->buffer), 1771 sldns_buffer_read_u16_at(c->buffer, 2), &edns); 1772 regional_free_all(worker->scratchpad); 1773 log_addr(VERB_ALGO, "refused nonrec (cache snoop) query from", 1774 &repinfo->client_addr, repinfo->client_addrlen); 1775 1776 goto send_reply; 1777 } 1778 1779 /* If we've found a local alias, replace the qname with the alias 1780 * target before resolving it. */ 1781 if(qinfo.local_alias) { 1782 struct ub_packed_rrset_key* rrset = qinfo.local_alias->rrset; 1783 struct packed_rrset_data* d = rrset->entry.data; 1784 1785 /* Sanity check: our current implementation only supports 1786 * a single CNAME RRset as a local alias. */ 1787 if(qinfo.local_alias->next || 1788 rrset->rk.type != htons(LDNS_RR_TYPE_CNAME) || 1789 d->count != 1) { 1790 log_err("assumption failure: unexpected local alias"); 1791 regional_free_all(worker->scratchpad); 1792 return 0; /* drop it */ 1793 } 1794 qinfo.qname = d->rr_data[0] + 2; 1795 qinfo.qname_len = d->rr_len[0] - 2; 1796 } 1797 1798 /* If we may apply IP-based actions to the answer, build the client 1799 * information. As this can be expensive, skip it if there is 1800 * absolutely no possibility of it. */ 1801 if((worker->daemon->use_response_ip || worker->daemon->use_rpz) && 1802 (qinfo.qtype == LDNS_RR_TYPE_A || 1803 qinfo.qtype == LDNS_RR_TYPE_AAAA || 1804 qinfo.qtype == LDNS_RR_TYPE_ANY)) { 1805 cinfo_tmp.taglist = acladdr->taglist; 1806 cinfo_tmp.taglen = acladdr->taglen; 1807 cinfo_tmp.tag_actions = acladdr->tag_actions; 1808 cinfo_tmp.tag_actions_size = acladdr->tag_actions_size; 1809 cinfo_tmp.tag_datas = acladdr->tag_datas; 1810 cinfo_tmp.tag_datas_size = acladdr->tag_datas_size; 1811 cinfo_tmp.view = acladdr->view; 1812 cinfo_tmp.respip_set = worker->daemon->respip_set; 1813 cinfo = &cinfo_tmp; 1814 } 1815 1816 /* Keep the original edns list around. The pointer could change if there is 1817 * a cached answer (through the inplace callback function there). 1818 * No need to actually copy the contents as they shouldn't change. 1819 * Used while prefetching and subnet is enabled. */ 1820 original_edns_list = edns.opt_list_in; 1821 lookup_cache: 1822 /* Lookup the cache. In case we chase an intermediate CNAME chain 1823 * this is a two-pass operation, and lookup_qinfo is different for 1824 * each pass. We should still pass the original qinfo to 1825 * answer_from_cache(), however, since it's used to build the reply. */ 1826 if(!edns_bypass_cache_stage(edns.opt_list_in, &worker->env)) { 1827 is_expired_answer = 0; 1828 is_secure_answer = 0; 1829 h = query_info_hash(lookup_qinfo, sldns_buffer_read_u16_at(c->buffer, 2)); 1830 if((e=slabhash_lookup(worker->env.msg_cache, h, lookup_qinfo, 0))) { 1831 struct reply_info* rep = (struct reply_info*)e->data; 1832 /* answer from cache - we have acquired a readlock on it */ 1833 if(answer_from_cache(worker, &qinfo, cinfo, &need_drop, 1834 &is_expired_answer, &is_secure_answer, 1835 &alias_rrset, &partial_rep, rep, 1836 *(uint16_t*)(void *)sldns_buffer_begin(c->buffer), 1837 sldns_buffer_read_u16_at(c->buffer, 2), repinfo, 1838 &edns)) { 1839 /* prefetch it if the prefetch TTL expired. 1840 * Note that if there is more than one pass 1841 * its qname must be that used for cache 1842 * lookup. */ 1843 if((worker->env.cfg->prefetch && 1844 *worker->env.now >= rep->prefetch_ttl) || 1845 (worker->env.cfg->serve_expired && 1846 *worker->env.now > rep->ttl)) { 1847 1848 time_t leeway = rep->ttl - *worker->env.now; 1849 if(rep->ttl < *worker->env.now) 1850 leeway = 0; 1851 lock_rw_unlock(&e->lock); 1852 1853 reply_and_prefetch(worker, lookup_qinfo, 1854 sldns_buffer_read_u16_at(c->buffer, 2), 1855 repinfo, leeway, 1856 (partial_rep || need_drop), 1857 rpz_passthru, 1858 original_edns_list); 1859 if(!partial_rep) { 1860 rc = 0; 1861 regional_free_all(worker->scratchpad); 1862 goto send_reply_rc; 1863 } 1864 } else if(!partial_rep) { 1865 lock_rw_unlock(&e->lock); 1866 regional_free_all(worker->scratchpad); 1867 goto send_reply; 1868 } else { 1869 /* Note that we've already released the 1870 * lock if we're here after prefetch. */ 1871 lock_rw_unlock(&e->lock); 1872 } 1873 /* We've found a partial reply ending with an 1874 * alias. Replace the lookup qinfo for the 1875 * alias target and lookup the cache again to 1876 * (possibly) complete the reply. As we're 1877 * passing the "base" reply, there will be no 1878 * more alias chasing. */ 1879 memset(&qinfo_tmp, 0, sizeof(qinfo_tmp)); 1880 get_cname_target(alias_rrset, &qinfo_tmp.qname, 1881 &qinfo_tmp.qname_len); 1882 if(!qinfo_tmp.qname) { 1883 log_err("unexpected: invalid answer alias"); 1884 regional_free_all(worker->scratchpad); 1885 return 0; /* drop query */ 1886 } 1887 qinfo_tmp.qtype = qinfo.qtype; 1888 qinfo_tmp.qclass = qinfo.qclass; 1889 lookup_qinfo = &qinfo_tmp; 1890 goto lookup_cache; 1891 } 1892 verbose(VERB_ALGO, "answer from the cache failed"); 1893 lock_rw_unlock(&e->lock); 1894 } 1895 1896 if(!LDNS_RD_WIRE(sldns_buffer_begin(c->buffer))) { 1897 if(answer_norec_from_cache(worker, &qinfo, 1898 *(uint16_t*)(void *)sldns_buffer_begin(c->buffer), 1899 sldns_buffer_read_u16_at(c->buffer, 2), repinfo, 1900 &edns)) { 1901 regional_free_all(worker->scratchpad); 1902 goto send_reply; 1903 } 1904 verbose(VERB_ALGO, "answer norec from cache -- " 1905 "need to validate or not primed"); 1906 } 1907 } 1908 sldns_buffer_rewind(c->buffer); 1909 server_stats_querymiss(&worker->stats, worker); 1910 1911 if(verbosity >= VERB_CLIENT) { 1912 if(c->type == comm_udp) 1913 log_addr(VERB_CLIENT, "udp request from", 1914 &repinfo->client_addr, repinfo->client_addrlen); 1915 else log_addr(VERB_CLIENT, "tcp request from", 1916 &repinfo->client_addr, repinfo->client_addrlen); 1917 } 1918 1919 /* grab a work request structure for this new request */ 1920 mesh_new_client(worker->env.mesh, &qinfo, cinfo, 1921 sldns_buffer_read_u16_at(c->buffer, 2), 1922 &edns, repinfo, *(uint16_t*)(void *)sldns_buffer_begin(c->buffer), 1923 rpz_passthru); 1924 regional_free_all(worker->scratchpad); 1925 worker_mem_report(worker, NULL); 1926 return 0; 1927 1928 send_reply: 1929 rc = 1; 1930 send_reply_rc: 1931 if(need_drop) { 1932 comm_point_drop_reply(repinfo); 1933 return 0; 1934 } 1935 if(is_expired_answer) { 1936 worker->stats.ans_expired++; 1937 } 1938 server_stats_insrcode(&worker->stats, c->buffer); 1939 if(worker->stats.extended) { 1940 if(is_secure_answer) worker->stats.ans_secure++; 1941 } 1942 #ifdef USE_DNSTAP 1943 /* 1944 * sending src (client)/dst (local service) addresses over DNSTAP from send_reply code label (when we serviced local zone for ex.) 1945 */ 1946 if(worker->dtenv.log_client_response_messages) { 1947 log_addr(VERB_ALGO, "from local addr", (void*)repinfo->c->socket->addr->ai_addr, repinfo->c->socket->addr->ai_addrlen); 1948 log_addr(VERB_ALGO, "response to client", &repinfo->client_addr, repinfo->client_addrlen); 1949 dt_msg_send_client_response(&worker->dtenv, &repinfo->client_addr, (void*)repinfo->c->socket->addr->ai_addr, c->type, c->ssl, c->buffer); 1950 } 1951 #endif 1952 if(worker->env.cfg->log_replies) 1953 { 1954 struct timeval tv; 1955 memset(&tv, 0, sizeof(tv)); 1956 if(qinfo.local_alias && qinfo.local_alias->rrset && 1957 qinfo.local_alias->rrset->rk.dname) { 1958 /* log original qname, before the local alias was 1959 * used to resolve that CNAME to something else */ 1960 qinfo.qname = qinfo.local_alias->rrset->rk.dname; 1961 log_reply_info(NO_VERBOSE, &qinfo, 1962 &repinfo->client_addr, repinfo->client_addrlen, 1963 tv, 1, c->buffer, 1964 (worker->env.cfg->log_destaddr?(void*)repinfo->c->socket->addr->ai_addr:NULL), 1965 c->type); 1966 } else { 1967 log_reply_info(NO_VERBOSE, &qinfo, 1968 &repinfo->client_addr, repinfo->client_addrlen, 1969 tv, 1, c->buffer, 1970 (worker->env.cfg->log_destaddr?(void*)repinfo->c->socket->addr->ai_addr:NULL), 1971 c->type); 1972 } 1973 } 1974 #ifdef USE_DNSCRYPT 1975 if(!dnsc_handle_uncurved_request(repinfo)) { 1976 return 0; 1977 } 1978 #endif 1979 return rc; 1980 } 1981 1982 void 1983 worker_sighandler(int sig, void* arg) 1984 { 1985 /* note that log, print, syscalls here give race conditions. 1986 * And cause hangups if the log-lock is held by the application. */ 1987 struct worker* worker = (struct worker*)arg; 1988 switch(sig) { 1989 #ifdef SIGHUP 1990 case SIGHUP: 1991 comm_base_exit(worker->base); 1992 break; 1993 #endif 1994 #ifdef SIGBREAK 1995 case SIGBREAK: 1996 #endif 1997 case SIGINT: 1998 worker->need_to_exit = 1; 1999 comm_base_exit(worker->base); 2000 break; 2001 #ifdef SIGQUIT 2002 case SIGQUIT: 2003 worker->need_to_exit = 1; 2004 comm_base_exit(worker->base); 2005 break; 2006 #endif 2007 case SIGTERM: 2008 worker->need_to_exit = 1; 2009 comm_base_exit(worker->base); 2010 break; 2011 default: 2012 /* unknown signal, ignored */ 2013 break; 2014 } 2015 } 2016 2017 /** restart statistics timer for worker, if enabled */ 2018 static void 2019 worker_restart_timer(struct worker* worker) 2020 { 2021 if(worker->env.cfg->stat_interval > 0) { 2022 struct timeval tv; 2023 #ifndef S_SPLINT_S 2024 tv.tv_sec = worker->env.cfg->stat_interval; 2025 tv.tv_usec = 0; 2026 #endif 2027 comm_timer_set(worker->stat_timer, &tv); 2028 } 2029 } 2030 2031 void worker_stat_timer_cb(void* arg) 2032 { 2033 struct worker* worker = (struct worker*)arg; 2034 server_stats_log(&worker->stats, worker, worker->thread_num); 2035 mesh_stats(worker->env.mesh, "mesh has"); 2036 worker_mem_report(worker, NULL); 2037 /* SHM is enabled, process data to SHM */ 2038 if (worker->daemon->cfg->shm_enable) { 2039 shm_main_run(worker); 2040 } 2041 if(!worker->daemon->cfg->stat_cumulative) { 2042 worker_stats_clear(worker); 2043 } 2044 /* start next timer */ 2045 worker_restart_timer(worker); 2046 } 2047 2048 void worker_probe_timer_cb(void* arg) 2049 { 2050 struct worker* worker = (struct worker*)arg; 2051 struct timeval tv; 2052 #ifndef S_SPLINT_S 2053 tv.tv_sec = (time_t)autr_probe_timer(&worker->env); 2054 tv.tv_usec = 0; 2055 #endif 2056 if(tv.tv_sec != 0) 2057 comm_timer_set(worker->env.probe_timer, &tv); 2058 } 2059 2060 struct worker* 2061 worker_create(struct daemon* daemon, int id, int* ports, int n) 2062 { 2063 unsigned int seed; 2064 struct worker* worker = (struct worker*)calloc(1, 2065 sizeof(struct worker)); 2066 if(!worker) 2067 return NULL; 2068 worker->numports = n; 2069 worker->ports = (int*)memdup(ports, sizeof(int)*n); 2070 if(!worker->ports) { 2071 free(worker); 2072 return NULL; 2073 } 2074 worker->daemon = daemon; 2075 worker->thread_num = id; 2076 if(!(worker->cmd = tube_create())) { 2077 free(worker->ports); 2078 free(worker); 2079 return NULL; 2080 } 2081 /* create random state here to avoid locking trouble in RAND_bytes */ 2082 if(!(worker->rndstate = ub_initstate(daemon->rand))) { 2083 log_err("could not init random numbers."); 2084 tube_delete(worker->cmd); 2085 free(worker->ports); 2086 free(worker); 2087 return NULL; 2088 } 2089 explicit_bzero(&seed, sizeof(seed)); 2090 return worker; 2091 } 2092 2093 int 2094 worker_init(struct worker* worker, struct config_file *cfg, 2095 struct listen_port* ports, int do_sigs) 2096 { 2097 #ifdef USE_DNSTAP 2098 struct dt_env* dtenv = &worker->dtenv; 2099 #else 2100 void* dtenv = NULL; 2101 #endif 2102 #ifdef HAVE_GETTID 2103 worker->thread_tid = gettid(); 2104 #endif 2105 worker->need_to_exit = 0; 2106 worker->base = comm_base_create(do_sigs); 2107 if(!worker->base) { 2108 log_err("could not create event handling base"); 2109 worker_delete(worker); 2110 return 0; 2111 } 2112 comm_base_set_slow_accept_handlers(worker->base, &worker_stop_accept, 2113 &worker_start_accept, worker); 2114 if(do_sigs) { 2115 #ifdef SIGHUP 2116 ub_thread_sig_unblock(SIGHUP); 2117 #endif 2118 #ifdef SIGBREAK 2119 ub_thread_sig_unblock(SIGBREAK); 2120 #endif 2121 ub_thread_sig_unblock(SIGINT); 2122 #ifdef SIGQUIT 2123 ub_thread_sig_unblock(SIGQUIT); 2124 #endif 2125 ub_thread_sig_unblock(SIGTERM); 2126 #ifndef LIBEVENT_SIGNAL_PROBLEM 2127 worker->comsig = comm_signal_create(worker->base, 2128 worker_sighandler, worker); 2129 if(!worker->comsig 2130 #ifdef SIGHUP 2131 || !comm_signal_bind(worker->comsig, SIGHUP) 2132 #endif 2133 #ifdef SIGQUIT 2134 || !comm_signal_bind(worker->comsig, SIGQUIT) 2135 #endif 2136 || !comm_signal_bind(worker->comsig, SIGTERM) 2137 #ifdef SIGBREAK 2138 || !comm_signal_bind(worker->comsig, SIGBREAK) 2139 #endif 2140 || !comm_signal_bind(worker->comsig, SIGINT)) { 2141 log_err("could not create signal handlers"); 2142 worker_delete(worker); 2143 return 0; 2144 } 2145 #endif /* LIBEVENT_SIGNAL_PROBLEM */ 2146 if(!daemon_remote_open_accept(worker->daemon->rc, 2147 worker->daemon->rc_ports, worker)) { 2148 worker_delete(worker); 2149 return 0; 2150 } 2151 #ifdef UB_ON_WINDOWS 2152 wsvc_setup_worker(worker); 2153 #endif /* UB_ON_WINDOWS */ 2154 } else { /* !do_sigs */ 2155 worker->comsig = NULL; 2156 } 2157 #ifdef USE_DNSTAP 2158 if(cfg->dnstap) { 2159 log_assert(worker->daemon->dtenv != NULL); 2160 memcpy(&worker->dtenv, worker->daemon->dtenv, sizeof(struct dt_env)); 2161 if(!dt_init(&worker->dtenv, worker->base)) 2162 fatal_exit("dt_init failed"); 2163 } 2164 #endif 2165 worker->front = listen_create(worker->base, ports, 2166 cfg->msg_buffer_size, (int)cfg->incoming_num_tcp, 2167 cfg->do_tcp_keepalive 2168 ? cfg->tcp_keepalive_timeout 2169 : cfg->tcp_idle_timeout, 2170 cfg->harden_large_queries, cfg->http_max_streams, 2171 cfg->http_endpoint, cfg->http_notls_downstream, 2172 worker->daemon->tcl, worker->daemon->listen_sslctx, 2173 dtenv, worker_handle_request, worker); 2174 if(!worker->front) { 2175 log_err("could not create listening sockets"); 2176 worker_delete(worker); 2177 return 0; 2178 } 2179 worker->back = outside_network_create(worker->base, 2180 cfg->msg_buffer_size, (size_t)cfg->outgoing_num_ports, 2181 cfg->out_ifs, cfg->num_out_ifs, cfg->do_ip4, cfg->do_ip6, 2182 cfg->do_tcp?cfg->outgoing_num_tcp:0, cfg->ip_dscp, 2183 worker->daemon->env->infra_cache, worker->rndstate, 2184 cfg->use_caps_bits_for_id, worker->ports, worker->numports, 2185 cfg->unwanted_threshold, cfg->outgoing_tcp_mss, 2186 &worker_alloc_cleanup, worker, 2187 cfg->do_udp || cfg->udp_upstream_without_downstream, 2188 worker->daemon->connect_sslctx, cfg->delay_close, 2189 cfg->tls_use_sni, dtenv, cfg->udp_connect, 2190 cfg->max_reuse_tcp_queries, cfg->tcp_reuse_timeout, 2191 cfg->tcp_auth_query_timeout); 2192 if(!worker->back) { 2193 log_err("could not create outgoing sockets"); 2194 worker_delete(worker); 2195 return 0; 2196 } 2197 iterator_set_ip46_support(&worker->daemon->mods, worker->daemon->env, 2198 worker->back); 2199 /* start listening to commands */ 2200 if(!tube_setup_bg_listen(worker->cmd, worker->base, 2201 &worker_handle_control_cmd, worker)) { 2202 log_err("could not create control compt."); 2203 worker_delete(worker); 2204 return 0; 2205 } 2206 worker->stat_timer = comm_timer_create(worker->base, 2207 worker_stat_timer_cb, worker); 2208 if(!worker->stat_timer) { 2209 log_err("could not create statistics timer"); 2210 } 2211 2212 /* we use the msg_buffer_size as a good estimate for what the 2213 * user wants for memory usage sizes */ 2214 worker->scratchpad = regional_create_custom(cfg->msg_buffer_size); 2215 if(!worker->scratchpad) { 2216 log_err("malloc failure"); 2217 worker_delete(worker); 2218 return 0; 2219 } 2220 2221 server_stats_init(&worker->stats, cfg); 2222 worker->alloc = worker->daemon->worker_allocs[worker->thread_num]; 2223 alloc_set_id_cleanup(worker->alloc, &worker_alloc_cleanup, worker); 2224 worker->env = *worker->daemon->env; 2225 comm_base_timept(worker->base, &worker->env.now, &worker->env.now_tv); 2226 worker->env.worker = worker; 2227 worker->env.worker_base = worker->base; 2228 worker->env.send_query = &worker_send_query; 2229 worker->env.alloc = worker->alloc; 2230 worker->env.outnet = worker->back; 2231 worker->env.rnd = worker->rndstate; 2232 /* If case prefetch is triggered, the corresponding mesh will clear 2233 * the scratchpad for the module env in the middle of request handling. 2234 * It would be prone to a use-after-free kind of bug, so we avoid 2235 * sharing it with worker's own scratchpad at the cost of having 2236 * one more pad per worker. */ 2237 worker->env.scratch = regional_create_custom(cfg->msg_buffer_size); 2238 if(!worker->env.scratch) { 2239 log_err("malloc failure"); 2240 worker_delete(worker); 2241 return 0; 2242 } 2243 worker->env.mesh = mesh_create(&worker->daemon->mods, &worker->env); 2244 if(!worker->env.mesh) { 2245 log_err("malloc failure"); 2246 worker_delete(worker); 2247 return 0; 2248 } 2249 /* Pass on daemon variables that we would need in the mesh area */ 2250 worker->env.mesh->use_response_ip = worker->daemon->use_response_ip; 2251 worker->env.mesh->use_rpz = worker->daemon->use_rpz; 2252 2253 worker->env.detach_subs = &mesh_detach_subs; 2254 worker->env.attach_sub = &mesh_attach_sub; 2255 worker->env.add_sub = &mesh_add_sub; 2256 worker->env.kill_sub = &mesh_state_delete; 2257 worker->env.detect_cycle = &mesh_detect_cycle; 2258 worker->env.scratch_buffer = sldns_buffer_new(cfg->msg_buffer_size); 2259 if(!worker->env.scratch_buffer) { 2260 log_err("malloc failure"); 2261 worker_delete(worker); 2262 return 0; 2263 } 2264 if(!(worker->env.fwds = forwards_create()) || 2265 !forwards_apply_cfg(worker->env.fwds, cfg)) { 2266 log_err("Could not set forward zones"); 2267 worker_delete(worker); 2268 return 0; 2269 } 2270 if(!(worker->env.hints = hints_create()) || 2271 !hints_apply_cfg(worker->env.hints, cfg)) { 2272 log_err("Could not set root or stub hints"); 2273 worker_delete(worker); 2274 return 0; 2275 } 2276 /* one probe timer per process -- if we have 5011 anchors */ 2277 if(autr_get_num_anchors(worker->env.anchors) > 0 2278 #ifndef THREADS_DISABLED 2279 && worker->thread_num == 0 2280 #endif 2281 ) { 2282 struct timeval tv; 2283 tv.tv_sec = 0; 2284 tv.tv_usec = 0; 2285 worker->env.probe_timer = comm_timer_create(worker->base, 2286 worker_probe_timer_cb, worker); 2287 if(!worker->env.probe_timer) { 2288 log_err("could not create 5011-probe timer"); 2289 } else { 2290 /* let timer fire, then it can reset itself */ 2291 comm_timer_set(worker->env.probe_timer, &tv); 2292 } 2293 } 2294 /* zone transfer tasks, setup once per process, if any */ 2295 if(worker->env.auth_zones 2296 #ifndef THREADS_DISABLED 2297 && worker->thread_num == 0 2298 #endif 2299 ) { 2300 auth_xfer_pickup_initial(worker->env.auth_zones, &worker->env); 2301 auth_zones_pickup_zonemd_verify(worker->env.auth_zones, 2302 &worker->env); 2303 } 2304 #ifdef USE_DNSTAP 2305 if(worker->daemon->cfg->dnstap 2306 #ifndef THREADS_DISABLED 2307 && worker->thread_num == 0 2308 #endif 2309 ) { 2310 if(!dt_io_thread_start(dtenv->dtio, comm_base_internal( 2311 worker->base), worker->daemon->num)) { 2312 log_err("could not start dnstap io thread"); 2313 worker_delete(worker); 2314 return 0; 2315 } 2316 } 2317 #endif /* USE_DNSTAP */ 2318 worker_mem_report(worker, NULL); 2319 /* if statistics enabled start timer */ 2320 if(worker->env.cfg->stat_interval > 0) { 2321 verbose(VERB_ALGO, "set statistics interval %d secs", 2322 worker->env.cfg->stat_interval); 2323 worker_restart_timer(worker); 2324 } 2325 pp_init(&sldns_write_uint16, &sldns_write_uint32); 2326 return 1; 2327 } 2328 2329 void 2330 worker_work(struct worker* worker) 2331 { 2332 comm_base_dispatch(worker->base); 2333 } 2334 2335 void 2336 worker_delete(struct worker* worker) 2337 { 2338 if(!worker) 2339 return; 2340 if(worker->env.mesh && verbosity >= VERB_OPS) { 2341 server_stats_log(&worker->stats, worker, worker->thread_num); 2342 mesh_stats(worker->env.mesh, "mesh has"); 2343 worker_mem_report(worker, NULL); 2344 } 2345 outside_network_quit_prepare(worker->back); 2346 mesh_delete(worker->env.mesh); 2347 sldns_buffer_free(worker->env.scratch_buffer); 2348 forwards_delete(worker->env.fwds); 2349 hints_delete(worker->env.hints); 2350 listen_delete(worker->front); 2351 outside_network_delete(worker->back); 2352 comm_signal_delete(worker->comsig); 2353 tube_delete(worker->cmd); 2354 comm_timer_delete(worker->stat_timer); 2355 comm_timer_delete(worker->env.probe_timer); 2356 free(worker->ports); 2357 if(worker->thread_num == 0) { 2358 #ifdef UB_ON_WINDOWS 2359 wsvc_desetup_worker(worker); 2360 #endif /* UB_ON_WINDOWS */ 2361 } 2362 #ifdef USE_DNSTAP 2363 if(worker->daemon->cfg->dnstap 2364 #ifndef THREADS_DISABLED 2365 && worker->thread_num == 0 2366 #endif 2367 ) { 2368 dt_io_thread_stop(worker->dtenv.dtio); 2369 } 2370 dt_deinit(&worker->dtenv); 2371 #endif /* USE_DNSTAP */ 2372 comm_base_delete(worker->base); 2373 ub_randfree(worker->rndstate); 2374 /* don't touch worker->alloc, as it's maintained in daemon */ 2375 regional_destroy(worker->env.scratch); 2376 regional_destroy(worker->scratchpad); 2377 free(worker); 2378 } 2379 2380 struct outbound_entry* 2381 worker_send_query(struct query_info* qinfo, uint16_t flags, int dnssec, 2382 int want_dnssec, int nocaps, int check_ratelimit, 2383 struct sockaddr_storage* addr, socklen_t addrlen, uint8_t* zone, 2384 size_t zonelen, int tcp_upstream, int ssl_upstream, char* tls_auth_name, 2385 struct module_qstate* q, int* was_ratelimited) 2386 { 2387 struct worker* worker = q->env->worker; 2388 struct outbound_entry* e = (struct outbound_entry*)regional_alloc( 2389 q->region, sizeof(*e)); 2390 if(!e) 2391 return NULL; 2392 e->qstate = q; 2393 e->qsent = outnet_serviced_query(worker->back, qinfo, flags, dnssec, 2394 want_dnssec, nocaps, check_ratelimit, tcp_upstream, 2395 ssl_upstream, tls_auth_name, addr, addrlen, zone, zonelen, q, 2396 worker_handle_service_reply, e, worker->back->udp_buff, q->env, 2397 was_ratelimited); 2398 if(!e->qsent) { 2399 return NULL; 2400 } 2401 return e; 2402 } 2403 2404 void 2405 worker_alloc_cleanup(void* arg) 2406 { 2407 struct worker* worker = (struct worker*)arg; 2408 slabhash_clear(&worker->env.rrset_cache->table); 2409 slabhash_clear(worker->env.msg_cache); 2410 } 2411 2412 void worker_stats_clear(struct worker* worker) 2413 { 2414 server_stats_init(&worker->stats, worker->env.cfg); 2415 mesh_stats_clear(worker->env.mesh); 2416 worker->back->unwanted_replies = 0; 2417 worker->back->num_tcp_outgoing = 0; 2418 worker->back->num_udp_outgoing = 0; 2419 } 2420 2421 void worker_start_accept(void* arg) 2422 { 2423 struct worker* worker = (struct worker*)arg; 2424 listen_start_accept(worker->front); 2425 if(worker->thread_num == 0) 2426 daemon_remote_start_accept(worker->daemon->rc); 2427 } 2428 2429 void worker_stop_accept(void* arg) 2430 { 2431 struct worker* worker = (struct worker*)arg; 2432 listen_stop_accept(worker->front); 2433 if(worker->thread_num == 0) 2434 daemon_remote_stop_accept(worker->daemon->rc); 2435 } 2436 2437 /* --- fake callbacks for fptr_wlist to work --- */ 2438 struct outbound_entry* libworker_send_query( 2439 struct query_info* ATTR_UNUSED(qinfo), 2440 uint16_t ATTR_UNUSED(flags), int ATTR_UNUSED(dnssec), 2441 int ATTR_UNUSED(want_dnssec), int ATTR_UNUSED(nocaps), 2442 int ATTR_UNUSED(check_ratelimit), 2443 struct sockaddr_storage* ATTR_UNUSED(addr), socklen_t ATTR_UNUSED(addrlen), 2444 uint8_t* ATTR_UNUSED(zone), size_t ATTR_UNUSED(zonelen), int ATTR_UNUSED(tcp_upstream), 2445 int ATTR_UNUSED(ssl_upstream), char* ATTR_UNUSED(tls_auth_name), 2446 struct module_qstate* ATTR_UNUSED(q), int* ATTR_UNUSED(was_ratelimited)) 2447 { 2448 log_assert(0); 2449 return 0; 2450 } 2451 2452 int libworker_handle_service_reply(struct comm_point* ATTR_UNUSED(c), 2453 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error), 2454 struct comm_reply* ATTR_UNUSED(reply_info)) 2455 { 2456 log_assert(0); 2457 return 0; 2458 } 2459 2460 void libworker_handle_control_cmd(struct tube* ATTR_UNUSED(tube), 2461 uint8_t* ATTR_UNUSED(buffer), size_t ATTR_UNUSED(len), 2462 int ATTR_UNUSED(error), void* ATTR_UNUSED(arg)) 2463 { 2464 log_assert(0); 2465 } 2466 2467 void libworker_fg_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode), 2468 sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s), 2469 char* ATTR_UNUSED(why_bogus), int ATTR_UNUSED(was_ratelimited)) 2470 { 2471 log_assert(0); 2472 } 2473 2474 void libworker_bg_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode), 2475 sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s), 2476 char* ATTR_UNUSED(why_bogus), int ATTR_UNUSED(was_ratelimited)) 2477 { 2478 log_assert(0); 2479 } 2480 2481 void libworker_event_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode), 2482 sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s), 2483 char* ATTR_UNUSED(why_bogus), int ATTR_UNUSED(was_ratelimited)) 2484 { 2485 log_assert(0); 2486 } 2487 2488 int context_query_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b)) 2489 { 2490 log_assert(0); 2491 return 0; 2492 } 2493 2494 int order_lock_cmp(const void* ATTR_UNUSED(e1), const void* ATTR_UNUSED(e2)) 2495 { 2496 log_assert(0); 2497 return 0; 2498 } 2499 2500 int codeline_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b)) 2501 { 2502 log_assert(0); 2503 return 0; 2504 } 2505 2506 #ifdef USE_DNSTAP 2507 void dtio_tap_callback(int ATTR_UNUSED(fd), short ATTR_UNUSED(ev), 2508 void* ATTR_UNUSED(arg)) 2509 { 2510 log_assert(0); 2511 } 2512 #endif 2513 2514 #ifdef USE_DNSTAP 2515 void dtio_mainfdcallback(int ATTR_UNUSED(fd), short ATTR_UNUSED(ev), 2516 void* ATTR_UNUSED(arg)) 2517 { 2518 log_assert(0); 2519 } 2520 #endif 2521