]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/unbound/util/data/msgreply.h
Upgrade Unbound to 1.6.0. More to follow.
[FreeBSD/FreeBSD.git] / contrib / unbound / util / data / msgreply.h
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
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 /**
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 sldns_buffer;
47 struct comm_reply;
48 struct alloc_cache;
49 struct iovec;
50 struct regional;
51 struct edns_data;
52 struct edns_option;
53 struct inplace_cb_reply;
54 struct inplace_cb_query;
55 struct module_qstate;
56 struct module_env;
57 struct msg_parse;
58 struct rrset_parse;
59 struct local_rrset;
60
61 /** calculate the prefetch TTL as 90% of original. Calculation
62  * without numerical overflow (uin32_t) */
63 #define PREFETCH_TTL_CALC(ttl) ((ttl) - (ttl)/10)
64
65 /**
66  * Structure to store query information that makes answers to queries
67  * different.
68  */
69 struct query_info {
70         /** 
71          * Salient data on the query: qname, in wireformat. 
72          * can be allocated or a pointer to outside buffer.
73          * User has to keep track on the status of this.
74          */
75         uint8_t* qname;
76         /** length of qname (including last 0 octet) */
77         size_t qname_len;
78         /** qtype, host byte order */
79         uint16_t qtype;
80         /** qclass, host byte order */
81         uint16_t qclass;
82         /**
83          * Alias local answer(s) for the qname.  If 'qname' is an alias defined
84          * in a local zone, this field will be set to the corresponding local
85          * RRset when the alias is determined.
86          * In the initial implementation this can only be a single CNAME RR
87          * (or NULL), but it could possibly be extended to be a DNAME or a
88          * chain of aliases.
89          * Users of this structure are responsible to initialize this field
90          * to be NULL; otherwise other part of query handling code may be
91          * confused.
92          * Users also have to be careful about the lifetime of data.  On return
93          * from local zone lookup, it may point to data derived from
94          * configuration that may be dynamically invalidated or data allocated
95          * in an ephemeral regional allocator.  A deep copy of the data may
96          * have to be generated if it has to be kept during iterative
97          * resolution. */
98         struct local_rrset* local_alias;
99 };
100
101 /**
102  * Information to reference an rrset
103  */
104 struct rrset_ref {
105         /** the key with lock, and ptr to packed data. */
106         struct ub_packed_rrset_key* key;
107         /** id needed */
108         rrset_id_t id;
109 };
110
111 /**
112  * Structure to store DNS query and the reply packet.
113  * To use it, copy over the flags from reply and modify using flags from
114  * the query (RD,CD if not AA). prepend ID. 
115  *
116  * Memory layout is:
117  *      o struct
118  *      o rrset_ref array
119  *      o packed_rrset_key* array.
120  *
121  * Memory layout is sometimes not packed, when the message is synthesized,
122  * for easy of the generation. It is allocated packed when it is copied
123  * from the region allocation to the malloc allocation.
124  */
125 struct reply_info {
126         /** the flags for the answer, host byte order. */
127         uint16_t flags;
128
129         /**
130          * This flag informs unbound the answer is authoritative and 
131          * the AA flag should be preserved. 
132          */
133         uint8_t authoritative;
134
135         /**
136          * Number of RRs in the query section.
137          * If qdcount is not 0, then it is 1, and the data that appears
138          * in the reply is the same as the query_info.
139          * Host byte order.
140          */
141         uint8_t qdcount;
142
143         /** 32 bit padding to pad struct member alignment to 64 bits. */
144         uint32_t padding;
145
146         /** 
147          * TTL of the entire reply (for negative caching).
148          * only for use when there are 0 RRsets in this message.
149          * if there are RRsets, check those instead.
150          */
151         time_t ttl;
152
153         /**
154          * TTL for prefetch. After it has expired, a prefetch is suitable.
155          * Smaller than the TTL, otherwise the prefetch would not happen.
156          */
157         time_t prefetch_ttl;
158
159         /**
160          * The security status from DNSSEC validation of this message.
161          */
162         enum sec_status security;
163
164         /**
165          * Number of RRsets in each section.
166          * The answer section. Add up the RRs in every RRset to calculate
167          * the number of RRs, and the count for the dns packet. 
168          * The number of RRs in RRsets can change due to RRset updates.
169          */
170         size_t an_numrrsets;
171
172         /** Count of authority section RRsets */
173         size_t ns_numrrsets; 
174         /** Count of additional section RRsets */
175         size_t ar_numrrsets;
176
177         /** number of RRsets: an_numrrsets + ns_numrrsets + ar_numrrsets */
178         size_t rrset_count;
179
180         /** 
181          * List of pointers (only) to the rrsets in the order in which 
182          * they appear in the reply message.  
183          * Number of elements is ancount+nscount+arcount RRsets.
184          * This is a pointer to that array. 
185          * Use the accessor function for access.
186          */
187         struct ub_packed_rrset_key** rrsets;
188
189         /** 
190          * Packed array of ids (see counts) and pointers to packed_rrset_key.
191          * The number equals ancount+nscount+arcount RRsets. 
192          * These are sorted in ascending pointer, the locking order. So
193          * this list can be locked (and id, ttl checked), to see if 
194          * all the data is available and recent enough.
195          *
196          * This is defined as an array of size 1, so that the compiler 
197          * associates the identifier with this position in the structure.
198          * Array bound overflow on this array then gives access to the further
199          * elements of the array, which are allocated after the main structure.
200          *
201          * It could be more pure to define as array of size 0, ref[0].
202          * But ref[1] may be less confusing for compilers.
203          * Use the accessor function for access.
204          */
205         struct rrset_ref ref[1];
206 };
207
208 /**
209  * Structure to keep hash table entry for message replies.
210  */
211 struct msgreply_entry {
212         /** the hash table key */
213         struct query_info key;
214         /** the hash table entry, data is struct reply_info* */
215         struct lruhash_entry entry;
216 };
217
218 /**
219  * Constructor for replyinfo.
220  * @param region: where to allocate the results, pass NULL to use malloc.
221  * @param flags: flags for the replyinfo.
222  * @param qd: qd count
223  * @param ttl: TTL of replyinfo
224  * @param prettl: prefetch ttl
225  * @param an: an count
226  * @param ns: ns count
227  * @param ar: ar count
228  * @param total: total rrset count (presumably an+ns+ar).
229  * @param sec: security status of the reply info.
230  * @return the reply_info base struct with the array for putting the rrsets
231  * in.  The array has been zeroed.  Returns NULL on malloc failure.
232  */
233 struct reply_info*
234 construct_reply_info_base(struct regional* region, uint16_t flags, size_t qd,
235                 time_t ttl, time_t prettl, size_t an, size_t ns, size_t ar,
236                 size_t total, enum sec_status sec);
237
238 /** 
239  * Parse wire query into a queryinfo structure, return 0 on parse error. 
240  * initialises the (prealloced) queryinfo structure as well.
241  * This query structure contains a pointer back info the buffer!
242  * This pointer avoids memory allocation. allocqname does memory allocation.
243  * @param m: the prealloced queryinfo structure to put query into.
244  *    must be unused, or _clear()ed.
245  * @param query: the wireformat packet query. starts with ID.
246  * @return: 0 on format error.
247  */
248 int query_info_parse(struct query_info* m, struct sldns_buffer* query);
249
250 /**
251  * Parse query reply.
252  * Fills in preallocated query_info structure (with ptr into buffer).
253  * Allocates reply_info and packed_rrsets. These are not yet added to any
254  * caches or anything, this is only parsing. Returns formerror on qdcount > 1.
255  * @param pkt: the packet buffer. Must be positioned after the query section.
256  * @param alloc: creates packed rrset key structures.
257  * @param rep: allocated reply_info is returned (only on no error).
258  * @param qinf: query_info is returned (only on no error).
259  * @param region: where to store temporary data (for parsing).
260  * @param edns: where to store edns information, does not need to be inited.
261  * @return: zero is OK, or DNS error code in case of error
262  *      o FORMERR for parse errors.
263  *      o SERVFAIL for memory allocation errors.
264  */
265 int reply_info_parse(struct sldns_buffer* pkt, struct alloc_cache* alloc,
266         struct query_info* qinf, struct reply_info** rep, 
267         struct regional* region, struct edns_data* edns);
268
269 /**
270  * Allocate and decompress parsed message and rrsets.
271  * @param pkt: for name decompression.
272  * @param msg: parsed message in scratch region.
273  * @param alloc: alloc cache for special rrset key structures.
274  *      Not used if region!=NULL, it can be NULL in that case.
275  * @param qinf: where to store query info.
276  *      qinf itself is allocated by the caller.
277  * @param rep: reply info is allocated and returned.
278  * @param region: if this parameter is NULL then malloc and the alloc is used.
279  *      otherwise, everything is allocated in this region.
280  *      In a region, no special rrset key structures are needed (not shared),
281  *      and no rrset_ref array in the reply is built up.
282  * @return 0 if allocation failed.
283  */
284 int parse_create_msg(struct sldns_buffer* pkt, struct msg_parse* msg,
285         struct alloc_cache* alloc, struct query_info* qinf,
286         struct reply_info** rep, struct regional* region);
287
288 /**
289  * Sorts the ref array.
290  * @param rep: reply info. rrsets must be filled in.
291  */
292 void reply_info_sortref(struct reply_info* rep);
293
294 /**
295  * Set TTLs inside the replyinfo to absolute values.
296  * @param rep: reply info. rrsets must be filled in. 
297  *      Also refs must be filled in.
298  * @param timenow: the current time.
299  */
300 void reply_info_set_ttls(struct reply_info* rep, time_t timenow);
301
302 /** 
303  * Delete reply_info and packed_rrsets (while they are not yet added to the
304  * hashtables.). Returns rrsets to the alloc cache.
305  * @param rep: reply_info to delete.
306  * @param alloc: where to return rrset structures to.
307  */
308 void reply_info_parsedelete(struct reply_info* rep, struct alloc_cache* alloc);
309
310 /**
311  * Compare two queryinfo structures, on query and type, class. 
312  * It is _not_ sorted in canonical ordering.
313  * @param m1: struct query_info* , void* here to ease use as function pointer.
314  * @param m2: struct query_info* , void* here to ease use as function pointer.
315  * @return: 0 = same, -1 m1 is smaller, +1 m1 is larger.
316  */
317 int query_info_compare(void* m1, void* m2);
318
319 /** clear out query info structure */
320 void query_info_clear(struct query_info* m);
321
322 /** calculate size of struct query_info + reply_info */
323 size_t msgreply_sizefunc(void* k, void* d);
324
325 /** delete msgreply_entry key structure */
326 void query_entry_delete(void *q, void* arg);
327
328 /** delete reply_info data structure */
329 void reply_info_delete(void* d, void* arg);
330
331 /** calculate hash value of query_info, lowercases the qname,
332  * uses CD flag for AAAA qtype */
333 hashvalue_t query_info_hash(struct query_info *q, uint16_t flags);
334
335 /**
336  * Setup query info entry
337  * @param q: query info to copy. Emptied as if clear is called.
338  * @param r: reply to init data.
339  * @param h: hash value.
340  * @return: newly allocated message reply cache item.
341  */
342 struct msgreply_entry* query_info_entrysetup(struct query_info* q,
343         struct reply_info* r, hashvalue_t h);
344
345 /**
346  * Copy reply_info and all rrsets in it and allocate.
347  * @param rep: what to copy, probably inside region, no ref[] array in it.
348  * @param alloc: how to allocate rrset keys.
349  *      Not used if region!=NULL, it can be NULL in that case.
350  * @param region: if this parameter is NULL then malloc and the alloc is used.
351  *      otherwise, everything is allocated in this region.
352  *      In a region, no special rrset key structures are needed (not shared),
353  *      and no rrset_ref array in the reply is built up.
354  * @return new reply info or NULL on memory error.
355  */
356 struct reply_info* reply_info_copy(struct reply_info* rep, 
357         struct alloc_cache* alloc, struct regional* region);
358
359 /**
360  * Copy a parsed rrset into given key, decompressing and allocating rdata.
361  * @param pkt: packet for decompression
362  * @param msg: the parser message (for flags for trust).
363  * @param pset: the parsed rrset to copy.
364  * @param region: if NULL - malloc, else data is allocated in this region.
365  * @param pk: a freshly obtained rrsetkey structure. No dname is set yet,
366  *      will be set on return.
367  *      Note that TTL will still be relative on return.
368  * @return false on alloc failure.
369  */
370 int parse_copy_decompress_rrset(struct sldns_buffer* pkt, struct msg_parse* msg,
371         struct rrset_parse *pset, struct regional* region, 
372         struct ub_packed_rrset_key* pk);
373
374 /**
375  * Find final cname target in reply, the one matching qinfo. Follows CNAMEs.
376  * @param qinfo: what to start with.
377  * @param rep: looks in answer section of this message.
378  * @return: pointer dname, or NULL if not found.
379  */
380 uint8_t* reply_find_final_cname_target(struct query_info* qinfo,
381         struct reply_info* rep);
382
383 /**
384  * Check if cname chain in cached reply is still valid.
385  * @param qinfo: query info with query name.
386  * @param rep: reply to check.
387  * @return: true if valid, false if invalid.
388  */
389 int reply_check_cname_chain(struct query_info* qinfo, struct reply_info* rep);
390
391 /**
392  * Check security status of all RRs in the message.
393  * @param rep: reply to check
394  * @return: true if all RRs are secure. False if not.
395  *    True if there are zero RRs.
396  */
397 int reply_all_rrsets_secure(struct reply_info* rep);
398
399 /**
400  * Find answer rrset in reply, the one matching qinfo. Follows CNAMEs, so the
401  * result may have a different owner name.
402  * @param qinfo: what to look for.
403  * @param rep: looks in answer section of this message.
404  * @return: pointer to rrset, or NULL if not found.
405  */
406 struct ub_packed_rrset_key* reply_find_answer_rrset(struct query_info* qinfo,
407         struct reply_info* rep);
408
409 /**
410  * Find rrset in reply, inside the answer section. Does not follow CNAMEs.
411  * @param rep: looks in answer section of this message.
412  * @param name: what to look for.
413  * @param namelen: length of name.
414  * @param type: looks for (host order).
415  * @param dclass: looks for (host order).
416  * @return: pointer to rrset, or NULL if not found.
417  */
418 struct ub_packed_rrset_key* reply_find_rrset_section_an(struct reply_info* rep,
419         uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass);
420
421 /**
422  * Find rrset in reply, inside the authority section. Does not follow CNAMEs.
423  * @param rep: looks in authority section of this message.
424  * @param name: what to look for.
425  * @param namelen: length of name.
426  * @param type: looks for (host order).
427  * @param dclass: looks for (host order).
428  * @return: pointer to rrset, or NULL if not found.
429  */
430 struct ub_packed_rrset_key* reply_find_rrset_section_ns(struct reply_info* rep,
431         uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass);
432
433 /**
434  * Find rrset in reply, inside any section. Does not follow CNAMEs.
435  * @param rep: looks in answer,authority and additional section of this message.
436  * @param name: what to look for.
437  * @param namelen: length of name.
438  * @param type: looks for (host order).
439  * @param dclass: looks for (host order).
440  * @return: pointer to rrset, or NULL if not found.
441  */
442 struct ub_packed_rrset_key* reply_find_rrset(struct reply_info* rep,
443         uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass);
444
445 /**
446  * Debug send the query info and reply info to the log in readable form.
447  * @param str: descriptive string printed with packet content.
448  * @param qinfo: query section.
449  * @param rep: rest of message.
450  */
451 void log_dns_msg(const char* str, struct query_info* qinfo, 
452         struct reply_info* rep);
453
454 /**
455  * Print string with neat domain name, type, class from query info.
456  * @param v: at what verbosity level to print this.
457  * @param str: string of message.
458  * @param qinf: query info structure with name, type and class.
459  */
460 void log_query_info(enum verbosity_value v, const char* str, 
461         struct query_info* qinf);
462
463 /**
464  * Append edns option to edns data structure
465  * @param edns: the edns data structure to append the edns option to.
466  * @param region: region to allocate the new edns option.
467  * @param code: the edns option's code.
468  * @param len: the edns option's length.
469  * @param data: the edns option's data.
470  * @return false on failure.
471  */
472 int edns_opt_append(struct edns_data* edns, struct regional* region,
473         uint16_t code, size_t len, uint8_t* data);
474
475 /**
476  * Append edns option to edns option list
477  * @param list: the edns option list to append the edns option to.
478  * @param code: the edns option's code.
479  * @param len: the edns option's length.
480  * @param data: the edns option's data.
481  * @param region: region to allocate the new edns option.
482  * @return false on failure.
483  */
484 int edns_opt_list_append(struct edns_option** list, uint16_t code, size_t len,
485         uint8_t* data, struct regional* region);
486
487 /**
488  * Remove any option found on the edns option list that matches the code.
489  * @param list: the list of edns options.
490  * @param code: the opt code to remove.
491  * @return true when at least one edns option was removed, false otherwise.
492  */
493 int edns_opt_list_remove(struct edns_option** list, uint16_t code);
494
495 /**
496  * Find edns option in edns list
497  * @param list: list of edns options (eg. edns.opt_list)
498  * @param code: opt code to find.
499  * @return NULL or the edns_option element.
500  */
501 struct edns_option* edns_opt_list_find(struct edns_option* list, uint16_t code);
502
503 /**
504  * Call the registered functions in the inplace_cb_reply linked list.
505  * This function is going to get called while answering with a resolved query.
506  * @param env: module environment.
507  * @param qinfo: query info.
508  * @param qstate: module qstate.
509  * @param rep: Reply info. Could be NULL.
510  * @param rcode: return code.
511  * @param edns: edns data of the reply.
512  * @param region: region to store data.
513  * @return false on failure (a callback function returned an error).
514  */
515 int inplace_cb_reply_call(struct module_env* env, struct query_info* qinfo,
516         struct module_qstate* qstate, struct reply_info* rep, int rcode,
517         struct edns_data* edns, struct regional* region);
518
519 /**
520  * Call the registered functions in the inplace_cb_reply_cache linked list.
521  * This function is going to get called while answering from cache.
522  * @param env: module environment.
523  * @param qinfo: query info.
524  * @param qstate: module qstate. NULL when replying from cache.
525  * @param rep: Reply info.
526  * @param rcode: return code.
527  * @param edns: edns data of the reply. Edns input can be found here.
528  * @param region: region to store data.
529  * @return false on failure (a callback function returned an error).
530  */
531 int inplace_cb_reply_cache_call(struct module_env* env,
532         struct query_info* qinfo, struct module_qstate* qstate,
533         struct reply_info* rep, int rcode, struct edns_data* edns,
534         struct regional* region);
535
536 /**
537  * Call the registered functions in the inplace_cb_reply_local linked list.
538  * This function is going to get called while answering with local data.
539  * @param env: module environment.
540  * @param qinfo: query info.
541  * @param qstate: module qstate. NULL when replying from cache.
542  * @param rep: Reply info.
543  * @param rcode: return code.
544  * @param edns: edns data of the reply. Edns input can be found here.
545  * @param region: region to store data.
546  * @return false on failure (a callback function returned an error).
547  */
548 int inplace_cb_reply_local_call(struct module_env* env,
549         struct query_info* qinfo, struct module_qstate* qstate,
550         struct reply_info* rep, int rcode, struct edns_data* edns,
551         struct regional* region);
552
553 /**
554  * Call the registered functions in the inplace_cb_reply linked list.
555  * This function is going to get called while answering with a servfail.
556  * @param env: module environment.
557  * @param qinfo: query info.
558  * @param qstate: module qstate. Contains the edns option lists. Could be NULL.
559  * @param rep: Reply info. NULL when servfail.
560  * @param rcode: return code. LDNS_RCODE_SERVFAIL.
561  * @param edns: edns data of the reply. Edns input can be found here if qstate
562  *      is NULL.
563  * @param region: region to store data.
564  * @return false on failure (a callback function returned an error).
565  */
566 int inplace_cb_reply_servfail_call(struct module_env* env,
567         struct query_info* qinfo, struct module_qstate* qstate,
568         struct reply_info* rep, int rcode, struct edns_data* edns,
569         struct regional* region);
570
571 /**
572  * Call the registered functions in the inplace_cb_query linked list.
573  * This function is going to get called just before sending a query to a
574  * nameserver.
575  * @param env: module environment.
576  * @param qinfo: query info.
577  * @param flags: flags of the query.
578  * @param addr: to which server to send the query.
579  * @param addrlen: length of addr.
580  * @param zone: name of the zone of the delegation point. wireformat dname.
581  *      This is the delegation point name for which the server is deemed
582  *      authoritative.
583  * @param zonelen: length of zone.
584  * @param qstate: module qstate.
585  * @param region: region to store data.
586  * @return false on failure (a callback function returned an error).
587  */
588 int inplace_cb_query_call(struct module_env* env, struct query_info* qinfo,
589         uint16_t flags, struct sockaddr_storage* addr, socklen_t addrlen,
590         uint8_t* zone, size_t zonelen, struct module_qstate* qstate,
591         struct regional* region);
592
593 /**
594  * Copy edns option list allocated to the new region
595  */
596 struct edns_option* edns_opt_copy_region(struct edns_option* list,
597         struct regional* region);
598
599 /**
600  * Copy edns option list allocated with malloc
601  */
602 struct edns_option* edns_opt_copy_alloc(struct edns_option* list);
603
604 /**
605  * Free edns option list allocated with malloc
606  */
607 void edns_opt_list_free(struct edns_option* list);
608
609 /**
610  * Compare an edns option. (not entire list).  Also compares contents.
611  */
612 int edns_opt_compare(struct edns_option* p, struct edns_option* q);
613
614 /**
615  * Compare edns option lists, also the order and contents of edns-options.
616  */
617 int edns_opt_list_compare(struct edns_option* p, struct edns_option* q);
618
619 #endif /* UTIL_DATA_MSGREPLY_H */