1 /* 2 * services/cache/dns.h - Cache services for DNS using msg and rrset caches. 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 DNS cache. 40 */ 41 42 #ifndef SERVICES_CACHE_DNS_H 43 #define SERVICES_CACHE_DNS_H 44 #include "util/storage/lruhash.h" 45 #include "util/data/msgreply.h" 46 struct module_env; 47 struct query_info; 48 struct reply_info; 49 struct regional; 50 struct delegpt; 51 52 /** Flags to control behavior of dns_cache_store() and dns_cache_store_msg(). 53 * Must be an unsigned 32-bit value larger than 0xffff */ 54 55 /** Allow caching a DNS message with a zero TTL. */ 56 #define DNSCACHE_STORE_ZEROTTL 0x100000 57 58 /** 59 * Region allocated message reply 60 */ 61 struct dns_msg { 62 /** query info */ 63 struct query_info qinfo; 64 /** reply info - ptr to packed repinfo structure */ 65 struct reply_info *rep; 66 }; 67 68 /** 69 * Allocate a dns_msg with malloc/alloc structure and store in dns cache. 70 * 71 * @param env: environment, with alloc structure and dns cache. 72 * @param qinf: query info, the query for which answer is stored. 73 * this is allocated in a region, and will be copied to malloc area 74 * before insertion. 75 * @param rep: reply in dns_msg from dns_alloc_msg for example. 76 * this is allocated in a region, and will be copied to malloc area 77 * before insertion. 78 * @param is_referral: If true, then the given message to be stored is a 79 * referral. The cache implementation may use this as a hint. 80 * It will store only the RRsets, not the message. 81 * @param leeway: TTL value, if not 0, other rrsets are considered expired 82 * that many seconds before actual TTL expiry. 83 * @param pside: if true, information came from a server which was fetched 84 * from the parentside of the zonecut. This means that the type NS 85 * can be updated to full TTL even in prefetch situations. 86 * @param region: region to allocate better entries from cache into. 87 * (used when is_referral is false). 88 * @param flags: flags with BIT_CD for AAAA queries in dns64 translation. 89 * The higher 16 bits are used internally to customize the cache policy. 90 * (See DNSCACHE_STORE_xxx flags). 91 * @param qstarttime: time when the query was started, and thus when the 92 * delegations were looked up. 93 * @param is_valrec: if the query is validation recursion and does not get 94 * dnssec validation itself. 95 * @return 0 on alloc error (out of memory). 96 */ 97 int dns_cache_store(struct module_env* env, struct query_info* qinf, 98 struct reply_info* rep, int is_referral, time_t leeway, int pside, 99 struct regional* region, uint32_t flags, time_t qstarttime, 100 int is_valrec); 101 102 /** 103 * Store message in the cache. Stores in message cache and rrset cache. 104 * Both qinfo and rep should be malloced and are put in the cache. 105 * They should not be used after this call, as they are then in shared cache. 106 * Does not return errors, they are logged and only lead to less cache. 107 * 108 * @param env: module environment with the DNS cache. 109 * @param qinfo: query info 110 * @param hash: hash over qinfo. 111 * @param rep: reply info, together with qinfo makes up the message. 112 * Adjusts the reply info TTLs to absolute time. 113 * @param leeway: TTL value, if not 0, other rrsets are considered expired 114 * that many seconds before actual TTL expiry. 115 * @param pside: if true, information came from a server which was fetched 116 * from the parentside of the zonecut. This means that the type NS 117 * can be updated to full TTL even in prefetch situations. 118 * @param qrep: message that can be altered with better rrs from cache. 119 * @param flags: customization flags for the cache policy. 120 * @param qstarttime: time when the query was started, and thus when the 121 * delegations were looked up. 122 * @param region: to allocate into for qmsg. 123 */ 124 void dns_cache_store_msg(struct module_env* env, struct query_info* qinfo, 125 hashvalue_type hash, struct reply_info* rep, time_t leeway, int pside, 126 struct reply_info* qrep, uint32_t flags, struct regional* region, 127 time_t qstarttime); 128 129 /** 130 * Find a delegation from the cache. 131 * @param env: module environment with the DNS cache. 132 * @param qname: query name. 133 * @param qnamelen: length of qname. 134 * @param qtype: query type. 135 * @param qclass: query class. 136 * @param region: where to allocate result delegation. 137 * @param msg: if not NULL, delegation message is returned here, synthesized 138 * from the cache. 139 * @param timenow: the time now, for checking if TTL on cache entries is OK. 140 * @param noexpiredabove: if set, no expired NS rrsets above the one found 141 * are tolerated. It only returns delegations where the delegations above 142 * it are valid. 143 * @param expiretop: if not NULL, name where check for expiry ends for 144 * noexpiredabove. 145 * @param expiretoplen: length of expiretop dname. 146 * @return new delegation or NULL on error or if not found in cache. 147 */ 148 struct delegpt* dns_cache_find_delegation(struct module_env* env, 149 uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass, 150 struct regional* region, struct dns_msg** msg, time_t timenow, 151 int noexpiredabove, uint8_t* expiretop, size_t expiretoplen); 152 153 /** 154 * generate dns_msg from cached message 155 * @param env: module environment with the DNS cache. NULL if the LRU from cache 156 * does not need to be touched. 157 * @param q: query info, contains qname that will make up the dns message. 158 * @param r: reply info that, together with qname, will make up the dns message. 159 * @param region: where to allocate dns message. 160 * @param now: the time now, for check if TTL on cache entry is ok. 161 * @param allow_expired: if true and serve-expired is enabled, it will allow 162 * for expired dns_msg to be generated based on the configured serve-expired 163 * logic. 164 * @param scratch: where to allocate temporary data. 165 * */ 166 struct dns_msg* tomsg(struct module_env* env, struct query_info* q, 167 struct reply_info* r, struct regional* region, time_t now, 168 int allow_expired, struct regional* scratch); 169 170 /** 171 * Deep copy a dns_msg to a region. 172 * @param origin: the dns_msg to copy. 173 * @param region: the region to copy all the data to. 174 * @return the new dns_msg or NULL on malloc error. 175 */ 176 struct dns_msg* dns_msg_deepcopy_region(struct dns_msg* origin, 177 struct regional* region); 178 179 /** 180 * Find cached message 181 * @param env: module environment with the DNS cache. 182 * @param qname: query name. 183 * @param qnamelen: length of qname. 184 * @param qtype: query type. 185 * @param qclass: query class. 186 * @param flags: flags with BIT_CD for AAAA queries in dns64 translation. 187 * @param region: where to allocate result. 188 * @param scratch: where to allocate temporary data. 189 * @param no_partial: if true, only complete messages and not a partial 190 * one (with only the start of the CNAME chain and not the rest). 191 * @param dpname: if not NULL, do not return NXDOMAIN above this name. 192 * @param dpnamelen: length of dpname. 193 * @return new response message (alloced in region, rrsets do not have IDs). 194 * or NULL on error or if not found in cache. 195 * TTLs are made relative to the current time. 196 */ 197 struct dns_msg* dns_cache_lookup(struct module_env* env, 198 uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass, 199 uint16_t flags, struct regional* region, struct regional* scratch, 200 int no_partial, uint8_t* dpname, size_t dpnamelen); 201 202 /** 203 * find and add A and AAAA records for missing nameservers in delegpt 204 * @param env: module environment with rrset cache 205 * @param qclass: which class to look in. 206 * @param region: where to store new dp info. 207 * @param dp: delegation point to fill missing entries. 208 * @param flags: rrset flags, or 0. 209 * @return false on alloc failure. 210 */ 211 int cache_fill_missing(struct module_env* env, uint16_t qclass, 212 struct regional* region, struct delegpt* dp, uint32_t flags); 213 214 /** 215 * Utility, create new, unpacked data structure for cache response. 216 * QR bit set, no AA. Query set as indicated. Space for number of rrsets. 217 * @param qname: query section name 218 * @param qnamelen: len of qname 219 * @param qtype: query section type 220 * @param qclass: query section class 221 * @param region: where to alloc. 222 * @param capacity: number of rrsets space to create in the array. 223 * @return new dns_msg struct or NULL on mem fail. 224 */ 225 struct dns_msg* dns_msg_create(uint8_t* qname, size_t qnamelen, uint16_t qtype, 226 uint16_t qclass, struct regional* region, size_t capacity); 227 228 /** 229 * Add rrset to authority section in unpacked dns_msg message. Must have enough 230 * space left, does not grow the array. 231 * @param msg: msg to put it in. 232 * @param region: region to alloc in 233 * @param rrset: to add in authority section 234 * @param now: now. 235 * @return true if worked, false on fail 236 */ 237 int dns_msg_authadd(struct dns_msg* msg, struct regional* region, 238 struct ub_packed_rrset_key* rrset, time_t now); 239 240 /** 241 * Add rrset to authority section in unpacked dns_msg message. Must have enough 242 * space left, does not grow the array. 243 * @param msg: msg to put it in. 244 * @param region: region to alloc in 245 * @param rrset: to add in authority section 246 * @param now: now. 247 * @return true if worked, false on fail 248 */ 249 int dns_msg_ansadd(struct dns_msg* msg, struct regional* region, 250 struct ub_packed_rrset_key* rrset, time_t now); 251 252 /** 253 * Adjust the prefetch_ttl for a cached message. This adds a value to the 254 * prefetch ttl - postponing the time when it will be prefetched for future 255 * incoming queries. 256 * @param env: module environment with caches and time. 257 * @param qinfo: query info for the query that needs adjustment. 258 * @param adjust: time in seconds to add to the prefetch_leeway. 259 * @param flags: flags with BIT_CD for AAAA queries in dns64 translation. 260 * @return false if not in cache. true if added. 261 */ 262 int dns_cache_prefetch_adjust(struct module_env* env, struct query_info* qinfo, 263 time_t adjust, uint16_t flags); 264 265 /** lookup message in message cache 266 * the returned nonNULL entry is locked and has to be unlocked by the caller */ 267 struct msgreply_entry* msg_cache_lookup(struct module_env* env, 268 uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass, 269 uint16_t flags, time_t now, int wr); 270 271 /** 272 * Remove entry from the message cache. For unwanted entries. 273 * @param env: with message cache. 274 * @param qname: query name, in wireformat 275 * @param qnamelen: length of qname, including terminating 0. 276 * @param qtype: query type, host order. 277 * @param qclass: query class, host order. 278 * @param flags: flags 279 */ 280 void msg_cache_remove(struct module_env* env, uint8_t* qname, size_t qnamelen, 281 uint16_t qtype, uint16_t qclass, uint16_t flags); 282 283 #endif /* SERVICES_CACHE_DNS_H */ 284