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