2 * validator/val_neg.h - validator aggressive negative caching functions.
4 * Copyright (c) 2008, 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
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.
39 * This file contains helper functions for the validator module.
40 * The functions help with aggressive negative caching.
41 * This creates new denials of existence, and proofs for absence of types
42 * from cached NSEC records.
45 #ifndef VALIDATOR_VAL_NEG_H
46 #define VALIDATOR_VAL_NEG_H
47 #include "util/locks.h"
48 #include "util/rbtree.h"
57 struct ub_packed_rrset_key;
60 * The negative cache. It is shared between the threads, so locked.
61 * Kept as validator-environ-state. It refers back to the rrset cache for
62 * data elements. It can be out of date and contain conflicting data
63 * from zone content changes.
64 * It contains a tree of zones, every zone has a tree of data elements.
65 * The data elements are part of one big LRU list, with one memory counter.
67 struct val_neg_cache {
68 /** the big lock on the negative cache. Because we use a rbtree
69 * for the data (quick lookup), we need a big lock */
71 /** The zone rbtree. contents sorted canonical, type val_neg_zone */
73 /** the first in linked list of LRU of val_neg_data */
74 struct val_neg_data* first;
75 /** last in lru (least recently used element) */
76 struct val_neg_data* last;
77 /** current memory in use (bytes) */
79 /** max memory to use (bytes) */
81 /** max nsec3 iterations allowed */
82 size_t nsec3_max_iter;
83 /** number of times neg cache records were used to generate NOERROR
85 size_t num_neg_cache_noerror;
86 /** number of times neg cache records were used to generate NXDOMAIN
88 size_t num_neg_cache_nxdomain;
92 * Per Zone aggressive negative caching data.
95 /** rbtree node element, key is this struct: the name, class */
101 /** labels in name */
104 /** pointer to parent zone in the negative cache */
105 struct val_neg_zone* parent;
107 /** the number of elements, including this one and the ones whose
108 * parents (-parents) include this one, that are in_use
109 * No elements have a count of zero, those are removed. */
112 /** if 0: NSEC zone, else NSEC3 hash algorithm in use */
114 /** nsec3 iteration count in use */
116 /** nsec3 salt in use */
118 /** length of salt in bytes */
119 size_t nsec3_saltlen;
121 /** tree of NSEC data for this zone, sorted canonical
122 * by NSEC owner name */
125 /** class of node; host order */
127 /** if this element is in use, boolean */
132 * Data element for aggressive negative caching.
133 * The tree of these elements acts as an index onto the rrset cache.
134 * It shows the NSEC records that (may) exist and are (possibly) secure.
135 * The rbtree allows for logN search for a covering NSEC record.
136 * To make tree insertion and deletion logN too, all the parent (one label
137 * less than the name) data elements are also in the rbtree, with a usage
138 * count for every data element.
139 * There is no actual data stored in this data element, if it is in_use,
140 * then the data can (possibly) be found in the rrset cache.
142 struct val_neg_data {
143 /** rbtree node element, key is this struct: the name */
147 /** length of name */
149 /** labels in name */
152 /** pointer to parent node in the negative cache */
153 struct val_neg_data* parent;
155 /** the number of elements, including this one and the ones whose
156 * parents (-parents) include this one, that are in use
157 * No elements have a count of zero, those are removed. */
160 /** the zone that this denial is part of */
161 struct val_neg_zone* zone;
163 /** previous in LRU */
164 struct val_neg_data* prev;
165 /** next in LRU (next element was less recently used) */
166 struct val_neg_data* next;
168 /** if this element is in use, boolean */
173 * Create negative cache
174 * @param cfg: config options.
175 * @param maxiter: max nsec3 iterations allowed.
176 * @return neg cache, empty or NULL on failure.
178 struct val_neg_cache* val_neg_create(struct config_file* cfg, size_t maxiter);
181 * see how much memory is in use by the negative cache.
182 * @param neg: negative cache
183 * @return number of bytes in use.
185 size_t val_neg_get_mem(struct val_neg_cache* neg);
188 * Destroy negative cache. There must no longer be any other threads.
189 * @param neg: negative cache.
191 void neg_cache_delete(struct val_neg_cache* neg);
194 * Comparison function for rbtree val neg data elements
196 int val_neg_data_compare(const void* a, const void* b);
199 * Comparison function for rbtree val neg zone elements
201 int val_neg_zone_compare(const void* a, const void* b);
204 * Insert NSECs from this message into the negative cache for reference.
205 * @param neg: negative cache
206 * @param rep: reply with NSECs.
207 * Errors are ignored, means that storage is omitted.
209 void val_neg_addreply(struct val_neg_cache* neg, struct reply_info* rep);
212 * Insert NSECs from this referral into the negative cache for reference.
213 * @param neg: negative cache
214 * @param rep: referral reply with NS, NSECs.
215 * @param zone: bailiwick for the referral.
216 * Errors are ignored, means that storage is omitted.
218 void val_neg_addreferral(struct val_neg_cache* neg, struct reply_info* rep,
222 * For the given query, try to get a reply out of the negative cache.
223 * The reply still needs to be validated.
224 * @param neg: negative cache.
225 * @param qinfo: query
226 * @param region: where to allocate reply.
227 * @param rrset_cache: rrset cache.
228 * @param buf: temporary buffer.
229 * @param now: to check TTLs against.
230 * @param addsoa: if true, produce result for external consumption.
231 * if false, do not add SOA - for unbound-internal consumption.
232 * @param topname: do not look higher than this name,
233 * so that the result cannot be taken from a zone above the current
234 * trust anchor. Which could happen with multiple islands of trust.
235 * if NULL, then no trust anchor is used, but also the algorithm becomes
236 * more conservative, especially for opt-out zones, since the receiver
237 * may have a trust-anchor below the optout and thus the optout cannot
238 * be used to create a proof from the negative cache.
239 * @param cfg: config options.
240 * @return a reply message if something was found.
241 * This reply may still need validation.
242 * NULL if nothing found (or out of memory).
244 struct dns_msg* val_neg_getmsg(struct val_neg_cache* neg,
245 struct query_info* qinfo, struct regional* region,
246 struct rrset_cache* rrset_cache, struct sldns_buffer* buf, time_t now,
247 int addsoa, uint8_t* topname, struct config_file* cfg);
250 /**** functions exposed for unit test ****/
252 * Insert data into the data tree of a zone
253 * Does not do locking.
254 * @param neg: negative cache
255 * @param zone: zone to insert into
256 * @param nsec: record to insert.
258 void neg_insert_data(struct val_neg_cache* neg,
259 struct val_neg_zone* zone, struct ub_packed_rrset_key* nsec);
262 * Delete a data element from the negative cache.
263 * May delete other data elements to keep tree coherent, or
264 * only mark the element as 'not in use'.
265 * Does not do locking.
266 * @param neg: negative cache.
267 * @param el: data element to delete.
269 void neg_delete_data(struct val_neg_cache* neg, struct val_neg_data* el);
272 * Find the given zone, from the SOA owner name and class
273 * Does not do locking.
274 * @param neg: negative cache
275 * @param nm: what to look for.
276 * @param len: length of nm
277 * @param dclass: class to look for.
278 * @return zone or NULL if not found.
280 struct val_neg_zone* neg_find_zone(struct val_neg_cache* neg,
281 uint8_t* nm, size_t len, uint16_t dclass);
285 * Does not do locking.
286 * @param neg: negative cache
287 * @param nm: what to look for.
288 * @param nm_len: length of name.
289 * @param dclass: class of zone, host order.
290 * @return zone or NULL if out of memory.
292 struct val_neg_zone* neg_create_zone(struct val_neg_cache* neg,
293 uint8_t* nm, size_t nm_len, uint16_t dclass);
296 * take a zone into use. increases counts of parents.
297 * Does not do locking.
298 * @param zone: zone to take into use.
300 void val_neg_zone_take_inuse(struct val_neg_zone* zone);
302 #endif /* VALIDATOR_VAL_NEG_H */