1238106Sdes/* 2238106Sdes * validator/val_kcache.c - validator key shared cache with validated keys 3238106Sdes * 4238106Sdes * Copyright (c) 2007, NLnet Labs. All rights reserved. 5238106Sdes * 6238106Sdes * This software is open source. 7238106Sdes * 8238106Sdes * Redistribution and use in source and binary forms, with or without 9238106Sdes * modification, are permitted provided that the following conditions 10238106Sdes * are met: 11238106Sdes * 12238106Sdes * Redistributions of source code must retain the above copyright notice, 13238106Sdes * this list of conditions and the following disclaimer. 14238106Sdes * 15238106Sdes * Redistributions in binary form must reproduce the above copyright notice, 16238106Sdes * this list of conditions and the following disclaimer in the documentation 17238106Sdes * and/or other materials provided with the distribution. 18238106Sdes * 19238106Sdes * Neither the name of the NLNET LABS nor the names of its contributors may 20238106Sdes * be used to endorse or promote products derived from this software without 21238106Sdes * specific prior written permission. 22238106Sdes * 23238106Sdes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24269257Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25269257Sdes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26269257Sdes * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27269257Sdes * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28269257Sdes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 29269257Sdes * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30269257Sdes * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31269257Sdes * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32269257Sdes * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33269257Sdes * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34238106Sdes */ 35238106Sdes 36238106Sdes/** 37238106Sdes * \file 38238106Sdes * 39238106Sdes * This file contains functions for dealing with the validator key cache. 40238106Sdes */ 41238106Sdes#include "config.h" 42238106Sdes#include "validator/val_kcache.h" 43238106Sdes#include "validator/val_kentry.h" 44238106Sdes#include "util/log.h" 45238106Sdes#include "util/config_file.h" 46238106Sdes#include "util/data/dname.h" 47238106Sdes#include "util/module.h" 48238106Sdes 49238106Sdesstruct key_cache* 50238106Sdeskey_cache_create(struct config_file* cfg) 51238106Sdes{ 52238106Sdes struct key_cache* kcache = (struct key_cache*)calloc(1, 53238106Sdes sizeof(*kcache)); 54238106Sdes size_t numtables, start_size, maxmem; 55238106Sdes if(!kcache) { 56238106Sdes log_err("malloc failure"); 57238106Sdes return NULL; 58238106Sdes } 59238106Sdes numtables = cfg->key_cache_slabs; 60238106Sdes start_size = HASH_DEFAULT_STARTARRAY; 61238106Sdes maxmem = cfg->key_cache_size; 62238106Sdes kcache->slab = slabhash_create(numtables, start_size, maxmem, 63238106Sdes &key_entry_sizefunc, &key_entry_compfunc, 64238106Sdes &key_entry_delkeyfunc, &key_entry_deldatafunc, NULL); 65238106Sdes if(!kcache->slab) { 66238106Sdes log_err("malloc failure"); 67238106Sdes free(kcache); 68238106Sdes return NULL; 69238106Sdes } 70238106Sdes return kcache; 71238106Sdes} 72238106Sdes 73238106Sdesvoid 74238106Sdeskey_cache_delete(struct key_cache* kcache) 75238106Sdes{ 76238106Sdes if(!kcache) 77238106Sdes return; 78238106Sdes slabhash_delete(kcache->slab); 79238106Sdes free(kcache); 80238106Sdes} 81238106Sdes 82238106Sdesvoid 83238106Sdeskey_cache_insert(struct key_cache* kcache, struct key_entry_key* kkey, 84238106Sdes struct module_qstate* qstate) 85238106Sdes{ 86238106Sdes struct key_entry_key* k = key_entry_copy(kkey); 87238106Sdes if(!k) 88238106Sdes return; 89238106Sdes if(key_entry_isbad(k) && qstate->errinf && 90238106Sdes qstate->env->cfg->val_log_level >= 2) { 91238106Sdes /* on malloc failure there is simply no reason string */ 92238106Sdes key_entry_set_reason(k, errinf_to_str(qstate)); 93238106Sdes } 94238106Sdes key_entry_hash(k); 95238106Sdes slabhash_insert(kcache->slab, k->entry.hash, &k->entry, 96238106Sdes k->entry.data, NULL); 97238106Sdes} 98238106Sdes 99238106Sdes/** 100238106Sdes * Lookup exactly in the key cache. Returns pointer to locked entry. 101238106Sdes * Caller must unlock it after use. 102238106Sdes * @param kcache: the key cache. 103238106Sdes * @param name: for what name to look; uncompressed wireformat 104238106Sdes * @param namelen: length of the name. 105238106Sdes * @param key_class: class of the key. 106238106Sdes * @param wr: set true to get a writelock. 107238106Sdes * @return key entry, locked, or NULL if not found. No TTL checking is 108238106Sdes * performed. 109238106Sdes */ 110238106Sdesstatic struct key_entry_key* 111238106Sdeskey_cache_search(struct key_cache* kcache, uint8_t* name, size_t namelen, 112238106Sdes uint16_t key_class, int wr) 113238106Sdes{ 114238106Sdes struct lruhash_entry* e; 115238106Sdes struct key_entry_key lookfor; 116238106Sdes lookfor.entry.key = &lookfor; 117238106Sdes lookfor.name = name; 118238106Sdes lookfor.namelen = namelen; 119238106Sdes lookfor.key_class = key_class; 120238106Sdes key_entry_hash(&lookfor); 121238106Sdes e = slabhash_lookup(kcache->slab, lookfor.entry.hash, &lookfor, wr); 122238106Sdes if(!e) 123238106Sdes return NULL; 124238106Sdes return (struct key_entry_key*)e->key; 125238106Sdes} 126238106Sdes 127238106Sdesstruct key_entry_key* 128238106Sdeskey_cache_obtain(struct key_cache* kcache, uint8_t* name, size_t namelen, 129269257Sdes uint16_t key_class, struct regional* region, time_t now) 130238106Sdes{ 131238106Sdes /* keep looking until we find a nonexpired entry */ 132238106Sdes while(1) { 133238106Sdes struct key_entry_key* k = key_cache_search(kcache, name, 134238106Sdes namelen, key_class, 0); 135238106Sdes if(k) { 136238106Sdes /* see if TTL is OK */ 137238106Sdes struct key_entry_data* d = (struct key_entry_data*) 138238106Sdes k->entry.data; 139238106Sdes if(now <= d->ttl) { 140238106Sdes /* copy and return it */ 141238106Sdes struct key_entry_key* retkey = 142238106Sdes key_entry_copy_toregion(k, region); 143238106Sdes lock_rw_unlock(&k->entry.lock); 144238106Sdes return retkey; 145238106Sdes } 146238106Sdes lock_rw_unlock(&k->entry.lock); 147238106Sdes } 148238106Sdes /* snip off first label to continue */ 149238106Sdes if(dname_is_root(name)) 150238106Sdes break; 151238106Sdes dname_remove_label(&name, &namelen); 152238106Sdes } 153238106Sdes return NULL; 154238106Sdes} 155238106Sdes 156238106Sdessize_t 157238106Sdeskey_cache_get_mem(struct key_cache* kcache) 158238106Sdes{ 159238106Sdes return sizeof(*kcache) + slabhash_get_mem(kcache->slab); 160238106Sdes} 161238106Sdes 162238106Sdesvoid key_cache_remove(struct key_cache* kcache, 163238106Sdes uint8_t* name, size_t namelen, uint16_t key_class) 164238106Sdes{ 165238106Sdes struct key_entry_key lookfor; 166238106Sdes lookfor.entry.key = &lookfor; 167238106Sdes lookfor.name = name; 168238106Sdes lookfor.namelen = namelen; 169238106Sdes lookfor.key_class = key_class; 170238106Sdes key_entry_hash(&lookfor); 171238106Sdes slabhash_remove(kcache->slab, lookfor.entry.hash, &lookfor); 172238106Sdes} 173