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