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 "util/rbtree.h" 47 #include "util/locks.h" 48 #include "daemon/acl_list.h" 49 #ifdef HAVE_NGHTTP2_NGHTTP2_H 50 #include <nghttp2/nghttp2.h> 51 #endif 52 #ifdef HAVE_NGTCP2 53 #include <ngtcp2/ngtcp2.h> 54 #include <ngtcp2/ngtcp2_crypto.h> 55 #endif 56 struct listen_list; 57 struct config_file; 58 struct addrinfo; 59 struct sldns_buffer; 60 struct tcl_list; 61 62 /** 63 * Listening for queries structure. 64 * Contains list of query-listen sockets. 65 */ 66 struct listen_dnsport { 67 /** Base for select calls */ 68 struct comm_base* base; 69 70 /** buffer shared by UDP connections, since there is only one 71 datagram at any time. */ 72 struct sldns_buffer* udp_buff; 73 #ifdef USE_DNSCRYPT 74 struct sldns_buffer* dnscrypt_udp_buff; 75 #endif 76 /** list of comm points used to get incoming events */ 77 struct listen_list* cps; 78 }; 79 80 /** 81 * Single linked list to store event points. 82 */ 83 struct listen_list { 84 /** next in list */ 85 struct listen_list* next; 86 /** event info */ 87 struct comm_point* com; 88 }; 89 90 /** 91 * type of ports 92 */ 93 enum listen_type { 94 /** udp type */ 95 listen_type_udp, 96 /** tcp type */ 97 listen_type_tcp, 98 /** udp ipv6 (v4mapped) for use with ancillary data */ 99 listen_type_udpancil, 100 /** ssl over tcp type */ 101 listen_type_ssl, 102 /** udp type + dnscrypt*/ 103 listen_type_udp_dnscrypt, 104 /** tcp type + dnscrypt */ 105 listen_type_tcp_dnscrypt, 106 /** udp ipv6 (v4mapped) for use with ancillary data + dnscrypt*/ 107 listen_type_udpancil_dnscrypt, 108 /** HTTP(2) over TLS over TCP */ 109 listen_type_http, 110 /** DNS over QUIC */ 111 listen_type_doq 112 }; 113 114 /* 115 * socket properties (just like NSD nsd_socket structure definition) 116 */ 117 struct unbound_socket { 118 /** the address of the socket */ 119 struct sockaddr* addr; 120 /** length of the address */ 121 socklen_t addrlen; 122 /** socket descriptor returned by socket() syscall */ 123 int s; 124 /** address family (AF_INET/AF_INET6) */ 125 int fam; 126 /** ACL on the socket (listening interface) */ 127 struct acl_addr* acl; 128 }; 129 130 /** 131 * Single linked list to store shared ports that have been 132 * opened for use by all threads. 133 */ 134 struct listen_port { 135 /** next in list */ 136 struct listen_port* next; 137 /** file descriptor, open and ready for use */ 138 int fd; 139 /** type of file descriptor, udp or tcp */ 140 enum listen_type ftype; 141 /** if the port should support PROXYv2 */ 142 int pp2_enabled; 143 /** fill in unbound_socket structure for every opened socket at 144 * Unbound startup */ 145 struct unbound_socket* socket; 146 }; 147 148 /** 149 * Create shared listening ports 150 * Getaddrinfo, create socket, bind and listen to zero or more 151 * interfaces for IP4 and/or IP6, for UDP and/or TCP. 152 * On the given port number. It creates the sockets. 153 * @param cfg: settings on what ports to open. 154 * @param ifs: interfaces to open, array of IP addresses, "ip[@port]". 155 * @param num_ifs: length of ifs. 156 * @param reuseport: set to true if you want reuseport, or NULL to not have it, 157 * set to false on exit if reuseport failed to apply (because of no 158 * kernel support). 159 * @return: linked list of ports or NULL on error. 160 */ 161 struct listen_port* listening_ports_open(struct config_file* cfg, 162 char** ifs, int num_ifs, int* reuseport); 163 164 /** 165 * Close and delete the (list of) listening ports. 166 */ 167 void listening_ports_free(struct listen_port* list); 168 169 struct config_strlist; 170 /** 171 * Resolve interface names in config and store result IP addresses 172 * @param ifs: array of interfaces. The list of interface names, if not NULL. 173 * @param num_ifs: length of ifs array. 174 * @param list: if not NULL, this is used as the list of interface names. 175 * @param resif: string array (malloced array of malloced strings) with 176 * result. NULL if cfg has none. 177 * @param num_resif: length of resif. Zero if cfg has zero num_ifs. 178 * @return 0 on failure. 179 */ 180 int resolve_interface_names(char** ifs, int num_ifs, 181 struct config_strlist* list, char*** resif, int* num_resif); 182 183 /** 184 * Create commpoints with for this thread for the shared ports. 185 * @param base: the comm_base that provides event functionality. 186 * for default all ifs. 187 * @param ports: the list of shared ports. 188 * @param bufsize: size of datagram buffer. 189 * @param tcp_accept_count: max number of simultaneous TCP connections 190 * from clients. 191 * @param tcp_idle_timeout: idle timeout for TCP connections in msec. 192 * @param harden_large_queries: whether query size should be limited. 193 * @param http_max_streams: maximum number of HTTP/2 streams per connection. 194 * @param http_endpoint: HTTP endpoint to service queries on 195 * @param http_notls: no TLS for http downstream 196 * @param tcp_conn_limit: TCP connection limit info. 197 * @param dot_sslctx: nonNULL if dot ssl context. 198 * @param doh_sslctx: nonNULL if doh ssl context. 199 * @param quic_sslctx: nonNULL if quic ssl context. 200 * @param dtenv: nonNULL if dnstap enabled. 201 * @param doq_table: the doq connection table, with shared information. 202 * @param rnd: random state. 203 * @param cfg: config file struct. 204 * @param cb: callback function when a request arrives. It is passed 205 * the packet and user argument. Return true to send a reply. 206 * @param cb_arg: user data argument for callback function. 207 * @return: the malloced listening structure, ready for use. NULL on error. 208 */ 209 struct listen_dnsport* 210 listen_create(struct comm_base* base, struct listen_port* ports, 211 size_t bufsize, int tcp_accept_count, int tcp_idle_timeout, 212 int harden_large_queries, uint32_t http_max_streams, 213 char* http_endpoint, int http_notls, struct tcl_list* tcp_conn_limit, 214 void* dot_sslctx, void* doh_sslctx, void* quic_sslctx, 215 struct dt_env* dtenv, 216 struct doq_table* doq_table, 217 struct ub_randstate* rnd,struct config_file* cfg, 218 comm_point_callback_type* cb, void *cb_arg); 219 220 /** 221 * delete the listening structure 222 * @param listen: listening structure. 223 */ 224 void listen_delete(struct listen_dnsport* listen); 225 226 /** setup the locks for the listen ports */ 227 void listen_setup_locks(void); 228 /** desetup the locks for the listen ports */ 229 void listen_desetup_locks(void); 230 231 /** 232 * delete listen_list of commpoints. Calls commpointdelete() on items. 233 * This may close the fds or not depending on flags. 234 * @param list: to delete. 235 */ 236 void listen_list_delete(struct listen_list* list); 237 238 /** 239 * get memory size used by the listening structs 240 * @param listen: listening structure. 241 * @return: size in bytes. 242 */ 243 size_t listen_get_mem(struct listen_dnsport* listen); 244 245 /** 246 * stop accept handlers for TCP (until enabled again) 247 * @param listen: listening structure. 248 */ 249 void listen_stop_accept(struct listen_dnsport* listen); 250 251 /** 252 * start accept handlers for TCP (was stopped before) 253 * @param listen: listening structure. 254 */ 255 void listen_start_accept(struct listen_dnsport* listen); 256 257 /** 258 * Create and bind nonblocking UDP socket 259 * @param family: for socket call. 260 * @param socktype: for socket call. 261 * @param addr: for bind call. 262 * @param addrlen: for bind call. 263 * @param v6only: if enabled, IP6 sockets get IP6ONLY option set. 264 * if enabled with value 2 IP6ONLY option is disabled. 265 * @param inuse: on error, this is set true if the port was in use. 266 * @param noproto: on error, this is set true if cause is that the 267 IPv6 proto (family) is not available. 268 * @param rcv: set size on rcvbuf with socket option, if 0 it is not set. 269 * @param snd: set size on sndbuf with socket option, if 0 it is not set. 270 * @param listen: if true, this is a listening UDP port, eg port 53, and 271 * set SO_REUSEADDR on it. 272 * @param reuseport: if nonNULL and true, try to set SO_REUSEPORT on 273 * listening UDP port. Set to false on return if it failed to do so. 274 * @param transparent: set IP_TRANSPARENT socket option. 275 * @param freebind: set IP_FREEBIND socket option. 276 * @param use_systemd: if true, fetch sockets from systemd. 277 * @param dscp: DSCP to use. 278 * @return: the socket. -1 on error. 279 */ 280 int create_udp_sock(int family, int socktype, struct sockaddr* addr, 281 socklen_t addrlen, int v6only, int* inuse, int* noproto, int rcv, 282 int snd, int listen, int* reuseport, int transparent, int freebind, int use_systemd, int dscp); 283 284 /** 285 * Create and bind TCP listening socket 286 * @param addr: address info ready to make socket. 287 * @param v6only: enable ip6 only flag on ip6 sockets. 288 * @param noproto: if error caused by lack of protocol support. 289 * @param reuseport: if nonNULL and true, try to set SO_REUSEPORT on 290 * listening UDP port. Set to false on return if it failed to do so. 291 * @param transparent: set IP_TRANSPARENT socket option. 292 * @param mss: maximum segment size of the socket. if zero, leaves the default. 293 * @param nodelay: if true set TCP_NODELAY and TCP_QUICKACK socket options. 294 * @param freebind: set IP_FREEBIND socket option. 295 * @param use_systemd: if true, fetch sockets from systemd. 296 * @param dscp: DSCP to use. 297 * @param additional: additional log information for the socket type. 298 * @return: the socket. -1 on error. 299 */ 300 int create_tcp_accept_sock(struct addrinfo *addr, int v6only, int* noproto, 301 int* reuseport, int transparent, int mss, int nodelay, int freebind, 302 int use_systemd, int dscp, const char* additional); 303 304 /** 305 * Create and bind local listening socket 306 * @param path: path to the socket. 307 * @param noproto: on error, this is set true if cause is that local sockets 308 * are not supported. 309 * @param use_systemd: if true, fetch sockets from systemd. 310 * @return: the socket. -1 on error. 311 */ 312 int create_local_accept_sock(const char* path, int* noproto, int use_systemd); 313 314 /** 315 * TCP request info. List of requests outstanding on the channel, that 316 * are asked for but not yet answered back. 317 */ 318 struct tcp_req_info { 319 /** the TCP comm point for this. Its buffer is used for read/write */ 320 struct comm_point* cp; 321 /** the buffer to use to spool reply from mesh into, 322 * it can then be copied to the result list and written. 323 * it is a pointer to the shared udp buffer. */ 324 struct sldns_buffer* spool_buffer; 325 /** are we in worker_handle function call (for recursion callback)*/ 326 int in_worker_handle; 327 /** is the comm point dropped (by worker handle). 328 * That means we have to disconnect the channel. */ 329 int is_drop; 330 /** is the comm point set to send_reply (by mesh new client in worker 331 * handle), if so answer is available in c.buffer */ 332 int is_reply; 333 /** read channel has closed, just write pending results */ 334 int read_is_closed; 335 /** read again */ 336 int read_again; 337 /** number of outstanding requests */ 338 int num_open_req; 339 /** list of outstanding requests */ 340 struct tcp_req_open_item* open_req_list; 341 /** number of pending writeable results */ 342 int num_done_req; 343 /** list of pending writable result packets, malloced one at a time */ 344 struct tcp_req_done_item* done_req_list; 345 }; 346 347 /** 348 * List of open items in TCP channel 349 */ 350 struct tcp_req_open_item { 351 /** next in list */ 352 struct tcp_req_open_item* next; 353 /** the mesh area of the mesh_state */ 354 struct mesh_area* mesh; 355 /** the mesh state */ 356 struct mesh_state* mesh_state; 357 }; 358 359 /** 360 * List of done items in TCP channel 361 */ 362 struct tcp_req_done_item { 363 /** next in list */ 364 struct tcp_req_done_item* next; 365 /** the buffer with packet contents */ 366 uint8_t* buf; 367 /** length of the buffer */ 368 size_t len; 369 }; 370 371 /** 372 * Create tcp request info structure that keeps track of open 373 * requests on the TCP channel that are resolved at the same time, 374 * and the pending results that have to get written back to that client. 375 * @param spoolbuf: shared buffer 376 * @return new structure or NULL on alloc failure. 377 */ 378 struct tcp_req_info* tcp_req_info_create(struct sldns_buffer* spoolbuf); 379 380 /** 381 * Delete tcp request structure. Called by owning commpoint. 382 * Removes mesh entry references and stored results from the lists. 383 * @param req: the tcp request info 384 */ 385 void tcp_req_info_delete(struct tcp_req_info* req); 386 387 /** 388 * Clear tcp request structure. Removes list entries, sets it up ready 389 * for the next connection. 390 * @param req: tcp request info structure. 391 */ 392 void tcp_req_info_clear(struct tcp_req_info* req); 393 394 /** 395 * Remove mesh state entry from list in tcp_req_info. 396 * caller has to manage the mesh state reply entry in the mesh state. 397 * @param req: the tcp req info that has the entry removed from the list. 398 * @param m: the state removed from the list. 399 */ 400 void tcp_req_info_remove_mesh_state(struct tcp_req_info* req, 401 struct mesh_state* m); 402 403 /** 404 * Handle write done of the last result packet 405 * @param req: the tcp req info. 406 */ 407 void tcp_req_info_handle_writedone(struct tcp_req_info* req); 408 409 /** 410 * Handle read done of a new request from the client 411 * @param req: the tcp req info. 412 */ 413 void tcp_req_info_handle_readdone(struct tcp_req_info* req); 414 415 /** 416 * Add mesh state to the tcp req list of open requests. 417 * So the comm_reply can be removed off the mesh reply list when 418 * the tcp channel has to be closed (for other reasons then that that 419 * request was done, eg. channel closed by client or some format error). 420 * @param req: tcp req info structure. It keeps track of the simultaneous 421 * requests and results on a tcp (or TLS) channel. 422 * @param mesh: mesh area for the state. 423 * @param m: mesh state to add. 424 * @return 0 on failure (malloc failure). 425 */ 426 int tcp_req_info_add_meshstate(struct tcp_req_info* req, 427 struct mesh_area* mesh, struct mesh_state* m); 428 429 /** 430 * Send reply on tcp simultaneous answer channel. May queue it up. 431 * @param req: request info structure. 432 */ 433 void tcp_req_info_send_reply(struct tcp_req_info* req); 434 435 /** the read channel has closed 436 * @param req: request. remaining queries are looked up and answered. 437 * @return zero if nothing to do, just close the tcp. 438 */ 439 int tcp_req_info_handle_read_close(struct tcp_req_info* req); 440 441 /** get the size of currently used tcp stream wait buffers (in bytes) */ 442 size_t tcp_req_info_get_stream_buffer_size(void); 443 444 /** get the size of currently used HTTP2 query buffers (in bytes) */ 445 size_t http2_get_query_buffer_size(void); 446 /** get the size of currently used HTTP2 response buffers (in bytes) */ 447 size_t http2_get_response_buffer_size(void); 448 449 #ifdef HAVE_NGHTTP2 450 /** 451 * Create nghttp2 callbacks to handle HTTP2 requests. 452 * @return malloc'ed struct, NULL on failure 453 */ 454 nghttp2_session_callbacks* http2_req_callbacks_create(void); 455 456 /** Free http2 stream buffers and decrease buffer counters */ 457 void http2_req_stream_clear(struct http2_stream* h2_stream); 458 459 /** 460 * DNS response ready to be submitted to nghttp2, to be prepared for sending 461 * out. Response is stored in c->buffer. Copy to rbuffer because the c->buffer 462 * might be used before this will be send out. 463 * @param h2_session: http2 session, containing c->buffer which contains answer 464 * @param h2_stream: http2 stream, containing buffer to store answer in 465 * @return 0 on error, 1 otherwise 466 */ 467 int http2_submit_dns_response(struct http2_session* h2_session); 468 #else 469 int http2_submit_dns_response(void* v); 470 #endif /* HAVE_NGHTTP2 */ 471 472 #ifdef HAVE_NGTCP2 473 struct doq_conid; 474 struct doq_server_socket; 475 476 /** 477 * DoQ shared connection table. This is the connections for the host. 478 * And some config parameter values for connections. The host has to 479 * respond on that ip,port for those connections, so they are shared 480 * between threads. 481 */ 482 struct doq_table { 483 /** the lock on the tree and config elements. insert and deletion, 484 * also lookup in the tree needs to hold the lock. */ 485 lock_rw_type lock; 486 /** rbtree of doq_conn, the connections to different destination 487 * addresses, and can be found by dcid. */ 488 struct rbtree_type* conn_tree; 489 /** lock for the conid tree, needed for the conid tree and also 490 * the conid elements */ 491 lock_rw_type conid_lock; 492 /** rbtree of doq_conid, connections can be found by their 493 * connection ids. Lookup by connection id, finds doq_conn. */ 494 struct rbtree_type* conid_tree; 495 /** the server scid length */ 496 int sv_scidlen; 497 /** the static secret for the server */ 498 uint8_t* static_secret; 499 /** length of the static secret */ 500 size_t static_secret_len; 501 /** the idle timeout in nanoseconds */ 502 uint64_t idle_timeout; 503 /** the list of write interested connections, hold the doq_table.lock 504 * to change them */ 505 struct doq_conn* write_list_first, *write_list_last; 506 /** rbtree of doq_timer. */ 507 struct rbtree_type* timer_tree; 508 /** lock on the current_size counter. */ 509 lock_basic_type size_lock; 510 /** current use, in bytes, of QUIC buffers. 511 * The doq_conn ngtcp2_conn structure, SSL structure and conid structs 512 * are not counted. */ 513 size_t current_size; 514 }; 515 516 /** 517 * create SSL context for QUIC 518 * @param key: private key file. 519 * @param pem: public key cert. 520 * @param verifypem: if nonNULL, verifylocation file. 521 * return SSL_CTX* or NULL on failure (logged). 522 */ 523 void* quic_sslctx_create(char* key, char* pem, char* verifypem); 524 525 /** create doq table */ 526 struct doq_table* doq_table_create(struct config_file* cfg, 527 struct ub_randstate* rnd); 528 529 /** delete doq table */ 530 void doq_table_delete(struct doq_table* table); 531 532 /** 533 * Timer information for doq timer. 534 */ 535 struct doq_timer { 536 /** The rbnode in the tree sorted by timeout value. Key this struct. */ 537 struct rbnode_type node; 538 /** The timeout value. Absolute time value. */ 539 struct timeval time; 540 /** If the timer is in the time tree, with the node. */ 541 int timer_in_tree; 542 /** If there are more timers with the exact same timeout value, 543 * they form a set of timers. The rbnode timer has a link to the list 544 * with the other timers in the set. The rbnode timer is not a 545 * member of the list with the other timers. The other timers are not 546 * linked into the tree. */ 547 struct doq_timer* setlist_first, *setlist_last; 548 /** If the timer is on the setlist. */ 549 int timer_in_list; 550 /** If in the setlist, the next and prev element. */ 551 struct doq_timer* setlist_next, *setlist_prev; 552 /** The connection that is timeouted. */ 553 struct doq_conn* conn; 554 /** The worker that is waiting for the timeout event. 555 * Set for the rbnode tree linked element. If a worker is waiting 556 * for the event. If NULL, no worker is waiting for this timeout. */ 557 struct doq_server_socket* worker_doq_socket; 558 }; 559 560 /** 561 * Key information that makes a doq_conn node in the tree lookup. 562 */ 563 struct doq_conn_key { 564 /** the remote endpoint and local endpoint and ifindex */ 565 struct doq_pkt_addr paddr; 566 /** the doq connection dcid */ 567 uint8_t* dcid; 568 /** length of dcid */ 569 size_t dcidlen; 570 }; 571 572 /** 573 * DoQ connection, for DNS over QUIC. One connection to a remote endpoint 574 * with a number of streams in it. Every stream is like a tcp stream with 575 * a uint16_t length, query read, and a uint16_t length and answer written. 576 */ 577 struct doq_conn { 578 /** rbtree node, key is addresses and dcid */ 579 struct rbnode_type node; 580 /** lock on the connection */ 581 lock_basic_type lock; 582 /** the key information, with dcid and address endpoint */ 583 struct doq_conn_key key; 584 /** the doq server socket for inside callbacks */ 585 struct doq_server_socket* doq_socket; 586 /** the doq table this connection is part of */ 587 struct doq_table* table; 588 /** if the connection is about to be deleted. */ 589 uint8_t is_deleted; 590 /** the version, the client chosen version of QUIC */ 591 uint32_t version; 592 /** the ngtcp2 connection, a server connection */ 593 struct ngtcp2_conn* conn; 594 /** the connection ids that are associated with this doq_conn. 595 * There can be a number, that can change. They are linked here, 596 * so that upon removal, the list of actually associated conid 597 * elements can be removed as well. */ 598 struct doq_conid* conid_list; 599 /** the ngtcp2 last error for the connection */ 600 #ifdef HAVE_NGTCP2_CCERR_DEFAULT 601 struct ngtcp2_ccerr ccerr; 602 #else 603 struct ngtcp2_connection_close_error last_error; 604 #endif 605 /** the recent tls alert error code */ 606 uint8_t tls_alert; 607 /** the ssl context, SSL* */ 608 void* ssl; 609 #ifdef HAVE_NGTCP2_CRYPTO_QUICTLS_CONFIGURE_SERVER_CONTEXT 610 /** the connection reference for ngtcp2_conn and userdata in ssl */ 611 struct ngtcp2_crypto_conn_ref conn_ref; 612 #endif 613 /** closure packet, if any */ 614 uint8_t* close_pkt; 615 /** length of closure packet. */ 616 size_t close_pkt_len; 617 /** closure ecn */ 618 uint32_t close_ecn; 619 /** the streams for this connection, of type doq_stream */ 620 struct rbtree_type stream_tree; 621 /** the streams that want write, they have something to write. 622 * The list is ordered, the last have to wait for the first to 623 * get their data written. */ 624 struct doq_stream* stream_write_first, *stream_write_last; 625 /** the conn has write interest if true, no write interest if false. */ 626 uint8_t write_interest; 627 /** if the conn is on the connection write list */ 628 uint8_t on_write_list; 629 /** the connection write list prev and next, if on the write list */ 630 struct doq_conn* write_prev, *write_next; 631 /** The timer for the connection. If unused, it is not in the tree 632 * and not in the list. It is alloced here, so that it is prealloced. 633 * It has to be set after every read and write on the connection, so 634 * this improves performance, but also the allocation does not fail. */ 635 struct doq_timer timer; 636 }; 637 638 /** 639 * Connection ID and the doq_conn that is that connection. A connection 640 * has an original dcid, and then more connection ids associated. 641 */ 642 struct doq_conid { 643 /** rbtree node, key is the connection id. */ 644 struct rbnode_type node; 645 /** the next and prev in the list of conids for the doq_conn */ 646 struct doq_conid* next, *prev; 647 /** key to the doq_conn that is the connection */ 648 struct doq_conn_key key; 649 /** the connection id, byte string */ 650 uint8_t* cid; 651 /** the length of cid */ 652 size_t cidlen; 653 }; 654 655 /** 656 * DoQ stream, for DNS over QUIC. 657 */ 658 struct doq_stream { 659 /** the rbtree node for the stream, key is the stream_id */ 660 rbnode_type node; 661 /** the stream id */ 662 int64_t stream_id; 663 /** if the stream is closed */ 664 uint8_t is_closed; 665 /** if the query is complete */ 666 uint8_t is_query_complete; 667 /** the number of bytes read on the stream, up to querylen+2. */ 668 size_t nread; 669 /** the length of the input query bytes */ 670 size_t inlen; 671 /** the input bytes */ 672 uint8_t* in; 673 /** does the stream have an answer to send */ 674 uint8_t is_answer_available; 675 /** the answer bytes sent, up to outlen+2. */ 676 size_t nwrite; 677 /** the length of the output answer bytes */ 678 size_t outlen; 679 /** the output length in network wireformat */ 680 uint16_t outlen_wire; 681 /** the output packet bytes */ 682 uint8_t* out; 683 /** if the stream is on the write list */ 684 uint8_t on_write_list; 685 /** the prev and next on the write list, if on the list */ 686 struct doq_stream* write_prev, *write_next; 687 }; 688 689 /** doq application error code that is sent when a stream is closed */ 690 #define DOQ_APP_ERROR_CODE 1 691 692 /** 693 * Create the doq connection. 694 * @param c: the comm point for the listening doq socket. 695 * @param paddr: with remote and local address and ifindex for the 696 * connection destination. This is where packets are sent. 697 * @param dcid: the dcid, Destination Connection ID. 698 * @param dcidlen: length of dcid. 699 * @param version: client chosen version. 700 * @return new doq connection or NULL on allocation failure. 701 */ 702 struct doq_conn* doq_conn_create(struct comm_point* c, 703 struct doq_pkt_addr* paddr, const uint8_t* dcid, size_t dcidlen, 704 uint32_t version); 705 706 /** 707 * Delete the doq connection structure. 708 * @param conn: to delete. 709 * @param table: with memory size. 710 */ 711 void doq_conn_delete(struct doq_conn* conn, struct doq_table* table); 712 713 /** compare function of doq_conn */ 714 int doq_conn_cmp(const void* key1, const void* key2); 715 716 /** compare function of doq_conid */ 717 int doq_conid_cmp(const void* key1, const void* key2); 718 719 /** compare function of doq_timer */ 720 int doq_timer_cmp(const void* key1, const void* key2); 721 722 /** compare function of doq_stream */ 723 int doq_stream_cmp(const void* key1, const void* key2); 724 725 /** setup the doq connection callbacks, and settings. */ 726 int doq_conn_setup(struct doq_conn* conn, uint8_t* scid, size_t scidlen, 727 uint8_t* ocid, size_t ocidlen, const uint8_t* token, size_t tokenlen); 728 729 /** fill a buffer with random data */ 730 void doq_fill_rand(struct ub_randstate* rnd, uint8_t* buf, size_t len); 731 732 /** delete a doq_conid */ 733 void doq_conid_delete(struct doq_conid* conid); 734 735 /** add a connection id to the doq_conn. 736 * caller must hold doq_table.conid_lock. */ 737 int doq_conn_associate_conid(struct doq_conn* conn, uint8_t* data, 738 size_t datalen); 739 740 /** remove a connection id from the doq_conn. 741 * caller must hold doq_table.conid_lock. */ 742 void doq_conn_dissociate_conid(struct doq_conn* conn, const uint8_t* data, 743 size_t datalen); 744 745 /** initial setup to link current connection ids to the doq_conn */ 746 int doq_conn_setup_conids(struct doq_conn* conn); 747 748 /** remove the connection ids from the doq_conn. 749 * caller must hold doq_table.conid_lock. */ 750 void doq_conn_clear_conids(struct doq_conn* conn); 751 752 /** find a conid in the doq_conn connection. 753 * caller must hold table.conid_lock. */ 754 struct doq_conid* doq_conid_find(struct doq_table* doq_table, 755 const uint8_t* data, size_t datalen); 756 757 /** receive a packet for a connection */ 758 int doq_conn_recv(struct comm_point* c, struct doq_pkt_addr* paddr, 759 struct doq_conn* conn, struct ngtcp2_pkt_info* pi, int* err_retry, 760 int* err_drop); 761 762 /** send packets for a connection */ 763 int doq_conn_write_streams(struct comm_point* c, struct doq_conn* conn, 764 int* err_drop); 765 766 /** send the close packet for the connection, perhaps again. */ 767 int doq_conn_send_close(struct comm_point* c, struct doq_conn* conn); 768 769 /** delete doq stream */ 770 void doq_stream_delete(struct doq_stream* stream); 771 772 /** doq read a connection key from repinfo. It is not malloced, but points 773 * into the repinfo for the dcid. */ 774 void doq_conn_key_from_repinfo(struct doq_conn_key* key, 775 struct comm_reply* repinfo); 776 777 /** doq find a stream in the connection */ 778 struct doq_stream* doq_stream_find(struct doq_conn* conn, int64_t stream_id); 779 780 /** doq shutdown the stream. */ 781 int doq_stream_close(struct doq_conn* conn, struct doq_stream* stream, 782 int send_shutdown); 783 784 /** send reply for a connection */ 785 int doq_stream_send_reply(struct doq_conn* conn, struct doq_stream* stream, 786 struct sldns_buffer* buf); 787 788 /** the connection has write interest, wants to write packets */ 789 void doq_conn_write_enable(struct doq_conn* conn); 790 791 /** the connection has no write interest, does not want to write packets */ 792 void doq_conn_write_disable(struct doq_conn* conn); 793 794 /** set the connection on or off the write list, depending on write interest */ 795 void doq_conn_set_write_list(struct doq_table* table, struct doq_conn* conn); 796 797 /** doq remove the connection from the write list */ 798 void doq_conn_write_list_remove(struct doq_table* table, 799 struct doq_conn* conn); 800 801 /** doq get the first conn from the write list, if any, popped from list. 802 * Locks the conn that is returned. */ 803 struct doq_conn* doq_table_pop_first(struct doq_table* table); 804 805 /** 806 * doq check if the timer for the conn needs to be changed. 807 * @param conn: connection, caller must hold lock on it. 808 * @param tv: time value, absolute time, returned. 809 * @return true if timer needs to be set to tv, false if no change is needed 810 * to the timer. The timer is already set to the right time in that case. 811 */ 812 int doq_conn_check_timer(struct doq_conn* conn, struct timeval* tv); 813 814 /** doq remove timer from tree */ 815 void doq_timer_tree_remove(struct doq_table* table, struct doq_timer* timer); 816 817 /** doq remove timer from list */ 818 void doq_timer_list_remove(struct doq_table* table, struct doq_timer* timer); 819 820 /** doq unset the timer if it was set. */ 821 void doq_timer_unset(struct doq_table* table, struct doq_timer* timer); 822 823 /** doq set the timer and add it. */ 824 void doq_timer_set(struct doq_table* table, struct doq_timer* timer, 825 struct doq_server_socket* worker_doq_socket, struct timeval* tv); 826 827 /** doq find a timeout in the timer tree */ 828 struct doq_timer* doq_timer_find_time(struct doq_table* table, 829 struct timeval* tv); 830 831 /** doq handle timeout for a connection. Pass conn locked. Returns false for 832 * deletion. */ 833 int doq_conn_handle_timeout(struct doq_conn* conn); 834 835 /** doq add size to the current quic buffer counter */ 836 void doq_table_quic_size_add(struct doq_table* table, size_t add); 837 838 /** doq subtract size from the current quic buffer counter */ 839 void doq_table_quic_size_subtract(struct doq_table* table, size_t subtract); 840 841 /** doq check if mem is available for quic. */ 842 int doq_table_quic_size_available(struct doq_table* table, 843 struct config_file* cfg, size_t mem); 844 845 /** doq get the quic size value */ 846 size_t doq_table_quic_size_get(struct doq_table* table); 847 #endif /* HAVE_NGTCP2 */ 848 849 char* set_ip_dscp(int socket, int addrfamily, int ds); 850 851 /** for debug and profiling purposes only 852 * @param ub_sock: the structure containing created socket info we want to print or log for 853 */ 854 void verbose_print_unbound_socket(struct unbound_socket* ub_sock); 855 856 /** event callback for testcode/doqclient */ 857 void doq_client_event_cb(int fd, short event, void* arg); 858 859 /** timer event callback for testcode/doqclient */ 860 void doq_client_timer_cb(int fd, short event, void* arg); 861 862 #ifdef HAVE_NGTCP2 863 /** get a timestamp in nanoseconds */ 864 ngtcp2_tstamp doq_get_timestamp_nanosec(void); 865 #endif 866 #endif /* LISTEN_DNSPORT_H */ 867