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