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
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 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"
53 #include "sldns/sbuffer.h"
55 /** store rrsets in the rrset cache.
56 * @param env: module environment with caches.
57 * @param rep: contains list of rrsets to store.
58 * @param now: current time.
59 * @param leeway: during prefetch how much leeway to update TTLs.
60 * This makes rrsets (other than type NS) timeout sooner so they get
61 * updated with a new full TTL.
62 * Type NS does not get this, because it must not be refreshed from the
63 * child domain, but keep counting down properly.
64 * @param pside: if from parentside discovered NS, so that its NS is okay
65 * in a prefetch situation to be updated (without becoming sticky).
66 * @param qrep: update rrsets here if cache is better
67 * @param region: for qrep allocs.
70 store_rrsets(struct module_env* env, struct reply_info* rep, time_t now,
71 time_t leeway, int pside, struct reply_info* qrep,
72 struct regional* region)
75 /* see if rrset already exists in cache, if not insert it. */
76 for(i=0; i<rep->rrset_count; i++) {
77 rep->ref[i].key = rep->rrsets[i];
78 rep->ref[i].id = rep->rrsets[i]->id;
79 /* update ref if it was in the cache */
80 switch(rrset_cache_update(env->rrset_cache, &rep->ref[i],
81 env->alloc, now + ((ntohs(rep->ref[i].key->rk.type)==
82 LDNS_RR_TYPE_NS && !pside)?0:leeway))) {
83 case 0: /* ref unchanged, item inserted */
85 case 2: /* ref updated, cache is superior */
87 struct ub_packed_rrset_key* ck;
88 lock_rw_rdlock(&rep->ref[i].key->entry.lock);
89 /* if deleted rrset, do not copy it */
90 if(rep->ref[i].key->id == 0)
92 else ck = packed_rrset_copy_region(
93 rep->ref[i].key, region, now);
94 lock_rw_unlock(&rep->ref[i].key->entry.lock);
96 /* use cached copy if memory allows */
100 /* no break: also copy key item */
101 case 1: /* ref updated, item inserted */
102 rep->rrsets[i] = rep->ref[i].key;
108 dns_cache_store_msg(struct module_env* env, struct query_info* qinfo,
109 hashvalue_t hash, struct reply_info* rep, time_t leeway, int pside,
110 struct reply_info* qrep, struct regional* region)
112 struct msgreply_entry* e;
113 time_t ttl = rep->ttl;
117 for(i=0; i<rep->rrset_count; i++) {
118 rep->ref[i].key = rep->rrsets[i];
119 rep->ref[i].id = rep->rrsets[i]->id;
122 /* there was a reply_info_sortref(rep) here but it seems to be
123 * unnecessary, because the cache gets locked per rrset. */
124 reply_info_set_ttls(rep, *env->now);
125 store_rrsets(env, rep, *env->now, leeway, pside, qrep, region);
127 /* we do not store the message, but we did store the RRs,
128 * which could be useful for delegation information */
129 verbose(VERB_ALGO, "TTL 0: dropped msg from cache");
134 /* store msg in the cache */
135 reply_info_sortref(rep);
136 if(!(e = query_info_entrysetup(qinfo, rep, hash))) {
137 log_err("store_msg: malloc failed");
140 slabhash_insert(env->msg_cache, hash, &e->entry, rep, env->alloc);
143 /** find closest NS or DNAME and returns the rrset (locked) */
144 static struct ub_packed_rrset_key*
145 find_closest_of_type(struct module_env* env, uint8_t* qname, size_t qnamelen,
146 uint16_t qclass, time_t now, uint16_t searchtype, int stripfront)
148 struct ub_packed_rrset_key *rrset;
152 /* strip off so that DNAMEs have strict subdomain match */
155 qnamelen -= lablen + 1;
158 /* snip off front part of qname until the type is found */
159 while(qnamelen > 0) {
160 if((rrset = rrset_cache_lookup(env->rrset_cache, qname,
161 qnamelen, searchtype, qclass, 0, now, 0)))
164 /* snip off front label */
167 qnamelen -= lablen + 1;
172 /** add addr to additional section */
174 addr_to_additional(struct ub_packed_rrset_key* rrset, struct regional* region,
175 struct dns_msg* msg, time_t now)
177 if((msg->rep->rrsets[msg->rep->rrset_count] =
178 packed_rrset_copy_region(rrset, region, now))) {
179 msg->rep->ar_numrrsets++;
180 msg->rep->rrset_count++;
184 /** lookup message in message cache */
185 static struct msgreply_entry*
186 msg_cache_lookup(struct module_env* env, uint8_t* qname, size_t qnamelen,
187 uint16_t qtype, uint16_t qclass, uint16_t flags, time_t now, int wr)
189 struct lruhash_entry* e;
194 k.qname_len = qnamelen;
197 h = query_info_hash(&k, flags);
198 e = slabhash_lookup(env->msg_cache, h, &k, wr);
201 if( now > ((struct reply_info*)e->data)->ttl ) {
202 lock_rw_unlock(&e->lock);
205 return (struct msgreply_entry*)e->key;
208 /** find and add A and AAAA records for nameservers in delegpt */
210 find_add_addrs(struct module_env* env, uint16_t qclass,
211 struct regional* region, struct delegpt* dp, time_t now,
212 struct dns_msg** msg)
214 struct delegpt_ns* ns;
215 struct msgreply_entry* neg;
216 struct ub_packed_rrset_key* akey;
217 for(ns = dp->nslist; ns; ns = ns->next) {
218 akey = rrset_cache_lookup(env->rrset_cache, ns->name,
219 ns->namelen, LDNS_RR_TYPE_A, qclass, 0, now, 0);
221 if(!delegpt_add_rrset_A(dp, region, akey, 0)) {
222 lock_rw_unlock(&akey->entry.lock);
226 addr_to_additional(akey, region, *msg, now);
227 lock_rw_unlock(&akey->entry.lock);
229 /* BIT_CD on false because delegpt lookup does
230 * not use dns64 translation */
231 neg = msg_cache_lookup(env, ns->name, ns->namelen,
232 LDNS_RR_TYPE_A, qclass, 0, now, 0);
234 delegpt_add_neg_msg(dp, neg);
235 lock_rw_unlock(&neg->entry.lock);
238 akey = rrset_cache_lookup(env->rrset_cache, ns->name,
239 ns->namelen, LDNS_RR_TYPE_AAAA, qclass, 0, now, 0);
241 if(!delegpt_add_rrset_AAAA(dp, region, akey, 0)) {
242 lock_rw_unlock(&akey->entry.lock);
246 addr_to_additional(akey, region, *msg, now);
247 lock_rw_unlock(&akey->entry.lock);
249 /* BIT_CD on false because delegpt lookup does
250 * not use dns64 translation */
251 neg = msg_cache_lookup(env, ns->name, ns->namelen,
252 LDNS_RR_TYPE_AAAA, qclass, 0, now, 0);
254 delegpt_add_neg_msg(dp, neg);
255 lock_rw_unlock(&neg->entry.lock);
262 /** find and add A and AAAA records for missing nameservers in delegpt */
264 cache_fill_missing(struct module_env* env, uint16_t qclass,
265 struct regional* region, struct delegpt* dp)
267 struct delegpt_ns* ns;
268 struct msgreply_entry* neg;
269 struct ub_packed_rrset_key* akey;
270 time_t now = *env->now;
271 for(ns = dp->nslist; ns; ns = ns->next) {
272 akey = rrset_cache_lookup(env->rrset_cache, ns->name,
273 ns->namelen, LDNS_RR_TYPE_A, qclass, 0, now, 0);
275 if(!delegpt_add_rrset_A(dp, region, akey, ns->lame)) {
276 lock_rw_unlock(&akey->entry.lock);
279 log_nametypeclass(VERB_ALGO, "found in cache",
280 ns->name, LDNS_RR_TYPE_A, qclass);
281 lock_rw_unlock(&akey->entry.lock);
283 /* BIT_CD on false because delegpt lookup does
284 * not use dns64 translation */
285 neg = msg_cache_lookup(env, ns->name, ns->namelen,
286 LDNS_RR_TYPE_A, qclass, 0, now, 0);
288 delegpt_add_neg_msg(dp, neg);
289 lock_rw_unlock(&neg->entry.lock);
292 akey = rrset_cache_lookup(env->rrset_cache, ns->name,
293 ns->namelen, LDNS_RR_TYPE_AAAA, qclass, 0, now, 0);
295 if(!delegpt_add_rrset_AAAA(dp, region, akey, ns->lame)) {
296 lock_rw_unlock(&akey->entry.lock);
299 log_nametypeclass(VERB_ALGO, "found in cache",
300 ns->name, LDNS_RR_TYPE_AAAA, qclass);
301 lock_rw_unlock(&akey->entry.lock);
303 /* BIT_CD on false because delegpt lookup does
304 * not use dns64 translation */
305 neg = msg_cache_lookup(env, ns->name, ns->namelen,
306 LDNS_RR_TYPE_AAAA, qclass, 0, now, 0);
308 delegpt_add_neg_msg(dp, neg);
309 lock_rw_unlock(&neg->entry.lock);
316 /** find and add DS or NSEC to delegation msg */
318 find_add_ds(struct module_env* env, struct regional* region,
319 struct dns_msg* msg, struct delegpt* dp, time_t now)
321 /* Lookup the DS or NSEC at the delegation point. */
322 struct ub_packed_rrset_key* rrset = rrset_cache_lookup(
323 env->rrset_cache, dp->name, dp->namelen, LDNS_RR_TYPE_DS,
324 msg->qinfo.qclass, 0, now, 0);
326 /* NOTE: this won't work for alternate NSEC schemes
328 rrset = rrset_cache_lookup(env->rrset_cache, dp->name,
329 dp->namelen, LDNS_RR_TYPE_NSEC, msg->qinfo.qclass,
331 /* Note: the PACKED_RRSET_NSEC_AT_APEX flag is not used.
332 * since this is a referral, we need the NSEC at the parent
333 * side of the zone cut, not the NSEC at apex side. */
334 if(rrset && nsec_has_type(rrset, LDNS_RR_TYPE_DS)) {
335 lock_rw_unlock(&rrset->entry.lock);
336 rrset = NULL; /* discard wrong NSEC */
340 /* add it to auth section. This is the second rrset. */
341 if((msg->rep->rrsets[msg->rep->rrset_count] =
342 packed_rrset_copy_region(rrset, region, now))) {
343 msg->rep->ns_numrrsets++;
344 msg->rep->rrset_count++;
346 lock_rw_unlock(&rrset->entry.lock);
351 dns_msg_create(uint8_t* qname, size_t qnamelen, uint16_t qtype,
352 uint16_t qclass, struct regional* region, size_t capacity)
354 struct dns_msg* msg = (struct dns_msg*)regional_alloc(region,
355 sizeof(struct dns_msg));
358 msg->qinfo.qname = regional_alloc_init(region, qname, qnamelen);
359 if(!msg->qinfo.qname)
361 msg->qinfo.qname_len = qnamelen;
362 msg->qinfo.qtype = qtype;
363 msg->qinfo.qclass = qclass;
364 /* non-packed reply_info, because it needs to grow the array */
365 msg->rep = (struct reply_info*)regional_alloc_zero(region,
366 sizeof(struct reply_info)-sizeof(struct rrset_ref));
369 if(capacity > RR_COUNT_MAX)
370 return NULL; /* integer overflow protection */
371 msg->rep->flags = BIT_QR; /* with QR, no AA */
372 msg->rep->qdcount = 1;
373 msg->rep->rrsets = (struct ub_packed_rrset_key**)
374 regional_alloc(region,
375 capacity*sizeof(struct ub_packed_rrset_key*));
376 if(!msg->rep->rrsets)
382 dns_msg_authadd(struct dns_msg* msg, struct regional* region,
383 struct ub_packed_rrset_key* rrset, time_t now)
385 if(!(msg->rep->rrsets[msg->rep->rrset_count++] =
386 packed_rrset_copy_region(rrset, region, now)))
388 msg->rep->ns_numrrsets++;
392 /** add rrset to answer section */
394 dns_msg_ansadd(struct dns_msg* msg, struct regional* region,
395 struct ub_packed_rrset_key* rrset, time_t now)
397 if(!(msg->rep->rrsets[msg->rep->rrset_count++] =
398 packed_rrset_copy_region(rrset, region, now)))
400 msg->rep->an_numrrsets++;
405 dns_cache_find_delegation(struct module_env* env, uint8_t* qname,
406 size_t qnamelen, uint16_t qtype, uint16_t qclass,
407 struct regional* region, struct dns_msg** msg, time_t now)
409 /* try to find closest NS rrset */
410 struct ub_packed_rrset_key* nskey;
411 struct packed_rrset_data* nsdata;
414 nskey = find_closest_of_type(env, qname, qnamelen, qclass, now,
416 if(!nskey) /* hope the caller has hints to prime or something */
418 nsdata = (struct packed_rrset_data*)nskey->entry.data;
419 /* got the NS key, create delegation point */
420 dp = delegpt_create(region);
421 if(!dp || !delegpt_set_name(dp, region, nskey->rk.dname)) {
422 lock_rw_unlock(&nskey->entry.lock);
423 log_err("find_delegation: out of memory");
426 /* create referral message */
428 /* allocate the array to as much as we could need:
429 * NS rrset + DS/NSEC rrset +
430 * A rrset for every NS RR
431 * AAAA rrset for every NS RR
433 *msg = dns_msg_create(qname, qnamelen, qtype, qclass, region,
434 2 + nsdata->count*2);
435 if(!*msg || !dns_msg_authadd(*msg, region, nskey, now)) {
436 lock_rw_unlock(&nskey->entry.lock);
437 log_err("find_delegation: out of memory");
441 if(!delegpt_rrset_add_ns(dp, region, nskey, 0))
442 log_err("find_delegation: addns out of memory");
443 lock_rw_unlock(&nskey->entry.lock); /* first unlock before next lookup*/
444 /* find and add DS/NSEC (if any) */
446 find_add_ds(env, region, *msg, dp, now);
447 /* find and add A entries */
448 if(!find_add_addrs(env, qclass, region, dp, now, msg))
449 log_err("find_delegation: addrs out of memory");
453 /** allocate dns_msg from query_info and reply_info */
454 static struct dns_msg*
455 gen_dns_msg(struct regional* region, struct query_info* q, size_t num)
457 struct dns_msg* msg = (struct dns_msg*)regional_alloc(region,
458 sizeof(struct dns_msg));
461 memcpy(&msg->qinfo, q, sizeof(struct query_info));
462 msg->qinfo.qname = regional_alloc_init(region, q->qname, q->qname_len);
463 if(!msg->qinfo.qname)
465 /* allocate replyinfo struct and rrset key array separately */
466 msg->rep = (struct reply_info*)regional_alloc(region,
467 sizeof(struct reply_info) - sizeof(struct rrset_ref));
470 if(num > RR_COUNT_MAX)
471 return NULL; /* integer overflow protection */
472 msg->rep->rrsets = (struct ub_packed_rrset_key**)
473 regional_alloc(region,
474 num * sizeof(struct ub_packed_rrset_key*));
475 if(!msg->rep->rrsets)
480 /** generate dns_msg from cached message */
481 static struct dns_msg*
482 tomsg(struct module_env* env, struct query_info* q, struct reply_info* r,
483 struct regional* region, time_t now, struct regional* scratch)
489 msg = gen_dns_msg(region, q, r->rrset_count);
492 msg->rep->flags = r->flags;
493 msg->rep->qdcount = r->qdcount;
494 msg->rep->ttl = r->ttl - now;
495 if(r->prefetch_ttl > now)
496 msg->rep->prefetch_ttl = r->prefetch_ttl - now;
497 else msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(msg->rep->ttl);
498 msg->rep->security = r->security;
499 msg->rep->an_numrrsets = r->an_numrrsets;
500 msg->rep->ns_numrrsets = r->ns_numrrsets;
501 msg->rep->ar_numrrsets = r->ar_numrrsets;
502 msg->rep->rrset_count = r->rrset_count;
503 msg->rep->authoritative = r->authoritative;
504 if(!rrset_array_lock(r->ref, r->rrset_count, now))
506 if(r->an_numrrsets > 0 && (r->rrsets[0]->rk.type == htons(
507 LDNS_RR_TYPE_CNAME) || r->rrsets[0]->rk.type == htons(
508 LDNS_RR_TYPE_DNAME)) && !reply_check_cname_chain(q, r)) {
509 /* cname chain is now invalid, reconstruct msg */
510 rrset_array_unlock(r->ref, r->rrset_count);
513 if(r->security == sec_status_secure && !reply_all_rrsets_secure(r)) {
514 /* message rrsets have changed status, revalidate */
515 rrset_array_unlock(r->ref, r->rrset_count);
518 for(i=0; i<msg->rep->rrset_count; i++) {
519 msg->rep->rrsets[i] = packed_rrset_copy_region(r->rrsets[i],
521 if(!msg->rep->rrsets[i]) {
522 rrset_array_unlock(r->ref, r->rrset_count);
526 rrset_array_unlock_touch(env->rrset_cache, scratch, r->ref,
531 /** synthesize RRset-only response from cached RRset item */
532 static struct dns_msg*
533 rrset_msg(struct ub_packed_rrset_key* rrset, struct regional* region,
534 time_t now, struct query_info* q)
537 struct packed_rrset_data* d = (struct packed_rrset_data*)
541 msg = gen_dns_msg(region, q, 1); /* only the CNAME (or other) RRset */
544 msg->rep->flags = BIT_QR; /* reply, no AA, no error */
545 msg->rep->authoritative = 0; /* reply stored in cache can't be authoritative */
546 msg->rep->qdcount = 1;
547 msg->rep->ttl = d->ttl - now;
548 msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(msg->rep->ttl);
549 msg->rep->security = sec_status_unchecked;
550 msg->rep->an_numrrsets = 1;
551 msg->rep->ns_numrrsets = 0;
552 msg->rep->ar_numrrsets = 0;
553 msg->rep->rrset_count = 1;
554 msg->rep->rrsets[0] = packed_rrset_copy_region(rrset, region, now);
555 if(!msg->rep->rrsets[0]) /* copy CNAME */
560 /** synthesize DNAME+CNAME response from cached DNAME item */
561 static struct dns_msg*
562 synth_dname_msg(struct ub_packed_rrset_key* rrset, struct regional* region,
563 time_t now, struct query_info* q)
566 struct ub_packed_rrset_key* ck;
567 struct packed_rrset_data* newd, *d = (struct packed_rrset_data*)
569 uint8_t* newname, *dtarg = NULL;
570 size_t newlen, dtarglen;
573 /* only allow validated (with DNSSEC) DNAMEs used from cache
574 * for insecure DNAMEs, query again. */
575 if(d->security != sec_status_secure)
577 msg = gen_dns_msg(region, q, 2); /* DNAME + CNAME RRset */
580 msg->rep->flags = BIT_QR; /* reply, no AA, no error */
581 msg->rep->authoritative = 0; /* reply stored in cache can't be authoritative */
582 msg->rep->qdcount = 1;
583 msg->rep->ttl = d->ttl - now;
584 msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(msg->rep->ttl);
585 msg->rep->security = sec_status_unchecked;
586 msg->rep->an_numrrsets = 1;
587 msg->rep->ns_numrrsets = 0;
588 msg->rep->ar_numrrsets = 0;
589 msg->rep->rrset_count = 1;
590 msg->rep->rrsets[0] = packed_rrset_copy_region(rrset, region, now);
591 if(!msg->rep->rrsets[0]) /* copy DNAME */
593 /* synth CNAME rrset */
594 get_cname_target(rrset, &dtarg, &dtarglen);
597 newlen = q->qname_len + dtarglen - rrset->rk.dname_len;
598 if(newlen > LDNS_MAX_DOMAINLEN) {
599 msg->rep->flags |= LDNS_RCODE_YXDOMAIN;
602 newname = (uint8_t*)regional_alloc(region, newlen);
605 /* new name is concatenation of qname front (without DNAME owner)
606 * and DNAME target name */
607 memcpy(newname, q->qname, q->qname_len-rrset->rk.dname_len);
608 memmove(newname+(q->qname_len-rrset->rk.dname_len), dtarg, dtarglen);
609 /* create rest of CNAME rrset */
610 ck = (struct ub_packed_rrset_key*)regional_alloc(region,
611 sizeof(struct ub_packed_rrset_key));
614 memset(&ck->entry, 0, sizeof(ck->entry));
615 msg->rep->rrsets[1] = ck;
617 ck->rk.type = htons(LDNS_RR_TYPE_CNAME);
618 ck->rk.rrset_class = rrset->rk.rrset_class;
620 ck->rk.dname = regional_alloc_init(region, q->qname, q->qname_len);
623 ck->rk.dname_len = q->qname_len;
624 ck->entry.hash = rrset_key_hash(&ck->rk);
625 newd = (struct packed_rrset_data*)regional_alloc_zero(region,
626 sizeof(struct packed_rrset_data) + sizeof(size_t) +
627 sizeof(uint8_t*) + sizeof(time_t) + sizeof(uint16_t)
631 ck->entry.data = newd;
632 newd->ttl = 0; /* 0 for synthesized CNAME TTL */
634 newd->rrsig_count = 0;
635 newd->trust = rrset_trust_ans_noAA;
636 newd->rr_len = (size_t*)((uint8_t*)newd +
637 sizeof(struct packed_rrset_data));
638 newd->rr_len[0] = newlen + sizeof(uint16_t);
639 packed_rrset_ptr_fixup(newd);
640 newd->rr_ttl[0] = newd->ttl;
641 msg->rep->ttl = newd->ttl;
642 msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(newd->ttl);
643 sldns_write_uint16(newd->rr_data[0], newlen);
644 memmove(newd->rr_data[0] + sizeof(uint16_t), newname, newlen);
645 msg->rep->an_numrrsets ++;
646 msg->rep->rrset_count ++;
650 /** Fill TYPE_ANY response with some data from cache */
651 static struct dns_msg*
652 fill_any(struct module_env* env,
653 uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass,
654 struct regional* region)
656 time_t now = *env->now;
657 struct dns_msg* msg = NULL;
658 uint16_t lookup[] = {LDNS_RR_TYPE_A, LDNS_RR_TYPE_AAAA,
659 LDNS_RR_TYPE_MX, LDNS_RR_TYPE_SOA, LDNS_RR_TYPE_NS, 0};
660 int i, num=5; /* number of RR types to look up */
661 log_assert(lookup[num] == 0);
663 for(i=0; i<num; i++) {
664 /* look up this RR for inclusion in type ANY response */
665 struct ub_packed_rrset_key* rrset = rrset_cache_lookup(
666 env->rrset_cache, qname, qnamelen, lookup[i],
668 struct packed_rrset_data *d;
672 /* only if rrset from answer section */
673 d = (struct packed_rrset_data*)rrset->entry.data;
674 if(d->trust == rrset_trust_add_noAA ||
675 d->trust == rrset_trust_auth_noAA ||
676 d->trust == rrset_trust_add_AA ||
677 d->trust == rrset_trust_auth_AA) {
678 lock_rw_unlock(&rrset->entry.lock);
682 /* create msg if none */
684 msg = dns_msg_create(qname, qnamelen, qtype, qclass,
685 region, (size_t)(num-i));
687 lock_rw_unlock(&rrset->entry.lock);
692 /* add RRset to response */
693 if(!dns_msg_ansadd(msg, region, rrset, now)) {
694 lock_rw_unlock(&rrset->entry.lock);
697 lock_rw_unlock(&rrset->entry.lock);
703 dns_cache_lookup(struct module_env* env,
704 uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass,
705 uint16_t flags, struct regional* region, struct regional* scratch)
707 struct lruhash_entry* e;
710 time_t now = *env->now;
711 struct ub_packed_rrset_key* rrset;
713 /* lookup first, this has both NXdomains and ANSWER responses */
715 k.qname_len = qnamelen;
718 h = query_info_hash(&k, flags);
719 e = slabhash_lookup(env->msg_cache, h, &k, 0);
721 struct msgreply_entry* key = (struct msgreply_entry*)e->key;
722 struct reply_info* data = (struct reply_info*)e->data;
723 struct dns_msg* msg = tomsg(env, &key->key, data, region, now,
726 lock_rw_unlock(&e->lock);
729 /* could be msg==NULL; due to TTL or not all rrsets available */
730 lock_rw_unlock(&e->lock);
733 /* see if a DNAME exists. Checked for first, to enforce that DNAMEs
734 * are more important, the CNAME is resynthesized and thus
735 * consistent with the DNAME */
736 if( (rrset=find_closest_of_type(env, qname, qnamelen, qclass, now,
737 LDNS_RR_TYPE_DNAME, 1))) {
738 /* synthesize a DNAME+CNAME message based on this */
739 struct dns_msg* msg = synth_dname_msg(rrset, region, now, &k);
741 lock_rw_unlock(&rrset->entry.lock);
744 lock_rw_unlock(&rrset->entry.lock);
747 /* see if we have CNAME for this domain,
748 * but not for DS records (which are part of the parent) */
749 if( qtype != LDNS_RR_TYPE_DS &&
750 (rrset=rrset_cache_lookup(env->rrset_cache, qname, qnamelen,
751 LDNS_RR_TYPE_CNAME, qclass, 0, now, 0))) {
752 struct dns_msg* msg = rrset_msg(rrset, region, now, &k);
754 lock_rw_unlock(&rrset->entry.lock);
757 lock_rw_unlock(&rrset->entry.lock);
760 /* construct DS, DNSKEY, DLV messages from rrset cache. */
761 if((qtype == LDNS_RR_TYPE_DS || qtype == LDNS_RR_TYPE_DNSKEY ||
762 qtype == LDNS_RR_TYPE_DLV) &&
763 (rrset=rrset_cache_lookup(env->rrset_cache, qname, qnamelen,
764 qtype, qclass, 0, now, 0))) {
765 /* if the rrset is from the additional section, and the
766 * signatures have fallen off, then do not synthesize a msg
767 * instead, allow a full query for signed results to happen.
768 * Forego all rrset data from additional section, because
769 * some signatures may not be present and cause validation
772 struct packed_rrset_data *d = (struct packed_rrset_data*)
774 if(d->trust != rrset_trust_add_noAA &&
775 d->trust != rrset_trust_add_AA &&
776 (qtype == LDNS_RR_TYPE_DS ||
777 (d->trust != rrset_trust_auth_noAA
778 && d->trust != rrset_trust_auth_AA) )) {
779 struct dns_msg* msg = rrset_msg(rrset, region, now, &k);
781 lock_rw_unlock(&rrset->entry.lock);
785 lock_rw_unlock(&rrset->entry.lock);
788 /* stop downwards cache search on NXDOMAIN.
789 * Empty nonterminals are NOERROR, so an NXDOMAIN for foo
790 * means bla.foo also does not exist. The DNSSEC proofs are
791 * the same. We search upwards for NXDOMAINs. */
792 if(env->cfg->harden_below_nxdomain)
793 while(!dname_is_root(k.qname)) {
794 dname_remove_label(&k.qname, &k.qname_len);
795 h = query_info_hash(&k, flags);
796 e = slabhash_lookup(env->msg_cache, h, &k, 0);
798 struct reply_info* data = (struct reply_info*)e->data;
800 if(FLAGS_GET_RCODE(data->flags) == LDNS_RCODE_NXDOMAIN
801 && data->security == sec_status_secure
802 && (msg=tomsg(env, &k, data, region, now, scratch))){
803 lock_rw_unlock(&e->lock);
804 msg->qinfo.qname=qname;
805 msg->qinfo.qname_len=qnamelen;
806 /* check that DNSSEC really works out */
807 msg->rep->security = sec_status_unchecked;
810 lock_rw_unlock(&e->lock);
814 /* fill common RR types for ANY response to avoid requery */
815 if(qtype == LDNS_RR_TYPE_ANY) {
816 return fill_any(env, qname, qnamelen, qtype, qclass, region);
823 dns_cache_store(struct module_env* env, struct query_info* msgqinf,
824 struct reply_info* msgrep, int is_referral, time_t leeway, int pside,
825 struct regional* region, uint16_t flags)
827 struct reply_info* rep = NULL;
828 /* alloc, malloc properly (not in region, like msg is) */
829 rep = reply_info_copy(msgrep, env->alloc, NULL);
832 /* ttl must be relative ;i.e. 0..86400 not time(0)+86400.
833 * the env->now is added to message and RRsets in this routine. */
834 /* the leeway is used to invalidate other rrsets earlier */
838 struct rrset_ref ref;
840 for(i=0; i<rep->rrset_count; i++) {
841 packed_rrset_ttl_add((struct packed_rrset_data*)
842 rep->rrsets[i]->entry.data, *env->now);
843 ref.key = rep->rrsets[i];
844 ref.id = rep->rrsets[i]->id;
845 /*ignore ret: it was in the cache, ref updated */
846 /* no leeway for typeNS */
847 (void)rrset_cache_update(env->rrset_cache, &ref,
848 env->alloc, *env->now +
849 ((ntohs(ref.key->rk.type)==LDNS_RR_TYPE_NS
850 && !pside) ? 0:leeway));
855 /* store msg, and rrsets */
856 struct query_info qinf;
860 qinf.qname = memdup(msgqinf->qname, msgqinf->qname_len);
862 reply_info_parsedelete(rep, env->alloc);
865 /* fixup flags to be sensible for a reply based on the cache */
866 /* this module means that RA is available. It is an answer QR.
867 * Not AA from cache. Not CD in cache (depends on client bit). */
868 rep->flags |= (BIT_RA | BIT_QR);
869 rep->flags &= ~(BIT_AA | BIT_CD);
870 h = query_info_hash(&qinf, flags);
871 dns_cache_store_msg(env, &qinf, h, rep, leeway, pside, msgrep,
873 /* qname is used inside query_info_entrysetup, and set to
874 * NULL. If it has not been used, free it. free(0) is safe. */
881 dns_cache_prefetch_adjust(struct module_env* env, struct query_info* qinfo,
882 time_t adjust, uint16_t flags)
884 struct msgreply_entry* msg;
885 msg = msg_cache_lookup(env, qinfo->qname, qinfo->qname_len,
886 qinfo->qtype, qinfo->qclass, flags, *env->now, 1);
888 struct reply_info* rep = (struct reply_info*)msg->entry.data;
890 rep->prefetch_ttl += adjust;
891 lock_rw_unlock(&msg->entry.lock);
894 lock_rw_unlock(&msg->entry.lock);