1 /* 2 * services/listen_dnsport.h - listen on port 53 for incoming DNS queries. 3 * 4 * Copyright (c) 2007, NLnet Labs. All rights reserved. 5 * 6 * This software is open source. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * Redistributions of source code must retain the above copyright notice, 13 * this list of conditions and the following disclaimer. 14 * 15 * Redistributions in binary form must reproduce the above copyright notice, 16 * this list of conditions and the following disclaimer in the documentation 17 * and/or other materials provided with the distribution. 18 * 19 * Neither the name of the NLNET LABS nor the names of its contributors may 20 * be used to endorse or promote products derived from this software without 21 * specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36 /** 37 * \file 38 * 39 * This file has functions to get queries from clients. 40 */ 41 42 #ifndef LISTEN_DNSPORT_H 43 #define LISTEN_DNSPORT_H 44 45 #include "util/netevent.h" 46 #include "daemon/acl_list.h" 47 #ifdef HAVE_NGHTTP2_NGHTTP2_H 48 #include <nghttp2/nghttp2.h> 49 #endif 50 struct listen_list; 51 struct config_file; 52 struct addrinfo; 53 struct sldns_buffer; 54 struct tcl_list; 55 56 /** 57 * Listening for queries structure. 58 * Contains list of query-listen sockets. 59 */ 60 struct listen_dnsport { 61 /** Base for select calls */ 62 struct comm_base* base; 63 64 /** buffer shared by UDP connections, since there is only one 65 datagram at any time. */ 66 struct sldns_buffer* udp_buff; 67 #ifdef USE_DNSCRYPT 68 struct sldns_buffer* dnscrypt_udp_buff; 69 #endif 70 /** list of comm points used to get incoming events */ 71 struct listen_list* cps; 72 }; 73 74 /** 75 * Single linked list to store event points. 76 */ 77 struct listen_list { 78 /** next in list */ 79 struct listen_list* next; 80 /** event info */ 81 struct comm_point* com; 82 }; 83 84 /** 85 * type of ports 86 */ 87 enum listen_type { 88 /** udp type */ 89 listen_type_udp, 90 /** tcp type */ 91 listen_type_tcp, 92 /** udp ipv6 (v4mapped) for use with ancillary data */ 93 listen_type_udpancil, 94 /** ssl over tcp type */ 95 listen_type_ssl, 96 /** udp type + dnscrypt*/ 97 listen_type_udp_dnscrypt, 98 /** tcp type + dnscrypt */ 99 listen_type_tcp_dnscrypt, 100 /** udp ipv6 (v4mapped) for use with ancillary data + dnscrypt*/ 101 listen_type_udpancil_dnscrypt, 102 /** HTTP(2) over TLS over TCP */ 103 listen_type_http 104 }; 105 106 /* 107 * socket properties (just like NSD nsd_socket structure definition) 108 */ 109 struct unbound_socket { 110 /** socket-address structure */ 111 struct addrinfo* addr; 112 /** socket descriptor returned by socket() syscall */ 113 int s; 114 /** address family (AF_INET/IF_INET6) */ 115 int fam; 116 /** ACL on the socket (listening interface) */ 117 struct acl_addr* acl; 118 }; 119 120 /** 121 * Single linked list to store shared ports that have been 122 * opened for use by all threads. 123 */ 124 struct listen_port { 125 /** next in list */ 126 struct listen_port* next; 127 /** file descriptor, open and ready for use */ 128 int fd; 129 /** type of file descriptor, udp or tcp */ 130 enum listen_type ftype; 131 /** if the port should support PROXYv2 */ 132 int pp2_enabled; 133 /** fill in unbound_socket structure for every opened socket at 134 * Unbound startup */ 135 struct unbound_socket* socket; 136 }; 137 138 /** 139 * Create shared listening ports 140 * Getaddrinfo, create socket, bind and listen to zero or more 141 * interfaces for IP4 and/or IP6, for UDP and/or TCP. 142 * On the given port number. It creates the sockets. 143 * @param cfg: settings on what ports to open. 144 * @param ifs: interfaces to open, array of IP addresses, "ip[@port]". 145 * @param num_ifs: length of ifs. 146 * @param reuseport: set to true if you want reuseport, or NULL to not have it, 147 * set to false on exit if reuseport failed to apply (because of no 148 * kernel support). 149 * @return: linked list of ports or NULL on error. 150 */ 151 struct listen_port* listening_ports_open(struct config_file* cfg, 152 char** ifs, int num_ifs, int* reuseport); 153 154 /** 155 * Close and delete the (list of) listening ports. 156 */ 157 void listening_ports_free(struct listen_port* list); 158 159 struct config_strlist; 160 /** 161 * Resolve interface names in config and store result IP addresses 162 * @param ifs: array of interfaces. The list of interface names, if not NULL. 163 * @param num_ifs: length of ifs array. 164 * @param list: if not NULL, this is used as the list of interface names. 165 * @param resif: string array (malloced array of malloced strings) with 166 * result. NULL if cfg has none. 167 * @param num_resif: length of resif. Zero if cfg has zero num_ifs. 168 * @return 0 on failure. 169 */ 170 int resolve_interface_names(char** ifs, int num_ifs, 171 struct config_strlist* list, char*** resif, int* num_resif); 172 173 /** 174 * Create commpoints with for this thread for the shared ports. 175 * @param base: the comm_base that provides event functionality. 176 * for default all ifs. 177 * @param ports: the list of shared ports. 178 * @param bufsize: size of datagram buffer. 179 * @param tcp_accept_count: max number of simultaneous TCP connections 180 * from clients. 181 * @param tcp_idle_timeout: idle timeout for TCP connections in msec. 182 * @param harden_large_queries: whether query size should be limited. 183 * @param http_max_streams: maximum number of HTTP/2 streams per connection. 184 * @param http_endpoint: HTTP endpoint to service queries on 185 * @param http_notls: no TLS for http downstream 186 * @param tcp_conn_limit: TCP connection limit info. 187 * @param sslctx: nonNULL if ssl context. 188 * @param dtenv: nonNULL if dnstap enabled. 189 * @param cb: callback function when a request arrives. It is passed 190 * the packet and user argument. Return true to send a reply. 191 * @param cb_arg: user data argument for callback function. 192 * @return: the malloced listening structure, ready for use. NULL on error. 193 */ 194 struct listen_dnsport* 195 listen_create(struct comm_base* base, struct listen_port* ports, 196 size_t bufsize, int tcp_accept_count, int tcp_idle_timeout, 197 int harden_large_queries, uint32_t http_max_streams, 198 char* http_endpoint, int http_notls, struct tcl_list* tcp_conn_limit, 199 void* sslctx, struct dt_env* dtenv, comm_point_callback_type* cb, 200 void *cb_arg); 201 202 /** 203 * delete the listening structure 204 * @param listen: listening structure. 205 */ 206 void listen_delete(struct listen_dnsport* listen); 207 208 /** setup the locks for the listen ports */ 209 void listen_setup_locks(void); 210 /** desetup the locks for the listen ports */ 211 void listen_desetup_locks(void); 212 213 /** 214 * delete listen_list of commpoints. Calls commpointdelete() on items. 215 * This may close the fds or not depending on flags. 216 * @param list: to delete. 217 */ 218 void listen_list_delete(struct listen_list* list); 219 220 /** 221 * get memory size used by the listening structs 222 * @param listen: listening structure. 223 * @return: size in bytes. 224 */ 225 size_t listen_get_mem(struct listen_dnsport* listen); 226 227 /** 228 * stop accept handlers for TCP (until enabled again) 229 * @param listen: listening structure. 230 */ 231 void listen_stop_accept(struct listen_dnsport* listen); 232 233 /** 234 * start accept handlers for TCP (was stopped before) 235 * @param listen: listening structure. 236 */ 237 void listen_start_accept(struct listen_dnsport* listen); 238 239 /** 240 * Create and bind nonblocking UDP socket 241 * @param family: for socket call. 242 * @param socktype: for socket call. 243 * @param addr: for bind call. 244 * @param addrlen: for bind call. 245 * @param v6only: if enabled, IP6 sockets get IP6ONLY option set. 246 * if enabled with value 2 IP6ONLY option is disabled. 247 * @param inuse: on error, this is set true if the port was in use. 248 * @param noproto: on error, this is set true if cause is that the 249 IPv6 proto (family) is not available. 250 * @param rcv: set size on rcvbuf with socket option, if 0 it is not set. 251 * @param snd: set size on sndbuf with socket option, if 0 it is not set. 252 * @param listen: if true, this is a listening UDP port, eg port 53, and 253 * set SO_REUSEADDR on it. 254 * @param reuseport: if nonNULL and true, try to set SO_REUSEPORT on 255 * listening UDP port. Set to false on return if it failed to do so. 256 * @param transparent: set IP_TRANSPARENT socket option. 257 * @param freebind: set IP_FREEBIND socket option. 258 * @param use_systemd: if true, fetch sockets from systemd. 259 * @param dscp: DSCP to use. 260 * @return: the socket. -1 on error. 261 */ 262 int create_udp_sock(int family, int socktype, struct sockaddr* addr, 263 socklen_t addrlen, int v6only, int* inuse, int* noproto, int rcv, 264 int snd, int listen, int* reuseport, int transparent, int freebind, int use_systemd, int dscp); 265 266 /** 267 * Create and bind TCP listening socket 268 * @param addr: address info ready to make socket. 269 * @param v6only: enable ip6 only flag on ip6 sockets. 270 * @param noproto: if error caused by lack of protocol support. 271 * @param reuseport: if nonNULL and true, try to set SO_REUSEPORT on 272 * listening UDP port. Set to false on return if it failed to do so. 273 * @param transparent: set IP_TRANSPARENT socket option. 274 * @param mss: maximum segment size of the socket. if zero, leaves the default. 275 * @param nodelay: if true set TCP_NODELAY and TCP_QUICKACK socket options. 276 * @param freebind: set IP_FREEBIND socket option. 277 * @param use_systemd: if true, fetch sockets from systemd. 278 * @param dscp: DSCP to use. 279 * @return: the socket. -1 on error. 280 */ 281 int create_tcp_accept_sock(struct addrinfo *addr, int v6only, int* noproto, 282 int* reuseport, int transparent, int mss, int nodelay, int freebind, 283 int use_systemd, int dscp); 284 285 /** 286 * Create and bind local listening socket 287 * @param path: path to the socket. 288 * @param noproto: on error, this is set true if cause is that local sockets 289 * are not supported. 290 * @param use_systemd: if true, fetch sockets from systemd. 291 * @return: the socket. -1 on error. 292 */ 293 int create_local_accept_sock(const char* path, int* noproto, int use_systemd); 294 295 /** 296 * TCP request info. List of requests outstanding on the channel, that 297 * are asked for but not yet answered back. 298 */ 299 struct tcp_req_info { 300 /** the TCP comm point for this. Its buffer is used for read/write */ 301 struct comm_point* cp; 302 /** the buffer to use to spool reply from mesh into, 303 * it can then be copied to the result list and written. 304 * it is a pointer to the shared udp buffer. */ 305 struct sldns_buffer* spool_buffer; 306 /** are we in worker_handle function call (for recursion callback)*/ 307 int in_worker_handle; 308 /** is the comm point dropped (by worker handle). 309 * That means we have to disconnect the channel. */ 310 int is_drop; 311 /** is the comm point set to send_reply (by mesh new client in worker 312 * handle), if so answer is available in c.buffer */ 313 int is_reply; 314 /** read channel has closed, just write pending results */ 315 int read_is_closed; 316 /** read again */ 317 int read_again; 318 /** number of outstanding requests */ 319 int num_open_req; 320 /** list of outstanding requests */ 321 struct tcp_req_open_item* open_req_list; 322 /** number of pending writeable results */ 323 int num_done_req; 324 /** list of pending writable result packets, malloced one at a time */ 325 struct tcp_req_done_item* done_req_list; 326 }; 327 328 /** 329 * List of open items in TCP channel 330 */ 331 struct tcp_req_open_item { 332 /** next in list */ 333 struct tcp_req_open_item* next; 334 /** the mesh area of the mesh_state */ 335 struct mesh_area* mesh; 336 /** the mesh state */ 337 struct mesh_state* mesh_state; 338 }; 339 340 /** 341 * List of done items in TCP channel 342 */ 343 struct tcp_req_done_item { 344 /** next in list */ 345 struct tcp_req_done_item* next; 346 /** the buffer with packet contents */ 347 uint8_t* buf; 348 /** length of the buffer */ 349 size_t len; 350 }; 351 352 /** 353 * Create tcp request info structure that keeps track of open 354 * requests on the TCP channel that are resolved at the same time, 355 * and the pending results that have to get written back to that client. 356 * @param spoolbuf: shared buffer 357 * @return new structure or NULL on alloc failure. 358 */ 359 struct tcp_req_info* tcp_req_info_create(struct sldns_buffer* spoolbuf); 360 361 /** 362 * Delete tcp request structure. Called by owning commpoint. 363 * Removes mesh entry references and stored results from the lists. 364 * @param req: the tcp request info 365 */ 366 void tcp_req_info_delete(struct tcp_req_info* req); 367 368 /** 369 * Clear tcp request structure. Removes list entries, sets it up ready 370 * for the next connection. 371 * @param req: tcp request info structure. 372 */ 373 void tcp_req_info_clear(struct tcp_req_info* req); 374 375 /** 376 * Remove mesh state entry from list in tcp_req_info. 377 * caller has to manage the mesh state reply entry in the mesh state. 378 * @param req: the tcp req info that has the entry removed from the list. 379 * @param m: the state removed from the list. 380 */ 381 void tcp_req_info_remove_mesh_state(struct tcp_req_info* req, 382 struct mesh_state* m); 383 384 /** 385 * Handle write done of the last result packet 386 * @param req: the tcp req info. 387 */ 388 void tcp_req_info_handle_writedone(struct tcp_req_info* req); 389 390 /** 391 * Handle read done of a new request from the client 392 * @param req: the tcp req info. 393 */ 394 void tcp_req_info_handle_readdone(struct tcp_req_info* req); 395 396 /** 397 * Add mesh state to the tcp req list of open requests. 398 * So the comm_reply can be removed off the mesh reply list when 399 * the tcp channel has to be closed (for other reasons then that that 400 * request was done, eg. channel closed by client or some format error). 401 * @param req: tcp req info structure. It keeps track of the simultaneous 402 * requests and results on a tcp (or TLS) channel. 403 * @param mesh: mesh area for the state. 404 * @param m: mesh state to add. 405 * @return 0 on failure (malloc failure). 406 */ 407 int tcp_req_info_add_meshstate(struct tcp_req_info* req, 408 struct mesh_area* mesh, struct mesh_state* m); 409 410 /** 411 * Send reply on tcp simultaneous answer channel. May queue it up. 412 * @param req: request info structure. 413 */ 414 void tcp_req_info_send_reply(struct tcp_req_info* req); 415 416 /** the read channel has closed 417 * @param req: request. remaining queries are looked up and answered. 418 * @return zero if nothing to do, just close the tcp. 419 */ 420 int tcp_req_info_handle_read_close(struct tcp_req_info* req); 421 422 /** get the size of currently used tcp stream wait buffers (in bytes) */ 423 size_t tcp_req_info_get_stream_buffer_size(void); 424 425 /** get the size of currently used HTTP2 query buffers (in bytes) */ 426 size_t http2_get_query_buffer_size(void); 427 /** get the size of currently used HTTP2 response buffers (in bytes) */ 428 size_t http2_get_response_buffer_size(void); 429 430 #ifdef HAVE_NGHTTP2 431 /** 432 * Create nghttp2 callbacks to handle HTTP2 requests. 433 * @return malloc'ed struct, NULL on failure 434 */ 435 nghttp2_session_callbacks* http2_req_callbacks_create(void); 436 437 /** Free http2 stream buffers and decrease buffer counters */ 438 void http2_req_stream_clear(struct http2_stream* h2_stream); 439 440 /** 441 * DNS response ready to be submitted to nghttp2, to be prepared for sending 442 * out. Response is stored in c->buffer. Copy to rbuffer because the c->buffer 443 * might be used before this will be send out. 444 * @param h2_session: http2 session, containing c->buffer which contains answer 445 * @param h2_stream: http2 stream, containing buffer to store answer in 446 * @return 0 on error, 1 otherwise 447 */ 448 int http2_submit_dns_response(struct http2_session* h2_session); 449 #else 450 int http2_submit_dns_response(void* v); 451 #endif /* HAVE_NGHTTP2 */ 452 453 char* set_ip_dscp(int socket, int addrfamily, int ds); 454 455 /** for debug and profiling purposes only 456 * @param ub_sock: the structure containing created socket info we want to print or log for 457 */ 458 void verbose_print_unbound_socket(struct unbound_socket* ub_sock); 459 460 #endif /* LISTEN_DNSPORT_H */ 461