1 /* 2 * services/cache/infra.h - infrastructure cache, server rtt and capabilities 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 contains the infrastructure cache, as well as rate limiting. 40 * Note that there are two sorts of rate-limiting here: 41 * - Pre-cache, per-query rate limiting (query ratelimits) 42 * - Post-cache, per-domain name rate limiting (infra-ratelimits) 43 */ 44 45 #ifndef SERVICES_CACHE_INFRA_H 46 #define SERVICES_CACHE_INFRA_H 47 #include "util/storage/lruhash.h" 48 #include "util/storage/dnstree.h" 49 #include "util/rtt.h" 50 #include "util/netevent.h" 51 #include "util/data/msgreply.h" 52 struct slabhash; 53 struct config_file; 54 55 /** 56 * Host information kept for every server, per zone. 57 */ 58 struct infra_key { 59 /** the host address. */ 60 struct sockaddr_storage addr; 61 /** length of addr. */ 62 socklen_t addrlen; 63 /** zone name in wireformat */ 64 uint8_t* zonename; 65 /** length of zonename */ 66 size_t namelen; 67 /** hash table entry, data of type infra_data. */ 68 struct lruhash_entry entry; 69 }; 70 71 /** 72 * Host information encompasses host capabilities and retransmission timeouts. 73 * And lameness information (notAuthoritative, noEDNS, Recursive) 74 */ 75 struct infra_data { 76 /** TTL value for this entry. absolute time. */ 77 time_t ttl; 78 79 /** time in seconds (absolute) when probing re-commences, 0 disabled */ 80 time_t probedelay; 81 /** round trip times for timeout calculation */ 82 struct rtt_info rtt; 83 84 /** edns version that the host supports, -1 means no EDNS */ 85 int edns_version; 86 /** if the EDNS lameness is already known or not. 87 * EDNS lame is when EDNS queries or replies are dropped, 88 * and cause a timeout */ 89 uint8_t edns_lame_known; 90 91 /** is the host lame (does not serve the zone authoritatively), 92 * or is the host dnssec lame (does not serve DNSSEC data) */ 93 uint8_t isdnsseclame; 94 /** is the host recursion lame (not AA, but RA) */ 95 uint8_t rec_lame; 96 /** the host is lame (not authoritative) for A records */ 97 uint8_t lame_type_A; 98 /** the host is lame (not authoritative) for other query types */ 99 uint8_t lame_other; 100 101 /** timeouts counter for type A */ 102 uint8_t timeout_A; 103 /** timeouts counter for type AAAA */ 104 uint8_t timeout_AAAA; 105 /** timeouts counter for others */ 106 uint8_t timeout_other; 107 }; 108 109 /** 110 * Infra cache 111 */ 112 struct infra_cache { 113 /** The hash table with hosts */ 114 struct slabhash* hosts; 115 /** TTL value for host information, in seconds */ 116 int host_ttl; 117 /** the hosts that are down are kept probed for recovery */ 118 int infra_keep_probing; 119 /** hash table with query rates per name: rate_key, rate_data */ 120 struct slabhash* domain_rates; 121 /** ratelimit settings for domains, struct domain_limit_data */ 122 rbtree_type domain_limits; 123 /** hash table with query rates per client ip: ip_rate_key, ip_rate_data */ 124 struct slabhash* client_ip_rates; 125 }; 126 127 /** ratelimit, unless overridden by domain_limits, 0 is off */ 128 extern int infra_dp_ratelimit; 129 130 /** 131 * ratelimit settings for domains 132 */ 133 struct domain_limit_data { 134 /** key for rbtree, must be first in struct, name of domain */ 135 struct name_tree_node node; 136 /** ratelimit for exact match with this name, -1 if not set */ 137 int lim; 138 /** ratelimit for names below this name, -1 if not set */ 139 int below; 140 }; 141 142 /** 143 * key for ratelimit lookups, a domain name 144 */ 145 struct rate_key { 146 /** lruhash key entry */ 147 struct lruhash_entry entry; 148 /** domain name in uncompressed wireformat */ 149 uint8_t* name; 150 /** length of name */ 151 size_t namelen; 152 }; 153 154 /** ip ratelimit, 0 is off */ 155 extern int infra_ip_ratelimit; 156 /** ip ratelimit for DNS Cookie clients, 0 is off */ 157 extern int infra_ip_ratelimit_cookie; 158 159 /** 160 * key for ip_ratelimit lookups, a source IP. 161 */ 162 struct ip_rate_key { 163 /** lruhash key entry */ 164 struct lruhash_entry entry; 165 /** client ip information */ 166 struct sockaddr_storage addr; 167 /** length of address */ 168 socklen_t addrlen; 169 }; 170 171 /** number of seconds to track qps rate */ 172 #define RATE_WINDOW 2 173 174 /** 175 * Data for ratelimits per domain name 176 * It is incremented when a non-cache-lookup happens for that domain name. 177 * The name is the delegation point we have for the name. 178 * If a new delegation point is found (a referral reply), the previous 179 * delegation point is decremented, and the new one is charged with the query. 180 */ 181 struct rate_data { 182 /** queries counted, for that second. 0 if not in use. */ 183 int qps[RATE_WINDOW]; 184 /** what the timestamp is of the qps array members, counter is 185 * valid for that timestamp. Usually now and now-1. */ 186 time_t timestamp[RATE_WINDOW]; 187 }; 188 189 #define ip_rate_data rate_data 190 191 /** infra host cache default hash lookup size */ 192 #define INFRA_HOST_STARTSIZE 32 193 /** bytes per zonename reserved in the hostcache, dnamelen(zonename.com.) */ 194 #define INFRA_BYTES_NAME 14 195 196 /** 197 * Create infra cache. 198 * @param cfg: config parameters or NULL for defaults. 199 * @return: new infra cache, or NULL. 200 */ 201 struct infra_cache* infra_create(struct config_file* cfg); 202 203 /** 204 * Delete infra cache. 205 * @param infra: infrastructure cache to delete. 206 */ 207 void infra_delete(struct infra_cache* infra); 208 209 /** 210 * Adjust infra cache to use updated configuration settings. 211 * This may clean the cache. Operates a bit like realloc. 212 * There may be no threading or use by other threads. 213 * @param infra: existing cache. If NULL a new infra cache is returned. 214 * @param cfg: config options. 215 * @return the new infra cache pointer or NULL on error. 216 */ 217 struct infra_cache* infra_adjust(struct infra_cache* infra, 218 struct config_file* cfg); 219 220 /** 221 * Plain find infra data function (used by the the other functions) 222 * @param infra: infrastructure cache. 223 * @param addr: host address. 224 * @param addrlen: length of addr. 225 * @param name: domain name of zone. 226 * @param namelen: length of domain name. 227 * @param wr: if true, writelock, else readlock. 228 * @return the entry, could be expired (this is not checked) or NULL. 229 */ 230 struct lruhash_entry* infra_lookup_nottl(struct infra_cache* infra, 231 struct sockaddr_storage* addr, socklen_t addrlen, uint8_t* name, 232 size_t namelen, int wr); 233 234 /** 235 * Find host information to send a packet. Creates new entry if not found. 236 * Lameness is empty. EDNS is 0 (try with first), and rtt is returned for 237 * the first message to it. 238 * Use this to send a packet only, because it also locks out others when 239 * probing is restricted. 240 * @param infra: infrastructure cache. 241 * @param addr: host address. 242 * @param addrlen: length of addr. 243 * @param name: domain name of zone. 244 * @param namelen: length of domain name. 245 * @param timenow: what time it is now. 246 * @param edns_vs: edns version it supports, is returned. 247 * @param edns_lame_known: if EDNS lame (EDNS is dropped in transit) has 248 * already been probed, is returned. 249 * @param to: timeout to use, is returned. 250 * @return: 0 on error. 251 */ 252 int infra_host(struct infra_cache* infra, struct sockaddr_storage* addr, 253 socklen_t addrlen, uint8_t* name, size_t namelen, 254 time_t timenow, int* edns_vs, uint8_t* edns_lame_known, int* to); 255 256 /** 257 * Set a host to be lame for the given zone. 258 * @param infra: infrastructure cache. 259 * @param addr: host address. 260 * @param addrlen: length of addr. 261 * @param name: domain name of zone apex. 262 * @param namelen: length of domain name. 263 * @param timenow: what time it is now. 264 * @param dnsseclame: if true the host is set dnssec lame. 265 * if false, the host is marked lame (not serving the zone). 266 * @param reclame: if true host is a recursor not AA server. 267 * if false, dnsseclame or marked lame. 268 * @param qtype: the query type for which it is lame. 269 * @return: 0 on error. 270 */ 271 int infra_set_lame(struct infra_cache* infra, 272 struct sockaddr_storage* addr, socklen_t addrlen, 273 uint8_t* name, size_t namelen, time_t timenow, int dnsseclame, 274 int reclame, uint16_t qtype); 275 276 /** 277 * Update rtt information for the host. 278 * @param infra: infrastructure cache. 279 * @param addr: host address. 280 * @param addrlen: length of addr. 281 * @param name: zone name 282 * @param namelen: zone name length 283 * @param qtype: query type. 284 * @param roundtrip: estimate of roundtrip time in milliseconds or -1 for 285 * timeout. 286 * @param orig_rtt: original rtt for the query that timed out (roundtrip==-1). 287 * ignored if roundtrip != -1. 288 * @param timenow: what time it is now. 289 * @return: 0 on error. new rto otherwise. 290 */ 291 int infra_rtt_update(struct infra_cache* infra, struct sockaddr_storage* addr, 292 socklen_t addrlen, uint8_t* name, size_t namelen, int qtype, 293 int roundtrip, int orig_rtt, time_t timenow); 294 295 /** 296 * Update information for the host, store that a TCP transaction works. 297 * @param infra: infrastructure cache. 298 * @param addr: host address. 299 * @param addrlen: length of addr. 300 * @param name: name of zone 301 * @param namelen: length of name 302 */ 303 void infra_update_tcp_works(struct infra_cache* infra, 304 struct sockaddr_storage* addr, socklen_t addrlen, 305 uint8_t* name, size_t namelen); 306 307 /** 308 * Update edns information for the host. 309 * @param infra: infrastructure cache. 310 * @param addr: host address. 311 * @param addrlen: length of addr. 312 * @param name: name of zone 313 * @param namelen: length of name 314 * @param edns_version: the version that it publishes. 315 * If it is known to support EDNS then no-EDNS is not stored over it. 316 * @param timenow: what time it is now. 317 * @return: 0 on error. 318 */ 319 int infra_edns_update(struct infra_cache* infra, 320 struct sockaddr_storage* addr, socklen_t addrlen, 321 uint8_t* name, size_t namelen, int edns_version, time_t timenow); 322 323 /** 324 * Get Lameness information and average RTT if host is in the cache. 325 * This information is to be used for server selection. 326 * @param infra: infrastructure cache. 327 * @param addr: host address. 328 * @param addrlen: length of addr. 329 * @param name: zone name. 330 * @param namelen: zone name length. 331 * @param qtype: the query to be made. 332 * @param lame: if function returns true, this returns lameness of the zone. 333 * @param dnsseclame: if function returns true, this returns if the zone 334 * is dnssec-lame. 335 * @param reclame: if function returns true, this is if it is recursion lame. 336 * @param rtt: if function returns true, this returns avg rtt of the server. 337 * The rtt value is unclamped and reflects recent timeouts. 338 * @param timenow: what time it is now. 339 * @return if found in cache, or false if not (or TTL bad). 340 */ 341 int infra_get_lame_rtt(struct infra_cache* infra, 342 struct sockaddr_storage* addr, socklen_t addrlen, 343 uint8_t* name, size_t namelen, uint16_t qtype, 344 int* lame, int* dnsseclame, int* reclame, int* rtt, time_t timenow); 345 346 /** 347 * Get additional (debug) info on timing. 348 * @param infra: infra cache. 349 * @param addr: host address. 350 * @param addrlen: length of addr. 351 * @param name: zone name 352 * @param namelen: zone name length 353 * @param rtt: the rtt_info is copied into here (caller alloced return struct). 354 * @param delay: probe delay (if any). 355 * @param timenow: what time it is now. 356 * @param tA: timeout counter on type A. 357 * @param tAAAA: timeout counter on type AAAA. 358 * @param tother: timeout counter on type other. 359 * @return TTL the infra host element is valid for. If -1: not found in cache. 360 * TTL -2: found but expired. 361 */ 362 long long infra_get_host_rto(struct infra_cache* infra, 363 struct sockaddr_storage* addr, socklen_t addrlen, uint8_t* name, 364 size_t namelen, struct rtt_info* rtt, int* delay, time_t timenow, 365 int* tA, int* tAAAA, int* tother); 366 367 /** 368 * Increment the query rate counter for a delegation point. 369 * @param infra: infra cache. 370 * @param name: zone name 371 * @param namelen: zone name length 372 * @param timenow: what time it is now. 373 * @param backoff: if backoff is enabled. 374 * @param qinfo: for logging, query name. 375 * @param replylist: for logging, querier's address (if any). 376 * @return 1 if it could be incremented. 0 if the increment overshot the 377 * ratelimit or if in the previous second the ratelimit was exceeded. 378 * Failures like alloc failures are not returned (probably as 1). 379 */ 380 int infra_ratelimit_inc(struct infra_cache* infra, uint8_t* name, 381 size_t namelen, time_t timenow, int backoff, struct query_info* qinfo, 382 struct comm_reply* replylist); 383 384 /** 385 * Decrement the query rate counter for a delegation point. 386 * Because the reply received for the delegation point was pleasant, 387 * we do not charge this delegation point with it (i.e. it was a referral). 388 * Should call it with same second as when inc() was called. 389 * @param infra: infra cache. 390 * @param name: zone name 391 * @param namelen: zone name length 392 * @param timenow: what time it is now. 393 */ 394 void infra_ratelimit_dec(struct infra_cache* infra, uint8_t* name, 395 size_t namelen, time_t timenow); 396 397 /** 398 * See if the query rate counter for a delegation point is exceeded. 399 * So, no queries are going to be allowed. 400 * @param infra: infra cache. 401 * @param name: zone name 402 * @param namelen: zone name length 403 * @param timenow: what time it is now. 404 * @param backoff: if backoff is enabled. 405 * @return true if exceeded. 406 */ 407 int infra_ratelimit_exceeded(struct infra_cache* infra, uint8_t* name, 408 size_t namelen, time_t timenow, int backoff); 409 410 /** find the maximum rate stored. 0 if no information. 411 * When backoff is enabled look for the maximum in the whole RATE_WINDOW. */ 412 int infra_rate_max(void* data, time_t now, int backoff); 413 414 /** find the ratelimit in qps for a domain. 0 if no limit for domain. */ 415 int infra_find_ratelimit(struct infra_cache* infra, uint8_t* name, 416 size_t namelen); 417 418 /** Update query ratelimit hash and decide 419 * whether or not a query should be dropped. 420 * @param infra: infra cache 421 * @param addr: client address 422 * @param addrlen: client address length 423 * @param timenow: what time it is now. 424 * @param has_cookie: if the request came with a DNS Cookie. 425 * @param backoff: if backoff is enabled. 426 * @param buffer: with query for logging. 427 * @return 1 if it could be incremented. 0 if the increment overshot the 428 * ratelimit and the query should be dropped. */ 429 int infra_ip_ratelimit_inc(struct infra_cache* infra, 430 struct sockaddr_storage* addr, socklen_t addrlen, time_t timenow, 431 int has_cookie, int backoff, struct sldns_buffer* buffer); 432 433 /** 434 * Get memory used by the infra cache. 435 * @param infra: infrastructure cache. 436 * @return memory in use in bytes. 437 */ 438 size_t infra_get_mem(struct infra_cache* infra); 439 440 /** calculate size for the hashtable, does not count size of lameness, 441 * so the hashtable is a fixed number of items */ 442 size_t infra_sizefunc(void* k, void* d); 443 444 /** compare two addresses, returns -1, 0, or +1 */ 445 int infra_compfunc(void* key1, void* key2); 446 447 /** delete key, and destroy the lock */ 448 void infra_delkeyfunc(void* k, void* arg); 449 450 /** delete data and destroy the lameness hashtable */ 451 void infra_deldatafunc(void* d, void* arg); 452 453 /** calculate size for the hashtable */ 454 size_t rate_sizefunc(void* k, void* d); 455 456 /** compare two names, returns -1, 0, or +1 */ 457 int rate_compfunc(void* key1, void* key2); 458 459 /** delete key, and destroy the lock */ 460 void rate_delkeyfunc(void* k, void* arg); 461 462 /** delete data */ 463 void rate_deldatafunc(void* d, void* arg); 464 465 /* calculate size for the client ip hashtable */ 466 size_t ip_rate_sizefunc(void* k, void* d); 467 468 /* compare two addresses */ 469 int ip_rate_compfunc(void* key1, void* key2); 470 471 /* delete key, and destroy the lock */ 472 void ip_rate_delkeyfunc(void* d, void* arg); 473 474 /* delete data */ 475 #define ip_rate_deldatafunc rate_deldatafunc 476 477 #endif /* SERVICES_CACHE_INFRA_H */ 478