xref: /freebsd/contrib/unbound/validator/val_kcache.c (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1 /*
2  * validator/val_kcache.c - validator key shared cache with validated keys
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 functions for dealing with the validator key cache.
40  */
41 #include "config.h"
42 #include "validator/val_kcache.h"
43 #include "validator/val_kentry.h"
44 #include "util/log.h"
45 #include "util/config_file.h"
46 #include "util/data/dname.h"
47 #include "util/module.h"
48 
49 struct key_cache*
50 key_cache_create(struct config_file* cfg)
51 {
52 	struct key_cache* kcache = (struct key_cache*)calloc(1,
53 		sizeof(*kcache));
54 	size_t numtables, start_size, maxmem;
55 	if(!kcache) {
56 		log_err("malloc failure");
57 		return NULL;
58 	}
59 	numtables = cfg->key_cache_slabs;
60 	start_size = HASH_DEFAULT_STARTARRAY;
61 	maxmem = cfg->key_cache_size;
62 	kcache->slab = slabhash_create(numtables, start_size, maxmem,
63 		&key_entry_sizefunc, &key_entry_compfunc,
64 		&key_entry_delkeyfunc, &key_entry_deldatafunc, NULL);
65 	if(!kcache->slab) {
66 		log_err("malloc failure");
67 		free(kcache);
68 		return NULL;
69 	}
70 	return kcache;
71 }
72 
73 void
74 key_cache_delete(struct key_cache* kcache)
75 {
76 	if(!kcache)
77 		return;
78 	slabhash_delete(kcache->slab);
79 	free(kcache);
80 }
81 
82 void
83 key_cache_insert(struct key_cache* kcache, struct key_entry_key* kkey,
84 	struct module_qstate* qstate)
85 {
86 	struct key_entry_key* k = key_entry_copy(kkey);
87 	if(!k)
88 		return;
89 	if(key_entry_isbad(k) && qstate->errinf &&
90 		qstate->env->cfg->val_log_level >= 2) {
91 		/* on malloc failure there is simply no reason string */
92 		key_entry_set_reason(k, errinf_to_str_bogus(qstate));
93 		key_entry_set_reason_bogus(k, errinf_to_reason_bogus(qstate));
94 	}
95 	key_entry_hash(k);
96 	slabhash_insert(kcache->slab, k->entry.hash, &k->entry,
97 		k->entry.data, NULL);
98 }
99 
100 /**
101  * Lookup exactly in the key cache. Returns pointer to locked entry.
102  * Caller must unlock it after use.
103  * @param kcache: the key cache.
104  * @param name: for what name to look; uncompressed wireformat
105  * @param namelen: length of the name.
106  * @param key_class: class of the key.
107  * @param wr: set true to get a writelock.
108  * @return key entry, locked, or NULL if not found. No TTL checking is
109  * 	performed.
110  */
111 static struct key_entry_key*
112 key_cache_search(struct key_cache* kcache, uint8_t* name, size_t namelen,
113 	uint16_t key_class, int wr)
114 {
115 	struct lruhash_entry* e;
116 	struct key_entry_key lookfor;
117 	lookfor.entry.key = &lookfor;
118 	lookfor.name = name;
119 	lookfor.namelen = namelen;
120 	lookfor.key_class = key_class;
121 	key_entry_hash(&lookfor);
122 	e = slabhash_lookup(kcache->slab, lookfor.entry.hash, &lookfor, wr);
123 	if(!e)
124 		return NULL;
125 	return (struct key_entry_key*)e->key;
126 }
127 
128 struct key_entry_key*
129 key_cache_obtain(struct key_cache* kcache, uint8_t* name, size_t namelen,
130 	uint16_t key_class, struct regional* region, time_t now)
131 {
132 	/* keep looking until we find a nonexpired entry */
133 	while(1) {
134 		struct key_entry_key* k = key_cache_search(kcache, name,
135 			namelen, key_class, 0);
136 		if(k) {
137 			/* see if TTL is OK */
138 			struct key_entry_data* d = (struct key_entry_data*)
139 				k->entry.data;
140 			if(now <= d->ttl) {
141 				/* copy and return it */
142 				struct key_entry_key* retkey =
143 					key_entry_copy_toregion(k, region);
144 				lock_rw_unlock(&k->entry.lock);
145 				return retkey;
146 			}
147 			lock_rw_unlock(&k->entry.lock);
148 		}
149 		/* snip off first label to continue */
150 		if(dname_is_root(name))
151 			break;
152 		dname_remove_label(&name, &namelen);
153 	}
154 	return NULL;
155 }
156 
157 size_t
158 key_cache_get_mem(struct key_cache* kcache)
159 {
160 	return sizeof(*kcache) + slabhash_get_mem(kcache->slab);
161 }
162 
163 void key_cache_remove(struct key_cache* kcache,
164 	uint8_t* name, size_t namelen, uint16_t key_class)
165 {
166 	struct key_entry_key lookfor;
167 	lookfor.entry.key = &lookfor;
168 	lookfor.name = name;
169 	lookfor.namelen = namelen;
170 	lookfor.key_class = key_class;
171 	key_entry_hash(&lookfor);
172 	slabhash_remove(kcache->slab, lookfor.entry.hash, &lookfor);
173 }
174