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