/src/unbound/services/cache/rrset.c
Line | Count | Source |
1 | | /* |
2 | | * services/cache/rrset.c - Resource record set cache. |
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 the rrset cache. |
40 | | */ |
41 | | #include "config.h" |
42 | | #include "services/cache/rrset.h" |
43 | | #include "sldns/rrdef.h" |
44 | | #include "util/storage/slabhash.h" |
45 | | #include "util/config_file.h" |
46 | | #include "util/data/packed_rrset.h" |
47 | | #include "util/data/msgreply.h" |
48 | | #include "util/data/msgparse.h" |
49 | | #include "util/data/dname.h" |
50 | | #include "util/regional.h" |
51 | | #include "util/alloc.h" |
52 | | #include "util/net_help.h" |
53 | | #include "validator/val_utils.h" |
54 | | |
55 | | void |
56 | | rrset_markdel(void* key) |
57 | 0 | { |
58 | 0 | struct ub_packed_rrset_key* r = (struct ub_packed_rrset_key*)key; |
59 | 0 | r->id = 0; |
60 | 0 | } |
61 | | |
62 | | struct rrset_cache* rrset_cache_create(struct config_file* cfg, |
63 | | struct alloc_cache* alloc) |
64 | 4.66k | { |
65 | 4.66k | size_t slabs = (cfg?cfg->rrset_cache_slabs:HASH_DEFAULT_SLABS); |
66 | 4.66k | size_t startarray = HASH_DEFAULT_STARTARRAY; |
67 | 4.66k | size_t maxmem = (cfg?cfg->rrset_cache_size:HASH_DEFAULT_MAXMEM); |
68 | | |
69 | 4.66k | struct rrset_cache *r = (struct rrset_cache*)slabhash_create(slabs, |
70 | 4.66k | startarray, maxmem, ub_rrset_sizefunc, ub_rrset_compare, |
71 | 4.66k | ub_rrset_key_delete, rrset_data_delete, alloc); |
72 | 4.66k | if(!r) |
73 | 0 | return NULL; |
74 | 4.66k | slabhash_setmarkdel(&r->table, &rrset_markdel); |
75 | 4.66k | return r; |
76 | 4.66k | } |
77 | | |
78 | | void rrset_cache_delete(struct rrset_cache* r) |
79 | 4.66k | { |
80 | 4.66k | if(!r) |
81 | 0 | return; |
82 | 4.66k | slabhash_delete(&r->table); |
83 | | /* slabhash delete also does free(r), since table is first in struct*/ |
84 | 4.66k | } |
85 | | |
86 | | struct rrset_cache* rrset_cache_adjust(struct rrset_cache *r, |
87 | | struct config_file* cfg, struct alloc_cache* alloc) |
88 | 0 | { |
89 | 0 | if(!r || !cfg || !slabhash_is_size(&r->table, cfg->rrset_cache_size, |
90 | 0 | cfg->rrset_cache_slabs)) |
91 | 0 | { |
92 | 0 | rrset_cache_delete(r); |
93 | 0 | r = rrset_cache_create(cfg, alloc); |
94 | 0 | } |
95 | 0 | return r; |
96 | 0 | } |
97 | | |
98 | | void |
99 | | rrset_cache_touch(struct rrset_cache* r, struct ub_packed_rrset_key* key, |
100 | | hashvalue_type hash, rrset_id_type id) |
101 | 0 | { |
102 | 0 | struct lruhash* table = slabhash_gettable(&r->table, hash); |
103 | | /* |
104 | | * This leads to locking problems, deadlocks, if the caller is |
105 | | * holding any other rrset lock. |
106 | | * Because a lookup through the hashtable does: |
107 | | * tablelock -> entrylock (for that entry caller holds) |
108 | | * And this would do |
109 | | * entrylock(already held) -> tablelock |
110 | | * And if two threads do this, it results in deadlock. |
111 | | * So, the caller must not hold entrylock. |
112 | | */ |
113 | 0 | lock_quick_lock(&table->lock); |
114 | | /* we have locked the hash table, the item can still be deleted. |
115 | | * because it could already have been reclaimed, but not yet set id=0. |
116 | | * This is because some lruhash routines have lazy deletion. |
117 | | * so, we must acquire a lock on the item to verify the id != 0. |
118 | | * also, with hash not changed, we are using the right slab. |
119 | | */ |
120 | 0 | lock_rw_rdlock(&key->entry.lock); |
121 | 0 | if(key->id == id && key->entry.hash == hash) { |
122 | 0 | lru_touch(table, &key->entry); |
123 | 0 | } |
124 | 0 | lock_rw_unlock(&key->entry.lock); |
125 | 0 | lock_quick_unlock(&table->lock); |
126 | 0 | } |
127 | | |
128 | | /** see if rrset needs to be updated in the cache */ |
129 | | static int |
130 | | need_to_update_rrset(void* nd, void* cd, time_t timenow, int equal, int ns, |
131 | | int a_aaaa) |
132 | 0 | { |
133 | 0 | struct packed_rrset_data* newd = (struct packed_rrset_data*)nd; |
134 | 0 | struct packed_rrset_data* cached = (struct packed_rrset_data*)cd; |
135 | | /* o if new data is expired, cached data is better */ |
136 | 0 | if( TTL_IS_EXPIRED(newd->ttl, timenow) && !TTL_IS_EXPIRED(cached->ttl, timenow)) |
137 | 0 | return 0; |
138 | | /* o store if rrset has been validated |
139 | | * everything better than bogus data |
140 | | * secure is preferred */ |
141 | 0 | if( newd->security == sec_status_secure && |
142 | 0 | cached->security != sec_status_secure) |
143 | 0 | return 1; |
144 | 0 | if( cached->security == sec_status_bogus && |
145 | 0 | newd->security != sec_status_bogus && !equal) |
146 | 0 | return 1; |
147 | | /* o if new RRset is more trustworthy - insert it */ |
148 | 0 | if( newd->trust > cached->trust ) { |
149 | | /* if the cached rrset is bogus, and new is equal, |
150 | | * do not update the TTL - let it expire. */ |
151 | 0 | if(equal && !TTL_IS_EXPIRED(cached->ttl, timenow) && |
152 | 0 | cached->security == sec_status_bogus) |
153 | 0 | return 0; |
154 | | /* ghost-domain: never let an NS overwrite extend lifetime |
155 | | * past the entry it replaces, regardless of trust. */ |
156 | | /* Also for A/AAAA and it is glue. */ |
157 | 0 | if((ns || |
158 | 0 | (a_aaaa && cached->trust==rrset_trust_add_noAA)) |
159 | 0 | && !TTL_IS_EXPIRED(cached->ttl, timenow) && |
160 | 0 | newd->ttl > cached->ttl) { |
161 | 0 | size_t i; |
162 | 0 | if(a_aaaa) newd->trust=rrset_trust_add_noAA; |
163 | 0 | newd->ttl = cached->ttl; |
164 | 0 | for(i=0; i<(newd->count+newd->rrsig_count); i++) |
165 | 0 | if(newd->rr_ttl[i] > newd->ttl) |
166 | 0 | newd->rr_ttl[i] = newd->ttl; |
167 | 0 | } |
168 | 0 | return 1; |
169 | 0 | } |
170 | | /* o item in cache has expired */ |
171 | 0 | if( TTL_IS_EXPIRED(cached->ttl, timenow) ) |
172 | 0 | return 1; |
173 | | /* o same trust, but different in data - insert it */ |
174 | 0 | if( newd->trust == cached->trust && !equal ) { |
175 | | /* if this is type NS, do not 'stick' to owner that changes |
176 | | * the NS RRset, but use the cached TTL for the new data, and |
177 | | * update to fetch the latest data. ttl is not expired, because |
178 | | * that check was before this one. */ |
179 | 0 | if(ns) { |
180 | 0 | size_t i; |
181 | 0 | newd->ttl = cached->ttl; |
182 | 0 | for(i=0; i<(newd->count+newd->rrsig_count); i++) |
183 | 0 | if(newd->rr_ttl[i] > newd->ttl) |
184 | 0 | newd->rr_ttl[i] = newd->ttl; |
185 | 0 | } |
186 | 0 | return 1; |
187 | 0 | } |
188 | 0 | return 0; |
189 | 0 | } |
190 | | |
191 | | /** Update RRSet special key ID */ |
192 | | static void |
193 | | rrset_update_id(struct rrset_ref* ref, struct alloc_cache* alloc) |
194 | 0 | { |
195 | | /* this may clear the cache and invalidate lock below */ |
196 | 0 | uint64_t newid = alloc_get_id(alloc); |
197 | | /* obtain writelock */ |
198 | 0 | lock_rw_wrlock(&ref->key->entry.lock); |
199 | | /* check if it was deleted in the meantime, if so, skip update */ |
200 | 0 | if(ref->key->id == ref->id) { |
201 | 0 | ref->key->id = newid; |
202 | 0 | ref->id = newid; |
203 | 0 | } |
204 | 0 | lock_rw_unlock(&ref->key->entry.lock); |
205 | 0 | } |
206 | | |
207 | | int |
208 | | rrset_cache_update(struct rrset_cache* r, struct rrset_ref* ref, |
209 | | struct alloc_cache* alloc, time_t timenow) |
210 | 15.3k | { |
211 | 15.3k | struct lruhash_entry* e; |
212 | 15.3k | struct ub_packed_rrset_key* k = ref->key; |
213 | 15.3k | hashvalue_type h = k->entry.hash; |
214 | 15.3k | uint16_t rrset_type = ntohs(k->rk.type); |
215 | 15.3k | int equal = 0; |
216 | 15.3k | log_assert(ref->id != 0 && k->id != 0); |
217 | 15.3k | log_assert(k->rk.dname != NULL); |
218 | 15.3k | if((k->rk.flags&PACKED_RRSET_0TTL_GRACE) !=0) { |
219 | 0 | log_nametypeclass(VERB_ALGO, "rrset store of PACKED_RRSET_0TTL_GRACE rrset skipped", k->rk.dname, rrset_type, ntohs(k->rk.rrset_class)); |
220 | 0 | ub_packed_rrset_parsedelete(k, alloc); |
221 | 0 | return 0; /* Do not store 0TTL items after apply of |
222 | | the grace ttl amount. |
223 | | This means the ref was not changed by the call. */ |
224 | 0 | } |
225 | | /* looks up item with a readlock - no editing! */ |
226 | 15.3k | if((e=slabhash_lookup(&r->table, h, k, 0)) != 0) { |
227 | | /* return id and key as they will be used in the cache |
228 | | * since the lruhash_insert, if item already exists, deallocs |
229 | | * the passed key in favor of the already stored key. |
230 | | * because of the small gap (see below) this key ptr and id |
231 | | * may prove later to be already deleted, which is no problem |
232 | | * as it only makes a cache miss. |
233 | | */ |
234 | 0 | ref->key = (struct ub_packed_rrset_key*)e->key; |
235 | 0 | ref->id = ref->key->id; |
236 | 0 | equal = rrsetdata_equal((struct packed_rrset_data*)k->entry. |
237 | 0 | data, (struct packed_rrset_data*)e->data); |
238 | 0 | if(!need_to_update_rrset(k->entry.data, e->data, timenow, |
239 | 0 | equal, (rrset_type==LDNS_RR_TYPE_NS), |
240 | 0 | (rrset_type==LDNS_RR_TYPE_A || rrset_type==LDNS_RR_TYPE_AAAA))) { |
241 | | /* cache is superior, return that value */ |
242 | 0 | if(equal) { |
243 | 0 | ub_packed_rrset_parsedelete(k, alloc); |
244 | | /* return readlocked item. */ |
245 | 0 | return 2; |
246 | 0 | } |
247 | 0 | lock_rw_unlock(&e->lock); |
248 | 0 | ub_packed_rrset_parsedelete(k, alloc); |
249 | 0 | return 1; |
250 | 0 | } |
251 | 0 | lock_rw_unlock(&e->lock); |
252 | | /* Go on and insert the passed item. |
253 | | * small gap here, where entry is not locked. |
254 | | * possibly entry is updated with something else. |
255 | | * we then overwrite that with our data. |
256 | | * this is just too bad, its cache anyway. */ |
257 | | /* use insert to update entry to manage lruhash |
258 | | * cache size values nicely. */ |
259 | 0 | } |
260 | 15.3k | log_assert(ref->key->id != 0); |
261 | 15.3k | slabhash_insert(&r->table, h, &k->entry, k->entry.data, alloc); |
262 | 15.3k | if(e) { |
263 | | /* For NSEC, NSEC3, DNAME, when rdata is updated, update |
264 | | * the ID number so that proofs in message cache are |
265 | | * invalidated */ |
266 | 0 | if((rrset_type == LDNS_RR_TYPE_NSEC |
267 | 0 | || rrset_type == LDNS_RR_TYPE_NSEC3 |
268 | 0 | || rrset_type == LDNS_RR_TYPE_DNAME) && !equal) { |
269 | 0 | rrset_update_id(ref, alloc); |
270 | 0 | } |
271 | 0 | return 1; |
272 | 0 | } |
273 | 15.3k | return 0; |
274 | 15.3k | } |
275 | | |
276 | | void rrset_cache_update_unlock(struct rrset_cache* r, struct rrset_ref* ref, |
277 | | struct alloc_cache* alloc, time_t timenow) |
278 | 15.3k | { |
279 | 15.3k | int ret = rrset_cache_update(r, ref, alloc, timenow); |
280 | 15.3k | if(ret == 2) { |
281 | 0 | lock_rw_unlock(&ref->key->entry.lock); |
282 | 0 | } |
283 | 15.3k | } |
284 | | |
285 | | /** See if the name is a within signer authority */ |
286 | | static int |
287 | | dname_subdomain_rrsig_signers(uint8_t* dname, |
288 | | struct ub_packed_rrset_key* rrset) |
289 | 0 | { |
290 | 0 | struct packed_rrset_data* d = (struct packed_rrset_data*) |
291 | 0 | rrset->entry.data; |
292 | 0 | size_t i; |
293 | 0 | if(!d || !d->rrsig_count) |
294 | 0 | return 0; |
295 | 0 | for(i=0; i<d->rrsig_count; i++) { |
296 | 0 | uint8_t* sname = NULL; |
297 | 0 | size_t slen = 0; |
298 | 0 | rrsig_get_signer(d->rr_data[d->count+i], d->rr_len[d->count+i], |
299 | 0 | &sname, &slen); |
300 | 0 | if(!sname || !slen) |
301 | 0 | return 0; /* malformed */ |
302 | 0 | if(!dname_subdomain_c(dname, sname)) |
303 | 0 | return 0; /* not a subdomain */ |
304 | 0 | } |
305 | 0 | return 1; |
306 | 0 | } |
307 | | |
308 | | void rrset_cache_update_wildcard(struct rrset_cache* rrset_cache, |
309 | | struct ub_packed_rrset_key* rrset, uint8_t* ce, size_t ce_len, |
310 | | struct alloc_cache* alloc, time_t timenow) |
311 | 0 | { |
312 | 0 | struct rrset_ref ref; |
313 | 0 | uint8_t wc_dname[LDNS_MAX_DOMAINLEN+3]; |
314 | 0 | uint8_t* new_dname; |
315 | 0 | size_t new_dname_len; |
316 | | |
317 | | /* See if the RRSIG signer name allows this wildcard, |
318 | | * the new rrset should fall within the zone of the RRSIG signer(s). */ |
319 | 0 | if(!dname_subdomain_rrsig_signers(ce, rrset)) { |
320 | 0 | verbose(VERB_ALGO, "wildcard canonical parent outside signer authority"); |
321 | 0 | return; |
322 | 0 | } |
323 | | |
324 | 0 | rrset = packed_rrset_copy_alloc(rrset, alloc, timenow); |
325 | 0 | if(!rrset) { |
326 | 0 | log_err("malloc failure in rrset_cache_update_wildcard"); |
327 | 0 | return; |
328 | 0 | } |
329 | | /* ce has at least one label less then qname, we can therefore safely |
330 | | * add the wildcard label. */ |
331 | 0 | wc_dname[0] = 1; |
332 | 0 | wc_dname[1] = (uint8_t)'*'; |
333 | 0 | memmove(wc_dname+2, ce, ce_len); |
334 | |
|
335 | 0 | new_dname_len = ce_len + 2; |
336 | 0 | new_dname = (uint8_t*)memdup(wc_dname, new_dname_len); |
337 | 0 | if(!new_dname) { |
338 | 0 | ub_packed_rrset_parsedelete(rrset, alloc); |
339 | 0 | log_err("memdup failure in rrset_cache_update_wildcard"); |
340 | 0 | return; |
341 | 0 | } |
342 | 0 | free(rrset->rk.dname); |
343 | 0 | rrset->rk.dname = new_dname; |
344 | 0 | rrset->rk.dname_len = new_dname_len; |
345 | |
|
346 | 0 | rrset->entry.hash = rrset_key_hash(&rrset->rk); |
347 | 0 | ref.key = rrset; |
348 | 0 | ref.id = rrset->id; |
349 | | /* if it was in the cache, ref updated */ |
350 | 0 | rrset_cache_update_unlock(rrset_cache, &ref, alloc, timenow); |
351 | 0 | } |
352 | | |
353 | | /** Grace period in seconds for TTL=0 DNAME rrsets (RFC 2308: do not cache). |
354 | | * Allows synthesis from cache within this window to reduce recursion load. */ |
355 | 0 | #define DNAME_TTL0_GRACE_SECONDS 1 |
356 | | |
357 | | struct ub_packed_rrset_key* |
358 | | rrset_cache_lookup(struct rrset_cache* r, uint8_t* qname, size_t qnamelen, |
359 | | uint16_t qtype, uint16_t qclass, uint32_t flags, time_t timenow, |
360 | | int wr) |
361 | 0 | { |
362 | 0 | struct lruhash_entry* e; |
363 | 0 | struct ub_packed_rrset_key key; |
364 | | |
365 | 0 | key.entry.key = &key; |
366 | 0 | key.entry.data = NULL; |
367 | 0 | key.rk.dname = qname; |
368 | 0 | key.rk.dname_len = qnamelen; |
369 | 0 | key.rk.type = htons(qtype); |
370 | 0 | key.rk.rrset_class = htons(qclass); |
371 | 0 | key.rk.flags = flags; |
372 | |
|
373 | 0 | key.entry.hash = rrset_key_hash(&key.rk); |
374 | |
|
375 | 0 | if((e = slabhash_lookup(&r->table, key.entry.hash, &key, wr))) { |
376 | | /* check TTL */ |
377 | 0 | struct packed_rrset_data* data = |
378 | 0 | (struct packed_rrset_data*)e->data; |
379 | 0 | struct ub_packed_rrset_key* k = (struct ub_packed_rrset_key*)e->key; |
380 | 0 | if(TTL_IS_EXPIRED(data->ttl, timenow)) { |
381 | | /* Allow TTL=0 DNAME within grace period for synthesis */ |
382 | 0 | if(qtype == LDNS_RR_TYPE_DNAME && |
383 | 0 | (k->rk.flags & PACKED_RRSET_UPSTREAM_0TTL) && |
384 | 0 | (timenow - data->ttl_add) <= DNAME_TTL0_GRACE_SECONDS) { |
385 | | /* within grace: allow for synthesis */ |
386 | 0 | } else { |
387 | 0 | lock_rw_unlock(&e->lock); |
388 | 0 | return NULL; |
389 | 0 | } |
390 | 0 | } |
391 | | /* we're done */ |
392 | 0 | return k; |
393 | 0 | } |
394 | 0 | return NULL; |
395 | 0 | } |
396 | | |
397 | | int |
398 | | rrset_array_lock(struct rrset_ref* ref, size_t count, time_t timenow) |
399 | 0 | { |
400 | 0 | size_t i; |
401 | 0 | struct packed_rrset_data* d; |
402 | 0 | for(i=0; i<count; i++) { |
403 | 0 | if(i>0 && ref[i].key == ref[i-1].key) |
404 | 0 | continue; /* only lock items once */ |
405 | 0 | lock_rw_rdlock(&ref[i].key->entry.lock); |
406 | 0 | d = ref[i].key->entry.data; |
407 | 0 | if(ref[i].id != ref[i].key->id || |
408 | 0 | TTL_IS_EXPIRED(d->ttl, timenow)) { |
409 | | /* failure! rollback our readlocks */ |
410 | 0 | rrset_array_unlock(ref, i+1); |
411 | 0 | return 0; |
412 | 0 | } |
413 | 0 | } |
414 | 0 | return 1; |
415 | 0 | } |
416 | | |
417 | | void |
418 | | rrset_array_unlock(struct rrset_ref* ref, size_t count) |
419 | 0 | { |
420 | 0 | size_t i; |
421 | 0 | for(i=0; i<count; i++) { |
422 | 0 | if(i>0 && ref[i].key == ref[i-1].key) |
423 | 0 | continue; /* only unlock items once */ |
424 | 0 | lock_rw_unlock(&ref[i].key->entry.lock); |
425 | 0 | } |
426 | 0 | } |
427 | | |
428 | | void |
429 | | rrset_array_unlock_touch(struct rrset_cache* r, struct regional* scratch, |
430 | | struct rrset_ref* ref, size_t count) |
431 | 0 | { |
432 | 0 | hashvalue_type* h; |
433 | 0 | size_t i; |
434 | 0 | if(count > RR_COUNT_MAX || !(h = (hashvalue_type*)regional_alloc( |
435 | 0 | scratch, sizeof(hashvalue_type)*count))) { |
436 | 0 | log_warn("rrset LRU: memory allocation failed"); |
437 | 0 | h = NULL; |
438 | 0 | } else /* store hash values */ |
439 | 0 | for(i=0; i<count; i++) |
440 | 0 | h[i] = ref[i].key->entry.hash; |
441 | | /* unlock */ |
442 | 0 | for(i=0; i<count; i++) { |
443 | 0 | if(i>0 && ref[i].key == ref[i-1].key) |
444 | 0 | continue; /* only unlock items once */ |
445 | 0 | lock_rw_unlock(&ref[i].key->entry.lock); |
446 | 0 | } |
447 | 0 | if(h) { |
448 | | /* LRU touch, with no rrset locks held */ |
449 | 0 | for(i=0; i<count; i++) { |
450 | 0 | if(i>0 && ref[i].key == ref[i-1].key) |
451 | 0 | continue; /* only touch items once */ |
452 | 0 | rrset_cache_touch(r, ref[i].key, h[i], ref[i].id); |
453 | 0 | } |
454 | 0 | } |
455 | 0 | } |
456 | | |
457 | | void |
458 | | rrset_update_sec_status(struct rrset_cache* r, |
459 | | struct ub_packed_rrset_key* rrset, time_t now) |
460 | 0 | { |
461 | 0 | struct packed_rrset_data* updata = |
462 | 0 | (struct packed_rrset_data*)rrset->entry.data; |
463 | 0 | struct lruhash_entry* e; |
464 | 0 | struct packed_rrset_data* cachedata; |
465 | | |
466 | | /* hash it again to make sure it has a hash */ |
467 | 0 | rrset->entry.hash = rrset_key_hash(&rrset->rk); |
468 | |
|
469 | 0 | e = slabhash_lookup(&r->table, rrset->entry.hash, rrset, 1); |
470 | 0 | if(!e) |
471 | 0 | return; /* not in the cache anymore */ |
472 | 0 | cachedata = (struct packed_rrset_data*)e->data; |
473 | 0 | if(!rrsetdata_equal(updata, cachedata)) { |
474 | 0 | lock_rw_unlock(&e->lock); |
475 | 0 | return; /* rrset has changed in the meantime */ |
476 | 0 | } |
477 | | /* update the cached rrset */ |
478 | 0 | if(updata->security > cachedata->security) { |
479 | 0 | size_t i; |
480 | 0 | if(updata->trust > cachedata->trust) |
481 | 0 | cachedata->trust = updata->trust; |
482 | 0 | cachedata->security = updata->security; |
483 | | /* for NS records only shorter TTLs, other types: update it */ |
484 | 0 | if(ntohs(rrset->rk.type) != LDNS_RR_TYPE_NS || |
485 | 0 | updata->ttl+now < cachedata->ttl || |
486 | 0 | cachedata->ttl < now || |
487 | 0 | updata->security == sec_status_bogus) { |
488 | 0 | cachedata->ttl = updata->ttl + now; |
489 | 0 | for(i=0; i<cachedata->count+cachedata->rrsig_count; i++) |
490 | 0 | cachedata->rr_ttl[i] = updata->rr_ttl[i]+now; |
491 | 0 | cachedata->ttl_add = now; |
492 | 0 | } |
493 | 0 | } |
494 | 0 | lock_rw_unlock(&e->lock); |
495 | 0 | } |
496 | | |
497 | | void |
498 | | rrset_check_sec_status(struct rrset_cache* r, |
499 | | struct ub_packed_rrset_key* rrset, time_t now) |
500 | 0 | { |
501 | 0 | struct packed_rrset_data* updata = |
502 | 0 | (struct packed_rrset_data*)rrset->entry.data; |
503 | 0 | struct lruhash_entry* e; |
504 | 0 | struct packed_rrset_data* cachedata; |
505 | | |
506 | | /* hash it again to make sure it has a hash */ |
507 | 0 | rrset->entry.hash = rrset_key_hash(&rrset->rk); |
508 | |
|
509 | 0 | e = slabhash_lookup(&r->table, rrset->entry.hash, rrset, 0); |
510 | 0 | if(!e) |
511 | 0 | return; /* not in the cache anymore */ |
512 | 0 | cachedata = (struct packed_rrset_data*)e->data; |
513 | 0 | if(now > cachedata->ttl || !rrsetdata_equal(updata, cachedata)) { |
514 | 0 | lock_rw_unlock(&e->lock); |
515 | 0 | return; /* expired, or rrset has changed in the meantime */ |
516 | 0 | } |
517 | 0 | if(cachedata->security > updata->security) { |
518 | 0 | updata->security = cachedata->security; |
519 | 0 | if(cachedata->security == sec_status_bogus) { |
520 | 0 | size_t i; |
521 | 0 | updata->ttl = cachedata->ttl - now; |
522 | 0 | for(i=0; i<cachedata->count+cachedata->rrsig_count; i++) |
523 | 0 | if(cachedata->rr_ttl[i] < now) |
524 | 0 | updata->rr_ttl[i] = 0; |
525 | 0 | else updata->rr_ttl[i] = |
526 | 0 | cachedata->rr_ttl[i]-now; |
527 | 0 | } |
528 | 0 | if(cachedata->trust > updata->trust) |
529 | 0 | updata->trust = cachedata->trust; |
530 | 0 | } |
531 | 0 | lock_rw_unlock(&e->lock); |
532 | 0 | } |
533 | | |
534 | | void |
535 | | rrset_cache_remove_above(struct rrset_cache* r, uint8_t** qname, size_t* |
536 | | qnamelen, uint16_t searchtype, uint16_t qclass, time_t now, uint8_t* |
537 | | qnametop, size_t qnametoplen) |
538 | 0 | { |
539 | 0 | struct ub_packed_rrset_key *rrset; |
540 | 0 | uint8_t lablen; |
541 | |
|
542 | 0 | while(*qnamelen > 0) { |
543 | | /* look one label higher */ |
544 | 0 | lablen = **qname; |
545 | 0 | *qname += lablen + 1; |
546 | 0 | *qnamelen -= lablen + 1; |
547 | 0 | if(*qnamelen <= 0) |
548 | 0 | return; |
549 | | |
550 | | /* stop at qnametop */ |
551 | 0 | if(qnametop && *qnamelen == qnametoplen && |
552 | 0 | query_dname_compare(*qname, qnametop)==0) |
553 | 0 | return; |
554 | | |
555 | 0 | if(verbosity >= VERB_ALGO) { |
556 | | /* looks up with a time of 0, to see expired entries */ |
557 | 0 | if((rrset = rrset_cache_lookup(r, *qname, |
558 | 0 | *qnamelen, searchtype, qclass, 0, 0, 0))) { |
559 | 0 | struct packed_rrset_data* data = |
560 | 0 | (struct packed_rrset_data*)rrset->entry.data; |
561 | 0 | int expired = (now > data->ttl); |
562 | 0 | lock_rw_unlock(&rrset->entry.lock); |
563 | 0 | if(expired) |
564 | 0 | log_nametypeclass(verbosity, "this " |
565 | 0 | "(grand)parent rrset will be " |
566 | 0 | "removed (expired)", |
567 | 0 | *qname, searchtype, qclass); |
568 | 0 | else log_nametypeclass(verbosity, "this " |
569 | 0 | "(grand)parent rrset will be " |
570 | 0 | "removed", |
571 | 0 | *qname, searchtype, qclass); |
572 | 0 | } |
573 | 0 | } |
574 | 0 | rrset_cache_remove(r, *qname, *qnamelen, searchtype, qclass, 0); |
575 | 0 | } |
576 | 0 | } |
577 | | |
578 | | int |
579 | | rrset_cache_expired_above(struct rrset_cache* r, uint8_t** qname, size_t* |
580 | | qnamelen, uint16_t searchtype, uint16_t qclass, time_t now, uint8_t* |
581 | | qnametop, size_t qnametoplen) |
582 | 0 | { |
583 | 0 | struct ub_packed_rrset_key *rrset; |
584 | 0 | uint8_t lablen; |
585 | |
|
586 | 0 | while(*qnamelen > 0) { |
587 | | /* look one label higher */ |
588 | 0 | lablen = **qname; |
589 | 0 | *qname += lablen + 1; |
590 | 0 | *qnamelen -= lablen + 1; |
591 | 0 | if(*qnamelen <= 0) |
592 | 0 | break; |
593 | | |
594 | | /* looks up with a time of 0, to see expired entries */ |
595 | 0 | if((rrset = rrset_cache_lookup(r, *qname, |
596 | 0 | *qnamelen, searchtype, qclass, 0, 0, 0))) { |
597 | 0 | struct packed_rrset_data* data = |
598 | 0 | (struct packed_rrset_data*)rrset->entry.data; |
599 | 0 | if(TTL_IS_EXPIRED(data->ttl, now)) { |
600 | | /* it is expired, this is not wanted */ |
601 | 0 | lock_rw_unlock(&rrset->entry.lock); |
602 | 0 | log_nametypeclass(VERB_ALGO, "this rrset is expired", *qname, searchtype, qclass); |
603 | 0 | return 1; |
604 | 0 | } |
605 | | /* it is not expired, continue looking */ |
606 | 0 | lock_rw_unlock(&rrset->entry.lock); |
607 | 0 | } |
608 | | |
609 | | /* do not look above the qnametop. */ |
610 | 0 | if(qnametop && *qnamelen == qnametoplen && |
611 | 0 | query_dname_compare(*qname, qnametop)==0) |
612 | 0 | break; |
613 | 0 | } |
614 | 0 | return 0; |
615 | 0 | } |
616 | | |
617 | | void rrset_cache_remove(struct rrset_cache* r, uint8_t* nm, size_t nmlen, |
618 | | uint16_t type, uint16_t dclass, uint32_t flags) |
619 | 0 | { |
620 | 0 | struct ub_packed_rrset_key key; |
621 | 0 | key.entry.key = &key; |
622 | 0 | key.rk.dname = nm; |
623 | 0 | key.rk.dname_len = nmlen; |
624 | 0 | key.rk.rrset_class = htons(dclass); |
625 | | key.rk.type = htons(type); |
626 | 0 | key.rk.flags = flags; |
627 | 0 | key.entry.hash = rrset_key_hash(&key.rk); |
628 | 0 | slabhash_remove(&r->table, key.entry.hash, &key); |
629 | 0 | } |