xref: /openbsd/usr.sbin/unbound/util/data/msgreply.h (revision 898184e3)
1 /*
2  * util/data/msgreply.h - store message and reply data.
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 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.
34  */
35 
36 /**
37  * \file
38  *
39  * This file contains a data structure to store a message and its reply.
40  */
41 
42 #ifndef UTIL_DATA_MSGREPLY_H
43 #define UTIL_DATA_MSGREPLY_H
44 #include "util/storage/lruhash.h"
45 #include "util/data/packed_rrset.h"
46 struct comm_reply;
47 struct alloc_cache;
48 struct iovec;
49 struct regional;
50 struct edns_data;
51 struct msg_parse;
52 struct rrset_parse;
53 
54 /** calculate the prefetch TTL as 90% of original. Calculation
55  * without numerical overflow (uin32_t) */
56 #define PREFETCH_TTL_CALC(ttl) ((ttl) - (ttl)/10)
57 
58 /**
59  * Structure to store query information that makes answers to queries
60  * different.
61  */
62 struct query_info {
63 	/**
64 	 * Salient data on the query: qname, in wireformat.
65 	 * can be allocated or a pointer to outside buffer.
66 	 * User has to keep track on the status of this.
67 	 */
68 	uint8_t* qname;
69 	/** length of qname (including last 0 octet) */
70 	size_t qname_len;
71 	/** qtype, host byte order */
72 	uint16_t qtype;
73 	/** qclass, host byte order */
74 	uint16_t qclass;
75 };
76 
77 /**
78  * Information to reference an rrset
79  */
80 struct rrset_ref {
81 	/** the key with lock, and ptr to packed data. */
82 	struct ub_packed_rrset_key* key;
83 	/** id needed */
84 	rrset_id_t id;
85 };
86 
87 /**
88  * Structure to store DNS query and the reply packet.
89  * To use it, copy over the flags from reply and modify using flags from
90  * the query (RD,CD if not AA). prepend ID.
91  *
92  * Memory layout is:
93  *	o struct
94  *	o rrset_ref array
95  *	o packed_rrset_key* array.
96  *
97  * Memory layout is sometimes not packed, when the message is synthesized,
98  * for easy of the generation. It is allocated packed when it is copied
99  * from the region allocation to the malloc allocation.
100  */
101 struct reply_info {
102 	/** the flags for the answer, host byte order. */
103 	uint16_t flags;
104 
105 	/**
106 	 * This flag informs unbound the answer is authoritative and
107 	 * the AA flag should be preserved.
108 	 */
109 	uint8_t authoritative;
110 
111 	/**
112 	 * Number of RRs in the query section.
113 	 * If qdcount is not 0, then it is 1, and the data that appears
114 	 * in the reply is the same as the query_info.
115 	 * Host byte order.
116 	 */
117 	uint8_t qdcount;
118 
119 	/**
120 	 * TTL of the entire reply (for negative caching).
121 	 * only for use when there are 0 RRsets in this message.
122 	 * if there are RRsets, check those instead.
123 	 */
124 	uint32_t ttl;
125 
126 	/**
127 	 * TTL for prefetch. After it has expired, a prefetch is suitable.
128 	 * Smaller than the TTL, otherwise the prefetch would not happen.
129 	 */
130 	uint32_t prefetch_ttl;
131 
132 	/** 32 bit padding to pad struct member alignment to 64 bits. */
133 	uint32_t padding;
134 
135 	/**
136 	 * The security status from DNSSEC validation of this message.
137 	 */
138 	enum sec_status security;
139 
140 	/**
141 	 * Number of RRsets in each section.
142 	 * The answer section. Add up the RRs in every RRset to calculate
143 	 * the number of RRs, and the count for the dns packet.
144 	 * The number of RRs in RRsets can change due to RRset updates.
145 	 */
146 	size_t an_numrrsets;
147 
148 	/** Count of authority section RRsets */
149 	size_t ns_numrrsets;
150 	/** Count of additional section RRsets */
151 	size_t ar_numrrsets;
152 
153 	/** number of RRsets: an_numrrsets + ns_numrrsets + ar_numrrsets */
154 	size_t rrset_count;
155 
156 	/**
157 	 * List of pointers (only) to the rrsets in the order in which
158 	 * they appear in the reply message.
159 	 * Number of elements is ancount+nscount+arcount RRsets.
160 	 * This is a pointer to that array.
161 	 * Use the accessor function for access.
162 	 */
163 	struct ub_packed_rrset_key** rrsets;
164 
165 	/**
166 	 * Packed array of ids (see counts) and pointers to packed_rrset_key.
167 	 * The number equals ancount+nscount+arcount RRsets.
168 	 * These are sorted in ascending pointer, the locking order. So
169 	 * this list can be locked (and id, ttl checked), to see if
170 	 * all the data is available and recent enough.
171 	 *
172 	 * This is defined as an array of size 1, so that the compiler
173 	 * associates the identifier with this position in the structure.
174 	 * Array bound overflow on this array then gives access to the further
175 	 * elements of the array, which are allocated after the main structure.
176 	 *
177 	 * It could be more pure to define as array of size 0, ref[0].
178 	 * But ref[1] may be less confusing for compilers.
179 	 * Use the accessor function for access.
180 	 */
181 	struct rrset_ref ref[1];
182 };
183 
184 /**
185  * Structure to keep hash table entry for message replies.
186  */
187 struct msgreply_entry {
188 	/** the hash table key */
189 	struct query_info key;
190 	/** the hash table entry, data is struct reply_info* */
191 	struct lruhash_entry entry;
192 };
193 
194 /**
195  * Parse wire query into a queryinfo structure, return 0 on parse error.
196  * initialises the (prealloced) queryinfo structure as well.
197  * This query structure contains a pointer back info the buffer!
198  * This pointer avoids memory allocation. allocqname does memory allocation.
199  * @param m: the prealloced queryinfo structure to put query into.
200  *    must be unused, or _clear()ed.
201  * @param query: the wireformat packet query. starts with ID.
202  * @return: 0 on format error.
203  */
204 int query_info_parse(struct query_info* m, ldns_buffer* query);
205 
206 /**
207  * Parse query reply.
208  * Fills in preallocated query_info structure (with ptr into buffer).
209  * Allocates reply_info and packed_rrsets. These are not yet added to any
210  * caches or anything, this is only parsing. Returns formerror on qdcount > 1.
211  * @param pkt: the packet buffer. Must be positioned after the query section.
212  * @param alloc: creates packed rrset key structures.
213  * @param rep: allocated reply_info is returned (only on no error).
214  * @param qinf: query_info is returned (only on no error).
215  * @param region: where to store temporary data (for parsing).
216  * @param edns: where to store edns information, does not need to be inited.
217  * @return: zero is OK, or DNS error code in case of error
218  *	o FORMERR for parse errors.
219  *	o SERVFAIL for memory allocation errors.
220  */
221 int reply_info_parse(ldns_buffer* pkt, struct alloc_cache* alloc,
222 	struct query_info* qinf, struct reply_info** rep,
223 	struct regional* region, struct edns_data* edns);
224 
225 /**
226  * Allocate and decompress parsed message and rrsets.
227  * @param pkt: for name decompression.
228  * @param msg: parsed message in scratch region.
229  * @param alloc: alloc cache for special rrset key structures.
230  *	Not used if region!=NULL, it can be NULL in that case.
231  * @param qinf: where to store query info.
232  *	qinf itself is allocated by the caller.
233  * @param rep: reply info is allocated and returned.
234  * @param region: if this parameter is NULL then malloc and the alloc is used.
235  *	otherwise, everything is allocated in this region.
236  *	In a region, no special rrset key structures are needed (not shared),
237  *	and no rrset_ref array in the reply is built up.
238  * @return 0 if allocation failed.
239  */
240 int parse_create_msg(ldns_buffer* pkt, struct msg_parse* msg,
241         struct alloc_cache* alloc, struct query_info* qinf,
242 	struct reply_info** rep, struct regional* region);
243 
244 /**
245  * Sorts the ref array.
246  * @param rep: reply info. rrsets must be filled in.
247  */
248 void reply_info_sortref(struct reply_info* rep);
249 
250 /**
251  * Set TTLs inside the replyinfo to absolute values.
252  * @param rep: reply info. rrsets must be filled in.
253  *	Also refs must be filled in.
254  * @param timenow: the current time.
255  */
256 void reply_info_set_ttls(struct reply_info* rep, uint32_t timenow);
257 
258 /**
259  * Delete reply_info and packed_rrsets (while they are not yet added to the
260  * hashtables.). Returns rrsets to the alloc cache.
261  * @param rep: reply_info to delete.
262  * @param alloc: where to return rrset structures to.
263  */
264 void reply_info_parsedelete(struct reply_info* rep, struct alloc_cache* alloc);
265 
266 /**
267  * Compare two queryinfo structures, on query and type, class.
268  * It is _not_ sorted in canonical ordering.
269  * @param m1: struct query_info* , void* here to ease use as function pointer.
270  * @param m2: struct query_info* , void* here to ease use as function pointer.
271  * @return: 0 = same, -1 m1 is smaller, +1 m1 is larger.
272  */
273 int query_info_compare(void* m1, void* m2);
274 
275 /** clear out query info structure */
276 void query_info_clear(struct query_info* m);
277 
278 /** calculate size of struct query_info + reply_info */
279 size_t msgreply_sizefunc(void* k, void* d);
280 
281 /** delete msgreply_entry key structure */
282 void query_entry_delete(void *q, void* arg);
283 
284 /** delete reply_info data structure */
285 void reply_info_delete(void* d, void* arg);
286 
287 /** calculate hash value of query_info, lowercases the qname */
288 hashvalue_t query_info_hash(struct query_info *q);
289 
290 /**
291  * Setup query info entry
292  * @param q: query info to copy. Emptied as if clear is called.
293  * @param r: reply to init data.
294  * @param h: hash value.
295  * @return: newly allocated message reply cache item.
296  */
297 struct msgreply_entry* query_info_entrysetup(struct query_info* q,
298 	struct reply_info* r, hashvalue_t h);
299 
300 /**
301  * Copy reply_info and all rrsets in it and allocate.
302  * @param rep: what to copy, probably inside region, no ref[] array in it.
303  * @param alloc: how to allocate rrset keys.
304  *	Not used if region!=NULL, it can be NULL in that case.
305  * @param region: if this parameter is NULL then malloc and the alloc is used.
306  *	otherwise, everything is allocated in this region.
307  *	In a region, no special rrset key structures are needed (not shared),
308  *	and no rrset_ref array in the reply is built up.
309  * @return new reply info or NULL on memory error.
310  */
311 struct reply_info* reply_info_copy(struct reply_info* rep,
312 	struct alloc_cache* alloc, struct regional* region);
313 
314 /**
315  * Copy a parsed rrset into given key, decompressing and allocating rdata.
316  * @param pkt: packet for decompression
317  * @param msg: the parser message (for flags for trust).
318  * @param pset: the parsed rrset to copy.
319  * @param region: if NULL - malloc, else data is allocated in this region.
320  * @param pk: a freshly obtained rrsetkey structure. No dname is set yet,
321  *	will be set on return.
322  *	Note that TTL will still be relative on return.
323  * @return false on alloc failure.
324  */
325 int parse_copy_decompress_rrset(ldns_buffer* pkt, struct msg_parse* msg,
326 	struct rrset_parse *pset, struct regional* region,
327 	struct ub_packed_rrset_key* pk);
328 
329 /**
330  * Find final cname target in reply, the one matching qinfo. Follows CNAMEs.
331  * @param qinfo: what to start with.
332  * @param rep: looks in answer section of this message.
333  * @return: pointer dname, or NULL if not found.
334  */
335 uint8_t* reply_find_final_cname_target(struct query_info* qinfo,
336 	struct reply_info* rep);
337 
338 /**
339  * Check if cname chain in cached reply is still valid.
340  * @param rep: reply to check.
341  * @return: true if valid, false if invalid.
342  */
343 int reply_check_cname_chain(struct reply_info* rep);
344 
345 /**
346  * Check security status of all RRs in the message.
347  * @param rep: reply to check
348  * @return: true if all RRs are secure. False if not.
349  *    True if there are zero RRs.
350  */
351 int reply_all_rrsets_secure(struct reply_info* rep);
352 
353 /**
354  * Find answer rrset in reply, the one matching qinfo. Follows CNAMEs, so the
355  * result may have a different owner name.
356  * @param qinfo: what to look for.
357  * @param rep: looks in answer section of this message.
358  * @return: pointer to rrset, or NULL if not found.
359  */
360 struct ub_packed_rrset_key* reply_find_answer_rrset(struct query_info* qinfo,
361 	struct reply_info* rep);
362 
363 /**
364  * Find rrset in reply, inside the answer section. Does not follow CNAMEs.
365  * @param rep: looks in answer section of this message.
366  * @param name: what to look for.
367  * @param namelen: length of name.
368  * @param type: looks for (host order).
369  * @param dclass: looks for (host order).
370  * @return: pointer to rrset, or NULL if not found.
371  */
372 struct ub_packed_rrset_key* reply_find_rrset_section_an(struct reply_info* rep,
373 	uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass);
374 
375 /**
376  * Find rrset in reply, inside the authority section. Does not follow CNAMEs.
377  * @param rep: looks in authority section of this message.
378  * @param name: what to look for.
379  * @param namelen: length of name.
380  * @param type: looks for (host order).
381  * @param dclass: looks for (host order).
382  * @return: pointer to rrset, or NULL if not found.
383  */
384 struct ub_packed_rrset_key* reply_find_rrset_section_ns(struct reply_info* rep,
385 	uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass);
386 
387 /**
388  * Find rrset in reply, inside any section. Does not follow CNAMEs.
389  * @param rep: looks in answer,authority and additional section of this message.
390  * @param name: what to look for.
391  * @param namelen: length of name.
392  * @param type: looks for (host order).
393  * @param dclass: looks for (host order).
394  * @return: pointer to rrset, or NULL if not found.
395  */
396 struct ub_packed_rrset_key* reply_find_rrset(struct reply_info* rep,
397 	uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass);
398 
399 /**
400  * Debug send the query info and reply info to the log in readable form.
401  * @param str: descriptive string printed with packet content.
402  * @param qinfo: query section.
403  * @param rep: rest of message.
404  */
405 void log_dns_msg(const char* str, struct query_info* qinfo,
406 	struct reply_info* rep);
407 
408 /**
409  * Print string with neat domain name, type, class from query info.
410  * @param v: at what verbosity level to print this.
411  * @param str: string of message.
412  * @param qinf: query info structure with name, type and class.
413  */
414 void log_query_info(enum verbosity_value v, const char* str,
415 	struct query_info* qinf);
416 
417 #endif /* UTIL_DATA_MSGREPLY_H */
418