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 77 /** number of buckets in parse rrset hash table. Must be power of 2. */ 78 #define PARSE_TABLE_SIZE 32 79 /** Maximum TTL that is allowed. */ 80 extern time_t MAX_TTL; 81 /** Minimum TTL that is allowed. */ 82 extern time_t MIN_TTL; 83 /** Maximum Negative TTL that is allowed */ 84 extern time_t MAX_NEG_TTL; 85 /** If we serve expired entries and prefetch them */ 86 extern int SERVE_EXPIRED; 87 /** Time to serve records after expiration */ 88 extern time_t SERVE_EXPIRED_TTL; 89 /** TTL to use for expired records */ 90 extern time_t SERVE_EXPIRED_REPLY_TTL; 91 /** Negative cache time (for entries without any RRs.) */ 92 #define NORR_TTL 5 /* seconds */ 93 /** If we serve the original TTL or decrementing TTLs */ 94 extern int SERVE_ORIGINAL_TTL; 95 96 /** 97 * Data stored in scratch pad memory during parsing. 98 * Stores the data that will enter into the msgreply and packet result. 99 */ 100 struct msg_parse { 101 /** id from message, network format. */ 102 uint16_t id; 103 /** flags from message, host format. */ 104 uint16_t flags; 105 /** count of RRs, host format */ 106 uint16_t qdcount; 107 /** count of RRs, host format */ 108 uint16_t ancount; 109 /** count of RRs, host format */ 110 uint16_t nscount; 111 /** count of RRs, host format */ 112 uint16_t arcount; 113 /** count of RRsets per section. */ 114 size_t an_rrsets; 115 /** count of RRsets per section. */ 116 size_t ns_rrsets; 117 /** count of RRsets per section. */ 118 size_t ar_rrsets; 119 /** total number of rrsets found. */ 120 size_t rrset_count; 121 122 /** query dname (pointer to start location in packet, NULL if none */ 123 uint8_t* qname; 124 /** length of query dname in octets, 0 if none */ 125 size_t qname_len; 126 /** query type, host order. 0 if qdcount=0 */ 127 uint16_t qtype; 128 /** query class, host order. 0 if qdcount=0 */ 129 uint16_t qclass; 130 131 /** 132 * Hash table array used during parsing to lookup rrset types. 133 * Based on name, type, class. Same hash value as in rrset cache. 134 */ 135 struct rrset_parse* hashtable[PARSE_TABLE_SIZE]; 136 137 /** linked list of rrsets that have been found (in order). */ 138 struct rrset_parse* rrset_first; 139 /** last element of rrset list. */ 140 struct rrset_parse* rrset_last; 141 }; 142 143 /** 144 * Data stored for an rrset during parsing. 145 */ 146 struct rrset_parse { 147 /** next in hash bucket */ 148 struct rrset_parse* rrset_bucket_next; 149 /** next in list of all rrsets */ 150 struct rrset_parse* rrset_all_next; 151 /** hash value of rrset */ 152 hashvalue_type hash; 153 /** which section was it found in: one of 154 * LDNS_SECTION_ANSWER, LDNS_SECTION_AUTHORITY, LDNS_SECTION_ADDITIONAL 155 */ 156 sldns_pkt_section section; 157 /** start of (possibly compressed) dname in packet */ 158 uint8_t* dname; 159 /** length of the dname uncompressed wireformat */ 160 size_t dname_len; 161 /** type, host order. */ 162 uint16_t type; 163 /** class, network order. var name so that it is not a c++ keyword. */ 164 uint16_t rrset_class; 165 /** the flags for the rrset, like for packedrrset */ 166 uint32_t flags; 167 /** number of RRs in the rr list */ 168 size_t rr_count; 169 /** sum of RR rdata sizes */ 170 size_t size; 171 /** linked list of RRs in this rrset. */ 172 struct rr_parse* rr_first; 173 /** last in list of RRs in this rrset. */ 174 struct rr_parse* rr_last; 175 /** number of RRSIGs over this rrset. */ 176 size_t rrsig_count; 177 /** linked list of RRsig RRs over this rrset. */ 178 struct rr_parse* rrsig_first; 179 /** last in list of RRSIG RRs over this rrset. */ 180 struct rr_parse* rrsig_last; 181 }; 182 183 /** 184 * Data stored for an RR during parsing. 185 */ 186 struct rr_parse { 187 /** 188 * Pointer to the RR. Points to start of TTL value in the packet. 189 * Rdata length and rdata follow it. 190 * its dname, type and class are the same and stored for the rrset. 191 */ 192 uint8_t* ttl_data; 193 /** true if ttl_data is not part of the packet, but elsewhere in mem. 194 * Set for generated CNAMEs for DNAMEs. */ 195 int outside_packet; 196 /** the length of the rdata if allocated (with no dname compression)*/ 197 size_t size; 198 /** next in list of RRs. */ 199 struct rr_parse* next; 200 }; 201 202 /** Check if label length is first octet of a compression pointer, pass u8. */ 203 #define LABEL_IS_PTR(x) ( ((x)&0xc0) == 0xc0 ) 204 /** Calculate destination offset of a compression pointer. pass first and 205 * second octets of the compression pointer. */ 206 #define PTR_OFFSET(x, y) ( ((x)&0x3f)<<8 | (y) ) 207 /** create a compression pointer to the given offset. */ 208 #define PTR_CREATE(offset) ((uint16_t)(0xc000 | (offset))) 209 210 /** error codes, extended with EDNS, so > 15. */ 211 #define EDNS_RCODE_BADVERS 16 /** bad EDNS version */ 212 /** largest valid compression offset */ 213 #define PTR_MAX_OFFSET 0x3fff 214 215 /** 216 * EDNS data storage 217 * rdata is parsed in a list (has accessor functions). allocated in a 218 * region. 219 */ 220 struct edns_data { 221 /** Extended RCODE */ 222 uint8_t ext_rcode; 223 /** The EDNS version number */ 224 uint8_t edns_version; 225 /** the EDNS bits field from ttl (host order): Z */ 226 uint16_t bits; 227 /** UDP reassembly size. */ 228 uint16_t udp_size; 229 /** rdata element list of options of an incoming packet created at 230 * parse time, or NULL if none */ 231 struct edns_option* opt_list_in; 232 /** rdata element list of options to encode for outgoing packets, 233 * or NULL if none */ 234 struct edns_option* opt_list_out; 235 /** rdata element list of outgoing edns options from modules 236 * or NULL if none */ 237 struct edns_option* opt_list_inplace_cb_out; 238 /** block size to pad */ 239 uint16_t padding_block_size; 240 /** if EDNS OPT record was present */ 241 unsigned int edns_present : 1; 242 /** if a cookie was present */ 243 unsigned int cookie_present : 1; 244 /** if the cookie validated */ 245 unsigned int cookie_valid : 1; 246 /** if the cookie holds only the client part */ 247 unsigned int cookie_client : 1; 248 }; 249 250 /** 251 * EDNS option 252 */ 253 struct edns_option { 254 /** next item in list */ 255 struct edns_option* next; 256 /** type of this edns option */ 257 uint16_t opt_code; 258 /** length of this edns option (cannot exceed uint16 in encoding) */ 259 size_t opt_len; 260 /** data of this edns option; allocated in region, or NULL if len=0 */ 261 uint8_t* opt_data; 262 }; 263 264 /** 265 * Obtain size in the packet of an rr type, that is before dname type. 266 * Do TYPE_DNAME, and type STR, yourself. Gives size for most regular types. 267 * @param rdf: the rdf type from the descriptor. 268 * @return: size in octets. 0 on failure. 269 */ 270 size_t get_rdf_size(sldns_rdf_type rdf); 271 272 /** 273 * Parse the packet. 274 * @param pkt: packet, position at call must be at start of packet. 275 * at end position is after packet. 276 * @param msg: where to store results. 277 * @param region: how to alloc results. 278 * @return: 0 if OK, or rcode on error. 279 */ 280 int parse_packet(struct sldns_buffer* pkt, struct msg_parse* msg, 281 struct regional* region); 282 283 /** 284 * After parsing the packet, extract EDNS data from packet. 285 * If not present this is noted in the data structure. 286 * If a parse error happens, an error code is returned. 287 * 288 * Quirks: 289 * o ignores OPT rdata. 290 * o ignores OPT owner name. 291 * o ignores extra OPT records, except the last one in the packet. 292 * 293 * @param msg: parsed message structure. Modified on exit, if EDNS was present 294 * it is removed from the additional section. 295 * @param edns: the edns data is stored here. Does not have to be initialised. 296 * @param region: region to alloc results in (edns option contents) 297 * @return: 0 on success. or an RCODE on an error. 298 * RCODE formerr if OPT in wrong section, and so on. 299 */ 300 int parse_extract_edns_from_response_msg(struct msg_parse* msg, 301 struct edns_data* edns, struct regional* region); 302 303 /** 304 * Skip RRs from packet 305 * @param pkt: the packet. position at start must be right after the query 306 * section. At end, right after EDNS data or no movement if failed. 307 * @param num: Limit of the number of records we want to parse. 308 * @return: 0 on success, 1 on failure. 309 */ 310 int skip_pkt_rrs(struct sldns_buffer* pkt, int num); 311 312 /** 313 * If EDNS data follows a query section, extract it and initialize edns struct. 314 * @param pkt: the packet. position at start must be right after the query 315 * section. At end, right after EDNS data or no movement if failed. 316 * @param edns: the edns data allocated by the caller. Does not have to be 317 * initialised. 318 * @param cfg: the configuration (with nsid value etc.) 319 * @param c: commpoint to determine transport (if needed) 320 * @param repinfo: commreply to determine the client address 321 * @param now: current time 322 * @param region: region to alloc results in (edns option contents) 323 * @return: 0 on success, or an RCODE on error. 324 * RCODE formerr if OPT is badly formatted and so on. 325 */ 326 int parse_edns_from_query_pkt(struct sldns_buffer* pkt, struct edns_data* edns, 327 struct config_file* cfg, struct comm_point* c, 328 struct comm_reply* repinfo, time_t now, struct regional* region); 329 330 /** 331 * Calculate hash value for rrset in packet. 332 * @param pkt: the packet. 333 * @param dname: pointer to uncompressed dname, or compressed dname in packet. 334 * @param type: rrset type in host order. 335 * @param dclass: rrset class in network order. 336 * @param rrset_flags: rrset flags (same as packed_rrset flags). 337 * @return hash value 338 */ 339 hashvalue_type pkt_hash_rrset(struct sldns_buffer* pkt, uint8_t* dname, 340 uint16_t type, uint16_t dclass, uint32_t rrset_flags); 341 342 /** 343 * Lookup in msg hashtable to find a rrset. 344 * @param msg: with the hashtable. 345 * @param pkt: packet for compressed names. 346 * @param h: hash value 347 * @param rrset_flags: flags of rrset sought for. 348 * @param dname: name of rrset sought for. 349 * @param dnamelen: len of dname. 350 * @param type: rrset type, host order. 351 * @param dclass: rrset class, network order. 352 * @return NULL or the rrset_parse if found. 353 */ 354 struct rrset_parse* msgparse_hashtable_lookup(struct msg_parse* msg, 355 struct sldns_buffer* pkt, hashvalue_type h, uint32_t rrset_flags, 356 uint8_t* dname, size_t dnamelen, uint16_t type, uint16_t dclass); 357 358 /** 359 * Remove rrset from hash table. 360 * @param msg: with hashtable. 361 * @param rrset: with hash value and id info. 362 */ 363 void msgparse_bucket_remove(struct msg_parse* msg, struct rrset_parse* rrset); 364 365 /** 366 * Log the edns options in the edns option list. 367 * @param level: the verbosity level. 368 * @param info_str: the informational string to be printed before the options. 369 * @param list: the edns option list. 370 */ 371 void log_edns_opt_list(enum verbosity_value level, const char* info_str, 372 struct edns_option* list); 373 374 #endif /* UTIL_DATA_MSGPARSE_H */ 375