Home | History | Annotate | Line # | Download | only in data
packed_rrset.h revision 1.1.1.2
      1 /*
      2  * util/data/packed_rrset.h - 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 #ifndef UTIL_DATA_PACKED_RRSET_H
     43 #define UTIL_DATA_PACKED_RRSET_H
     44 #include "util/storage/lruhash.h"
     45 struct alloc_cache;
     46 struct regional;
     47 
     48 /** type used to uniquely identify rrsets. Cannot be reused without
     49  * clearing the cache. */
     50 typedef uint64_t rrset_id_type;
     51 
     52 /** this rrset is NSEC and is at zone apex (at child side of zonecut) */
     53 #define PACKED_RRSET_NSEC_AT_APEX 0x1
     54 /** this rrset is A/AAAA and is in-zone-glue (from parent side of zonecut) */
     55 #define PACKED_RRSET_PARENT_SIDE 0x2
     56 /** this rrset is SOA and has the negative ttl (from nxdomain or nodata),
     57  * this is set on SOA rrsets in the authority section, to keep its TTL separate
     58  * from the SOA in the answer section from a direct SOA query or ANY query. */
     59 #define PACKED_RRSET_SOA_NEG 0x4
     60 /** This rrset is considered to have a fixed TTL; its TTL doesn't have to be
     61  * updated on encoding in a reply.  This flag is not expected to be set in
     62  * cached data. */
     63 #define PACKED_RRSET_FIXEDTTL 0x80000000
     64 
     65 /** number of rrs and rrsets for integer overflow protection.  More than
     66  * this is not really possible (64K packet has much less RRs and RRsets) in
     67  * a message.  And this is small enough that also multiplied there is no
     68  * integer overflow. */
     69 #define RR_COUNT_MAX 0xffffff
     70 
     71 /**
     72  * The identifying information for an RRset.
     73  */
     74 struct packed_rrset_key {
     75 	/**
     76 	 * The domain name. If not null (for id=0) it is allocated, and
     77 	 * contains the wireformat domain name.
     78 	 * This dname is not canonicalized.
     79 	 */
     80 	uint8_t* dname;
     81 	/**
     82 	 * Length of the domain name, including last 0 root octet.
     83 	 */
     84 	size_t dname_len;
     85 	/**
     86 	 * Flags. 32bit to be easy for hashing:
     87 	 * 	o PACKED_RRSET_NSEC_AT_APEX
     88 	 * 	o PACKED_RRSET_PARENT_SIDE
     89 	 * 	o PACKED_RRSET_SOA_NEG
     90 	 * 	o PACKED_RRSET_FIXEDTTL (not supposed to be cached)
     91 	 */
     92 	uint32_t flags;
     93 	/** the rrset type in network format */
     94 	uint16_t type;
     95 	/** the rrset class in network format */
     96 	uint16_t rrset_class;
     97 };
     98 
     99 /**
    100  * This structure contains an RRset. A set of resource records that
    101  * share the same domain name, type and class.
    102  *
    103  * Due to memory management and threading, the key structure cannot be
    104  * deleted, although the data can be. The id can be set to 0 to store and the
    105  * structure can be recycled with a new id.
    106  */
    107 struct ub_packed_rrset_key {
    108 	/**
    109 	 * entry into hashtable. Note the lock is never destroyed,
    110 	 *  even when this key is retired to the cache.
    111 	 * the data pointer (if not null) points to a struct packed_rrset.
    112 	 */
    113 	struct lruhash_entry entry;
    114 	/**
    115 	 * the ID of this rrset. unique, based on threadid + sequenceno.
    116 	 * ids are not reused, except after flushing the cache.
    117 	 * zero is an unused entry, and never a valid id.
    118 	 * Check this value after getting entry.lock.
    119 	 * The other values in this struct may only be altered after changing
    120 	 * the id (which needs a writelock on entry.lock).
    121 	 */
    122 	rrset_id_type id;
    123 	/** key data: dname, type and class */
    124 	struct packed_rrset_key rk;
    125 };
    126 
    127 /**
    128  * RRset trustworthiness. Bigger value is more trust. RFC 2181.
    129  * The rrset_trust_add_noAA, rrset_trust_auth_noAA, rrset_trust_add_AA,
    130  * are mentioned as the same trustworthiness in 2181, but split up here
    131  * for ease of processing.
    132  *
    133  * rrset_trust_nonauth_ans_AA, rrset_trust_ans_noAA
    134  * are also mentioned as the same trustworthiness in 2181, but split up here
    135  * for ease of processing.
    136  *
    137  * Added trust_none for a sane initial value, smaller than anything else.
    138  * Added validated and ultimate trust for keys and rrsig validated content.
    139  */
    140 enum rrset_trust {
    141 	/** initial value for trust */
    142 	rrset_trust_none = 0,
    143 	/** Additional information from non-authoritative answers */
    144 	rrset_trust_add_noAA,
    145 	/** Data from the authority section of a non-authoritative answer */
    146 	rrset_trust_auth_noAA,
    147 	/** Additional information from an authoritative answer */
    148 	rrset_trust_add_AA,
    149 	/** non-authoritative data from the answer section of authoritative
    150 	 * answers */
    151 	rrset_trust_nonauth_ans_AA,
    152 	/** Data from the answer section of a non-authoritative answer */
    153 	rrset_trust_ans_noAA,
    154 	/** Glue from a primary zone, or glue from a zone transfer */
    155 	rrset_trust_glue,
    156 	/** Data from the authority section of an authoritative answer */
    157 	rrset_trust_auth_AA,
    158 	/** The authoritative data included in the answer section of an
    159 	 *  authoritative reply */
    160 	rrset_trust_ans_AA,
    161 	/** Data from a zone transfer, other than glue */
    162 	rrset_trust_sec_noglue,
    163 	/** Data from a primary zone file, other than glue data */
    164 	rrset_trust_prim_noglue,
    165 	/** DNSSEC(rfc4034) validated with trusted keys */
    166 	rrset_trust_validated,
    167 	/** ultimately trusted, no more trust is possible;
    168 	 * trusted keys from the unbound configuration setup. */
    169 	rrset_trust_ultimate
    170 };
    171 
    172 /**
    173  * Security status from validation for data.
    174  * The order is significant; more secure, more proven later.
    175  */
    176 enum sec_status {
    177 	/** UNCHECKED means that object has yet to be validated. */
    178 	sec_status_unchecked = 0,
    179 	/** BOGUS means that the object (RRset or message) failed to validate
    180 	 *  (according to local policy), but should have validated. */
    181 	sec_status_bogus,
    182 	/** INDETERMINATE means that the object is insecure, but not
    183 	 * authoritatively so. Generally this means that the RRset is not
    184 	 * below a configured trust anchor. */
    185 	sec_status_indeterminate,
    186 	/** INSECURE means that the object is authoritatively known to be
    187 	 * insecure. Generally this means that this RRset is below a trust
    188 	 * anchor, but also below a verified, insecure delegation. */
    189 	sec_status_insecure,
    190 	/** SECURE means that the object (RRset or message) validated
    191 	 * according to local policy. */
    192 	sec_status_secure
    193 };
    194 
    195 /**
    196  * RRset data.
    197  *
    198  * The data is packed, stored contiguously in memory.
    199  *
    200  * It is not always stored contiguously, in that case, an unpacked-packed
    201  * rrset has the arrays separate.  A bunch of routines work on that, but
    202  * the packed rrset that is contiguous is for the rrset-cache and the
    203  * cache-response routines in daemon/worker.c.
    204  *
    205  * memory layout:
    206  *	o base struct
    207  *	o rr_len size_t array
    208  *	o rr_data uint8_t* array
    209  *	o rr_ttl time_t array (after size_t and ptrs because those may be
    210  *		64bit and this array before those would make them unaligned).
    211  *		Since the stuff before is 32/64bit, rr_ttl is 32 bit aligned.
    212  *	o rr_data rdata wireformats
    213  *	o rrsig_data rdata wireformat(s)
    214  *
    215  * Rdata is stored in wireformat. The dname is stored in wireformat.
    216  * TTLs are stored as absolute values (and could be expired).
    217  *
    218  * RRSIGs are stored in the arrays after the regular rrs.
    219  *
    220  * You need the packed_rrset_key to know dname, type, class of the
    221  * resource records in this RRset. (if signed the rrsig gives the type too).
    222  *
    223  * On the wire an RR is:
    224  *	name, type, class, ttl, rdlength, rdata.
    225  * So we need to send the following per RR:
    226  *	key.dname, ttl, rr_data[i].
    227  *	since key.dname ends with type and class.
    228  *	and rr_data starts with the rdlength.
    229  *	the ttl value to send changes due to time.
    230  */
    231 struct packed_rrset_data {
    232 	/** TTL (in seconds like time()) of the rrset.
    233 	 * Same for all RRs see rfc2181(5.2).  */
    234 	time_t ttl;
    235 	/** number of rrs. */
    236 	size_t count;
    237 	/** number of rrsigs, if 0 no rrsigs */
    238 	size_t rrsig_count;
    239 	/** the trustworthiness of the rrset data */
    240 	enum rrset_trust trust;
    241 	/** security status of the rrset data */
    242 	enum sec_status security;
    243 	/** length of every rr's rdata, rr_len[i] is size of rr_data[i]. */
    244 	size_t* rr_len;
    245 	/** ttl of every rr. rr_ttl[i] ttl of rr i. */
    246 	time_t *rr_ttl;
    247 	/**
    248 	 * Array of pointers to every rr's rdata.
    249 	 * The rr_data[i] rdata is stored in uncompressed wireformat.
    250 	 * The first uint16_t of rr_data[i] is network format rdlength.
    251 	 *
    252 	 * rr_data[count] to rr_data[count+rrsig_count] contain the rrsig data.
    253 	 */
    254 	uint8_t** rr_data;
    255 };
    256 
    257 /**
    258  * An RRset can be represented using both key and data together.
    259  * Split into key and data structures to simplify implementation of
    260  * caching schemes.
    261  */
    262 struct packed_rrset {
    263 	/** domain name, type and class */
    264 	struct packed_rrset_key* k;
    265 	/** ttl, count and rdatas (and rrsig) */
    266 	struct packed_rrset_data* d;
    267 };
    268 
    269 /**
    270  * list of packed rrsets
    271  */
    272 struct packed_rrset_list {
    273 	/** next in list */
    274 	struct packed_rrset_list* next;
    275 	/** rrset key and data */
    276 	struct packed_rrset rrset;
    277 };
    278 
    279 /**
    280  * Delete packed rrset key and data, not entered in hashtables yet.
    281  * Used during parsing.
    282  * @param pkey: rrset key structure with locks, key and data pointers.
    283  * @param alloc: where to return the unfree-able key structure.
    284  */
    285 void ub_packed_rrset_parsedelete(struct ub_packed_rrset_key* pkey,
    286 	struct alloc_cache* alloc);
    287 
    288 /**
    289  * Memory size of rrset data. RRset data must be filled in correctly.
    290  * @param data: data to examine.
    291  * @return size in bytes.
    292  */
    293 size_t packed_rrset_sizeof(struct packed_rrset_data* data);
    294 
    295 /**
    296  * Get TTL of rrset. RRset data must be filled in correctly.
    297  * @param key: rrset key, with data to examine.
    298  * @return ttl value.
    299  */
    300 time_t ub_packed_rrset_ttl(struct ub_packed_rrset_key* key);
    301 
    302 /**
    303  * Calculate memory size of rrset entry. For hash table usage.
    304  * @param key: struct ub_packed_rrset_key*.
    305  * @param data: struct packed_rrset_data*.
    306  * @return size in bytes.
    307  */
    308 size_t ub_rrset_sizefunc(void* key, void* data);
    309 
    310 /**
    311  * compares two rrset keys.
    312  * @param k1: struct ub_packed_rrset_key*.
    313  * @param k2: struct ub_packed_rrset_key*.
    314  * @return 0 if equal.
    315  */
    316 int ub_rrset_compare(void* k1, void* k2);
    317 
    318 /**
    319  * compare two rrset data structures.
    320  * Compared rdata and rrsigdata, not the trust or ttl value.
    321  * @param d1: data to compare.
    322  * @param d2: data to compare.
    323  * @return 1 if equal.
    324  */
    325 int rrsetdata_equal(struct packed_rrset_data* d1, struct packed_rrset_data* d2);
    326 
    327 /**
    328  * Old key to be deleted. RRset keys are recycled via alloc.
    329  * The id is set to 0. So that other threads, after acquiring a lock always
    330  * get the correct value, in this case the 0 deleted-special value.
    331  * @param key: struct ub_packed_rrset_key*.
    332  * @param userdata: alloc structure to use for recycling.
    333  */
    334 void ub_rrset_key_delete(void* key, void* userdata);
    335 
    336 /**
    337  * Old data to be deleted.
    338  * @param data: what to delete.
    339  * @param userdata: user data ptr.
    340  */
    341 void rrset_data_delete(void* data, void* userdata);
    342 
    343 /**
    344  * Calculate hash value for a packed rrset key.
    345  * @param key: the rrset key with name, type, class, flags.
    346  * @return hash value.
    347  */
    348 hashvalue_type rrset_key_hash(struct packed_rrset_key* key);
    349 
    350 /**
    351  * Fixup pointers in fixed data packed_rrset_data blob.
    352  * After a memcpy of the data for example. Will set internal pointers right.
    353  * @param data: rrset data structure. Otherwise correctly filled in.
    354  */
    355 void packed_rrset_ptr_fixup(struct packed_rrset_data* data);
    356 
    357 /**
    358  * Fixup TTLs in fixed data packed_rrset_data blob.
    359  * @param data: rrset data structure. Otherwise correctly filled in.
    360  * @param add: how many seconds to add, pass time(0) for example.
    361  */
    362 void packed_rrset_ttl_add(struct packed_rrset_data* data, time_t add);
    363 
    364 /**
    365  * Utility procedure to extract CNAME target name from its rdata.
    366  * Failsafes; it will change passed dname to a valid dname or do nothing.
    367  * @param rrset: the rrset structure. Must be a CNAME.
    368  *	Only first RR is used (multiple RRs are technically illegal anyway).
    369  * 	Also works on type DNAME. Returns target name.
    370  * @param dname: this pointer is updated to point into the cname rdata.
    371  *	If a failsafe fails, nothing happens to the pointer (such as the
    372  *	rdata was not a valid dname, not a CNAME, ...).
    373  * @param dname_len: length of dname is returned.
    374  */
    375 void get_cname_target(struct ub_packed_rrset_key* rrset, uint8_t** dname,
    376 	size_t* dname_len);
    377 
    378 /**
    379  * Get a printable string for a rrset trust value
    380  * @param s: rrset trust value
    381  * @return printable string.
    382  */
    383 const char* rrset_trust_to_string(enum rrset_trust s);
    384 
    385 /**
    386  * Get a printable string for a security status value
    387  * @param s: security status
    388  * @return printable string.
    389  */
    390 const char* sec_status_to_string(enum sec_status s);
    391 
    392 /**
    393  * Print string with neat domain name, type, class from rrset.
    394  * @param v: at what verbosity level to print this.
    395  * @param str: string of message.
    396  * @param rrset: structure with name, type and class.
    397  */
    398 void log_rrset_key(enum verbosity_value v, const char* str,
    399 	struct ub_packed_rrset_key* rrset);
    400 
    401 /**
    402  * Convert RR from RRset to string.
    403  * @param rrset: structure with data.
    404  * @param i: index of rr or RRSIG.
    405  * @param now: time that is subtracted from ttl before printout. Can be 0.
    406  * @param dest: destination string buffer. Must be nonNULL.
    407  * @param dest_len: length of dest buffer (>0).
    408  * @return false on failure.
    409  */
    410 int packed_rr_to_string(struct ub_packed_rrset_key* rrset, size_t i,
    411 	time_t now, char* dest, size_t dest_len);
    412 
    413 /**
    414  * Print the string with prefix, one rr per line.
    415  * @param v: at what verbosity level to print this.
    416  * @param str: string of message.
    417  * @param rrset: with name, and rdata, and rrsigs.
    418  */
    419 void log_packed_rrset(enum verbosity_value v, const char* str,
    420 	struct ub_packed_rrset_key* rrset);
    421 
    422 /**
    423  * Allocate rrset in region - no more locks needed
    424  * @param key: a (just from rrset cache looked up) rrset key + valid,
    425  * 	packed data record.
    426  * @param region: where to alloc the copy
    427  * @param now: adjust the TTLs to be relative (subtract from all TTLs).
    428  * @return new region-alloced rrset key or NULL on alloc failure.
    429  */
    430 struct ub_packed_rrset_key* packed_rrset_copy_region(
    431 	struct ub_packed_rrset_key* key, struct regional* region,
    432 	time_t now);
    433 
    434 /**
    435  * Allocate rrset with malloc (from region or you are holding the lock).
    436  * @param key: key with data entry.
    437  * @param alloc: alloc_cache to create rrset_keys
    438  * @param now: adjust the TTLs to be absolute (add to all TTLs).
    439  * @return new region-alloced rrset key or NULL on alloc failure.
    440  */
    441 struct ub_packed_rrset_key* packed_rrset_copy_alloc(
    442 	struct ub_packed_rrset_key* key, struct alloc_cache* alloc,
    443 	time_t now);
    444 
    445 #endif /* UTIL_DATA_PACKED_RRSET_H */
    446