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