2 * util/data/msgparse.h - parse wireformat DNS messages.
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
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.
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.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
37 * Contains message parsing data structures.
38 * These point back into the packet buffer.
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.
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.
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.
63 #ifndef UTIL_DATA_MSGPARSE_H
64 #define UTIL_DATA_MSGPARSE_H
65 #include "util/storage/lruhash.h"
66 #include <ldns/packet.h>
71 /** number of buckets in parse rrset hash table. Must be power of 2. */
72 #define PARSE_TABLE_SIZE 32
73 /** Maximum TTL that is allowed. */
74 extern uint32_t MAX_TTL;
75 /** Minimum TTL that is allowed. */
76 extern uint32_t MIN_TTL;
77 /** Negative cache time (for entries without any RRs.) */
78 #define NORR_TTL 5 /* seconds */
81 * Data stored in scratch pad memory during parsing.
82 * Stores the data that will enter into the msgreply and packet result.
85 /** id from message, network format. */
87 /** flags from message, host format. */
89 /** count of RRs, host format */
91 /** count of RRs, host format */
93 /** count of RRs, host format */
95 /** count of RRs, host format */
97 /** count of RRsets per section. */
99 /** count of RRsets per section. */
101 /** count of RRsets per section. */
103 /** total number of rrsets found. */
106 /** query dname (pointer to start location in packet, NULL if none */
108 /** length of query dname in octets, 0 if none */
110 /** query type, host order. 0 if qdcount=0 */
112 /** query class, host order. 0 if qdcount=0 */
116 * Hash table array used during parsing to lookup rrset types.
117 * Based on name, type, class. Same hash value as in rrset cache.
119 struct rrset_parse* hashtable[PARSE_TABLE_SIZE];
121 /** linked list of rrsets that have been found (in order). */
122 struct rrset_parse* rrset_first;
123 /** last element of rrset list. */
124 struct rrset_parse* rrset_last;
128 * Data stored for an rrset during parsing.
131 /** next in hash bucket */
132 struct rrset_parse* rrset_bucket_next;
133 /** next in list of all rrsets */
134 struct rrset_parse* rrset_all_next;
135 /** hash value of rrset */
137 /** which section was it found in: one of
138 * LDNS_SECTION_ANSWER, LDNS_SECTION_AUTHORITY, LDNS_SECTION_ADDITIONAL
140 ldns_pkt_section section;
141 /** start of (possibly compressed) dname in packet */
143 /** length of the dname uncompressed wireformat */
145 /** type, host order. */
147 /** class, network order. var name so that it is not a c++ keyword. */
148 uint16_t rrset_class;
149 /** the flags for the rrset, like for packedrrset */
151 /** number of RRs in the rr list */
153 /** sum of RR rdata sizes */
155 /** linked list of RRs in this rrset. */
156 struct rr_parse* rr_first;
157 /** last in list of RRs in this rrset. */
158 struct rr_parse* rr_last;
159 /** number of RRSIGs over this rrset. */
161 /** linked list of RRsig RRs over this rrset. */
162 struct rr_parse* rrsig_first;
163 /** last in list of RRSIG RRs over this rrset. */
164 struct rr_parse* rrsig_last;
168 * Data stored for an RR during parsing.
172 * Pointer to the RR. Points to start of TTL value in the packet.
173 * Rdata length and rdata follow it.
174 * its dname, type and class are the same and stored for the rrset.
177 /** true if ttl_data is not part of the packet, but elsewhere in mem.
178 * Set for generated CNAMEs for DNAMEs. */
180 /** the length of the rdata if allocated (with no dname compression)*/
182 /** next in list of RRs. */
183 struct rr_parse* next;
186 /** Check if label length is first octet of a compression pointer, pass u8. */
187 #define LABEL_IS_PTR(x) ( ((x)&0xc0) == 0xc0 )
188 /** Calculate destination offset of a compression pointer. pass first and
189 * second octets of the compression pointer. */
190 #define PTR_OFFSET(x, y) ( ((x)&0x3f)<<8 | (y) )
191 /** create a compression pointer to the given offset. */
192 #define PTR_CREATE(offset) ((uint16_t)(0xc000 | (offset)))
194 /** error codes, extended with EDNS, so > 15. */
195 #define EDNS_RCODE_BADVERS 16 /** bad EDNS version */
196 /** largest valid compression offset */
197 #define PTR_MAX_OFFSET 0x3fff
201 * EDNS rdata is ignored.
204 /** if EDNS OPT record was present */
206 /** Extended RCODE */
208 /** The EDNS version number */
209 uint8_t edns_version;
210 /** the EDNS bits field from ttl (host order): Z */
212 /** UDP reassembly size. */
217 * Obtain size in the packet of an rr type, that is before dname type.
218 * Do TYPE_DNAME, and type STR, yourself. Gives size for most regular types.
219 * @param rdf: the rdf type from the descriptor.
220 * @return: size in octets. 0 on failure.
222 size_t get_rdf_size(ldns_rdf_type rdf);
226 * @param pkt: packet, position at call must be at start of packet.
227 * at end position is after packet.
228 * @param msg: where to store results.
229 * @param region: how to alloc results.
230 * @return: 0 if OK, or rcode on error.
232 int parse_packet(ldns_buffer* pkt, struct msg_parse* msg,
233 struct regional* region);
236 * After parsing the packet, extract EDNS data from packet.
237 * If not present this is noted in the data structure.
238 * If a parse error happens, an error code is returned.
241 * o ignores OPT rdata.
242 * o ignores OPT owner name.
243 * o ignores extra OPT records, except the last one in the packet.
245 * @param msg: parsed message structure. Modified on exit, if EDNS was present
246 * it is removed from the additional section.
247 * @param edns: the edns data is stored here. Does not have to be initialised.
248 * @return: 0 on success. or an RCODE on an error.
249 * RCODE formerr if OPT in wrong section, and so on.
251 int parse_extract_edns(struct msg_parse* msg, struct edns_data* edns);
254 * If EDNS data follows a query section, extract it and initialize edns struct.
255 * @param pkt: the packet. position at start must be right after the query
256 * section. At end, right after EDNS data or no movement if failed.
257 * @param edns: the edns data allocated by the caller. Does not have to be
259 * @return: 0 on success, or an RCODE on error.
260 * RCODE formerr if OPT is badly formatted and so on.
262 int parse_edns_from_pkt(ldns_buffer* pkt, struct edns_data* edns);
265 * Calculate hash value for rrset in packet.
266 * @param pkt: the packet.
267 * @param dname: pointer to uncompressed dname, or compressed dname in packet.
268 * @param type: rrset type in host order.
269 * @param dclass: rrset class in network order.
270 * @param rrset_flags: rrset flags (same as packed_rrset flags).
273 hashvalue_t pkt_hash_rrset(ldns_buffer* pkt, uint8_t* dname, uint16_t type,
274 uint16_t dclass, uint32_t rrset_flags);
277 * Lookup in msg hashtable to find a rrset.
278 * @param msg: with the hashtable.
279 * @param pkt: packet for compressed names.
280 * @param h: hash value
281 * @param rrset_flags: flags of rrset sought for.
282 * @param dname: name of rrset sought for.
283 * @param dnamelen: len of dname.
284 * @param type: rrset type, host order.
285 * @param dclass: rrset class, network order.
286 * @return NULL or the rrset_parse if found.
288 struct rrset_parse* msgparse_hashtable_lookup(struct msg_parse* msg,
289 ldns_buffer* pkt, hashvalue_t h, uint32_t rrset_flags,
290 uint8_t* dname, size_t dnamelen, uint16_t type, uint16_t dclass);
293 * Remove rrset from hash table.
294 * @param msg: with hashtable.
295 * @param rrset: with hash value and id info.
297 void msgparse_bucket_remove(struct msg_parse* msg, struct rrset_parse* rrset);
299 #endif /* UTIL_DATA_MSGPARSE_H */