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