2 * services/outside_network.h - listen to answers from the network
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
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.
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.
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.
39 * This file has functions to send queries to authoritative servers,
40 * and wait for the pending answer, with timeouts.
43 #ifndef OUTSIDE_NETWORK_H
44 #define OUTSIDE_NETWORK_H
46 #include "util/rbtree.h"
47 #include "util/netevent.h"
48 #include "dnstap/dnstap_config.h"
50 struct pending_timeout;
59 struct serviced_query;
67 * Send queries to outside servers and wait for answers from servers.
68 * Contains answer-listen sockets.
70 struct outside_network {
71 /** Base for select calls */
72 struct comm_base* base;
73 /** pointer to time in seconds */
75 /** pointer to time in microseconds */
76 struct timeval* now_tv;
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. */
84 /** use x20 bits to encode additional ID random bits */
86 /** outside network wants to quit. Stop queued msgs from sent. */
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 */
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 */
105 /** if udp is delay-closed (delayed answers do not meet closed port)*/
107 /** timeout for delayclose */
108 struct timeval delay_tv;
110 /** array of outgoing IP4 interfaces */
111 struct port_if* ip4_ifs;
112 /** number of outgoing IP4 interfaces */
115 /** array of outgoing IP6 interfaces */
116 struct port_if* ip6_ifs;
117 /** number of outgoing IP6 interfaces */
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;
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 */
135 /** if SNI will be used for TLS connections */
138 /** dnstap environment */
139 struct dt_env* dtenv;
141 /** maximum segment size of tcp socket */
143 /** IP_TOS socket option requested on the sockets */
147 * Array of tcp pending used for outgoing TCP connections.
148 * Each can be used to establish a TCP connection with a server.
149 * The file descriptors are -1 if they are free, and need to be
150 * opened for the tcp connection. Can be used for ip4 and ip6.
152 struct pending_tcp **tcp_conns;
153 /** number of tcp communication points. */
155 /** number of tcp communication points in use. */
156 size_t num_tcp_outgoing;
157 /** list of tcp comm points that are free for use */
158 struct pending_tcp* tcp_free;
159 /** list of tcp queries waiting for a buffer */
160 struct waiting_tcp* tcp_wait_first;
161 /** last of waiting query list */
162 struct waiting_tcp* tcp_wait_last;
166 * Outgoing interface. Ports available and currently used are tracked
170 /** address ready to allocate new socket (except port no). */
171 struct sockaddr_storage addr;
172 /** length of addr field */
175 /** prefix length of network address (in bits), for randomisation.
176 * if 0, no randomisation. */
179 #ifndef DISABLE_EXPLICIT_PORT_RANDOMISATION
180 /** the available ports array. These are unused.
181 * Only the first total-inuse part is filled. */
183 /** the total number of available ports (size of the array) */
187 /** array of the commpoints currently in use.
188 * allocated for max number of fds, first part in use. */
189 struct port_comm** out;
190 /** max number of fds, size of out array */
192 /** number of commpoints (and thus also ports) in use */
197 * Outgoing commpoint for UDP port.
200 /** next in free list */
201 struct port_comm* next;
202 /** which port number (when in use) */
204 /** interface it is used in */
206 /** index in the out array of the interface */
208 /** number of outstanding queries on this port */
210 /** UDP commpoint, fd=-1 if not in use */
211 struct comm_point* cp;
215 * A query that has an answer pending for it.
218 /** redblacktree entry, key is the pending struct(id, addr). */
220 /** the ID for the query. int so that a value out of range can
221 * be used to signify a pending that is for certain not present in
222 * the rbtree. (and for which deletion is safe). */
224 /** remote address. */
225 struct sockaddr_storage addr;
226 /** length of addr field in use. */
228 /** comm point it was sent on (and reply must come back on). */
229 struct port_comm* pc;
231 struct comm_timer* timer;
232 /** callback for the timeout, error or reply to the message */
233 comm_point_callback_type* cb;
234 /** callback user argument */
236 /** the outside network it is part of */
237 struct outside_network* outnet;
238 /** the corresponding serviced_query */
239 struct serviced_query* sq;
241 /*---- filled if udp pending is waiting -----*/
242 /** next in waiting list. */
243 struct pending* next_waiting;
244 /** timeout in msec */
246 /** The query itself, the query packet to send. */
248 /** length of query packet. */
253 * Pending TCP query to server.
256 /** next in list of free tcp comm points, or NULL. */
257 struct pending_tcp* next_free;
258 /** the ID for the query; checked in reply */
260 /** tcp comm point it was sent on (and reply must come back on). */
261 struct comm_point* c;
262 /** the query being serviced, NULL if the pending_tcp is unused. */
263 struct waiting_tcp* query;
267 * Query waiting for TCP buffer.
271 * next in waiting list.
272 * if pkt==0, this points to the pending_tcp structure.
274 struct waiting_tcp* next_waiting;
275 /** timeout event; timer keeps running whether the query is
276 * waiting for a buffer or the tcp reply is pending */
277 struct comm_timer* timer;
278 /** the outside network it is part of */
279 struct outside_network* outnet;
280 /** remote address. */
281 struct sockaddr_storage addr;
282 /** length of addr field in use. */
285 * The query itself, the query packet to send.
286 * allocated after the waiting_tcp structure.
287 * set to NULL when the query is serviced and it part of pending_tcp.
288 * if this is NULL, the next_waiting points to the pending_tcp.
291 /** length of query packet. */
293 /** callback for the timeout, error or reply to the message */
294 comm_point_callback_type* cb;
295 /** callback user argument */
297 /** if it uses ssl upstream */
299 /** ref to the tls_auth_name from the serviced_query */
304 * Callback to party interested in serviced query results.
306 struct service_callback {
307 /** next in callback list */
308 struct service_callback* next;
309 /** callback function */
310 comm_point_callback_type* cb;
311 /** user argument for callback function */
315 /** fallback size for fragmentation for EDNS in IPv4 */
316 #define EDNS_FRAG_SIZE_IP4 1472
317 /** fallback size for EDNS in IPv6, fits one fragment with ip6-tunnel-ids */
318 #define EDNS_FRAG_SIZE_IP6 1232
321 * Query service record.
322 * Contains query and destination. UDP, TCP, EDNS are all tried.
323 * complete with retries and timeouts. A number of interested parties can
324 * receive a callback.
326 struct serviced_query {
327 /** The rbtree node, key is this record */
329 /** The query that needs to be answered. Starts with flags u16,
330 * then qdcount, ..., including qname, qtype, qclass. Does not include
333 /** length of qbuf. */
335 /** If an EDNS section is included, the DO/CD bit will be turned on. */
337 /** We want signatures, or else the answer is likely useless */
339 /** ignore capsforid */
341 /** tcp upstream used, use tcp, or ssl_upstream for SSL */
342 int tcp_upstream, ssl_upstream;
343 /** the name of the tls authentication name, eg. 'ns.example.com'
346 /** where to send it */
347 struct sockaddr_storage addr;
348 /** length of addr field in use. */
350 /** zone name, uncompressed domain name in wireformat */
352 /** length of zone name */
356 /** current status */
357 enum serviced_query_status {
358 /** initial status */
360 /** UDP with EDNS sent */
361 serviced_query_UDP_EDNS,
362 /** UDP without EDNS sent */
364 /** TCP with EDNS sent */
365 serviced_query_TCP_EDNS,
366 /** TCP without EDNS sent */
368 /** probe to test noEDNS0 (EDNS gives FORMERRorNOTIMP) */
369 serviced_query_UDP_EDNS_fallback,
370 /** probe to test TCP noEDNS0 (EDNS gives FORMERRorNOTIMP) */
371 serviced_query_TCP_EDNS_fallback,
372 /** send UDP query with EDNS1480 (or 1280) */
373 serviced_query_UDP_EDNS_FRAG
375 /** variable with current status */
377 /** true if serviced_query is scheduled for deletion already */
379 /** number of UDP retries */
381 /** time last UDP was sent */
382 struct timeval last_sent_time;
383 /** rtt of last message */
385 /** do we know edns probe status already, for UDP_EDNS queries */
387 /** edns options to use for sending upstream packet */
388 struct edns_option* opt_list;
389 /** outside network this is part of */
390 struct outside_network* outnet;
391 /** list of interested parties that need callback on results. */
392 struct service_callback* cblist;
393 /** the UDP or TCP query that is pending, see status which */
398 * Create outside_network structure with N udp ports.
399 * @param base: the communication base to use for event handling.
400 * @param bufsize: size for network buffers.
401 * @param num_ports: number of udp ports to open per interface.
402 * @param ifs: interface names (or NULL for default interface).
403 * These interfaces must be able to access all authoritative servers.
404 * @param num_ifs: number of names in array ifs.
405 * @param do_ip4: service IP4.
406 * @param do_ip6: service IP6.
407 * @param num_tcp: number of outgoing tcp buffers to preallocate.
408 * @param dscp: DSCP to use.
409 * @param infra: pointer to infra cached used for serviced queries.
410 * @param rnd: stored to create random numbers for serviced queries.
411 * @param use_caps_for_id: enable to use 0x20 bits to encode id randomness.
412 * @param availports: array of available ports.
413 * @param numavailports: number of available ports in array.
414 * @param unwanted_threshold: when to take defensive action.
415 * @param unwanted_action: the action to take.
416 * @param unwanted_param: user parameter to action.
417 * @param tcp_mss: maximum segment size of tcp socket.
418 * @param do_udp: if udp is done.
419 * @param sslctx: context to create outgoing connections with (if enabled).
420 * @param delayclose: if not 0, udp sockets are delayed before timeout closure.
421 * msec to wait on timeouted udp sockets.
422 * @param tls_use_sni: if SNI is used for TLS connections.
423 * @param dtenv: environment to send dnstap events with (if enabled).
424 * @return: the new structure (with no pending answers) or NULL on error.
426 struct outside_network* outside_network_create(struct comm_base* base,
427 size_t bufsize, size_t num_ports, char** ifs, int num_ifs,
428 int do_ip4, int do_ip6, size_t num_tcp, int dscp, struct infra_cache* infra,
429 struct ub_randstate* rnd, int use_caps_for_id, int* availports,
430 int numavailports, size_t unwanted_threshold, int tcp_mss,
431 void (*unwanted_action)(void*), void* unwanted_param, int do_udp,
432 void* sslctx, int delayclose, int tls_use_sni, struct dt_env *dtenv);
435 * Delete outside_network structure.
436 * @param outnet: object to delete.
438 void outside_network_delete(struct outside_network* outnet);
441 * Prepare for quit. Sends no more queries, even if queued up.
442 * @param outnet: object to prepare for removal
444 void outside_network_quit_prepare(struct outside_network* outnet);
447 * Send UDP query, create pending answer.
448 * Changes the ID for the query to be random and unique for that destination.
449 * @param sq: serviced query.
450 * @param packet: wireformat query to send to destination.
451 * @param timeout: in milliseconds from now.
452 * @param callback: function to call on error, timeout or reply.
453 * @param callback_arg: user argument for callback function.
454 * @return: NULL on error for malloc or socket. Else the pending query object.
456 struct pending* pending_udp_query(struct serviced_query* sq,
457 struct sldns_buffer* packet, int timeout, comm_point_callback_type* callback,
461 * Send TCP query. May wait for TCP buffer. Selects ID to be random, and
463 * @param sq: serviced query.
464 * @param packet: wireformat query to send to destination. copied from.
465 * @param timeout: in milliseconds from now.
466 * Timer starts running now. Timer may expire if all buffers are used,
467 * without any query been sent to the server yet.
468 * @param callback: function to call on error, timeout or reply.
469 * @param callback_arg: user argument for callback function.
470 * @return: false on error for malloc or socket. Else the pending TCP object.
472 struct waiting_tcp* pending_tcp_query(struct serviced_query* sq,
473 struct sldns_buffer* packet, int timeout, comm_point_callback_type* callback,
477 * Delete pending answer.
478 * @param outnet: outside network the pending query is part of.
479 * Internal feature: if outnet is NULL, p is not unlinked from rbtree.
482 void pending_delete(struct outside_network* outnet, struct pending* p);
485 * Perform a serviced query to the authoritative servers.
486 * Duplicate efforts are detected, and EDNS, TCP and UDP retry is performed.
487 * @param outnet: outside network, with rbtree of serviced queries.
488 * @param qinfo: query info.
489 * @param flags: flags u16 (host format), includes opcode, CD bit.
490 * @param dnssec: if set, DO bit is set in EDNS queries.
491 * If the value includes BIT_CD, CD bit is set when in EDNS queries.
492 * If the value includes BIT_DO, DO bit is set when in EDNS queries.
493 * @param want_dnssec: signatures are needed, without EDNS the answer is
494 * likely to be useless.
495 * @param nocaps: ignore use_caps_for_id and use unperturbed qname.
496 * @param tcp_upstream: use TCP for upstream queries.
497 * @param ssl_upstream: use SSL for upstream queries.
498 * @param tls_auth_name: when ssl_upstream is true, use this name to check
499 * the server's peer certificate.
500 * @param addr: to which server to send the query.
501 * @param addrlen: length of addr.
502 * @param zone: name of the zone of the delegation point. wireformat dname.
503 This is the delegation point name for which the server is deemed
505 * @param zonelen: length of zone.
506 * @param qstate: module qstate. Mainly for inspecting the available
508 * @param callback: callback function.
509 * @param callback_arg: user argument to callback function.
510 * @param buff: scratch buffer to create query contents in. Empty on exit.
511 * @param env: the module environment.
512 * @return 0 on error, or pointer to serviced query that is used to answer
513 * this serviced query may be shared with other callbacks as well.
515 struct serviced_query* outnet_serviced_query(struct outside_network* outnet,
516 struct query_info* qinfo, uint16_t flags, int dnssec, int want_dnssec,
517 int nocaps, int tcp_upstream, int ssl_upstream, char* tls_auth_name,
518 struct sockaddr_storage* addr, socklen_t addrlen, uint8_t* zone,
519 size_t zonelen, struct module_qstate* qstate,
520 comm_point_callback_type* callback, void* callback_arg,
521 struct sldns_buffer* buff, struct module_env* env);
524 * Remove service query callback.
525 * If that leads to zero callbacks, the query is completely cancelled.
526 * @param sq: serviced query to adjust.
527 * @param cb_arg: callback argument of callback that needs removal.
528 * same as the callback_arg to outnet_serviced_query().
530 void outnet_serviced_query_stop(struct serviced_query* sq, void* cb_arg);
533 * Get memory size in use by outside network.
534 * Counts buffers and outstanding query (serviced queries) malloced data.
535 * @param outnet: outside network structure.
536 * @return size in bytes.
538 size_t outnet_get_mem(struct outside_network* outnet);
541 * Get memory size in use by serviced query while it is servicing callbacks.
542 * This takes into account the pre-deleted status of it; it will be deleted
543 * when the callbacks are done.
544 * @param sq: serviced query.
545 * @return size in bytes.
547 size_t serviced_get_mem(struct serviced_query* sq);
549 /** get TCP file descriptor for address, returns -1 on failure,
550 * tcp_mss is 0 or maxseg size to set for TCP packets. */
551 int outnet_get_tcp_fd(struct sockaddr_storage* addr, socklen_t addrlen, int tcp_mss, int dscp);
554 * Create udp commpoint suitable for sending packets to the destination.
555 * @param outnet: outside_network with the comm_base it is attached to,
556 * with the outgoing interfaces chosen from, and rnd gen for random.
557 * @param cb: callback function for the commpoint.
558 * @param cb_arg: callback argument for cb.
559 * @param to_addr: intended destination.
560 * @param to_addrlen: length of to_addr.
561 * @return commpoint that you can comm_point_send_udp_msg with, or NULL.
563 struct comm_point* outnet_comm_point_for_udp(struct outside_network* outnet,
564 comm_point_callback_type* cb, void* cb_arg,
565 struct sockaddr_storage* to_addr, socklen_t to_addrlen);
568 * Create tcp commpoint suitable for communication to the destination.
569 * It also performs connect() to the to_addr.
570 * @param outnet: outside_network with the comm_base it is attached to,
572 * @param cb: callback function for the commpoint.
573 * @param cb_arg: callback argument for cb.
574 * @param to_addr: intended destination.
575 * @param to_addrlen: length of to_addr.
576 * @param query: initial packet to send writing, in buffer. It is copied
577 * to the commpoint buffer that is created.
578 * @param timeout: timeout for the TCP connection.
579 * timeout in milliseconds, or -1 for no (change to the) timeout.
581 * @param ssl: set to true for TLS.
582 * @param host: hostname for host name verification of TLS (or NULL if no TLS).
583 * @return tcp_out commpoint, or NULL.
585 struct comm_point* outnet_comm_point_for_tcp(struct outside_network* outnet,
586 comm_point_callback_type* cb, void* cb_arg,
587 struct sockaddr_storage* to_addr, socklen_t to_addrlen,
588 struct sldns_buffer* query, int timeout, int ssl, char* host);
591 * Create http commpoint suitable for communication to the destination.
592 * Creates the http request buffer. It also performs connect() to the to_addr.
593 * @param outnet: outside_network with the comm_base it is attached to,
595 * @param cb: callback function for the commpoint.
596 * @param cb_arg: callback argument for cb.
597 * @param to_addr: intended destination.
598 * @param to_addrlen: length of to_addr.
599 * @param timeout: timeout for the TCP connection.
600 * timeout in milliseconds, or -1 for no (change to the) timeout.
602 * @param ssl: set to true for https.
603 * @param host: hostname to use for the destination. part of http request.
604 * @param path: pathname to lookup, eg. name of the file on the destination.
605 * @return http_out commpoint, or NULL.
607 struct comm_point* outnet_comm_point_for_http(struct outside_network* outnet,
608 comm_point_callback_type* cb, void* cb_arg,
609 struct sockaddr_storage* to_addr, socklen_t to_addrlen, int timeout,
610 int ssl, char* host, char* path);
612 /** connect tcp connection to addr, 0 on failure */
613 int outnet_tcp_connect(int s, struct sockaddr_storage* addr, socklen_t addrlen);
615 /** callback for incoming udp answers from the network */
616 int outnet_udp_cb(struct comm_point* c, void* arg, int error,
617 struct comm_reply *reply_info);
619 /** callback for pending tcp connections */
620 int outnet_tcp_cb(struct comm_point* c, void* arg, int error,
621 struct comm_reply *reply_info);
623 /** callback for udp timeout */
624 void pending_udp_timer_cb(void *arg);
626 /** callback for udp delay for timeout */
627 void pending_udp_timer_delay_cb(void *arg);
629 /** callback for outgoing TCP timer event */
630 void outnet_tcptimer(void* arg);
632 /** callback for serviced query UDP answers */
633 int serviced_udp_callback(struct comm_point* c, void* arg, int error,
634 struct comm_reply* rep);
636 /** TCP reply or error callback for serviced queries */
637 int serviced_tcp_callback(struct comm_point* c, void* arg, int error,
638 struct comm_reply* rep);
640 /** compare function of pending rbtree */
641 int pending_cmp(const void* key1, const void* key2);
643 /** compare function of serviced query rbtree */
644 int serviced_cmp(const void* key1, const void* key2);
646 #endif /* OUTSIDE_NETWORK_H */