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