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