2 * services/cache/dns.c - Cache services for DNS using msg and rrset caches.
4 * Copyright (c) 2007, 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 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.
39 * This file contains the DNS cache.
42 #include "iterator/iter_delegpt.h"
43 #include "validator/val_nsec.h"
44 #include "services/cache/dns.h"
45 #include "services/cache/rrset.h"
46 #include "util/data/msgreply.h"
47 #include "util/data/packed_rrset.h"
48 #include "util/data/dname.h"
49 #include "util/module.h"
50 #include "util/net_help.h"
51 #include "util/regional.h"
52 #include "util/config_file.h"
54 /** store rrsets in the rrset cache.
55 * @param env: module environment with caches.
56 * @param rep: contains list of rrsets to store.
57 * @param now: current time.
58 * @param leeway: during prefetch how much leeway to update TTLs.
59 * This makes rrsets (other than type NS) timeout sooner so they get
60 * updated with a new full TTL.
61 * Type NS does not get this, because it must not be refreshed from the
62 * child domain, but keep counting down properly.
63 * @param pside: if from parentside discovered NS, so that its NS is okay
64 * in a prefetch situation to be updated (without becoming sticky).
65 * @param qrep: update rrsets here if cache is better
66 * @param region: for qrep allocs.
69 store_rrsets(struct module_env* env, struct reply_info* rep, uint32_t now,
70 uint32_t leeway, int pside, struct reply_info* qrep,
71 struct regional* region)
74 /* see if rrset already exists in cache, if not insert it. */
75 for(i=0; i<rep->rrset_count; i++) {
76 rep->ref[i].key = rep->rrsets[i];
77 rep->ref[i].id = rep->rrsets[i]->id;
78 /* update ref if it was in the cache */
79 switch(rrset_cache_update(env->rrset_cache, &rep->ref[i],
80 env->alloc, now + ((ntohs(rep->ref[i].key->rk.type)==
81 LDNS_RR_TYPE_NS && !pside)?0:leeway))) {
82 case 0: /* ref unchanged, item inserted */
84 case 2: /* ref updated, cache is superior */
86 struct ub_packed_rrset_key* ck;
87 lock_rw_rdlock(&rep->ref[i].key->entry.lock);
88 /* if deleted rrset, do not copy it */
89 if(rep->ref[i].key->id == 0)
91 else ck = packed_rrset_copy_region(
92 rep->ref[i].key, region, now);
93 lock_rw_unlock(&rep->ref[i].key->entry.lock);
95 /* use cached copy if memory allows */
99 /* no break: also copy key item */
100 case 1: /* ref updated, item inserted */
101 rep->rrsets[i] = rep->ref[i].key;
107 dns_cache_store_msg(struct module_env* env, struct query_info* qinfo,
108 hashvalue_t hash, struct reply_info* rep, uint32_t leeway, int pside,
109 struct reply_info* qrep, struct regional* region)
111 struct msgreply_entry* e;
112 uint32_t ttl = rep->ttl;
116 for(i=0; i<rep->rrset_count; i++) {
117 rep->ref[i].key = rep->rrsets[i];
118 rep->ref[i].id = rep->rrsets[i]->id;
121 /* there was a reply_info_sortref(rep) here but it seems to be
122 * unnecessary, because the cache gets locked per rrset. */
123 reply_info_set_ttls(rep, *env->now);
124 store_rrsets(env, rep, *env->now, leeway, pside, qrep, region);
126 /* we do not store the message, but we did store the RRs,
127 * which could be useful for delegation information */
128 verbose(VERB_ALGO, "TTL 0: dropped msg from cache");
133 /* store msg in the cache */
134 reply_info_sortref(rep);
135 if(!(e = query_info_entrysetup(qinfo, rep, hash))) {
136 log_err("store_msg: malloc failed");
139 slabhash_insert(env->msg_cache, hash, &e->entry, rep, env->alloc);
142 /** find closest NS or DNAME and returns the rrset (locked) */
143 static struct ub_packed_rrset_key*
144 find_closest_of_type(struct module_env* env, uint8_t* qname, size_t qnamelen,
145 uint16_t qclass, uint32_t now, uint16_t searchtype, int stripfront)
147 struct ub_packed_rrset_key *rrset;
151 /* strip off so that DNAMEs have strict subdomain match */
154 qnamelen -= lablen + 1;
157 /* snip off front part of qname until the type is found */
158 while(qnamelen > 0) {
159 if((rrset = rrset_cache_lookup(env->rrset_cache, qname,
160 qnamelen, searchtype, qclass, 0, now, 0)))
163 /* snip off front label */
166 qnamelen -= lablen + 1;
171 /** add addr to additional section */
173 addr_to_additional(struct ub_packed_rrset_key* rrset, struct regional* region,
174 struct dns_msg* msg, uint32_t now)
176 if((msg->rep->rrsets[msg->rep->rrset_count] =
177 packed_rrset_copy_region(rrset, region, now))) {
178 msg->rep->ar_numrrsets++;
179 msg->rep->rrset_count++;
183 /** lookup message in message cache */
184 static struct msgreply_entry*
185 msg_cache_lookup(struct module_env* env, uint8_t* qname, size_t qnamelen,
186 uint16_t qtype, uint16_t qclass, uint32_t now, int wr)
188 struct lruhash_entry* e;
193 k.qname_len = qnamelen;
196 h = query_info_hash(&k);
197 e = slabhash_lookup(env->msg_cache, h, &k, wr);
200 if( now > ((struct reply_info*)e->data)->ttl ) {
201 lock_rw_unlock(&e->lock);
204 return (struct msgreply_entry*)e->key;
207 /** find and add A and AAAA records for nameservers in delegpt */
209 find_add_addrs(struct module_env* env, uint16_t qclass,
210 struct regional* region, struct delegpt* dp, uint32_t now,
211 struct dns_msg** msg)
213 struct delegpt_ns* ns;
214 struct msgreply_entry* neg;
215 struct ub_packed_rrset_key* akey;
216 for(ns = dp->nslist; ns; ns = ns->next) {
217 akey = rrset_cache_lookup(env->rrset_cache, ns->name,
218 ns->namelen, LDNS_RR_TYPE_A, qclass, 0, now, 0);
220 if(!delegpt_add_rrset_A(dp, region, akey, 0)) {
221 lock_rw_unlock(&akey->entry.lock);
225 addr_to_additional(akey, region, *msg, now);
226 lock_rw_unlock(&akey->entry.lock);
228 neg = msg_cache_lookup(env, ns->name, ns->namelen,
229 LDNS_RR_TYPE_A, qclass, now, 0);
231 delegpt_add_neg_msg(dp, neg);
232 lock_rw_unlock(&neg->entry.lock);
235 akey = rrset_cache_lookup(env->rrset_cache, ns->name,
236 ns->namelen, LDNS_RR_TYPE_AAAA, qclass, 0, now, 0);
238 if(!delegpt_add_rrset_AAAA(dp, region, akey, 0)) {
239 lock_rw_unlock(&akey->entry.lock);
243 addr_to_additional(akey, region, *msg, now);
244 lock_rw_unlock(&akey->entry.lock);
246 neg = msg_cache_lookup(env, ns->name, ns->namelen,
247 LDNS_RR_TYPE_AAAA, qclass, now, 0);
249 delegpt_add_neg_msg(dp, neg);
250 lock_rw_unlock(&neg->entry.lock);
257 /** find and add A and AAAA records for missing nameservers in delegpt */
259 cache_fill_missing(struct module_env* env, uint16_t qclass,
260 struct regional* region, struct delegpt* dp)
262 struct delegpt_ns* ns;
263 struct msgreply_entry* neg;
264 struct ub_packed_rrset_key* akey;
265 uint32_t now = *env->now;
266 for(ns = dp->nslist; ns; ns = ns->next) {
267 akey = rrset_cache_lookup(env->rrset_cache, ns->name,
268 ns->namelen, LDNS_RR_TYPE_A, qclass, 0, now, 0);
270 if(!delegpt_add_rrset_A(dp, region, akey, (int)ns->lame)) {
271 lock_rw_unlock(&akey->entry.lock);
274 log_nametypeclass(VERB_ALGO, "found in cache",
275 ns->name, LDNS_RR_TYPE_A, qclass);
276 lock_rw_unlock(&akey->entry.lock);
278 neg = msg_cache_lookup(env, ns->name, ns->namelen,
279 LDNS_RR_TYPE_A, qclass, now, 0);
281 delegpt_add_neg_msg(dp, neg);
282 lock_rw_unlock(&neg->entry.lock);
285 akey = rrset_cache_lookup(env->rrset_cache, ns->name,
286 ns->namelen, LDNS_RR_TYPE_AAAA, qclass, 0, now, 0);
288 if(!delegpt_add_rrset_AAAA(dp, region, akey, (int)ns->lame)) {
289 lock_rw_unlock(&akey->entry.lock);
292 log_nametypeclass(VERB_ALGO, "found in cache",
293 ns->name, LDNS_RR_TYPE_AAAA, qclass);
294 lock_rw_unlock(&akey->entry.lock);
296 neg = msg_cache_lookup(env, ns->name, ns->namelen,
297 LDNS_RR_TYPE_AAAA, qclass, now, 0);
299 delegpt_add_neg_msg(dp, neg);
300 lock_rw_unlock(&neg->entry.lock);
307 /** find and add DS or NSEC to delegation msg */
309 find_add_ds(struct module_env* env, struct regional* region,
310 struct dns_msg* msg, struct delegpt* dp, uint32_t now)
312 /* Lookup the DS or NSEC at the delegation point. */
313 struct ub_packed_rrset_key* rrset = rrset_cache_lookup(
314 env->rrset_cache, dp->name, dp->namelen, LDNS_RR_TYPE_DS,
315 msg->qinfo.qclass, 0, now, 0);
317 /* NOTE: this won't work for alternate NSEC schemes
319 rrset = rrset_cache_lookup(env->rrset_cache, dp->name,
320 dp->namelen, LDNS_RR_TYPE_NSEC, msg->qinfo.qclass,
322 /* Note: the PACKED_RRSET_NSEC_AT_APEX flag is not used.
323 * since this is a referral, we need the NSEC at the parent
324 * side of the zone cut, not the NSEC at apex side. */
325 if(rrset && nsec_has_type(rrset, LDNS_RR_TYPE_DS)) {
326 lock_rw_unlock(&rrset->entry.lock);
327 rrset = NULL; /* discard wrong NSEC */
331 /* add it to auth section. This is the second rrset. */
332 if((msg->rep->rrsets[msg->rep->rrset_count] =
333 packed_rrset_copy_region(rrset, region, now))) {
334 msg->rep->ns_numrrsets++;
335 msg->rep->rrset_count++;
337 lock_rw_unlock(&rrset->entry.lock);
342 dns_msg_create(uint8_t* qname, size_t qnamelen, uint16_t qtype,
343 uint16_t qclass, struct regional* region, size_t capacity)
345 struct dns_msg* msg = (struct dns_msg*)regional_alloc(region,
346 sizeof(struct dns_msg));
349 msg->qinfo.qname = regional_alloc_init(region, qname, qnamelen);
350 if(!msg->qinfo.qname)
352 msg->qinfo.qname_len = qnamelen;
353 msg->qinfo.qtype = qtype;
354 msg->qinfo.qclass = qclass;
355 /* non-packed reply_info, because it needs to grow the array */
356 msg->rep = (struct reply_info*)regional_alloc_zero(region,
357 sizeof(struct reply_info)-sizeof(struct rrset_ref));
360 msg->rep->flags = BIT_QR; /* with QR, no AA */
361 msg->rep->qdcount = 1;
362 msg->rep->rrsets = (struct ub_packed_rrset_key**)
363 regional_alloc(region,
364 capacity*sizeof(struct ub_packed_rrset_key*));
365 if(!msg->rep->rrsets)
371 dns_msg_authadd(struct dns_msg* msg, struct regional* region,
372 struct ub_packed_rrset_key* rrset, uint32_t now)
374 if(!(msg->rep->rrsets[msg->rep->rrset_count++] =
375 packed_rrset_copy_region(rrset, region, now)))
377 msg->rep->ns_numrrsets++;
382 dns_cache_find_delegation(struct module_env* env, uint8_t* qname,
383 size_t qnamelen, uint16_t qtype, uint16_t qclass,
384 struct regional* region, struct dns_msg** msg, uint32_t now)
386 /* try to find closest NS rrset */
387 struct ub_packed_rrset_key* nskey;
388 struct packed_rrset_data* nsdata;
391 nskey = find_closest_of_type(env, qname, qnamelen, qclass, now,
393 if(!nskey) /* hope the caller has hints to prime or something */
395 nsdata = (struct packed_rrset_data*)nskey->entry.data;
396 /* got the NS key, create delegation point */
397 dp = delegpt_create(region);
398 if(!dp || !delegpt_set_name(dp, region, nskey->rk.dname)) {
399 lock_rw_unlock(&nskey->entry.lock);
400 log_err("find_delegation: out of memory");
403 /* create referral message */
405 /* allocate the array to as much as we could need:
406 * NS rrset + DS/NSEC rrset +
407 * A rrset for every NS RR
408 * AAAA rrset for every NS RR
410 *msg = dns_msg_create(qname, qnamelen, qtype, qclass, region,
411 2 + nsdata->count*2);
412 if(!*msg || !dns_msg_authadd(*msg, region, nskey, now)) {
413 lock_rw_unlock(&nskey->entry.lock);
414 log_err("find_delegation: out of memory");
418 if(!delegpt_rrset_add_ns(dp, region, nskey, 0))
419 log_err("find_delegation: addns out of memory");
420 lock_rw_unlock(&nskey->entry.lock); /* first unlock before next lookup*/
421 /* find and add DS/NSEC (if any) */
423 find_add_ds(env, region, *msg, dp, now);
424 /* find and add A entries */
425 if(!find_add_addrs(env, qclass, region, dp, now, msg))
426 log_err("find_delegation: addrs out of memory");
430 /** allocate dns_msg from query_info and reply_info */
431 static struct dns_msg*
432 gen_dns_msg(struct regional* region, struct query_info* q, size_t num)
434 struct dns_msg* msg = (struct dns_msg*)regional_alloc(region,
435 sizeof(struct dns_msg));
438 memcpy(&msg->qinfo, q, sizeof(struct query_info));
439 msg->qinfo.qname = regional_alloc_init(region, q->qname, q->qname_len);
440 if(!msg->qinfo.qname)
442 /* allocate replyinfo struct and rrset key array separately */
443 msg->rep = (struct reply_info*)regional_alloc(region,
444 sizeof(struct reply_info) - sizeof(struct rrset_ref));
447 msg->rep->rrsets = (struct ub_packed_rrset_key**)
448 regional_alloc(region,
449 num * sizeof(struct ub_packed_rrset_key*));
450 if(!msg->rep->rrsets)
455 /** generate dns_msg from cached message */
456 static struct dns_msg*
457 tomsg(struct module_env* env, struct query_info* q, struct reply_info* r,
458 struct regional* region, uint32_t now, struct regional* scratch)
464 msg = gen_dns_msg(region, q, r->rrset_count);
467 msg->rep->flags = r->flags;
468 msg->rep->qdcount = r->qdcount;
469 msg->rep->ttl = r->ttl - now;
470 if(r->prefetch_ttl > now)
471 msg->rep->prefetch_ttl = r->prefetch_ttl - now;
472 else msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(msg->rep->ttl);
473 msg->rep->security = r->security;
474 msg->rep->an_numrrsets = r->an_numrrsets;
475 msg->rep->ns_numrrsets = r->ns_numrrsets;
476 msg->rep->ar_numrrsets = r->ar_numrrsets;
477 msg->rep->rrset_count = r->rrset_count;
478 msg->rep->authoritative = r->authoritative;
479 if(!rrset_array_lock(r->ref, r->rrset_count, now))
481 if(r->an_numrrsets > 0 && (r->rrsets[0]->rk.type == htons(
482 LDNS_RR_TYPE_CNAME) || r->rrsets[0]->rk.type == htons(
483 LDNS_RR_TYPE_DNAME)) && !reply_check_cname_chain(r)) {
484 /* cname chain is now invalid, reconstruct msg */
485 rrset_array_unlock(r->ref, r->rrset_count);
488 if(r->security == sec_status_secure && !reply_all_rrsets_secure(r)) {
489 /* message rrsets have changed status, revalidate */
490 rrset_array_unlock(r->ref, r->rrset_count);
493 for(i=0; i<msg->rep->rrset_count; i++) {
494 msg->rep->rrsets[i] = packed_rrset_copy_region(r->rrsets[i],
496 if(!msg->rep->rrsets[i]) {
497 rrset_array_unlock(r->ref, r->rrset_count);
501 rrset_array_unlock_touch(env->rrset_cache, scratch, r->ref,
506 /** synthesize RRset-only response from cached RRset item */
507 static struct dns_msg*
508 rrset_msg(struct ub_packed_rrset_key* rrset, struct regional* region,
509 uint32_t now, struct query_info* q)
512 struct packed_rrset_data* d = (struct packed_rrset_data*)
516 msg = gen_dns_msg(region, q, 1); /* only the CNAME (or other) RRset */
519 msg->rep->flags = BIT_QR; /* reply, no AA, no error */
520 msg->rep->authoritative = 0; /* reply stored in cache can't be authoritative */
521 msg->rep->qdcount = 1;
522 msg->rep->ttl = d->ttl - now;
523 msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(msg->rep->ttl);
524 msg->rep->security = sec_status_unchecked;
525 msg->rep->an_numrrsets = 1;
526 msg->rep->ns_numrrsets = 0;
527 msg->rep->ar_numrrsets = 0;
528 msg->rep->rrset_count = 1;
529 msg->rep->rrsets[0] = packed_rrset_copy_region(rrset, region, now);
530 if(!msg->rep->rrsets[0]) /* copy CNAME */
535 /** synthesize DNAME+CNAME response from cached DNAME item */
536 static struct dns_msg*
537 synth_dname_msg(struct ub_packed_rrset_key* rrset, struct regional* region,
538 uint32_t now, struct query_info* q)
541 struct ub_packed_rrset_key* ck;
542 struct packed_rrset_data* newd, *d = (struct packed_rrset_data*)
544 uint8_t* newname, *dtarg = NULL;
545 size_t newlen, dtarglen;
548 /* only allow validated (with DNSSEC) DNAMEs used from cache
549 * for insecure DNAMEs, query again. */
550 if(d->security != sec_status_secure)
552 msg = gen_dns_msg(region, q, 2); /* DNAME + CNAME RRset */
555 msg->rep->flags = BIT_QR; /* reply, no AA, no error */
556 msg->rep->authoritative = 0; /* reply stored in cache can't be authoritative */
557 msg->rep->qdcount = 1;
558 msg->rep->ttl = d->ttl - now;
559 msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(msg->rep->ttl);
560 msg->rep->security = sec_status_unchecked;
561 msg->rep->an_numrrsets = 1;
562 msg->rep->ns_numrrsets = 0;
563 msg->rep->ar_numrrsets = 0;
564 msg->rep->rrset_count = 1;
565 msg->rep->rrsets[0] = packed_rrset_copy_region(rrset, region, now);
566 if(!msg->rep->rrsets[0]) /* copy DNAME */
568 /* synth CNAME rrset */
569 get_cname_target(rrset, &dtarg, &dtarglen);
572 newlen = q->qname_len + dtarglen - rrset->rk.dname_len;
573 if(newlen > LDNS_MAX_DOMAINLEN) {
574 msg->rep->flags |= LDNS_RCODE_YXDOMAIN;
577 newname = (uint8_t*)regional_alloc(region, newlen);
580 /* new name is concatenation of qname front (without DNAME owner)
581 * and DNAME target name */
582 memcpy(newname, q->qname, q->qname_len-rrset->rk.dname_len);
583 memmove(newname+(q->qname_len-rrset->rk.dname_len), dtarg, dtarglen);
584 /* create rest of CNAME rrset */
585 ck = (struct ub_packed_rrset_key*)regional_alloc(region,
586 sizeof(struct ub_packed_rrset_key));
589 memset(&ck->entry, 0, sizeof(ck->entry));
590 msg->rep->rrsets[1] = ck;
592 ck->rk.type = htons(LDNS_RR_TYPE_CNAME);
593 ck->rk.rrset_class = rrset->rk.rrset_class;
595 ck->rk.dname = regional_alloc_init(region, q->qname, q->qname_len);
598 ck->rk.dname_len = q->qname_len;
599 ck->entry.hash = rrset_key_hash(&ck->rk);
600 newd = (struct packed_rrset_data*)regional_alloc_zero(region,
601 sizeof(struct packed_rrset_data) + sizeof(size_t) +
602 sizeof(uint8_t*) + sizeof(uint32_t) + sizeof(uint16_t)
606 ck->entry.data = newd;
607 newd->ttl = 0; /* 0 for synthesized CNAME TTL */
609 newd->rrsig_count = 0;
610 newd->trust = rrset_trust_ans_noAA;
611 newd->rr_len = (size_t*)((uint8_t*)newd +
612 sizeof(struct packed_rrset_data));
613 newd->rr_len[0] = newlen + sizeof(uint16_t);
614 packed_rrset_ptr_fixup(newd);
615 newd->rr_ttl[0] = newd->ttl;
616 msg->rep->ttl = newd->ttl;
617 msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(newd->ttl);
618 ldns_write_uint16(newd->rr_data[0], newlen);
619 memmove(newd->rr_data[0] + sizeof(uint16_t), newname, newlen);
620 msg->rep->an_numrrsets ++;
621 msg->rep->rrset_count ++;
626 dns_cache_lookup(struct module_env* env,
627 uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass,
628 struct regional* region, struct regional* scratch)
630 struct lruhash_entry* e;
633 uint32_t now = *env->now;
634 struct ub_packed_rrset_key* rrset;
636 /* lookup first, this has both NXdomains and ANSWER responses */
638 k.qname_len = qnamelen;
641 h = query_info_hash(&k);
642 e = slabhash_lookup(env->msg_cache, h, &k, 0);
644 struct msgreply_entry* key = (struct msgreply_entry*)e->key;
645 struct reply_info* data = (struct reply_info*)e->data;
646 struct dns_msg* msg = tomsg(env, &key->key, data, region, now,
649 lock_rw_unlock(&e->lock);
652 /* could be msg==NULL; due to TTL or not all rrsets available */
653 lock_rw_unlock(&e->lock);
656 /* see if a DNAME exists. Checked for first, to enforce that DNAMEs
657 * are more important, the CNAME is resynthesized and thus
658 * consistent with the DNAME */
659 if( (rrset=find_closest_of_type(env, qname, qnamelen, qclass, now,
660 LDNS_RR_TYPE_DNAME, 1))) {
661 /* synthesize a DNAME+CNAME message based on this */
662 struct dns_msg* msg = synth_dname_msg(rrset, region, now, &k);
664 lock_rw_unlock(&rrset->entry.lock);
667 lock_rw_unlock(&rrset->entry.lock);
670 /* see if we have CNAME for this domain,
671 * but not for DS records (which are part of the parent) */
672 if( qtype != LDNS_RR_TYPE_DS &&
673 (rrset=rrset_cache_lookup(env->rrset_cache, qname, qnamelen,
674 LDNS_RR_TYPE_CNAME, qclass, 0, now, 0))) {
675 struct dns_msg* msg = rrset_msg(rrset, region, now, &k);
677 lock_rw_unlock(&rrset->entry.lock);
680 lock_rw_unlock(&rrset->entry.lock);
683 /* construct DS, DNSKEY, DLV messages from rrset cache. */
684 if((qtype == LDNS_RR_TYPE_DS || qtype == LDNS_RR_TYPE_DNSKEY ||
685 qtype == LDNS_RR_TYPE_DLV) &&
686 (rrset=rrset_cache_lookup(env->rrset_cache, qname, qnamelen,
687 qtype, qclass, 0, now, 0))) {
688 /* if the rrset is from the additional section, and the
689 * signatures have fallen off, then do not synthesize a msg
690 * instead, allow a full query for signed results to happen.
691 * Forego all rrset data from additional section, because
692 * some signatures may not be present and cause validation
695 struct packed_rrset_data *d = (struct packed_rrset_data*)
697 if(d->trust != rrset_trust_add_noAA &&
698 d->trust != rrset_trust_add_AA &&
699 (qtype == LDNS_RR_TYPE_DS ||
700 (d->trust != rrset_trust_auth_noAA
701 && d->trust != rrset_trust_auth_AA) )) {
702 struct dns_msg* msg = rrset_msg(rrset, region, now, &k);
704 lock_rw_unlock(&rrset->entry.lock);
708 lock_rw_unlock(&rrset->entry.lock);
711 /* stop downwards cache search on NXDOMAIN.
712 * Empty nonterminals are NOERROR, so an NXDOMAIN for foo
713 * means bla.foo also does not exist. The DNSSEC proofs are
714 * the same. We search upwards for NXDOMAINs. */
715 if(env->cfg->harden_below_nxdomain)
716 while(!dname_is_root(k.qname)) {
717 dname_remove_label(&k.qname, &k.qname_len);
718 h = query_info_hash(&k);
719 e = slabhash_lookup(env->msg_cache, h, &k, 0);
721 struct reply_info* data = (struct reply_info*)e->data;
723 if(FLAGS_GET_RCODE(data->flags) == LDNS_RCODE_NXDOMAIN
724 && data->security == sec_status_secure
725 && (msg=tomsg(env, &k, data, region, now, scratch))){
726 lock_rw_unlock(&e->lock);
727 msg->qinfo.qname=qname;
728 msg->qinfo.qname_len=qnamelen;
729 /* check that DNSSEC really works out */
730 msg->rep->security = sec_status_unchecked;
733 lock_rw_unlock(&e->lock);
741 dns_cache_store(struct module_env* env, struct query_info* msgqinf,
742 struct reply_info* msgrep, int is_referral, uint32_t leeway, int pside,
743 struct regional* region)
745 struct reply_info* rep = NULL;
746 /* alloc, malloc properly (not in region, like msg is) */
747 rep = reply_info_copy(msgrep, env->alloc, NULL);
750 /* ttl must be relative ;i.e. 0..86400 not time(0)+86400.
751 * the env->now is added to message and RRsets in this routine. */
752 /* the leeway is used to invalidate other rrsets earlier */
756 struct rrset_ref ref;
758 for(i=0; i<rep->rrset_count; i++) {
759 packed_rrset_ttl_add((struct packed_rrset_data*)
760 rep->rrsets[i]->entry.data, *env->now);
761 ref.key = rep->rrsets[i];
762 ref.id = rep->rrsets[i]->id;
763 /*ignore ret: it was in the cache, ref updated */
764 /* no leeway for typeNS */
765 (void)rrset_cache_update(env->rrset_cache, &ref,
766 env->alloc, *env->now +
767 ((ntohs(ref.key->rk.type)==LDNS_RR_TYPE_NS
768 && !pside) ? 0:leeway));
773 /* store msg, and rrsets */
774 struct query_info qinf;
778 qinf.qname = memdup(msgqinf->qname, msgqinf->qname_len);
780 reply_info_parsedelete(rep, env->alloc);
783 /* fixup flags to be sensible for a reply based on the cache */
784 /* this module means that RA is available. It is an answer QR.
785 * Not AA from cache. Not CD in cache (depends on client bit). */
786 rep->flags |= (BIT_RA | BIT_QR);
787 rep->flags &= ~(BIT_AA | BIT_CD);
788 h = query_info_hash(&qinf);
789 dns_cache_store_msg(env, &qinf, h, rep, leeway, pside, msgrep,
791 /* qname is used inside query_info_entrysetup, and set to
792 * NULL. If it has not been used, free it. free(0) is safe. */