1 /* $NetBSD: server.h,v 1.10 2025/01/26 16:25:46 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 /*! \file */ 19 20 #include <inttypes.h> 21 #include <stdbool.h> 22 23 #include <isc/fuzz.h> 24 #include <isc/histo.h> 25 #include <isc/log.h> 26 #include <isc/magic.h> 27 #include <isc/quota.h> 28 #include <isc/random.h> 29 #include <isc/sockaddr.h> 30 #include <isc/types.h> 31 32 #include <dns/acl.h> 33 #include <dns/types.h> 34 35 #include <ns/types.h> 36 37 #define NS_SERVER_LOGQUERIES 0x00000001U /*%< log queries */ 38 #define NS_SERVER_NOAA 0x00000002U /*%< -T noaa */ 39 #define NS_SERVER_NOSOA 0x00000004U /*%< -T nosoa */ 40 #define NS_SERVER_NONEAREST 0x00000008U /*%< -T nonearest */ 41 #define NS_SERVER_NOEDNS 0x00000020U /*%< -T noedns */ 42 #define NS_SERVER_DROPEDNS 0x00000040U /*%< -T dropedns */ 43 #define NS_SERVER_NOTCP 0x00000080U /*%< -T notcp */ 44 #define NS_SERVER_DISABLE4 0x00000100U /*%< -6 */ 45 #define NS_SERVER_DISABLE6 0x00000200U /*%< -4 */ 46 #define NS_SERVER_FIXEDLOCAL 0x00000400U /*%< -T fixedlocal */ 47 #define NS_SERVER_SIGVALINSECS 0x00000800U /*%< -T sigvalinsecs */ 48 #define NS_SERVER_EDNSFORMERR 0x00001000U /*%< -T ednsformerr (STD13) */ 49 #define NS_SERVER_EDNSNOTIMP 0x00002000U /*%< -T ednsnotimp */ 50 #define NS_SERVER_EDNSREFUSED 0x00004000U /*%< -T ednsrefused */ 51 #define NS_SERVER_TRANSFERINSECS 0x00008000U /*%< -T transferinsecs */ 52 #define NS_SERVER_TRANSFERSLOWLY 0x00010000U /*%< -T transferslowly */ 53 #define NS_SERVER_TRANSFERSTUCK 0x00020000U /*%< -T transferstuck */ 54 #define NS_SERVER_LOGRESPONSES 0x00040000U /*%< log responses */ 55 56 /*% 57 * Type for callback function to get hostname. 58 */ 59 typedef isc_result_t (*ns_hostnamecb_t)(char *buf, size_t len); 60 61 /*% 62 * Type for callback function to signal the fuzzer thread 63 * when built with AFL. 64 */ 65 typedef void (*ns_fuzzcb_t)(void); 66 67 /*% 68 * Type for callback function to get the view that can answer a query. 69 */ 70 typedef isc_result_t (*ns_matchview_t)( 71 isc_netaddr_t *srcaddr, isc_netaddr_t *destaddr, dns_message_t *message, 72 dns_aclenv_t *env, ns_server_t *sctx, isc_loop_t *loop, isc_job_cb cb, 73 void *cbarg, isc_result_t *sigresultp, isc_result_t *viewmatchresult, 74 dns_view_t **viewp); 75 76 /*% 77 * Server context. 78 */ 79 struct ns_server { 80 unsigned int magic; 81 isc_mem_t *mctx; 82 83 isc_refcount_t references; 84 85 /*% Server cookie secret and algorithm */ 86 unsigned char secret[32]; 87 ns_cookiealg_t cookiealg; 88 ns_altsecretlist_t altsecrets; 89 bool answercookie; 90 91 /*% Quotas */ 92 isc_quota_t recursionquota; 93 isc_quota_t tcpquota; 94 isc_quota_t xfroutquota; 95 isc_quota_t updquota; 96 isc_quota_t sig0checksquota; 97 dns_acl_t *sig0checksquota_exempt; 98 ISC_LIST(isc_quota_t) http_quotas; 99 isc_mutex_t http_quotas_lock; 100 101 /*% Test options and other configurables */ 102 uint32_t options; 103 104 dns_acl_t *blackholeacl; 105 uint16_t udpsize; 106 uint16_t transfer_tcp_message_size; 107 bool interface_auto; 108 dns_tkeyctx_t *tkeyctx; 109 uint8_t max_restarts; 110 111 /*% Server id for NSID */ 112 char *server_id; 113 bool usehostname; 114 115 /*% Fuzzer callback */ 116 isc_fuzztype_t fuzztype; 117 ns_fuzzcb_t fuzznotify; 118 119 /*% Callback to find a matching view for a query */ 120 ns_matchview_t matchingview; 121 122 /*% Stats counters */ 123 ns_stats_t *nsstats; 124 dns_stats_t *rcvquerystats; 125 dns_stats_t *opcodestats; 126 dns_stats_t *rcodestats; 127 128 isc_histomulti_t *udpinstats4; 129 isc_histomulti_t *udpoutstats4; 130 isc_histomulti_t *udpinstats6; 131 isc_histomulti_t *udpoutstats6; 132 133 isc_histomulti_t *tcpinstats4; 134 isc_histomulti_t *tcpoutstats4; 135 isc_histomulti_t *tcpinstats6; 136 isc_histomulti_t *tcpoutstats6; 137 }; 138 139 struct ns_altsecret { 140 ISC_LINK(ns_altsecret_t) link; 141 unsigned char secret[32]; 142 }; 143 144 void 145 ns_server_create(isc_mem_t *mctx, ns_matchview_t matchingview, 146 ns_server_t **sctxp); 147 /*%< 148 * Create a server context object with default settings. 149 */ 150 151 void 152 ns_server_attach(ns_server_t *src, ns_server_t **dest); 153 /*%< 154 * Attach a server context. 155 * 156 * Requires: 157 *\li 'src' is valid. 158 */ 159 160 void 161 ns_server_detach(ns_server_t **sctxp); 162 /*%< 163 * Detach from a server context. If its reference count drops to zero, destroy 164 * it, freeing its memory. 165 * 166 * Requires: 167 *\li '*sctxp' is valid. 168 * Ensures: 169 *\li '*sctxp' is NULL on return. 170 */ 171 172 isc_result_t 173 ns_server_setserverid(ns_server_t *sctx, const char *serverid); 174 /*%< 175 * Set sctx->server_id to 'serverid'. If it was set previously, free the memory. 176 * 177 * Requires: 178 *\li 'sctx' is valid. 179 */ 180 181 void 182 ns_server_setoption(ns_server_t *sctx, unsigned int option, bool value); 183 /*%< 184 * Set the given options on (if 'value' == #true) 185 * or off (if 'value' == #false). 186 * 187 * Requires: 188 *\li 'sctx' is valid 189 */ 190 191 bool 192 ns_server_getoption(ns_server_t *sctx, unsigned int option); 193 /*%< 194 * Returns the current value of the specified server option. 195 * 196 * Requires: 197 *\li 'sctx' is valid. 198 */ 199 200 void 201 ns_server_append_http_quota(ns_server_t *sctx, isc_quota_t *http_quota); 202 /*%< 203 * Add a quota to the list of HTTP quotas to destroy it safely later. 204 * 205 * Requires: 206 *\li 'sctx' is valid; 207 *\li 'http_quota' is not 'NULL'. 208 */ 209