1 /* 2 * libunbound/worker.c - worker thread or process that resolves 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 contains the worker process or thread that performs 40 * the DNS resolving and validation. The worker is called by a procedure 41 * and if in the background continues until exit, if in the foreground 42 * returns from the procedure when done. 43 */ 44 #include "config.h" 45 #ifdef HAVE_SSL 46 #include <openssl/ssl.h> 47 #endif 48 #include "libunbound/libworker.h" 49 #include "libunbound/context.h" 50 #include "libunbound/unbound.h" 51 #include "libunbound/worker.h" 52 #include "libunbound/unbound-event.h" 53 #include "services/outside_network.h" 54 #include "services/mesh.h" 55 #include "services/localzone.h" 56 #include "services/cache/rrset.h" 57 #include "services/outbound_list.h" 58 #include "services/authzone.h" 59 #include "util/fptr_wlist.h" 60 #include "util/module.h" 61 #include "util/regional.h" 62 #include "util/random.h" 63 #include "util/config_file.h" 64 #include "util/netevent.h" 65 #include "util/storage/lookup3.h" 66 #include "util/storage/slabhash.h" 67 #include "util/net_help.h" 68 #include "util/data/dname.h" 69 #include "util/data/msgreply.h" 70 #include "util/data/msgencode.h" 71 #include "util/tube.h" 72 #include "iterator/iter_fwd.h" 73 #include "iterator/iter_hints.h" 74 #include "sldns/sbuffer.h" 75 #include "sldns/str2wire.h" 76 #ifdef USE_DNSTAP 77 #include "dnstap/dtstream.h" 78 #endif 79 80 #ifdef HAVE_TARGETCONDITIONALS_H 81 #include <TargetConditionals.h> 82 #endif 83 84 #if (defined(TARGET_OS_TV) && TARGET_OS_TV) || (defined(TARGET_OS_WATCH) && TARGET_OS_WATCH) 85 #undef HAVE_FORK 86 #endif 87 88 /** handle new query command for bg worker */ 89 static void handle_newq(struct libworker* w, uint8_t* buf, uint32_t len); 90 91 /** delete libworker env */ 92 static void 93 libworker_delete_env(struct libworker* w) 94 { 95 if(w->env) { 96 outside_network_quit_prepare(w->back); 97 mesh_delete(w->env->mesh); 98 context_release_alloc(w->ctx, w->env->alloc, 99 !w->is_bg || w->is_bg_thread); 100 sldns_buffer_free(w->env->scratch_buffer); 101 regional_destroy(w->env->scratch); 102 forwards_delete(w->env->fwds); 103 hints_delete(w->env->hints); 104 ub_randfree(w->env->rnd); 105 free(w->env); 106 } 107 #ifdef HAVE_SSL 108 SSL_CTX_free(w->sslctx); 109 #endif 110 outside_network_delete(w->back); 111 } 112 113 /** delete libworker struct */ 114 static void 115 libworker_delete(struct libworker* w) 116 { 117 if(!w) return; 118 libworker_delete_env(w); 119 comm_base_delete(w->base); 120 free(w); 121 } 122 123 void 124 libworker_delete_event(struct libworker* w) 125 { 126 if(!w) return; 127 libworker_delete_env(w); 128 comm_base_delete_no_base(w->base); 129 free(w); 130 } 131 132 /** setup fresh libworker struct */ 133 static struct libworker* 134 libworker_setup(struct ub_ctx* ctx, int is_bg, struct ub_event_base* eb) 135 { 136 struct libworker* w = (struct libworker*)calloc(1, sizeof(*w)); 137 struct config_file* cfg = ctx->env->cfg; 138 int* ports; 139 int numports; 140 if(!w) return NULL; 141 w->is_bg = is_bg; 142 w->ctx = ctx; 143 w->env = (struct module_env*)malloc(sizeof(*w->env)); 144 if(!w->env) { 145 free(w); 146 return NULL; 147 } 148 *w->env = *ctx->env; 149 w->env->alloc = context_obtain_alloc(ctx, !w->is_bg || w->is_bg_thread); 150 if(!w->env->alloc) { 151 libworker_delete(w); 152 return NULL; 153 } 154 w->thread_num = w->env->alloc->thread_num; 155 alloc_set_id_cleanup(w->env->alloc, &libworker_alloc_cleanup, w); 156 if(!w->is_bg || w->is_bg_thread) { 157 lock_basic_lock(&ctx->cfglock); 158 } 159 w->env->scratch = regional_create_custom(cfg->msg_buffer_size); 160 w->env->scratch_buffer = sldns_buffer_new(cfg->msg_buffer_size); 161 w->env->fwds = forwards_create(); 162 if(w->env->fwds && !forwards_apply_cfg(w->env->fwds, cfg)) { 163 forwards_delete(w->env->fwds); 164 w->env->fwds = NULL; 165 } 166 w->env->hints = hints_create(); 167 if(w->env->hints && !hints_apply_cfg(w->env->hints, cfg)) { 168 hints_delete(w->env->hints); 169 w->env->hints = NULL; 170 } 171 w->sslctx = connect_sslctx_create(NULL, NULL, 172 cfg->tls_cert_bundle, cfg->tls_win_cert); 173 if(!w->sslctx) { 174 /* to make the setup fail after unlock */ 175 hints_delete(w->env->hints); 176 w->env->hints = NULL; 177 } 178 if(!w->is_bg || w->is_bg_thread) { 179 lock_basic_unlock(&ctx->cfglock); 180 } 181 if(!w->env->scratch || !w->env->scratch_buffer || !w->env->fwds || 182 !w->env->hints) { 183 libworker_delete(w); 184 return NULL; 185 } 186 w->env->worker = (struct worker*)w; 187 w->env->probe_timer = NULL; 188 if(!w->is_bg || w->is_bg_thread) { 189 lock_basic_lock(&ctx->cfglock); 190 } 191 if(!(w->env->rnd = ub_initstate(ctx->seed_rnd))) { 192 if(!w->is_bg || w->is_bg_thread) { 193 lock_basic_unlock(&ctx->cfglock); 194 } 195 libworker_delete(w); 196 return NULL; 197 } 198 if(!w->is_bg || w->is_bg_thread) { 199 lock_basic_unlock(&ctx->cfglock); 200 } 201 if(1) { 202 /* primitive lockout for threading: if it overwrites another 203 * thread it is like wiping the cache (which is likely empty 204 * at the start) */ 205 /* note we are holding the ctx lock in normal threaded 206 * cases so that is solved properly, it is only for many ctx 207 * in different threads that this may clash */ 208 static int done_raninit = 0; 209 if(!done_raninit) { 210 done_raninit = 1; 211 hash_set_raninit((uint32_t)ub_random(w->env->rnd)); 212 } 213 } 214 215 if(eb) 216 w->base = comm_base_create_event(eb); 217 else w->base = comm_base_create(0); 218 if(!w->base) { 219 libworker_delete(w); 220 return NULL; 221 } 222 w->env->worker_base = w->base; 223 if(!w->is_bg || w->is_bg_thread) { 224 lock_basic_lock(&ctx->cfglock); 225 } 226 numports = cfg_condense_ports(cfg, &ports); 227 if(numports == 0) { 228 if(!w->is_bg || w->is_bg_thread) { 229 lock_basic_unlock(&ctx->cfglock); 230 } 231 libworker_delete(w); 232 return NULL; 233 } 234 w->back = outside_network_create(w->base, cfg->msg_buffer_size, 235 (size_t)cfg->outgoing_num_ports, cfg->out_ifs, 236 cfg->num_out_ifs, cfg->do_ip4, cfg->do_ip6, 237 cfg->do_tcp?cfg->outgoing_num_tcp:0, cfg->ip_dscp, 238 w->env->infra_cache, w->env->rnd, cfg->use_caps_bits_for_id, 239 ports, numports, cfg->unwanted_threshold, 240 cfg->outgoing_tcp_mss, &libworker_alloc_cleanup, w, 241 cfg->do_udp || cfg->udp_upstream_without_downstream, w->sslctx, 242 cfg->delay_close, cfg->tls_use_sni, NULL, cfg->udp_connect, 243 cfg->max_reuse_tcp_queries, cfg->tcp_reuse_timeout, 244 cfg->tcp_auth_query_timeout); 245 w->env->outnet = w->back; 246 if(!w->is_bg || w->is_bg_thread) { 247 lock_basic_unlock(&ctx->cfglock); 248 } 249 free(ports); 250 if(!w->back) { 251 libworker_delete(w); 252 return NULL; 253 } 254 w->env->mesh = mesh_create(&ctx->mods, w->env); 255 if(!w->env->mesh) { 256 libworker_delete(w); 257 return NULL; 258 } 259 w->env->send_query = &libworker_send_query; 260 w->env->detach_subs = &mesh_detach_subs; 261 w->env->attach_sub = &mesh_attach_sub; 262 w->env->add_sub = &mesh_add_sub; 263 w->env->kill_sub = &mesh_state_delete; 264 w->env->detect_cycle = &mesh_detect_cycle; 265 comm_base_timept(w->base, &w->env->now, &w->env->now_tv); 266 return w; 267 } 268 269 struct libworker* libworker_create_event(struct ub_ctx* ctx, 270 struct ub_event_base* eb) 271 { 272 return libworker_setup(ctx, 0, eb); 273 } 274 275 /** handle cancel command for bg worker */ 276 static void 277 handle_cancel(struct libworker* w, uint8_t* buf, uint32_t len) 278 { 279 struct ctx_query* q; 280 if(w->is_bg_thread) { 281 lock_basic_lock(&w->ctx->cfglock); 282 q = context_deserialize_cancel(w->ctx, buf, len); 283 lock_basic_unlock(&w->ctx->cfglock); 284 } else { 285 q = context_deserialize_cancel(w->ctx, buf, len); 286 } 287 if(!q) { 288 /* probably simply lookup failed, i.e. the message had been 289 * processed and answered before the cancel arrived */ 290 return; 291 } 292 q->cancelled = 1; 293 free(buf); 294 } 295 296 /** do control command coming into bg server */ 297 static void 298 libworker_do_cmd(struct libworker* w, uint8_t* msg, uint32_t len) 299 { 300 switch(context_serial_getcmd(msg, len)) { 301 default: 302 case UB_LIBCMD_ANSWER: 303 log_err("unknown command for bg worker %d", 304 (int)context_serial_getcmd(msg, len)); 305 /* and fall through to quit */ 306 /* fallthrough */ 307 case UB_LIBCMD_QUIT: 308 free(msg); 309 comm_base_exit(w->base); 310 break; 311 case UB_LIBCMD_NEWQUERY: 312 handle_newq(w, msg, len); 313 break; 314 case UB_LIBCMD_CANCEL: 315 handle_cancel(w, msg, len); 316 break; 317 } 318 } 319 320 /** handle control command coming into server */ 321 void 322 libworker_handle_control_cmd(struct tube* ATTR_UNUSED(tube), 323 uint8_t* msg, size_t len, int err, void* arg) 324 { 325 struct libworker* w = (struct libworker*)arg; 326 327 if(err != 0) { 328 free(msg); 329 /* it is of no use to go on, exit */ 330 comm_base_exit(w->base); 331 return; 332 } 333 libworker_do_cmd(w, msg, len); /* also frees the buf */ 334 } 335 336 /** the background thread func */ 337 static void* 338 libworker_dobg(void* arg) 339 { 340 /* setup */ 341 uint32_t m; 342 struct libworker* w = (struct libworker*)arg; 343 struct ub_ctx* ctx; 344 if(!w) { 345 log_err("libunbound bg worker init failed, nomem"); 346 return NULL; 347 } 348 ctx = w->ctx; 349 log_thread_set(&w->thread_num); 350 #ifdef THREADS_DISABLED 351 /* we are forked */ 352 w->is_bg_thread = 0; 353 /* close non-used parts of the pipes */ 354 tube_close_write(ctx->qq_pipe); 355 tube_close_read(ctx->rr_pipe); 356 #endif 357 if(!tube_setup_bg_listen(ctx->qq_pipe, w->base, 358 libworker_handle_control_cmd, w)) { 359 log_err("libunbound bg worker init failed, no bglisten"); 360 return NULL; 361 } 362 if(!tube_setup_bg_write(ctx->rr_pipe, w->base)) { 363 log_err("libunbound bg worker init failed, no bgwrite"); 364 return NULL; 365 } 366 367 /* do the work */ 368 comm_base_dispatch(w->base); 369 370 /* cleanup */ 371 m = UB_LIBCMD_QUIT; 372 w->want_quit = 1; 373 tube_remove_bg_listen(w->ctx->qq_pipe); 374 tube_remove_bg_write(w->ctx->rr_pipe); 375 libworker_delete(w); 376 (void)tube_write_msg(ctx->rr_pipe, (uint8_t*)&m, 377 (uint32_t)sizeof(m), 0); 378 #ifdef THREADS_DISABLED 379 /* close pipes from forked process before exit */ 380 tube_close_read(ctx->qq_pipe); 381 tube_close_write(ctx->rr_pipe); 382 #endif 383 return NULL; 384 } 385 386 int libworker_bg(struct ub_ctx* ctx) 387 { 388 struct libworker* w; 389 /* fork or threadcreate */ 390 lock_basic_lock(&ctx->cfglock); 391 if(ctx->dothread) { 392 lock_basic_unlock(&ctx->cfglock); 393 w = libworker_setup(ctx, 1, NULL); 394 if(!w) return UB_NOMEM; 395 w->is_bg_thread = 1; 396 ctx->thread_worker = w; 397 #ifdef ENABLE_LOCK_CHECKS 398 w->thread_num = 1; /* for nicer DEBUG checklocks */ 399 #endif 400 ub_thread_create(&ctx->bg_tid, libworker_dobg, w); 401 } else { 402 lock_basic_unlock(&ctx->cfglock); 403 #ifndef HAVE_FORK 404 /* no fork on windows */ 405 return UB_FORKFAIL; 406 #else /* HAVE_FORK */ 407 switch((ctx->bg_pid=fork())) { 408 case 0: 409 w = libworker_setup(ctx, 1, NULL); 410 if(!w) fatal_exit("out of memory"); 411 /* close non-used parts of the pipes */ 412 tube_close_write(ctx->qq_pipe); 413 tube_close_read(ctx->rr_pipe); 414 (void)libworker_dobg(w); 415 exit(0); 416 break; 417 case -1: 418 return UB_FORKFAIL; 419 default: 420 /* close non-used parts, so that the worker 421 * bgprocess gets 'pipe closed' when the 422 * main process exits */ 423 tube_close_read(ctx->qq_pipe); 424 tube_close_write(ctx->rr_pipe); 425 break; 426 } 427 #endif /* HAVE_FORK */ 428 } 429 return UB_NOERROR; 430 } 431 432 /** insert canonname */ 433 static int 434 fill_canon(struct ub_result* res, uint8_t* s) 435 { 436 char buf[255+2]; 437 dname_str(s, buf); 438 res->canonname = strdup(buf); 439 return res->canonname != 0; 440 } 441 442 /** fill data into result */ 443 static int 444 fill_res(struct ub_result* res, struct ub_packed_rrset_key* answer, 445 uint8_t* finalcname, struct query_info* rq, struct reply_info* rep) 446 { 447 size_t i; 448 struct packed_rrset_data* data; 449 res->ttl = 0; 450 if(!answer) { 451 if(finalcname) { 452 if(!fill_canon(res, finalcname)) 453 return 0; /* out of memory */ 454 } 455 if(rep->rrset_count != 0) 456 res->ttl = (int)rep->ttl; 457 res->data = (char**)calloc(1, sizeof(char*)); 458 if(!res->data) 459 return 0; /* out of memory */ 460 res->len = (int*)calloc(1, sizeof(int)); 461 if(!res->len) { 462 free(res->data); 463 res->data = NULL; 464 return 0; /* out of memory */ 465 } 466 return 1; 467 } 468 data = (struct packed_rrset_data*)answer->entry.data; 469 if(query_dname_compare(rq->qname, answer->rk.dname) != 0) { 470 if(!fill_canon(res, answer->rk.dname)) 471 return 0; /* out of memory */ 472 } else res->canonname = NULL; 473 res->data = (char**)calloc(data->count+1, sizeof(char*)); 474 if(!res->data) 475 return 0; /* out of memory */ 476 res->len = (int*)calloc(data->count+1, sizeof(int)); 477 if(!res->len) { 478 free(res->data); 479 res->data = NULL; 480 return 0; /* out of memory */ 481 } 482 for(i=0; i<data->count; i++) { 483 /* remove rdlength from rdata */ 484 res->len[i] = (int)(data->rr_len[i] - 2); 485 res->data[i] = memdup(data->rr_data[i]+2, (size_t)res->len[i]); 486 if(!res->data[i]) { 487 size_t j; 488 for(j=0; j<i; j++) { 489 free(res->data[j]); 490 res->data[j] = NULL; 491 } 492 free(res->data); 493 res->data = NULL; 494 free(res->len); 495 res->len = NULL; 496 return 0; /* out of memory */ 497 } 498 } 499 /* ttl for positive answers, from CNAME and answer RRs */ 500 if(data->count != 0) { 501 size_t j; 502 res->ttl = (int)data->ttl; 503 for(j=0; j<rep->an_numrrsets; j++) { 504 struct packed_rrset_data* d = 505 (struct packed_rrset_data*)rep->rrsets[j]-> 506 entry.data; 507 if((int)d->ttl < res->ttl) 508 res->ttl = (int)d->ttl; 509 } 510 } 511 /* ttl for negative answers */ 512 if(data->count == 0 && rep->rrset_count != 0) 513 res->ttl = (int)rep->ttl; 514 res->data[data->count] = NULL; 515 res->len[data->count] = 0; 516 return 1; 517 } 518 519 /** fill result from parsed message, on error fills servfail */ 520 void 521 libworker_enter_result(struct ub_result* res, sldns_buffer* buf, 522 struct regional* temp, enum sec_status msg_security) 523 { 524 struct query_info rq; 525 struct reply_info* rep; 526 res->rcode = LDNS_RCODE_SERVFAIL; 527 rep = parse_reply_in_temp_region(buf, temp, &rq); 528 if(!rep) { 529 log_err("cannot parse buf"); 530 return; /* error parsing buf, or out of memory */ 531 } 532 if(!fill_res(res, reply_find_answer_rrset(&rq, rep), 533 reply_find_final_cname_target(&rq, rep), &rq, rep)) 534 return; /* out of memory */ 535 /* rcode, havedata, nxdomain, secure, bogus */ 536 res->rcode = (int)FLAGS_GET_RCODE(rep->flags); 537 if(res->data && res->data[0]) 538 res->havedata = 1; 539 if(res->rcode == LDNS_RCODE_NXDOMAIN) 540 res->nxdomain = 1; 541 if(msg_security == sec_status_secure) 542 res->secure = 1; 543 if(msg_security == sec_status_bogus || 544 msg_security == sec_status_secure_sentinel_fail) 545 res->bogus = 1; 546 } 547 548 /** fillup fg results */ 549 static void 550 libworker_fillup_fg(struct ctx_query* q, int rcode, sldns_buffer* buf, 551 enum sec_status s, char* why_bogus, int was_ratelimited) 552 { 553 q->res->was_ratelimited = was_ratelimited; 554 if(why_bogus) 555 q->res->why_bogus = strdup(why_bogus); 556 if(rcode != 0) { 557 q->res->rcode = rcode; 558 q->msg_security = s; 559 return; 560 } 561 562 q->res->rcode = LDNS_RCODE_SERVFAIL; 563 q->msg_security = sec_status_unchecked; 564 q->msg = memdup(sldns_buffer_begin(buf), sldns_buffer_limit(buf)); 565 q->msg_len = sldns_buffer_limit(buf); 566 if(!q->msg) { 567 return; /* the error is in the rcode */ 568 } 569 570 /* canonname and results */ 571 q->msg_security = s; 572 libworker_enter_result(q->res, buf, q->w->env->scratch, s); 573 } 574 575 void 576 libworker_fg_done_cb(void* arg, int rcode, sldns_buffer* buf, enum sec_status s, 577 char* why_bogus, int was_ratelimited) 578 { 579 struct ctx_query* q = (struct ctx_query*)arg; 580 /* fg query is done; exit comm base */ 581 comm_base_exit(q->w->base); 582 583 libworker_fillup_fg(q, rcode, buf, s, why_bogus, was_ratelimited); 584 } 585 586 /** setup qinfo and edns */ 587 static int 588 setup_qinfo_edns(struct libworker* w, struct ctx_query* q, 589 struct query_info* qinfo, struct edns_data* edns) 590 { 591 qinfo->qtype = (uint16_t)q->res->qtype; 592 qinfo->qclass = (uint16_t)q->res->qclass; 593 qinfo->local_alias = NULL; 594 qinfo->qname = sldns_str2wire_dname(q->res->qname, &qinfo->qname_len); 595 if(!qinfo->qname) { 596 return 0; 597 } 598 edns->edns_present = 1; 599 edns->ext_rcode = 0; 600 edns->edns_version = 0; 601 edns->bits = EDNS_DO; 602 edns->opt_list_in = NULL; 603 edns->opt_list_out = NULL; 604 edns->opt_list_inplace_cb_out = NULL; 605 edns->padding_block_size = 0; 606 edns->cookie_present = 0; 607 edns->cookie_valid = 0; 608 if(sldns_buffer_capacity(w->back->udp_buff) < 65535) 609 edns->udp_size = (uint16_t)sldns_buffer_capacity( 610 w->back->udp_buff); 611 else edns->udp_size = 65535; 612 return 1; 613 } 614 615 int libworker_fg(struct ub_ctx* ctx, struct ctx_query* q) 616 { 617 struct libworker* w = libworker_setup(ctx, 0, NULL); 618 uint16_t qflags, qid; 619 struct query_info qinfo; 620 struct edns_data edns; 621 if(!w) 622 return UB_INITFAIL; 623 if(!setup_qinfo_edns(w, q, &qinfo, &edns)) { 624 libworker_delete(w); 625 return UB_SYNTAX; 626 } 627 qid = 0; 628 qflags = BIT_RD; 629 q->w = w; 630 /* see if there is a fixed answer */ 631 sldns_buffer_write_u16_at(w->back->udp_buff, 0, qid); 632 sldns_buffer_write_u16_at(w->back->udp_buff, 2, qflags); 633 if(local_zones_answer(ctx->local_zones, w->env, &qinfo, &edns, 634 w->back->udp_buff, w->env->scratch, NULL, NULL, 0, NULL, 0, 635 NULL, 0, NULL, 0, NULL)) { 636 regional_free_all(w->env->scratch); 637 libworker_fillup_fg(q, LDNS_RCODE_NOERROR, 638 w->back->udp_buff, sec_status_insecure, NULL, 0); 639 libworker_delete(w); 640 free(qinfo.qname); 641 return UB_NOERROR; 642 } 643 if(ctx->env->auth_zones && auth_zones_answer(ctx->env->auth_zones, 644 w->env, &qinfo, &edns, NULL, w->back->udp_buff, w->env->scratch)) { 645 regional_free_all(w->env->scratch); 646 libworker_fillup_fg(q, LDNS_RCODE_NOERROR, 647 w->back->udp_buff, sec_status_insecure, NULL, 0); 648 libworker_delete(w); 649 free(qinfo.qname); 650 return UB_NOERROR; 651 } 652 /* process new query */ 653 if(!mesh_new_callback(w->env->mesh, &qinfo, qflags, &edns, 654 w->back->udp_buff, qid, libworker_fg_done_cb, q, 0)) { 655 free(qinfo.qname); 656 return UB_NOMEM; 657 } 658 free(qinfo.qname); 659 660 /* wait for reply */ 661 comm_base_dispatch(w->base); 662 663 libworker_delete(w); 664 return UB_NOERROR; 665 } 666 667 void 668 libworker_event_done_cb(void* arg, int rcode, sldns_buffer* buf, 669 enum sec_status s, char* why_bogus, int was_ratelimited) 670 { 671 struct ctx_query* q = (struct ctx_query*)arg; 672 ub_event_callback_type cb = q->cb_event; 673 void* cb_arg = q->cb_arg; 674 int cancelled = q->cancelled; 675 676 /* delete it now */ 677 struct ub_ctx* ctx = q->w->ctx; 678 lock_basic_lock(&ctx->cfglock); 679 (void)rbtree_delete(&ctx->queries, q->node.key); 680 ctx->num_async--; 681 context_query_delete(q); 682 lock_basic_unlock(&ctx->cfglock); 683 684 if(!cancelled) { 685 /* call callback */ 686 int sec = 0; 687 if(s == sec_status_bogus) 688 sec = 1; 689 else if(s == sec_status_secure) 690 sec = 2; 691 (*cb)(cb_arg, rcode, (buf?(void*)sldns_buffer_begin(buf):NULL), 692 (buf?(int)sldns_buffer_limit(buf):0), sec, why_bogus, was_ratelimited); 693 } 694 } 695 696 int libworker_attach_mesh(struct ub_ctx* ctx, struct ctx_query* q, 697 int* async_id) 698 { 699 struct libworker* w = ctx->event_worker; 700 uint16_t qflags, qid; 701 struct query_info qinfo; 702 struct edns_data edns; 703 if(!w) 704 return UB_INITFAIL; 705 if(!setup_qinfo_edns(w, q, &qinfo, &edns)) 706 return UB_SYNTAX; 707 qid = 0; 708 qflags = BIT_RD; 709 q->w = w; 710 /* see if there is a fixed answer */ 711 sldns_buffer_write_u16_at(w->back->udp_buff, 0, qid); 712 sldns_buffer_write_u16_at(w->back->udp_buff, 2, qflags); 713 if(local_zones_answer(ctx->local_zones, w->env, &qinfo, &edns, 714 w->back->udp_buff, w->env->scratch, NULL, NULL, 0, NULL, 0, 715 NULL, 0, NULL, 0, NULL)) { 716 regional_free_all(w->env->scratch); 717 free(qinfo.qname); 718 libworker_event_done_cb(q, LDNS_RCODE_NOERROR, 719 w->back->udp_buff, sec_status_insecure, NULL, 0); 720 return UB_NOERROR; 721 } 722 if(ctx->env->auth_zones && auth_zones_answer(ctx->env->auth_zones, 723 w->env, &qinfo, &edns, NULL, w->back->udp_buff, w->env->scratch)) { 724 regional_free_all(w->env->scratch); 725 free(qinfo.qname); 726 libworker_event_done_cb(q, LDNS_RCODE_NOERROR, 727 w->back->udp_buff, sec_status_insecure, NULL, 0); 728 return UB_NOERROR; 729 } 730 /* process new query */ 731 if(async_id) 732 *async_id = q->querynum; 733 if(!mesh_new_callback(w->env->mesh, &qinfo, qflags, &edns, 734 w->back->udp_buff, qid, libworker_event_done_cb, q, 0)) { 735 free(qinfo.qname); 736 return UB_NOMEM; 737 } 738 free(qinfo.qname); 739 return UB_NOERROR; 740 } 741 742 /** add result to the bg worker result queue */ 743 static void 744 add_bg_result(struct libworker* w, struct ctx_query* q, sldns_buffer* pkt, 745 int err, char* reason, int was_ratelimited) 746 { 747 uint8_t* msg = NULL; 748 uint32_t len = 0; 749 750 if(w->want_quit) { 751 context_query_delete(q); 752 return; 753 } 754 /* serialize and delete unneeded q */ 755 if(w->is_bg_thread) { 756 lock_basic_lock(&w->ctx->cfglock); 757 if(reason) 758 q->res->why_bogus = strdup(reason); 759 q->res->was_ratelimited = was_ratelimited; 760 if(pkt) { 761 q->msg_len = sldns_buffer_remaining(pkt); 762 q->msg = memdup(sldns_buffer_begin(pkt), q->msg_len); 763 if(!q->msg) { 764 msg = context_serialize_answer(q, UB_NOMEM, NULL, &len); 765 } else { 766 msg = context_serialize_answer(q, err, NULL, &len); 767 } 768 } else { 769 msg = context_serialize_answer(q, err, NULL, &len); 770 } 771 lock_basic_unlock(&w->ctx->cfglock); 772 } else { 773 if(reason) 774 q->res->why_bogus = strdup(reason); 775 q->res->was_ratelimited = was_ratelimited; 776 msg = context_serialize_answer(q, err, pkt, &len); 777 (void)rbtree_delete(&w->ctx->queries, q->node.key); 778 w->ctx->num_async--; 779 context_query_delete(q); 780 } 781 782 if(!msg) { 783 log_err("out of memory for async answer"); 784 return; 785 } 786 if(!tube_queue_item(w->ctx->rr_pipe, msg, len)) { 787 log_err("out of memory for async answer"); 788 return; 789 } 790 } 791 792 void 793 libworker_bg_done_cb(void* arg, int rcode, sldns_buffer* buf, enum sec_status s, 794 char* why_bogus, int was_ratelimited) 795 { 796 struct ctx_query* q = (struct ctx_query*)arg; 797 798 if(q->cancelled || q->w->back->want_to_quit) { 799 if(q->w->is_bg_thread) { 800 /* delete it now */ 801 struct ub_ctx* ctx = q->w->ctx; 802 lock_basic_lock(&ctx->cfglock); 803 (void)rbtree_delete(&ctx->queries, q->node.key); 804 ctx->num_async--; 805 context_query_delete(q); 806 lock_basic_unlock(&ctx->cfglock); 807 } 808 /* cancelled, do not give answer */ 809 return; 810 } 811 q->msg_security = s; 812 if(!buf) { 813 buf = q->w->env->scratch_buffer; 814 } 815 if(rcode != 0) { 816 error_encode(buf, rcode, NULL, 0, BIT_RD, NULL); 817 } 818 add_bg_result(q->w, q, buf, UB_NOERROR, why_bogus, was_ratelimited); 819 } 820 821 822 /** handle new query command for bg worker */ 823 static void 824 handle_newq(struct libworker* w, uint8_t* buf, uint32_t len) 825 { 826 uint16_t qflags, qid; 827 struct query_info qinfo; 828 struct edns_data edns; 829 struct ctx_query* q; 830 if(w->is_bg_thread) { 831 lock_basic_lock(&w->ctx->cfglock); 832 q = context_lookup_new_query(w->ctx, buf, len); 833 lock_basic_unlock(&w->ctx->cfglock); 834 } else { 835 q = context_deserialize_new_query(w->ctx, buf, len); 836 } 837 free(buf); 838 if(!q) { 839 log_err("failed to deserialize newq"); 840 return; 841 } 842 if(!setup_qinfo_edns(w, q, &qinfo, &edns)) { 843 add_bg_result(w, q, NULL, UB_SYNTAX, NULL, 0); 844 return; 845 } 846 qid = 0; 847 qflags = BIT_RD; 848 /* see if there is a fixed answer */ 849 sldns_buffer_write_u16_at(w->back->udp_buff, 0, qid); 850 sldns_buffer_write_u16_at(w->back->udp_buff, 2, qflags); 851 if(local_zones_answer(w->ctx->local_zones, w->env, &qinfo, &edns, 852 w->back->udp_buff, w->env->scratch, NULL, NULL, 0, NULL, 0, 853 NULL, 0, NULL, 0, NULL)) { 854 regional_free_all(w->env->scratch); 855 q->msg_security = sec_status_insecure; 856 add_bg_result(w, q, w->back->udp_buff, UB_NOERROR, NULL, 0); 857 free(qinfo.qname); 858 return; 859 } 860 if(w->ctx->env->auth_zones && auth_zones_answer(w->ctx->env->auth_zones, 861 w->env, &qinfo, &edns, NULL, w->back->udp_buff, w->env->scratch)) { 862 regional_free_all(w->env->scratch); 863 q->msg_security = sec_status_insecure; 864 add_bg_result(w, q, w->back->udp_buff, UB_NOERROR, NULL, 0); 865 free(qinfo.qname); 866 return; 867 } 868 q->w = w; 869 /* process new query */ 870 if(!mesh_new_callback(w->env->mesh, &qinfo, qflags, &edns, 871 w->back->udp_buff, qid, libworker_bg_done_cb, q, 0)) { 872 add_bg_result(w, q, NULL, UB_NOMEM, NULL, 0); 873 } 874 free(qinfo.qname); 875 } 876 877 void libworker_alloc_cleanup(void* arg) 878 { 879 struct libworker* w = (struct libworker*)arg; 880 slabhash_clear(&w->env->rrset_cache->table); 881 slabhash_clear(w->env->msg_cache); 882 } 883 884 struct outbound_entry* libworker_send_query(struct query_info* qinfo, 885 uint16_t flags, int dnssec, int want_dnssec, int nocaps, 886 int check_ratelimit, 887 struct sockaddr_storage* addr, socklen_t addrlen, uint8_t* zone, 888 size_t zonelen, int tcp_upstream, int ssl_upstream, char* tls_auth_name, 889 struct module_qstate* q, int* was_ratelimited) 890 { 891 struct libworker* w = (struct libworker*)q->env->worker; 892 struct outbound_entry* e = (struct outbound_entry*)regional_alloc( 893 q->region, sizeof(*e)); 894 if(!e) 895 return NULL; 896 e->qstate = q; 897 e->qsent = outnet_serviced_query(w->back, qinfo, flags, dnssec, 898 want_dnssec, nocaps, check_ratelimit, tcp_upstream, ssl_upstream, 899 tls_auth_name, addr, addrlen, zone, zonelen, q, 900 libworker_handle_service_reply, e, w->back->udp_buff, q->env, 901 was_ratelimited); 902 if(!e->qsent) { 903 return NULL; 904 } 905 return e; 906 } 907 908 int 909 libworker_handle_service_reply(struct comm_point* c, void* arg, int error, 910 struct comm_reply* reply_info) 911 { 912 struct outbound_entry* e = (struct outbound_entry*)arg; 913 struct libworker* lw = (struct libworker*)e->qstate->env->worker; 914 915 if(error != 0) { 916 mesh_report_reply(lw->env->mesh, e, reply_info, error); 917 return 0; 918 } 919 /* sanity check. */ 920 if(!LDNS_QR_WIRE(sldns_buffer_begin(c->buffer)) 921 || LDNS_OPCODE_WIRE(sldns_buffer_begin(c->buffer)) != 922 LDNS_PACKET_QUERY 923 || LDNS_QDCOUNT(sldns_buffer_begin(c->buffer)) > 1) { 924 /* error becomes timeout for the module as if this reply 925 * never arrived. */ 926 mesh_report_reply(lw->env->mesh, e, reply_info, 927 NETEVENT_TIMEOUT); 928 return 0; 929 } 930 mesh_report_reply(lw->env->mesh, e, reply_info, NETEVENT_NOERROR); 931 return 0; 932 } 933 934 /* --- fake callbacks for fptr_wlist to work --- */ 935 void worker_handle_control_cmd(struct tube* ATTR_UNUSED(tube), 936 uint8_t* ATTR_UNUSED(buffer), size_t ATTR_UNUSED(len), 937 int ATTR_UNUSED(error), void* ATTR_UNUSED(arg)) 938 { 939 log_assert(0); 940 } 941 942 int worker_handle_request(struct comm_point* ATTR_UNUSED(c), 943 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error), 944 struct comm_reply* ATTR_UNUSED(repinfo)) 945 { 946 log_assert(0); 947 return 0; 948 } 949 950 int worker_handle_service_reply(struct comm_point* ATTR_UNUSED(c), 951 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error), 952 struct comm_reply* ATTR_UNUSED(reply_info)) 953 { 954 log_assert(0); 955 return 0; 956 } 957 958 int remote_accept_callback(struct comm_point* ATTR_UNUSED(c), 959 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error), 960 struct comm_reply* ATTR_UNUSED(repinfo)) 961 { 962 log_assert(0); 963 return 0; 964 } 965 966 int remote_control_callback(struct comm_point* ATTR_UNUSED(c), 967 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error), 968 struct comm_reply* ATTR_UNUSED(repinfo)) 969 { 970 log_assert(0); 971 return 0; 972 } 973 974 void worker_sighandler(int ATTR_UNUSED(sig), void* ATTR_UNUSED(arg)) 975 { 976 log_assert(0); 977 } 978 979 struct outbound_entry* worker_send_query(struct query_info* ATTR_UNUSED(qinfo), 980 uint16_t ATTR_UNUSED(flags), int ATTR_UNUSED(dnssec), 981 int ATTR_UNUSED(want_dnssec), int ATTR_UNUSED(nocaps), 982 int ATTR_UNUSED(check_ratelimit), 983 struct sockaddr_storage* ATTR_UNUSED(addr), socklen_t ATTR_UNUSED(addrlen), 984 uint8_t* ATTR_UNUSED(zone), size_t ATTR_UNUSED(zonelen), int ATTR_UNUSED(tcp_upstream), 985 int ATTR_UNUSED(ssl_upstream), char* ATTR_UNUSED(tls_auth_name), 986 struct module_qstate* ATTR_UNUSED(q), int* ATTR_UNUSED(was_ratelimited)) 987 { 988 log_assert(0); 989 return 0; 990 } 991 992 void 993 worker_alloc_cleanup(void* ATTR_UNUSED(arg)) 994 { 995 log_assert(0); 996 } 997 998 void worker_stat_timer_cb(void* ATTR_UNUSED(arg)) 999 { 1000 log_assert(0); 1001 } 1002 1003 void worker_probe_timer_cb(void* ATTR_UNUSED(arg)) 1004 { 1005 log_assert(0); 1006 } 1007 1008 void worker_start_accept(void* ATTR_UNUSED(arg)) 1009 { 1010 log_assert(0); 1011 } 1012 1013 void worker_stop_accept(void* ATTR_UNUSED(arg)) 1014 { 1015 log_assert(0); 1016 } 1017 1018 int order_lock_cmp(const void* ATTR_UNUSED(e1), const void* ATTR_UNUSED(e2)) 1019 { 1020 log_assert(0); 1021 return 0; 1022 } 1023 1024 int 1025 codeline_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b)) 1026 { 1027 log_assert(0); 1028 return 0; 1029 } 1030 1031 int replay_var_compare(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b)) 1032 { 1033 log_assert(0); 1034 return 0; 1035 } 1036 1037 void remote_get_opt_ssl(char* ATTR_UNUSED(str), void* ATTR_UNUSED(arg)) 1038 { 1039 log_assert(0); 1040 } 1041 1042 #ifdef UB_ON_WINDOWS 1043 void 1044 worker_win_stop_cb(int ATTR_UNUSED(fd), short ATTR_UNUSED(ev), void* 1045 ATTR_UNUSED(arg)) { 1046 log_assert(0); 1047 } 1048 1049 void 1050 wsvc_cron_cb(void* ATTR_UNUSED(arg)) 1051 { 1052 log_assert(0); 1053 } 1054 #endif /* UB_ON_WINDOWS */ 1055 1056 #ifdef USE_DNSTAP 1057 void dtio_tap_callback(int ATTR_UNUSED(fd), short ATTR_UNUSED(ev), 1058 void* ATTR_UNUSED(arg)) 1059 { 1060 log_assert(0); 1061 } 1062 #endif 1063 1064 #ifdef USE_DNSTAP 1065 void dtio_mainfdcallback(int ATTR_UNUSED(fd), short ATTR_UNUSED(ev), 1066 void* ATTR_UNUSED(arg)) 1067 { 1068 log_assert(0); 1069 } 1070 #endif 1071