msgparse.h revision 1.1.1.3 1 1.1 christos /*
2 1.1 christos * util/data/msgparse.h - parse wireformat DNS messages.
3 1.1 christos *
4 1.1 christos * Copyright (c) 2007, NLnet Labs. All rights reserved.
5 1.1 christos *
6 1.1 christos * This software is open source.
7 1.1 christos *
8 1.1 christos * Redistribution and use in source and binary forms, with or without
9 1.1 christos * modification, are permitted provided that the following conditions
10 1.1 christos * are met:
11 1.1 christos *
12 1.1 christos * Redistributions of source code must retain the above copyright notice,
13 1.1 christos * this list of conditions and the following disclaimer.
14 1.1 christos *
15 1.1 christos * Redistributions in binary form must reproduce the above copyright notice,
16 1.1 christos * this list of conditions and the following disclaimer in the documentation
17 1.1 christos * and/or other materials provided with the distribution.
18 1.1 christos *
19 1.1 christos * Neither the name of the NLNET LABS nor the names of its contributors may
20 1.1 christos * be used to endorse or promote products derived from this software without
21 1.1 christos * specific prior written permission.
22 1.1 christos *
23 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 1.1 christos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 1.1 christos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 1.1 christos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 1.1 christos * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 1.1 christos * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 1.1 christos * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 1.1 christos * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 1.1 christos * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 1.1 christos * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 1.1 christos * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 1.1 christos */
35 1.1 christos /**
36 1.1 christos * \file
37 1.1 christos * Contains message parsing data structures.
38 1.1 christos * These point back into the packet buffer.
39 1.1 christos *
40 1.1 christos * During parsing RRSIGS are put together with the rrsets they (claim to) sign.
41 1.1 christos * This process works as follows:
42 1.1 christos * o if RRSIG follows the data rrset, it is added to the rrset rrsig list.
43 1.1 christos * o if no matching data rrset is found, the RRSIG becomes a new rrset.
44 1.1 christos * o If the data rrset later follows the RRSIG
45 1.1 christos * o See if the RRSIG rrset contains multiple types, and needs to
46 1.1 christos * have the rrsig(s) for that data type split off.
47 1.1 christos * o Put the data rr as data type in the rrset and rrsig in list.
48 1.1 christos * o RRSIGs are allowed to move to a different section. The section of
49 1.1 christos * the data item is used for the final rrset.
50 1.1 christos * o multiple signatures over an RRset are possible.
51 1.1 christos *
52 1.1 christos * For queries of qtype=RRSIG, some special handling is needed, to avoid
53 1.1 christos * splitting the RRSIG in the answer section.
54 1.1 christos * o duplicate, not split, RRSIGs from the answer section, if qtype=RRSIG.
55 1.1 christos * o check for doubles in the rrsig list when adding an RRSIG to data,
56 1.1 christos * so that a data rrset is signed by RRSIGs with different rdata.
57 1.1 christos * when qtype=RRSIG.
58 1.1 christos * This will move the RRSIG from the answer section to sign the data further
59 1.1 christos * in the packet (if possible). If then after that, more RRSIGs are found
60 1.1 christos * that sign the data as well, doubles are removed.
61 1.1 christos */
62 1.1 christos
63 1.1 christos #ifndef UTIL_DATA_MSGPARSE_H
64 1.1 christos #define UTIL_DATA_MSGPARSE_H
65 1.1 christos #include "util/storage/lruhash.h"
66 1.1 christos #include "sldns/pkthdr.h"
67 1.1 christos #include "sldns/rrdef.h"
68 1.1 christos struct sldns_buffer;
69 1.1 christos struct rrset_parse;
70 1.1 christos struct rr_parse;
71 1.1 christos struct regional;
72 1.1 christos struct edns_option;
73 1.1 christos
74 1.1 christos /** number of buckets in parse rrset hash table. Must be power of 2. */
75 1.1 christos #define PARSE_TABLE_SIZE 32
76 1.1 christos /** Maximum TTL that is allowed. */
77 1.1 christos extern time_t MAX_TTL;
78 1.1 christos /** Minimum TTL that is allowed. */
79 1.1 christos extern time_t MIN_TTL;
80 1.1 christos /** Maximum Negative TTL that is allowed */
81 1.1 christos extern time_t MAX_NEG_TTL;
82 1.1.1.3 christos /** Time to serve records after expiration */
83 1.1.1.3 christos extern time_t SERVE_EXPIRED_TTL;
84 1.1 christos /** Negative cache time (for entries without any RRs.) */
85 1.1 christos #define NORR_TTL 5 /* seconds */
86 1.1 christos
87 1.1 christos /**
88 1.1 christos * Data stored in scratch pad memory during parsing.
89 1.1 christos * Stores the data that will enter into the msgreply and packet result.
90 1.1 christos */
91 1.1 christos struct msg_parse {
92 1.1 christos /** id from message, network format. */
93 1.1 christos uint16_t id;
94 1.1 christos /** flags from message, host format. */
95 1.1 christos uint16_t flags;
96 1.1 christos /** count of RRs, host format */
97 1.1 christos uint16_t qdcount;
98 1.1 christos /** count of RRs, host format */
99 1.1 christos uint16_t ancount;
100 1.1 christos /** count of RRs, host format */
101 1.1 christos uint16_t nscount;
102 1.1 christos /** count of RRs, host format */
103 1.1 christos uint16_t arcount;
104 1.1 christos /** count of RRsets per section. */
105 1.1 christos size_t an_rrsets;
106 1.1 christos /** count of RRsets per section. */
107 1.1 christos size_t ns_rrsets;
108 1.1 christos /** count of RRsets per section. */
109 1.1 christos size_t ar_rrsets;
110 1.1 christos /** total number of rrsets found. */
111 1.1 christos size_t rrset_count;
112 1.1 christos
113 1.1 christos /** query dname (pointer to start location in packet, NULL if none */
114 1.1 christos uint8_t* qname;
115 1.1 christos /** length of query dname in octets, 0 if none */
116 1.1 christos size_t qname_len;
117 1.1 christos /** query type, host order. 0 if qdcount=0 */
118 1.1 christos uint16_t qtype;
119 1.1 christos /** query class, host order. 0 if qdcount=0 */
120 1.1 christos uint16_t qclass;
121 1.1 christos
122 1.1 christos /**
123 1.1 christos * Hash table array used during parsing to lookup rrset types.
124 1.1 christos * Based on name, type, class. Same hash value as in rrset cache.
125 1.1 christos */
126 1.1 christos struct rrset_parse* hashtable[PARSE_TABLE_SIZE];
127 1.1 christos
128 1.1 christos /** linked list of rrsets that have been found (in order). */
129 1.1 christos struct rrset_parse* rrset_first;
130 1.1 christos /** last element of rrset list. */
131 1.1 christos struct rrset_parse* rrset_last;
132 1.1 christos };
133 1.1 christos
134 1.1 christos /**
135 1.1 christos * Data stored for an rrset during parsing.
136 1.1 christos */
137 1.1 christos struct rrset_parse {
138 1.1 christos /** next in hash bucket */
139 1.1 christos struct rrset_parse* rrset_bucket_next;
140 1.1 christos /** next in list of all rrsets */
141 1.1 christos struct rrset_parse* rrset_all_next;
142 1.1 christos /** hash value of rrset */
143 1.1.1.2 christos hashvalue_type hash;
144 1.1 christos /** which section was it found in: one of
145 1.1 christos * LDNS_SECTION_ANSWER, LDNS_SECTION_AUTHORITY, LDNS_SECTION_ADDITIONAL
146 1.1 christos */
147 1.1 christos sldns_pkt_section section;
148 1.1 christos /** start of (possibly compressed) dname in packet */
149 1.1 christos uint8_t* dname;
150 1.1 christos /** length of the dname uncompressed wireformat */
151 1.1 christos size_t dname_len;
152 1.1 christos /** type, host order. */
153 1.1 christos uint16_t type;
154 1.1 christos /** class, network order. var name so that it is not a c++ keyword. */
155 1.1 christos uint16_t rrset_class;
156 1.1 christos /** the flags for the rrset, like for packedrrset */
157 1.1 christos uint32_t flags;
158 1.1 christos /** number of RRs in the rr list */
159 1.1 christos size_t rr_count;
160 1.1 christos /** sum of RR rdata sizes */
161 1.1 christos size_t size;
162 1.1 christos /** linked list of RRs in this rrset. */
163 1.1 christos struct rr_parse* rr_first;
164 1.1 christos /** last in list of RRs in this rrset. */
165 1.1 christos struct rr_parse* rr_last;
166 1.1 christos /** number of RRSIGs over this rrset. */
167 1.1 christos size_t rrsig_count;
168 1.1 christos /** linked list of RRsig RRs over this rrset. */
169 1.1 christos struct rr_parse* rrsig_first;
170 1.1 christos /** last in list of RRSIG RRs over this rrset. */
171 1.1 christos struct rr_parse* rrsig_last;
172 1.1 christos };
173 1.1 christos
174 1.1 christos /**
175 1.1 christos * Data stored for an RR during parsing.
176 1.1 christos */
177 1.1 christos struct rr_parse {
178 1.1 christos /**
179 1.1 christos * Pointer to the RR. Points to start of TTL value in the packet.
180 1.1 christos * Rdata length and rdata follow it.
181 1.1 christos * its dname, type and class are the same and stored for the rrset.
182 1.1 christos */
183 1.1 christos uint8_t* ttl_data;
184 1.1 christos /** true if ttl_data is not part of the packet, but elsewhere in mem.
185 1.1 christos * Set for generated CNAMEs for DNAMEs. */
186 1.1 christos int outside_packet;
187 1.1 christos /** the length of the rdata if allocated (with no dname compression)*/
188 1.1 christos size_t size;
189 1.1 christos /** next in list of RRs. */
190 1.1 christos struct rr_parse* next;
191 1.1 christos };
192 1.1 christos
193 1.1 christos /** Check if label length is first octet of a compression pointer, pass u8. */
194 1.1 christos #define LABEL_IS_PTR(x) ( ((x)&0xc0) == 0xc0 )
195 1.1 christos /** Calculate destination offset of a compression pointer. pass first and
196 1.1 christos * second octets of the compression pointer. */
197 1.1 christos #define PTR_OFFSET(x, y) ( ((x)&0x3f)<<8 | (y) )
198 1.1 christos /** create a compression pointer to the given offset. */
199 1.1 christos #define PTR_CREATE(offset) ((uint16_t)(0xc000 | (offset)))
200 1.1 christos
201 1.1 christos /** error codes, extended with EDNS, so > 15. */
202 1.1 christos #define EDNS_RCODE_BADVERS 16 /** bad EDNS version */
203 1.1 christos /** largest valid compression offset */
204 1.1 christos #define PTR_MAX_OFFSET 0x3fff
205 1.1 christos
206 1.1 christos /**
207 1.1 christos * EDNS data storage
208 1.1 christos * rdata is parsed in a list (has accessor functions). allocated in a
209 1.1 christos * region.
210 1.1 christos */
211 1.1 christos struct edns_data {
212 1.1 christos /** if EDNS OPT record was present */
213 1.1 christos int edns_present;
214 1.1 christos /** Extended RCODE */
215 1.1 christos uint8_t ext_rcode;
216 1.1 christos /** The EDNS version number */
217 1.1 christos uint8_t edns_version;
218 1.1 christos /** the EDNS bits field from ttl (host order): Z */
219 1.1 christos uint16_t bits;
220 1.1 christos /** UDP reassembly size. */
221 1.1 christos uint16_t udp_size;
222 1.1 christos /** rdata element list, or NULL if none */
223 1.1 christos struct edns_option* opt_list;
224 1.1 christos };
225 1.1 christos
226 1.1 christos /**
227 1.1 christos * EDNS option
228 1.1 christos */
229 1.1 christos struct edns_option {
230 1.1 christos /** next item in list */
231 1.1 christos struct edns_option* next;
232 1.1 christos /** type of this edns option */
233 1.1 christos uint16_t opt_code;
234 1.1 christos /** length of this edns option (cannot exceed uint16 in encoding) */
235 1.1 christos size_t opt_len;
236 1.1 christos /** data of this edns option; allocated in region, or NULL if len=0 */
237 1.1 christos uint8_t* opt_data;
238 1.1 christos };
239 1.1 christos
240 1.1 christos /**
241 1.1 christos * Obtain size in the packet of an rr type, that is before dname type.
242 1.1 christos * Do TYPE_DNAME, and type STR, yourself. Gives size for most regular types.
243 1.1 christos * @param rdf: the rdf type from the descriptor.
244 1.1 christos * @return: size in octets. 0 on failure.
245 1.1 christos */
246 1.1 christos size_t get_rdf_size(sldns_rdf_type rdf);
247 1.1 christos
248 1.1 christos /**
249 1.1 christos * Parse the packet.
250 1.1 christos * @param pkt: packet, position at call must be at start of packet.
251 1.1 christos * at end position is after packet.
252 1.1 christos * @param msg: where to store results.
253 1.1 christos * @param region: how to alloc results.
254 1.1 christos * @return: 0 if OK, or rcode on error.
255 1.1 christos */
256 1.1 christos int parse_packet(struct sldns_buffer* pkt, struct msg_parse* msg,
257 1.1 christos struct regional* region);
258 1.1 christos
259 1.1 christos /**
260 1.1 christos * After parsing the packet, extract EDNS data from packet.
261 1.1 christos * If not present this is noted in the data structure.
262 1.1 christos * If a parse error happens, an error code is returned.
263 1.1 christos *
264 1.1 christos * Quirks:
265 1.1 christos * o ignores OPT rdata.
266 1.1 christos * o ignores OPT owner name.
267 1.1 christos * o ignores extra OPT records, except the last one in the packet.
268 1.1 christos *
269 1.1 christos * @param msg: parsed message structure. Modified on exit, if EDNS was present
270 1.1 christos * it is removed from the additional section.
271 1.1 christos * @param edns: the edns data is stored here. Does not have to be initialised.
272 1.1 christos * @param region: region to alloc results in (edns option contents)
273 1.1 christos * @return: 0 on success. or an RCODE on an error.
274 1.1 christos * RCODE formerr if OPT in wrong section, and so on.
275 1.1 christos */
276 1.1 christos int parse_extract_edns(struct msg_parse* msg, struct edns_data* edns,
277 1.1 christos struct regional* region);
278 1.1 christos
279 1.1 christos /**
280 1.1 christos * If EDNS data follows a query section, extract it and initialize edns struct.
281 1.1 christos * @param pkt: the packet. position at start must be right after the query
282 1.1 christos * section. At end, right after EDNS data or no movement if failed.
283 1.1 christos * @param edns: the edns data allocated by the caller. Does not have to be
284 1.1 christos * initialised.
285 1.1 christos * @param region: region to alloc results in (edns option contents)
286 1.1 christos * @return: 0 on success, or an RCODE on error.
287 1.1 christos * RCODE formerr if OPT is badly formatted and so on.
288 1.1 christos */
289 1.1 christos int parse_edns_from_pkt(struct sldns_buffer* pkt, struct edns_data* edns,
290 1.1 christos struct regional* region);
291 1.1 christos
292 1.1 christos /**
293 1.1 christos * Calculate hash value for rrset in packet.
294 1.1 christos * @param pkt: the packet.
295 1.1 christos * @param dname: pointer to uncompressed dname, or compressed dname in packet.
296 1.1 christos * @param type: rrset type in host order.
297 1.1 christos * @param dclass: rrset class in network order.
298 1.1 christos * @param rrset_flags: rrset flags (same as packed_rrset flags).
299 1.1 christos * @return hash value
300 1.1 christos */
301 1.1.1.2 christos hashvalue_type pkt_hash_rrset(struct sldns_buffer* pkt, uint8_t* dname,
302 1.1.1.2 christos uint16_t type, uint16_t dclass, uint32_t rrset_flags);
303 1.1 christos
304 1.1 christos /**
305 1.1 christos * Lookup in msg hashtable to find a rrset.
306 1.1 christos * @param msg: with the hashtable.
307 1.1 christos * @param pkt: packet for compressed names.
308 1.1 christos * @param h: hash value
309 1.1 christos * @param rrset_flags: flags of rrset sought for.
310 1.1 christos * @param dname: name of rrset sought for.
311 1.1 christos * @param dnamelen: len of dname.
312 1.1 christos * @param type: rrset type, host order.
313 1.1 christos * @param dclass: rrset class, network order.
314 1.1 christos * @return NULL or the rrset_parse if found.
315 1.1 christos */
316 1.1 christos struct rrset_parse* msgparse_hashtable_lookup(struct msg_parse* msg,
317 1.1.1.2 christos struct sldns_buffer* pkt, hashvalue_type h, uint32_t rrset_flags,
318 1.1 christos uint8_t* dname, size_t dnamelen, uint16_t type, uint16_t dclass);
319 1.1 christos
320 1.1 christos /**
321 1.1 christos * Remove rrset from hash table.
322 1.1 christos * @param msg: with hashtable.
323 1.1 christos * @param rrset: with hash value and id info.
324 1.1 christos */
325 1.1 christos void msgparse_bucket_remove(struct msg_parse* msg, struct rrset_parse* rrset);
326 1.1 christos
327 1.1.1.2 christos /**
328 1.1.1.2 christos * Log the edns options in the edns option list.
329 1.1.1.2 christos * @param level: the verbosity level.
330 1.1.1.2 christos * @param info_str: the informational string to be printed before the options.
331 1.1.1.2 christos * @param list: the edns option list.
332 1.1.1.2 christos */
333 1.1.1.2 christos void log_edns_opt_list(enum verbosity_value level, const char* info_str,
334 1.1.1.2 christos struct edns_option* list);
335 1.1.1.2 christos
336 1.1 christos #endif /* UTIL_DATA_MSGPARSE_H */
337