1238106Sdes/*
2238106Sdes * services/listen_dnsport.h - listen on port 53 for incoming DNS queries.
3238106Sdes *
4238106Sdes * Copyright (c) 2007, NLnet Labs. All rights reserved.
5238106Sdes *
6238106Sdes * This software is open source.
7238106Sdes *
8238106Sdes * Redistribution and use in source and binary forms, with or without
9238106Sdes * modification, are permitted provided that the following conditions
10238106Sdes * are met:
11238106Sdes *
12238106Sdes * Redistributions of source code must retain the above copyright notice,
13238106Sdes * this list of conditions and the following disclaimer.
14238106Sdes *
15238106Sdes * Redistributions in binary form must reproduce the above copyright notice,
16238106Sdes * this list of conditions and the following disclaimer in the documentation
17238106Sdes * and/or other materials provided with the distribution.
18238106Sdes *
19238106Sdes * Neither the name of the NLNET LABS nor the names of its contributors may
20238106Sdes * be used to endorse or promote products derived from this software without
21238106Sdes * specific prior written permission.
22238106Sdes *
23238106Sdes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24266114Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25266114Sdes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26266114Sdes * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27266114Sdes * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28266114Sdes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29266114Sdes * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30266114Sdes * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31266114Sdes * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32266114Sdes * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33266114Sdes * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34238106Sdes */
35238106Sdes
36238106Sdes/**
37238106Sdes * \file
38238106Sdes *
39238106Sdes * This file has functions to get queries from clients.
40238106Sdes */
41238106Sdes
42238106Sdes#ifndef LISTEN_DNSPORT_H
43238106Sdes#define LISTEN_DNSPORT_H
44238106Sdes
45238106Sdes#include "util/netevent.h"
46368129Scy#ifdef HAVE_NGHTTP2_NGHTTP2_H
47368129Scy#include <nghttp2/nghttp2.h>
48368129Scy#endif
49238106Sdesstruct listen_list;
50238106Sdesstruct config_file;
51238106Sdesstruct addrinfo;
52266114Sdesstruct sldns_buffer;
53356345Scystruct tcl_list;
54238106Sdes
55238106Sdes/**
56238106Sdes * Listening for queries structure.
57238106Sdes * Contains list of query-listen sockets.
58238106Sdes */
59238106Sdesstruct listen_dnsport {
60238106Sdes	/** Base for select calls */
61238106Sdes	struct comm_base* base;
62238106Sdes
63238106Sdes	/** buffer shared by UDP connections, since there is only one
64238106Sdes	    datagram at any time. */
65266114Sdes	struct sldns_buffer* udp_buff;
66356345Scy#ifdef USE_DNSCRYPT
67356345Scy	struct sldns_buffer* dnscrypt_udp_buff;
68356345Scy#endif
69238106Sdes	/** list of comm points used to get incoming events */
70238106Sdes	struct listen_list* cps;
71238106Sdes};
72238106Sdes
73238106Sdes/**
74238106Sdes * Single linked list to store event points.
75238106Sdes */
76238106Sdesstruct listen_list {
77238106Sdes	/** next in list */
78238106Sdes	struct listen_list* next;
79238106Sdes	/** event info */
80238106Sdes	struct comm_point* com;
81238106Sdes};
82238106Sdes
83238106Sdes/**
84238106Sdes * type of ports
85238106Sdes */
86238106Sdesenum listen_type {
87238106Sdes	/** udp type */
88238106Sdes	listen_type_udp,
89238106Sdes	/** tcp type */
90238106Sdes	listen_type_tcp,
91238106Sdes	/** udp ipv6 (v4mapped) for use with ancillary data */
92238106Sdes	listen_type_udpancil,
93238106Sdes	/** ssl over tcp type */
94356345Scy	listen_type_ssl,
95356345Scy	/** udp type  + dnscrypt*/
96356345Scy	listen_type_udp_dnscrypt,
97356345Scy	/** tcp type + dnscrypt */
98356345Scy	listen_type_tcp_dnscrypt,
99356345Scy	/** udp ipv6 (v4mapped) for use with ancillary data + dnscrypt*/
100368129Scy	listen_type_udpancil_dnscrypt,
101368129Scy	/** HTTP(2) over TLS over TCP */
102368129Scy	listen_type_http
103238106Sdes};
104238106Sdes
105238106Sdes/**
106238106Sdes * Single linked list to store shared ports that have been
107238106Sdes * opened for use by all threads.
108238106Sdes */
109238106Sdesstruct listen_port {
110238106Sdes	/** next in list */
111238106Sdes	struct listen_port* next;
112238106Sdes	/** file descriptor, open and ready for use */
113238106Sdes	int fd;
114238106Sdes	/** type of file descriptor, udp or tcp */
115238106Sdes	enum listen_type ftype;
116238106Sdes};
117238106Sdes
118238106Sdes/**
119238106Sdes * Create shared listening ports
120238106Sdes * Getaddrinfo, create socket, bind and listen to zero or more
121238106Sdes * interfaces for IP4 and/or IP6, for UDP and/or TCP.
122238106Sdes * On the given port number. It creates the sockets.
123238106Sdes * @param cfg: settings on what ports to open.
124368129Scy * @param ifs: interfaces to open, array of IP addresses, "ip[@port]".
125368129Scy * @param num_ifs: length of ifs.
126266114Sdes * @param reuseport: set to true if you want reuseport, or NULL to not have it,
127266114Sdes *   set to false on exit if reuseport failed to apply (because of no
128266114Sdes *   kernel support).
129238106Sdes * @return: linked list of ports or NULL on error.
130238106Sdes */
131266114Sdesstruct listen_port* listening_ports_open(struct config_file* cfg,
132368129Scy	char** ifs, int num_ifs, int* reuseport);
133238106Sdes
134238106Sdes/**
135238106Sdes * Close and delete the (list of) listening ports.
136238106Sdes */
137238106Sdesvoid listening_ports_free(struct listen_port* list);
138238106Sdes
139238106Sdes/**
140368129Scy * Resolve interface names in config and store result IP addresses
141368129Scy * @param cfg: config
142368129Scy * @param resif: string array (malloced array of malloced strings) with
143368129Scy * 	result.  NULL if cfg has none.
144368129Scy * @param num_resif: length of resif.  Zero if cfg has zero num_ifs.
145368129Scy * @return 0 on failure.
146368129Scy */
147368129Scyint resolve_interface_names(struct config_file* cfg, char*** resif,
148368129Scy	int* num_resif);
149368129Scy
150368129Scy/**
151238106Sdes * Create commpoints with for this thread for the shared ports.
152238106Sdes * @param base: the comm_base that provides event functionality.
153238106Sdes *	for default all ifs.
154238106Sdes * @param ports: the list of shared ports.
155238106Sdes * @param bufsize: size of datagram buffer.
156238106Sdes * @param tcp_accept_count: max number of simultaneous TCP connections
157238106Sdes * 	from clients.
158356345Scy * @param tcp_idle_timeout: idle timeout for TCP connections in msec.
159368129Scy * @param harden_large_queries: whether query size should be limited.
160368129Scy * @param http_max_streams: maximum number of HTTP/2 streams per connection.
161368129Scy * @param http_endpoint: HTTP endpoint to service queries on
162368693Scy * @param http_notls: no TLS for http downstream
163356345Scy * @param tcp_conn_limit: TCP connection limit info.
164238106Sdes * @param sslctx: nonNULL if ssl context.
165276605Sdes * @param dtenv: nonNULL if dnstap enabled.
166238106Sdes * @param cb: callback function when a request arrives. It is passed
167238106Sdes *	  the packet and user argument. Return true to send a reply.
168238106Sdes * @param cb_arg: user data argument for callback function.
169238106Sdes * @return: the malloced listening structure, ready for use. NULL on error.
170238106Sdes */
171368129Scystruct listen_dnsport*
172368129Scylisten_create(struct comm_base* base, struct listen_port* ports,
173368129Scy	size_t bufsize, int tcp_accept_count, int tcp_idle_timeout,
174368129Scy	int harden_large_queries, uint32_t http_max_streams,
175368693Scy	char* http_endpoint, int http_notls, struct tcl_list* tcp_conn_limit,
176368693Scy	void* sslctx, struct dt_env* dtenv, comm_point_callback_type* cb,
177368693Scy	void *cb_arg);
178238106Sdes
179238106Sdes/**
180238106Sdes * delete the listening structure
181238106Sdes * @param listen: listening structure.
182238106Sdes */
183238106Sdesvoid listen_delete(struct listen_dnsport* listen);
184238106Sdes
185238106Sdes/**
186238106Sdes * delete listen_list of commpoints. Calls commpointdelete() on items.
187238106Sdes * This may close the fds or not depending on flags.
188238106Sdes * @param list: to delete.
189238106Sdes */
190238106Sdesvoid listen_list_delete(struct listen_list* list);
191238106Sdes
192238106Sdes/**
193238106Sdes * get memory size used by the listening structs
194238106Sdes * @param listen: listening structure.
195238106Sdes * @return: size in bytes.
196238106Sdes */
197238106Sdessize_t listen_get_mem(struct listen_dnsport* listen);
198238106Sdes
199238106Sdes/**
200238106Sdes * stop accept handlers for TCP (until enabled again)
201238106Sdes * @param listen: listening structure.
202238106Sdes */
203238106Sdesvoid listen_stop_accept(struct listen_dnsport* listen);
204238106Sdes
205238106Sdes/**
206238106Sdes * start accept handlers for TCP (was stopped before)
207238106Sdes * @param listen: listening structure.
208238106Sdes */
209238106Sdesvoid listen_start_accept(struct listen_dnsport* listen);
210238106Sdes
211238106Sdes/**
212238106Sdes * Create and bind nonblocking UDP socket
213238106Sdes * @param family: for socket call.
214238106Sdes * @param socktype: for socket call.
215238106Sdes * @param addr: for bind call.
216238106Sdes * @param addrlen: for bind call.
217238106Sdes * @param v6only: if enabled, IP6 sockets get IP6ONLY option set.
218238106Sdes * 	if enabled with value 2 IP6ONLY option is disabled.
219238106Sdes * @param inuse: on error, this is set true if the port was in use.
220238106Sdes * @param noproto: on error, this is set true if cause is that the
221238106Sdes	IPv6 proto (family) is not available.
222238106Sdes * @param rcv: set size on rcvbuf with socket option, if 0 it is not set.
223238106Sdes * @param snd: set size on sndbuf with socket option, if 0 it is not set.
224266114Sdes * @param listen: if true, this is a listening UDP port, eg port 53, and
225266114Sdes * 	set SO_REUSEADDR on it.
226266114Sdes * @param reuseport: if nonNULL and true, try to set SO_REUSEPORT on
227266114Sdes * 	listening UDP port.  Set to false on return if it failed to do so.
228287917Sdes * @param transparent: set IP_TRANSPARENT socket option.
229307729Sdes * @param freebind: set IP_FREEBIND socket option.
230356345Scy * @param use_systemd: if true, fetch sockets from systemd.
231366095Scy * @param dscp: DSCP to use.
232238106Sdes * @return: the socket. -1 on error.
233238106Sdes */
234238106Sdesint create_udp_sock(int family, int socktype, struct sockaddr* addr,
235238106Sdes	socklen_t addrlen, int v6only, int* inuse, int* noproto, int rcv,
236366095Scy	int snd, int listen, int* reuseport, int transparent, int freebind, int use_systemd, int dscp);
237238106Sdes
238238106Sdes/**
239238106Sdes * Create and bind TCP listening socket
240238106Sdes * @param addr: address info ready to make socket.
241238106Sdes * @param v6only: enable ip6 only flag on ip6 sockets.
242238106Sdes * @param noproto: if error caused by lack of protocol support.
243266114Sdes * @param reuseport: if nonNULL and true, try to set SO_REUSEPORT on
244266114Sdes * 	listening UDP port.  Set to false on return if it failed to do so.
245287917Sdes * @param transparent: set IP_TRANSPARENT socket option.
246296415Sdes * @param mss: maximum segment size of the socket. if zero, leaves the default.
247368129Scy * @param nodelay: if true set TCP_NODELAY and TCP_QUICKACK socket options.
248307729Sdes * @param freebind: set IP_FREEBIND socket option.
249356345Scy * @param use_systemd: if true, fetch sockets from systemd.
250366095Scy * @param dscp: DSCP to use.
251238106Sdes * @return: the socket. -1 on error.
252238106Sdes */
253266114Sdesint create_tcp_accept_sock(struct addrinfo *addr, int v6only, int* noproto,
254368129Scy	int* reuseport, int transparent, int mss, int nodelay, int freebind,
255368129Scy	int use_systemd, int dscp);
256238106Sdes
257276699Sdes/**
258276699Sdes * Create and bind local listening socket
259276699Sdes * @param path: path to the socket.
260276699Sdes * @param noproto: on error, this is set true if cause is that local sockets
261276699Sdes *	are not supported.
262356345Scy * @param use_systemd: if true, fetch sockets from systemd.
263276699Sdes * @return: the socket. -1 on error.
264276699Sdes */
265356345Scyint create_local_accept_sock(const char* path, int* noproto, int use_systemd);
266276699Sdes
267356345Scy/**
268356345Scy * TCP request info.  List of requests outstanding on the channel, that
269356345Scy * are asked for but not yet answered back.
270356345Scy */
271356345Scystruct tcp_req_info {
272356345Scy	/** the TCP comm point for this.  Its buffer is used for read/write */
273356345Scy	struct comm_point* cp;
274356345Scy	/** the buffer to use to spool reply from mesh into,
275356345Scy	 * it can then be copied to the result list and written.
276356345Scy	 * it is a pointer to the shared udp buffer. */
277356345Scy	struct sldns_buffer* spool_buffer;
278356345Scy	/** are we in worker_handle function call (for recursion callback)*/
279356345Scy	int in_worker_handle;
280356345Scy	/** is the comm point dropped (by worker handle).
281356345Scy	 * That means we have to disconnect the channel. */
282356345Scy	int is_drop;
283356345Scy	/** is the comm point set to send_reply (by mesh new client in worker
284356345Scy	 * handle), if so answer is available in c.buffer */
285356345Scy	int is_reply;
286356345Scy	/** read channel has closed, just write pending results */
287356345Scy	int read_is_closed;
288356345Scy	/** read again */
289356345Scy	int read_again;
290356345Scy	/** number of outstanding requests */
291356345Scy	int num_open_req;
292356345Scy	/** list of outstanding requests */
293356345Scy	struct tcp_req_open_item* open_req_list;
294356345Scy	/** number of pending writeable results */
295356345Scy	int num_done_req;
296356345Scy	/** list of pending writable result packets, malloced one at a time */
297356345Scy	struct tcp_req_done_item* done_req_list;
298356345Scy};
299356345Scy
300356345Scy/**
301356345Scy * List of open items in TCP channel
302356345Scy */
303356345Scystruct tcp_req_open_item {
304356345Scy	/** next in list */
305356345Scy	struct tcp_req_open_item* next;
306356345Scy	/** the mesh area of the mesh_state */
307356345Scy	struct mesh_area* mesh;
308356345Scy	/** the mesh state */
309356345Scy	struct mesh_state* mesh_state;
310356345Scy};
311356345Scy
312356345Scy/**
313356345Scy * List of done items in TCP channel
314356345Scy */
315356345Scystruct tcp_req_done_item {
316356345Scy	/** next in list */
317356345Scy	struct tcp_req_done_item* next;
318356345Scy	/** the buffer with packet contents */
319356345Scy	uint8_t* buf;
320356345Scy	/** length of the buffer */
321356345Scy	size_t len;
322356345Scy};
323356345Scy
324356345Scy/**
325356345Scy * Create tcp request info structure that keeps track of open
326356345Scy * requests on the TCP channel that are resolved at the same time,
327356345Scy * and the pending results that have to get written back to that client.
328356345Scy * @param spoolbuf: shared buffer
329356345Scy * @return new structure or NULL on alloc failure.
330356345Scy */
331356345Scystruct tcp_req_info* tcp_req_info_create(struct sldns_buffer* spoolbuf);
332356345Scy
333356345Scy/**
334356345Scy * Delete tcp request structure.  Called by owning commpoint.
335356345Scy * Removes mesh entry references and stored results from the lists.
336356345Scy * @param req: the tcp request info
337356345Scy */
338356345Scyvoid tcp_req_info_delete(struct tcp_req_info* req);
339356345Scy
340356345Scy/**
341356345Scy * Clear tcp request structure.  Removes list entries, sets it up ready
342356345Scy * for the next connection.
343356345Scy * @param req: tcp request info structure.
344356345Scy */
345356345Scyvoid tcp_req_info_clear(struct tcp_req_info* req);
346356345Scy
347356345Scy/**
348356345Scy * Remove mesh state entry from list in tcp_req_info.
349356345Scy * caller has to manage the mesh state reply entry in the mesh state.
350356345Scy * @param req: the tcp req info that has the entry removed from the list.
351356345Scy * @param m: the state removed from the list.
352356345Scy */
353356345Scyvoid tcp_req_info_remove_mesh_state(struct tcp_req_info* req,
354356345Scy	struct mesh_state* m);
355356345Scy
356356345Scy/**
357356345Scy * Handle write done of the last result packet
358356345Scy * @param req: the tcp req info.
359356345Scy */
360356345Scyvoid tcp_req_info_handle_writedone(struct tcp_req_info* req);
361356345Scy
362356345Scy/**
363356345Scy * Handle read done of a new request from the client
364356345Scy * @param req: the tcp req info.
365356345Scy */
366356345Scyvoid tcp_req_info_handle_readdone(struct tcp_req_info* req);
367356345Scy
368356345Scy/**
369356345Scy * Add mesh state to the tcp req list of open requests.
370356345Scy * So the comm_reply can be removed off the mesh reply list when
371356345Scy * the tcp channel has to be closed (for other reasons then that that
372356345Scy * request was done, eg. channel closed by client or some format error).
373356345Scy * @param req: tcp req info structure.  It keeps track of the simultaneous
374356345Scy * 	requests and results on a tcp (or TLS) channel.
375356345Scy * @param mesh: mesh area for the state.
376356345Scy * @param m: mesh state to add.
377356345Scy * @return 0 on failure (malloc failure).
378356345Scy */
379356345Scyint tcp_req_info_add_meshstate(struct tcp_req_info* req,
380356345Scy	struct mesh_area* mesh, struct mesh_state* m);
381356345Scy
382356345Scy/**
383356345Scy * Send reply on tcp simultaneous answer channel.  May queue it up.
384356345Scy * @param req: request info structure.
385356345Scy */
386356345Scyvoid tcp_req_info_send_reply(struct tcp_req_info* req);
387356345Scy
388356345Scy/** the read channel has closed
389356345Scy * @param req: request. remaining queries are looked up and answered.
390356345Scy * @return zero if nothing to do, just close the tcp.
391356345Scy */
392356345Scyint tcp_req_info_handle_read_close(struct tcp_req_info* req);
393356345Scy
394356345Scy/** get the size of currently used tcp stream wait buffers (in bytes) */
395356345Scysize_t tcp_req_info_get_stream_buffer_size(void);
396356345Scy
397368129Scy/** get the size of currently used HTTP2 query buffers (in bytes) */
398368129Scysize_t http2_get_query_buffer_size(void);
399368129Scy/** get the size of currently used HTTP2 response buffers (in bytes) */
400368129Scysize_t http2_get_response_buffer_size(void);
401368129Scy
402368129Scy#ifdef HAVE_NGHTTP2
403368129Scy/**
404368129Scy * Create nghttp2 callbacks to handle HTTP2 requests.
405368129Scy * @return malloc'ed struct, NULL on failure
406368129Scy */
407369939Sgit2svnnghttp2_session_callbacks* http2_req_callbacks_create(void);
408368129Scy
409368129Scy/** Free http2 stream buffers and decrease buffer counters */
410368129Scyvoid http2_req_stream_clear(struct http2_stream* h2_stream);
411368129Scy
412368129Scy/**
413368129Scy * DNS response ready to be submitted to nghttp2, to be prepared for sending
414368129Scy * out. Response is stored in c->buffer. Copy to rbuffer because the c->buffer
415368129Scy * might be used before this will be send out.
416368129Scy * @param h2_session: http2 session, containing c->buffer which contains answer
417368129Scy * @param h2_stream: http2 stream, containing buffer to store answer in
418368129Scy * @return 0 on error, 1 otherwise
419368129Scy */
420368129Scyint http2_submit_dns_response(struct http2_session* h2_session);
421368129Scy#else
422368129Scyint http2_submit_dns_response(void* v);
423368129Scy#endif /* HAVE_NGHTTP2 */
424368129Scy
425366095Scychar* set_ip_dscp(int socket, int addrfamily, int ds);
426366095Scy
427238106Sdes#endif /* LISTEN_DNSPORT_H */
428