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;
64 * Send queries to outside servers and wait for answers from servers.
65 * Contains answer-listen sockets.
67 struct outside_network {
68 /** Base for select calls */
69 struct comm_base* base;
70 /** pointer to time in seconds */
72 /** pointer to time in microseconds */
73 struct timeval* now_tv;
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. */
81 /** use x20 bits to encode additional ID random bits */
83 /** outside network wants to quit. Stop queued msgs from sent. */
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 */
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 */
102 /** if udp is delay-closed (delayed answers do not meet closed port)*/
104 /** timeout for delayclose */
105 struct timeval delay_tv;
107 /** array of outgoing IP4 interfaces */
108 struct port_if* ip4_ifs;
109 /** number of outgoing IP4 interfaces */
112 /** array of outgoing IP6 interfaces */
113 struct port_if* ip6_ifs;
114 /** number of outgoing IP6 interfaces */
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;
122 /** pending udp answers. sorted by id, addr */
124 /** serviced queries, sorted by qbuf, addr, dnssec */
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 */
133 /** dnstap environment */
134 struct dt_env* dtenv;
136 /** maximum segment size of tcp socket */
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.
145 struct pending_tcp **tcp_conns;
146 /** number of tcp communication points. */
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;
159 * Outgoing interface. Ports available and currently used are tracked
163 /** address ready to allocate new socket (except port no). */
164 struct sockaddr_storage addr;
165 /** length of addr field */
168 /** the available ports array. These are unused.
169 * Only the first total-inuse part is filled. */
171 /** the total number of available ports (size of the array) */
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 */
179 /** number of commpoints (and thus also ports) in use */
184 * Outgoing commpoint for UDP port.
187 /** next in free list */
188 struct port_comm* next;
189 /** which port number (when in use) */
191 /** interface it is used in */
193 /** index in the out array of the interface */
195 /** number of outstanding queries on this port */
197 /** UDP commpoint, fd=-1 if not in use */
198 struct comm_point* cp;
202 * A query that has an answer pending for it.
205 /** redblacktree entry, key is the pending struct(id, addr). */
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). */
211 /** remote address. */
212 struct sockaddr_storage addr;
213 /** length of addr field in use. */
215 /** comm point it was sent on (and reply must come back on). */
216 struct port_comm* pc;
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 */
223 /** the outside network it is part of */
224 struct outside_network* outnet;
225 /** the corresponding serviced_query */
226 struct serviced_query* sq;
228 /*---- filled if udp pending is waiting -----*/
229 /** next in waiting list. */
230 struct pending* next_waiting;
231 /** timeout in msec */
233 /** The query itself, the query packet to send. */
235 /** length of query packet. */
240 * Pending TCP query to server.
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 */
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;
254 * Query waiting for TCP buffer.
258 * next in waiting list.
259 * if pkt==0, this points to the pending_tcp structure.
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. */
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.
278 /** length of query packet. */
280 /** callback for the timeout, error or reply to the message */
281 comm_point_callback_t* cb;
282 /** callback user argument */
284 /** if it uses ssl upstream */
289 * Callback to party interested in serviced query results.
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 */
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
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.
311 struct serviced_query {
312 /** The rbtree node, key is this record */
314 /** The query that needs to be answered. Starts with flags u16,
315 * then qdcount, ..., including qname, qtype, qclass. Does not include
318 /** length of qbuf. */
320 /** If an EDNS section is included, the DO/CD bit will be turned on. */
322 /** We want signatures, or else the answer is likely useless */
324 /** ignore capsforid */
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. */
332 /** zone name, uncompressed domain name in wireformat */
334 /** length of zone name */
338 /** current status */
339 enum serviced_query_status {
340 /** initial status */
342 /** UDP with EDNS sent */
343 serviced_query_UDP_EDNS,
344 /** UDP without EDNS sent */
346 /** TCP with EDNS sent */
347 serviced_query_TCP_EDNS,
348 /** TCP without EDNS sent */
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
359 /** variable with current status */
361 /** true if serviced_query is scheduled for deletion already */
363 /** number of UDP retries */
365 /** time last UDP was sent */
366 struct timeval last_sent_time;
367 /** rtt of last (UDP) message */
369 /** do we know edns probe status already, for UDP_EDNS queries */
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 */
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.
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);
417 * Delete outside_network structure.
418 * @param outnet: object to delete.
420 void outside_network_delete(struct outside_network* outnet);
423 * Prepare for quit. Sends no more queries, even if queued up.
424 * @param outnet: object to prepare for removal
426 void outside_network_quit_prepare(struct outside_network* outnet);
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.
438 struct pending* pending_udp_query(struct serviced_query* sq,
439 struct sldns_buffer* packet, int timeout, comm_point_callback_t* callback,
443 * Send TCP query. May wait for TCP buffer. Selects ID to be random, and
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.
454 struct waiting_tcp* pending_tcp_query(struct serviced_query* sq,
455 struct sldns_buffer* packet, int timeout, comm_point_callback_t* callback,
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.
464 void pending_delete(struct outside_network* outnet, struct pending* p);
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
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.
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);
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().
512 void outnet_serviced_query_stop(struct serviced_query* sq, void* cb_arg);
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.
520 size_t outnet_get_mem(struct outside_network* outnet);
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.
529 size_t serviced_get_mem(struct serviced_query* sq);
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);
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);
539 /** callback for udp timeout */
540 void pending_udp_timer_cb(void *arg);
542 /** callback for udp delay for timeout */
543 void pending_udp_timer_delay_cb(void *arg);
545 /** callback for outgoing TCP timer event */
546 void outnet_tcptimer(void* arg);
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);
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);
556 /** compare function of pending rbtree */
557 int pending_cmp(const void* key1, const void* key2);
559 /** compare function of serviced query rbtree */
560 int serviced_cmp(const void* key1, const void* key2);
562 #endif /* OUTSIDE_NETWORK_H */