1ae8c6e27Sflorian /*
2ae8c6e27Sflorian  * services/cache/dns.h - Cache services for DNS using msg and rrset caches.
3ae8c6e27Sflorian  *
4ae8c6e27Sflorian  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5ae8c6e27Sflorian  *
6ae8c6e27Sflorian  * This software is open source.
7ae8c6e27Sflorian  *
8ae8c6e27Sflorian  * Redistribution and use in source and binary forms, with or without
9ae8c6e27Sflorian  * modification, are permitted provided that the following conditions
10ae8c6e27Sflorian  * are met:
11ae8c6e27Sflorian  *
12ae8c6e27Sflorian  * Redistributions of source code must retain the above copyright notice,
13ae8c6e27Sflorian  * this list of conditions and the following disclaimer.
14ae8c6e27Sflorian  *
15ae8c6e27Sflorian  * Redistributions in binary form must reproduce the above copyright notice,
16ae8c6e27Sflorian  * this list of conditions and the following disclaimer in the documentation
17ae8c6e27Sflorian  * and/or other materials provided with the distribution.
18ae8c6e27Sflorian  *
19ae8c6e27Sflorian  * Neither the name of the NLNET LABS nor the names of its contributors may
20ae8c6e27Sflorian  * be used to endorse or promote products derived from this software without
21ae8c6e27Sflorian  * specific prior written permission.
22ae8c6e27Sflorian  *
23ae8c6e27Sflorian  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24ae8c6e27Sflorian  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25ae8c6e27Sflorian  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26ae8c6e27Sflorian  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27ae8c6e27Sflorian  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28ae8c6e27Sflorian  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29ae8c6e27Sflorian  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30ae8c6e27Sflorian  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31ae8c6e27Sflorian  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32ae8c6e27Sflorian  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33ae8c6e27Sflorian  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34ae8c6e27Sflorian  */
35ae8c6e27Sflorian 
36ae8c6e27Sflorian /**
37ae8c6e27Sflorian  * \file
38ae8c6e27Sflorian  *
39ae8c6e27Sflorian  * This file contains the DNS cache.
40ae8c6e27Sflorian  */
41ae8c6e27Sflorian 
42ae8c6e27Sflorian #ifndef SERVICES_CACHE_DNS_H
43ae8c6e27Sflorian #define SERVICES_CACHE_DNS_H
44ae8c6e27Sflorian #include "util/storage/lruhash.h"
45ae8c6e27Sflorian #include "util/data/msgreply.h"
46ae8c6e27Sflorian struct module_env;
47ae8c6e27Sflorian struct query_info;
48ae8c6e27Sflorian struct reply_info;
49ae8c6e27Sflorian struct regional;
50ae8c6e27Sflorian struct delegpt;
51ae8c6e27Sflorian 
52ae8c6e27Sflorian /** Flags to control behavior of dns_cache_store() and dns_cache_store_msg().
53ae8c6e27Sflorian  *  Must be an unsigned 32-bit value larger than 0xffff */
54ae8c6e27Sflorian 
55ae8c6e27Sflorian /** Allow caching a DNS message with a zero TTL. */
56ae8c6e27Sflorian #define DNSCACHE_STORE_ZEROTTL 0x100000
57ae8c6e27Sflorian 
58ae8c6e27Sflorian /**
59ae8c6e27Sflorian  * Region allocated message reply
60ae8c6e27Sflorian  */
61ae8c6e27Sflorian struct dns_msg {
62ae8c6e27Sflorian 	/** query info */
63ae8c6e27Sflorian 	struct query_info qinfo;
64ae8c6e27Sflorian 	/** reply info - ptr to packed repinfo structure */
65ae8c6e27Sflorian 	struct reply_info *rep;
66ae8c6e27Sflorian };
67ae8c6e27Sflorian 
68ae8c6e27Sflorian /**
69ae8c6e27Sflorian  * Allocate a dns_msg with malloc/alloc structure and store in dns cache.
70ae8c6e27Sflorian  *
71ae8c6e27Sflorian  * @param env: environment, with alloc structure and dns cache.
72ae8c6e27Sflorian  * @param qinf: query info, the query for which answer is stored.
73ae8c6e27Sflorian  * 	this is allocated in a region, and will be copied to malloc area
74ae8c6e27Sflorian  * 	before insertion.
75ae8c6e27Sflorian  * @param rep: reply in dns_msg from dns_alloc_msg for example.
76ae8c6e27Sflorian  * 	this is allocated in a region, and will be copied to malloc area
77ae8c6e27Sflorian  * 	before insertion.
78ae8c6e27Sflorian  * @param is_referral: If true, then the given message to be stored is a
79ae8c6e27Sflorian  *      referral. The cache implementation may use this as a hint.
80ae8c6e27Sflorian  *      It will store only the RRsets, not the message.
81ae8c6e27Sflorian  * @param leeway: TTL value, if not 0, other rrsets are considered expired
82ae8c6e27Sflorian  *	that many seconds before actual TTL expiry.
83ae8c6e27Sflorian  * @param pside: if true, information came from a server which was fetched
84ae8c6e27Sflorian  * 	from the parentside of the zonecut.  This means that the type NS
85ae8c6e27Sflorian  * 	can be updated to full TTL even in prefetch situations.
86ae8c6e27Sflorian  * @param region: region to allocate better entries from cache into.
87ae8c6e27Sflorian  *   (used when is_referral is false).
88ae8c6e27Sflorian  * @param flags: flags with BIT_CD for AAAA queries in dns64 translation.
89ae8c6e27Sflorian  *   The higher 16 bits are used internally to customize the cache policy.
90ae8c6e27Sflorian  *   (See DNSCACHE_STORE_xxx flags).
916d08cb1bSflorian  * @param qstarttime: time when the query was started, and thus when the
926d08cb1bSflorian  * 	delegations were looked up.
93ae8c6e27Sflorian  * @return 0 on alloc error (out of memory).
94ae8c6e27Sflorian  */
95ae8c6e27Sflorian int dns_cache_store(struct module_env* env, struct query_info* qinf,
96ae8c6e27Sflorian         struct reply_info* rep, int is_referral, time_t leeway, int pside,
976d08cb1bSflorian 	struct regional* region, uint32_t flags, time_t qstarttime);
98ae8c6e27Sflorian 
99ae8c6e27Sflorian /**
100ae8c6e27Sflorian  * Store message in the cache. Stores in message cache and rrset cache.
101ae8c6e27Sflorian  * Both qinfo and rep should be malloced and are put in the cache.
102ae8c6e27Sflorian  * They should not be used after this call, as they are then in shared cache.
103ae8c6e27Sflorian  * Does not return errors, they are logged and only lead to less cache.
104ae8c6e27Sflorian  *
105ae8c6e27Sflorian  * @param env: module environment with the DNS cache.
106ae8c6e27Sflorian  * @param qinfo: query info
107ae8c6e27Sflorian  * @param hash: hash over qinfo.
108ae8c6e27Sflorian  * @param rep: reply info, together with qinfo makes up the message.
109ae8c6e27Sflorian  *	Adjusts the reply info TTLs to absolute time.
110ae8c6e27Sflorian  * @param leeway: TTL value, if not 0, other rrsets are considered expired
111ae8c6e27Sflorian  *	that many seconds before actual TTL expiry.
112ae8c6e27Sflorian  * @param pside: if true, information came from a server which was fetched
113ae8c6e27Sflorian  * 	from the parentside of the zonecut.  This means that the type NS
114ae8c6e27Sflorian  * 	can be updated to full TTL even in prefetch situations.
115ae8c6e27Sflorian  * @param qrep: message that can be altered with better rrs from cache.
116ae8c6e27Sflorian  * @param flags: customization flags for the cache policy.
1176d08cb1bSflorian  * @param qstarttime: time when the query was started, and thus when the
1186d08cb1bSflorian  * 	delegations were looked up.
119ae8c6e27Sflorian  * @param region: to allocate into for qmsg.
120ae8c6e27Sflorian  */
121ae8c6e27Sflorian void dns_cache_store_msg(struct module_env* env, struct query_info* qinfo,
122ae8c6e27Sflorian 	hashvalue_type hash, struct reply_info* rep, time_t leeway, int pside,
1236d08cb1bSflorian 	struct reply_info* qrep, uint32_t flags, struct regional* region,
1246d08cb1bSflorian 	time_t qstarttime);
125ae8c6e27Sflorian 
126ae8c6e27Sflorian /**
127ae8c6e27Sflorian  * Find a delegation from the cache.
128ae8c6e27Sflorian  * @param env: module environment with the DNS cache.
129ae8c6e27Sflorian  * @param qname: query name.
130ae8c6e27Sflorian  * @param qnamelen: length of qname.
131ae8c6e27Sflorian  * @param qtype: query type.
132ae8c6e27Sflorian  * @param qclass: query class.
133ae8c6e27Sflorian  * @param region: where to allocate result delegation.
134ae8c6e27Sflorian  * @param msg: if not NULL, delegation message is returned here, synthesized
135ae8c6e27Sflorian  *	from the cache.
136ae8c6e27Sflorian  * @param timenow: the time now, for checking if TTL on cache entries is OK.
1376d08cb1bSflorian  * @param noexpiredabove: if set, no expired NS rrsets above the one found
1386d08cb1bSflorian  * 	are tolerated. It only returns delegations where the delegations above
1396d08cb1bSflorian  * 	it are valid.
1406d08cb1bSflorian  * @param expiretop: if not NULL, name where check for expiry ends for
1416d08cb1bSflorian  * 	noexpiredabove.
1426d08cb1bSflorian  * @param expiretoplen: length of expiretop dname.
143ae8c6e27Sflorian  * @return new delegation or NULL on error or if not found in cache.
144ae8c6e27Sflorian  */
145ae8c6e27Sflorian struct delegpt* dns_cache_find_delegation(struct module_env* env,
146ae8c6e27Sflorian 	uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass,
1476d08cb1bSflorian 	struct regional* region, struct dns_msg** msg, time_t timenow,
1486d08cb1bSflorian 	int noexpiredabove, uint8_t* expiretop, size_t expiretoplen);
149ae8c6e27Sflorian 
150ae8c6e27Sflorian /**
151ae8c6e27Sflorian  * generate dns_msg from cached message
152ae8c6e27Sflorian  * @param env: module environment with the DNS cache. NULL if the LRU from cache
153ae8c6e27Sflorian  * 	does not need to be touched.
154ae8c6e27Sflorian  * @param q: query info, contains qname that will make up the dns message.
155ae8c6e27Sflorian  * @param r: reply info that, together with qname, will make up the dns message.
156ae8c6e27Sflorian  * @param region: where to allocate dns message.
157ae8c6e27Sflorian  * @param now: the time now, for check if TTL on cache entry is ok.
158d32eb43cSflorian  * @param allow_expired: if true and serve-expired is enabled, it will allow
159d32eb43cSflorian  *	for expired dns_msg to be generated based on the configured serve-expired
160d32eb43cSflorian  *	logic.
161ae8c6e27Sflorian  * @param scratch: where to allocate temporary data.
162ae8c6e27Sflorian  * */
163ae8c6e27Sflorian struct dns_msg* tomsg(struct module_env* env, struct query_info* q,
164ae8c6e27Sflorian 	struct reply_info* r, struct regional* region, time_t now,
165d32eb43cSflorian 	int allow_expired, struct regional* scratch);
166ae8c6e27Sflorian 
167ae8c6e27Sflorian /**
168*fed3efa7Sflorian  * Deep copy a dns_msg to a region.
169*fed3efa7Sflorian  * @param origin: the dns_msg to copy.
170*fed3efa7Sflorian  * @param region: the region to copy all the data to.
171*fed3efa7Sflorian  * @return the new dns_msg or NULL on malloc error.
172*fed3efa7Sflorian  */
173*fed3efa7Sflorian struct dns_msg* dns_msg_deepcopy_region(struct dns_msg* origin,
174*fed3efa7Sflorian 	struct regional* region);
175*fed3efa7Sflorian 
176*fed3efa7Sflorian /**
177ae8c6e27Sflorian  * Find cached message
178ae8c6e27Sflorian  * @param env: module environment with the DNS cache.
179ae8c6e27Sflorian  * @param qname: query name.
180ae8c6e27Sflorian  * @param qnamelen: length of qname.
181ae8c6e27Sflorian  * @param qtype: query type.
182ae8c6e27Sflorian  * @param qclass: query class.
183ae8c6e27Sflorian  * @param flags: flags with BIT_CD for AAAA queries in dns64 translation.
184ae8c6e27Sflorian  * @param region: where to allocate result.
185ae8c6e27Sflorian  * @param scratch: where to allocate temporary data.
186ae8c6e27Sflorian  * @param no_partial: if true, only complete messages and not a partial
187ae8c6e27Sflorian  *	one (with only the start of the CNAME chain and not the rest).
188411c5950Sflorian  * @param dpname: if not NULL, do not return NXDOMAIN above this name.
189411c5950Sflorian  * @param dpnamelen: length of dpname.
190ae8c6e27Sflorian  * @return new response message (alloced in region, rrsets do not have IDs).
191ae8c6e27Sflorian  * 	or NULL on error or if not found in cache.
192ae8c6e27Sflorian  *	TTLs are made relative to the current time.
193ae8c6e27Sflorian  */
194ae8c6e27Sflorian struct dns_msg* dns_cache_lookup(struct module_env* env,
195ae8c6e27Sflorian 	uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass,
196ae8c6e27Sflorian 	uint16_t flags, struct regional* region, struct regional* scratch,
197411c5950Sflorian 	int no_partial, uint8_t* dpname, size_t dpnamelen);
198ae8c6e27Sflorian 
199ae8c6e27Sflorian /**
200ae8c6e27Sflorian  * find and add A and AAAA records for missing nameservers in delegpt
201ae8c6e27Sflorian  * @param env: module environment with rrset cache
202ae8c6e27Sflorian  * @param qclass: which class to look in.
203ae8c6e27Sflorian  * @param region: where to store new dp info.
204ae8c6e27Sflorian  * @param dp: delegation point to fill missing entries.
205ae8c6e27Sflorian  * @return false on alloc failure.
206ae8c6e27Sflorian  */
207ae8c6e27Sflorian int cache_fill_missing(struct module_env* env, uint16_t qclass,
208ae8c6e27Sflorian 	struct regional* region, struct delegpt* dp);
209ae8c6e27Sflorian 
210ae8c6e27Sflorian /**
211ae8c6e27Sflorian  * Utility, create new, unpacked data structure for cache response.
212ae8c6e27Sflorian  * QR bit set, no AA. Query set as indicated. Space for number of rrsets.
213ae8c6e27Sflorian  * @param qname: query section name
214ae8c6e27Sflorian  * @param qnamelen: len of qname
215ae8c6e27Sflorian  * @param qtype: query section type
216ae8c6e27Sflorian  * @param qclass: query section class
217ae8c6e27Sflorian  * @param region: where to alloc.
218ae8c6e27Sflorian  * @param capacity: number of rrsets space to create in the array.
219ae8c6e27Sflorian  * @return new dns_msg struct or NULL on mem fail.
220ae8c6e27Sflorian  */
221ae8c6e27Sflorian struct dns_msg* dns_msg_create(uint8_t* qname, size_t qnamelen, uint16_t qtype,
222ae8c6e27Sflorian 	uint16_t qclass, struct regional* region, size_t capacity);
223ae8c6e27Sflorian 
224ae8c6e27Sflorian /**
225ae8c6e27Sflorian  * Add rrset to authority section in unpacked dns_msg message. Must have enough
226ae8c6e27Sflorian  * space left, does not grow the array.
227ae8c6e27Sflorian  * @param msg: msg to put it in.
228ae8c6e27Sflorian  * @param region: region to alloc in
229ae8c6e27Sflorian  * @param rrset: to add in authority section
230ae8c6e27Sflorian  * @param now: now.
231ae8c6e27Sflorian  * @return true if worked, false on fail
232ae8c6e27Sflorian  */
233ae8c6e27Sflorian int dns_msg_authadd(struct dns_msg* msg, struct regional* region,
234ae8c6e27Sflorian 	struct ub_packed_rrset_key* rrset, time_t now);
235ae8c6e27Sflorian 
236ae8c6e27Sflorian /**
237ae8c6e27Sflorian  * Add rrset to authority section in unpacked dns_msg message. Must have enough
238ae8c6e27Sflorian  * space left, does not grow the array.
239ae8c6e27Sflorian  * @param msg: msg to put it in.
240ae8c6e27Sflorian  * @param region: region to alloc in
241ae8c6e27Sflorian  * @param rrset: to add in authority section
242ae8c6e27Sflorian  * @param now: now.
243ae8c6e27Sflorian  * @return true if worked, false on fail
244ae8c6e27Sflorian  */
245ae8c6e27Sflorian int dns_msg_ansadd(struct dns_msg* msg, struct regional* region,
246ae8c6e27Sflorian 	struct ub_packed_rrset_key* rrset, time_t now);
247ae8c6e27Sflorian 
248ae8c6e27Sflorian /**
249ae8c6e27Sflorian  * Adjust the prefetch_ttl for a cached message.  This adds a value to the
250ae8c6e27Sflorian  * prefetch ttl - postponing the time when it will be prefetched for future
251ae8c6e27Sflorian  * incoming queries.
252ae8c6e27Sflorian  * @param env: module environment with caches and time.
253ae8c6e27Sflorian  * @param qinfo: query info for the query that needs adjustment.
254ae8c6e27Sflorian  * @param adjust: time in seconds to add to the prefetch_leeway.
255ae8c6e27Sflorian  * @param flags: flags with BIT_CD for AAAA queries in dns64 translation.
256ae8c6e27Sflorian  * @return false if not in cache. true if added.
257ae8c6e27Sflorian  */
258ae8c6e27Sflorian int dns_cache_prefetch_adjust(struct module_env* env, struct query_info* qinfo,
259ae8c6e27Sflorian         time_t adjust, uint16_t flags);
260ae8c6e27Sflorian 
261ae8c6e27Sflorian /** lookup message in message cache
262ae8c6e27Sflorian  * the returned nonNULL entry is locked and has to be unlocked by the caller */
263ae8c6e27Sflorian struct msgreply_entry* msg_cache_lookup(struct module_env* env,
264ae8c6e27Sflorian 	uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass,
265ae8c6e27Sflorian 	uint16_t flags, time_t now, int wr);
266ae8c6e27Sflorian 
267ae8c6e27Sflorian /**
268ae8c6e27Sflorian  * Remove entry from the message cache.  For unwanted entries.
269ae8c6e27Sflorian  * @param env: with message cache.
270ae8c6e27Sflorian  * @param qname: query name, in wireformat
271ae8c6e27Sflorian  * @param qnamelen: length of qname, including terminating 0.
272ae8c6e27Sflorian  * @param qtype: query type, host order.
273ae8c6e27Sflorian  * @param qclass: query class, host order.
274ae8c6e27Sflorian  * @param flags: flags
275ae8c6e27Sflorian  */
276ae8c6e27Sflorian void msg_cache_remove(struct module_env* env, uint8_t* qname, size_t qnamelen,
277ae8c6e27Sflorian 	uint16_t qtype, uint16_t qclass, uint16_t flags);
278ae8c6e27Sflorian 
279ae8c6e27Sflorian #endif /* SERVICES_CACHE_DNS_H */
280