1 /* 2 * services/outside_network.h - listen to answers from the network 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 has functions to send queries to authoritative servers, 40 * and wait for the pending answer, with timeouts. 41 */ 42 43 #ifndef OUTSIDE_NETWORK_H 44 #define OUTSIDE_NETWORK_H 45 46 #include "util/rbtree.h" 47 #include "util/netevent.h" 48 #include "dnstap/dnstap_config.h" 49 struct pending; 50 struct pending_timeout; 51 struct ub_randstate; 52 struct pending_tcp; 53 struct waiting_tcp; 54 struct waiting_udp; 55 struct infra_cache; 56 struct port_comm; 57 struct port_if; 58 struct sldns_buffer; 59 struct serviced_query; 60 struct dt_env; 61 struct edns_option; 62 struct module_env; 63 struct module_qstate; 64 struct query_info; 65 66 /** 67 * Send queries to outside servers and wait for answers from servers. 68 * Contains answer-listen sockets. 69 */ 70 struct outside_network { 71 /** Base for select calls */ 72 struct comm_base* base; 73 /** pointer to time in seconds */ 74 time_t* now_secs; 75 /** pointer to time in microseconds */ 76 struct timeval* now_tv; 77 78 /** buffer shared by UDP connections, since there is only one 79 datagram at any time. */ 80 struct sldns_buffer* udp_buff; 81 /** serviced_callbacks malloc overhead when processing multiple 82 * identical serviced queries to the same server. */ 83 size_t svcd_overhead; 84 /** use x20 bits to encode additional ID random bits */ 85 int use_caps_for_id; 86 /** outside network wants to quit. Stop queued msgs from sent. */ 87 int want_to_quit; 88 89 /** number of unwanted replies received (for statistics) */ 90 size_t unwanted_replies; 91 /** cumulative total of unwanted replies (for defense) */ 92 size_t unwanted_total; 93 /** threshold when to take defensive action. If 0 then never. */ 94 size_t unwanted_threshold; 95 /** what action to take, called when defensive action is needed */ 96 void (*unwanted_action)(void*); 97 /** user param for action */ 98 void* unwanted_param; 99 100 /** linked list of available commpoints, unused file descriptors, 101 * for use as outgoing UDP ports. cp.fd=-1 in them. */ 102 struct port_comm* unused_fds; 103 /** if udp is done */ 104 int do_udp; 105 /** if udp is delay-closed (delayed answers do not meet closed port)*/ 106 int delayclose; 107 /** timeout for delayclose */ 108 struct timeval delay_tv; 109 110 /** array of outgoing IP4 interfaces */ 111 struct port_if* ip4_ifs; 112 /** number of outgoing IP4 interfaces */ 113 int num_ip4; 114 115 /** array of outgoing IP6 interfaces */ 116 struct port_if* ip6_ifs; 117 /** number of outgoing IP6 interfaces */ 118 int num_ip6; 119 120 /** pending udp queries waiting to be sent out, waiting for fd */ 121 struct pending* udp_wait_first; 122 /** last pending udp query in list */ 123 struct pending* udp_wait_last; 124 125 /** pending udp answers. sorted by id, addr */ 126 rbtree_type* pending; 127 /** serviced queries, sorted by qbuf, addr, dnssec */ 128 rbtree_type* serviced; 129 /** host cache, pointer but not owned by outnet. */ 130 struct infra_cache* infra; 131 /** where to get random numbers */ 132 struct ub_randstate* rnd; 133 /** ssl context to create ssl wrapped TCP with DNS connections */ 134 void* sslctx; 135 #ifdef USE_DNSTAP 136 /** dnstap environment */ 137 struct dt_env* dtenv; 138 #endif 139 /** maximum segment size of tcp socket */ 140 int tcp_mss; 141 142 /** 143 * Array of tcp pending used for outgoing TCP connections. 144 * Each can be used to establish a TCP connection with a server. 145 * The file descriptors are -1 if they are free, and need to be 146 * opened for the tcp connection. Can be used for ip4 and ip6. 147 */ 148 struct pending_tcp **tcp_conns; 149 /** number of tcp communication points. */ 150 size_t num_tcp; 151 /** number of tcp communication points in use. */ 152 size_t num_tcp_outgoing; 153 /** list of tcp comm points that are free for use */ 154 struct pending_tcp* tcp_free; 155 /** list of tcp queries waiting for a buffer */ 156 struct waiting_tcp* tcp_wait_first; 157 /** last of waiting query list */ 158 struct waiting_tcp* tcp_wait_last; 159 }; 160 161 /** 162 * Outgoing interface. Ports available and currently used are tracked 163 * per interface 164 */ 165 struct port_if { 166 /** address ready to allocate new socket (except port no). */ 167 struct sockaddr_storage addr; 168 /** length of addr field */ 169 socklen_t addrlen; 170 171 /** prefix length of network address (in bits), for randomisation. 172 * if 0, no randomisation. */ 173 int pfxlen; 174 175 /** the available ports array. These are unused. 176 * Only the first total-inuse part is filled. */ 177 int* avail_ports; 178 /** the total number of available ports (size of the array) */ 179 int avail_total; 180 181 /** array of the commpoints currently in use. 182 * allocated for max number of fds, first part in use. */ 183 struct port_comm** out; 184 /** max number of fds, size of out array */ 185 int maxout; 186 /** number of commpoints (and thus also ports) in use */ 187 int inuse; 188 }; 189 190 /** 191 * Outgoing commpoint for UDP port. 192 */ 193 struct port_comm { 194 /** next in free list */ 195 struct port_comm* next; 196 /** which port number (when in use) */ 197 int number; 198 /** interface it is used in */ 199 struct port_if* pif; 200 /** index in the out array of the interface */ 201 int index; 202 /** number of outstanding queries on this port */ 203 int num_outstanding; 204 /** UDP commpoint, fd=-1 if not in use */ 205 struct comm_point* cp; 206 }; 207 208 /** 209 * A query that has an answer pending for it. 210 */ 211 struct pending { 212 /** redblacktree entry, key is the pending struct(id, addr). */ 213 rbnode_type node; 214 /** the ID for the query. int so that a value out of range can 215 * be used to signify a pending that is for certain not present in 216 * the rbtree. (and for which deletion is safe). */ 217 unsigned int id; 218 /** remote address. */ 219 struct sockaddr_storage addr; 220 /** length of addr field in use. */ 221 socklen_t addrlen; 222 /** comm point it was sent on (and reply must come back on). */ 223 struct port_comm* pc; 224 /** timeout event */ 225 struct comm_timer* timer; 226 /** callback for the timeout, error or reply to the message */ 227 comm_point_callback_type* cb; 228 /** callback user argument */ 229 void* cb_arg; 230 /** the outside network it is part of */ 231 struct outside_network* outnet; 232 /** the corresponding serviced_query */ 233 struct serviced_query* sq; 234 235 /*---- filled if udp pending is waiting -----*/ 236 /** next in waiting list. */ 237 struct pending* next_waiting; 238 /** timeout in msec */ 239 int timeout; 240 /** The query itself, the query packet to send. */ 241 uint8_t* pkt; 242 /** length of query packet. */ 243 size_t pkt_len; 244 }; 245 246 /** 247 * Pending TCP query to server. 248 */ 249 struct pending_tcp { 250 /** next in list of free tcp comm points, or NULL. */ 251 struct pending_tcp* next_free; 252 /** the ID for the query; checked in reply */ 253 uint16_t id; 254 /** tcp comm point it was sent on (and reply must come back on). */ 255 struct comm_point* c; 256 /** the query being serviced, NULL if the pending_tcp is unused. */ 257 struct waiting_tcp* query; 258 }; 259 260 /** 261 * Query waiting for TCP buffer. 262 */ 263 struct waiting_tcp { 264 /** 265 * next in waiting list. 266 * if pkt==0, this points to the pending_tcp structure. 267 */ 268 struct waiting_tcp* next_waiting; 269 /** timeout event; timer keeps running whether the query is 270 * waiting for a buffer or the tcp reply is pending */ 271 struct comm_timer* timer; 272 /** the outside network it is part of */ 273 struct outside_network* outnet; 274 /** remote address. */ 275 struct sockaddr_storage addr; 276 /** length of addr field in use. */ 277 socklen_t addrlen; 278 /** 279 * The query itself, the query packet to send. 280 * allocated after the waiting_tcp structure. 281 * set to NULL when the query is serviced and it part of pending_tcp. 282 * if this is NULL, the next_waiting points to the pending_tcp. 283 */ 284 uint8_t* pkt; 285 /** length of query packet. */ 286 size_t pkt_len; 287 /** callback for the timeout, error or reply to the message */ 288 comm_point_callback_type* cb; 289 /** callback user argument */ 290 void* cb_arg; 291 /** if it uses ssl upstream */ 292 int ssl_upstream; 293 }; 294 295 /** 296 * Callback to party interested in serviced query results. 297 */ 298 struct service_callback { 299 /** next in callback list */ 300 struct service_callback* next; 301 /** callback function */ 302 comm_point_callback_type* cb; 303 /** user argument for callback function */ 304 void* cb_arg; 305 }; 306 307 /** fallback size for fragmentation for EDNS in IPv4 */ 308 #define EDNS_FRAG_SIZE_IP4 1472 309 /** fallback size for EDNS in IPv6, fits one fragment with ip6-tunnel-ids */ 310 #define EDNS_FRAG_SIZE_IP6 1232 311 312 /** 313 * Query service record. 314 * Contains query and destination. UDP, TCP, EDNS are all tried. 315 * complete with retries and timeouts. A number of interested parties can 316 * receive a callback. 317 */ 318 struct serviced_query { 319 /** The rbtree node, key is this record */ 320 rbnode_type node; 321 /** The query that needs to be answered. Starts with flags u16, 322 * then qdcount, ..., including qname, qtype, qclass. Does not include 323 * EDNS record. */ 324 uint8_t* qbuf; 325 /** length of qbuf. */ 326 size_t qbuflen; 327 /** If an EDNS section is included, the DO/CD bit will be turned on. */ 328 int dnssec; 329 /** We want signatures, or else the answer is likely useless */ 330 int want_dnssec; 331 /** ignore capsforid */ 332 int nocaps; 333 /** tcp upstream used, use tcp, or ssl_upstream for SSL */ 334 int tcp_upstream, ssl_upstream; 335 /** where to send it */ 336 struct sockaddr_storage addr; 337 /** length of addr field in use. */ 338 socklen_t addrlen; 339 /** zone name, uncompressed domain name in wireformat */ 340 uint8_t* zone; 341 /** length of zone name */ 342 size_t zonelen; 343 /** qtype */ 344 int qtype; 345 /** current status */ 346 enum serviced_query_status { 347 /** initial status */ 348 serviced_initial, 349 /** UDP with EDNS sent */ 350 serviced_query_UDP_EDNS, 351 /** UDP without EDNS sent */ 352 serviced_query_UDP, 353 /** TCP with EDNS sent */ 354 serviced_query_TCP_EDNS, 355 /** TCP without EDNS sent */ 356 serviced_query_TCP, 357 /** probe to test EDNS lameness (EDNS is dropped) */ 358 serviced_query_PROBE_EDNS, 359 /** probe to test noEDNS0 (EDNS gives FORMERRorNOTIMP) */ 360 serviced_query_UDP_EDNS_fallback, 361 /** probe to test TCP noEDNS0 (EDNS gives FORMERRorNOTIMP) */ 362 serviced_query_TCP_EDNS_fallback, 363 /** send UDP query with EDNS1480 (or 1280) */ 364 serviced_query_UDP_EDNS_FRAG 365 } 366 /** variable with current status */ 367 status; 368 /** true if serviced_query is scheduled for deletion already */ 369 int to_be_deleted; 370 /** number of UDP retries */ 371 int retry; 372 /** time last UDP was sent */ 373 struct timeval last_sent_time; 374 /** rtt of last (UDP) message */ 375 int last_rtt; 376 /** do we know edns probe status already, for UDP_EDNS queries */ 377 int edns_lame_known; 378 /** edns options to use for sending upstream packet */ 379 struct edns_option* opt_list; 380 /** outside network this is part of */ 381 struct outside_network* outnet; 382 /** list of interested parties that need callback on results. */ 383 struct service_callback* cblist; 384 /** the UDP or TCP query that is pending, see status which */ 385 void* pending; 386 }; 387 388 /** 389 * Create outside_network structure with N udp ports. 390 * @param base: the communication base to use for event handling. 391 * @param bufsize: size for network buffers. 392 * @param num_ports: number of udp ports to open per interface. 393 * @param ifs: interface names (or NULL for default interface). 394 * These interfaces must be able to access all authoritative servers. 395 * @param num_ifs: number of names in array ifs. 396 * @param do_ip4: service IP4. 397 * @param do_ip6: service IP6. 398 * @param num_tcp: number of outgoing tcp buffers to preallocate. 399 * @param infra: pointer to infra cached used for serviced queries. 400 * @param rnd: stored to create random numbers for serviced queries. 401 * @param use_caps_for_id: enable to use 0x20 bits to encode id randomness. 402 * @param availports: array of available ports. 403 * @param numavailports: number of available ports in array. 404 * @param unwanted_threshold: when to take defensive action. 405 * @param unwanted_action: the action to take. 406 * @param unwanted_param: user parameter to action. 407 * @param tcp_mss: maximum segment size of tcp socket. 408 * @param do_udp: if udp is done. 409 * @param sslctx: context to create outgoing connections with (if enabled). 410 * @param delayclose: if not 0, udp sockets are delayed before timeout closure. 411 * msec to wait on timeouted udp sockets. 412 * @param dtenv: environment to send dnstap events with (if enabled). 413 * @return: the new structure (with no pending answers) or NULL on error. 414 */ 415 struct outside_network* outside_network_create(struct comm_base* base, 416 size_t bufsize, size_t num_ports, char** ifs, int num_ifs, 417 int do_ip4, int do_ip6, size_t num_tcp, struct infra_cache* infra, 418 struct ub_randstate* rnd, int use_caps_for_id, int* availports, 419 int numavailports, size_t unwanted_threshold, int tcp_mss, 420 void (*unwanted_action)(void*), void* unwanted_param, int do_udp, 421 void* sslctx, int delayclose, struct dt_env *dtenv); 422 423 /** 424 * Delete outside_network structure. 425 * @param outnet: object to delete. 426 */ 427 void outside_network_delete(struct outside_network* outnet); 428 429 /** 430 * Prepare for quit. Sends no more queries, even if queued up. 431 * @param outnet: object to prepare for removal 432 */ 433 void outside_network_quit_prepare(struct outside_network* outnet); 434 435 /** 436 * Send UDP query, create pending answer. 437 * Changes the ID for the query to be random and unique for that destination. 438 * @param sq: serviced query. 439 * @param packet: wireformat query to send to destination. 440 * @param timeout: in milliseconds from now. 441 * @param callback: function to call on error, timeout or reply. 442 * @param callback_arg: user argument for callback function. 443 * @return: NULL on error for malloc or socket. Else the pending query object. 444 */ 445 struct pending* pending_udp_query(struct serviced_query* sq, 446 struct sldns_buffer* packet, int timeout, comm_point_callback_type* callback, 447 void* callback_arg); 448 449 /** 450 * Send TCP query. May wait for TCP buffer. Selects ID to be random, and 451 * checks id. 452 * @param sq: serviced query. 453 * @param packet: wireformat query to send to destination. copied from. 454 * @param timeout: in seconds from now. 455 * Timer starts running now. Timer may expire if all buffers are used, 456 * without any query been sent to the server yet. 457 * @param callback: function to call on error, timeout or reply. 458 * @param callback_arg: user argument for callback function. 459 * @return: false on error for malloc or socket. Else the pending TCP object. 460 */ 461 struct waiting_tcp* pending_tcp_query(struct serviced_query* sq, 462 struct sldns_buffer* packet, int timeout, comm_point_callback_type* callback, 463 void* callback_arg); 464 465 /** 466 * Delete pending answer. 467 * @param outnet: outside network the pending query is part of. 468 * Internal feature: if outnet is NULL, p is not unlinked from rbtree. 469 * @param p: deleted 470 */ 471 void pending_delete(struct outside_network* outnet, struct pending* p); 472 473 /** 474 * Perform a serviced query to the authoritative servers. 475 * Duplicate efforts are detected, and EDNS, TCP and UDP retry is performed. 476 * @param outnet: outside network, with rbtree of serviced queries. 477 * @param qinfo: query info. 478 * @param flags: flags u16 (host format), includes opcode, CD bit. 479 * @param dnssec: if set, DO bit is set in EDNS queries. 480 * If the value includes BIT_CD, CD bit is set when in EDNS queries. 481 * If the value includes BIT_DO, DO bit is set when in EDNS queries. 482 * @param want_dnssec: signatures are needed, without EDNS the answer is 483 * likely to be useless. 484 * @param nocaps: ignore use_caps_for_id and use unperturbed qname. 485 * @param tcp_upstream: use TCP for upstream queries. 486 * @param ssl_upstream: use SSL for upstream queries. 487 * @param addr: to which server to send the query. 488 * @param addrlen: length of addr. 489 * @param zone: name of the zone of the delegation point. wireformat dname. 490 This is the delegation point name for which the server is deemed 491 authoritative. 492 * @param zonelen: length of zone. 493 * @param qstate: module qstate. Mainly for inspecting the available 494 * edns_opts_lists. 495 * @param callback: callback function. 496 * @param callback_arg: user argument to callback function. 497 * @param buff: scratch buffer to create query contents in. Empty on exit. 498 * @param env: the module environment. 499 * @return 0 on error, or pointer to serviced query that is used to answer 500 * this serviced query may be shared with other callbacks as well. 501 */ 502 struct serviced_query* outnet_serviced_query(struct outside_network* outnet, 503 struct query_info* qinfo, uint16_t flags, int dnssec, int want_dnssec, 504 int nocaps, int tcp_upstream, int ssl_upstream, 505 struct sockaddr_storage* addr, socklen_t addrlen, uint8_t* zone, 506 size_t zonelen, struct module_qstate* qstate, 507 comm_point_callback_type* callback, void* callback_arg, 508 struct sldns_buffer* buff, struct module_env* env); 509 510 /** 511 * Remove service query callback. 512 * If that leads to zero callbacks, the query is completely cancelled. 513 * @param sq: serviced query to adjust. 514 * @param cb_arg: callback argument of callback that needs removal. 515 * same as the callback_arg to outnet_serviced_query(). 516 */ 517 void outnet_serviced_query_stop(struct serviced_query* sq, void* cb_arg); 518 519 /** 520 * Get memory size in use by outside network. 521 * Counts buffers and outstanding query (serviced queries) malloced data. 522 * @param outnet: outside network structure. 523 * @return size in bytes. 524 */ 525 size_t outnet_get_mem(struct outside_network* outnet); 526 527 /** 528 * Get memory size in use by serviced query while it is servicing callbacks. 529 * This takes into account the pre-deleted status of it; it will be deleted 530 * when the callbacks are done. 531 * @param sq: serviced query. 532 * @return size in bytes. 533 */ 534 size_t serviced_get_mem(struct serviced_query* sq); 535 536 /** callback for incoming udp answers from the network */ 537 int outnet_udp_cb(struct comm_point* c, void* arg, int error, 538 struct comm_reply *reply_info); 539 540 /** callback for pending tcp connections */ 541 int outnet_tcp_cb(struct comm_point* c, void* arg, int error, 542 struct comm_reply *reply_info); 543 544 /** callback for udp timeout */ 545 void pending_udp_timer_cb(void *arg); 546 547 /** callback for udp delay for timeout */ 548 void pending_udp_timer_delay_cb(void *arg); 549 550 /** callback for outgoing TCP timer event */ 551 void outnet_tcptimer(void* arg); 552 553 /** callback for serviced query UDP answers */ 554 int serviced_udp_callback(struct comm_point* c, void* arg, int error, 555 struct comm_reply* rep); 556 557 /** TCP reply or error callback for serviced queries */ 558 int serviced_tcp_callback(struct comm_point* c, void* arg, int error, 559 struct comm_reply* rep); 560 561 /** compare function of pending rbtree */ 562 int pending_cmp(const void* key1, const void* key2); 563 564 /** compare function of serviced query rbtree */ 565 int serviced_cmp(const void* key1, const void* key2); 566 567 #endif /* OUTSIDE_NETWORK_H */ 568