listen_dnsport.h revision 368129
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 162356345Scy * @param tcp_conn_limit: TCP connection limit info. 163238106Sdes * @param sslctx: nonNULL if ssl context. 164276605Sdes * @param dtenv: nonNULL if dnstap enabled. 165238106Sdes * @param cb: callback function when a request arrives. It is passed 166238106Sdes * the packet and user argument. Return true to send a reply. 167238106Sdes * @param cb_arg: user data argument for callback function. 168238106Sdes * @return: the malloced listening structure, ready for use. NULL on error. 169238106Sdes */ 170368129Scystruct listen_dnsport* 171368129Scylisten_create(struct comm_base* base, struct listen_port* ports, 172368129Scy size_t bufsize, int tcp_accept_count, int tcp_idle_timeout, 173368129Scy int harden_large_queries, uint32_t http_max_streams, 174368129Scy char* http_endpoint, struct tcl_list* tcp_conn_limit, void* sslctx, 175368129Scy struct dt_env* dtenv, comm_point_callback_type* cb, void *cb_arg); 176238106Sdes 177238106Sdes/** 178238106Sdes * delete the listening structure 179238106Sdes * @param listen: listening structure. 180238106Sdes */ 181238106Sdesvoid listen_delete(struct listen_dnsport* listen); 182238106Sdes 183238106Sdes/** 184238106Sdes * delete listen_list of commpoints. Calls commpointdelete() on items. 185238106Sdes * This may close the fds or not depending on flags. 186238106Sdes * @param list: to delete. 187238106Sdes */ 188238106Sdesvoid listen_list_delete(struct listen_list* list); 189238106Sdes 190238106Sdes/** 191238106Sdes * get memory size used by the listening structs 192238106Sdes * @param listen: listening structure. 193238106Sdes * @return: size in bytes. 194238106Sdes */ 195238106Sdessize_t listen_get_mem(struct listen_dnsport* listen); 196238106Sdes 197238106Sdes/** 198238106Sdes * stop accept handlers for TCP (until enabled again) 199238106Sdes * @param listen: listening structure. 200238106Sdes */ 201238106Sdesvoid listen_stop_accept(struct listen_dnsport* listen); 202238106Sdes 203238106Sdes/** 204238106Sdes * start accept handlers for TCP (was stopped before) 205238106Sdes * @param listen: listening structure. 206238106Sdes */ 207238106Sdesvoid listen_start_accept(struct listen_dnsport* listen); 208238106Sdes 209238106Sdes/** 210238106Sdes * Create and bind nonblocking UDP socket 211238106Sdes * @param family: for socket call. 212238106Sdes * @param socktype: for socket call. 213238106Sdes * @param addr: for bind call. 214238106Sdes * @param addrlen: for bind call. 215238106Sdes * @param v6only: if enabled, IP6 sockets get IP6ONLY option set. 216238106Sdes * if enabled with value 2 IP6ONLY option is disabled. 217238106Sdes * @param inuse: on error, this is set true if the port was in use. 218238106Sdes * @param noproto: on error, this is set true if cause is that the 219238106Sdes IPv6 proto (family) is not available. 220238106Sdes * @param rcv: set size on rcvbuf with socket option, if 0 it is not set. 221238106Sdes * @param snd: set size on sndbuf with socket option, if 0 it is not set. 222266114Sdes * @param listen: if true, this is a listening UDP port, eg port 53, and 223266114Sdes * set SO_REUSEADDR on it. 224266114Sdes * @param reuseport: if nonNULL and true, try to set SO_REUSEPORT on 225266114Sdes * listening UDP port. Set to false on return if it failed to do so. 226287917Sdes * @param transparent: set IP_TRANSPARENT socket option. 227307729Sdes * @param freebind: set IP_FREEBIND socket option. 228356345Scy * @param use_systemd: if true, fetch sockets from systemd. 229366095Scy * @param dscp: DSCP to use. 230238106Sdes * @return: the socket. -1 on error. 231238106Sdes */ 232238106Sdesint create_udp_sock(int family, int socktype, struct sockaddr* addr, 233238106Sdes socklen_t addrlen, int v6only, int* inuse, int* noproto, int rcv, 234366095Scy int snd, int listen, int* reuseport, int transparent, int freebind, int use_systemd, int dscp); 235238106Sdes 236238106Sdes/** 237238106Sdes * Create and bind TCP listening socket 238238106Sdes * @param addr: address info ready to make socket. 239238106Sdes * @param v6only: enable ip6 only flag on ip6 sockets. 240238106Sdes * @param noproto: if error caused by lack of protocol support. 241266114Sdes * @param reuseport: if nonNULL and true, try to set SO_REUSEPORT on 242266114Sdes * listening UDP port. Set to false on return if it failed to do so. 243287917Sdes * @param transparent: set IP_TRANSPARENT socket option. 244296415Sdes * @param mss: maximum segment size of the socket. if zero, leaves the default. 245368129Scy * @param nodelay: if true set TCP_NODELAY and TCP_QUICKACK socket options. 246307729Sdes * @param freebind: set IP_FREEBIND socket option. 247356345Scy * @param use_systemd: if true, fetch sockets from systemd. 248366095Scy * @param dscp: DSCP to use. 249238106Sdes * @return: the socket. -1 on error. 250238106Sdes */ 251266114Sdesint create_tcp_accept_sock(struct addrinfo *addr, int v6only, int* noproto, 252368129Scy int* reuseport, int transparent, int mss, int nodelay, int freebind, 253368129Scy int use_systemd, int dscp); 254238106Sdes 255276699Sdes/** 256276699Sdes * Create and bind local listening socket 257276699Sdes * @param path: path to the socket. 258276699Sdes * @param noproto: on error, this is set true if cause is that local sockets 259276699Sdes * are not supported. 260356345Scy * @param use_systemd: if true, fetch sockets from systemd. 261276699Sdes * @return: the socket. -1 on error. 262276699Sdes */ 263356345Scyint create_local_accept_sock(const char* path, int* noproto, int use_systemd); 264276699Sdes 265356345Scy/** 266356345Scy * TCP request info. List of requests outstanding on the channel, that 267356345Scy * are asked for but not yet answered back. 268356345Scy */ 269356345Scystruct tcp_req_info { 270356345Scy /** the TCP comm point for this. Its buffer is used for read/write */ 271356345Scy struct comm_point* cp; 272356345Scy /** the buffer to use to spool reply from mesh into, 273356345Scy * it can then be copied to the result list and written. 274356345Scy * it is a pointer to the shared udp buffer. */ 275356345Scy struct sldns_buffer* spool_buffer; 276356345Scy /** are we in worker_handle function call (for recursion callback)*/ 277356345Scy int in_worker_handle; 278356345Scy /** is the comm point dropped (by worker handle). 279356345Scy * That means we have to disconnect the channel. */ 280356345Scy int is_drop; 281356345Scy /** is the comm point set to send_reply (by mesh new client in worker 282356345Scy * handle), if so answer is available in c.buffer */ 283356345Scy int is_reply; 284356345Scy /** read channel has closed, just write pending results */ 285356345Scy int read_is_closed; 286356345Scy /** read again */ 287356345Scy int read_again; 288356345Scy /** number of outstanding requests */ 289356345Scy int num_open_req; 290356345Scy /** list of outstanding requests */ 291356345Scy struct tcp_req_open_item* open_req_list; 292356345Scy /** number of pending writeable results */ 293356345Scy int num_done_req; 294356345Scy /** list of pending writable result packets, malloced one at a time */ 295356345Scy struct tcp_req_done_item* done_req_list; 296356345Scy}; 297356345Scy 298356345Scy/** 299356345Scy * List of open items in TCP channel 300356345Scy */ 301356345Scystruct tcp_req_open_item { 302356345Scy /** next in list */ 303356345Scy struct tcp_req_open_item* next; 304356345Scy /** the mesh area of the mesh_state */ 305356345Scy struct mesh_area* mesh; 306356345Scy /** the mesh state */ 307356345Scy struct mesh_state* mesh_state; 308356345Scy}; 309356345Scy 310356345Scy/** 311356345Scy * List of done items in TCP channel 312356345Scy */ 313356345Scystruct tcp_req_done_item { 314356345Scy /** next in list */ 315356345Scy struct tcp_req_done_item* next; 316356345Scy /** the buffer with packet contents */ 317356345Scy uint8_t* buf; 318356345Scy /** length of the buffer */ 319356345Scy size_t len; 320356345Scy}; 321356345Scy 322356345Scy/** 323356345Scy * Create tcp request info structure that keeps track of open 324356345Scy * requests on the TCP channel that are resolved at the same time, 325356345Scy * and the pending results that have to get written back to that client. 326356345Scy * @param spoolbuf: shared buffer 327356345Scy * @return new structure or NULL on alloc failure. 328356345Scy */ 329356345Scystruct tcp_req_info* tcp_req_info_create(struct sldns_buffer* spoolbuf); 330356345Scy 331356345Scy/** 332356345Scy * Delete tcp request structure. Called by owning commpoint. 333356345Scy * Removes mesh entry references and stored results from the lists. 334356345Scy * @param req: the tcp request info 335356345Scy */ 336356345Scyvoid tcp_req_info_delete(struct tcp_req_info* req); 337356345Scy 338356345Scy/** 339356345Scy * Clear tcp request structure. Removes list entries, sets it up ready 340356345Scy * for the next connection. 341356345Scy * @param req: tcp request info structure. 342356345Scy */ 343356345Scyvoid tcp_req_info_clear(struct tcp_req_info* req); 344356345Scy 345356345Scy/** 346356345Scy * Remove mesh state entry from list in tcp_req_info. 347356345Scy * caller has to manage the mesh state reply entry in the mesh state. 348356345Scy * @param req: the tcp req info that has the entry removed from the list. 349356345Scy * @param m: the state removed from the list. 350356345Scy */ 351356345Scyvoid tcp_req_info_remove_mesh_state(struct tcp_req_info* req, 352356345Scy struct mesh_state* m); 353356345Scy 354356345Scy/** 355356345Scy * Handle write done of the last result packet 356356345Scy * @param req: the tcp req info. 357356345Scy */ 358356345Scyvoid tcp_req_info_handle_writedone(struct tcp_req_info* req); 359356345Scy 360356345Scy/** 361356345Scy * Handle read done of a new request from the client 362356345Scy * @param req: the tcp req info. 363356345Scy */ 364356345Scyvoid tcp_req_info_handle_readdone(struct tcp_req_info* req); 365356345Scy 366356345Scy/** 367356345Scy * Add mesh state to the tcp req list of open requests. 368356345Scy * So the comm_reply can be removed off the mesh reply list when 369356345Scy * the tcp channel has to be closed (for other reasons then that that 370356345Scy * request was done, eg. channel closed by client or some format error). 371356345Scy * @param req: tcp req info structure. It keeps track of the simultaneous 372356345Scy * requests and results on a tcp (or TLS) channel. 373356345Scy * @param mesh: mesh area for the state. 374356345Scy * @param m: mesh state to add. 375356345Scy * @return 0 on failure (malloc failure). 376356345Scy */ 377356345Scyint tcp_req_info_add_meshstate(struct tcp_req_info* req, 378356345Scy struct mesh_area* mesh, struct mesh_state* m); 379356345Scy 380356345Scy/** 381356345Scy * Send reply on tcp simultaneous answer channel. May queue it up. 382356345Scy * @param req: request info structure. 383356345Scy */ 384356345Scyvoid tcp_req_info_send_reply(struct tcp_req_info* req); 385356345Scy 386356345Scy/** the read channel has closed 387356345Scy * @param req: request. remaining queries are looked up and answered. 388356345Scy * @return zero if nothing to do, just close the tcp. 389356345Scy */ 390356345Scyint tcp_req_info_handle_read_close(struct tcp_req_info* req); 391356345Scy 392356345Scy/** get the size of currently used tcp stream wait buffers (in bytes) */ 393356345Scysize_t tcp_req_info_get_stream_buffer_size(void); 394356345Scy 395368129Scy/** get the size of currently used HTTP2 query buffers (in bytes) */ 396368129Scysize_t http2_get_query_buffer_size(void); 397368129Scy/** get the size of currently used HTTP2 response buffers (in bytes) */ 398368129Scysize_t http2_get_response_buffer_size(void); 399368129Scy 400368129Scy#ifdef HAVE_NGHTTP2 401368129Scy/** 402368129Scy * Create nghttp2 callbacks to handle HTTP2 requests. 403368129Scy * @return malloc'ed struct, NULL on failure 404368129Scy */ 405368129Scynghttp2_session_callbacks* http2_req_callbacks_create(); 406368129Scy 407368129Scy/** Free http2 stream buffers and decrease buffer counters */ 408368129Scyvoid http2_req_stream_clear(struct http2_stream* h2_stream); 409368129Scy 410368129Scy/** 411368129Scy * DNS response ready to be submitted to nghttp2, to be prepared for sending 412368129Scy * out. Response is stored in c->buffer. Copy to rbuffer because the c->buffer 413368129Scy * might be used before this will be send out. 414368129Scy * @param h2_session: http2 session, containing c->buffer which contains answer 415368129Scy * @param h2_stream: http2 stream, containing buffer to store answer in 416368129Scy * @return 0 on error, 1 otherwise 417368129Scy */ 418368129Scyint http2_submit_dns_response(struct http2_session* h2_session); 419368129Scy#else 420368129Scyint http2_submit_dns_response(void* v); 421368129Scy#endif /* HAVE_NGHTTP2 */ 422368129Scy 423366095Scychar* set_ip_dscp(int socket, int addrfamily, int ds); 424366095Scy 425238106Sdes#endif /* LISTEN_DNSPORT_H */ 426