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