xref: /freebsd/contrib/unbound/util/data/packed_rrset.c (revision f1951fd745b894fe6586c298874af98544a5e272)
1 /*
2  * util/data/packed_rrset.c - data storage for a set of resource records.
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /**
37  * \file
38  *
39  * This file contains the data storage for RRsets.
40  */
41 
42 #include "config.h"
43 #include "util/data/packed_rrset.h"
44 #include "util/data/dname.h"
45 #include "util/storage/lookup3.h"
46 #include "util/log.h"
47 #include "util/alloc.h"
48 #include "util/regional.h"
49 #include "util/net_help.h"
50 #include "sldns/rrdef.h"
51 #include "sldns/sbuffer.h"
52 #include "sldns/wire2str.h"
53 
54 void
55 ub_packed_rrset_parsedelete(struct ub_packed_rrset_key* pkey,
56         struct alloc_cache* alloc)
57 {
58 	if(!pkey)
59 		return;
60 	free(pkey->entry.data);
61 	pkey->entry.data = NULL;
62 	free(pkey->rk.dname);
63 	pkey->rk.dname = NULL;
64 	pkey->id = 0;
65 	alloc_special_release(alloc, pkey);
66 }
67 
68 size_t
69 ub_rrset_sizefunc(void* key, void* data)
70 {
71 	struct ub_packed_rrset_key* k = (struct ub_packed_rrset_key*)key;
72 	struct packed_rrset_data* d = (struct packed_rrset_data*)data;
73 	size_t s = sizeof(struct ub_packed_rrset_key) + k->rk.dname_len;
74 	s += packed_rrset_sizeof(d) + lock_get_mem(&k->entry.lock);
75 	return s;
76 }
77 
78 size_t
79 packed_rrset_sizeof(struct packed_rrset_data* d)
80 {
81 	size_t s;
82 	if(d->rrsig_count > 0) {
83 		s = ((uint8_t*)d->rr_data[d->count+d->rrsig_count-1] -
84 			(uint8_t*)d) + d->rr_len[d->count+d->rrsig_count-1];
85 	} else {
86 		log_assert(d->count > 0);
87 		s = ((uint8_t*)d->rr_data[d->count-1] - (uint8_t*)d) +
88 			d->rr_len[d->count-1];
89 	}
90 	return s;
91 }
92 
93 int
94 ub_rrset_compare(void* k1, void* k2)
95 {
96 	struct ub_packed_rrset_key* key1 = (struct ub_packed_rrset_key*)k1;
97 	struct ub_packed_rrset_key* key2 = (struct ub_packed_rrset_key*)k2;
98 	int c;
99 	if(key1 == key2)
100 		return 0;
101 	if(key1->rk.type != key2->rk.type) {
102 		if(key1->rk.type < key2->rk.type)
103 			return -1;
104 		return 1;
105 	}
106 	if(key1->rk.dname_len != key2->rk.dname_len) {
107 		if(key1->rk.dname_len < key2->rk.dname_len)
108 			return -1;
109 		return 1;
110 	}
111 	if((c=query_dname_compare(key1->rk.dname, key2->rk.dname)) != 0)
112 		return c;
113 	if(key1->rk.rrset_class != key2->rk.rrset_class) {
114 		if(key1->rk.rrset_class < key2->rk.rrset_class)
115 			return -1;
116 		return 1;
117 	}
118 	if(key1->rk.flags != key2->rk.flags) {
119 		if(key1->rk.flags < key2->rk.flags)
120 			return -1;
121 		return 1;
122 	}
123 	return 0;
124 }
125 
126 void
127 ub_rrset_key_delete(void* key, void* userdata)
128 {
129 	struct ub_packed_rrset_key* k = (struct ub_packed_rrset_key*)key;
130 	struct alloc_cache* a = (struct alloc_cache*)userdata;
131 	k->id = 0;
132 	free(k->rk.dname);
133 	k->rk.dname = NULL;
134 	alloc_special_release(a, k);
135 }
136 
137 void
138 rrset_data_delete(void* data, void* ATTR_UNUSED(userdata))
139 {
140 	struct packed_rrset_data* d = (struct packed_rrset_data*)data;
141 	free(d);
142 }
143 
144 int
145 rrsetdata_equal(struct packed_rrset_data* d1, struct packed_rrset_data* d2)
146 {
147 	size_t i;
148 	size_t total;
149 	if(d1->count != d2->count || d1->rrsig_count != d2->rrsig_count)
150 		return 0;
151 	total = d1->count + d1->rrsig_count;
152 	for(i=0; i<total; i++) {
153 		if(d1->rr_len[i] != d2->rr_len[i])
154 			return 0;
155 		if(memcmp(d1->rr_data[i], d2->rr_data[i], d1->rr_len[i]) != 0)
156 			return 0;
157 	}
158 	return 1;
159 }
160 
161 hashvalue_type
162 rrset_key_hash(struct packed_rrset_key* key)
163 {
164 	/* type is hashed in host order */
165 	uint16_t t = ntohs(key->type);
166 	/* Note this MUST be identical to pkt_hash_rrset in msgparse.c */
167 	/* this routine does not have a compressed name */
168 	hashvalue_type h = 0xab;
169 	h = dname_query_hash(key->dname, h);
170 	h = hashlittle(&t, sizeof(t), h);
171 	h = hashlittle(&key->rrset_class, sizeof(uint16_t), h);
172 	h = hashlittle(&key->flags, sizeof(uint32_t), h);
173 	return h;
174 }
175 
176 void
177 packed_rrset_ptr_fixup(struct packed_rrset_data* data)
178 {
179 	size_t i;
180 	size_t total = data->count + data->rrsig_count;
181 	uint8_t* nextrdata;
182 	/* fixup pointers in packed rrset data */
183 	data->rr_len = (size_t*)((uint8_t*)data +
184 		sizeof(struct packed_rrset_data));
185 	data->rr_data = (uint8_t**)&(data->rr_len[total]);
186 	data->rr_ttl = (time_t*)&(data->rr_data[total]);
187 	nextrdata = (uint8_t*)&(data->rr_ttl[total]);
188 	for(i=0; i<total; i++) {
189 		data->rr_data[i] = nextrdata;
190 		nextrdata += data->rr_len[i];
191 	}
192 }
193 
194 void
195 get_cname_target(struct ub_packed_rrset_key* rrset, uint8_t** dname,
196 	size_t* dname_len)
197 {
198 	struct packed_rrset_data* d;
199 	size_t len;
200 	if(ntohs(rrset->rk.type) != LDNS_RR_TYPE_CNAME &&
201 		ntohs(rrset->rk.type) != LDNS_RR_TYPE_DNAME)
202 		return;
203 	d = (struct packed_rrset_data*)rrset->entry.data;
204 	if(d->count < 1)
205 		return;
206 	if(d->rr_len[0] < 3) /* at least rdatalen + 0byte root label */
207 		return;
208 	len = sldns_read_uint16(d->rr_data[0]);
209 	if(len != d->rr_len[0] - sizeof(uint16_t))
210 		return;
211 	if(dname_valid(d->rr_data[0]+sizeof(uint16_t), len) != len)
212 		return;
213 	*dname = d->rr_data[0]+sizeof(uint16_t);
214 	*dname_len = len;
215 }
216 
217 void
218 packed_rrset_ttl_add(struct packed_rrset_data* data, time_t add)
219 {
220 	size_t i;
221 	size_t total = data->count + data->rrsig_count;
222 	data->ttl += add;
223 	for(i=0; i<total; i++)
224 		data->rr_ttl[i] += add;
225 }
226 
227 const char*
228 rrset_trust_to_string(enum rrset_trust s)
229 {
230 	switch(s) {
231 	case rrset_trust_none: 		return "rrset_trust_none";
232 	case rrset_trust_add_noAA: 	return "rrset_trust_add_noAA";
233 	case rrset_trust_auth_noAA: 	return "rrset_trust_auth_noAA";
234 	case rrset_trust_add_AA: 	return "rrset_trust_add_AA";
235 	case rrset_trust_nonauth_ans_AA:return "rrset_trust_nonauth_ans_AA";
236 	case rrset_trust_ans_noAA: 	return "rrset_trust_ans_noAA";
237 	case rrset_trust_glue: 		return "rrset_trust_glue";
238 	case rrset_trust_auth_AA: 	return "rrset_trust_auth_AA";
239 	case rrset_trust_ans_AA: 	return "rrset_trust_ans_AA";
240 	case rrset_trust_sec_noglue: 	return "rrset_trust_sec_noglue";
241 	case rrset_trust_prim_noglue: 	return "rrset_trust_prim_noglue";
242 	case rrset_trust_validated: 	return "rrset_trust_validated";
243 	case rrset_trust_ultimate: 	return "rrset_trust_ultimate";
244 	}
245 	return "unknown_rrset_trust_value";
246 }
247 
248 const char*
249 sec_status_to_string(enum sec_status s)
250 {
251 	switch(s) {
252 	case sec_status_unchecked: 	return "sec_status_unchecked";
253 	case sec_status_bogus: 		return "sec_status_bogus";
254 	case sec_status_indeterminate: 	return "sec_status_indeterminate";
255 	case sec_status_insecure: 	return "sec_status_insecure";
256 	case sec_status_secure_sentinel_fail: 	return "sec_status_secure_sentinel_fail";
257 	case sec_status_secure: 	return "sec_status_secure";
258 	}
259 	return "unknown_sec_status_value";
260 }
261 
262 void log_rrset_key(enum verbosity_value v, const char* str,
263 	struct ub_packed_rrset_key* rrset)
264 {
265 	if(verbosity >= v)
266 		log_nametypeclass(v, str, rrset->rk.dname,
267 			ntohs(rrset->rk.type), ntohs(rrset->rk.rrset_class));
268 }
269 
270 int packed_rr_to_string(struct ub_packed_rrset_key* rrset, size_t i,
271 	time_t now, char* dest, size_t dest_len)
272 {
273 	struct packed_rrset_data* d = (struct packed_rrset_data*)rrset->
274 		entry.data;
275 	uint8_t rr[65535];
276 	size_t rlen = rrset->rk.dname_len + 2 + 2 + 4 + d->rr_len[i];
277 	log_assert(dest_len > 0 && dest);
278 	if(rlen > dest_len) {
279 		dest[0] = 0;
280 		return 0;
281 	}
282 	memmove(rr, rrset->rk.dname, rrset->rk.dname_len);
283 	if(i < d->count)
284 		memmove(rr+rrset->rk.dname_len, &rrset->rk.type, 2);
285 	else	sldns_write_uint16(rr+rrset->rk.dname_len, LDNS_RR_TYPE_RRSIG);
286 	memmove(rr+rrset->rk.dname_len+2, &rrset->rk.rrset_class, 2);
287 	sldns_write_uint32(rr+rrset->rk.dname_len+4,
288 		(uint32_t)(d->rr_ttl[i]-now));
289 	memmove(rr+rrset->rk.dname_len+8, d->rr_data[i], d->rr_len[i]);
290 	if(sldns_wire2str_rr_buf(rr, rlen, dest, dest_len) == -1) {
291 		log_info("rrbuf failure %d %s", (int)d->rr_len[i], dest);
292 		dest[0] = 0;
293 		return 0;
294 	}
295 	return 1;
296 }
297 
298 void log_packed_rrset(enum verbosity_value v, const char* str,
299 	struct ub_packed_rrset_key* rrset)
300 {
301 	struct packed_rrset_data* d = (struct packed_rrset_data*)rrset->
302 		entry.data;
303 	char buf[65535];
304 	size_t i;
305 	if(verbosity < v)
306 		return;
307 	for(i=0; i<d->count+d->rrsig_count; i++) {
308 		if(!packed_rr_to_string(rrset, i, 0, buf, sizeof(buf))) {
309 			log_info("%s: rr %d wire2str-error", str, (int)i);
310 		} else {
311 			log_info("%s: %s", str, buf);
312 		}
313 	}
314 }
315 
316 time_t
317 ub_packed_rrset_ttl(struct ub_packed_rrset_key* key)
318 {
319 	struct packed_rrset_data* d = (struct packed_rrset_data*)key->
320 		entry.data;
321 	return d->ttl;
322 }
323 
324 struct ub_packed_rrset_key*
325 packed_rrset_copy_region(struct ub_packed_rrset_key* key,
326 	struct regional* region, time_t now)
327 {
328 	struct ub_packed_rrset_key* ck = regional_alloc(region,
329 		sizeof(struct ub_packed_rrset_key));
330 	struct packed_rrset_data* d;
331 	struct packed_rrset_data* data = (struct packed_rrset_data*)
332 		key->entry.data;
333 	size_t dsize, i;
334 	if(!ck)
335 		return NULL;
336 	ck->id = key->id;
337 	memset(&ck->entry, 0, sizeof(ck->entry));
338 	ck->entry.hash = key->entry.hash;
339 	ck->entry.key = ck;
340 	ck->rk = key->rk;
341 	ck->rk.dname = regional_alloc_init(region, key->rk.dname,
342 		key->rk.dname_len);
343 	if(!ck->rk.dname)
344 		return NULL;
345 	dsize = packed_rrset_sizeof(data);
346 	d = (struct packed_rrset_data*)regional_alloc_init(region, data, dsize);
347 	if(!d)
348 		return NULL;
349 	ck->entry.data = d;
350 	packed_rrset_ptr_fixup(d);
351 	/* make TTLs relative - once per rrset */
352 	for(i=0; i<d->count + d->rrsig_count; i++) {
353 		if(d->rr_ttl[i] < now)
354 			d->rr_ttl[i] = 0;
355 		else	d->rr_ttl[i] -= now;
356 	}
357 	if(d->ttl < now)
358 		d->ttl = 0;
359 	else	d->ttl -= now;
360 	return ck;
361 }
362 
363 struct ub_packed_rrset_key*
364 packed_rrset_copy_alloc(struct ub_packed_rrset_key* key,
365 	struct alloc_cache* alloc, time_t now)
366 {
367 	struct packed_rrset_data* fd, *dd;
368 	struct ub_packed_rrset_key* dk = alloc_special_obtain(alloc);
369 	if(!dk) return NULL;
370 	fd = (struct packed_rrset_data*)key->entry.data;
371 	dk->entry.hash = key->entry.hash;
372 	dk->rk = key->rk;
373 	dk->rk.dname = (uint8_t*)memdup(key->rk.dname, key->rk.dname_len);
374 	if(!dk->rk.dname) {
375 		alloc_special_release(alloc, dk);
376 		return NULL;
377 	}
378 	dd = (struct packed_rrset_data*)memdup(fd, packed_rrset_sizeof(fd));
379 	if(!dd) {
380 		free(dk->rk.dname);
381 		alloc_special_release(alloc, dk);
382 		return NULL;
383 	}
384 	packed_rrset_ptr_fixup(dd);
385 	dk->entry.data = (void*)dd;
386 	packed_rrset_ttl_add(dd, now);
387 	return dk;
388 }
389