dns.c revision 356345
1/*
2 * services/cache/dns.c - Cache services for DNS using msg and rrset caches.
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 DNS cache.
40 */
41#include "config.h"
42#include "iterator/iter_delegpt.h"
43#include "iterator/iter_utils.h"
44#include "validator/val_nsec.h"
45#include "validator/val_utils.h"
46#include "services/cache/dns.h"
47#include "services/cache/rrset.h"
48#include "util/data/msgreply.h"
49#include "util/data/packed_rrset.h"
50#include "util/data/dname.h"
51#include "util/module.h"
52#include "util/net_help.h"
53#include "util/regional.h"
54#include "util/config_file.h"
55#include "sldns/sbuffer.h"
56
57/** store rrsets in the rrset cache.
58 * @param env: module environment with caches.
59 * @param rep: contains list of rrsets to store.
60 * @param now: current time.
61 * @param leeway: during prefetch how much leeway to update TTLs.
62 * 	This makes rrsets (other than type NS) timeout sooner so they get
63 * 	updated with a new full TTL.
64 * 	Type NS does not get this, because it must not be refreshed from the
65 * 	child domain, but keep counting down properly.
66 * @param pside: if from parentside discovered NS, so that its NS is okay
67 * 	in a prefetch situation to be updated (without becoming sticky).
68 * @param qrep: update rrsets here if cache is better
69 * @param region: for qrep allocs.
70 */
71static void
72store_rrsets(struct module_env* env, struct reply_info* rep, time_t now,
73	time_t leeway, int pside, struct reply_info* qrep,
74	struct regional* region)
75{
76        size_t i;
77        /* see if rrset already exists in cache, if not insert it. */
78        for(i=0; i<rep->rrset_count; i++) {
79                rep->ref[i].key = rep->rrsets[i];
80                rep->ref[i].id = rep->rrsets[i]->id;
81		/* update ref if it was in the cache */
82		switch(rrset_cache_update(env->rrset_cache, &rep->ref[i],
83                        env->alloc, now + ((ntohs(rep->ref[i].key->rk.type)==
84			LDNS_RR_TYPE_NS && !pside)?0:leeway))) {
85		case 0: /* ref unchanged, item inserted */
86			break;
87		case 2: /* ref updated, cache is superior */
88			if(region) {
89				struct ub_packed_rrset_key* ck;
90				lock_rw_rdlock(&rep->ref[i].key->entry.lock);
91				/* if deleted rrset, do not copy it */
92				if(rep->ref[i].key->id == 0)
93					ck = NULL;
94				else 	ck = packed_rrset_copy_region(
95					rep->ref[i].key, region, now);
96				lock_rw_unlock(&rep->ref[i].key->entry.lock);
97				if(ck) {
98					/* use cached copy if memory allows */
99					qrep->rrsets[i] = ck;
100				}
101			}
102			/* no break: also copy key item */
103			/* the line below is matched by gcc regex and silences
104			 * the fallthrough warning */
105			/* fallthrough */
106		case 1: /* ref updated, item inserted */
107                        rep->rrsets[i] = rep->ref[i].key;
108		}
109        }
110}
111
112/** delete message from message cache */
113void
114msg_cache_remove(struct module_env* env, uint8_t* qname, size_t qnamelen,
115	uint16_t qtype, uint16_t qclass, uint16_t flags)
116{
117	struct query_info k;
118	hashvalue_type h;
119
120	k.qname = qname;
121	k.qname_len = qnamelen;
122	k.qtype = qtype;
123	k.qclass = qclass;
124	k.local_alias = NULL;
125	h = query_info_hash(&k, flags);
126	slabhash_remove(env->msg_cache, h, &k);
127}
128
129/** remove servfail msg cache entry */
130static void
131msg_del_servfail(struct module_env* env, struct query_info* qinfo,
132	uint32_t flags)
133{
134	struct msgreply_entry* e;
135	/* see if the entry is servfail, and then remove it, so that
136	 * lookups move from the cacheresponse stage to the recursionresponse
137	 * stage */
138	e = msg_cache_lookup(env, qinfo->qname, qinfo->qname_len,
139		qinfo->qtype, qinfo->qclass, flags, 0, 0);
140	if(!e) return;
141	/* we don't check for the ttl here, also expired servfail entries
142	 * are removed.  If the user uses serve-expired, they would still be
143	 * used to answer from cache */
144	if(FLAGS_GET_RCODE(((struct reply_info*)e->entry.data)->flags)
145		!= LDNS_RCODE_SERVFAIL) {
146		lock_rw_unlock(&e->entry.lock);
147		return;
148	}
149	lock_rw_unlock(&e->entry.lock);
150	msg_cache_remove(env, qinfo->qname, qinfo->qname_len, qinfo->qtype,
151		qinfo->qclass, flags);
152}
153
154void
155dns_cache_store_msg(struct module_env* env, struct query_info* qinfo,
156	hashvalue_type hash, struct reply_info* rep, time_t leeway, int pside,
157	struct reply_info* qrep, uint32_t flags, struct regional* region)
158{
159	struct msgreply_entry* e;
160	time_t ttl = rep->ttl;
161	size_t i;
162
163	/* store RRsets */
164        for(i=0; i<rep->rrset_count; i++) {
165		rep->ref[i].key = rep->rrsets[i];
166		rep->ref[i].id = rep->rrsets[i]->id;
167	}
168
169	/* there was a reply_info_sortref(rep) here but it seems to be
170	 * unnecessary, because the cache gets locked per rrset. */
171	reply_info_set_ttls(rep, *env->now);
172	store_rrsets(env, rep, *env->now, leeway, pside, qrep, region);
173	if(ttl == 0 && !(flags & DNSCACHE_STORE_ZEROTTL)) {
174		/* we do not store the message, but we did store the RRs,
175		 * which could be useful for delegation information */
176		verbose(VERB_ALGO, "TTL 0: dropped msg from cache");
177		free(rep);
178		/* if the message is SERVFAIL in cache, remove that SERVFAIL,
179		 * so that the TTL 0 response can be returned for future
180		 * responses (i.e. don't get answered by the servfail from
181		 * cache, but instead go to recursion to get this TTL0
182		 * response). */
183		msg_del_servfail(env, qinfo, flags);
184		return;
185	}
186
187	/* store msg in the cache */
188	reply_info_sortref(rep);
189	if(!(e = query_info_entrysetup(qinfo, rep, hash))) {
190		log_err("store_msg: malloc failed");
191		return;
192	}
193	slabhash_insert(env->msg_cache, hash, &e->entry, rep, env->alloc);
194}
195
196/** find closest NS or DNAME and returns the rrset (locked) */
197static struct ub_packed_rrset_key*
198find_closest_of_type(struct module_env* env, uint8_t* qname, size_t qnamelen,
199	uint16_t qclass, time_t now, uint16_t searchtype, int stripfront)
200{
201	struct ub_packed_rrset_key *rrset;
202	uint8_t lablen;
203
204	if(stripfront) {
205		/* strip off so that DNAMEs have strict subdomain match */
206		lablen = *qname;
207		qname += lablen + 1;
208		qnamelen -= lablen + 1;
209	}
210
211	/* snip off front part of qname until the type is found */
212	while(qnamelen > 0) {
213		if((rrset = rrset_cache_lookup(env->rrset_cache, qname,
214			qnamelen, searchtype, qclass, 0, now, 0)))
215			return rrset;
216
217		/* snip off front label */
218		lablen = *qname;
219		qname += lablen + 1;
220		qnamelen -= lablen + 1;
221	}
222	return NULL;
223}
224
225/** add addr to additional section */
226static void
227addr_to_additional(struct ub_packed_rrset_key* rrset, struct regional* region,
228	struct dns_msg* msg, time_t now)
229{
230	if((msg->rep->rrsets[msg->rep->rrset_count] =
231		packed_rrset_copy_region(rrset, region, now))) {
232		msg->rep->ar_numrrsets++;
233		msg->rep->rrset_count++;
234	}
235}
236
237/** lookup message in message cache */
238struct msgreply_entry*
239msg_cache_lookup(struct module_env* env, uint8_t* qname, size_t qnamelen,
240	uint16_t qtype, uint16_t qclass, uint16_t flags, time_t now, int wr)
241{
242	struct lruhash_entry* e;
243	struct query_info k;
244	hashvalue_type h;
245
246	k.qname = qname;
247	k.qname_len = qnamelen;
248	k.qtype = qtype;
249	k.qclass = qclass;
250	k.local_alias = NULL;
251	h = query_info_hash(&k, flags);
252	e = slabhash_lookup(env->msg_cache, h, &k, wr);
253
254	if(!e) return NULL;
255	if( now > ((struct reply_info*)e->data)->ttl ) {
256		lock_rw_unlock(&e->lock);
257		return NULL;
258	}
259	return (struct msgreply_entry*)e->key;
260}
261
262/** find and add A and AAAA records for nameservers in delegpt */
263static int
264find_add_addrs(struct module_env* env, uint16_t qclass,
265	struct regional* region, struct delegpt* dp, time_t now,
266	struct dns_msg** msg)
267{
268	struct delegpt_ns* ns;
269	struct msgreply_entry* neg;
270	struct ub_packed_rrset_key* akey;
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);
274		if(akey) {
275			if(!delegpt_add_rrset_A(dp, region, akey, 0)) {
276				lock_rw_unlock(&akey->entry.lock);
277				return 0;
278			}
279			if(msg)
280				addr_to_additional(akey, region, *msg, now);
281			lock_rw_unlock(&akey->entry.lock);
282		} else {
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);
287			if(neg) {
288				delegpt_add_neg_msg(dp, neg);
289				lock_rw_unlock(&neg->entry.lock);
290			}
291		}
292		akey = rrset_cache_lookup(env->rrset_cache, ns->name,
293			ns->namelen, LDNS_RR_TYPE_AAAA, qclass, 0, now, 0);
294		if(akey) {
295			if(!delegpt_add_rrset_AAAA(dp, region, akey, 0)) {
296				lock_rw_unlock(&akey->entry.lock);
297				return 0;
298			}
299			if(msg)
300				addr_to_additional(akey, region, *msg, now);
301			lock_rw_unlock(&akey->entry.lock);
302		} else {
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);
307			if(neg) {
308				delegpt_add_neg_msg(dp, neg);
309				lock_rw_unlock(&neg->entry.lock);
310			}
311		}
312	}
313	return 1;
314}
315
316/** find and add A and AAAA records for missing nameservers in delegpt */
317int
318cache_fill_missing(struct module_env* env, uint16_t qclass,
319	struct regional* region, struct delegpt* dp)
320{
321	struct delegpt_ns* ns;
322	struct msgreply_entry* neg;
323	struct ub_packed_rrset_key* akey;
324	time_t now = *env->now;
325	for(ns = dp->nslist; ns; ns = ns->next) {
326		akey = rrset_cache_lookup(env->rrset_cache, ns->name,
327			ns->namelen, LDNS_RR_TYPE_A, qclass, 0, now, 0);
328		if(akey) {
329			if(!delegpt_add_rrset_A(dp, region, akey, ns->lame)) {
330				lock_rw_unlock(&akey->entry.lock);
331				return 0;
332			}
333			log_nametypeclass(VERB_ALGO, "found in cache",
334				ns->name, LDNS_RR_TYPE_A, qclass);
335			lock_rw_unlock(&akey->entry.lock);
336		} else {
337			/* BIT_CD on false because delegpt lookup does
338			 * not use dns64 translation */
339			neg = msg_cache_lookup(env, ns->name, ns->namelen,
340				LDNS_RR_TYPE_A, qclass, 0, now, 0);
341			if(neg) {
342				delegpt_add_neg_msg(dp, neg);
343				lock_rw_unlock(&neg->entry.lock);
344			}
345		}
346		akey = rrset_cache_lookup(env->rrset_cache, ns->name,
347			ns->namelen, LDNS_RR_TYPE_AAAA, qclass, 0, now, 0);
348		if(akey) {
349			if(!delegpt_add_rrset_AAAA(dp, region, akey, ns->lame)) {
350				lock_rw_unlock(&akey->entry.lock);
351				return 0;
352			}
353			log_nametypeclass(VERB_ALGO, "found in cache",
354				ns->name, LDNS_RR_TYPE_AAAA, qclass);
355			lock_rw_unlock(&akey->entry.lock);
356		} else {
357			/* BIT_CD on false because delegpt lookup does
358			 * not use dns64 translation */
359			neg = msg_cache_lookup(env, ns->name, ns->namelen,
360				LDNS_RR_TYPE_AAAA, qclass, 0, now, 0);
361			if(neg) {
362				delegpt_add_neg_msg(dp, neg);
363				lock_rw_unlock(&neg->entry.lock);
364			}
365		}
366	}
367	return 1;
368}
369
370/** find and add DS or NSEC to delegation msg */
371static void
372find_add_ds(struct module_env* env, struct regional* region,
373	struct dns_msg* msg, struct delegpt* dp, time_t now)
374{
375	/* Lookup the DS or NSEC at the delegation point. */
376	struct ub_packed_rrset_key* rrset = rrset_cache_lookup(
377		env->rrset_cache, dp->name, dp->namelen, LDNS_RR_TYPE_DS,
378		msg->qinfo.qclass, 0, now, 0);
379	if(!rrset) {
380		/* NOTE: this won't work for alternate NSEC schemes
381		 *	(opt-in, NSEC3) */
382		rrset = rrset_cache_lookup(env->rrset_cache, dp->name,
383			dp->namelen, LDNS_RR_TYPE_NSEC, msg->qinfo.qclass,
384			0, now, 0);
385		/* Note: the PACKED_RRSET_NSEC_AT_APEX flag is not used.
386		 * since this is a referral, we need the NSEC at the parent
387		 * side of the zone cut, not the NSEC at apex side. */
388		if(rrset && nsec_has_type(rrset, LDNS_RR_TYPE_DS)) {
389			lock_rw_unlock(&rrset->entry.lock);
390			rrset = NULL; /* discard wrong NSEC */
391		}
392	}
393	if(rrset) {
394		/* add it to auth section. This is the second rrset. */
395		if((msg->rep->rrsets[msg->rep->rrset_count] =
396			packed_rrset_copy_region(rrset, region, now))) {
397			msg->rep->ns_numrrsets++;
398			msg->rep->rrset_count++;
399		}
400		lock_rw_unlock(&rrset->entry.lock);
401	}
402}
403
404struct dns_msg*
405dns_msg_create(uint8_t* qname, size_t qnamelen, uint16_t qtype,
406	uint16_t qclass, struct regional* region, size_t capacity)
407{
408	struct dns_msg* msg = (struct dns_msg*)regional_alloc(region,
409		sizeof(struct dns_msg));
410	if(!msg)
411		return NULL;
412	msg->qinfo.qname = regional_alloc_init(region, qname, qnamelen);
413	if(!msg->qinfo.qname)
414		return NULL;
415	msg->qinfo.qname_len = qnamelen;
416	msg->qinfo.qtype = qtype;
417	msg->qinfo.qclass = qclass;
418	msg->qinfo.local_alias = NULL;
419	/* non-packed reply_info, because it needs to grow the array */
420	msg->rep = (struct reply_info*)regional_alloc_zero(region,
421		sizeof(struct reply_info)-sizeof(struct rrset_ref));
422	if(!msg->rep)
423		return NULL;
424	if(capacity > RR_COUNT_MAX)
425		return NULL; /* integer overflow protection */
426	msg->rep->flags = BIT_QR; /* with QR, no AA */
427	msg->rep->qdcount = 1;
428	msg->rep->rrsets = (struct ub_packed_rrset_key**)
429		regional_alloc(region,
430		capacity*sizeof(struct ub_packed_rrset_key*));
431	if(!msg->rep->rrsets)
432		return NULL;
433	return msg;
434}
435
436int
437dns_msg_authadd(struct dns_msg* msg, struct regional* region,
438	struct ub_packed_rrset_key* rrset, time_t now)
439{
440	if(!(msg->rep->rrsets[msg->rep->rrset_count++] =
441		packed_rrset_copy_region(rrset, region, now)))
442		return 0;
443	msg->rep->ns_numrrsets++;
444	return 1;
445}
446
447int
448dns_msg_ansadd(struct dns_msg* msg, struct regional* region,
449	struct ub_packed_rrset_key* rrset, time_t now)
450{
451	if(!(msg->rep->rrsets[msg->rep->rrset_count++] =
452		packed_rrset_copy_region(rrset, region, now)))
453		return 0;
454	msg->rep->an_numrrsets++;
455	return 1;
456}
457
458struct delegpt*
459dns_cache_find_delegation(struct module_env* env, uint8_t* qname,
460	size_t qnamelen, uint16_t qtype, uint16_t qclass,
461	struct regional* region, struct dns_msg** msg, time_t now)
462{
463	/* try to find closest NS rrset */
464	struct ub_packed_rrset_key* nskey;
465	struct packed_rrset_data* nsdata;
466	struct delegpt* dp;
467
468	nskey = find_closest_of_type(env, qname, qnamelen, qclass, now,
469		LDNS_RR_TYPE_NS, 0);
470	if(!nskey) /* hope the caller has hints to prime or something */
471		return NULL;
472	nsdata = (struct packed_rrset_data*)nskey->entry.data;
473	/* got the NS key, create delegation point */
474	dp = delegpt_create(region);
475	if(!dp || !delegpt_set_name(dp, region, nskey->rk.dname)) {
476		lock_rw_unlock(&nskey->entry.lock);
477		log_err("find_delegation: out of memory");
478		return NULL;
479	}
480	/* create referral message */
481	if(msg) {
482		/* allocate the array to as much as we could need:
483		 *	NS rrset + DS/NSEC rrset +
484		 *	A rrset for every NS RR
485		 *	AAAA rrset for every NS RR
486		 */
487		*msg = dns_msg_create(qname, qnamelen, qtype, qclass, region,
488			2 + nsdata->count*2);
489		if(!*msg || !dns_msg_authadd(*msg, region, nskey, now)) {
490			lock_rw_unlock(&nskey->entry.lock);
491			log_err("find_delegation: out of memory");
492			return NULL;
493		}
494	}
495	if(!delegpt_rrset_add_ns(dp, region, nskey, 0))
496		log_err("find_delegation: addns out of memory");
497	lock_rw_unlock(&nskey->entry.lock); /* first unlock before next lookup*/
498	/* find and add DS/NSEC (if any) */
499	if(msg)
500		find_add_ds(env, region, *msg, dp, now);
501	/* find and add A entries */
502	if(!find_add_addrs(env, qclass, region, dp, now, msg))
503		log_err("find_delegation: addrs out of memory");
504	return dp;
505}
506
507/** allocate dns_msg from query_info and reply_info */
508static struct dns_msg*
509gen_dns_msg(struct regional* region, struct query_info* q, size_t num)
510{
511	struct dns_msg* msg = (struct dns_msg*)regional_alloc(region,
512		sizeof(struct dns_msg));
513	if(!msg)
514		return NULL;
515	memcpy(&msg->qinfo, q, sizeof(struct query_info));
516	msg->qinfo.qname = regional_alloc_init(region, q->qname, q->qname_len);
517	if(!msg->qinfo.qname)
518		return NULL;
519	/* allocate replyinfo struct and rrset key array separately */
520	msg->rep = (struct reply_info*)regional_alloc(region,
521		sizeof(struct reply_info) - sizeof(struct rrset_ref));
522	if(!msg->rep)
523		return NULL;
524	if(num > RR_COUNT_MAX)
525		return NULL; /* integer overflow protection */
526	msg->rep->rrsets = (struct ub_packed_rrset_key**)
527		regional_alloc(region,
528		num * sizeof(struct ub_packed_rrset_key*));
529	if(!msg->rep->rrsets)
530		return NULL;
531	return msg;
532}
533
534struct dns_msg*
535tomsg(struct module_env* env, struct query_info* q, struct reply_info* r,
536	struct regional* region, time_t now, struct regional* scratch)
537{
538	struct dns_msg* msg;
539	size_t i;
540	if(now > r->ttl)
541		return NULL;
542	msg = gen_dns_msg(region, q, r->rrset_count);
543	if(!msg)
544		return NULL;
545	msg->rep->flags = r->flags;
546	msg->rep->qdcount = r->qdcount;
547	msg->rep->ttl = r->ttl - now;
548	if(r->prefetch_ttl > now)
549		msg->rep->prefetch_ttl = r->prefetch_ttl - now;
550	else	msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(msg->rep->ttl);
551	msg->rep->serve_expired_ttl = msg->rep->ttl + SERVE_EXPIRED_TTL;
552	msg->rep->security = r->security;
553	msg->rep->an_numrrsets = r->an_numrrsets;
554	msg->rep->ns_numrrsets = r->ns_numrrsets;
555	msg->rep->ar_numrrsets = r->ar_numrrsets;
556	msg->rep->rrset_count = r->rrset_count;
557        msg->rep->authoritative = r->authoritative;
558	if(!rrset_array_lock(r->ref, r->rrset_count, now))
559		return NULL;
560	if(r->an_numrrsets > 0 && (r->rrsets[0]->rk.type == htons(
561		LDNS_RR_TYPE_CNAME) || r->rrsets[0]->rk.type == htons(
562		LDNS_RR_TYPE_DNAME)) && !reply_check_cname_chain(q, r)) {
563		/* cname chain is now invalid, reconstruct msg */
564		rrset_array_unlock(r->ref, r->rrset_count);
565		return NULL;
566	}
567	if(r->security == sec_status_secure && !reply_all_rrsets_secure(r)) {
568		/* message rrsets have changed status, revalidate */
569		rrset_array_unlock(r->ref, r->rrset_count);
570		return NULL;
571	}
572	for(i=0; i<msg->rep->rrset_count; i++) {
573		msg->rep->rrsets[i] = packed_rrset_copy_region(r->rrsets[i],
574			region, now);
575		if(!msg->rep->rrsets[i]) {
576			rrset_array_unlock(r->ref, r->rrset_count);
577			return NULL;
578		}
579	}
580	if(env)
581		rrset_array_unlock_touch(env->rrset_cache, scratch, r->ref,
582		r->rrset_count);
583	else
584		rrset_array_unlock(r->ref, r->rrset_count);
585	return msg;
586}
587
588/** synthesize RRset-only response from cached RRset item */
589static struct dns_msg*
590rrset_msg(struct ub_packed_rrset_key* rrset, struct regional* region,
591	time_t now, struct query_info* q)
592{
593	struct dns_msg* msg;
594	struct packed_rrset_data* d = (struct packed_rrset_data*)
595		rrset->entry.data;
596	if(now > d->ttl)
597		return NULL;
598	msg = gen_dns_msg(region, q, 1); /* only the CNAME (or other) RRset */
599	if(!msg)
600		return NULL;
601	msg->rep->flags = BIT_QR; /* reply, no AA, no error */
602        msg->rep->authoritative = 0; /* reply stored in cache can't be authoritative */
603	msg->rep->qdcount = 1;
604	msg->rep->ttl = d->ttl - now;
605	msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(msg->rep->ttl);
606	msg->rep->serve_expired_ttl = msg->rep->ttl + SERVE_EXPIRED_TTL;
607	msg->rep->security = sec_status_unchecked;
608	msg->rep->an_numrrsets = 1;
609	msg->rep->ns_numrrsets = 0;
610	msg->rep->ar_numrrsets = 0;
611	msg->rep->rrset_count = 1;
612	msg->rep->rrsets[0] = packed_rrset_copy_region(rrset, region, now);
613	if(!msg->rep->rrsets[0]) /* copy CNAME */
614		return NULL;
615	return msg;
616}
617
618/** synthesize DNAME+CNAME response from cached DNAME item */
619static struct dns_msg*
620synth_dname_msg(struct ub_packed_rrset_key* rrset, struct regional* region,
621	time_t now, struct query_info* q, enum sec_status* sec_status)
622{
623	struct dns_msg* msg;
624	struct ub_packed_rrset_key* ck;
625	struct packed_rrset_data* newd, *d = (struct packed_rrset_data*)
626		rrset->entry.data;
627	uint8_t* newname, *dtarg = NULL;
628	size_t newlen, dtarglen;
629	if(now > d->ttl)
630		return NULL;
631	/* only allow validated (with DNSSEC) DNAMEs used from cache
632	 * for insecure DNAMEs, query again. */
633	*sec_status = d->security;
634	/* return sec status, so the status of the CNAME can be checked
635	 * by the calling routine. */
636	msg = gen_dns_msg(region, q, 2); /* DNAME + CNAME RRset */
637	if(!msg)
638		return NULL;
639	msg->rep->flags = BIT_QR; /* reply, no AA, no error */
640        msg->rep->authoritative = 0; /* reply stored in cache can't be authoritative */
641	msg->rep->qdcount = 1;
642	msg->rep->ttl = d->ttl - now;
643	msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(msg->rep->ttl);
644	msg->rep->serve_expired_ttl = msg->rep->ttl + SERVE_EXPIRED_TTL;
645	msg->rep->security = sec_status_unchecked;
646	msg->rep->an_numrrsets = 1;
647	msg->rep->ns_numrrsets = 0;
648	msg->rep->ar_numrrsets = 0;
649	msg->rep->rrset_count = 1;
650	msg->rep->rrsets[0] = packed_rrset_copy_region(rrset, region, now);
651	if(!msg->rep->rrsets[0]) /* copy DNAME */
652		return NULL;
653	/* synth CNAME rrset */
654	get_cname_target(rrset, &dtarg, &dtarglen);
655	if(!dtarg)
656		return NULL;
657	newlen = q->qname_len + dtarglen - rrset->rk.dname_len;
658	if(newlen > LDNS_MAX_DOMAINLEN) {
659		msg->rep->flags |= LDNS_RCODE_YXDOMAIN;
660		return msg;
661	}
662	newname = (uint8_t*)regional_alloc(region, newlen);
663	if(!newname)
664		return NULL;
665	/* new name is concatenation of qname front (without DNAME owner)
666	 * and DNAME target name */
667	memcpy(newname, q->qname, q->qname_len-rrset->rk.dname_len);
668	memmove(newname+(q->qname_len-rrset->rk.dname_len), dtarg, dtarglen);
669	/* create rest of CNAME rrset */
670	ck = (struct ub_packed_rrset_key*)regional_alloc(region,
671		sizeof(struct ub_packed_rrset_key));
672	if(!ck)
673		return NULL;
674	memset(&ck->entry, 0, sizeof(ck->entry));
675	msg->rep->rrsets[1] = ck;
676	ck->entry.key = ck;
677	ck->rk.type = htons(LDNS_RR_TYPE_CNAME);
678	ck->rk.rrset_class = rrset->rk.rrset_class;
679	ck->rk.flags = 0;
680	ck->rk.dname = regional_alloc_init(region, q->qname, q->qname_len);
681	if(!ck->rk.dname)
682		return NULL;
683	ck->rk.dname_len = q->qname_len;
684	ck->entry.hash = rrset_key_hash(&ck->rk);
685	newd = (struct packed_rrset_data*)regional_alloc_zero(region,
686		sizeof(struct packed_rrset_data) + sizeof(size_t) +
687		sizeof(uint8_t*) + sizeof(time_t) + sizeof(uint16_t)
688		+ newlen);
689	if(!newd)
690		return NULL;
691	ck->entry.data = newd;
692	newd->ttl = 0; /* 0 for synthesized CNAME TTL */
693	newd->count = 1;
694	newd->rrsig_count = 0;
695	newd->trust = rrset_trust_ans_noAA;
696	newd->rr_len = (size_t*)((uint8_t*)newd +
697		sizeof(struct packed_rrset_data));
698	newd->rr_len[0] = newlen + sizeof(uint16_t);
699	packed_rrset_ptr_fixup(newd);
700	newd->rr_ttl[0] = newd->ttl;
701	msg->rep->ttl = newd->ttl;
702	msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(newd->ttl);
703	msg->rep->serve_expired_ttl = newd->ttl + SERVE_EXPIRED_TTL;
704	sldns_write_uint16(newd->rr_data[0], newlen);
705	memmove(newd->rr_data[0] + sizeof(uint16_t), newname, newlen);
706	msg->rep->an_numrrsets ++;
707	msg->rep->rrset_count ++;
708	return msg;
709}
710
711/** Fill TYPE_ANY response with some data from cache */
712static struct dns_msg*
713fill_any(struct module_env* env,
714	uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass,
715	struct regional* region)
716{
717	time_t now = *env->now;
718	struct dns_msg* msg = NULL;
719	uint16_t lookup[] = {LDNS_RR_TYPE_A, LDNS_RR_TYPE_AAAA,
720		LDNS_RR_TYPE_MX, LDNS_RR_TYPE_SOA, LDNS_RR_TYPE_NS,
721		LDNS_RR_TYPE_DNAME, 0};
722	int i, num=6; /* number of RR types to look up */
723	log_assert(lookup[num] == 0);
724
725	if(env->cfg->deny_any) {
726		/* return empty message */
727		msg = dns_msg_create(qname, qnamelen, qtype, qclass,
728			region, 0);
729		if(!msg) {
730			return NULL;
731		}
732		/* set NOTIMPL for RFC 8482 */
733		msg->rep->flags |= LDNS_RCODE_NOTIMPL;
734		msg->rep->security = sec_status_indeterminate;
735		return msg;
736	}
737
738	for(i=0; i<num; i++) {
739		/* look up this RR for inclusion in type ANY response */
740		struct ub_packed_rrset_key* rrset = rrset_cache_lookup(
741			env->rrset_cache, qname, qnamelen, lookup[i],
742			qclass, 0, now, 0);
743		struct packed_rrset_data *d;
744		if(!rrset)
745			continue;
746
747		/* only if rrset from answer section */
748		d = (struct packed_rrset_data*)rrset->entry.data;
749		if(d->trust == rrset_trust_add_noAA ||
750			d->trust == rrset_trust_auth_noAA ||
751			d->trust == rrset_trust_add_AA ||
752			d->trust == rrset_trust_auth_AA) {
753			lock_rw_unlock(&rrset->entry.lock);
754			continue;
755		}
756
757		/* create msg if none */
758		if(!msg) {
759			msg = dns_msg_create(qname, qnamelen, qtype, qclass,
760				region, (size_t)(num-i));
761			if(!msg) {
762				lock_rw_unlock(&rrset->entry.lock);
763				return NULL;
764			}
765		}
766
767		/* add RRset to response */
768		if(!dns_msg_ansadd(msg, region, rrset, now)) {
769			lock_rw_unlock(&rrset->entry.lock);
770			return NULL;
771		}
772		lock_rw_unlock(&rrset->entry.lock);
773	}
774	return msg;
775}
776
777struct dns_msg*
778dns_cache_lookup(struct module_env* env,
779	uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass,
780	uint16_t flags, struct regional* region, struct regional* scratch,
781	int no_partial)
782{
783	struct lruhash_entry* e;
784	struct query_info k;
785	hashvalue_type h;
786	time_t now = *env->now;
787	struct ub_packed_rrset_key* rrset;
788
789	/* lookup first, this has both NXdomains and ANSWER responses */
790	k.qname = qname;
791	k.qname_len = qnamelen;
792	k.qtype = qtype;
793	k.qclass = qclass;
794	k.local_alias = NULL;
795	h = query_info_hash(&k, flags);
796	e = slabhash_lookup(env->msg_cache, h, &k, 0);
797	if(e) {
798		struct msgreply_entry* key = (struct msgreply_entry*)e->key;
799		struct reply_info* data = (struct reply_info*)e->data;
800		struct dns_msg* msg = tomsg(env, &key->key, data, region, now,
801			scratch);
802		if(msg) {
803			lock_rw_unlock(&e->lock);
804			return msg;
805		}
806		/* could be msg==NULL; due to TTL or not all rrsets available */
807		lock_rw_unlock(&e->lock);
808	}
809
810	/* see if a DNAME exists. Checked for first, to enforce that DNAMEs
811	 * are more important, the CNAME is resynthesized and thus
812	 * consistent with the DNAME */
813	if(!no_partial &&
814		(rrset=find_closest_of_type(env, qname, qnamelen, qclass, now,
815		LDNS_RR_TYPE_DNAME, 1))) {
816		/* synthesize a DNAME+CNAME message based on this */
817		enum sec_status sec_status = sec_status_unchecked;
818		struct dns_msg* msg = synth_dname_msg(rrset, region, now, &k,
819			&sec_status);
820		if(msg) {
821			struct ub_packed_rrset_key* cname_rrset;
822			lock_rw_unlock(&rrset->entry.lock);
823			/* now, after unlocking the DNAME rrset lock,
824			 * check the sec_status, and see if we need to look
825			 * up the CNAME record associated before it can
826			 * be used */
827			/* normally, only secure DNAMEs allowed from cache*/
828			if(sec_status == sec_status_secure)
829				return msg;
830			/* but if we have a CNAME cached with this name, then we
831			 * have previously already allowed this name to pass.
832			 * the next cache lookup is going to fetch that CNAME itself,
833			 * but it is better to have the (unsigned)DNAME + CNAME in
834			 * that case */
835			cname_rrset = rrset_cache_lookup(
836				env->rrset_cache, qname, qnamelen,
837				LDNS_RR_TYPE_CNAME, qclass, 0, now, 0);
838			if(cname_rrset) {
839				/* CNAME already synthesized by
840				 * synth_dname_msg routine, so we can
841				 * straight up return the msg */
842				lock_rw_unlock(&cname_rrset->entry.lock);
843				return msg;
844			}
845		} else {
846			lock_rw_unlock(&rrset->entry.lock);
847		}
848	}
849
850	/* see if we have CNAME for this domain,
851	 * but not for DS records (which are part of the parent) */
852	if(!no_partial && qtype != LDNS_RR_TYPE_DS &&
853	   (rrset=rrset_cache_lookup(env->rrset_cache, qname, qnamelen,
854		LDNS_RR_TYPE_CNAME, qclass, 0, now, 0))) {
855		uint8_t* wc = NULL;
856		size_t wl;
857		/* if the rrset is not a wildcard expansion, with wcname */
858		/* because, if we return that CNAME rrset on its own, it is
859		 * missing the NSEC or NSEC3 proof */
860		if(!(val_rrset_wildcard(rrset, &wc, &wl) && wc != NULL)) {
861			struct dns_msg* msg = rrset_msg(rrset, region, now, &k);
862			if(msg) {
863				lock_rw_unlock(&rrset->entry.lock);
864				return msg;
865			}
866		}
867		lock_rw_unlock(&rrset->entry.lock);
868	}
869
870	/* construct DS, DNSKEY, DLV messages from rrset cache. */
871	if((qtype == LDNS_RR_TYPE_DS || qtype == LDNS_RR_TYPE_DNSKEY ||
872		qtype == LDNS_RR_TYPE_DLV) &&
873		(rrset=rrset_cache_lookup(env->rrset_cache, qname, qnamelen,
874		qtype, qclass, 0, now, 0))) {
875		/* if the rrset is from the additional section, and the
876		 * signatures have fallen off, then do not synthesize a msg
877		 * instead, allow a full query for signed results to happen.
878		 * Forego all rrset data from additional section, because
879		 * some signatures may not be present and cause validation
880		 * failure.
881		 */
882		struct packed_rrset_data *d = (struct packed_rrset_data*)
883			rrset->entry.data;
884		if(d->trust != rrset_trust_add_noAA &&
885			d->trust != rrset_trust_add_AA &&
886			(qtype == LDNS_RR_TYPE_DS ||
887				(d->trust != rrset_trust_auth_noAA
888				&& d->trust != rrset_trust_auth_AA) )) {
889			struct dns_msg* msg = rrset_msg(rrset, region, now, &k);
890			if(msg) {
891				lock_rw_unlock(&rrset->entry.lock);
892				return msg;
893			}
894		}
895		lock_rw_unlock(&rrset->entry.lock);
896	}
897
898	/* stop downwards cache search on NXDOMAIN.
899	 * Empty nonterminals are NOERROR, so an NXDOMAIN for foo
900	 * means bla.foo also does not exist.  The DNSSEC proofs are
901	 * the same.  We search upwards for NXDOMAINs. */
902	if(env->cfg->harden_below_nxdomain)
903	    while(!dname_is_root(k.qname)) {
904		dname_remove_label(&k.qname, &k.qname_len);
905		h = query_info_hash(&k, flags);
906		e = slabhash_lookup(env->msg_cache, h, &k, 0);
907		if(!e && k.qtype != LDNS_RR_TYPE_A &&
908			env->cfg->qname_minimisation) {
909			k.qtype = LDNS_RR_TYPE_A;
910			h = query_info_hash(&k, flags);
911			e = slabhash_lookup(env->msg_cache, h, &k, 0);
912		}
913		if(e) {
914			struct reply_info* data = (struct reply_info*)e->data;
915			struct dns_msg* msg;
916			if(FLAGS_GET_RCODE(data->flags) == LDNS_RCODE_NXDOMAIN
917			  && data->security == sec_status_secure
918			  && (data->an_numrrsets == 0 ||
919				ntohs(data->rrsets[0]->rk.type) != LDNS_RR_TYPE_CNAME)
920			  && (msg=tomsg(env, &k, data, region, now, scratch))){
921				lock_rw_unlock(&e->lock);
922				msg->qinfo.qname=qname;
923				msg->qinfo.qname_len=qnamelen;
924				/* check that DNSSEC really works out */
925				msg->rep->security = sec_status_unchecked;
926				iter_scrub_nxdomain(msg);
927				return msg;
928			}
929			lock_rw_unlock(&e->lock);
930		}
931		k.qtype = qtype;
932	    }
933
934	/* fill common RR types for ANY response to avoid requery */
935	if(qtype == LDNS_RR_TYPE_ANY) {
936		return fill_any(env, qname, qnamelen, qtype, qclass, region);
937	}
938
939	return NULL;
940}
941
942int
943dns_cache_store(struct module_env* env, struct query_info* msgqinf,
944        struct reply_info* msgrep, int is_referral, time_t leeway, int pside,
945	struct regional* region, uint32_t flags)
946{
947	struct reply_info* rep = NULL;
948	/* alloc, malloc properly (not in region, like msg is) */
949	rep = reply_info_copy(msgrep, env->alloc, NULL);
950	if(!rep)
951		return 0;
952	/* ttl must be relative ;i.e. 0..86400 not  time(0)+86400.
953	 * the env->now is added to message and RRsets in this routine. */
954	/* the leeway is used to invalidate other rrsets earlier */
955
956	if(is_referral) {
957		/* store rrsets */
958		struct rrset_ref ref;
959		size_t i;
960		for(i=0; i<rep->rrset_count; i++) {
961			packed_rrset_ttl_add((struct packed_rrset_data*)
962				rep->rrsets[i]->entry.data, *env->now);
963			ref.key = rep->rrsets[i];
964			ref.id = rep->rrsets[i]->id;
965			/*ignore ret: it was in the cache, ref updated */
966			/* no leeway for typeNS */
967			(void)rrset_cache_update(env->rrset_cache, &ref,
968				env->alloc, *env->now +
969				((ntohs(ref.key->rk.type)==LDNS_RR_TYPE_NS
970				 && !pside) ? 0:leeway));
971		}
972		free(rep);
973		return 1;
974	} else {
975		/* store msg, and rrsets */
976		struct query_info qinf;
977		hashvalue_type h;
978
979		qinf = *msgqinf;
980		qinf.qname = memdup(msgqinf->qname, msgqinf->qname_len);
981		if(!qinf.qname) {
982			reply_info_parsedelete(rep, env->alloc);
983			return 0;
984		}
985		/* fixup flags to be sensible for a reply based on the cache */
986		/* this module means that RA is available. It is an answer QR.
987		 * Not AA from cache. Not CD in cache (depends on client bit). */
988		rep->flags |= (BIT_RA | BIT_QR);
989		rep->flags &= ~(BIT_AA | BIT_CD);
990		h = query_info_hash(&qinf, (uint16_t)flags);
991		dns_cache_store_msg(env, &qinf, h, rep, leeway, pside, msgrep,
992			flags, region);
993		/* qname is used inside query_info_entrysetup, and set to
994		 * NULL. If it has not been used, free it. free(0) is safe. */
995		free(qinf.qname);
996	}
997	return 1;
998}
999
1000int
1001dns_cache_prefetch_adjust(struct module_env* env, struct query_info* qinfo,
1002        time_t adjust, uint16_t flags)
1003{
1004	struct msgreply_entry* msg;
1005	msg = msg_cache_lookup(env, qinfo->qname, qinfo->qname_len,
1006		qinfo->qtype, qinfo->qclass, flags, *env->now, 1);
1007	if(msg) {
1008		struct reply_info* rep = (struct reply_info*)msg->entry.data;
1009		if(rep) {
1010			rep->prefetch_ttl += adjust;
1011			lock_rw_unlock(&msg->entry.lock);
1012			return 1;
1013		}
1014		lock_rw_unlock(&msg->entry.lock);
1015	}
1016	return 0;
1017}
1018