Home | History | Annotate | Line # | Download | only in validator
      1      1.1  christos /*
      2      1.1  christos  * validator/val_neg.c - validator aggressive negative caching functions.
      3      1.1  christos  *
      4      1.1  christos  * Copyright (c) 2008, NLnet Labs. All rights reserved.
      5      1.1  christos  *
      6      1.1  christos  * This software is open source.
      7      1.1  christos  *
      8      1.1  christos  * Redistribution and use in source and binary forms, with or without
      9      1.1  christos  * modification, are permitted provided that the following conditions
     10      1.1  christos  * are met:
     11      1.1  christos  *
     12      1.1  christos  * Redistributions of source code must retain the above copyright notice,
     13      1.1  christos  * this list of conditions and the following disclaimer.
     14      1.1  christos  *
     15      1.1  christos  * Redistributions in binary form must reproduce the above copyright notice,
     16      1.1  christos  * this list of conditions and the following disclaimer in the documentation
     17      1.1  christos  * and/or other materials provided with the distribution.
     18      1.1  christos  *
     19      1.1  christos  * Neither the name of the NLNET LABS nor the names of its contributors may
     20      1.1  christos  * be used to endorse or promote products derived from this software without
     21      1.1  christos  * specific prior written permission.
     22      1.1  christos  *
     23      1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     24      1.1  christos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     25      1.1  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     26      1.1  christos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     27      1.1  christos  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     28      1.1  christos  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
     29      1.1  christos  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     30      1.1  christos  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     31      1.1  christos  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     32      1.1  christos  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     33      1.1  christos  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34      1.1  christos  */
     35      1.1  christos 
     36      1.1  christos /**
     37      1.1  christos  * \file
     38      1.1  christos  *
     39      1.1  christos  * This file contains helper functions for the validator module.
     40      1.1  christos  * The functions help with aggressive negative caching.
     41      1.1  christos  * This creates new denials of existence, and proofs for absence of types
     42      1.1  christos  * from cached NSEC records.
     43      1.1  christos  */
     44      1.1  christos #include "config.h"
     45      1.1  christos #ifdef HAVE_OPENSSL_SSL_H
     46  1.1.1.6  christos #include <openssl/ssl.h>
     47      1.1  christos #define NSEC3_SHA_LEN SHA_DIGEST_LENGTH
     48      1.1  christos #else
     49      1.1  christos #define NSEC3_SHA_LEN 20
     50      1.1  christos #endif
     51      1.1  christos #include "validator/val_neg.h"
     52      1.1  christos #include "validator/val_nsec.h"
     53      1.1  christos #include "validator/val_nsec3.h"
     54      1.1  christos #include "validator/val_utils.h"
     55      1.1  christos #include "util/data/dname.h"
     56      1.1  christos #include "util/data/msgreply.h"
     57      1.1  christos #include "util/log.h"
     58      1.1  christos #include "util/net_help.h"
     59      1.1  christos #include "util/config_file.h"
     60      1.1  christos #include "services/cache/rrset.h"
     61      1.1  christos #include "services/cache/dns.h"
     62      1.1  christos #include "sldns/rrdef.h"
     63      1.1  christos #include "sldns/sbuffer.h"
     64      1.1  christos 
     65      1.1  christos int val_neg_data_compare(const void* a, const void* b)
     66      1.1  christos {
     67      1.1  christos 	struct val_neg_data* x = (struct val_neg_data*)a;
     68      1.1  christos 	struct val_neg_data* y = (struct val_neg_data*)b;
     69      1.1  christos 	int m;
     70      1.1  christos 	return dname_canon_lab_cmp(x->name, x->labs, y->name, y->labs, &m);
     71      1.1  christos }
     72      1.1  christos 
     73      1.1  christos int val_neg_zone_compare(const void* a, const void* b)
     74      1.1  christos {
     75      1.1  christos 	struct val_neg_zone* x = (struct val_neg_zone*)a;
     76      1.1  christos 	struct val_neg_zone* y = (struct val_neg_zone*)b;
     77      1.1  christos 	int m;
     78      1.1  christos 	if(x->dclass != y->dclass) {
     79      1.1  christos 		if(x->dclass < y->dclass)
     80      1.1  christos 			return -1;
     81      1.1  christos 		return 1;
     82      1.1  christos 	}
     83      1.1  christos 	return dname_canon_lab_cmp(x->name, x->labs, y->name, y->labs, &m);
     84      1.1  christos }
     85      1.1  christos 
     86      1.1  christos struct val_neg_cache* val_neg_create(struct config_file* cfg, size_t maxiter)
     87      1.1  christos {
     88      1.1  christos 	struct val_neg_cache* neg = (struct val_neg_cache*)calloc(1,
     89      1.1  christos 		sizeof(*neg));
     90      1.1  christos 	if(!neg) {
     91      1.1  christos 		log_err("Could not create neg cache: out of memory");
     92      1.1  christos 		return NULL;
     93      1.1  christos 	}
     94      1.1  christos 	neg->nsec3_max_iter = maxiter;
     95      1.1  christos 	neg->max = 1024*1024; /* 1 M is thousands of entries */
     96      1.1  christos 	if(cfg) neg->max = cfg->neg_cache_size;
     97      1.1  christos 	rbtree_init(&neg->tree, &val_neg_zone_compare);
     98      1.1  christos 	lock_basic_init(&neg->lock);
     99      1.1  christos 	lock_protect(&neg->lock, neg, sizeof(*neg));
    100      1.1  christos 	return neg;
    101      1.1  christos }
    102      1.1  christos 
    103      1.1  christos size_t val_neg_get_mem(struct val_neg_cache* neg)
    104      1.1  christos {
    105      1.1  christos 	size_t result;
    106      1.1  christos 	lock_basic_lock(&neg->lock);
    107      1.1  christos 	result = sizeof(*neg) + neg->use;
    108      1.1  christos 	lock_basic_unlock(&neg->lock);
    109      1.1  christos 	return result;
    110      1.1  christos }
    111      1.1  christos 
    112      1.1  christos /** clear datas on cache deletion */
    113      1.1  christos static void
    114  1.1.1.2  christos neg_clear_datas(rbnode_type* n, void* ATTR_UNUSED(arg))
    115      1.1  christos {
    116      1.1  christos 	struct val_neg_data* d = (struct val_neg_data*)n;
    117      1.1  christos 	free(d->name);
    118      1.1  christos 	free(d);
    119      1.1  christos }
    120      1.1  christos 
    121      1.1  christos /** clear zones on cache deletion */
    122      1.1  christos static void
    123  1.1.1.2  christos neg_clear_zones(rbnode_type* n, void* ATTR_UNUSED(arg))
    124      1.1  christos {
    125      1.1  christos 	struct val_neg_zone* z = (struct val_neg_zone*)n;
    126      1.1  christos 	/* delete all the rrset entries in the tree */
    127      1.1  christos 	traverse_postorder(&z->tree, &neg_clear_datas, NULL);
    128      1.1  christos 	free(z->nsec3_salt);
    129      1.1  christos 	free(z->name);
    130      1.1  christos 	free(z);
    131      1.1  christos }
    132      1.1  christos 
    133      1.1  christos void neg_cache_delete(struct val_neg_cache* neg)
    134      1.1  christos {
    135      1.1  christos 	if(!neg) return;
    136      1.1  christos 	lock_basic_destroy(&neg->lock);
    137      1.1  christos 	/* delete all the zones in the tree */
    138      1.1  christos 	traverse_postorder(&neg->tree, &neg_clear_zones, NULL);
    139      1.1  christos 	free(neg);
    140      1.1  christos }
    141      1.1  christos 
    142      1.1  christos /**
    143      1.1  christos  * Put data element at the front of the LRU list.
    144      1.1  christos  * @param neg: negative cache with LRU start and end.
    145      1.1  christos  * @param data: this data is fronted.
    146      1.1  christos  */
    147      1.1  christos static void neg_lru_front(struct val_neg_cache* neg,
    148      1.1  christos 	struct val_neg_data* data)
    149      1.1  christos {
    150      1.1  christos 	data->prev = NULL;
    151      1.1  christos 	data->next = neg->first;
    152      1.1  christos 	if(!neg->first)
    153      1.1  christos 		neg->last = data;
    154      1.1  christos 	else	neg->first->prev = data;
    155      1.1  christos 	neg->first = data;
    156      1.1  christos }
    157      1.1  christos 
    158      1.1  christos /**
    159      1.1  christos  * Remove data element from LRU list.
    160      1.1  christos  * @param neg: negative cache with LRU start and end.
    161      1.1  christos  * @param data: this data is removed from the list.
    162      1.1  christos  */
    163      1.1  christos static void neg_lru_remove(struct val_neg_cache* neg,
    164      1.1  christos 	struct val_neg_data* data)
    165      1.1  christos {
    166      1.1  christos 	if(data->prev)
    167      1.1  christos 		data->prev->next = data->next;
    168      1.1  christos 	else	neg->first = data->next;
    169      1.1  christos 	if(data->next)
    170      1.1  christos 		data->next->prev = data->prev;
    171      1.1  christos 	else	neg->last = data->prev;
    172      1.1  christos }
    173      1.1  christos 
    174      1.1  christos /**
    175      1.1  christos  * Touch LRU for data element, put it at the start of the LRU list.
    176      1.1  christos  * @param neg: negative cache with LRU start and end.
    177      1.1  christos  * @param data: this data is used.
    178      1.1  christos  */
    179      1.1  christos static void neg_lru_touch(struct val_neg_cache* neg,
    180      1.1  christos 	struct val_neg_data* data)
    181      1.1  christos {
    182      1.1  christos 	if(data == neg->first)
    183      1.1  christos 		return; /* nothing to do */
    184      1.1  christos 	/* remove from current lru position */
    185      1.1  christos 	neg_lru_remove(neg, data);
    186      1.1  christos 	/* add at front */
    187      1.1  christos 	neg_lru_front(neg, data);
    188      1.1  christos }
    189      1.1  christos 
    190      1.1  christos /**
    191      1.1  christos  * Delete a zone element from the negative cache.
    192      1.1  christos  * May delete other zone elements to keep tree coherent, or
    193      1.1  christos  * only mark the element as 'not in use'.
    194      1.1  christos  * @param neg: negative cache.
    195      1.1  christos  * @param z: zone element to delete.
    196      1.1  christos  */
    197      1.1  christos static void neg_delete_zone(struct val_neg_cache* neg, struct val_neg_zone* z)
    198      1.1  christos {
    199      1.1  christos 	struct val_neg_zone* p, *np;
    200      1.1  christos 	if(!z) return;
    201      1.1  christos 	log_assert(z->in_use);
    202      1.1  christos 	log_assert(z->count > 0);
    203      1.1  christos 	z->in_use = 0;
    204      1.1  christos 
    205      1.1  christos 	/* go up the tree and reduce counts */
    206      1.1  christos 	p = z;
    207      1.1  christos 	while(p) {
    208      1.1  christos 		log_assert(p->count > 0);
    209      1.1  christos 		p->count --;
    210      1.1  christos 		p = p->parent;
    211      1.1  christos 	}
    212      1.1  christos 
    213      1.1  christos 	/* remove zones with zero count */
    214      1.1  christos 	p = z;
    215      1.1  christos 	while(p && p->count == 0) {
    216      1.1  christos 		np = p->parent;
    217      1.1  christos 		(void)rbtree_delete(&neg->tree, &p->node);
    218      1.1  christos 		neg->use -= p->len + sizeof(*p);
    219      1.1  christos 		free(p->nsec3_salt);
    220      1.1  christos 		free(p->name);
    221      1.1  christos 		free(p);
    222      1.1  christos 		p = np;
    223      1.1  christos 	}
    224      1.1  christos }
    225      1.1  christos 
    226      1.1  christos void neg_delete_data(struct val_neg_cache* neg, struct val_neg_data* el)
    227      1.1  christos {
    228      1.1  christos 	struct val_neg_zone* z;
    229      1.1  christos 	struct val_neg_data* p, *np;
    230      1.1  christos 	if(!el) return;
    231      1.1  christos 	z = el->zone;
    232      1.1  christos 	log_assert(el->in_use);
    233      1.1  christos 	log_assert(el->count > 0);
    234      1.1  christos 	el->in_use = 0;
    235      1.1  christos 
    236      1.1  christos 	/* remove it from the lru list */
    237      1.1  christos 	neg_lru_remove(neg, el);
    238  1.1.1.4  christos 	log_assert(neg->first != el && neg->last != el);
    239      1.1  christos 
    240      1.1  christos 	/* go up the tree and reduce counts */
    241      1.1  christos 	p = el;
    242      1.1  christos 	while(p) {
    243      1.1  christos 		log_assert(p->count > 0);
    244      1.1  christos 		p->count --;
    245      1.1  christos 		p = p->parent;
    246      1.1  christos 	}
    247      1.1  christos 
    248      1.1  christos 	/* delete 0 count items from tree */
    249      1.1  christos 	p = el;
    250      1.1  christos 	while(p && p->count == 0) {
    251      1.1  christos 		np = p->parent;
    252      1.1  christos 		(void)rbtree_delete(&z->tree, &p->node);
    253      1.1  christos 		neg->use -= p->len + sizeof(*p);
    254      1.1  christos 		free(p->name);
    255      1.1  christos 		free(p);
    256      1.1  christos 		p = np;
    257      1.1  christos 	}
    258      1.1  christos 
    259      1.1  christos 	/* check if the zone is now unused */
    260      1.1  christos 	if(z->tree.count == 0) {
    261      1.1  christos 		neg_delete_zone(neg, z);
    262      1.1  christos 	}
    263      1.1  christos }
    264      1.1  christos 
    265      1.1  christos /**
    266      1.1  christos  * Create more space in negative cache
    267      1.1  christos  * The oldest elements are deleted until enough space is present.
    268      1.1  christos  * Empty zones are deleted.
    269      1.1  christos  * @param neg: negative cache.
    270      1.1  christos  * @param need: how many bytes are needed.
    271      1.1  christos  */
    272      1.1  christos static void neg_make_space(struct val_neg_cache* neg, size_t need)
    273      1.1  christos {
    274      1.1  christos 	/* delete elements until enough space or its empty */
    275      1.1  christos 	while(neg->last && neg->max < neg->use + need) {
    276      1.1  christos 		neg_delete_data(neg, neg->last);
    277      1.1  christos 	}
    278      1.1  christos }
    279      1.1  christos 
    280      1.1  christos struct val_neg_zone* neg_find_zone(struct val_neg_cache* neg,
    281      1.1  christos 	uint8_t* nm, size_t len, uint16_t dclass)
    282      1.1  christos {
    283      1.1  christos 	struct val_neg_zone lookfor;
    284      1.1  christos 	struct val_neg_zone* result;
    285      1.1  christos 	lookfor.node.key = &lookfor;
    286      1.1  christos 	lookfor.name = nm;
    287      1.1  christos 	lookfor.len = len;
    288      1.1  christos 	lookfor.labs = dname_count_labels(lookfor.name);
    289      1.1  christos 	lookfor.dclass = dclass;
    290      1.1  christos 
    291      1.1  christos 	result = (struct val_neg_zone*)
    292      1.1  christos 		rbtree_search(&neg->tree, lookfor.node.key);
    293      1.1  christos 	return result;
    294      1.1  christos }
    295      1.1  christos 
    296      1.1  christos /**
    297      1.1  christos  * Find the given data
    298      1.1  christos  * @param zone: negative zone
    299      1.1  christos  * @param nm: what to look for.
    300      1.1  christos  * @param len: length of nm
    301      1.1  christos  * @param labs: labels in nm
    302      1.1  christos  * @return data or NULL if not found.
    303      1.1  christos  */
    304      1.1  christos static struct val_neg_data* neg_find_data(struct val_neg_zone* zone,
    305      1.1  christos 	uint8_t* nm, size_t len, int labs)
    306      1.1  christos {
    307      1.1  christos 	struct val_neg_data lookfor;
    308      1.1  christos 	struct val_neg_data* result;
    309      1.1  christos 	lookfor.node.key = &lookfor;
    310      1.1  christos 	lookfor.name = nm;
    311      1.1  christos 	lookfor.len = len;
    312      1.1  christos 	lookfor.labs = labs;
    313      1.1  christos 
    314      1.1  christos 	result = (struct val_neg_data*)
    315      1.1  christos 		rbtree_search(&zone->tree, lookfor.node.key);
    316      1.1  christos 	return result;
    317      1.1  christos }
    318      1.1  christos 
    319      1.1  christos /**
    320      1.1  christos  * Calculate space needed for the data and all its parents
    321      1.1  christos  * @param rep: NSEC entries.
    322      1.1  christos  * @return size.
    323      1.1  christos  */
    324      1.1  christos static size_t calc_data_need(struct reply_info* rep)
    325      1.1  christos {
    326      1.1  christos 	uint8_t* d;
    327      1.1  christos 	size_t i, len, res = 0;
    328      1.1  christos 
    329      1.1  christos 	for(i=rep->an_numrrsets; i<rep->an_numrrsets+rep->ns_numrrsets; i++) {
    330      1.1  christos 		if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_NSEC) {
    331      1.1  christos 			d = rep->rrsets[i]->rk.dname;
    332      1.1  christos 			len = rep->rrsets[i]->rk.dname_len;
    333      1.1  christos 			res = sizeof(struct val_neg_data) + len;
    334      1.1  christos 			while(!dname_is_root(d)) {
    335      1.1  christos 				log_assert(len > 1); /* not root label */
    336      1.1  christos 				dname_remove_label(&d, &len);
    337      1.1  christos 				res += sizeof(struct val_neg_data) + len;
    338      1.1  christos 			}
    339      1.1  christos 		}
    340      1.1  christos 	}
    341      1.1  christos 	return res;
    342      1.1  christos }
    343      1.1  christos 
    344      1.1  christos /**
    345      1.1  christos  * Calculate space needed for zone and all its parents
    346      1.1  christos  * @param d: name of zone
    347      1.1  christos  * @param len: length of name
    348      1.1  christos  * @return size.
    349      1.1  christos  */
    350      1.1  christos static size_t calc_zone_need(uint8_t* d, size_t len)
    351      1.1  christos {
    352      1.1  christos 	size_t res = sizeof(struct val_neg_zone) + len;
    353      1.1  christos 	while(!dname_is_root(d)) {
    354      1.1  christos 		log_assert(len > 1); /* not root label */
    355      1.1  christos 		dname_remove_label(&d, &len);
    356      1.1  christos 		res += sizeof(struct val_neg_zone) + len;
    357      1.1  christos 	}
    358      1.1  christos 	return res;
    359      1.1  christos }
    360      1.1  christos 
    361      1.1  christos /**
    362      1.1  christos  * Find closest existing parent zone of the given name.
    363      1.1  christos  * @param neg: negative cache.
    364      1.1  christos  * @param nm: name to look for
    365      1.1  christos  * @param nm_len: length of nm
    366      1.1  christos  * @param labs: labelcount of nm.
    367      1.1  christos  * @param qclass: class.
    368      1.1  christos  * @return the zone or NULL if none found.
    369      1.1  christos  */
    370      1.1  christos static struct val_neg_zone* neg_closest_zone_parent(struct val_neg_cache* neg,
    371      1.1  christos 	uint8_t* nm, size_t nm_len, int labs, uint16_t qclass)
    372      1.1  christos {
    373      1.1  christos 	struct val_neg_zone key;
    374      1.1  christos 	struct val_neg_zone* result;
    375  1.1.1.2  christos 	rbnode_type* res = NULL;
    376      1.1  christos 	key.node.key = &key;
    377      1.1  christos 	key.name = nm;
    378      1.1  christos 	key.len = nm_len;
    379      1.1  christos 	key.labs = labs;
    380      1.1  christos 	key.dclass = qclass;
    381      1.1  christos 	if(rbtree_find_less_equal(&neg->tree, &key, &res)) {
    382      1.1  christos 		/* exact match */
    383      1.1  christos 		result = (struct val_neg_zone*)res;
    384      1.1  christos 	} else {
    385      1.1  christos 		/* smaller element (or no element) */
    386      1.1  christos 		int m;
    387      1.1  christos 		result = (struct val_neg_zone*)res;
    388      1.1  christos 		if(!result || result->dclass != qclass)
    389      1.1  christos 			return NULL;
    390      1.1  christos 		/* count number of labels matched */
    391      1.1  christos 		(void)dname_lab_cmp(result->name, result->labs, key.name,
    392      1.1  christos 			key.labs, &m);
    393      1.1  christos 		while(result) { /* go up until qname is subdomain of stub */
    394      1.1  christos 			if(result->labs <= m)
    395      1.1  christos 				break;
    396      1.1  christos 			result = result->parent;
    397      1.1  christos 		}
    398      1.1  christos 	}
    399      1.1  christos 	return result;
    400      1.1  christos }
    401      1.1  christos 
    402      1.1  christos /**
    403      1.1  christos  * Find closest existing parent data for the given name.
    404      1.1  christos  * @param zone: to look in.
    405      1.1  christos  * @param nm: name to look for
    406      1.1  christos  * @param nm_len: length of nm
    407      1.1  christos  * @param labs: labelcount of nm.
    408      1.1  christos  * @return the data or NULL if none found.
    409      1.1  christos  */
    410      1.1  christos static struct val_neg_data* neg_closest_data_parent(
    411      1.1  christos 	struct val_neg_zone* zone, uint8_t* nm, size_t nm_len, int labs)
    412      1.1  christos {
    413      1.1  christos 	struct val_neg_data key;
    414      1.1  christos 	struct val_neg_data* result;
    415  1.1.1.2  christos 	rbnode_type* res = NULL;
    416      1.1  christos 	key.node.key = &key;
    417      1.1  christos 	key.name = nm;
    418      1.1  christos 	key.len = nm_len;
    419      1.1  christos 	key.labs = labs;
    420      1.1  christos 	if(rbtree_find_less_equal(&zone->tree, &key, &res)) {
    421      1.1  christos 		/* exact match */
    422      1.1  christos 		result = (struct val_neg_data*)res;
    423      1.1  christos 	} else {
    424      1.1  christos 		/* smaller element (or no element) */
    425      1.1  christos 		int m;
    426      1.1  christos 		result = (struct val_neg_data*)res;
    427      1.1  christos 		if(!result)
    428      1.1  christos 			return NULL;
    429      1.1  christos 		/* count number of labels matched */
    430      1.1  christos 		(void)dname_lab_cmp(result->name, result->labs, key.name,
    431      1.1  christos 			key.labs, &m);
    432      1.1  christos 		while(result) { /* go up until qname is subdomain of stub */
    433      1.1  christos 			if(result->labs <= m)
    434      1.1  christos 				break;
    435      1.1  christos 			result = result->parent;
    436      1.1  christos 		}
    437      1.1  christos 	}
    438      1.1  christos 	return result;
    439      1.1  christos }
    440      1.1  christos 
    441      1.1  christos /**
    442      1.1  christos  * Create a single zone node
    443      1.1  christos  * @param nm: name for zone (copied)
    444      1.1  christos  * @param nm_len: length of name
    445      1.1  christos  * @param labs: labels in name.
    446      1.1  christos  * @param dclass: class of zone, host order.
    447      1.1  christos  * @return new zone or NULL on failure
    448      1.1  christos  */
    449      1.1  christos static struct val_neg_zone* neg_setup_zone_node(
    450      1.1  christos 	uint8_t* nm, size_t nm_len, int labs, uint16_t dclass)
    451      1.1  christos {
    452      1.1  christos 	struct val_neg_zone* zone =
    453      1.1  christos 		(struct val_neg_zone*)calloc(1, sizeof(*zone));
    454      1.1  christos 	if(!zone) {
    455      1.1  christos 		return NULL;
    456      1.1  christos 	}
    457      1.1  christos 	zone->node.key = zone;
    458      1.1  christos 	zone->name = memdup(nm, nm_len);
    459      1.1  christos 	if(!zone->name) {
    460      1.1  christos 		free(zone);
    461      1.1  christos 		return NULL;
    462      1.1  christos 	}
    463      1.1  christos 	zone->len = nm_len;
    464      1.1  christos 	zone->labs = labs;
    465      1.1  christos 	zone->dclass = dclass;
    466      1.1  christos 
    467      1.1  christos 	rbtree_init(&zone->tree, &val_neg_data_compare);
    468      1.1  christos 	return zone;
    469      1.1  christos }
    470      1.1  christos 
    471      1.1  christos /**
    472      1.1  christos  * Create a linked list of parent zones, starting at longname ending on
    473      1.1  christos  * the parent (can be NULL, creates to the root).
    474      1.1  christos  * @param nm: name for lowest in chain
    475      1.1  christos  * @param nm_len: length of name
    476      1.1  christos  * @param labs: labels in name.
    477      1.1  christos  * @param dclass: class of zone.
    478      1.1  christos  * @param parent: NULL for to root, else so it fits under here.
    479      1.1  christos  * @return zone; a chain of zones and their parents up to the parent.
    480      1.1  christos  *  	or NULL on malloc failure
    481      1.1  christos  */
    482      1.1  christos static struct val_neg_zone* neg_zone_chain(
    483      1.1  christos 	uint8_t* nm, size_t nm_len, int labs, uint16_t dclass,
    484      1.1  christos 	struct val_neg_zone* parent)
    485      1.1  christos {
    486      1.1  christos 	int i;
    487      1.1  christos 	int tolabs = parent?parent->labs:0;
    488      1.1  christos 	struct val_neg_zone* zone, *prev = NULL, *first = NULL;
    489      1.1  christos 
    490      1.1  christos 	/* create the new subtree, i is labelcount of current creation */
    491      1.1  christos 	/* this creates a 'first' to z->parent=NULL list of zones */
    492      1.1  christos 	for(i=labs; i!=tolabs; i--) {
    493      1.1  christos 		/* create new item */
    494      1.1  christos 		zone = neg_setup_zone_node(nm, nm_len, i, dclass);
    495      1.1  christos 		if(!zone) {
    496      1.1  christos 			/* need to delete other allocations in this routine!*/
    497      1.1  christos 			struct val_neg_zone* p=first, *np;
    498      1.1  christos 			while(p) {
    499      1.1  christos 				np = p->parent;
    500      1.1  christos 				free(p->name);
    501      1.1  christos 				free(p);
    502      1.1  christos 				p = np;
    503      1.1  christos 			}
    504      1.1  christos 			return NULL;
    505      1.1  christos 		}
    506      1.1  christos 		if(i == labs) {
    507      1.1  christos 			first = zone;
    508      1.1  christos 		} else {
    509      1.1  christos 			prev->parent = zone;
    510      1.1  christos 		}
    511      1.1  christos 		/* prepare for next name */
    512      1.1  christos 		prev = zone;
    513      1.1  christos 		dname_remove_label(&nm, &nm_len);
    514      1.1  christos 	}
    515      1.1  christos 	return first;
    516      1.1  christos }
    517      1.1  christos 
    518      1.1  christos void val_neg_zone_take_inuse(struct val_neg_zone* zone)
    519      1.1  christos {
    520      1.1  christos 	if(!zone->in_use) {
    521      1.1  christos 		struct val_neg_zone* p;
    522      1.1  christos 		zone->in_use = 1;
    523      1.1  christos 		/* increase usage count of all parents */
    524      1.1  christos 		for(p=zone; p; p = p->parent) {
    525      1.1  christos 			p->count++;
    526      1.1  christos 		}
    527      1.1  christos 	}
    528      1.1  christos }
    529      1.1  christos 
    530      1.1  christos struct val_neg_zone* neg_create_zone(struct val_neg_cache* neg,
    531      1.1  christos 	uint8_t* nm, size_t nm_len, uint16_t dclass)
    532      1.1  christos {
    533      1.1  christos 	struct val_neg_zone* zone;
    534      1.1  christos 	struct val_neg_zone* parent;
    535      1.1  christos 	struct val_neg_zone* p, *np;
    536      1.1  christos 	int labs = dname_count_labels(nm);
    537      1.1  christos 
    538      1.1  christos 	/* find closest enclosing parent zone that (still) exists */
    539      1.1  christos 	parent = neg_closest_zone_parent(neg, nm, nm_len, labs, dclass);
    540      1.1  christos 	if(parent && query_dname_compare(parent->name, nm) == 0)
    541      1.1  christos 		return parent; /* already exists, weird */
    542      1.1  christos 	/* if parent exists, it is in use */
    543      1.1  christos 	log_assert(!parent || parent->count > 0);
    544      1.1  christos 	zone = neg_zone_chain(nm, nm_len, labs, dclass, parent);
    545      1.1  christos 	if(!zone) {
    546      1.1  christos 		return NULL;
    547      1.1  christos 	}
    548      1.1  christos 
    549      1.1  christos 	/* insert the list of zones into the tree */
    550      1.1  christos 	p = zone;
    551      1.1  christos 	while(p) {
    552      1.1  christos 		np = p->parent;
    553      1.1  christos 		/* mem use */
    554      1.1  christos 		neg->use += sizeof(struct val_neg_zone) + p->len;
    555      1.1  christos 		/* insert in tree */
    556      1.1  christos 		(void)rbtree_insert(&neg->tree, &p->node);
    557      1.1  christos 		/* last one needs proper parent pointer */
    558      1.1  christos 		if(np == NULL)
    559      1.1  christos 			p->parent = parent;
    560      1.1  christos 		p = np;
    561      1.1  christos 	}
    562      1.1  christos 	return zone;
    563      1.1  christos }
    564      1.1  christos 
    565      1.1  christos /** find zone name of message, returns the SOA record */
    566      1.1  christos static struct ub_packed_rrset_key* reply_find_soa(struct reply_info* rep)
    567      1.1  christos {
    568      1.1  christos 	size_t i;
    569      1.1  christos 	for(i=rep->an_numrrsets; i< rep->an_numrrsets+rep->ns_numrrsets; i++){
    570      1.1  christos 		if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_SOA)
    571      1.1  christos 			return rep->rrsets[i];
    572      1.1  christos 	}
    573      1.1  christos 	return NULL;
    574      1.1  christos }
    575      1.1  christos 
    576      1.1  christos /** see if the reply has NSEC records worthy of caching */
    577      1.1  christos static int reply_has_nsec(struct reply_info* rep)
    578      1.1  christos {
    579      1.1  christos 	size_t i;
    580      1.1  christos 	struct packed_rrset_data* d;
    581      1.1  christos 	if(rep->security != sec_status_secure)
    582      1.1  christos 		return 0;
    583      1.1  christos 	for(i=rep->an_numrrsets; i< rep->an_numrrsets+rep->ns_numrrsets; i++){
    584      1.1  christos 		if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_NSEC) {
    585      1.1  christos 			d = (struct packed_rrset_data*)rep->rrsets[i]->
    586      1.1  christos 				entry.data;
    587      1.1  christos 			if(d->security == sec_status_secure)
    588      1.1  christos 				return 1;
    589      1.1  christos 		}
    590      1.1  christos 	}
    591      1.1  christos 	return 0;
    592      1.1  christos }
    593      1.1  christos 
    594      1.1  christos 
    595      1.1  christos /**
    596      1.1  christos  * Create single node of data element.
    597      1.1  christos  * @param nm: name (copied)
    598      1.1  christos  * @param nm_len: length of name
    599      1.1  christos  * @param labs: labels in name.
    600      1.1  christos  * @return element with name nm, or NULL malloc failure.
    601      1.1  christos  */
    602      1.1  christos static struct val_neg_data* neg_setup_data_node(
    603      1.1  christos 	uint8_t* nm, size_t nm_len, int labs)
    604      1.1  christos {
    605      1.1  christos 	struct val_neg_data* el;
    606      1.1  christos 	el = (struct val_neg_data*)calloc(1, sizeof(*el));
    607      1.1  christos 	if(!el) {
    608      1.1  christos 		return NULL;
    609      1.1  christos 	}
    610      1.1  christos 	el->node.key = el;
    611      1.1  christos 	el->name = memdup(nm, nm_len);
    612      1.1  christos 	if(!el->name) {
    613      1.1  christos 		free(el);
    614      1.1  christos 		return NULL;
    615      1.1  christos 	}
    616      1.1  christos 	el->len = nm_len;
    617      1.1  christos 	el->labs = labs;
    618      1.1  christos 	return el;
    619      1.1  christos }
    620      1.1  christos 
    621      1.1  christos /**
    622      1.1  christos  * Create chain of data element and parents
    623      1.1  christos  * @param nm: name
    624      1.1  christos  * @param nm_len: length of name
    625      1.1  christos  * @param labs: labels in name.
    626      1.1  christos  * @param parent: up to where to make, if NULL up to root label.
    627      1.1  christos  * @return lowest element with name nm, or NULL malloc failure.
    628      1.1  christos  */
    629      1.1  christos static struct val_neg_data* neg_data_chain(
    630      1.1  christos 	uint8_t* nm, size_t nm_len, int labs, struct val_neg_data* parent)
    631      1.1  christos {
    632      1.1  christos 	int i;
    633      1.1  christos 	int tolabs = parent?parent->labs:0;
    634      1.1  christos 	struct val_neg_data* el, *first = NULL, *prev = NULL;
    635      1.1  christos 
    636      1.1  christos 	/* create the new subtree, i is labelcount of current creation */
    637      1.1  christos 	/* this creates a 'first' to z->parent=NULL list of zones */
    638      1.1  christos 	for(i=labs; i!=tolabs; i--) {
    639      1.1  christos 		/* create new item */
    640      1.1  christos 		el = neg_setup_data_node(nm, nm_len, i);
    641      1.1  christos 		if(!el) {
    642      1.1  christos 			/* need to delete other allocations in this routine!*/
    643      1.1  christos 			struct val_neg_data* p = first, *np;
    644      1.1  christos 			while(p) {
    645      1.1  christos 				np = p->parent;
    646      1.1  christos 				free(p->name);
    647      1.1  christos 				free(p);
    648      1.1  christos 				p = np;
    649      1.1  christos 			}
    650      1.1  christos 			return NULL;
    651      1.1  christos 		}
    652      1.1  christos 		if(i == labs) {
    653      1.1  christos 			first = el;
    654      1.1  christos 		} else {
    655      1.1  christos 			prev->parent = el;
    656      1.1  christos 		}
    657      1.1  christos 
    658      1.1  christos 		/* prepare for next name */
    659      1.1  christos 		prev = el;
    660      1.1  christos 		dname_remove_label(&nm, &nm_len);
    661      1.1  christos 	}
    662      1.1  christos 	return first;
    663      1.1  christos }
    664      1.1  christos 
    665      1.1  christos /**
    666      1.1  christos  * Remove NSEC records between start and end points.
    667      1.1  christos  * By walking the tree, the tree is sorted canonically.
    668      1.1  christos  * @param neg: negative cache.
    669      1.1  christos  * @param zone: the zone
    670      1.1  christos  * @param el: element to start walking at.
    671      1.1  christos  * @param nsec: the nsec record with the end point
    672      1.1  christos  */
    673      1.1  christos static void wipeout(struct val_neg_cache* neg, struct val_neg_zone* zone,
    674      1.1  christos 	struct val_neg_data* el, struct ub_packed_rrset_key* nsec)
    675      1.1  christos {
    676      1.1  christos 	struct packed_rrset_data* d = (struct packed_rrset_data*)nsec->
    677      1.1  christos 		entry.data;
    678      1.1  christos 	uint8_t* end;
    679      1.1  christos 	size_t end_len;
    680      1.1  christos 	int end_labs, m;
    681  1.1.1.2  christos 	rbnode_type* walk, *next;
    682      1.1  christos 	struct val_neg_data* cur;
    683      1.1  christos 	uint8_t buf[257];
    684      1.1  christos 	/* get endpoint */
    685      1.1  christos 	if(!d || d->count == 0 || d->rr_len[0] < 2+1)
    686      1.1  christos 		return;
    687      1.1  christos 	if(ntohs(nsec->rk.type) == LDNS_RR_TYPE_NSEC) {
    688      1.1  christos 		end = d->rr_data[0]+2;
    689      1.1  christos 		end_len = dname_valid(end, d->rr_len[0]-2);
    690      1.1  christos 		end_labs = dname_count_labels(end);
    691      1.1  christos 	} else {
    692      1.1  christos 		/* NSEC3 */
    693      1.1  christos 		if(!nsec3_get_nextowner_b32(nsec, 0, buf, sizeof(buf)))
    694      1.1  christos 			return;
    695      1.1  christos 		end = buf;
    696      1.1  christos 		end_labs = dname_count_size_labels(end, &end_len);
    697      1.1  christos 	}
    698      1.1  christos 
    699      1.1  christos 	/* sanity check, both owner and end must be below the zone apex */
    700      1.1  christos 	if(!dname_subdomain_c(el->name, zone->name) ||
    701      1.1  christos 		!dname_subdomain_c(end, zone->name))
    702      1.1  christos 		return;
    703      1.1  christos 
    704      1.1  christos 	/* detect end of zone NSEC ; wipe until the end of zone */
    705      1.1  christos 	if(query_dname_compare(end, zone->name) == 0) {
    706      1.1  christos 		end = NULL;
    707      1.1  christos 	}
    708      1.1  christos 
    709      1.1  christos 	walk = rbtree_next(&el->node);
    710      1.1  christos 	while(walk && walk != RBTREE_NULL) {
    711      1.1  christos 		cur = (struct val_neg_data*)walk;
    712      1.1  christos 		/* sanity check: must be larger than start */
    713      1.1  christos 		if(dname_canon_lab_cmp(cur->name, cur->labs,
    714      1.1  christos 			el->name, el->labs, &m) <= 0) {
    715      1.1  christos 			/* r == 0 skip original record. */
    716      1.1  christos 			/* r < 0  too small! */
    717      1.1  christos 			walk = rbtree_next(walk);
    718      1.1  christos 			continue;
    719      1.1  christos 		}
    720      1.1  christos 		/* stop at endpoint, also data at empty nonterminals must be
    721      1.1  christos 		 * removed (no NSECs there) so everything between
    722      1.1  christos 		 * start and end */
    723      1.1  christos 		if(end && dname_canon_lab_cmp(cur->name, cur->labs,
    724      1.1  christos 			end, end_labs, &m) >= 0) {
    725      1.1  christos 			break;
    726      1.1  christos 		}
    727      1.1  christos 		/* this element has to be deleted, but we cannot do it
    728      1.1  christos 		 * now, because we are walking the tree still ... */
    729      1.1  christos 		/* get the next element: */
    730      1.1  christos 		next = rbtree_next(walk);
    731      1.1  christos 		/* now delete the original element, this may trigger
    732      1.1  christos 		 * rbtree rebalances, but really, the next element is
    733      1.1  christos 		 * the one we need.
    734      1.1  christos 		 * But it may trigger delete of other data and the
    735      1.1  christos 		 * entire zone. However, if that happens, this is done
    736      1.1  christos 		 * by deleting the *parents* of the element for deletion,
    737      1.1  christos 		 * and maybe also the entire zone if it is empty.
    738      1.1  christos 		 * But parents are smaller in canonical compare, thus,
    739      1.1  christos 		 * if a larger element exists, then it is not a parent,
    740      1.1  christos 		 * it cannot get deleted, the zone cannot get empty.
    741      1.1  christos 		 * If the next==NULL, then zone can be empty. */
    742      1.1  christos 		if(cur->in_use)
    743      1.1  christos 			neg_delete_data(neg, cur);
    744      1.1  christos 		walk = next;
    745      1.1  christos 	}
    746      1.1  christos }
    747      1.1  christos 
    748      1.1  christos void neg_insert_data(struct val_neg_cache* neg,
    749      1.1  christos 	struct val_neg_zone* zone, struct ub_packed_rrset_key* nsec)
    750      1.1  christos {
    751      1.1  christos 	struct packed_rrset_data* d;
    752      1.1  christos 	struct val_neg_data* parent;
    753      1.1  christos 	struct val_neg_data* el;
    754      1.1  christos 	uint8_t* nm = nsec->rk.dname;
    755      1.1  christos 	size_t nm_len = nsec->rk.dname_len;
    756      1.1  christos 	int labs = dname_count_labels(nsec->rk.dname);
    757      1.1  christos 
    758      1.1  christos 	d = (struct packed_rrset_data*)nsec->entry.data;
    759      1.1  christos 	if( !(d->security == sec_status_secure ||
    760      1.1  christos 		(d->security == sec_status_unchecked && d->rrsig_count > 0)))
    761      1.1  christos 		return;
    762      1.1  christos 	log_nametypeclass(VERB_ALGO, "negcache rr",
    763      1.1  christos 		nsec->rk.dname, ntohs(nsec->rk.type),
    764      1.1  christos 		ntohs(nsec->rk.rrset_class));
    765      1.1  christos 
    766      1.1  christos 	/* find closest enclosing parent data that (still) exists */
    767      1.1  christos 	parent = neg_closest_data_parent(zone, nm, nm_len, labs);
    768      1.1  christos 	if(parent && query_dname_compare(parent->name, nm) == 0) {
    769      1.1  christos 		/* perfect match already exists */
    770      1.1  christos 		log_assert(parent->count > 0);
    771      1.1  christos 		el = parent;
    772      1.1  christos 	} else {
    773      1.1  christos 		struct val_neg_data* p, *np;
    774      1.1  christos 
    775      1.1  christos 		/* create subtree for perfect match */
    776      1.1  christos 		/* if parent exists, it is in use */
    777      1.1  christos 		log_assert(!parent || parent->count > 0);
    778      1.1  christos 
    779      1.1  christos 		el = neg_data_chain(nm, nm_len, labs, parent);
    780      1.1  christos 		if(!el) {
    781      1.1  christos 			log_err("out of memory inserting NSEC negative cache");
    782      1.1  christos 			return;
    783      1.1  christos 		}
    784      1.1  christos 		el->in_use = 0; /* set on below */
    785      1.1  christos 
    786      1.1  christos 		/* insert the list of zones into the tree */
    787      1.1  christos 		p = el;
    788      1.1  christos 		while(p) {
    789      1.1  christos 			np = p->parent;
    790      1.1  christos 			/* mem use */
    791      1.1  christos 			neg->use += sizeof(struct val_neg_data) + p->len;
    792      1.1  christos 			/* insert in tree */
    793      1.1  christos 			p->zone = zone;
    794      1.1  christos 			(void)rbtree_insert(&zone->tree, &p->node);
    795      1.1  christos 			/* last one needs proper parent pointer */
    796      1.1  christos 			if(np == NULL)
    797      1.1  christos 				p->parent = parent;
    798      1.1  christos 			p = np;
    799      1.1  christos 		}
    800      1.1  christos 	}
    801      1.1  christos 
    802      1.1  christos 	if(!el->in_use) {
    803      1.1  christos 		struct val_neg_data* p;
    804      1.1  christos 
    805      1.1  christos 		el->in_use = 1;
    806      1.1  christos 		/* increase usage count of all parents */
    807      1.1  christos 		for(p=el; p; p = p->parent) {
    808      1.1  christos 			p->count++;
    809      1.1  christos 		}
    810      1.1  christos 
    811      1.1  christos 		neg_lru_front(neg, el);
    812      1.1  christos 	} else {
    813      1.1  christos 		/* in use, bring to front, lru */
    814      1.1  christos 		neg_lru_touch(neg, el);
    815      1.1  christos 	}
    816      1.1  christos 
    817      1.1  christos 	/* if nsec3 store last used parameters */
    818      1.1  christos 	if(ntohs(nsec->rk.type) == LDNS_RR_TYPE_NSEC3) {
    819      1.1  christos 		int h;
    820      1.1  christos 		uint8_t* s;
    821      1.1  christos 		size_t slen, it;
    822      1.1  christos 		if(nsec3_get_params(nsec, 0, &h, &it, &s, &slen) &&
    823      1.1  christos 			it <= neg->nsec3_max_iter &&
    824      1.1  christos 			(h != zone->nsec3_hash || it != zone->nsec3_iter ||
    825      1.1  christos 			slen != zone->nsec3_saltlen ||
    826  1.1.1.7  christos 			(slen != 0 && zone->nsec3_salt && s
    827  1.1.1.7  christos 			  && memcmp(zone->nsec3_salt, s, slen) != 0))) {
    828      1.1  christos 
    829      1.1  christos 			if(slen > 0) {
    830      1.1  christos 				uint8_t* sa = memdup(s, slen);
    831      1.1  christos 				if(sa) {
    832      1.1  christos 					free(zone->nsec3_salt);
    833      1.1  christos 					zone->nsec3_salt = sa;
    834      1.1  christos 					zone->nsec3_saltlen = slen;
    835      1.1  christos 					zone->nsec3_iter = it;
    836      1.1  christos 					zone->nsec3_hash = h;
    837      1.1  christos 				}
    838      1.1  christos 			} else {
    839      1.1  christos 				free(zone->nsec3_salt);
    840      1.1  christos 				zone->nsec3_salt = NULL;
    841      1.1  christos 				zone->nsec3_saltlen = 0;
    842      1.1  christos 				zone->nsec3_iter = it;
    843      1.1  christos 				zone->nsec3_hash = h;
    844      1.1  christos 			}
    845      1.1  christos 		}
    846      1.1  christos 	}
    847      1.1  christos 
    848      1.1  christos 	/* wipe out the cache items between NSEC start and end */
    849      1.1  christos 	wipeout(neg, zone, el, nsec);
    850      1.1  christos }
    851      1.1  christos 
    852  1.1.1.3  christos /** see if the reply has signed NSEC records and return the signer */
    853  1.1.1.3  christos static uint8_t* reply_nsec_signer(struct reply_info* rep, size_t* signer_len,
    854  1.1.1.3  christos 	uint16_t* dclass)
    855  1.1.1.3  christos {
    856  1.1.1.3  christos 	size_t i;
    857  1.1.1.3  christos 	struct packed_rrset_data* d;
    858  1.1.1.3  christos 	uint8_t* s;
    859  1.1.1.3  christos 	for(i=rep->an_numrrsets; i< rep->an_numrrsets+rep->ns_numrrsets; i++){
    860  1.1.1.3  christos 		if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_NSEC ||
    861  1.1.1.3  christos 			ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_NSEC3) {
    862  1.1.1.3  christos 			d = (struct packed_rrset_data*)rep->rrsets[i]->
    863  1.1.1.3  christos 				entry.data;
    864  1.1.1.3  christos 			/* return first signer name of first NSEC */
    865  1.1.1.3  christos 			if(d->rrsig_count != 0) {
    866  1.1.1.3  christos 				val_find_rrset_signer(rep->rrsets[i],
    867  1.1.1.3  christos 					&s, signer_len);
    868  1.1.1.3  christos 				if(s && *signer_len) {
    869  1.1.1.3  christos 					*dclass = ntohs(rep->rrsets[i]->
    870  1.1.1.3  christos 						rk.rrset_class);
    871  1.1.1.3  christos 					return s;
    872  1.1.1.3  christos 				}
    873  1.1.1.3  christos 			}
    874  1.1.1.3  christos 		}
    875  1.1.1.3  christos 	}
    876  1.1.1.3  christos 	return 0;
    877  1.1.1.3  christos }
    878  1.1.1.3  christos 
    879      1.1  christos void val_neg_addreply(struct val_neg_cache* neg, struct reply_info* rep)
    880      1.1  christos {
    881      1.1  christos 	size_t i, need;
    882      1.1  christos 	struct ub_packed_rrset_key* soa;
    883  1.1.1.3  christos 	uint8_t* dname = NULL;
    884  1.1.1.3  christos 	size_t dname_len;
    885  1.1.1.3  christos 	uint16_t rrset_class;
    886      1.1  christos 	struct val_neg_zone* zone;
    887      1.1  christos 	/* see if secure nsecs inside */
    888      1.1  christos 	if(!reply_has_nsec(rep))
    889      1.1  christos 		return;
    890      1.1  christos 	/* find the zone name in message */
    891  1.1.1.3  christos 	if((soa = reply_find_soa(rep))) {
    892  1.1.1.3  christos 		dname = soa->rk.dname;
    893  1.1.1.3  christos 		dname_len = soa->rk.dname_len;
    894  1.1.1.3  christos 		rrset_class = ntohs(soa->rk.rrset_class);
    895  1.1.1.3  christos 	}
    896  1.1.1.3  christos 	else {
    897  1.1.1.3  christos 		/* No SOA in positive (wildcard) answer. Use signer from the
    898  1.1.1.3  christos 		 * validated answer RRsets' signature. */
    899  1.1.1.3  christos 		if(!(dname = reply_nsec_signer(rep, &dname_len, &rrset_class)))
    900  1.1.1.3  christos 			return;
    901  1.1.1.3  christos 	}
    902      1.1  christos 
    903      1.1  christos 	log_nametypeclass(VERB_ALGO, "negcache insert for zone",
    904  1.1.1.3  christos 		dname, LDNS_RR_TYPE_SOA, rrset_class);
    905      1.1  christos 
    906      1.1  christos 	/* ask for enough space to store all of it */
    907      1.1  christos 	need = calc_data_need(rep) +
    908  1.1.1.3  christos 		calc_zone_need(dname, dname_len);
    909      1.1  christos 	lock_basic_lock(&neg->lock);
    910      1.1  christos 	neg_make_space(neg, need);
    911      1.1  christos 
    912      1.1  christos 	/* find or create the zone entry */
    913  1.1.1.3  christos 	zone = neg_find_zone(neg, dname, dname_len, rrset_class);
    914      1.1  christos 	if(!zone) {
    915  1.1.1.3  christos 		if(!(zone = neg_create_zone(neg, dname, dname_len,
    916  1.1.1.3  christos 			rrset_class))) {
    917      1.1  christos 			lock_basic_unlock(&neg->lock);
    918      1.1  christos 			log_err("out of memory adding negative zone");
    919      1.1  christos 			return;
    920      1.1  christos 		}
    921      1.1  christos 	}
    922      1.1  christos 	val_neg_zone_take_inuse(zone);
    923      1.1  christos 
    924      1.1  christos 	/* insert the NSECs */
    925      1.1  christos 	for(i=rep->an_numrrsets; i< rep->an_numrrsets+rep->ns_numrrsets; i++){
    926      1.1  christos 		if(ntohs(rep->rrsets[i]->rk.type) != LDNS_RR_TYPE_NSEC)
    927      1.1  christos 			continue;
    928      1.1  christos 		if(!dname_subdomain_c(rep->rrsets[i]->rk.dname,
    929      1.1  christos 			zone->name)) continue;
    930      1.1  christos 		/* insert NSEC into this zone's tree */
    931      1.1  christos 		neg_insert_data(neg, zone, rep->rrsets[i]);
    932      1.1  christos 	}
    933      1.1  christos 	if(zone->tree.count == 0) {
    934      1.1  christos 		/* remove empty zone if inserts failed */
    935      1.1  christos 		neg_delete_zone(neg, zone);
    936      1.1  christos 	}
    937      1.1  christos 	lock_basic_unlock(&neg->lock);
    938      1.1  christos }
    939      1.1  christos 
    940      1.1  christos /**
    941      1.1  christos  * Lookup closest data record. For NSEC denial.
    942      1.1  christos  * @param zone: zone to look in
    943      1.1  christos  * @param qname: name to look for.
    944      1.1  christos  * @param len: length of name
    945      1.1  christos  * @param labs: labels in name
    946      1.1  christos  * @param data: data element, exact or smaller or NULL
    947      1.1  christos  * @return true if exact match.
    948      1.1  christos  */
    949      1.1  christos static int neg_closest_data(struct val_neg_zone* zone,
    950      1.1  christos 	uint8_t* qname, size_t len, int labs, struct val_neg_data** data)
    951      1.1  christos {
    952      1.1  christos 	struct val_neg_data key;
    953  1.1.1.2  christos 	rbnode_type* r;
    954      1.1  christos 	key.node.key = &key;
    955      1.1  christos 	key.name = qname;
    956      1.1  christos 	key.len = len;
    957      1.1  christos 	key.labs = labs;
    958      1.1  christos 	if(rbtree_find_less_equal(&zone->tree, &key, &r)) {
    959      1.1  christos 		/* exact match */
    960      1.1  christos 		*data = (struct val_neg_data*)r;
    961      1.1  christos 		return 1;
    962      1.1  christos 	} else {
    963      1.1  christos 		/* smaller match */
    964      1.1  christos 		*data = (struct val_neg_data*)r;
    965      1.1  christos 		return 0;
    966      1.1  christos 	}
    967      1.1  christos }
    968      1.1  christos 
    969      1.1  christos void val_neg_addreferral(struct val_neg_cache* neg, struct reply_info* rep,
    970      1.1  christos 	uint8_t* zone_name)
    971      1.1  christos {
    972      1.1  christos 	size_t i, need;
    973      1.1  christos 	uint8_t* signer;
    974      1.1  christos 	size_t signer_len;
    975      1.1  christos 	uint16_t dclass;
    976      1.1  christos 	struct val_neg_zone* zone;
    977      1.1  christos 	/* no SOA in this message, find RRSIG over NSEC's signer name.
    978      1.1  christos 	 * note the NSEC records are maybe not validated yet */
    979      1.1  christos 	signer = reply_nsec_signer(rep, &signer_len, &dclass);
    980      1.1  christos 	if(!signer)
    981      1.1  christos 		return;
    982      1.1  christos 	if(!dname_subdomain_c(signer, zone_name)) {
    983      1.1  christos 		/* the signer is not in the bailiwick, throw it out */
    984      1.1  christos 		return;
    985      1.1  christos 	}
    986      1.1  christos 
    987      1.1  christos 	log_nametypeclass(VERB_ALGO, "negcache insert referral ",
    988      1.1  christos 		signer, LDNS_RR_TYPE_NS, dclass);
    989      1.1  christos 
    990      1.1  christos 	/* ask for enough space to store all of it */
    991      1.1  christos 	need = calc_data_need(rep) + calc_zone_need(signer, signer_len);
    992      1.1  christos 	lock_basic_lock(&neg->lock);
    993      1.1  christos 	neg_make_space(neg, need);
    994      1.1  christos 
    995      1.1  christos 	/* find or create the zone entry */
    996      1.1  christos 	zone = neg_find_zone(neg, signer, signer_len, dclass);
    997      1.1  christos 	if(!zone) {
    998      1.1  christos 		if(!(zone = neg_create_zone(neg, signer, signer_len,
    999      1.1  christos 			dclass))) {
   1000      1.1  christos 			lock_basic_unlock(&neg->lock);
   1001      1.1  christos 			log_err("out of memory adding negative zone");
   1002      1.1  christos 			return;
   1003      1.1  christos 		}
   1004      1.1  christos 	}
   1005      1.1  christos 	val_neg_zone_take_inuse(zone);
   1006      1.1  christos 
   1007      1.1  christos 	/* insert the NSECs */
   1008      1.1  christos 	for(i=rep->an_numrrsets; i< rep->an_numrrsets+rep->ns_numrrsets; i++){
   1009      1.1  christos 		if(ntohs(rep->rrsets[i]->rk.type) != LDNS_RR_TYPE_NSEC &&
   1010      1.1  christos 			ntohs(rep->rrsets[i]->rk.type) != LDNS_RR_TYPE_NSEC3)
   1011      1.1  christos 			continue;
   1012      1.1  christos 		if(!dname_subdomain_c(rep->rrsets[i]->rk.dname,
   1013      1.1  christos 			zone->name)) continue;
   1014      1.1  christos 		/* insert NSEC into this zone's tree */
   1015      1.1  christos 		neg_insert_data(neg, zone, rep->rrsets[i]);
   1016      1.1  christos 	}
   1017      1.1  christos 	if(zone->tree.count == 0) {
   1018      1.1  christos 		/* remove empty zone if inserts failed */
   1019      1.1  christos 		neg_delete_zone(neg, zone);
   1020      1.1  christos 	}
   1021      1.1  christos 	lock_basic_unlock(&neg->lock);
   1022      1.1  christos }
   1023      1.1  christos 
   1024      1.1  christos /**
   1025      1.1  christos  * Check that an NSEC3 rrset does not have a type set.
   1026      1.1  christos  * None of the nsec3s in a hash-collision are allowed to have the type.
   1027      1.1  christos  * (since we do not know which one is the nsec3 looked at, flags, ..., we
   1028      1.1  christos  * ignore the cached item and let it bypass negative caching).
   1029      1.1  christos  * @param k: the nsec3 rrset to check.
   1030      1.1  christos  * @param t: type to check
   1031      1.1  christos  * @return true if no RRs have the type.
   1032      1.1  christos  */
   1033      1.1  christos static int nsec3_no_type(struct ub_packed_rrset_key* k, uint16_t t)
   1034      1.1  christos {
   1035      1.1  christos 	int count = (int)((struct packed_rrset_data*)k->entry.data)->count;
   1036      1.1  christos 	int i;
   1037      1.1  christos 	for(i=0; i<count; i++)
   1038      1.1  christos 		if(nsec3_has_type(k, i, t))
   1039      1.1  christos 			return 0;
   1040      1.1  christos 	return 1;
   1041      1.1  christos }
   1042      1.1  christos 
   1043      1.1  christos /**
   1044      1.1  christos  * See if rrset exists in rrset cache.
   1045      1.1  christos  * If it does, the bit is checked, and if not expired, it is returned
   1046      1.1  christos  * allocated in region.
   1047      1.1  christos  * @param rrset_cache: rrset cache
   1048      1.1  christos  * @param qname: to lookup rrset name
   1049      1.1  christos  * @param qname_len: length of qname.
   1050      1.1  christos  * @param qtype: type of rrset to lookup, host order
   1051      1.1  christos  * @param qclass: class of rrset to lookup, host order
   1052      1.1  christos  * @param flags: flags for rrset to lookup
   1053      1.1  christos  * @param region: where to alloc result
   1054      1.1  christos  * @param checkbit: if true, a bit in the nsec typemap is checked for absence.
   1055      1.1  christos  * @param checktype: which bit to check
   1056      1.1  christos  * @param now: to check ttl against
   1057      1.1  christos  * @return rrset or NULL
   1058      1.1  christos  */
   1059      1.1  christos static struct ub_packed_rrset_key*
   1060      1.1  christos grab_nsec(struct rrset_cache* rrset_cache, uint8_t* qname, size_t qname_len,
   1061      1.1  christos 	uint16_t qtype, uint16_t qclass, uint32_t flags,
   1062      1.1  christos 	struct regional* region, int checkbit, uint16_t checktype,
   1063      1.1  christos 	time_t now)
   1064      1.1  christos {
   1065      1.1  christos 	struct ub_packed_rrset_key* r, *k = rrset_cache_lookup(rrset_cache,
   1066      1.1  christos 		qname, qname_len, qtype, qclass, flags, now, 0);
   1067      1.1  christos 	struct packed_rrset_data* d;
   1068      1.1  christos 	if(!k) return NULL;
   1069      1.1  christos 	d = (struct packed_rrset_data*)k->entry.data;
   1070      1.1  christos 	if(d->ttl < now) {
   1071      1.1  christos 		lock_rw_unlock(&k->entry.lock);
   1072      1.1  christos 		return NULL;
   1073      1.1  christos 	}
   1074      1.1  christos 	/* only secure or unchecked records that have signatures. */
   1075      1.1  christos 	if( ! ( d->security == sec_status_secure ||
   1076      1.1  christos 		(d->security == sec_status_unchecked &&
   1077      1.1  christos 		d->rrsig_count > 0) ) ) {
   1078      1.1  christos 		lock_rw_unlock(&k->entry.lock);
   1079      1.1  christos 		return NULL;
   1080      1.1  christos 	}
   1081      1.1  christos 	/* check if checktype is absent */
   1082      1.1  christos 	if(checkbit && (
   1083      1.1  christos 		(qtype == LDNS_RR_TYPE_NSEC && nsec_has_type(k, checktype)) ||
   1084      1.1  christos 		(qtype == LDNS_RR_TYPE_NSEC3 && !nsec3_no_type(k, checktype))
   1085      1.1  christos 		)) {
   1086      1.1  christos 		lock_rw_unlock(&k->entry.lock);
   1087      1.1  christos 		return NULL;
   1088      1.1  christos 	}
   1089      1.1  christos 	/* looks OK! copy to region and return it */
   1090      1.1  christos 	r = packed_rrset_copy_region(k, region, now);
   1091      1.1  christos 	/* if it failed, we return the NULL */
   1092      1.1  christos 	lock_rw_unlock(&k->entry.lock);
   1093      1.1  christos 	return r;
   1094      1.1  christos }
   1095      1.1  christos 
   1096  1.1.1.3  christos /**
   1097  1.1.1.3  christos  * Get best NSEC record for qname. Might be matching, covering or totally
   1098  1.1.1.3  christos  * useless.
   1099  1.1.1.3  christos  * @param neg_cache: neg cache
   1100  1.1.1.3  christos  * @param qname: to lookup rrset name
   1101  1.1.1.3  christos  * @param qname_len: length of qname.
   1102  1.1.1.3  christos  * @param qclass: class of rrset to lookup, host order
   1103  1.1.1.3  christos  * @param rrset_cache: rrset cache
   1104  1.1.1.3  christos  * @param now: to check ttl against
   1105  1.1.1.3  christos  * @param region: where to alloc result
   1106  1.1.1.3  christos  * @return rrset or NULL
   1107  1.1.1.3  christos  */
   1108  1.1.1.3  christos static struct ub_packed_rrset_key*
   1109  1.1.1.3  christos neg_find_nsec(struct val_neg_cache* neg_cache, uint8_t* qname, size_t qname_len,
   1110  1.1.1.3  christos 	uint16_t qclass, struct rrset_cache* rrset_cache, time_t now,
   1111  1.1.1.3  christos 	struct regional* region)
   1112  1.1.1.3  christos {
   1113  1.1.1.3  christos 	int labs;
   1114  1.1.1.3  christos 	uint32_t flags;
   1115  1.1.1.3  christos 	struct val_neg_zone* zone;
   1116  1.1.1.3  christos 	struct val_neg_data* data;
   1117  1.1.1.3  christos 	struct ub_packed_rrset_key* nsec;
   1118  1.1.1.3  christos 
   1119  1.1.1.3  christos 	labs = dname_count_labels(qname);
   1120  1.1.1.3  christos 	lock_basic_lock(&neg_cache->lock);
   1121  1.1.1.3  christos 	zone = neg_closest_zone_parent(neg_cache, qname, qname_len, labs,
   1122  1.1.1.3  christos 		qclass);
   1123  1.1.1.3  christos 	while(zone && !zone->in_use)
   1124  1.1.1.3  christos 		zone = zone->parent;
   1125  1.1.1.3  christos 	if(!zone) {
   1126  1.1.1.3  christos 		lock_basic_unlock(&neg_cache->lock);
   1127  1.1.1.3  christos 		return NULL;
   1128  1.1.1.3  christos 	}
   1129  1.1.1.3  christos 
   1130  1.1.1.3  christos 	/* NSEC only for now */
   1131  1.1.1.3  christos 	if(zone->nsec3_hash) {
   1132  1.1.1.3  christos 		lock_basic_unlock(&neg_cache->lock);
   1133  1.1.1.3  christos 		return NULL;
   1134  1.1.1.3  christos 	}
   1135  1.1.1.3  christos 
   1136  1.1.1.3  christos 	/* ignore return value, don't care if it is an exact or smaller match */
   1137  1.1.1.3  christos 	(void)neg_closest_data(zone, qname, qname_len, labs, &data);
   1138  1.1.1.3  christos 	if(!data) {
   1139  1.1.1.3  christos 		lock_basic_unlock(&neg_cache->lock);
   1140  1.1.1.3  christos 		return NULL;
   1141  1.1.1.3  christos 	}
   1142  1.1.1.3  christos 
   1143  1.1.1.3  christos 	/* ENT nodes are not in use, try the previous node. If the previous node
   1144  1.1.1.3  christos 	 * is not in use, we don't have an useful NSEC and give up. */
   1145  1.1.1.3  christos 	if(!data->in_use) {
   1146  1.1.1.3  christos 		data = (struct val_neg_data*)rbtree_previous((rbnode_type*)data);
   1147  1.1.1.3  christos 		if((rbnode_type*)data == RBTREE_NULL || !data->in_use) {
   1148  1.1.1.3  christos 			lock_basic_unlock(&neg_cache->lock);
   1149  1.1.1.3  christos 			return NULL;
   1150  1.1.1.3  christos 		}
   1151  1.1.1.3  christos 	}
   1152  1.1.1.3  christos 
   1153  1.1.1.3  christos 	flags = 0;
   1154  1.1.1.3  christos 	if(query_dname_compare(data->name, zone->name) == 0)
   1155  1.1.1.3  christos 		flags = PACKED_RRSET_NSEC_AT_APEX;
   1156  1.1.1.3  christos 
   1157  1.1.1.3  christos 	nsec = grab_nsec(rrset_cache, data->name, data->len, LDNS_RR_TYPE_NSEC,
   1158  1.1.1.3  christos 		zone->dclass, flags, region, 0, 0, now);
   1159  1.1.1.3  christos 	lock_basic_unlock(&neg_cache->lock);
   1160  1.1.1.3  christos 	return nsec;
   1161  1.1.1.3  christos }
   1162  1.1.1.3  christos 
   1163      1.1  christos /** find nsec3 closest encloser in neg cache */
   1164      1.1  christos static struct val_neg_data*
   1165      1.1  christos neg_find_nsec3_ce(struct val_neg_zone* zone, uint8_t* qname, size_t qname_len,
   1166      1.1  christos 		int qlabs, sldns_buffer* buf, uint8_t* hashnc, size_t* nclen)
   1167      1.1  christos {
   1168      1.1  christos 	struct val_neg_data* data;
   1169      1.1  christos 	uint8_t hashce[NSEC3_SHA_LEN];
   1170      1.1  christos 	uint8_t b32[257];
   1171      1.1  christos 	size_t celen, b32len;
   1172      1.1  christos 
   1173      1.1  christos 	*nclen = 0;
   1174      1.1  christos 	while(qlabs > 0) {
   1175      1.1  christos 		/* hash */
   1176      1.1  christos 		if(!(celen=nsec3_get_hashed(buf, qname, qname_len,
   1177      1.1  christos 			zone->nsec3_hash, zone->nsec3_iter, zone->nsec3_salt,
   1178      1.1  christos 			zone->nsec3_saltlen, hashce, sizeof(hashce))))
   1179      1.1  christos 			return NULL;
   1180      1.1  christos 		if(!(b32len=nsec3_hash_to_b32(hashce, celen, zone->name,
   1181      1.1  christos 			zone->len, b32, sizeof(b32))))
   1182      1.1  christos 			return NULL;
   1183      1.1  christos 
   1184      1.1  christos 		/* lookup (exact match only) */
   1185      1.1  christos 		data = neg_find_data(zone, b32, b32len, zone->labs+1);
   1186      1.1  christos 		if(data && data->in_use) {
   1187      1.1  christos 			/* found ce match! */
   1188      1.1  christos 			return data;
   1189      1.1  christos 		}
   1190      1.1  christos 
   1191      1.1  christos 		*nclen = celen;
   1192      1.1  christos 		memmove(hashnc, hashce, celen);
   1193      1.1  christos 		dname_remove_label(&qname, &qname_len);
   1194      1.1  christos 		qlabs --;
   1195      1.1  christos 	}
   1196      1.1  christos 	return NULL;
   1197      1.1  christos }
   1198      1.1  christos 
   1199      1.1  christos /** check nsec3 parameters on nsec3 rrset with current zone values */
   1200      1.1  christos static int
   1201      1.1  christos neg_params_ok(struct val_neg_zone* zone, struct ub_packed_rrset_key* rrset)
   1202      1.1  christos {
   1203      1.1  christos 	int h;
   1204      1.1  christos 	uint8_t* s;
   1205      1.1  christos 	size_t slen, it;
   1206      1.1  christos 	if(!nsec3_get_params(rrset, 0, &h, &it, &s, &slen))
   1207      1.1  christos 		return 0;
   1208      1.1  christos 	return (h == zone->nsec3_hash && it == zone->nsec3_iter &&
   1209      1.1  christos 		slen == zone->nsec3_saltlen &&
   1210  1.1.1.7  christos 		(slen != 0 && zone->nsec3_salt && s
   1211  1.1.1.7  christos 		  && memcmp(zone->nsec3_salt, s, slen) == 0));
   1212      1.1  christos }
   1213      1.1  christos 
   1214      1.1  christos /** get next closer for nsec3 proof */
   1215      1.1  christos static struct ub_packed_rrset_key*
   1216      1.1  christos neg_nsec3_getnc(struct val_neg_zone* zone, uint8_t* hashnc, size_t nclen,
   1217      1.1  christos 	struct rrset_cache* rrset_cache, struct regional* region,
   1218      1.1  christos 	time_t now, uint8_t* b32, size_t maxb32)
   1219      1.1  christos {
   1220      1.1  christos 	struct ub_packed_rrset_key* nc_rrset;
   1221      1.1  christos 	struct val_neg_data* data;
   1222      1.1  christos 	size_t b32len;
   1223      1.1  christos 
   1224      1.1  christos 	if(!(b32len=nsec3_hash_to_b32(hashnc, nclen, zone->name,
   1225      1.1  christos 		zone->len, b32, maxb32)))
   1226      1.1  christos 		return NULL;
   1227      1.1  christos 	(void)neg_closest_data(zone, b32, b32len, zone->labs+1, &data);
   1228      1.1  christos 	if(!data && zone->tree.count != 0) {
   1229      1.1  christos 		/* could be before the first entry ; return the last
   1230      1.1  christos 		 * entry (possibly the rollover nsec3 at end) */
   1231      1.1  christos 		data = (struct val_neg_data*)rbtree_last(&zone->tree);
   1232      1.1  christos 	}
   1233      1.1  christos 	while(data && !data->in_use)
   1234      1.1  christos 		data = data->parent;
   1235      1.1  christos 	if(!data)
   1236      1.1  christos 		return NULL;
   1237      1.1  christos 	/* got a data element in tree, grab it */
   1238      1.1  christos 	nc_rrset = grab_nsec(rrset_cache, data->name, data->len,
   1239      1.1  christos 		LDNS_RR_TYPE_NSEC3, zone->dclass, 0, region, 0, 0, now);
   1240      1.1  christos 	if(!nc_rrset)
   1241      1.1  christos 		return NULL;
   1242      1.1  christos 	if(!neg_params_ok(zone, nc_rrset))
   1243      1.1  christos 		return NULL;
   1244      1.1  christos 	return nc_rrset;
   1245      1.1  christos }
   1246      1.1  christos 
   1247      1.1  christos /** neg cache nsec3 proof procedure*/
   1248      1.1  christos static struct dns_msg*
   1249      1.1  christos neg_nsec3_proof_ds(struct val_neg_zone* zone, uint8_t* qname, size_t qname_len,
   1250      1.1  christos 		int qlabs, sldns_buffer* buf, struct rrset_cache* rrset_cache,
   1251      1.1  christos 		struct regional* region, time_t now, uint8_t* topname)
   1252      1.1  christos {
   1253      1.1  christos 	struct dns_msg* msg;
   1254      1.1  christos 	struct val_neg_data* data;
   1255      1.1  christos 	uint8_t hashnc[NSEC3_SHA_LEN];
   1256      1.1  christos 	size_t nclen;
   1257      1.1  christos 	struct ub_packed_rrset_key* ce_rrset, *nc_rrset;
   1258      1.1  christos 	struct nsec3_cached_hash c;
   1259      1.1  christos 	uint8_t nc_b32[257];
   1260      1.1  christos 
   1261      1.1  christos 	/* for NSEC3 ; determine the closest encloser for which we
   1262      1.1  christos 	 * can find an exact match. Remember the hashed lower name,
   1263      1.1  christos 	 * since that is the one we need a closest match for.
   1264      1.1  christos 	 * If we find a match straight away, then it becomes NODATA.
   1265      1.1  christos 	 * Otherwise, NXDOMAIN or if OPTOUT, an insecure delegation.
   1266      1.1  christos 	 * Also check that parameters are the same on closest encloser
   1267      1.1  christos 	 * and on closest match.
   1268      1.1  christos 	 */
   1269      1.1  christos 	if(!zone->nsec3_hash)
   1270      1.1  christos 		return NULL; /* not nsec3 zone */
   1271      1.1  christos 
   1272      1.1  christos 	if(!(data=neg_find_nsec3_ce(zone, qname, qname_len, qlabs, buf,
   1273      1.1  christos 		hashnc, &nclen))) {
   1274      1.1  christos 		return NULL;
   1275      1.1  christos 	}
   1276      1.1  christos 
   1277      1.1  christos 	/* grab the ce rrset */
   1278      1.1  christos 	ce_rrset = grab_nsec(rrset_cache, data->name, data->len,
   1279      1.1  christos 		LDNS_RR_TYPE_NSEC3, zone->dclass, 0, region, 1,
   1280      1.1  christos 		LDNS_RR_TYPE_DS, now);
   1281      1.1  christos 	if(!ce_rrset)
   1282      1.1  christos 		return NULL;
   1283      1.1  christos 	if(!neg_params_ok(zone, ce_rrset))
   1284      1.1  christos 		return NULL;
   1285      1.1  christos 
   1286      1.1  christos 	if(nclen == 0) {
   1287      1.1  christos 		/* exact match, just check the type bits */
   1288      1.1  christos 		/* need: -SOA, -DS, +NS */
   1289      1.1  christos 		if(nsec3_has_type(ce_rrset, 0, LDNS_RR_TYPE_SOA) ||
   1290      1.1  christos 			nsec3_has_type(ce_rrset, 0, LDNS_RR_TYPE_DS) ||
   1291      1.1  christos 			!nsec3_has_type(ce_rrset, 0, LDNS_RR_TYPE_NS))
   1292      1.1  christos 			return NULL;
   1293      1.1  christos 		if(!(msg = dns_msg_create(qname, qname_len,
   1294      1.1  christos 			LDNS_RR_TYPE_DS, zone->dclass, region, 1)))
   1295      1.1  christos 			return NULL;
   1296      1.1  christos 		/* TTL reduced in grab_nsec */
   1297      1.1  christos 		if(!dns_msg_authadd(msg, region, ce_rrset, 0))
   1298      1.1  christos 			return NULL;
   1299      1.1  christos 		return msg;
   1300      1.1  christos 	}
   1301      1.1  christos 
   1302      1.1  christos 	/* optout is not allowed without knowing the trust-anchor in use,
   1303      1.1  christos 	 * otherwise the optout could spoof away that anchor */
   1304      1.1  christos 	if(!topname)
   1305      1.1  christos 		return NULL;
   1306      1.1  christos 
   1307      1.1  christos 	/* if there is no exact match, it must be in an optout span
   1308      1.1  christos 	 * (an existing DS implies an NSEC3 must exist) */
   1309      1.1  christos 	nc_rrset = neg_nsec3_getnc(zone, hashnc, nclen, rrset_cache,
   1310      1.1  christos 		region, now, nc_b32, sizeof(nc_b32));
   1311      1.1  christos 	if(!nc_rrset)
   1312      1.1  christos 		return NULL;
   1313      1.1  christos 	if(!neg_params_ok(zone, nc_rrset))
   1314      1.1  christos 		return NULL;
   1315      1.1  christos 	if(!nsec3_has_optout(nc_rrset, 0))
   1316      1.1  christos 		return NULL;
   1317      1.1  christos 	c.hash = hashnc;
   1318      1.1  christos 	c.hash_len = nclen;
   1319      1.1  christos 	c.b32 = nc_b32+1;
   1320      1.1  christos 	c.b32_len = (size_t)nc_b32[0];
   1321      1.1  christos 	if(nsec3_covers(zone->name, &c, nc_rrset, 0, buf)) {
   1322      1.1  christos 		/* nc_rrset covers the next closer name.
   1323      1.1  christos 		 * ce_rrset equals a closer encloser.
   1324      1.1  christos 		 * nc_rrset is optout.
   1325      1.1  christos 		 * No need to check wildcard for type DS */
   1326      1.1  christos 		/* capacity=3: ce + nc + soa(if needed) */
   1327      1.1  christos 		if(!(msg = dns_msg_create(qname, qname_len,
   1328      1.1  christos 			LDNS_RR_TYPE_DS, zone->dclass, region, 3)))
   1329      1.1  christos 			return NULL;
   1330      1.1  christos 		/* now=0 because TTL was reduced in grab_nsec */
   1331      1.1  christos 		if(!dns_msg_authadd(msg, region, ce_rrset, 0))
   1332      1.1  christos 			return NULL;
   1333      1.1  christos 		if(!dns_msg_authadd(msg, region, nc_rrset, 0))
   1334      1.1  christos 			return NULL;
   1335      1.1  christos 		return msg;
   1336      1.1  christos 	}
   1337      1.1  christos 	return NULL;
   1338      1.1  christos }
   1339      1.1  christos 
   1340      1.1  christos /**
   1341      1.1  christos  * Add SOA record for external responses.
   1342      1.1  christos  * @param rrset_cache: to look into.
   1343      1.1  christos  * @param now: current time.
   1344      1.1  christos  * @param region: where to perform the allocation
   1345      1.1  christos  * @param msg: current msg with NSEC.
   1346      1.1  christos  * @param zone: val_neg_zone if we have one.
   1347      1.1  christos  * @return false on lookup or alloc failure.
   1348      1.1  christos  */
   1349      1.1  christos static int add_soa(struct rrset_cache* rrset_cache, time_t now,
   1350      1.1  christos 	struct regional* region, struct dns_msg* msg, struct val_neg_zone* zone)
   1351      1.1  christos {
   1352      1.1  christos 	struct ub_packed_rrset_key* soa;
   1353      1.1  christos 	uint8_t* nm;
   1354      1.1  christos 	size_t nmlen;
   1355      1.1  christos 	uint16_t dclass;
   1356      1.1  christos 	if(zone) {
   1357      1.1  christos 		nm = zone->name;
   1358      1.1  christos 		nmlen = zone->len;
   1359      1.1  christos 		dclass = zone->dclass;
   1360      1.1  christos 	} else {
   1361      1.1  christos 		/* Assumes the signer is the zone SOA to add */
   1362      1.1  christos 		nm = reply_nsec_signer(msg->rep, &nmlen, &dclass);
   1363      1.1  christos 		if(!nm)
   1364      1.1  christos 			return 0;
   1365      1.1  christos 	}
   1366      1.1  christos 	soa = rrset_cache_lookup(rrset_cache, nm, nmlen, LDNS_RR_TYPE_SOA,
   1367      1.1  christos 		dclass, PACKED_RRSET_SOA_NEG, now, 0);
   1368      1.1  christos 	if(!soa)
   1369      1.1  christos 		return 0;
   1370      1.1  christos 	if(!dns_msg_authadd(msg, region, soa, now)) {
   1371      1.1  christos 		lock_rw_unlock(&soa->entry.lock);
   1372      1.1  christos 		return 0;
   1373      1.1  christos 	}
   1374      1.1  christos 	lock_rw_unlock(&soa->entry.lock);
   1375      1.1  christos 	return 1;
   1376      1.1  christos }
   1377      1.1  christos 
   1378      1.1  christos struct dns_msg*
   1379      1.1  christos val_neg_getmsg(struct val_neg_cache* neg, struct query_info* qinfo,
   1380      1.1  christos 	struct regional* region, struct rrset_cache* rrset_cache,
   1381  1.1.1.3  christos 	sldns_buffer* buf, time_t now, int addsoa, uint8_t* topname,
   1382  1.1.1.3  christos 	struct config_file* cfg)
   1383      1.1  christos {
   1384      1.1  christos 	struct dns_msg* msg;
   1385  1.1.1.3  christos 	struct ub_packed_rrset_key* nsec; /* qname matching/covering nsec */
   1386  1.1.1.3  christos 	struct ub_packed_rrset_key* wcrr; /* wildcard record or nsec */
   1387  1.1.1.3  christos 	uint8_t* nodata_wc = NULL;
   1388  1.1.1.3  christos 	uint8_t* ce = NULL;
   1389  1.1.1.3  christos 	size_t ce_len;
   1390  1.1.1.3  christos 	uint8_t wc_ce[LDNS_MAX_DOMAINLEN+3];
   1391  1.1.1.3  christos 	struct query_info wc_qinfo;
   1392  1.1.1.3  christos 	struct ub_packed_rrset_key* cache_wc;
   1393  1.1.1.3  christos 	struct packed_rrset_data* wcrr_data;
   1394  1.1.1.3  christos 	int rcode = LDNS_RCODE_NOERROR;
   1395      1.1  christos 	uint8_t* zname;
   1396      1.1  christos 	size_t zname_len;
   1397      1.1  christos 	int zname_labs;
   1398      1.1  christos 	struct val_neg_zone* zone;
   1399      1.1  christos 
   1400  1.1.1.3  christos 	/* only for DS queries when aggressive use of NSEC is disabled */
   1401  1.1.1.3  christos 	if(qinfo->qtype != LDNS_RR_TYPE_DS && !cfg->aggressive_nsec)
   1402      1.1  christos 		return NULL;
   1403      1.1  christos 	log_assert(!topname || dname_subdomain_c(qinfo->qname, topname));
   1404      1.1  christos 
   1405  1.1.1.3  christos 	/* Get best available NSEC for qname */
   1406  1.1.1.3  christos 	nsec = neg_find_nsec(neg, qinfo->qname, qinfo->qname_len, qinfo->qclass,
   1407  1.1.1.3  christos 		rrset_cache, now, region);
   1408  1.1.1.3  christos 
   1409  1.1.1.3  christos 	/* Matching NSEC, use to generate No Data answer. Not creating answers
   1410  1.1.1.3  christos 	 * yet for No Data proven using wildcard. */
   1411  1.1.1.3  christos 	if(nsec && nsec_proves_nodata(nsec, qinfo, &nodata_wc) && !nodata_wc) {
   1412  1.1.1.6  christos 		/* do not create nodata answers for qtype ANY, it is a query
   1413  1.1.1.6  christos 		 * type, not an rrtype to disprove. Nameerrors are useful for
   1414  1.1.1.6  christos 		 * qtype ANY, in the else branch. */
   1415  1.1.1.6  christos 		if(qinfo->qtype == LDNS_RR_TYPE_ANY)
   1416  1.1.1.6  christos 			return NULL;
   1417      1.1  christos 		if(!(msg = dns_msg_create(qinfo->qname, qinfo->qname_len,
   1418      1.1  christos 			qinfo->qtype, qinfo->qclass, region, 2)))
   1419      1.1  christos 			return NULL;
   1420  1.1.1.3  christos 		if(!dns_msg_authadd(msg, region, nsec, 0))
   1421  1.1.1.3  christos 			return NULL;
   1422  1.1.1.3  christos 		if(addsoa && !add_soa(rrset_cache, now, region, msg, NULL))
   1423  1.1.1.3  christos 			return NULL;
   1424  1.1.1.3  christos 
   1425  1.1.1.3  christos 		lock_basic_lock(&neg->lock);
   1426  1.1.1.3  christos 		neg->num_neg_cache_noerror++;
   1427  1.1.1.3  christos 		lock_basic_unlock(&neg->lock);
   1428  1.1.1.3  christos 		return msg;
   1429  1.1.1.3  christos 	} else if(nsec && val_nsec_proves_name_error(nsec, qinfo->qname)) {
   1430  1.1.1.3  christos 		if(!(msg = dns_msg_create(qinfo->qname, qinfo->qname_len,
   1431  1.1.1.3  christos 			qinfo->qtype, qinfo->qclass, region, 3)))
   1432  1.1.1.3  christos 			return NULL;
   1433  1.1.1.3  christos 		if(!(ce = nsec_closest_encloser(qinfo->qname, nsec)))
   1434  1.1.1.3  christos 			return NULL;
   1435  1.1.1.3  christos 		dname_count_size_labels(ce, &ce_len);
   1436  1.1.1.3  christos 
   1437  1.1.1.3  christos 		/* No extra extra NSEC required if both nameerror qname and
   1438  1.1.1.3  christos 		 * nodata *.ce. are proven already. */
   1439  1.1.1.3  christos 		if(!nodata_wc || query_dname_compare(nodata_wc, ce) != 0) {
   1440  1.1.1.3  christos 			/* Qname proven non existing, get wildcard record for
   1441  1.1.1.3  christos 			 * QTYPE or NSEC covering or matching wildcard. */
   1442  1.1.1.3  christos 
   1443  1.1.1.3  christos 			/* Num labels in ce is always smaller than in qname,
   1444  1.1.1.3  christos 			 * therefore adding the wildcard label cannot overflow
   1445  1.1.1.3  christos 			 * buffer. */
   1446  1.1.1.3  christos 			wc_ce[0] = 1;
   1447  1.1.1.3  christos 			wc_ce[1] = (uint8_t)'*';
   1448  1.1.1.3  christos 			memmove(wc_ce+2, ce, ce_len);
   1449  1.1.1.3  christos 			wc_qinfo.qname = wc_ce;
   1450  1.1.1.3  christos 			wc_qinfo.qname_len = ce_len + 2;
   1451  1.1.1.3  christos 			wc_qinfo.qtype = qinfo->qtype;
   1452  1.1.1.3  christos 
   1453  1.1.1.3  christos 
   1454  1.1.1.3  christos 			if((cache_wc = rrset_cache_lookup(rrset_cache, wc_qinfo.qname,
   1455  1.1.1.3  christos 				wc_qinfo.qname_len, wc_qinfo.qtype,
   1456  1.1.1.3  christos 				qinfo->qclass, 0/*flags*/, now, 0/*read only*/))) {
   1457  1.1.1.3  christos 				/* Synthesize wildcard answer */
   1458  1.1.1.3  christos 				wcrr_data = (struct packed_rrset_data*)cache_wc->entry.data;
   1459  1.1.1.3  christos 				if(!(wcrr_data->security == sec_status_secure ||
   1460  1.1.1.3  christos 					(wcrr_data->security == sec_status_unchecked &&
   1461  1.1.1.3  christos 					wcrr_data->rrsig_count > 0))) {
   1462  1.1.1.3  christos 					lock_rw_unlock(&cache_wc->entry.lock);
   1463  1.1.1.3  christos 					return NULL;
   1464  1.1.1.3  christos 				}
   1465  1.1.1.3  christos 				if(!(wcrr = packed_rrset_copy_region(cache_wc,
   1466  1.1.1.3  christos 					region, now))) {
   1467  1.1.1.3  christos 					lock_rw_unlock(&cache_wc->entry.lock);
   1468  1.1.1.3  christos 					return NULL;
   1469  1.1.1.3  christos 				};
   1470  1.1.1.3  christos 				lock_rw_unlock(&cache_wc->entry.lock);
   1471  1.1.1.3  christos 				wcrr->rk.dname = qinfo->qname;
   1472  1.1.1.3  christos 				wcrr->rk.dname_len = qinfo->qname_len;
   1473  1.1.1.3  christos 				if(!dns_msg_ansadd(msg, region, wcrr, 0))
   1474  1.1.1.3  christos 					return NULL;
   1475  1.1.1.3  christos 				/* No SOA needed for wildcard synthesised
   1476  1.1.1.3  christos 				 * answer. */
   1477  1.1.1.3  christos 				addsoa = 0;
   1478  1.1.1.3  christos 			} else {
   1479  1.1.1.3  christos 				/* Get wildcard NSEC for possible non existence
   1480  1.1.1.3  christos 				 * proof */
   1481  1.1.1.3  christos 				if(!(wcrr = neg_find_nsec(neg, wc_qinfo.qname,
   1482  1.1.1.3  christos 					wc_qinfo.qname_len, qinfo->qclass,
   1483  1.1.1.3  christos 					rrset_cache, now, region)))
   1484  1.1.1.3  christos 					return NULL;
   1485  1.1.1.3  christos 
   1486  1.1.1.3  christos 				nodata_wc = NULL;
   1487  1.1.1.3  christos 				if(val_nsec_proves_name_error(wcrr, wc_ce))
   1488  1.1.1.3  christos 					rcode = LDNS_RCODE_NXDOMAIN;
   1489  1.1.1.3  christos 				else if(!nsec_proves_nodata(wcrr, &wc_qinfo,
   1490  1.1.1.3  christos 					&nodata_wc) || nodata_wc)
   1491  1.1.1.3  christos 					/* &nodata_wc shouldn't be set, wc_qinfo
   1492  1.1.1.3  christos 					 * already contains wildcard domain. */
   1493  1.1.1.3  christos 					/* NSEC doesn't prove anything for
   1494  1.1.1.3  christos 					 * wildcard. */
   1495  1.1.1.3  christos 					return NULL;
   1496  1.1.1.3  christos 				if(query_dname_compare(wcrr->rk.dname,
   1497  1.1.1.3  christos 					nsec->rk.dname) != 0)
   1498  1.1.1.3  christos 					if(!dns_msg_authadd(msg, region, wcrr, 0))
   1499  1.1.1.3  christos 						return NULL;
   1500  1.1.1.3  christos 			}
   1501  1.1.1.3  christos 		}
   1502  1.1.1.3  christos 
   1503  1.1.1.3  christos 		if(!dns_msg_authadd(msg, region, nsec, 0))
   1504      1.1  christos 			return NULL;
   1505      1.1  christos 		if(addsoa && !add_soa(rrset_cache, now, region, msg, NULL))
   1506      1.1  christos 			return NULL;
   1507  1.1.1.3  christos 
   1508  1.1.1.3  christos 		/* Increment statistic counters */
   1509  1.1.1.3  christos 		lock_basic_lock(&neg->lock);
   1510  1.1.1.3  christos 		if(rcode == LDNS_RCODE_NOERROR)
   1511  1.1.1.3  christos 			neg->num_neg_cache_noerror++;
   1512  1.1.1.3  christos 		else if(rcode == LDNS_RCODE_NXDOMAIN)
   1513  1.1.1.3  christos 			neg->num_neg_cache_nxdomain++;
   1514  1.1.1.3  christos 		lock_basic_unlock(&neg->lock);
   1515  1.1.1.3  christos 
   1516  1.1.1.3  christos 		FLAGS_SET_RCODE(msg->rep->flags, rcode);
   1517      1.1  christos 		return msg;
   1518      1.1  christos 	}
   1519      1.1  christos 
   1520  1.1.1.3  christos 	/* No aggressive use of NSEC3 for now, only proceed for DS types. */
   1521  1.1.1.3  christos 	if(qinfo->qtype != LDNS_RR_TYPE_DS){
   1522  1.1.1.3  christos 		return NULL;
   1523  1.1.1.3  christos 	}
   1524      1.1  christos 	/* check NSEC3 neg cache for type DS */
   1525      1.1  christos 	/* need to look one zone higher for DS type */
   1526      1.1  christos 	zname = qinfo->qname;
   1527      1.1  christos 	zname_len = qinfo->qname_len;
   1528      1.1  christos 	dname_remove_label(&zname, &zname_len);
   1529      1.1  christos 	zname_labs = dname_count_labels(zname);
   1530      1.1  christos 
   1531      1.1  christos 	/* lookup closest zone */
   1532      1.1  christos 	lock_basic_lock(&neg->lock);
   1533      1.1  christos 	zone = neg_closest_zone_parent(neg, zname, zname_len, zname_labs,
   1534      1.1  christos 		qinfo->qclass);
   1535      1.1  christos 	while(zone && !zone->in_use)
   1536      1.1  christos 		zone = zone->parent;
   1537      1.1  christos 	/* check that the zone is not too high up so that we do not pick data
   1538      1.1  christos 	 * out of a zone that is above the last-seen key (or trust-anchor). */
   1539      1.1  christos 	if(zone && topname) {
   1540      1.1  christos 		if(!dname_subdomain_c(zone->name, topname))
   1541      1.1  christos 			zone = NULL;
   1542      1.1  christos 	}
   1543      1.1  christos 	if(!zone) {
   1544      1.1  christos 		lock_basic_unlock(&neg->lock);
   1545      1.1  christos 		return NULL;
   1546      1.1  christos 	}
   1547      1.1  christos 
   1548      1.1  christos 	msg = neg_nsec3_proof_ds(zone, qinfo->qname, qinfo->qname_len,
   1549      1.1  christos 		zname_labs+1, buf, rrset_cache, region, now, topname);
   1550      1.1  christos 	if(msg && addsoa && !add_soa(rrset_cache, now, region, msg, zone)) {
   1551      1.1  christos 		lock_basic_unlock(&neg->lock);
   1552      1.1  christos 		return NULL;
   1553      1.1  christos 	}
   1554      1.1  christos 	lock_basic_unlock(&neg->lock);
   1555      1.1  christos 	return msg;
   1556      1.1  christos }
   1557  1.1.1.7  christos 
   1558  1.1.1.7  christos void
   1559  1.1.1.7  christos val_neg_adjust_size(struct val_neg_cache* neg, size_t max)
   1560  1.1.1.7  christos {
   1561  1.1.1.7  christos 	lock_basic_lock(&neg->lock);
   1562  1.1.1.7  christos 	neg->max = max;
   1563  1.1.1.7  christos 	neg_make_space(neg, 0);
   1564  1.1.1.7  christos 	lock_basic_unlock(&neg->lock);
   1565  1.1.1.7  christos }
   1566