Home | History | Annotate | Line # | Download | only in ns
      1 /*	$NetBSD: client.h,v 1.21 2026/04/08 00:16:17 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
      5  *
      6  * SPDX-License-Identifier: MPL-2.0
      7  *
      8  * This Source Code Form is subject to the terms of the Mozilla Public
      9  * License, v. 2.0. If a copy of the MPL was not distributed with this
     10  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
     11  *
     12  * See the COPYRIGHT file distributed with this work for additional
     13  * information regarding copyright ownership.
     14  */
     15 
     16 #pragma once
     17 
     18 /*****
     19 ***** Module Info
     20 *****/
     21 
     22 /*! \file
     23  * \brief
     24  * This module defines two objects, ns_client_t and ns_clientmgr_t.
     25  *
     26  * An ns_client_t object handles incoming DNS requests from clients
     27  * on a given network interface.
     28  *
     29  * Each ns_client_t object can handle only one TCP connection or UDP
     30  * request at a time.  Therefore, several ns_client_t objects are
     31  * typically created to serve each network interface, e.g., one
     32  * for handling TCP requests and a few (one per CPU) for handling
     33  * UDP requests.
     34  *
     35  * Incoming requests are classified as queries, zone transfer
     36  * requests, update requests, notify requests, etc, and handed off
     37  * to the appropriate request handler.  When the request has been
     38  * fully handled (which can be much later), the ns_client_t must be
     39  * notified of this by calling one of the following functions
     40  * exactly once in the context of its task:
     41  * \code
     42  *   ns_client_send()	 (sending a non-error response)
     43  *   ns_client_sendraw() (sending a raw response)
     44  *   ns_client_error()	 (sending an error response)
     45  *   ns_client_drop() (sending no response, logging the reason)
     46  *\endcode
     47  * This will release any resources used by the request and
     48  * and allow the ns_client_t to listen for the next request.
     49  *
     50  * A ns_clientmgr_t manages a number of ns_client_t objects.
     51  * New ns_client_t objects are created by calling
     52  * ns_clientmgr_createclients(). They are destroyed by
     53  * destroying their manager.
     54  */
     55 
     56 /***
     57  *** Imports
     58  ***/
     59 
     60 #include <inttypes.h>
     61 #include <stdbool.h>
     62 
     63 #include <isc/atomic.h>
     64 #include <isc/buffer.h>
     65 #include <isc/magic.h>
     66 #include <isc/netmgr.h>
     67 #include <isc/quota.h>
     68 #include <isc/stdtime.h>
     69 
     70 #include <dns/db.h>
     71 #include <dns/ecs.h>
     72 #include <dns/fixedname.h>
     73 #include <dns/name.h>
     74 #include <dns/rdataclass.h>
     75 #include <dns/rdatatype.h>
     76 #include <dns/types.h>
     77 
     78 #include <ns/query.h>
     79 #include <ns/types.h>
     80 
     81 /***
     82  *** Types
     83  ***/
     84 
     85 #define NS_CLIENT_TCP_BUFFER_SIZE  65535
     86 #define NS_CLIENT_SEND_BUFFER_SIZE 4096
     87 
     88 /*!
     89  * Client object states.  Ordering is significant: higher-numbered
     90  * states are generally "more active", meaning that the client can
     91  * have more dynamically allocated data, outstanding events, etc.
     92  * In the list below, any such properties listed for state N
     93  * also apply to any state > N.
     94  */
     95 
     96 typedef enum {
     97 	NS_CLIENTSTATE_FREED = 0,
     98 	/*%<
     99 	 * The client object no longer exists.
    100 	 */
    101 
    102 	NS_CLIENTSTATE_INACTIVE = 1,
    103 	/*%<
    104 	 * The client object exists and has a task and timer.
    105 	 * Its "query" struct and sendbuf are initialized.
    106 	 * It has a message and OPT, both in the reset state.
    107 	 */
    108 
    109 	NS_CLIENTSTATE_READY = 2,
    110 	/*%<
    111 	 * The client object is either a TCP or a UDP one, and
    112 	 * it is associated with a network interface.  It is on the
    113 	 * client manager's list of active clients.
    114 	 *
    115 	 * If it is a TCP client object, it has a TCP listener socket
    116 	 * and an outstanding TCP listen request.
    117 	 *
    118 	 * If it is a UDP client object, it has a UDP listener socket
    119 	 * and an outstanding UDP receive request.
    120 	 */
    121 
    122 	NS_CLIENTSTATE_WORKING = 3,
    123 	/*%<
    124 	 * The client object has received a request and is working
    125 	 * on it.  It has a view, and it may have any of a non-reset OPT,
    126 	 * recursion quota, and an outstanding write request.
    127 	 */
    128 
    129 	NS_CLIENTSTATE_RECURSING = 4,
    130 	/*%<
    131 	 * The client object is recursing.  It will be on the
    132 	 * 'recursing' list.
    133 	 */
    134 
    135 	NS_CLIENTSTATE_MAX = 5
    136 	/*%<
    137 	 * Sentinel value used to indicate "no state".
    138 	 */
    139 } ns_clientstate_t;
    140 
    141 typedef ISC_LIST(ns_client_t) client_list_t;
    142 
    143 /*% nameserver client manager structure */
    144 struct ns_clientmgr {
    145 	/* Unlocked. */
    146 	unsigned int magic;
    147 
    148 	isc_mem_t     *mctx;
    149 	isc_mempool_t *namepool;
    150 	isc_mempool_t *rdspool;
    151 
    152 	ns_server_t   *sctx;
    153 	isc_refcount_t references;
    154 	uint32_t       tid;
    155 	isc_loop_t    *loop;
    156 
    157 	dns_aclenv_t *aclenv;
    158 
    159 	/* Lock covers the recursing list */
    160 	isc_mutex_t   reclock;
    161 	client_list_t recursing; /*%< Recursing clients */
    162 
    163 	uint8_t tcp_buffer[NS_CLIENT_TCP_BUFFER_SIZE];
    164 };
    165 
    166 /*% nameserver client structure */
    167 struct ns_client {
    168 	unsigned int	 magic;
    169 	ns_clientmgr_t	*manager;
    170 	ns_clientstate_t state;
    171 	bool		 async;
    172 	unsigned int	 attributes;
    173 	dns_view_t	*view;
    174 	dns_dispatch_t	*dispatch;
    175 	isc_nmhandle_t	*handle;       /* Permanent pointer to handle */
    176 	isc_nmhandle_t	*sendhandle;   /* Waiting for send callback */
    177 	isc_nmhandle_t	*reqhandle;    /* Waiting for request callback
    178 					  (query, update, notify) */
    179 	isc_nmhandle_t *updatehandle;  /* Waiting for update callback */
    180 	isc_nmhandle_t *restarthandle; /* Waiting for restart callback */
    181 	unsigned char  *tcpbuf;
    182 	size_t		tcpbuf_size;
    183 	dns_message_t  *message;
    184 	dns_rdataset_t *opt;
    185 	dns_edectx_t	edectx;
    186 	uint16_t	udpsize;
    187 	uint16_t	extflags;
    188 	int16_t		ednsversion; /* -1 noedns */
    189 	uint16_t	additionaldepth;
    190 	void (*cleanup)(ns_client_t *);
    191 	ns_query_t    query;
    192 	isc_time_t    requesttime;
    193 	isc_stdtime_t now;
    194 	isc_time_t    tnow;
    195 	dns_name_t    signername; /*%< [T]SIG key name */
    196 	dns_name_t   *signer;	  /*%< NULL if not valid sig */
    197 	isc_result_t  sigresult;
    198 	isc_result_t  viewmatchresult;
    199 	isc_buffer_t *buffer;
    200 	isc_buffer_t  tbuffer;
    201 
    202 	isc_sockaddr_t peeraddr;
    203 	bool	       peeraddr_valid;
    204 	isc_netaddr_t  destaddr;
    205 	isc_sockaddr_t destsockaddr;
    206 
    207 	dns_ecs_t ecs; /*%< EDNS client subnet sent by client */
    208 
    209 	/*%
    210 	 * Information about recent FORMERR response(s), for
    211 	 * FORMERR loop avoidance.  This is separate for each
    212 	 * client object rather than global only to avoid
    213 	 * the need for locking.
    214 	 */
    215 	struct {
    216 		isc_sockaddr_t	addr;
    217 		isc_stdtime_t	time;
    218 		dns_messageid_t id;
    219 	} formerrcache;
    220 
    221 	/*% Callback function to send a response when unit testing */
    222 	void (*sendcb)(isc_buffer_t *buf);
    223 
    224 	ISC_LINK(ns_client_t) rlink;
    225 	unsigned char  cookie[8];
    226 	uint32_t       expire;
    227 	unsigned char *keytag;
    228 	uint16_t       keytag_len;
    229 
    230 	/*%
    231 	 * Used to override the DNS response code in ns_client_error().
    232 	 * If set to -1, the rcode is determined from the result code,
    233 	 * but if set to any other value, the least significant 12
    234 	 * bits will be used as the rcode in the response message.
    235 	 */
    236 	int32_t rcode_override;
    237 
    238 	uint8_t sendbuf[NS_CLIENT_SEND_BUFFER_SIZE];
    239 };
    240 
    241 #define NS_CLIENT_MAGIC	   ISC_MAGIC('N', 'S', 'C', 'c')
    242 #define NS_CLIENT_VALID(c) ISC_MAGIC_VALID(c, NS_CLIENT_MAGIC)
    243 
    244 #define NS_CLIENTATTR_TCP	 0x00001
    245 #define NS_CLIENTATTR_RA	 0x00002 /*%< Client gets recursive service */
    246 #define NS_CLIENTATTR_PKTINFO	 0x00004 /*%< pktinfo is valid */
    247 #define NS_CLIENTATTR_MULTICAST	 0x00008 /*%< recv'd from multicast */
    248 #define NS_CLIENTATTR_WANTDNSSEC 0x00010 /*%< include dnssec records */
    249 #define NS_CLIENTATTR_WANTNSID	 0x00020 /*%< include nameserver ID */
    250 #define NS_CLIENTATTR_BADCOOKIE \
    251 	0x00040 /*%< Presented cookie is bad/out-of-date */
    252 /* Obsolete: NS_CLIENTATTR_FILTER_AAAA_RC 0x00080 */
    253 #define NS_CLIENTATTR_WANTAD	 0x00100 /*%< want AD in response if possible */
    254 #define NS_CLIENTATTR_WANTCOOKIE 0x00200 /*%< return a COOKIE */
    255 #define NS_CLIENTATTR_HAVECOOKIE 0x00400 /*%< has a valid COOKIE */
    256 #define NS_CLIENTATTR_WANTEXPIRE 0x00800 /*%< return seconds to expire */
    257 #define NS_CLIENTATTR_HAVEEXPIRE 0x01000 /*%< return seconds to expire */
    258 #define NS_CLIENTATTR_WANTOPT	 0x02000 /*%< add opt to reply */
    259 #define NS_CLIENTATTR_HAVEECS	 0x04000 /*%< received an ECS option */
    260 #define NS_CLIENTATTR_WANTPAD	 0x08000 /*%< pad reply */
    261 #define NS_CLIENTATTR_USEKEEPALIVE 0x10000 /*%< use TCP keepalive */
    262 
    263 #define NS_CLIENTATTR_NOSETFC 0x20000 /*%< don't set servfail cache */
    264 
    265 /*
    266  * Flag to use with the SERVFAIL cache to indicate
    267  * that a query had the CD bit set.
    268  */
    269 #define NS_FAILCACHE_CD 0x01
    270 
    271 #ifdef _LP64
    272 extern atomic_uint_fast64_t ns_client_requests;
    273 #else
    274 extern atomic_uint_fast32_t ns_client_requests;
    275 #endif
    276 
    277 /***
    278  *** Functions
    279  ***/
    280 
    281 /*
    282  * Note!  These ns_client_ routines MUST be called ONLY from the client's
    283  * task in order to ensure synchronization.
    284  */
    285 
    286 void
    287 ns_client_send(ns_client_t *client);
    288 /*%<
    289  * Finish processing the current client request and
    290  * send client->message as a response.
    291  * \brief
    292  * Note!  These ns_client_ routines MUST be called ONLY from the client's
    293  * task in order to ensure synchronization.
    294  */
    295 
    296 void
    297 ns_client_sendraw(ns_client_t *client, dns_message_t *msg);
    298 /*%<
    299  * Finish processing the current client request and
    300  * send msg as a response using client->message->id for the id.
    301  */
    302 
    303 void
    304 ns_client_error(ns_client_t *client, isc_result_t result);
    305 /*%<
    306  * Finish processing the current client request and return
    307  * an error response to the client.  The error response
    308  * will have an RCODE determined by 'result'.
    309  */
    310 
    311 void
    312 ns_client_drop(ns_client_t *client, isc_result_t result);
    313 /*%<
    314  * Log the reason the current client request has failed; no response
    315  * will be sent.
    316  */
    317 
    318 isc_result_t
    319 ns_client_replace(ns_client_t *client);
    320 /*%<
    321  * Try to replace the current client with a new one, so that the
    322  * current one can go off and do some lengthy work without
    323  * leaving the dispatch/socket without service.
    324  */
    325 
    326 void
    327 ns_client_settimeout(ns_client_t *client, unsigned int seconds);
    328 /*%<
    329  * Set a timer in the client to go off in the specified amount of time.
    330  */
    331 
    332 isc_result_t
    333 ns_clientmgr_create(ns_server_t *sctx, isc_loopmgr_t *loopmgr,
    334 		    dns_aclenv_t *aclenv, int tid, ns_clientmgr_t **managerp);
    335 /*%<
    336  * Create a client manager.
    337  */
    338 
    339 void
    340 ns_clientmgr_shutdown(ns_clientmgr_t *manager);
    341 /*%<
    342  * Shutdown a client manager and all ns_client_t objects
    343  * managed by it
    344  */
    345 
    346 isc_sockaddr_t *
    347 ns_client_getsockaddr(ns_client_t *client);
    348 /*%<
    349  * Get the socket address of the client whose request is
    350  * currently being processed.
    351  */
    352 
    353 isc_sockaddr_t *
    354 ns_client_getdestaddr(ns_client_t *client);
    355 /*%<
    356  * Get the destination address (server) for the request that is
    357  * currently being processed.
    358  */
    359 
    360 isc_result_t
    361 ns_client_checkaclsilent(ns_client_t *client, isc_netaddr_t *netaddr,
    362 			 dns_acl_t *acl, bool default_allow);
    363 
    364 /*%<
    365  * Convenience function for client request ACL checking.
    366  *
    367  * Check the current client request against 'acl'.  If 'acl'
    368  * is NULL, allow the request iff 'default_allow' is true.
    369  * If netaddr is NULL, check the ACL against client->peeraddr;
    370  * otherwise check it against netaddr.
    371  *
    372  * Notes:
    373  *\li	This is appropriate for checking allow-update,
    374  * 	allow-query, allow-transfer, etc.  It is not appropriate
    375  * 	for checking the blackhole list because we treat positive
    376  * 	matches as "allow" and negative matches as "deny"; in
    377  *	the case of the blackhole list this would be backwards.
    378  *
    379  * Requires:
    380  *\li	'client' points to a valid client.
    381  *\li	'netaddr' points to a valid address, or is NULL.
    382  *\li	'acl' points to a valid ACL, or is NULL.
    383  *
    384  * Returns:
    385  *\li	ISC_R_SUCCESS	if the request should be allowed
    386  * \li	DNS_R_REFUSED	if the request should be denied
    387  *\li	No other return values are possible.
    388  */
    389 
    390 isc_result_t
    391 ns_client_checkacl(ns_client_t *client, isc_sockaddr_t *sockaddr,
    392 		   const char *opname, dns_acl_t *acl, bool default_allow,
    393 		   int log_level);
    394 /*%<
    395  * Like ns_client_checkaclsilent, except the outcome of the check is
    396  * logged at log level 'log_level' if denied, and at debug 3 if approved.
    397  * Log messages will refer to the request as an 'opname' request.
    398  *
    399  * Requires:
    400  *\li	'client' points to a valid client.
    401  *\li	'sockaddr' points to a valid address, or is NULL.
    402  *\li	'acl' points to a valid ACL, or is NULL.
    403  *\li	'opname' points to a null-terminated string.
    404  */
    405 
    406 void
    407 ns_client_log(ns_client_t *client, isc_logcategory_t *category,
    408 	      isc_logmodule_t *module, int level, const char *fmt, ...)
    409 	ISC_FORMAT_PRINTF(5, 6);
    410 
    411 void
    412 ns_client_logv(ns_client_t *client, isc_logcategory_t *category,
    413 	       isc_logmodule_t *module, int level, const char *fmt, va_list ap)
    414 	ISC_FORMAT_PRINTF(5, 0);
    415 
    416 void
    417 ns_client_aclmsg(const char *msg, const dns_name_t *name, dns_rdatatype_t type,
    418 		 dns_rdataclass_t rdclass, char *buf, size_t len);
    419 
    420 #define NS_CLIENT_ACLMSGSIZE(x)                           \
    421 	(DNS_NAME_FORMATSIZE + DNS_RDATATYPE_FORMATSIZE + \
    422 	 DNS_RDATACLASS_FORMATSIZE + sizeof(x) + sizeof("'/'"))
    423 
    424 void
    425 ns_client_recursing(ns_client_t *client);
    426 /*%<
    427  * Add client to end of th recursing list.
    428  */
    429 
    430 void
    431 ns_client_killoldestquery(ns_client_t *client);
    432 /*%<
    433  * Kill the oldest recursive query (recursing list head).
    434  */
    435 
    436 void
    437 ns_client_dumprecursing(FILE *f, ns_clientmgr_t *manager);
    438 /*%<
    439  * Dump the outstanding recursive queries to 'f'.
    440  */
    441 
    442 void
    443 ns_client_qnamereplace(ns_client_t *client, dns_name_t *name);
    444 /*%<
    445  * Replace the qname.
    446  */
    447 
    448 isc_result_t
    449 ns_client_sourceip(dns_clientinfo_t *ci, isc_sockaddr_t **addrp);
    450 
    451 isc_result_t
    452 ns_client_addopt(ns_client_t *client, dns_message_t *message,
    453 		 dns_rdataset_t **opt);
    454 
    455 /*%<
    456  * Get a client object from the inactive queue, or create one, as needed.
    457  * (Not intended for use outside this module and associated tests.)
    458  */
    459 
    460 void
    461 ns_client_request(isc_nmhandle_t *handle, isc_result_t eresult,
    462 		  isc_region_t *region, void *arg);
    463 
    464 /*%<
    465  * Handle client requests.
    466  * (Not intended for use outside this module and associated tests.)
    467  */
    468 
    469 isc_result_t
    470 ns__client_tcpconn(isc_nmhandle_t *handle, isc_result_t result, void *arg);
    471 
    472 /*%<
    473  * Called every time a TCP connection is establish.  This is used for
    474  * updating TCP statistics.
    475  */
    476 
    477 dns_rdataset_t *
    478 ns_client_newrdataset(ns_client_t *client);
    479 
    480 void
    481 ns_client_putrdataset(ns_client_t *client, dns_rdataset_t **rdatasetp);
    482 /*%<
    483  * Get and release temporary rdatasets in the client message;
    484  * used in query.c and in plugins.
    485  */
    486 
    487 isc_result_t
    488 ns_client_newnamebuf(ns_client_t *client);
    489 /*%<
    490  * Allocate a name buffer for the client message.
    491  */
    492 
    493 dns_name_t *
    494 ns_client_newname(ns_client_t *client, isc_buffer_t *dbuf, isc_buffer_t *nbuf);
    495 /*%<
    496  * Get a temporary name for the client message.
    497  */
    498 
    499 isc_buffer_t *
    500 ns_client_getnamebuf(ns_client_t *client);
    501 /*%<
    502  * Get a name buffer from the pool, or allocate a new one if needed.
    503  */
    504 
    505 void
    506 ns_client_keepname(ns_client_t *client, dns_name_t *name, isc_buffer_t *dbuf);
    507 /*%<
    508  * Adjust buffer 'dbuf' to reflect that 'name' is using space in it,
    509  * and set client attributes appropriately.
    510  */
    511 
    512 void
    513 ns_client_releasename(ns_client_t *client, dns_name_t **namep);
    514 /*%<
    515  * Release 'name' back to the pool of temporary names for the client
    516  * message. If it is using a name buffer, relinquish its exclusive
    517  * rights on the buffer.
    518  */
    519 
    520 isc_result_t
    521 ns_client_newdbversion(ns_client_t *client, unsigned int n);
    522 /*%<
    523  * Allocate 'n' new database versions for use by client queries.
    524  */
    525 
    526 ns_dbversion_t *
    527 ns_client_getdbversion(ns_client_t *client);
    528 /*%<
    529  * Get a free database version for use by a client query, allocating
    530  * a new one if necessary.
    531  */
    532 
    533 ns_dbversion_t *
    534 ns_client_findversion(ns_client_t *client, dns_db_t *db);
    535 /*%<
    536  * Find the correct database version to use with a client query.
    537  * If we have already done a query related to the database 'db',
    538  * make sure subsequent queries are from the same version;
    539  * otherwise, take a database version from the list of dbversions
    540  * allocated by ns_client_newdbversion().
    541  */
    542 
    543 ISC_REFCOUNT_DECL(ns_clientmgr);
    544 
    545 void
    546 ns__client_setup(ns_client_t *client, ns_clientmgr_t *manager, bool new);
    547 /*%<
    548  * Perform initial setup of an allocated client.
    549  */
    550 
    551 void
    552 ns__client_reset_cb(void *client0);
    553 /*%<
    554  * Reset the client object so that it can be reused.
    555  */
    556 
    557 void
    558 ns__client_put_cb(void *client0);
    559 /*%<
    560  * Free all resources allocated to this client object, so that
    561  * it can be freed.
    562  */
    563