1 /* 2 * util/module.h - DNS handling module interface 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 interface for DNS handling modules. 40 * 41 * The module interface uses the DNS modules as state machines. The 42 * state machines are activated in sequence to operate on queries. Once 43 * they are done, the reply is passed back. In the usual setup the mesh 44 * is the caller of the state machines and once things are done sends replies 45 * and invokes result callbacks. 46 * 47 * The module provides a number of functions, listed in the module_func_block. 48 * The module is inited and destroyed and memory usage queries, for the 49 * module as a whole, for entire-module state (such as a cache). And per-query 50 * functions are called, operate to move the state machine and cleanup of 51 * the per-query state. 52 * 53 * Most per-query state should simply be allocated in the query region. 54 * This is destroyed at the end of the query. 55 * 56 * The module environment contains services and information and caches 57 * shared by the modules and the rest of the system. It also contains 58 * function pointers for module-specific tasks (like sending queries). 59 * 60 * *** Example module calls for a normal query 61 * 62 * In this example, the query does not need recursion, all the other data 63 * can be found in the cache. This makes the example shorter. 64 * 65 * At the start of the program the iterator module is initialised. 66 * The iterator module sets up its global state, such as donotquery lists 67 * and private address trees. 68 * 69 * A query comes in, and a mesh entry is created for it. The mesh 70 * starts the resolution process. The validator module is the first 71 * in the list of modules, and it is started on this new query. The 72 * operate() function is called. The validator decides it needs not do 73 * anything yet until there is a result and returns wait_module, that 74 * causes the next module in the list to be started. 75 * 76 * The next module is the iterator. It is started on the passed query and 77 * decides to perform a lookup. For this simple example, the delegation 78 * point information is available, and all the iterator wants to do is 79 * send a UDP query. The iterator uses env.send_query() to send the 80 * query. Then the iterator suspends (returns from the operate call). 81 * 82 * When the UDP reply comes back (and on errors and timeouts), the 83 * operate function is called for the query, on the iterator module, 84 * with the event that there is a reply. The iterator decides that this 85 * is enough, the work is done. It returns the value finished from the 86 * operate call, which causes the previous module to be started. 87 * 88 * The previous module, the validator module, is started with the event 89 * that the iterator module is done. The validator decides to validate 90 * the query. Once it is done (which could take recursive lookups, but 91 * in this example no recursive lookups are needed), it returns from the 92 * operate function with finished. 93 * 94 * There is no previous module from the validator module, and the mesh 95 * takes this to mean that the query is finally done. The mesh invokes 96 * callbacks and sends packets to queriers. 97 * 98 * If other modules had been waiting (recursively) on the answer to this 99 * query, then the mesh will tell them about it. It calls the inform_super 100 * routine on all the waiting modules, and once that is done it calls all of 101 * them with the operate() call. During inform_super the query that is done 102 * still exists and information can be copied from it (but the module should 103 * not really re-entry codepoints and services). During the operate call 104 * the modules can use stored state to continue operation with the results. 105 * (network buffers are used to contain the answer packet during the 106 * inform_super phase, but after that the network buffers will be cleared 107 * of their contents so that other tasks can be performed). 108 * 109 * *** Example module calls for recursion 110 * 111 * A module is called in operate, and it decides that it wants to perform 112 * recursion. That is, it wants the full state-machine-list to operate on 113 * a different query. It calls env.attach_sub() to create a new query state. 114 * The routine returns the newly created state, and potentially the module 115 * can edit the module-states for the newly created query (i.e. pass along 116 * some information, like delegation points). The module then suspends, 117 * returns from the operate routine. 118 * 119 * The mesh meanwhile will have the newly created query (or queries) on 120 * a waiting list, and will call operate() on this query (or queries). 121 * It starts again at the start of the module list for them. The query 122 * (or queries) continue to operate their state machines, until they are 123 * done. When they are done the mesh calls inform_super on the module that 124 * wanted the recursion. After that the mesh calls operate() on the module 125 * that wanted to do the recursion, and during this phase the module could, 126 * for example, decide to create more recursions. 127 * 128 * If the module decides it no longer wants the recursive information 129 * it can call detach_subs. Those queries will still run to completion, 130 * potentially filling the cache with information. Inform_super is not 131 * called any more. 132 * 133 * The iterator module will fetch items from the cache, so a recursion 134 * attempt may complete very quickly if the item is in cache. The calling 135 * module has to wait for completion or eventual timeout. A recursive query 136 * that times out returns a servfail rcode (servfail is also returned for 137 * other errors during the lookup). 138 * 139 * Results are passed in the qstate, the rcode member is used to pass 140 * errors without requiring memory allocation, so that the code can continue 141 * in out-of-memory conditions. If the rcode member is 0 (NOERROR) then 142 * the dns_msg entry contains a filled out message. This message may 143 * also contain an rcode that is nonzero, but in this case additional 144 * information (query, additional) can be passed along. 145 * 146 * The rcode and dns_msg are used to pass the result from the rightmost 147 * module towards the leftmost modules and then towards the user. 148 * 149 * If you want to avoid recursion-cycles where queries need other queries 150 * that need the first one, use detect_cycle() to see if that will happen. 151 * 152 */ 153 154 #ifndef UTIL_MODULE_H 155 #define UTIL_MODULE_H 156 #include "util/storage/lruhash.h" 157 #include "util/data/msgreply.h" 158 #include "util/data/msgparse.h" 159 struct sldns_buffer; 160 struct alloc_cache; 161 struct rrset_cache; 162 struct key_cache; 163 struct config_file; 164 struct slabhash; 165 struct query_info; 166 struct edns_data; 167 struct regional; 168 struct worker; 169 struct comm_base; 170 struct auth_zones; 171 struct outside_network; 172 struct module_qstate; 173 struct ub_randstate; 174 struct mesh_area; 175 struct mesh_state; 176 struct val_anchors; 177 struct val_neg_cache; 178 struct iter_forwards; 179 struct iter_hints; 180 struct respip_set; 181 struct respip_client_info; 182 struct respip_addr_info; 183 struct module_stack; 184 185 /** Maximum number of modules in operation */ 186 #define MAX_MODULE 16 187 188 /** Maximum number of known edns options */ 189 #define MAX_KNOWN_EDNS_OPTS 256 190 191 struct errinf_strlist { 192 /** next item in list */ 193 struct errinf_strlist* next; 194 /** config option string */ 195 char* str; 196 /** EDE code companion to the error str */ 197 int reason_bogus; 198 }; 199 200 enum inplace_cb_list_type { 201 /* Inplace callbacks for when a resolved reply is ready to be sent to the 202 * front.*/ 203 inplace_cb_reply = 0, 204 /* Inplace callbacks for when a reply is given from the cache. */ 205 inplace_cb_reply_cache, 206 /* Inplace callbacks for when a reply is given with local data 207 * (or Chaos reply). */ 208 inplace_cb_reply_local, 209 /* Inplace callbacks for when the reply is servfail. */ 210 inplace_cb_reply_servfail, 211 /* Inplace callbacks for when a query is ready to be sent to the back.*/ 212 inplace_cb_query, 213 /* Inplace callback for when a reply is received from the back. */ 214 inplace_cb_query_response, 215 /* Inplace callback for when EDNS is parsed on a reply received from the 216 * back. */ 217 inplace_cb_edns_back_parsed, 218 /* Total number of types. Used for array initialization. 219 * Should always be last. */ 220 inplace_cb_types_total 221 }; 222 223 224 /** Known edns option. Can be populated during modules' init. */ 225 struct edns_known_option { 226 /** type of this edns option */ 227 uint16_t opt_code; 228 /** whether the option needs to bypass the cache stage */ 229 int bypass_cache_stage; 230 /** whether the option needs mesh aggregation */ 231 int no_aggregation; 232 }; 233 234 /** 235 * Inplace callback list of registered routines to be called. 236 */ 237 struct inplace_cb { 238 /** next in list */ 239 struct inplace_cb* next; 240 /** Inplace callback routine */ 241 void* cb; 242 void* cb_arg; 243 /** module id */ 244 int id; 245 }; 246 247 /** 248 * Inplace callback function called before replying. 249 * Called as func(qinfo, qstate, rep, rcode, edns, opt_list_out, repinfo, 250 * region, id, python_callback) 251 * Where: 252 * qinfo: the query info. 253 * qstate: the module state. NULL when calling before the query reaches the 254 * mesh states. 255 * rep: reply_info. Could be NULL. 256 * rcode: the return code. 257 * edns: the edns_data of the reply. When qstate is NULL, it is also used as 258 * the edns input. 259 * opt_list_out: the edns options list for the reply. 260 * repinfo: reply information for a communication point. NULL when calling 261 * during the mesh states; the same could be found from 262 * qstate->mesh_info->reply_list. 263 * region: region to store data. 264 * id: module id. 265 * python_callback: only used for registering a python callback function. 266 */ 267 typedef int inplace_cb_reply_func_type(struct query_info* qinfo, 268 struct module_qstate* qstate, struct reply_info* rep, int rcode, 269 struct edns_data* edns, struct edns_option** opt_list_out, 270 struct comm_reply* repinfo, struct regional* region, 271 struct timeval* start_time, int id, void* callback); 272 273 /** 274 * Inplace callback function called before sending the query to a nameserver. 275 * Called as func(qinfo, flags, qstate, addr, addrlen, zone, zonelen, region, 276 * id, python_callback) 277 * Where: 278 * qinfo: query info. 279 * flags: flags of the query. 280 * qstate: query state. 281 * addr: to which server to send the query. 282 * addrlen: length of addr. 283 * zone: name of the zone of the delegation point. wireformat dname. 284 * This is the delegation point name for which the server is deemed 285 * authoritative. 286 * zonelen: length of zone. 287 * region: region to store data. 288 * id: module id. 289 * python_callback: only used for registering a python callback function. 290 */ 291 typedef int inplace_cb_query_func_type(struct query_info* qinfo, uint16_t flags, 292 struct module_qstate* qstate, struct sockaddr_storage* addr, 293 socklen_t addrlen, uint8_t* zone, size_t zonelen, struct regional* region, 294 int id, void* callback); 295 296 /** 297 * Inplace callback function called after parsing edns on query reply. 298 * Called as func(qstate, id, cb_args) 299 * Where: 300 * qstate: the query state. 301 * id: module id. 302 * cb_args: argument passed when registering callback. 303 */ 304 typedef int inplace_cb_edns_back_parsed_func_type(struct module_qstate* qstate, 305 int id, void* cb_args); 306 307 /** 308 * Inplace callback function called after parsing query response. 309 * Called as func(qstate, response, id, cb_args) 310 * Where: 311 * qstate: the query state. 312 * response: query response. 313 * id: module id. 314 * cb_args: argument passed when registering callback. 315 */ 316 typedef int inplace_cb_query_response_func_type(struct module_qstate* qstate, 317 struct dns_msg* response, int id, void* cb_args); 318 319 /** 320 * Function called when looking for (expired) cached answers during the serve 321 * expired logic. 322 * Called as func(qstate, lookup_qinfo, &is_expired) 323 * Where: 324 * qstate: the query state. 325 * lookup_qinfo: the qinfo to lookup for. 326 * is_expired: set if the cached answer is expired. 327 */ 328 typedef struct dns_msg* serve_expired_lookup_func_type( 329 struct module_qstate* qstate, struct query_info* lookup_qinfo, 330 int* is_expired); 331 332 /** 333 * Module environment. 334 * Services and data provided to the module. 335 */ 336 struct module_env { 337 /* --- data --- */ 338 /** config file with config options */ 339 struct config_file* cfg; 340 /** shared message cache */ 341 struct slabhash* msg_cache; 342 /** shared rrset cache */ 343 struct rrset_cache* rrset_cache; 344 /** shared infrastructure cache (edns, lameness) */ 345 struct infra_cache* infra_cache; 346 /** shared key cache */ 347 struct key_cache* key_cache; 348 349 /* --- services --- */ 350 /** 351 * Send serviced DNS query to server. UDP/TCP and EDNS is handled. 352 * operate() should return with wait_reply. Later on a callback 353 * will cause operate() to be called with event timeout or reply. 354 * The time until a timeout is calculated from roundtrip timing, 355 * several UDP retries are attempted. 356 * @param qinfo: query info. 357 * @param flags: host order flags word, with opcode and CD bit. 358 * @param dnssec: if set, EDNS record will have bits set. 359 * If EDNS_DO bit is set, DO bit is set in EDNS records. 360 * If BIT_CD is set, CD bit is set in queries with EDNS records. 361 * @param want_dnssec: if set, the validator wants DNSSEC. Without 362 * EDNS, the answer is likely to be useless for this domain. 363 * @param nocaps: do not use caps_for_id, use the qname as given. 364 * (ignored if caps_for_id is disabled). 365 * @param check_ratelimit: if set, will check ratelimit before sending out. 366 * @param addr: where to. 367 * @param addrlen: length of addr. 368 * @param zone: delegation point name. 369 * @param zonelen: length of zone name. 370 * @param tcp_upstream: use TCP for upstream queries. 371 * @param ssl_upstream: use SSL for upstream queries. 372 * @param tls_auth_name: if ssl_upstream, use this name with TLS 373 * authentication. 374 * @param q: which query state to reactivate upon return. 375 * @param was_ratelimited: it will signal back if the query failed to pass the 376 * ratelimit check. 377 * @return: false on failure (memory or socket related). no query was 378 * sent. Or returns an outbound entry with qsent and qstate set. 379 * This outbound_entry will be used on later module invocations 380 * that involve this query (timeout, error or reply). 381 */ 382 struct outbound_entry* (*send_query)(struct query_info* qinfo, 383 uint16_t flags, int dnssec, int want_dnssec, int nocaps, 384 int check_ratelimit, 385 struct sockaddr_storage* addr, socklen_t addrlen, 386 uint8_t* zone, size_t zonelen, int tcp_upstream, int ssl_upstream, 387 char* tls_auth_name, struct module_qstate* q, int* was_ratelimited); 388 389 /** 390 * Detach-subqueries. 391 * Remove all sub-query references from this query state. 392 * Keeps super-references of those sub-queries correct. 393 * Updates stat items in mesh_area structure. 394 * @param qstate: used to find mesh state. 395 */ 396 void (*detach_subs)(struct module_qstate* qstate); 397 398 /** 399 * Attach subquery. 400 * Creates it if it does not exist already. 401 * Keeps sub and super references correct. 402 * Updates stat items in mesh_area structure. 403 * Pass if it is priming query or not. 404 * return: 405 * o if error (malloc) happened. 406 * o need to initialise the new state (module init; it is a new state). 407 * so that the next run of the query with this module is successful. 408 * o no init needed, attachment successful. 409 * 410 * @param qstate: the state to find mesh state, and that wants to 411 * receive the results from the new subquery. 412 * @param qinfo: what to query for (copied). 413 * @param qflags: what flags to use (RD, CD flag or not). 414 * @param prime: if it is a (stub) priming query. 415 * @param valrec: validation lookup recursion, does not need validation 416 * @param newq: If the new subquery needs initialisation, it is 417 * returned, otherwise NULL is returned. 418 * @return: false on error, true if success (and init may be needed). 419 */ 420 int (*attach_sub)(struct module_qstate* qstate, 421 struct query_info* qinfo, uint16_t qflags, int prime, 422 int valrec, struct module_qstate** newq); 423 424 /** 425 * Add detached query. 426 * Creates it if it does not exist already. 427 * Does not make super/sub references. 428 * Performs a cycle detection - for double check - and fails if there is 429 * one. 430 * Updates stat items in mesh_area structure. 431 * Pass if it is priming query or not. 432 * return: 433 * o if error (malloc) happened. 434 * o need to initialise the new state (module init; it is a new state). 435 * so that the next run of the query with this module is successful. 436 * o no init needed, attachment successful. 437 * o added subquery, created if it did not exist already. 438 * 439 * @param qstate: the state to find mesh state, and that wants to receive 440 * the results from the new subquery. 441 * @param qinfo: what to query for (copied). 442 * @param qflags: what flags to use (RD / CD flag or not). 443 * @param prime: if it is a (stub) priming query. 444 * @param valrec: if it is a validation recursion query (lookup of key, DS). 445 * @param newq: If the new subquery needs initialisation, it is returned, 446 * otherwise NULL is returned. 447 * @param sub: The added mesh state, created if it did not exist already. 448 * @return: false on error, true if success (and init may be needed). 449 */ 450 int (*add_sub)(struct module_qstate* qstate, 451 struct query_info* qinfo, uint16_t qflags, int prime, 452 int valrec, struct module_qstate** newq, 453 struct mesh_state** sub); 454 455 /** 456 * Kill newly attached sub. If attach_sub returns newq for 457 * initialisation, but that fails, then this routine will cleanup and 458 * delete the freshly created sub. 459 * @param newq: the new subquery that is no longer needed. 460 * It is removed. 461 */ 462 void (*kill_sub)(struct module_qstate* newq); 463 464 /** 465 * Detect if adding a dependency for qstate on name,type,class will 466 * create a dependency cycle. 467 * @param qstate: given mesh querystate. 468 * @param qinfo: query info for dependency. 469 * @param flags: query flags of dependency, RD/CD flags. 470 * @param prime: if dependency is a priming query or not. 471 * @param valrec: validation lookup recursion, does not need validation 472 * @return true if the name,type,class exists and the given 473 * qstate mesh exists as a dependency of that name. Thus 474 * if qstate becomes dependent on name,type,class then a 475 * cycle is created. 476 */ 477 int (*detect_cycle)(struct module_qstate* qstate, 478 struct query_info* qinfo, uint16_t flags, int prime, 479 int valrec); 480 481 /** region for temporary usage. May be cleared after operate() call. */ 482 struct regional* scratch; 483 /** buffer for temporary usage. May be cleared after operate() call. */ 484 struct sldns_buffer* scratch_buffer; 485 /** internal data for daemon - worker thread. */ 486 struct worker* worker; 487 /** the worker event base */ 488 struct comm_base* worker_base; 489 /** the outside network */ 490 struct outside_network* outnet; 491 /** mesh area with query state dependencies */ 492 struct mesh_area* mesh; 493 /** allocation service */ 494 struct alloc_cache* alloc; 495 /** random table to generate random numbers */ 496 struct ub_randstate* rnd; 497 /** time in seconds, converted to integer */ 498 time_t* now; 499 /** time in microseconds. Relatively recent. */ 500 struct timeval* now_tv; 501 /** is validation required for messages, controls client-facing 502 * validation status (AD bits) and servfails */ 503 int need_to_validate; 504 /** trusted key storage; these are the configured keys, if not NULL, 505 * otherwise configured by validator. These are the trust anchors, 506 * and are not primed and ready for validation, but on the bright 507 * side, they are read only memory, thus no locks and fast. */ 508 struct val_anchors* anchors; 509 /** negative cache, configured by the validator. if not NULL, 510 * contains NSEC record lookup trees. */ 511 struct val_neg_cache* neg_cache; 512 /** the 5011-probe timer (if any) */ 513 struct comm_timer* probe_timer; 514 /** auth zones */ 515 struct auth_zones* auth_zones; 516 /** Mapping of forwarding zones to targets. 517 * iterator forwarder information. */ 518 struct iter_forwards* fwds; 519 /** 520 * iterator stub information. 521 * The hints -- these aren't stored in the cache because they don't 522 * expire. The hints are always used to "prime" the cache. Note 523 * that both root hints and stub zone "hints" are stored in this 524 * data structure. 525 */ 526 struct iter_hints* hints; 527 /** module specific data. indexed by module id. */ 528 void* modinfo[MAX_MODULE]; 529 530 /* Shared linked list of inplace callback functions */ 531 struct inplace_cb* inplace_cb_lists[inplace_cb_types_total]; 532 533 /** 534 * Shared array of known edns options (size MAX_KNOWN_EDNS_OPTS). 535 * Filled by edns literate modules during init. 536 */ 537 struct edns_known_option* edns_known_options; 538 /* Number of known edns options */ 539 size_t edns_known_options_num; 540 /** EDNS client string information */ 541 struct edns_strings* edns_strings; 542 543 /** module stack */ 544 struct module_stack* modstack; 545 #ifdef USE_CACHEDB 546 /** the cachedb enabled value, copied and stored here. */ 547 int cachedb_enabled; 548 #endif 549 /* Make every mesh state unique, do not aggregate mesh states. */ 550 int unique_mesh; 551 }; 552 553 /** 554 * External visible states of the module state machine 555 * Modules may also have an internal state. 556 * Modules are supposed to run to completion or until blocked. 557 */ 558 enum module_ext_state { 559 /** initial state - new query */ 560 module_state_initial = 0, 561 /** waiting for reply to outgoing network query */ 562 module_wait_reply, 563 /** module is waiting for another module */ 564 module_wait_module, 565 /** module is waiting for another module; that other is restarted */ 566 module_restart_next, 567 /** module is waiting for sub-query */ 568 module_wait_subquery, 569 /** module could not finish the query */ 570 module_error, 571 /** module is finished with query */ 572 module_finished 573 }; 574 575 /** 576 * Events that happen to modules, that start or wakeup modules. 577 */ 578 enum module_ev { 579 /** new query */ 580 module_event_new = 0, 581 /** query passed by other module */ 582 module_event_pass, 583 /** reply inbound from server */ 584 module_event_reply, 585 /** no reply, timeout or other error */ 586 module_event_noreply, 587 /** reply is there, but capitalisation check failed */ 588 module_event_capsfail, 589 /** next module is done, and its reply is awaiting you */ 590 module_event_moddone, 591 /** error */ 592 module_event_error 593 }; 594 595 /** 596 * Linked list of sockaddrs 597 * May be allocated such that only 'len' bytes of addr exist for the structure. 598 */ 599 struct sock_list { 600 /** next in list */ 601 struct sock_list* next; 602 /** length of addr */ 603 socklen_t len; 604 /** sockaddr */ 605 struct sockaddr_storage addr; 606 }; 607 608 struct respip_action_info; 609 610 /** 611 * Struct to hold relevant data for serve expired 612 */ 613 struct serve_expired_data { 614 struct comm_timer* timer; 615 serve_expired_lookup_func_type* get_cached_answer; 616 }; 617 618 /** 619 * Module state, per query. 620 */ 621 struct module_qstate { 622 /** which query is being answered: name, type, class */ 623 struct query_info qinfo; 624 /** flags uint16 from query */ 625 uint16_t query_flags; 626 /** if this is a (stub or root) priming query (with hints) */ 627 int is_priming; 628 /** if this is a validation recursion query that does not get 629 * validation itself */ 630 int is_valrec; 631 #ifdef CLIENT_SUBNET 632 /** the client network address is needed for the client-subnet option 633 * when prefetching, but we can't use reply_list in mesh_info, because 634 * we don't want to send a reply for the internal query. */ 635 struct sockaddr_storage client_addr; 636 #endif 637 638 /** comm_reply contains server replies */ 639 struct comm_reply* reply; 640 /** the reply message, with message for client and calling module */ 641 struct dns_msg* return_msg; 642 /** the rcode, in case of error, instead of a reply message */ 643 int return_rcode; 644 /** origin of the reply (can be NULL from cache, list for cnames) */ 645 struct sock_list* reply_origin; 646 /** IP blacklist for queries */ 647 struct sock_list* blacklist; 648 /** region for this query. Cleared when query process finishes. */ 649 struct regional* region; 650 /** failure reason information if val-log-level is high */ 651 struct errinf_strlist* errinf; 652 /** which module is executing */ 653 int curmod; 654 /** module states */ 655 enum module_ext_state ext_state[MAX_MODULE]; 656 /** module specific data for query. indexed by module id. */ 657 void* minfo[MAX_MODULE]; 658 /** environment for this query */ 659 struct module_env* env; 660 /** mesh related information for this query */ 661 struct mesh_state* mesh_info; 662 /** how many seconds before expiry is this prefetched (0 if not) */ 663 time_t prefetch_leeway; 664 /** serve expired data */ 665 struct serve_expired_data* serve_expired_data; 666 667 /** incoming edns options from the front end */ 668 struct edns_option* edns_opts_front_in; 669 /** outgoing edns options to the back end */ 670 struct edns_option* edns_opts_back_out; 671 /** incoming edns options from the back end */ 672 struct edns_option* edns_opts_back_in; 673 /** outgoing edns options to the front end */ 674 struct edns_option* edns_opts_front_out; 675 /** whether modules should answer from the cache */ 676 int no_cache_lookup; 677 /** whether modules should store answer in the cache */ 678 int no_cache_store; 679 /** whether to refetch a fresh answer on finishing this state*/ 680 int need_refetch; 681 /** whether the query (or a subquery) was ratelimited */ 682 int was_ratelimited; 683 /** time when query was started. This is when the qstate is created. 684 * This is used so that type NS data cannot be overwritten by them 685 * expiring while the lookup is in progress, using data fetched from 686 * those servers. By comparing expiry time with qstarttime for type NS. 687 */ 688 time_t qstarttime; 689 /** whether a message from cachedb will be used for the reply */ 690 int is_cachedb_answer; 691 692 /** 693 * Attributes of clients that share the qstate that may affect IP-based 694 * actions. 695 */ 696 struct respip_client_info* client_info; 697 698 /** Extended result of response-ip action processing, mainly 699 * for logging purposes. */ 700 struct respip_action_info* respip_action_info; 701 /** if the query has been modified by rpz processing. */ 702 int rpz_applied; 703 /** if the query is rpz passthru, no further rpz processing for it */ 704 int rpz_passthru; 705 /* Flag tcp required. */ 706 int tcp_required; 707 708 /** whether the reply should be dropped */ 709 int is_drop; 710 }; 711 712 /** 713 * Module functionality block 714 */ 715 struct module_func_block { 716 /** text string name of module */ 717 const char* name; 718 719 /** 720 * Set up the module for start. This is called only once at startup. 721 * Privileged operations like opening device files may be done here. 722 * The function ptr can be NULL, if it is not used. 723 * @param env: module environment. 724 * @param id: module id number. 725 * return: 0 on error 726 */ 727 int (*startup)(struct module_env* env, int id); 728 729 /** 730 * Close down the module for stop. This is called only once before 731 * shutdown to free resources allocated during startup(). 732 * Closing privileged ports or files must be done here. 733 * The function ptr can be NULL, if it is not used. 734 * @param env: module environment. 735 * @param id: module id number. 736 */ 737 void (*destartup)(struct module_env* env, int id); 738 739 /** 740 * Initialise the module. Called when restarting or reloading the 741 * daemon. 742 * This is the place to apply settings from the config file. 743 * @param env: module environment. 744 * @param id: module id number. 745 * return: 0 on error 746 */ 747 int (*init)(struct module_env* env, int id); 748 749 /** 750 * Deinitialise the module, undo stuff done during init(). 751 * Called before reloading the daemon. 752 * @param env: module environment. 753 * @param id: module id number. 754 */ 755 void (*deinit)(struct module_env* env, int id); 756 757 /** 758 * accept a new query, or work further on existing query. 759 * Changes the qstate->ext_state to be correct on exit. 760 * @param ev: event that causes the module state machine to 761 * (re-)activate. 762 * @param qstate: the query state. 763 * Note that this method is not allowed to change the 764 * query state 'identity', that is query info, qflags, 765 * and priming status. 766 * Attach a subquery to get results to a different query. 767 * @param id: module id number that operate() is called on. 768 * @param outbound: if not NULL this event is due to the reply/timeout 769 * or error on this outbound query. 770 * @return: if at exit the ext_state is: 771 * o wait_module: next module is started. (with pass event). 772 * o error or finished: previous module is resumed. 773 * o otherwise it waits until that event happens (assumes 774 * the service routine to make subrequest or send message 775 * have been called. 776 */ 777 void (*operate)(struct module_qstate* qstate, enum module_ev event, 778 int id, struct outbound_entry* outbound); 779 780 /** 781 * inform super querystate about the results from this subquerystate. 782 * Is called when the querystate is finished. The method invoked is 783 * the one from the current module active in the super querystate. 784 * @param qstate: the query state that is finished. 785 * Examine return_rcode and return_reply in the qstate. 786 * @param id: module id for this module. 787 * This coincides with the current module for the super qstate. 788 * @param super: the super querystate that needs to be informed. 789 */ 790 void (*inform_super)(struct module_qstate* qstate, int id, 791 struct module_qstate* super); 792 793 /** 794 * clear module specific data 795 */ 796 void (*clear)(struct module_qstate* qstate, int id); 797 798 /** 799 * How much memory is the module specific data using. 800 * @param env: module environment. 801 * @param id: the module id. 802 * @return the number of bytes that are alloced. 803 */ 804 size_t (*get_mem)(struct module_env* env, int id); 805 }; 806 807 /** 808 * Debug utility: module external qstate to string 809 * @param s: the state value. 810 * @return descriptive string. 811 */ 812 const char* strextstate(enum module_ext_state s); 813 814 /** 815 * Debug utility: module event to string 816 * @param e: the module event value. 817 * @return descriptive string. 818 */ 819 const char* strmodulevent(enum module_ev e); 820 821 /** 822 * Append text to the error info for validation. 823 * @param qstate: query state. 824 * @param str: copied into query region and appended. 825 * Failures to allocate are logged. 826 */ 827 void errinf(struct module_qstate* qstate, const char* str); 828 void errinf_ede(struct module_qstate* qstate, const char* str, 829 sldns_ede_code reason_bogus); 830 831 /** 832 * Append text to error info: from 1.2.3.4 833 * @param qstate: query state. 834 * @param origin: sock list with origin of trouble. 835 * Every element added. 836 * If NULL: nothing is added. 837 * if 0len element: 'from cache' is added. 838 */ 839 void errinf_origin(struct module_qstate* qstate, struct sock_list *origin); 840 841 /** 842 * Append text to error info: for RRset name type class 843 * @param qstate: query state. 844 * @param rr: rrset_key. 845 */ 846 void errinf_rrset(struct module_qstate* qstate, struct ub_packed_rrset_key *rr); 847 848 /** 849 * Append text to error info: str dname 850 * @param qstate: query state. 851 * @param str: explanation string 852 * @param dname: the dname. 853 */ 854 void errinf_dname(struct module_qstate* qstate, const char* str, 855 uint8_t* dname); 856 857 /** 858 * Create error info in string. For validation failures. 859 * @param qstate: query state. 860 * @param region: the region for the result or NULL for malloced result. 861 * @return string or NULL on malloc failure (already logged). 862 * This string is malloced if region is NULL and has to be freed by caller. 863 */ 864 char* errinf_to_str_bogus(struct module_qstate* qstate, struct regional* region); 865 866 /** 867 * Check the sldns_ede_code of the qstate->errinf. 868 * @param qstate: query state. 869 * @return the latest explicitly set sldns_ede_code or LDNS_EDE_NONE. 870 */ 871 sldns_ede_code errinf_to_reason_bogus(struct module_qstate* qstate); 872 873 /** 874 * Create error info in string. For other servfails. 875 * @param qstate: query state. 876 * @return string or NULL on malloc failure (already logged). 877 */ 878 char* errinf_to_str_servfail(struct module_qstate* qstate); 879 880 /** 881 * Create error info in string. For misc failures that are not servfail. 882 * @param qstate: query state. 883 * @return string or NULL on malloc failure (already logged). 884 */ 885 char* errinf_to_str_misc(struct module_qstate* qstate); 886 887 /** 888 * Initialize the edns known options by allocating the required space. 889 * @param env: the module environment. 890 * @return false on failure (no memory). 891 */ 892 int edns_known_options_init(struct module_env* env); 893 894 /** 895 * Free the allocated space for the known edns options. 896 * @param env: the module environment. 897 */ 898 void edns_known_options_delete(struct module_env* env); 899 900 /** 901 * Register a known edns option. Overwrite the flags if it is already 902 * registered. Used before creating workers to register known edns options. 903 * @param opt_code: the edns option code. 904 * @param bypass_cache_stage: whether the option interacts with the cache. 905 * @param no_aggregation: whether the option implies more specific 906 * aggregation. 907 * @param env: the module environment. 908 * @return true on success, false on failure (registering more options than 909 * allowed or trying to register after the environment is copied to the 910 * threads.) 911 */ 912 int edns_register_option(uint16_t opt_code, int bypass_cache_stage, 913 int no_aggregation, struct module_env* env); 914 915 /** 916 * Register an inplace callback function. 917 * @param cb: pointer to the callback function. 918 * @param type: inplace callback type. 919 * @param cbarg: argument for the callback function, or NULL. 920 * @param env: the module environment. 921 * @param id: module id. 922 * @return true on success, false on failure (out of memory or trying to 923 * register after the environment is copied to the threads.) 924 */ 925 int 926 inplace_cb_register(void* cb, enum inplace_cb_list_type type, void* cbarg, 927 struct module_env* env, int id); 928 929 /** 930 * Delete callback for specified type and module id. 931 * @param env: the module environment. 932 * @param type: inplace callback type. 933 * @param id: module id. 934 */ 935 void 936 inplace_cb_delete(struct module_env* env, enum inplace_cb_list_type type, 937 int id); 938 939 /** 940 * Delete all the inplace callback linked lists. 941 * @param env: the module environment. 942 */ 943 void inplace_cb_lists_delete(struct module_env* env); 944 945 /** 946 * Check if an edns option is known. 947 * @param opt_code: the edns option code. 948 * @param env: the module environment. 949 * @return pointer to registered option if the edns option is known, 950 * NULL otherwise. 951 */ 952 struct edns_known_option* edns_option_is_known(uint16_t opt_code, 953 struct module_env* env); 954 955 /** 956 * Check if an edns option needs to bypass the reply from cache stage. 957 * @param list: the edns options. 958 * @param env: the module environment. 959 * @return true if an edns option needs to bypass the cache stage, 960 * false otherwise. 961 */ 962 int edns_bypass_cache_stage(struct edns_option* list, 963 struct module_env* env); 964 965 /** 966 * Check if an unique mesh state is required. Might be triggered by EDNS option 967 * or set for the complete env. 968 * @param list: the edns options. 969 * @param env: the module environment. 970 * @return true if an edns option needs a unique mesh state, 971 * false otherwise. 972 */ 973 int unique_mesh_state(struct edns_option* list, struct module_env* env); 974 975 /** 976 * Log the known edns options. 977 * @param level: the desired verbosity level. 978 * @param env: the module environment. 979 */ 980 void log_edns_known_options(enum verbosity_value level, 981 struct module_env* env); 982 983 /** 984 * Copy state that may have happened in the subquery and is always relevant to 985 * the super. 986 * @param qstate: query state that finished. 987 * @param id: module id. 988 * @param super: the qstate to inform. 989 */ 990 void copy_state_to_super(struct module_qstate* qstate, int id, 991 struct module_qstate* super); 992 993 #endif /* UTIL_MODULE_H */ 994