1 /* 2 * util/data/msgparse.h - parse wireformat DNS messages. 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 * \file 37 * Contains message parsing data structures. 38 * These point back into the packet buffer. 39 * 40 * During parsing RRSIGS are put together with the rrsets they (claim to) sign. 41 * This process works as follows: 42 * o if RRSIG follows the data rrset, it is added to the rrset rrsig list. 43 * o if no matching data rrset is found, the RRSIG becomes a new rrset. 44 * o If the data rrset later follows the RRSIG 45 * o See if the RRSIG rrset contains multiple types, and needs to 46 * have the rrsig(s) for that data type split off. 47 * o Put the data rr as data type in the rrset and rrsig in list. 48 * o RRSIGs are allowed to move to a different section. The section of 49 * the data item is used for the final rrset. 50 * o multiple signatures over an RRset are possible. 51 * 52 * For queries of qtype=RRSIG, some special handling is needed, to avoid 53 * splitting the RRSIG in the answer section. 54 * o duplicate, not split, RRSIGs from the answer section, if qtype=RRSIG. 55 * o check for doubles in the rrsig list when adding an RRSIG to data, 56 * so that a data rrset is signed by RRSIGs with different rdata. 57 * when qtype=RRSIG. 58 * This will move the RRSIG from the answer section to sign the data further 59 * in the packet (if possible). If then after that, more RRSIGs are found 60 * that sign the data as well, doubles are removed. 61 */ 62 63 #ifndef UTIL_DATA_MSGPARSE_H 64 #define UTIL_DATA_MSGPARSE_H 65 #include "util/storage/lruhash.h" 66 #include "sldns/pkthdr.h" 67 #include "sldns/rrdef.h" 68 struct sldns_buffer; 69 struct rrset_parse; 70 struct rr_parse; 71 struct regional; 72 struct edns_option; 73 struct config_file; 74 struct comm_point; 75 struct comm_reply; 76 struct cookie_secrets; 77 78 /** number of buckets in parse rrset hash table. Must be power of 2. */ 79 #define PARSE_TABLE_SIZE 32 80 /** Maximum TTL that is allowed. */ 81 extern time_t MAX_TTL; 82 /** Minimum TTL that is allowed. */ 83 extern time_t MIN_TTL; 84 /** Maximum Negative TTL that is allowed */ 85 extern time_t MAX_NEG_TTL; 86 /** Minimum Negative TTL that is allowed */ 87 extern time_t MIN_NEG_TTL; 88 /** If we serve expired entries and prefetch them */ 89 extern int SERVE_EXPIRED; 90 /** Time to serve records after expiration */ 91 extern time_t SERVE_EXPIRED_TTL; 92 /** Reset serve expired TTL after failed update attempt */ 93 extern time_t SERVE_EXPIRED_TTL_RESET; 94 /** TTL to use for expired records */ 95 extern time_t SERVE_EXPIRED_REPLY_TTL; 96 /** Negative cache time (for entries without any RRs.) */ 97 #define NORR_TTL 5 /* seconds */ 98 /** If we serve the original TTL or decrementing TTLs */ 99 extern int SERVE_ORIGINAL_TTL; 100 101 /** calculate the prefetch TTL as 90% of original. Calculation 102 * without numerical overflow (uin32_t) */ 103 #define PREFETCH_TTL_CALC(ttl) ((ttl) - (ttl)/10) 104 105 /* caclulate the TTL used for expired answers to somewhat make sense wrt the 106 * original TTL; don't reply with higher TTL than the original */ 107 #ifdef UNBOUND_DEBUG 108 time_t debug_expired_reply_ttl_calc(time_t ttl, time_t ttl_add); 109 #define EXPIRED_REPLY_TTL_CALC(ttl, ttl_add) \ 110 debug_expired_reply_ttl_calc(ttl, ttl_add) 111 #else 112 #define EXPIRED_REPLY_TTL_CALC(ttl, ttl_add) \ 113 (SERVE_EXPIRED_REPLY_TTL < (ttl) - (ttl_add) ? \ 114 SERVE_EXPIRED_REPLY_TTL : (ttl) - (ttl_add)) 115 #endif 116 117 /** Update the reply_info TTL from an RRSet's TTL, essentially keeping the TTL 118 * sane with all the (progressively added) rrsets to the message */ 119 #define UPDATE_TTL_FROM_RRSET(ttl, rrsetttl) \ 120 ((ttl) = ((ttl) < (rrsetttl)) ? (ttl) : (rrsetttl)) 121 122 /** Check if TTL is expired. 0 TTL is considered expired. 123 * Used mainly to identify parts of the code that do this comparison. */ 124 #define TTL_IS_EXPIRED(ttl, now) ((ttl) <= (now)) 125 126 /** 127 * Data stored in scratch pad memory during parsing. 128 * Stores the data that will enter into the msgreply and packet result. 129 */ 130 struct msg_parse { 131 /** id from message, network format. */ 132 uint16_t id; 133 /** flags from message, host format. */ 134 uint16_t flags; 135 /** count of RRs, host format */ 136 uint16_t qdcount; 137 /** count of RRs, host format */ 138 uint16_t ancount; 139 /** count of RRs, host format */ 140 uint16_t nscount; 141 /** count of RRs, host format */ 142 uint16_t arcount; 143 /** count of RRsets per section. */ 144 size_t an_rrsets; 145 /** count of RRsets per section. */ 146 size_t ns_rrsets; 147 /** count of RRsets per section. */ 148 size_t ar_rrsets; 149 /** total number of rrsets found. */ 150 size_t rrset_count; 151 152 /** query dname (pointer to start location in packet, NULL if none */ 153 uint8_t* qname; 154 /** length of query dname in octets, 0 if none */ 155 size_t qname_len; 156 /** query type, host order. 0 if qdcount=0 */ 157 uint16_t qtype; 158 /** query class, host order. 0 if qdcount=0 */ 159 uint16_t qclass; 160 161 /** 162 * Hash table array used during parsing to lookup rrset types. 163 * Based on name, type, class. Same hash value as in rrset cache. 164 */ 165 struct rrset_parse* hashtable[PARSE_TABLE_SIZE]; 166 167 /** linked list of rrsets that have been found (in order). */ 168 struct rrset_parse* rrset_first; 169 /** last element of rrset list. */ 170 struct rrset_parse* rrset_last; 171 }; 172 173 /** 174 * Data stored for an rrset during parsing. 175 */ 176 struct rrset_parse { 177 /** next in hash bucket */ 178 struct rrset_parse* rrset_bucket_next; 179 /** next in list of all rrsets */ 180 struct rrset_parse* rrset_all_next; 181 /** hash value of rrset */ 182 hashvalue_type hash; 183 /** which section was it found in: one of 184 * LDNS_SECTION_ANSWER, LDNS_SECTION_AUTHORITY, LDNS_SECTION_ADDITIONAL 185 */ 186 sldns_pkt_section section; 187 /** start of (possibly compressed) dname in packet */ 188 uint8_t* dname; 189 /** length of the dname uncompressed wireformat */ 190 size_t dname_len; 191 /** type, host order. */ 192 uint16_t type; 193 /** class, network order. var name so that it is not a c++ keyword. */ 194 uint16_t rrset_class; 195 /** the flags for the rrset, like for packedrrset */ 196 uint32_t flags; 197 /** number of RRs in the rr list */ 198 size_t rr_count; 199 /** sum of RR rdata sizes */ 200 size_t size; 201 /** linked list of RRs in this rrset. */ 202 struct rr_parse* rr_first; 203 /** last in list of RRs in this rrset. */ 204 struct rr_parse* rr_last; 205 /** number of RRSIGs over this rrset. */ 206 size_t rrsig_count; 207 /** linked list of RRsig RRs over this rrset. */ 208 struct rr_parse* rrsig_first; 209 /** last in list of RRSIG RRs over this rrset. */ 210 struct rr_parse* rrsig_last; 211 }; 212 213 /** 214 * Data stored for an RR during parsing. 215 */ 216 struct rr_parse { 217 /** 218 * Pointer to the RR. Points to start of TTL value in the packet. 219 * Rdata length and rdata follow it. 220 * its dname, type and class are the same and stored for the rrset. 221 */ 222 uint8_t* ttl_data; 223 /** true if ttl_data is not part of the packet, but elsewhere in mem. 224 * Set for generated CNAMEs for DNAMEs. */ 225 int outside_packet; 226 /** the length of the rdata if allocated (with no dname compression)*/ 227 size_t size; 228 /** next in list of RRs. */ 229 struct rr_parse* next; 230 }; 231 232 /** Check if label length is first octet of a compression pointer, pass u8. */ 233 #define LABEL_IS_PTR(x) ( ((x)&0xc0) == 0xc0 ) 234 /** Calculate destination offset of a compression pointer. pass first and 235 * second octets of the compression pointer. */ 236 #define PTR_OFFSET(x, y) ( ((x)&0x3f)<<8 | (y) ) 237 /** create a compression pointer to the given offset. */ 238 #define PTR_CREATE(offset) ((uint16_t)(0xc000 | (offset))) 239 240 /** error codes, extended with EDNS, so > 15. */ 241 #define EDNS_RCODE_BADVERS 16 /** bad EDNS version */ 242 /** largest valid compression offset */ 243 #define PTR_MAX_OFFSET 0x3fff 244 245 /** 246 * EDNS data storage 247 * rdata is parsed in a list (has accessor functions). allocated in a 248 * region. 249 */ 250 struct edns_data { 251 /** Extended RCODE */ 252 uint8_t ext_rcode; 253 /** The EDNS version number */ 254 uint8_t edns_version; 255 /** the EDNS bits field from ttl (host order): Z */ 256 uint16_t bits; 257 /** UDP reassembly size. */ 258 uint16_t udp_size; 259 /** rdata element list of options of an incoming packet created at 260 * parse time, or NULL if none */ 261 struct edns_option* opt_list_in; 262 /** rdata element list of options to encode for outgoing packets, 263 * or NULL if none */ 264 struct edns_option* opt_list_out; 265 /** rdata element list of outgoing edns options from modules 266 * or NULL if none */ 267 struct edns_option* opt_list_inplace_cb_out; 268 /** block size to pad */ 269 uint16_t padding_block_size; 270 /** if EDNS OPT record was present */ 271 unsigned int edns_present : 1; 272 /** if a cookie was present */ 273 unsigned int cookie_present : 1; 274 /** if the cookie validated */ 275 unsigned int cookie_valid : 1; 276 /** if the cookie holds only the client part */ 277 unsigned int cookie_client : 1; 278 }; 279 280 /** 281 * EDNS option 282 */ 283 struct edns_option { 284 /** next item in list */ 285 struct edns_option* next; 286 /** type of this edns option */ 287 uint16_t opt_code; 288 /** length of this edns option (cannot exceed uint16 in encoding) */ 289 size_t opt_len; 290 /** data of this edns option; allocated in region, or NULL if len=0 */ 291 uint8_t* opt_data; 292 }; 293 294 /** 295 * Obtain size in the packet of an rr type, that is before dname type. 296 * Do TYPE_DNAME, and type STR, yourself. Gives size for most regular types. 297 * @param rdf: the rdf type from the descriptor. 298 * @return: size in octets. 0 on failure. 299 */ 300 size_t get_rdf_size(sldns_rdf_type rdf); 301 302 /** 303 * Parse the packet. 304 * @param pkt: packet, position at call must be at start of packet. 305 * at end position is after packet. 306 * @param msg: where to store results. 307 * @param region: how to alloc results. 308 * @return: 0 if OK, or rcode on error. 309 */ 310 int parse_packet(struct sldns_buffer* pkt, struct msg_parse* msg, 311 struct regional* region); 312 313 /** 314 * After parsing the packet, extract EDNS data from packet. 315 * If not present this is noted in the data structure. 316 * If a parse error happens, an error code is returned. 317 * 318 * Quirks: 319 * o ignores OPT rdata. 320 * o ignores OPT owner name. 321 * o ignores extra OPT records, except the last one in the packet. 322 * 323 * @param msg: parsed message structure. Modified on exit, if EDNS was present 324 * it is removed from the additional section. 325 * @param edns: the edns data is stored here. Does not have to be initialised. 326 * @param region: region to alloc results in (edns option contents) 327 * @return: 0 on success. or an RCODE on an error. 328 * RCODE formerr if OPT in wrong section, and so on. 329 */ 330 int parse_extract_edns_from_response_msg(struct msg_parse* msg, 331 struct edns_data* edns, struct regional* region); 332 333 /** 334 * Skip RRs from packet 335 * @param pkt: the packet. position at start must be right after the query 336 * section. At end, right after EDNS data or partial movement if failed. 337 * @param num: Limit of the number of records we want to parse. 338 * @return: 1 on success, 0 on failure. 339 */ 340 int skip_pkt_rrs(struct sldns_buffer* pkt, int num); 341 342 /** 343 * If EDNS data follows a query section, extract it and initialize edns struct. 344 * @param pkt: the packet. position at start must be right after the query 345 * section. At end, right after EDNS data or partial movement if failed. 346 * @param edns: the edns data allocated by the caller. Does not have to be 347 * initialised. 348 * @param cfg: the configuration (with nsid value etc.) 349 * @param c: commpoint to determine transport (if needed) 350 * @param repinfo: commreply to determine the client address 351 * @param now: current time 352 * @param region: region to alloc results in (edns option contents) 353 * @param cookie_secrets: the cookie secrets for EDNS COOKIE validation. 354 * @return: 0 on success, or an RCODE on error. 355 * RCODE formerr if OPT is badly formatted and so on. 356 */ 357 int parse_edns_from_query_pkt(struct sldns_buffer* pkt, struct edns_data* edns, 358 struct config_file* cfg, struct comm_point* c, 359 struct comm_reply* repinfo, time_t now, struct regional* region, 360 struct cookie_secrets* cookie_secrets); 361 362 /** 363 * Calculate hash value for rrset in packet. 364 * @param pkt: the packet. 365 * @param dname: pointer to uncompressed dname, or compressed dname in packet. 366 * @param type: rrset type in host order. 367 * @param dclass: rrset class in network order. 368 * @param rrset_flags: rrset flags (same as packed_rrset flags). 369 * @return hash value 370 */ 371 hashvalue_type pkt_hash_rrset(struct sldns_buffer* pkt, uint8_t* dname, 372 uint16_t type, uint16_t dclass, uint32_t rrset_flags); 373 374 /** 375 * Lookup in msg hashtable to find a rrset. 376 * @param msg: with the hashtable. 377 * @param pkt: packet for compressed names. 378 * @param h: hash value 379 * @param rrset_flags: flags of rrset sought for. 380 * @param dname: name of rrset sought for. 381 * @param dnamelen: len of dname. 382 * @param type: rrset type, host order. 383 * @param dclass: rrset class, network order. 384 * @return NULL or the rrset_parse if found. 385 */ 386 struct rrset_parse* msgparse_hashtable_lookup(struct msg_parse* msg, 387 struct sldns_buffer* pkt, hashvalue_type h, uint32_t rrset_flags, 388 uint8_t* dname, size_t dnamelen, uint16_t type, uint16_t dclass); 389 390 /** 391 * Remove rrset from hash table. 392 * @param msg: with hashtable. 393 * @param rrset: with hash value and id info. 394 */ 395 void msgparse_bucket_remove(struct msg_parse* msg, struct rrset_parse* rrset); 396 397 /** 398 * Log the edns options in the edns option list. 399 * @param level: the verbosity level. 400 * @param info_str: the informational string to be printed before the options. 401 * @param list: the edns option list. 402 */ 403 void log_edns_opt_list(enum verbosity_value level, const char* info_str, 404 struct edns_option* list); 405 406 /** 407 * Remove RR from msgparse RRset. 408 * @param str: this string is used for logging if verbose. If NULL, there is 409 * no logging of the remove. 410 * @param pkt: packet in buffer that is removed from. Used to log the name 411 * of the item removed. 412 * @param rrset: RRset that the RR is removed from. 413 * @param prev: previous RR in list, or NULL. 414 * @param rr: RR that is removed. 415 * @param addr: address used for logging, if verbose, or NULL then it is not 416 * used. 417 * @param addrlen: length of addr, if that is not NULL. 418 * @return true if rrset is entirely bad, it would then need to be removed. 419 */ 420 int msgparse_rrset_remove_rr(const char* str, struct sldns_buffer* pkt, 421 struct rrset_parse* rrset, struct rr_parse* prev, struct rr_parse* rr, 422 struct sockaddr_storage* addr, socklen_t addrlen); 423 424 #endif /* UTIL_DATA_MSGPARSE_H */ 425