1ae8c6e27Sflorian /* 2ae8c6e27Sflorian * services/cache/dns.h - Cache services for DNS using msg and rrset caches. 3ae8c6e27Sflorian * 4ae8c6e27Sflorian * Copyright (c) 2007, NLnet Labs. All rights reserved. 5ae8c6e27Sflorian * 6ae8c6e27Sflorian * This software is open source. 7ae8c6e27Sflorian * 8ae8c6e27Sflorian * Redistribution and use in source and binary forms, with or without 9ae8c6e27Sflorian * modification, are permitted provided that the following conditions 10ae8c6e27Sflorian * are met: 11ae8c6e27Sflorian * 12ae8c6e27Sflorian * Redistributions of source code must retain the above copyright notice, 13ae8c6e27Sflorian * this list of conditions and the following disclaimer. 14ae8c6e27Sflorian * 15ae8c6e27Sflorian * Redistributions in binary form must reproduce the above copyright notice, 16ae8c6e27Sflorian * this list of conditions and the following disclaimer in the documentation 17ae8c6e27Sflorian * and/or other materials provided with the distribution. 18ae8c6e27Sflorian * 19ae8c6e27Sflorian * Neither the name of the NLNET LABS nor the names of its contributors may 20ae8c6e27Sflorian * be used to endorse or promote products derived from this software without 21ae8c6e27Sflorian * specific prior written permission. 22ae8c6e27Sflorian * 23ae8c6e27Sflorian * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24ae8c6e27Sflorian * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25ae8c6e27Sflorian * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26ae8c6e27Sflorian * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27ae8c6e27Sflorian * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28ae8c6e27Sflorian * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 29ae8c6e27Sflorian * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30ae8c6e27Sflorian * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31ae8c6e27Sflorian * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32ae8c6e27Sflorian * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33ae8c6e27Sflorian * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34ae8c6e27Sflorian */ 35ae8c6e27Sflorian 36ae8c6e27Sflorian /** 37ae8c6e27Sflorian * \file 38ae8c6e27Sflorian * 39ae8c6e27Sflorian * This file contains the DNS cache. 40ae8c6e27Sflorian */ 41ae8c6e27Sflorian 42ae8c6e27Sflorian #ifndef SERVICES_CACHE_DNS_H 43ae8c6e27Sflorian #define SERVICES_CACHE_DNS_H 44ae8c6e27Sflorian #include "util/storage/lruhash.h" 45ae8c6e27Sflorian #include "util/data/msgreply.h" 46ae8c6e27Sflorian struct module_env; 47ae8c6e27Sflorian struct query_info; 48ae8c6e27Sflorian struct reply_info; 49ae8c6e27Sflorian struct regional; 50ae8c6e27Sflorian struct delegpt; 51ae8c6e27Sflorian 52ae8c6e27Sflorian /** Flags to control behavior of dns_cache_store() and dns_cache_store_msg(). 53ae8c6e27Sflorian * Must be an unsigned 32-bit value larger than 0xffff */ 54ae8c6e27Sflorian 55ae8c6e27Sflorian /** Allow caching a DNS message with a zero TTL. */ 56ae8c6e27Sflorian #define DNSCACHE_STORE_ZEROTTL 0x100000 57ae8c6e27Sflorian 58ae8c6e27Sflorian /** 59ae8c6e27Sflorian * Region allocated message reply 60ae8c6e27Sflorian */ 61ae8c6e27Sflorian struct dns_msg { 62ae8c6e27Sflorian /** query info */ 63ae8c6e27Sflorian struct query_info qinfo; 64ae8c6e27Sflorian /** reply info - ptr to packed repinfo structure */ 65ae8c6e27Sflorian struct reply_info *rep; 66ae8c6e27Sflorian }; 67ae8c6e27Sflorian 68ae8c6e27Sflorian /** 69ae8c6e27Sflorian * Allocate a dns_msg with malloc/alloc structure and store in dns cache. 70ae8c6e27Sflorian * 71ae8c6e27Sflorian * @param env: environment, with alloc structure and dns cache. 72ae8c6e27Sflorian * @param qinf: query info, the query for which answer is stored. 73ae8c6e27Sflorian * this is allocated in a region, and will be copied to malloc area 74ae8c6e27Sflorian * before insertion. 75ae8c6e27Sflorian * @param rep: reply in dns_msg from dns_alloc_msg for example. 76ae8c6e27Sflorian * this is allocated in a region, and will be copied to malloc area 77ae8c6e27Sflorian * before insertion. 78ae8c6e27Sflorian * @param is_referral: If true, then the given message to be stored is a 79ae8c6e27Sflorian * referral. The cache implementation may use this as a hint. 80ae8c6e27Sflorian * It will store only the RRsets, not the message. 81ae8c6e27Sflorian * @param leeway: TTL value, if not 0, other rrsets are considered expired 82ae8c6e27Sflorian * that many seconds before actual TTL expiry. 83ae8c6e27Sflorian * @param pside: if true, information came from a server which was fetched 84ae8c6e27Sflorian * from the parentside of the zonecut. This means that the type NS 85ae8c6e27Sflorian * can be updated to full TTL even in prefetch situations. 86ae8c6e27Sflorian * @param region: region to allocate better entries from cache into. 87ae8c6e27Sflorian * (used when is_referral is false). 88ae8c6e27Sflorian * @param flags: flags with BIT_CD for AAAA queries in dns64 translation. 89ae8c6e27Sflorian * The higher 16 bits are used internally to customize the cache policy. 90ae8c6e27Sflorian * (See DNSCACHE_STORE_xxx flags). 91ae8c6e27Sflorian * @return 0 on alloc error (out of memory). 92ae8c6e27Sflorian */ 93ae8c6e27Sflorian int dns_cache_store(struct module_env* env, struct query_info* qinf, 94ae8c6e27Sflorian struct reply_info* rep, int is_referral, time_t leeway, int pside, 95ae8c6e27Sflorian struct regional* region, uint32_t flags); 96ae8c6e27Sflorian 97ae8c6e27Sflorian /** 98ae8c6e27Sflorian * Store message in the cache. Stores in message cache and rrset cache. 99ae8c6e27Sflorian * Both qinfo and rep should be malloced and are put in the cache. 100ae8c6e27Sflorian * They should not be used after this call, as they are then in shared cache. 101ae8c6e27Sflorian * Does not return errors, they are logged and only lead to less cache. 102ae8c6e27Sflorian * 103ae8c6e27Sflorian * @param env: module environment with the DNS cache. 104ae8c6e27Sflorian * @param qinfo: query info 105ae8c6e27Sflorian * @param hash: hash over qinfo. 106ae8c6e27Sflorian * @param rep: reply info, together with qinfo makes up the message. 107ae8c6e27Sflorian * Adjusts the reply info TTLs to absolute time. 108ae8c6e27Sflorian * @param leeway: TTL value, if not 0, other rrsets are considered expired 109ae8c6e27Sflorian * that many seconds before actual TTL expiry. 110ae8c6e27Sflorian * @param pside: if true, information came from a server which was fetched 111ae8c6e27Sflorian * from the parentside of the zonecut. This means that the type NS 112ae8c6e27Sflorian * can be updated to full TTL even in prefetch situations. 113ae8c6e27Sflorian * @param qrep: message that can be altered with better rrs from cache. 114ae8c6e27Sflorian * @param flags: customization flags for the cache policy. 115ae8c6e27Sflorian * @param region: to allocate into for qmsg. 116ae8c6e27Sflorian */ 117ae8c6e27Sflorian void dns_cache_store_msg(struct module_env* env, struct query_info* qinfo, 118ae8c6e27Sflorian hashvalue_type hash, struct reply_info* rep, time_t leeway, int pside, 119ae8c6e27Sflorian struct reply_info* qrep, uint32_t flags, struct regional* region); 120ae8c6e27Sflorian 121ae8c6e27Sflorian /** 122ae8c6e27Sflorian * Find a delegation from the cache. 123ae8c6e27Sflorian * @param env: module environment with the DNS cache. 124ae8c6e27Sflorian * @param qname: query name. 125ae8c6e27Sflorian * @param qnamelen: length of qname. 126ae8c6e27Sflorian * @param qtype: query type. 127ae8c6e27Sflorian * @param qclass: query class. 128ae8c6e27Sflorian * @param region: where to allocate result delegation. 129ae8c6e27Sflorian * @param msg: if not NULL, delegation message is returned here, synthesized 130ae8c6e27Sflorian * from the cache. 131ae8c6e27Sflorian * @param timenow: the time now, for checking if TTL on cache entries is OK. 132ae8c6e27Sflorian * @return new delegation or NULL on error or if not found in cache. 133ae8c6e27Sflorian */ 134ae8c6e27Sflorian struct delegpt* dns_cache_find_delegation(struct module_env* env, 135ae8c6e27Sflorian uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass, 136ae8c6e27Sflorian struct regional* region, struct dns_msg** msg, time_t timenow); 137ae8c6e27Sflorian 138ae8c6e27Sflorian /** 139ae8c6e27Sflorian * generate dns_msg from cached message 140ae8c6e27Sflorian * @param env: module environment with the DNS cache. NULL if the LRU from cache 141ae8c6e27Sflorian * does not need to be touched. 142ae8c6e27Sflorian * @param q: query info, contains qname that will make up the dns message. 143ae8c6e27Sflorian * @param r: reply info that, together with qname, will make up the dns message. 144ae8c6e27Sflorian * @param region: where to allocate dns message. 145ae8c6e27Sflorian * @param now: the time now, for check if TTL on cache entry is ok. 146*d32eb43cSflorian * @param allow_expired: if true and serve-expired is enabled, it will allow 147*d32eb43cSflorian * for expired dns_msg to be generated based on the configured serve-expired 148*d32eb43cSflorian * logic. 149ae8c6e27Sflorian * @param scratch: where to allocate temporary data. 150ae8c6e27Sflorian * */ 151ae8c6e27Sflorian struct dns_msg* tomsg(struct module_env* env, struct query_info* q, 152ae8c6e27Sflorian struct reply_info* r, struct regional* region, time_t now, 153*d32eb43cSflorian int allow_expired, struct regional* scratch); 154ae8c6e27Sflorian 155ae8c6e27Sflorian /** 156ae8c6e27Sflorian * Find cached message 157ae8c6e27Sflorian * @param env: module environment with the DNS cache. 158ae8c6e27Sflorian * @param qname: query name. 159ae8c6e27Sflorian * @param qnamelen: length of qname. 160ae8c6e27Sflorian * @param qtype: query type. 161ae8c6e27Sflorian * @param qclass: query class. 162ae8c6e27Sflorian * @param flags: flags with BIT_CD for AAAA queries in dns64 translation. 163ae8c6e27Sflorian * @param region: where to allocate result. 164ae8c6e27Sflorian * @param scratch: where to allocate temporary data. 165ae8c6e27Sflorian * @param no_partial: if true, only complete messages and not a partial 166ae8c6e27Sflorian * one (with only the start of the CNAME chain and not the rest). 167ae8c6e27Sflorian * @return new response message (alloced in region, rrsets do not have IDs). 168ae8c6e27Sflorian * or NULL on error or if not found in cache. 169ae8c6e27Sflorian * TTLs are made relative to the current time. 170ae8c6e27Sflorian */ 171ae8c6e27Sflorian struct dns_msg* dns_cache_lookup(struct module_env* env, 172ae8c6e27Sflorian uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass, 173ae8c6e27Sflorian uint16_t flags, struct regional* region, struct regional* scratch, 174ae8c6e27Sflorian int no_partial); 175ae8c6e27Sflorian 176ae8c6e27Sflorian /** 177ae8c6e27Sflorian * find and add A and AAAA records for missing nameservers in delegpt 178ae8c6e27Sflorian * @param env: module environment with rrset cache 179ae8c6e27Sflorian * @param qclass: which class to look in. 180ae8c6e27Sflorian * @param region: where to store new dp info. 181ae8c6e27Sflorian * @param dp: delegation point to fill missing entries. 182ae8c6e27Sflorian * @return false on alloc failure. 183ae8c6e27Sflorian */ 184ae8c6e27Sflorian int cache_fill_missing(struct module_env* env, uint16_t qclass, 185ae8c6e27Sflorian struct regional* region, struct delegpt* dp); 186ae8c6e27Sflorian 187ae8c6e27Sflorian /** 188ae8c6e27Sflorian * Utility, create new, unpacked data structure for cache response. 189ae8c6e27Sflorian * QR bit set, no AA. Query set as indicated. Space for number of rrsets. 190ae8c6e27Sflorian * @param qname: query section name 191ae8c6e27Sflorian * @param qnamelen: len of qname 192ae8c6e27Sflorian * @param qtype: query section type 193ae8c6e27Sflorian * @param qclass: query section class 194ae8c6e27Sflorian * @param region: where to alloc. 195ae8c6e27Sflorian * @param capacity: number of rrsets space to create in the array. 196ae8c6e27Sflorian * @return new dns_msg struct or NULL on mem fail. 197ae8c6e27Sflorian */ 198ae8c6e27Sflorian struct dns_msg* dns_msg_create(uint8_t* qname, size_t qnamelen, uint16_t qtype, 199ae8c6e27Sflorian uint16_t qclass, struct regional* region, size_t capacity); 200ae8c6e27Sflorian 201ae8c6e27Sflorian /** 202ae8c6e27Sflorian * Add rrset to authority section in unpacked dns_msg message. Must have enough 203ae8c6e27Sflorian * space left, does not grow the array. 204ae8c6e27Sflorian * @param msg: msg to put it in. 205ae8c6e27Sflorian * @param region: region to alloc in 206ae8c6e27Sflorian * @param rrset: to add in authority section 207ae8c6e27Sflorian * @param now: now. 208ae8c6e27Sflorian * @return true if worked, false on fail 209ae8c6e27Sflorian */ 210ae8c6e27Sflorian int dns_msg_authadd(struct dns_msg* msg, struct regional* region, 211ae8c6e27Sflorian struct ub_packed_rrset_key* rrset, time_t now); 212ae8c6e27Sflorian 213ae8c6e27Sflorian /** 214ae8c6e27Sflorian * Add rrset to authority section in unpacked dns_msg message. Must have enough 215ae8c6e27Sflorian * space left, does not grow the array. 216ae8c6e27Sflorian * @param msg: msg to put it in. 217ae8c6e27Sflorian * @param region: region to alloc in 218ae8c6e27Sflorian * @param rrset: to add in authority section 219ae8c6e27Sflorian * @param now: now. 220ae8c6e27Sflorian * @return true if worked, false on fail 221ae8c6e27Sflorian */ 222ae8c6e27Sflorian int dns_msg_ansadd(struct dns_msg* msg, struct regional* region, 223ae8c6e27Sflorian struct ub_packed_rrset_key* rrset, time_t now); 224ae8c6e27Sflorian 225ae8c6e27Sflorian /** 226ae8c6e27Sflorian * Adjust the prefetch_ttl for a cached message. This adds a value to the 227ae8c6e27Sflorian * prefetch ttl - postponing the time when it will be prefetched for future 228ae8c6e27Sflorian * incoming queries. 229ae8c6e27Sflorian * @param env: module environment with caches and time. 230ae8c6e27Sflorian * @param qinfo: query info for the query that needs adjustment. 231ae8c6e27Sflorian * @param adjust: time in seconds to add to the prefetch_leeway. 232ae8c6e27Sflorian * @param flags: flags with BIT_CD for AAAA queries in dns64 translation. 233ae8c6e27Sflorian * @return false if not in cache. true if added. 234ae8c6e27Sflorian */ 235ae8c6e27Sflorian int dns_cache_prefetch_adjust(struct module_env* env, struct query_info* qinfo, 236ae8c6e27Sflorian time_t adjust, uint16_t flags); 237ae8c6e27Sflorian 238ae8c6e27Sflorian /** lookup message in message cache 239ae8c6e27Sflorian * the returned nonNULL entry is locked and has to be unlocked by the caller */ 240ae8c6e27Sflorian struct msgreply_entry* msg_cache_lookup(struct module_env* env, 241ae8c6e27Sflorian uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass, 242ae8c6e27Sflorian uint16_t flags, time_t now, int wr); 243ae8c6e27Sflorian 244ae8c6e27Sflorian /** 245ae8c6e27Sflorian * Remove entry from the message cache. For unwanted entries. 246ae8c6e27Sflorian * @param env: with message cache. 247ae8c6e27Sflorian * @param qname: query name, in wireformat 248ae8c6e27Sflorian * @param qnamelen: length of qname, including terminating 0. 249ae8c6e27Sflorian * @param qtype: query type, host order. 250ae8c6e27Sflorian * @param qclass: query class, host order. 251ae8c6e27Sflorian * @param flags: flags 252ae8c6e27Sflorian */ 253ae8c6e27Sflorian void msg_cache_remove(struct module_env* env, uint8_t* qname, size_t qnamelen, 254ae8c6e27Sflorian uint16_t qtype, uint16_t qclass, uint16_t flags); 255ae8c6e27Sflorian 256ae8c6e27Sflorian #endif /* SERVICES_CACHE_DNS_H */ 257