1 /* 2 * util/data/msgreply.h - store message and reply data. 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 a data structure to store a message and its reply. 40 */ 41 42 #ifndef UTIL_DATA_MSGREPLY_H 43 #define UTIL_DATA_MSGREPLY_H 44 #include "util/storage/lruhash.h" 45 #include "util/data/packed_rrset.h" 46 struct sldns_buffer; 47 struct comm_reply; 48 struct alloc_cache; 49 struct iovec; 50 struct regional; 51 struct edns_data; 52 struct msg_parse; 53 struct rrset_parse; 54 55 /** calculate the prefetch TTL as 90% of original. Calculation 56 * without numerical overflow (uin32_t) */ 57 #define PREFETCH_TTL_CALC(ttl) ((ttl) - (ttl)/10) 58 59 /** 60 * Structure to store query information that makes answers to queries 61 * different. 62 */ 63 struct query_info { 64 /** 65 * Salient data on the query: qname, in wireformat. 66 * can be allocated or a pointer to outside buffer. 67 * User has to keep track on the status of this. 68 */ 69 uint8_t* qname; 70 /** length of qname (including last 0 octet) */ 71 size_t qname_len; 72 /** qtype, host byte order */ 73 uint16_t qtype; 74 /** qclass, host byte order */ 75 uint16_t qclass; 76 }; 77 78 /** 79 * Information to reference an rrset 80 */ 81 struct rrset_ref { 82 /** the key with lock, and ptr to packed data. */ 83 struct ub_packed_rrset_key* key; 84 /** id needed */ 85 rrset_id_t id; 86 }; 87 88 /** 89 * Structure to store DNS query and the reply packet. 90 * To use it, copy over the flags from reply and modify using flags from 91 * the query (RD,CD if not AA). prepend ID. 92 * 93 * Memory layout is: 94 * o struct 95 * o rrset_ref array 96 * o packed_rrset_key* array. 97 * 98 * Memory layout is sometimes not packed, when the message is synthesized, 99 * for easy of the generation. It is allocated packed when it is copied 100 * from the region allocation to the malloc allocation. 101 */ 102 struct reply_info { 103 /** the flags for the answer, host byte order. */ 104 uint16_t flags; 105 106 /** 107 * This flag informs unbound the answer is authoritative and 108 * the AA flag should be preserved. 109 */ 110 uint8_t authoritative; 111 112 /** 113 * Number of RRs in the query section. 114 * If qdcount is not 0, then it is 1, and the data that appears 115 * in the reply is the same as the query_info. 116 * Host byte order. 117 */ 118 uint8_t qdcount; 119 120 /** 32 bit padding to pad struct member alignment to 64 bits. */ 121 uint32_t padding; 122 123 /** 124 * TTL of the entire reply (for negative caching). 125 * only for use when there are 0 RRsets in this message. 126 * if there are RRsets, check those instead. 127 */ 128 time_t ttl; 129 130 /** 131 * TTL for prefetch. After it has expired, a prefetch is suitable. 132 * Smaller than the TTL, otherwise the prefetch would not happen. 133 */ 134 time_t prefetch_ttl; 135 136 /** 137 * The security status from DNSSEC validation of this message. 138 */ 139 enum sec_status security; 140 141 /** 142 * Number of RRsets in each section. 143 * The answer section. Add up the RRs in every RRset to calculate 144 * the number of RRs, and the count for the dns packet. 145 * The number of RRs in RRsets can change due to RRset updates. 146 */ 147 size_t an_numrrsets; 148 149 /** Count of authority section RRsets */ 150 size_t ns_numrrsets; 151 /** Count of additional section RRsets */ 152 size_t ar_numrrsets; 153 154 /** number of RRsets: an_numrrsets + ns_numrrsets + ar_numrrsets */ 155 size_t rrset_count; 156 157 /** 158 * List of pointers (only) to the rrsets in the order in which 159 * they appear in the reply message. 160 * Number of elements is ancount+nscount+arcount RRsets. 161 * This is a pointer to that array. 162 * Use the accessor function for access. 163 */ 164 struct ub_packed_rrset_key** rrsets; 165 166 /** 167 * Packed array of ids (see counts) and pointers to packed_rrset_key. 168 * The number equals ancount+nscount+arcount RRsets. 169 * These are sorted in ascending pointer, the locking order. So 170 * this list can be locked (and id, ttl checked), to see if 171 * all the data is available and recent enough. 172 * 173 * This is defined as an array of size 1, so that the compiler 174 * associates the identifier with this position in the structure. 175 * Array bound overflow on this array then gives access to the further 176 * elements of the array, which are allocated after the main structure. 177 * 178 * It could be more pure to define as array of size 0, ref[0]. 179 * But ref[1] may be less confusing for compilers. 180 * Use the accessor function for access. 181 */ 182 struct rrset_ref ref[1]; 183 }; 184 185 /** 186 * Structure to keep hash table entry for message replies. 187 */ 188 struct msgreply_entry { 189 /** the hash table key */ 190 struct query_info key; 191 /** the hash table entry, data is struct reply_info* */ 192 struct lruhash_entry entry; 193 }; 194 195 /** 196 * Constructor for replyinfo. 197 * @param region: where to allocate the results, pass NULL to use malloc. 198 * @param flags: flags for the replyinfo. 199 * @param qd: qd count 200 * @param ttl: TTL of replyinfo 201 * @param prettl: prefetch ttl 202 * @param an: an count 203 * @param ns: ns count 204 * @param ar: ar count 205 * @param total: total rrset count (presumably an+ns+ar). 206 * @param sec: security status of the reply info. 207 * @return the reply_info base struct with the array for putting the rrsets 208 * in. The array has been zeroed. Returns NULL on malloc failure. 209 */ 210 struct reply_info* 211 construct_reply_info_base(struct regional* region, uint16_t flags, size_t qd, 212 time_t ttl, time_t prettl, size_t an, size_t ns, size_t ar, 213 size_t total, enum sec_status sec); 214 215 /** 216 * Parse wire query into a queryinfo structure, return 0 on parse error. 217 * initialises the (prealloced) queryinfo structure as well. 218 * This query structure contains a pointer back info the buffer! 219 * This pointer avoids memory allocation. allocqname does memory allocation. 220 * @param m: the prealloced queryinfo structure to put query into. 221 * must be unused, or _clear()ed. 222 * @param query: the wireformat packet query. starts with ID. 223 * @return: 0 on format error. 224 */ 225 int query_info_parse(struct query_info* m, struct sldns_buffer* query); 226 227 /** 228 * Parse query reply. 229 * Fills in preallocated query_info structure (with ptr into buffer). 230 * Allocates reply_info and packed_rrsets. These are not yet added to any 231 * caches or anything, this is only parsing. Returns formerror on qdcount > 1. 232 * @param pkt: the packet buffer. Must be positioned after the query section. 233 * @param alloc: creates packed rrset key structures. 234 * @param rep: allocated reply_info is returned (only on no error). 235 * @param qinf: query_info is returned (only on no error). 236 * @param region: where to store temporary data (for parsing). 237 * @param edns: where to store edns information, does not need to be inited. 238 * @return: zero is OK, or DNS error code in case of error 239 * o FORMERR for parse errors. 240 * o SERVFAIL for memory allocation errors. 241 */ 242 int reply_info_parse(struct sldns_buffer* pkt, struct alloc_cache* alloc, 243 struct query_info* qinf, struct reply_info** rep, 244 struct regional* region, struct edns_data* edns); 245 246 /** 247 * Allocate and decompress parsed message and rrsets. 248 * @param pkt: for name decompression. 249 * @param msg: parsed message in scratch region. 250 * @param alloc: alloc cache for special rrset key structures. 251 * Not used if region!=NULL, it can be NULL in that case. 252 * @param qinf: where to store query info. 253 * qinf itself is allocated by the caller. 254 * @param rep: reply info is allocated and returned. 255 * @param region: if this parameter is NULL then malloc and the alloc is used. 256 * otherwise, everything is allocated in this region. 257 * In a region, no special rrset key structures are needed (not shared), 258 * and no rrset_ref array in the reply is built up. 259 * @return 0 if allocation failed. 260 */ 261 int parse_create_msg(struct sldns_buffer* pkt, struct msg_parse* msg, 262 struct alloc_cache* alloc, struct query_info* qinf, 263 struct reply_info** rep, struct regional* region); 264 265 /** 266 * Sorts the ref array. 267 * @param rep: reply info. rrsets must be filled in. 268 */ 269 void reply_info_sortref(struct reply_info* rep); 270 271 /** 272 * Set TTLs inside the replyinfo to absolute values. 273 * @param rep: reply info. rrsets must be filled in. 274 * Also refs must be filled in. 275 * @param timenow: the current time. 276 */ 277 void reply_info_set_ttls(struct reply_info* rep, time_t timenow); 278 279 /** 280 * Delete reply_info and packed_rrsets (while they are not yet added to the 281 * hashtables.). Returns rrsets to the alloc cache. 282 * @param rep: reply_info to delete. 283 * @param alloc: where to return rrset structures to. 284 */ 285 void reply_info_parsedelete(struct reply_info* rep, struct alloc_cache* alloc); 286 287 /** 288 * Compare two queryinfo structures, on query and type, class. 289 * It is _not_ sorted in canonical ordering. 290 * @param m1: struct query_info* , void* here to ease use as function pointer. 291 * @param m2: struct query_info* , void* here to ease use as function pointer. 292 * @return: 0 = same, -1 m1 is smaller, +1 m1 is larger. 293 */ 294 int query_info_compare(void* m1, void* m2); 295 296 /** clear out query info structure */ 297 void query_info_clear(struct query_info* m); 298 299 /** calculate size of struct query_info + reply_info */ 300 size_t msgreply_sizefunc(void* k, void* d); 301 302 /** delete msgreply_entry key structure */ 303 void query_entry_delete(void *q, void* arg); 304 305 /** delete reply_info data structure */ 306 void reply_info_delete(void* d, void* arg); 307 308 /** calculate hash value of query_info, lowercases the qname, 309 * uses CD flag for AAAA qtype */ 310 hashvalue_t query_info_hash(struct query_info *q, uint16_t flags); 311 312 /** 313 * Setup query info entry 314 * @param q: query info to copy. Emptied as if clear is called. 315 * @param r: reply to init data. 316 * @param h: hash value. 317 * @return: newly allocated message reply cache item. 318 */ 319 struct msgreply_entry* query_info_entrysetup(struct query_info* q, 320 struct reply_info* r, hashvalue_t h); 321 322 /** 323 * Copy reply_info and all rrsets in it and allocate. 324 * @param rep: what to copy, probably inside region, no ref[] array in it. 325 * @param alloc: how to allocate rrset keys. 326 * Not used if region!=NULL, it can be NULL in that case. 327 * @param region: if this parameter is NULL then malloc and the alloc is used. 328 * otherwise, everything is allocated in this region. 329 * In a region, no special rrset key structures are needed (not shared), 330 * and no rrset_ref array in the reply is built up. 331 * @return new reply info or NULL on memory error. 332 */ 333 struct reply_info* reply_info_copy(struct reply_info* rep, 334 struct alloc_cache* alloc, struct regional* region); 335 336 /** 337 * Copy a parsed rrset into given key, decompressing and allocating rdata. 338 * @param pkt: packet for decompression 339 * @param msg: the parser message (for flags for trust). 340 * @param pset: the parsed rrset to copy. 341 * @param region: if NULL - malloc, else data is allocated in this region. 342 * @param pk: a freshly obtained rrsetkey structure. No dname is set yet, 343 * will be set on return. 344 * Note that TTL will still be relative on return. 345 * @return false on alloc failure. 346 */ 347 int parse_copy_decompress_rrset(struct sldns_buffer* pkt, struct msg_parse* msg, 348 struct rrset_parse *pset, struct regional* region, 349 struct ub_packed_rrset_key* pk); 350 351 /** 352 * Find final cname target in reply, the one matching qinfo. Follows CNAMEs. 353 * @param qinfo: what to start with. 354 * @param rep: looks in answer section of this message. 355 * @return: pointer dname, or NULL if not found. 356 */ 357 uint8_t* reply_find_final_cname_target(struct query_info* qinfo, 358 struct reply_info* rep); 359 360 /** 361 * Check if cname chain in cached reply is still valid. 362 * @param qinfo: query info with query name. 363 * @param rep: reply to check. 364 * @return: true if valid, false if invalid. 365 */ 366 int reply_check_cname_chain(struct query_info* qinfo, struct reply_info* rep); 367 368 /** 369 * Check security status of all RRs in the message. 370 * @param rep: reply to check 371 * @return: true if all RRs are secure. False if not. 372 * True if there are zero RRs. 373 */ 374 int reply_all_rrsets_secure(struct reply_info* rep); 375 376 /** 377 * Find answer rrset in reply, the one matching qinfo. Follows CNAMEs, so the 378 * result may have a different owner name. 379 * @param qinfo: what to look for. 380 * @param rep: looks in answer section of this message. 381 * @return: pointer to rrset, or NULL if not found. 382 */ 383 struct ub_packed_rrset_key* reply_find_answer_rrset(struct query_info* qinfo, 384 struct reply_info* rep); 385 386 /** 387 * Find rrset in reply, inside the answer section. Does not follow CNAMEs. 388 * @param rep: looks in answer section of this message. 389 * @param name: what to look for. 390 * @param namelen: length of name. 391 * @param type: looks for (host order). 392 * @param dclass: looks for (host order). 393 * @return: pointer to rrset, or NULL if not found. 394 */ 395 struct ub_packed_rrset_key* reply_find_rrset_section_an(struct reply_info* rep, 396 uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass); 397 398 /** 399 * Find rrset in reply, inside the authority section. Does not follow CNAMEs. 400 * @param rep: looks in authority section of this message. 401 * @param name: what to look for. 402 * @param namelen: length of name. 403 * @param type: looks for (host order). 404 * @param dclass: looks for (host order). 405 * @return: pointer to rrset, or NULL if not found. 406 */ 407 struct ub_packed_rrset_key* reply_find_rrset_section_ns(struct reply_info* rep, 408 uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass); 409 410 /** 411 * Find rrset in reply, inside any section. Does not follow CNAMEs. 412 * @param rep: looks in answer,authority and additional section of this message. 413 * @param name: what to look for. 414 * @param namelen: length of name. 415 * @param type: looks for (host order). 416 * @param dclass: looks for (host order). 417 * @return: pointer to rrset, or NULL if not found. 418 */ 419 struct ub_packed_rrset_key* reply_find_rrset(struct reply_info* rep, 420 uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass); 421 422 /** 423 * Debug send the query info and reply info to the log in readable form. 424 * @param str: descriptive string printed with packet content. 425 * @param qinfo: query section. 426 * @param rep: rest of message. 427 */ 428 void log_dns_msg(const char* str, struct query_info* qinfo, 429 struct reply_info* rep); 430 431 /** 432 * Print string with neat domain name, type, class from query info. 433 * @param v: at what verbosity level to print this. 434 * @param str: string of message. 435 * @param qinf: query info structure with name, type and class. 436 */ 437 void log_query_info(enum verbosity_value v, const char* str, 438 struct query_info* qinf); 439 440 #endif /* UTIL_DATA_MSGREPLY_H */ 441