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