PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 2ms

/contrib/bind9/lib/dns/rbtdb.c

https://bitbucket.org/freebsd/freebsd-head/
C | 9332 lines | 6603 code | 1036 blank | 1693 comment | 2043 complexity | 246c4f82217f43e68410e305432312b4 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, LGPL-2.0, LGPL-2.1, BSD-2-Clause, 0BSD, JSON, AGPL-1.0, GPL-2.0
  1. /*
  2. * Copyright (C) 2004-2012 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 1999-2003 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. * PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* $Id$ */
  18. /*! \file */
  19. /*
  20. * Principal Author: Bob Halley
  21. */
  22. #include <config.h>
  23. /* #define inline */
  24. #include <isc/event.h>
  25. #include <isc/heap.h>
  26. #include <isc/mem.h>
  27. #include <isc/mutex.h>
  28. #include <isc/platform.h>
  29. #include <isc/print.h>
  30. #include <isc/random.h>
  31. #include <isc/refcount.h>
  32. #include <isc/rwlock.h>
  33. #include <isc/serial.h>
  34. #include <isc/string.h>
  35. #include <isc/task.h>
  36. #include <isc/time.h>
  37. #include <isc/util.h>
  38. #include <dns/acache.h>
  39. #include <dns/db.h>
  40. #include <dns/dbiterator.h>
  41. #include <dns/events.h>
  42. #include <dns/fixedname.h>
  43. #include <dns/lib.h>
  44. #include <dns/log.h>
  45. #include <dns/masterdump.h>
  46. #include <dns/nsec.h>
  47. #include <dns/nsec3.h>
  48. #include <dns/rbt.h>
  49. #include <dns/rpz.h>
  50. #include <dns/rdata.h>
  51. #include <dns/rdataset.h>
  52. #include <dns/rdatasetiter.h>
  53. #include <dns/rdataslab.h>
  54. #include <dns/rdatastruct.h>
  55. #include <dns/result.h>
  56. #include <dns/stats.h>
  57. #include <dns/view.h>
  58. #include <dns/zone.h>
  59. #include <dns/zonekey.h>
  60. #ifdef DNS_RBTDB_VERSION64
  61. #include "rbtdb64.h"
  62. #else
  63. #include "rbtdb.h"
  64. #endif
  65. #ifdef DNS_RBTDB_VERSION64
  66. #define RBTDB_MAGIC ISC_MAGIC('R', 'B', 'D', '8')
  67. #else
  68. #define RBTDB_MAGIC ISC_MAGIC('R', 'B', 'D', '4')
  69. #endif
  70. /*%
  71. * Note that "impmagic" is not the first four bytes of the struct, so
  72. * ISC_MAGIC_VALID cannot be used.
  73. */
  74. #define VALID_RBTDB(rbtdb) ((rbtdb) != NULL && \
  75. (rbtdb)->common.impmagic == RBTDB_MAGIC)
  76. #ifdef DNS_RBTDB_VERSION64
  77. typedef isc_uint64_t rbtdb_serial_t;
  78. /*%
  79. * Make casting easier in symbolic debuggers by using different names
  80. * for the 64 bit version.
  81. */
  82. #define dns_rbtdb_t dns_rbtdb64_t
  83. #define rdatasetheader_t rdatasetheader64_t
  84. #define rbtdb_version_t rbtdb_version64_t
  85. #else
  86. typedef isc_uint32_t rbtdb_serial_t;
  87. #endif
  88. typedef isc_uint32_t rbtdb_rdatatype_t;
  89. #define RBTDB_RDATATYPE_BASE(type) ((dns_rdatatype_t)((type) & 0xFFFF))
  90. #define RBTDB_RDATATYPE_EXT(type) ((dns_rdatatype_t)((type) >> 16))
  91. #define RBTDB_RDATATYPE_VALUE(b, e) ((rbtdb_rdatatype_t)((e) << 16) | (b))
  92. #define RBTDB_RDATATYPE_SIGNSEC \
  93. RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig, dns_rdatatype_nsec)
  94. #define RBTDB_RDATATYPE_SIGNSEC3 \
  95. RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig, dns_rdatatype_nsec3)
  96. #define RBTDB_RDATATYPE_SIGNS \
  97. RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig, dns_rdatatype_ns)
  98. #define RBTDB_RDATATYPE_SIGCNAME \
  99. RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig, dns_rdatatype_cname)
  100. #define RBTDB_RDATATYPE_SIGDNAME \
  101. RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig, dns_rdatatype_dname)
  102. #define RBTDB_RDATATYPE_NCACHEANY \
  103. RBTDB_RDATATYPE_VALUE(0, dns_rdatatype_any)
  104. /*
  105. * We use rwlock for DB lock only when ISC_RWLOCK_USEATOMIC is non 0.
  106. * Using rwlock is effective with regard to lookup performance only when
  107. * it is implemented in an efficient way.
  108. * Otherwise, it is generally wise to stick to the simple locking since rwlock
  109. * would require more memory or can even make lookups slower due to its own
  110. * overhead (when it internally calls mutex locks).
  111. */
  112. #ifdef ISC_RWLOCK_USEATOMIC
  113. #define DNS_RBTDB_USERWLOCK 1
  114. #else
  115. #define DNS_RBTDB_USERWLOCK 0
  116. #endif
  117. #if DNS_RBTDB_USERWLOCK
  118. #define RBTDB_INITLOCK(l) isc_rwlock_init((l), 0, 0)
  119. #define RBTDB_DESTROYLOCK(l) isc_rwlock_destroy(l)
  120. #define RBTDB_LOCK(l, t) RWLOCK((l), (t))
  121. #define RBTDB_UNLOCK(l, t) RWUNLOCK((l), (t))
  122. #else
  123. #define RBTDB_INITLOCK(l) isc_mutex_init(l)
  124. #define RBTDB_DESTROYLOCK(l) DESTROYLOCK(l)
  125. #define RBTDB_LOCK(l, t) LOCK(l)
  126. #define RBTDB_UNLOCK(l, t) UNLOCK(l)
  127. #endif
  128. /*
  129. * Since node locking is sensitive to both performance and memory footprint,
  130. * we need some trick here. If we have both high-performance rwlock and
  131. * high performance and small-memory reference counters, we use rwlock for
  132. * node lock and isc_refcount for node references. In this case, we don't have
  133. * to protect the access to the counters by locks.
  134. * Otherwise, we simply use ordinary mutex lock for node locking, and use
  135. * simple integers as reference counters which is protected by the lock.
  136. * In most cases, we can simply use wrapper macros such as NODE_LOCK and
  137. * NODE_UNLOCK. In some other cases, however, we need to protect reference
  138. * counters first and then protect other parts of a node as read-only data.
  139. * Special additional macros, NODE_STRONGLOCK(), NODE_WEAKLOCK(), etc, are also
  140. * provided for these special cases. When we can use the efficient backend
  141. * routines, we should only protect the "other members" by NODE_WEAKLOCK(read).
  142. * Otherwise, we should use NODE_STRONGLOCK() to protect the entire critical
  143. * section including the access to the reference counter.
  144. * Note that we cannot use NODE_LOCK()/NODE_UNLOCK() wherever the protected
  145. * section is also protected by NODE_STRONGLOCK().
  146. */
  147. #if defined(ISC_RWLOCK_USEATOMIC) && defined(DNS_RBT_USEISCREFCOUNT)
  148. typedef isc_rwlock_t nodelock_t;
  149. #define NODE_INITLOCK(l) isc_rwlock_init((l), 0, 0)
  150. #define NODE_DESTROYLOCK(l) isc_rwlock_destroy(l)
  151. #define NODE_LOCK(l, t) RWLOCK((l), (t))
  152. #define NODE_UNLOCK(l, t) RWUNLOCK((l), (t))
  153. #define NODE_TRYUPGRADE(l) isc_rwlock_tryupgrade(l)
  154. #define NODE_STRONGLOCK(l) ((void)0)
  155. #define NODE_STRONGUNLOCK(l) ((void)0)
  156. #define NODE_WEAKLOCK(l, t) NODE_LOCK(l, t)
  157. #define NODE_WEAKUNLOCK(l, t) NODE_UNLOCK(l, t)
  158. #define NODE_WEAKDOWNGRADE(l) isc_rwlock_downgrade(l)
  159. #else
  160. typedef isc_mutex_t nodelock_t;
  161. #define NODE_INITLOCK(l) isc_mutex_init(l)
  162. #define NODE_DESTROYLOCK(l) DESTROYLOCK(l)
  163. #define NODE_LOCK(l, t) LOCK(l)
  164. #define NODE_UNLOCK(l, t) UNLOCK(l)
  165. #define NODE_TRYUPGRADE(l) ISC_R_SUCCESS
  166. #define NODE_STRONGLOCK(l) LOCK(l)
  167. #define NODE_STRONGUNLOCK(l) UNLOCK(l)
  168. #define NODE_WEAKLOCK(l, t) ((void)0)
  169. #define NODE_WEAKUNLOCK(l, t) ((void)0)
  170. #define NODE_WEAKDOWNGRADE(l) ((void)0)
  171. #endif
  172. /*%
  173. * Whether to rate-limit updating the LRU to avoid possible thread contention.
  174. * Our performance measurement has shown the cost is marginal, so it's defined
  175. * to be 0 by default either with or without threads.
  176. */
  177. #ifndef DNS_RBTDB_LIMITLRUUPDATE
  178. #define DNS_RBTDB_LIMITLRUUPDATE 0
  179. #endif
  180. /*
  181. * Allow clients with a virtual time of up to 5 minutes in the past to see
  182. * records that would have otherwise have expired.
  183. */
  184. #define RBTDB_VIRTUAL 300
  185. struct noqname {
  186. dns_name_t name;
  187. void * neg;
  188. void * negsig;
  189. dns_rdatatype_t type;
  190. };
  191. typedef struct acachectl acachectl_t;
  192. typedef struct rdatasetheader {
  193. /*%
  194. * Locked by the owning node's lock.
  195. */
  196. rbtdb_serial_t serial;
  197. dns_ttl_t rdh_ttl;
  198. rbtdb_rdatatype_t type;
  199. isc_uint16_t attributes;
  200. dns_trust_t trust;
  201. struct noqname *noqname;
  202. struct noqname *closest;
  203. /*%<
  204. * We don't use the LIST macros, because the LIST structure has
  205. * both head and tail pointers, and is doubly linked.
  206. */
  207. struct rdatasetheader *next;
  208. /*%<
  209. * If this is the top header for an rdataset, 'next' points
  210. * to the top header for the next rdataset (i.e., the next type).
  211. * Otherwise, it points up to the header whose down pointer points
  212. * at this header.
  213. */
  214. struct rdatasetheader *down;
  215. /*%<
  216. * Points to the header for the next older version of
  217. * this rdataset.
  218. */
  219. isc_uint32_t count;
  220. /*%<
  221. * Monotonously increased every time this rdataset is bound so that
  222. * it is used as the base of the starting point in DNS responses
  223. * when the "cyclic" rrset-order is required. Since the ordering
  224. * should not be so crucial, no lock is set for the counter for
  225. * performance reasons.
  226. */
  227. acachectl_t *additional_auth;
  228. acachectl_t *additional_glue;
  229. dns_rbtnode_t *node;
  230. isc_stdtime_t last_used;
  231. ISC_LINK(struct rdatasetheader) link;
  232. unsigned int heap_index;
  233. /*%<
  234. * Used for TTL-based cache cleaning.
  235. */
  236. isc_stdtime_t resign;
  237. } rdatasetheader_t;
  238. typedef ISC_LIST(rdatasetheader_t) rdatasetheaderlist_t;
  239. typedef ISC_LIST(dns_rbtnode_t) rbtnodelist_t;
  240. #define RDATASET_ATTR_NONEXISTENT 0x0001
  241. #define RDATASET_ATTR_STALE 0x0002
  242. #define RDATASET_ATTR_IGNORE 0x0004
  243. #define RDATASET_ATTR_RETAIN 0x0008
  244. #define RDATASET_ATTR_NXDOMAIN 0x0010
  245. #define RDATASET_ATTR_RESIGN 0x0020
  246. #define RDATASET_ATTR_STATCOUNT 0x0040
  247. #define RDATASET_ATTR_OPTOUT 0x0080
  248. #define RDATASET_ATTR_NEGATIVE 0x0100
  249. typedef struct acache_cbarg {
  250. dns_rdatasetadditional_t type;
  251. unsigned int count;
  252. dns_db_t *db;
  253. dns_dbnode_t *node;
  254. rdatasetheader_t *header;
  255. } acache_cbarg_t;
  256. struct acachectl {
  257. dns_acacheentry_t *entry;
  258. acache_cbarg_t *cbarg;
  259. };
  260. /*
  261. * XXX
  262. * When the cache will pre-expire data (due to memory low or other
  263. * situations) before the rdataset's TTL has expired, it MUST
  264. * respect the RETAIN bit and not expire the data until its TTL is
  265. * expired.
  266. */
  267. #undef IGNORE /* WIN32 winbase.h defines this. */
  268. #define EXISTS(header) \
  269. (((header)->attributes & RDATASET_ATTR_NONEXISTENT) == 0)
  270. #define NONEXISTENT(header) \
  271. (((header)->attributes & RDATASET_ATTR_NONEXISTENT) != 0)
  272. #define IGNORE(header) \
  273. (((header)->attributes & RDATASET_ATTR_IGNORE) != 0)
  274. #define RETAIN(header) \
  275. (((header)->attributes & RDATASET_ATTR_RETAIN) != 0)
  276. #define NXDOMAIN(header) \
  277. (((header)->attributes & RDATASET_ATTR_NXDOMAIN) != 0)
  278. #define RESIGN(header) \
  279. (((header)->attributes & RDATASET_ATTR_RESIGN) != 0)
  280. #define OPTOUT(header) \
  281. (((header)->attributes & RDATASET_ATTR_OPTOUT) != 0)
  282. #define NEGATIVE(header) \
  283. (((header)->attributes & RDATASET_ATTR_NEGATIVE) != 0)
  284. #define DEFAULT_NODE_LOCK_COUNT 7 /*%< Should be prime. */
  285. /*%
  286. * Number of buckets for cache DB entries (locks, LRU lists, TTL heaps).
  287. * There is a tradeoff issue about configuring this value: if this is too
  288. * small, it may cause heavier contention between threads; if this is too large,
  289. * LRU purge algorithm won't work well (entries tend to be purged prematurely).
  290. * The default value should work well for most environments, but this can
  291. * also be configurable at compilation time via the
  292. * DNS_RBTDB_CACHE_NODE_LOCK_COUNT variable. This value must be larger than
  293. * 1 due to the assumption of overmem_purge().
  294. */
  295. #ifdef DNS_RBTDB_CACHE_NODE_LOCK_COUNT
  296. #if DNS_RBTDB_CACHE_NODE_LOCK_COUNT <= 1
  297. #error "DNS_RBTDB_CACHE_NODE_LOCK_COUNT must be larger than 1"
  298. #else
  299. #define DEFAULT_CACHE_NODE_LOCK_COUNT DNS_RBTDB_CACHE_NODE_LOCK_COUNT
  300. #endif
  301. #else
  302. #define DEFAULT_CACHE_NODE_LOCK_COUNT 16
  303. #endif /* DNS_RBTDB_CACHE_NODE_LOCK_COUNT */
  304. typedef struct {
  305. nodelock_t lock;
  306. /* Protected in the refcount routines. */
  307. isc_refcount_t references;
  308. /* Locked by lock. */
  309. isc_boolean_t exiting;
  310. } rbtdb_nodelock_t;
  311. typedef struct rbtdb_changed {
  312. dns_rbtnode_t * node;
  313. isc_boolean_t dirty;
  314. ISC_LINK(struct rbtdb_changed) link;
  315. } rbtdb_changed_t;
  316. typedef ISC_LIST(rbtdb_changed_t) rbtdb_changedlist_t;
  317. typedef enum {
  318. dns_db_insecure,
  319. dns_db_partial,
  320. dns_db_secure
  321. } dns_db_secure_t;
  322. typedef struct dns_rbtdb dns_rbtdb_t;
  323. typedef struct rbtdb_version {
  324. /* Not locked */
  325. rbtdb_serial_t serial;
  326. dns_rbtdb_t * rbtdb;
  327. /*
  328. * Protected in the refcount routines.
  329. * XXXJT: should we change the lock policy based on the refcount
  330. * performance?
  331. */
  332. isc_refcount_t references;
  333. /* Locked by database lock. */
  334. isc_boolean_t writer;
  335. isc_boolean_t commit_ok;
  336. rbtdb_changedlist_t changed_list;
  337. rdatasetheaderlist_t resigned_list;
  338. ISC_LINK(struct rbtdb_version) link;
  339. dns_db_secure_t secure;
  340. isc_boolean_t havensec3;
  341. /* NSEC3 parameters */
  342. dns_hash_t hash;
  343. isc_uint8_t flags;
  344. isc_uint16_t iterations;
  345. isc_uint8_t salt_length;
  346. unsigned char salt[DNS_NSEC3_SALTSIZE];
  347. } rbtdb_version_t;
  348. typedef ISC_LIST(rbtdb_version_t) rbtdb_versionlist_t;
  349. struct dns_rbtdb {
  350. /* Unlocked. */
  351. dns_db_t common;
  352. /* Locks the data in this struct */
  353. #if DNS_RBTDB_USERWLOCK
  354. isc_rwlock_t lock;
  355. #else
  356. isc_mutex_t lock;
  357. #endif
  358. /* Locks the tree structure (prevents nodes appearing/disappearing) */
  359. isc_rwlock_t tree_lock;
  360. /* Locks for individual tree nodes */
  361. unsigned int node_lock_count;
  362. rbtdb_nodelock_t * node_locks;
  363. dns_rbtnode_t * origin_node;
  364. dns_stats_t * rrsetstats; /* cache DB only */
  365. /* Locked by lock. */
  366. unsigned int active;
  367. isc_refcount_t references;
  368. unsigned int attributes;
  369. rbtdb_serial_t current_serial;
  370. rbtdb_serial_t least_serial;
  371. rbtdb_serial_t next_serial;
  372. rbtdb_version_t * current_version;
  373. rbtdb_version_t * future_version;
  374. rbtdb_versionlist_t open_versions;
  375. isc_task_t * task;
  376. dns_dbnode_t *soanode;
  377. dns_dbnode_t *nsnode;
  378. /*
  379. * This is a linked list used to implement the LRU cache. There will
  380. * be node_lock_count linked lists here. Nodes in bucket 1 will be
  381. * placed on the linked list rdatasets[1].
  382. */
  383. rdatasetheaderlist_t *rdatasets;
  384. /*%
  385. * Temporary storage for stale cache nodes and dynamically deleted
  386. * nodes that await being cleaned up.
  387. */
  388. rbtnodelist_t *deadnodes;
  389. /*
  390. * Heaps. These are used for TTL based expiry in a cache,
  391. * or for zone resigning in a zone DB. hmctx is the memory
  392. * context to use for the heap (which differs from the main
  393. * database memory context in the case of a cache).
  394. */
  395. isc_mem_t * hmctx;
  396. isc_heap_t **heaps;
  397. /* Locked by tree_lock. */
  398. dns_rbt_t * tree;
  399. dns_rbt_t * nsec;
  400. dns_rbt_t * nsec3;
  401. dns_rpz_cidr_t * rpz_cidr;
  402. /* Unlocked */
  403. unsigned int quantum;
  404. };
  405. #define RBTDB_ATTR_LOADED 0x01
  406. #define RBTDB_ATTR_LOADING 0x02
  407. /*%
  408. * Search Context
  409. */
  410. typedef struct {
  411. dns_rbtdb_t * rbtdb;
  412. rbtdb_version_t * rbtversion;
  413. rbtdb_serial_t serial;
  414. unsigned int options;
  415. dns_rbtnodechain_t chain;
  416. isc_boolean_t copy_name;
  417. isc_boolean_t need_cleanup;
  418. isc_boolean_t wild;
  419. dns_rbtnode_t * zonecut;
  420. rdatasetheader_t * zonecut_rdataset;
  421. rdatasetheader_t * zonecut_sigrdataset;
  422. dns_fixedname_t zonecut_name;
  423. isc_stdtime_t now;
  424. } rbtdb_search_t;
  425. /*%
  426. * Load Context
  427. */
  428. typedef struct {
  429. dns_rbtdb_t * rbtdb;
  430. isc_stdtime_t now;
  431. } rbtdb_load_t;
  432. static void rdataset_disassociate(dns_rdataset_t *rdataset);
  433. static isc_result_t rdataset_first(dns_rdataset_t *rdataset);
  434. static isc_result_t rdataset_next(dns_rdataset_t *rdataset);
  435. static void rdataset_current(dns_rdataset_t *rdataset, dns_rdata_t *rdata);
  436. static void rdataset_clone(dns_rdataset_t *source, dns_rdataset_t *target);
  437. static unsigned int rdataset_count(dns_rdataset_t *rdataset);
  438. static isc_result_t rdataset_getnoqname(dns_rdataset_t *rdataset,
  439. dns_name_t *name,
  440. dns_rdataset_t *neg,
  441. dns_rdataset_t *negsig);
  442. static isc_result_t rdataset_getclosest(dns_rdataset_t *rdataset,
  443. dns_name_t *name,
  444. dns_rdataset_t *neg,
  445. dns_rdataset_t *negsig);
  446. static isc_result_t rdataset_getadditional(dns_rdataset_t *rdataset,
  447. dns_rdatasetadditional_t type,
  448. dns_rdatatype_t qtype,
  449. dns_acache_t *acache,
  450. dns_zone_t **zonep,
  451. dns_db_t **dbp,
  452. dns_dbversion_t **versionp,
  453. dns_dbnode_t **nodep,
  454. dns_name_t *fname,
  455. dns_message_t *msg,
  456. isc_stdtime_t now);
  457. static isc_result_t rdataset_setadditional(dns_rdataset_t *rdataset,
  458. dns_rdatasetadditional_t type,
  459. dns_rdatatype_t qtype,
  460. dns_acache_t *acache,
  461. dns_zone_t *zone,
  462. dns_db_t *db,
  463. dns_dbversion_t *version,
  464. dns_dbnode_t *node,
  465. dns_name_t *fname);
  466. static isc_result_t rdataset_putadditional(dns_acache_t *acache,
  467. dns_rdataset_t *rdataset,
  468. dns_rdatasetadditional_t type,
  469. dns_rdatatype_t qtype);
  470. static inline isc_boolean_t need_headerupdate(rdatasetheader_t *header,
  471. isc_stdtime_t now);
  472. static void update_header(dns_rbtdb_t *rbtdb, rdatasetheader_t *header,
  473. isc_stdtime_t now);
  474. static void expire_header(dns_rbtdb_t *rbtdb, rdatasetheader_t *header,
  475. isc_boolean_t tree_locked);
  476. static void overmem_purge(dns_rbtdb_t *rbtdb, unsigned int locknum_start,
  477. isc_stdtime_t now, isc_boolean_t tree_locked);
  478. static isc_result_t resign_insert(dns_rbtdb_t *rbtdb, int idx,
  479. rdatasetheader_t *newheader);
  480. static void prune_tree(isc_task_t *task, isc_event_t *event);
  481. static void rdataset_settrust(dns_rdataset_t *rdataset, dns_trust_t trust);
  482. static void rdataset_expire(dns_rdataset_t *rdataset);
  483. static dns_rdatasetmethods_t rdataset_methods = {
  484. rdataset_disassociate,
  485. rdataset_first,
  486. rdataset_next,
  487. rdataset_current,
  488. rdataset_clone,
  489. rdataset_count,
  490. NULL,
  491. rdataset_getnoqname,
  492. NULL,
  493. rdataset_getclosest,
  494. rdataset_getadditional,
  495. rdataset_setadditional,
  496. rdataset_putadditional,
  497. rdataset_settrust,
  498. rdataset_expire
  499. };
  500. static void rdatasetiter_destroy(dns_rdatasetiter_t **iteratorp);
  501. static isc_result_t rdatasetiter_first(dns_rdatasetiter_t *iterator);
  502. static isc_result_t rdatasetiter_next(dns_rdatasetiter_t *iterator);
  503. static void rdatasetiter_current(dns_rdatasetiter_t *iterator,
  504. dns_rdataset_t *rdataset);
  505. static dns_rdatasetitermethods_t rdatasetiter_methods = {
  506. rdatasetiter_destroy,
  507. rdatasetiter_first,
  508. rdatasetiter_next,
  509. rdatasetiter_current
  510. };
  511. typedef struct rbtdb_rdatasetiter {
  512. dns_rdatasetiter_t common;
  513. rdatasetheader_t * current;
  514. } rbtdb_rdatasetiter_t;
  515. static void dbiterator_destroy(dns_dbiterator_t **iteratorp);
  516. static isc_result_t dbiterator_first(dns_dbiterator_t *iterator);
  517. static isc_result_t dbiterator_last(dns_dbiterator_t *iterator);
  518. static isc_result_t dbiterator_seek(dns_dbiterator_t *iterator,
  519. dns_name_t *name);
  520. static isc_result_t dbiterator_prev(dns_dbiterator_t *iterator);
  521. static isc_result_t dbiterator_next(dns_dbiterator_t *iterator);
  522. static isc_result_t dbiterator_current(dns_dbiterator_t *iterator,
  523. dns_dbnode_t **nodep,
  524. dns_name_t *name);
  525. static isc_result_t dbiterator_pause(dns_dbiterator_t *iterator);
  526. static isc_result_t dbiterator_origin(dns_dbiterator_t *iterator,
  527. dns_name_t *name);
  528. static dns_dbiteratormethods_t dbiterator_methods = {
  529. dbiterator_destroy,
  530. dbiterator_first,
  531. dbiterator_last,
  532. dbiterator_seek,
  533. dbiterator_prev,
  534. dbiterator_next,
  535. dbiterator_current,
  536. dbiterator_pause,
  537. dbiterator_origin
  538. };
  539. #define DELETION_BATCH_MAX 64
  540. /*
  541. * If 'paused' is ISC_TRUE, then the tree lock is not being held.
  542. */
  543. typedef struct rbtdb_dbiterator {
  544. dns_dbiterator_t common;
  545. isc_boolean_t paused;
  546. isc_boolean_t new_origin;
  547. isc_rwlocktype_t tree_locked;
  548. isc_result_t result;
  549. dns_fixedname_t name;
  550. dns_fixedname_t origin;
  551. dns_rbtnodechain_t chain;
  552. dns_rbtnodechain_t nsec3chain;
  553. dns_rbtnodechain_t *current;
  554. dns_rbtnode_t *node;
  555. dns_rbtnode_t *deletions[DELETION_BATCH_MAX];
  556. int delete;
  557. isc_boolean_t nsec3only;
  558. isc_boolean_t nonsec3;
  559. } rbtdb_dbiterator_t;
  560. #define IS_STUB(rbtdb) (((rbtdb)->common.attributes & DNS_DBATTR_STUB) != 0)
  561. #define IS_CACHE(rbtdb) (((rbtdb)->common.attributes & DNS_DBATTR_CACHE) != 0)
  562. static void free_rbtdb(dns_rbtdb_t *rbtdb, isc_boolean_t log,
  563. isc_event_t *event);
  564. static void overmem(dns_db_t *db, isc_boolean_t overmem);
  565. #ifdef BIND9
  566. static void setnsec3parameters(dns_db_t *db, rbtdb_version_t *version);
  567. #endif
  568. /*%
  569. * 'init_count' is used to initialize 'newheader->count' which inturn
  570. * is used to determine where in the cycle rrset-order cyclic starts.
  571. * We don't lock this as we don't care about simultaneous updates.
  572. *
  573. * Note:
  574. * Both init_count and header->count can be ISC_UINT32_MAX.
  575. * The count on the returned rdataset however can't be as
  576. * that indicates that the database does not implement cyclic
  577. * processing.
  578. */
  579. static unsigned int init_count;
  580. /*
  581. * Locking
  582. *
  583. * If a routine is going to lock more than one lock in this module, then
  584. * the locking must be done in the following order:
  585. *
  586. * Tree Lock
  587. *
  588. * Node Lock (Only one from the set may be locked at one time by
  589. * any caller)
  590. *
  591. * Database Lock
  592. *
  593. * Failure to follow this hierarchy can result in deadlock.
  594. */
  595. /*
  596. * Deleting Nodes
  597. *
  598. * For zone databases the node for the origin of the zone MUST NOT be deleted.
  599. */
  600. /*
  601. * DB Routines
  602. */
  603. static void
  604. attach(dns_db_t *source, dns_db_t **targetp) {
  605. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)source;
  606. REQUIRE(VALID_RBTDB(rbtdb));
  607. isc_refcount_increment(&rbtdb->references, NULL);
  608. *targetp = source;
  609. }
  610. static void
  611. free_rbtdb_callback(isc_task_t *task, isc_event_t *event) {
  612. dns_rbtdb_t *rbtdb = event->ev_arg;
  613. UNUSED(task);
  614. free_rbtdb(rbtdb, ISC_TRUE, event);
  615. }
  616. static void
  617. update_rrsetstats(dns_rbtdb_t *rbtdb, rdatasetheader_t *header,
  618. isc_boolean_t increment)
  619. {
  620. dns_rdatastatstype_t statattributes = 0;
  621. dns_rdatastatstype_t base = 0;
  622. dns_rdatastatstype_t type;
  623. /* At the moment we count statistics only for cache DB */
  624. INSIST(IS_CACHE(rbtdb));
  625. if (NEGATIVE(header)) {
  626. if (NXDOMAIN(header))
  627. statattributes = DNS_RDATASTATSTYPE_ATTR_NXDOMAIN;
  628. else {
  629. statattributes = DNS_RDATASTATSTYPE_ATTR_NXRRSET;
  630. base = RBTDB_RDATATYPE_EXT(header->type);
  631. }
  632. } else
  633. base = RBTDB_RDATATYPE_BASE(header->type);
  634. type = DNS_RDATASTATSTYPE_VALUE(base, statattributes);
  635. if (increment)
  636. dns_rdatasetstats_increment(rbtdb->rrsetstats, type);
  637. else
  638. dns_rdatasetstats_decrement(rbtdb->rrsetstats, type);
  639. }
  640. static void
  641. set_ttl(dns_rbtdb_t *rbtdb, rdatasetheader_t *header, dns_ttl_t newttl) {
  642. int idx;
  643. isc_heap_t *heap;
  644. dns_ttl_t oldttl;
  645. oldttl = header->rdh_ttl;
  646. header->rdh_ttl = newttl;
  647. if (!IS_CACHE(rbtdb))
  648. return;
  649. /*
  650. * It's possible the rbtdb is not a cache. If this is the case,
  651. * we will not have a heap, and we move on. If we do, though,
  652. * we might need to adjust things.
  653. */
  654. if (header->heap_index == 0 || newttl == oldttl)
  655. return;
  656. idx = header->node->locknum;
  657. if (rbtdb->heaps == NULL || rbtdb->heaps[idx] == NULL)
  658. return;
  659. heap = rbtdb->heaps[idx];
  660. if (newttl < oldttl)
  661. isc_heap_increased(heap, header->heap_index);
  662. else
  663. isc_heap_decreased(heap, header->heap_index);
  664. }
  665. /*%
  666. * These functions allow the heap code to rank the priority of each
  667. * element. It returns ISC_TRUE if v1 happens "sooner" than v2.
  668. */
  669. static isc_boolean_t
  670. ttl_sooner(void *v1, void *v2) {
  671. rdatasetheader_t *h1 = v1;
  672. rdatasetheader_t *h2 = v2;
  673. if (h1->rdh_ttl < h2->rdh_ttl)
  674. return (ISC_TRUE);
  675. return (ISC_FALSE);
  676. }
  677. static isc_boolean_t
  678. resign_sooner(void *v1, void *v2) {
  679. rdatasetheader_t *h1 = v1;
  680. rdatasetheader_t *h2 = v2;
  681. if (h1->resign < h2->resign)
  682. return (ISC_TRUE);
  683. return (ISC_FALSE);
  684. }
  685. /*%
  686. * This function sets the heap index into the header.
  687. */
  688. static void
  689. set_index(void *what, unsigned int index) {
  690. rdatasetheader_t *h = what;
  691. h->heap_index = index;
  692. }
  693. /*%
  694. * Work out how many nodes can be deleted in the time between two
  695. * requests to the nameserver. Smooth the resulting number and use it
  696. * as a estimate for the number of nodes to be deleted in the next
  697. * iteration.
  698. */
  699. static unsigned int
  700. adjust_quantum(unsigned int old, isc_time_t *start) {
  701. unsigned int pps = dns_pps; /* packets per second */
  702. unsigned int interval;
  703. isc_uint64_t usecs;
  704. isc_time_t end;
  705. unsigned int new;
  706. if (pps < 100)
  707. pps = 100;
  708. isc_time_now(&end);
  709. interval = 1000000 / pps; /* interval in usec */
  710. if (interval == 0)
  711. interval = 1;
  712. usecs = isc_time_microdiff(&end, start);
  713. if (usecs == 0) {
  714. /*
  715. * We were unable to measure the amount of time taken.
  716. * Double the nodes deleted next time.
  717. */
  718. old *= 2;
  719. if (old > 1000)
  720. old = 1000;
  721. return (old);
  722. }
  723. new = old * interval;
  724. new /= (unsigned int)usecs;
  725. if (new == 0)
  726. new = 1;
  727. else if (new > 1000)
  728. new = 1000;
  729. /* Smooth */
  730. new = (new + old * 3) / 4;
  731. isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_CACHE,
  732. ISC_LOG_DEBUG(1), "adjust_quantum -> %d", new);
  733. return (new);
  734. }
  735. static void
  736. free_rbtdb(dns_rbtdb_t *rbtdb, isc_boolean_t log, isc_event_t *event) {
  737. unsigned int i;
  738. isc_ondestroy_t ondest;
  739. isc_result_t result;
  740. char buf[DNS_NAME_FORMATSIZE];
  741. dns_rbt_t **treep;
  742. isc_time_t start;
  743. if (IS_CACHE(rbtdb) && rbtdb->common.rdclass == dns_rdataclass_in)
  744. overmem((dns_db_t *)rbtdb, (isc_boolean_t)-1);
  745. REQUIRE(rbtdb->current_version != NULL || EMPTY(rbtdb->open_versions));
  746. REQUIRE(rbtdb->future_version == NULL);
  747. if (rbtdb->current_version != NULL) {
  748. unsigned int refs;
  749. isc_refcount_decrement(&rbtdb->current_version->references,
  750. &refs);
  751. INSIST(refs == 0);
  752. UNLINK(rbtdb->open_versions, rbtdb->current_version, link);
  753. isc_refcount_destroy(&rbtdb->current_version->references);
  754. isc_mem_put(rbtdb->common.mctx, rbtdb->current_version,
  755. sizeof(rbtdb_version_t));
  756. }
  757. /*
  758. * We assume the number of remaining dead nodes is reasonably small;
  759. * the overhead of unlinking all nodes here should be negligible.
  760. */
  761. for (i = 0; i < rbtdb->node_lock_count; i++) {
  762. dns_rbtnode_t *node;
  763. node = ISC_LIST_HEAD(rbtdb->deadnodes[i]);
  764. while (node != NULL) {
  765. ISC_LIST_UNLINK(rbtdb->deadnodes[i], node, deadlink);
  766. node = ISC_LIST_HEAD(rbtdb->deadnodes[i]);
  767. }
  768. }
  769. if (event == NULL)
  770. rbtdb->quantum = (rbtdb->task != NULL) ? 100 : 0;
  771. for (;;) {
  772. /*
  773. * pick the next tree to (start to) destroy
  774. */
  775. treep = &rbtdb->tree;
  776. if (*treep == NULL) {
  777. treep = &rbtdb->nsec;
  778. if (*treep == NULL) {
  779. treep = &rbtdb->nsec3;
  780. /*
  781. * we're finished after clear cutting
  782. */
  783. if (*treep == NULL)
  784. break;
  785. }
  786. }
  787. isc_time_now(&start);
  788. result = dns_rbt_destroy2(treep, rbtdb->quantum);
  789. if (result == ISC_R_QUOTA) {
  790. INSIST(rbtdb->task != NULL);
  791. if (rbtdb->quantum != 0)
  792. rbtdb->quantum = adjust_quantum(rbtdb->quantum,
  793. &start);
  794. if (event == NULL)
  795. event = isc_event_allocate(rbtdb->common.mctx,
  796. NULL,
  797. DNS_EVENT_FREESTORAGE,
  798. free_rbtdb_callback,
  799. rbtdb,
  800. sizeof(isc_event_t));
  801. if (event == NULL)
  802. continue;
  803. isc_task_send(rbtdb->task, &event);
  804. return;
  805. }
  806. INSIST(result == ISC_R_SUCCESS && *treep == NULL);
  807. }
  808. if (event != NULL)
  809. isc_event_free(&event);
  810. if (log) {
  811. if (dns_name_dynamic(&rbtdb->common.origin))
  812. dns_name_format(&rbtdb->common.origin, buf,
  813. sizeof(buf));
  814. else
  815. strcpy(buf, "<UNKNOWN>");
  816. isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
  817. DNS_LOGMODULE_CACHE, ISC_LOG_DEBUG(1),
  818. "done free_rbtdb(%s)", buf);
  819. }
  820. if (dns_name_dynamic(&rbtdb->common.origin))
  821. dns_name_free(&rbtdb->common.origin, rbtdb->common.mctx);
  822. for (i = 0; i < rbtdb->node_lock_count; i++) {
  823. isc_refcount_destroy(&rbtdb->node_locks[i].references);
  824. NODE_DESTROYLOCK(&rbtdb->node_locks[i].lock);
  825. }
  826. /*
  827. * Clean up LRU / re-signing order lists.
  828. */
  829. if (rbtdb->rdatasets != NULL) {
  830. for (i = 0; i < rbtdb->node_lock_count; i++)
  831. INSIST(ISC_LIST_EMPTY(rbtdb->rdatasets[i]));
  832. isc_mem_put(rbtdb->common.mctx, rbtdb->rdatasets,
  833. rbtdb->node_lock_count *
  834. sizeof(rdatasetheaderlist_t));
  835. }
  836. /*
  837. * Clean up dead node buckets.
  838. */
  839. if (rbtdb->deadnodes != NULL) {
  840. for (i = 0; i < rbtdb->node_lock_count; i++)
  841. INSIST(ISC_LIST_EMPTY(rbtdb->deadnodes[i]));
  842. isc_mem_put(rbtdb->common.mctx, rbtdb->deadnodes,
  843. rbtdb->node_lock_count * sizeof(rbtnodelist_t));
  844. }
  845. /*
  846. * Clean up heap objects.
  847. */
  848. if (rbtdb->heaps != NULL) {
  849. for (i = 0; i < rbtdb->node_lock_count; i++)
  850. isc_heap_destroy(&rbtdb->heaps[i]);
  851. isc_mem_put(rbtdb->hmctx, rbtdb->heaps,
  852. rbtdb->node_lock_count * sizeof(isc_heap_t *));
  853. }
  854. if (rbtdb->rrsetstats != NULL)
  855. dns_stats_detach(&rbtdb->rrsetstats);
  856. #ifdef BIND9
  857. if (rbtdb->rpz_cidr != NULL)
  858. dns_rpz_cidr_free(&rbtdb->rpz_cidr);
  859. #endif
  860. isc_mem_put(rbtdb->common.mctx, rbtdb->node_locks,
  861. rbtdb->node_lock_count * sizeof(rbtdb_nodelock_t));
  862. isc_rwlock_destroy(&rbtdb->tree_lock);
  863. isc_refcount_destroy(&rbtdb->references);
  864. if (rbtdb->task != NULL)
  865. isc_task_detach(&rbtdb->task);
  866. RBTDB_DESTROYLOCK(&rbtdb->lock);
  867. rbtdb->common.magic = 0;
  868. rbtdb->common.impmagic = 0;
  869. ondest = rbtdb->common.ondest;
  870. isc_mem_detach(&rbtdb->hmctx);
  871. isc_mem_putanddetach(&rbtdb->common.mctx, rbtdb, sizeof(*rbtdb));
  872. isc_ondestroy_notify(&ondest, rbtdb);
  873. }
  874. static inline void
  875. maybe_free_rbtdb(dns_rbtdb_t *rbtdb) {
  876. isc_boolean_t want_free = ISC_FALSE;
  877. unsigned int i;
  878. unsigned int inactive = 0;
  879. /* XXX check for open versions here */
  880. if (rbtdb->soanode != NULL)
  881. dns_db_detachnode((dns_db_t *)rbtdb, &rbtdb->soanode);
  882. if (rbtdb->nsnode != NULL)
  883. dns_db_detachnode((dns_db_t *)rbtdb, &rbtdb->nsnode);
  884. /*
  885. * Even though there are no external direct references, there still
  886. * may be nodes in use.
  887. */
  888. for (i = 0; i < rbtdb->node_lock_count; i++) {
  889. NODE_LOCK(&rbtdb->node_locks[i].lock, isc_rwlocktype_write);
  890. rbtdb->node_locks[i].exiting = ISC_TRUE;
  891. NODE_UNLOCK(&rbtdb->node_locks[i].lock, isc_rwlocktype_write);
  892. if (isc_refcount_current(&rbtdb->node_locks[i].references)
  893. == 0) {
  894. inactive++;
  895. }
  896. }
  897. if (inactive != 0) {
  898. RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_write);
  899. rbtdb->active -= inactive;
  900. if (rbtdb->active == 0)
  901. want_free = ISC_TRUE;
  902. RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_write);
  903. if (want_free) {
  904. char buf[DNS_NAME_FORMATSIZE];
  905. if (dns_name_dynamic(&rbtdb->common.origin))
  906. dns_name_format(&rbtdb->common.origin, buf,
  907. sizeof(buf));
  908. else
  909. strcpy(buf, "<UNKNOWN>");
  910. isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
  911. DNS_LOGMODULE_CACHE, ISC_LOG_DEBUG(1),
  912. "calling free_rbtdb(%s)", buf);
  913. free_rbtdb(rbtdb, ISC_TRUE, NULL);
  914. }
  915. }
  916. }
  917. static void
  918. detach(dns_db_t **dbp) {
  919. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)(*dbp);
  920. unsigned int refs;
  921. REQUIRE(VALID_RBTDB(rbtdb));
  922. isc_refcount_decrement(&rbtdb->references, &refs);
  923. if (refs == 0)
  924. maybe_free_rbtdb(rbtdb);
  925. *dbp = NULL;
  926. }
  927. static void
  928. currentversion(dns_db_t *db, dns_dbversion_t **versionp) {
  929. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
  930. rbtdb_version_t *version;
  931. unsigned int refs;
  932. REQUIRE(VALID_RBTDB(rbtdb));
  933. RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_read);
  934. version = rbtdb->current_version;
  935. isc_refcount_increment(&version->references, &refs);
  936. RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_read);
  937. *versionp = (dns_dbversion_t *)version;
  938. }
  939. static inline rbtdb_version_t *
  940. allocate_version(isc_mem_t *mctx, rbtdb_serial_t serial,
  941. unsigned int references, isc_boolean_t writer)
  942. {
  943. isc_result_t result;
  944. rbtdb_version_t *version;
  945. version = isc_mem_get(mctx, sizeof(*version));
  946. if (version == NULL)
  947. return (NULL);
  948. version->serial = serial;
  949. result = isc_refcount_init(&version->references, references);
  950. if (result != ISC_R_SUCCESS) {
  951. isc_mem_put(mctx, version, sizeof(*version));
  952. return (NULL);
  953. }
  954. version->writer = writer;
  955. version->commit_ok = ISC_FALSE;
  956. ISC_LIST_INIT(version->changed_list);
  957. ISC_LIST_INIT(version->resigned_list);
  958. ISC_LINK_INIT(version, link);
  959. return (version);
  960. }
  961. static isc_result_t
  962. newversion(dns_db_t *db, dns_dbversion_t **versionp) {
  963. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
  964. rbtdb_version_t *version;
  965. REQUIRE(VALID_RBTDB(rbtdb));
  966. REQUIRE(versionp != NULL && *versionp == NULL);
  967. REQUIRE(rbtdb->future_version == NULL);
  968. RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_write);
  969. RUNTIME_CHECK(rbtdb->next_serial != 0); /* XXX Error? */
  970. version = allocate_version(rbtdb->common.mctx, rbtdb->next_serial, 1,
  971. ISC_TRUE);
  972. if (version != NULL) {
  973. version->rbtdb = rbtdb;
  974. version->commit_ok = ISC_TRUE;
  975. version->secure = rbtdb->current_version->secure;
  976. version->havensec3 = rbtdb->current_version->havensec3;
  977. if (version->havensec3) {
  978. version->flags = rbtdb->current_version->flags;
  979. version->iterations =
  980. rbtdb->current_version->iterations;
  981. version->hash = rbtdb->current_version->hash;
  982. version->salt_length =
  983. rbtdb->current_version->salt_length;
  984. memcpy(version->salt, rbtdb->current_version->salt,
  985. version->salt_length);
  986. } else {
  987. version->flags = 0;
  988. version->iterations = 0;
  989. version->hash = 0;
  990. version->salt_length = 0;
  991. memset(version->salt, 0, sizeof(version->salt));
  992. }
  993. rbtdb->next_serial++;
  994. rbtdb->future_version = version;
  995. }
  996. RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_write);
  997. if (version == NULL)
  998. return (ISC_R_NOMEMORY);
  999. *versionp = version;
  1000. return (ISC_R_SUCCESS);
  1001. }
  1002. static void
  1003. attachversion(dns_db_t *db, dns_dbversion_t *source,
  1004. dns_dbversion_t **targetp)
  1005. {
  1006. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
  1007. rbtdb_version_t *rbtversion = source;
  1008. unsigned int refs;
  1009. REQUIRE(VALID_RBTDB(rbtdb));
  1010. INSIST(rbtversion != NULL && rbtversion->rbtdb == rbtdb);
  1011. isc_refcount_increment(&rbtversion->references, &refs);
  1012. INSIST(refs > 1);
  1013. *targetp = rbtversion;
  1014. }
  1015. static rbtdb_changed_t *
  1016. add_changed(dns_rbtdb_t *rbtdb, rbtdb_version_t *version,
  1017. dns_rbtnode_t *node)
  1018. {
  1019. rbtdb_changed_t *changed;
  1020. unsigned int refs;
  1021. /*
  1022. * Caller must be holding the node lock if its reference must be
  1023. * protected by the lock.
  1024. */
  1025. changed = isc_mem_get(rbtdb->common.mctx, sizeof(*changed));
  1026. RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_write);
  1027. REQUIRE(version->writer);
  1028. if (changed != NULL) {
  1029. dns_rbtnode_refincrement(node, &refs);
  1030. INSIST(refs != 0);
  1031. changed->node = node;
  1032. changed->dirty = ISC_FALSE;
  1033. ISC_LIST_INITANDAPPEND(version->changed_list, changed, link);
  1034. } else
  1035. version->commit_ok = ISC_FALSE;
  1036. RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_write);
  1037. return (changed);
  1038. }
  1039. static void
  1040. free_acachearray(isc_mem_t *mctx, rdatasetheader_t *header,
  1041. acachectl_t *array)
  1042. {
  1043. unsigned int count;
  1044. unsigned int i;
  1045. unsigned char *raw; /* RDATASLAB */
  1046. /*
  1047. * The caller must be holding the corresponding node lock.
  1048. */
  1049. if (array == NULL)
  1050. return;
  1051. raw = (unsigned char *)header + sizeof(*header);
  1052. count = raw[0] * 256 + raw[1];
  1053. /*
  1054. * Sanity check: since an additional cache entry has a reference to
  1055. * the original DB node (in the callback arg), there should be no
  1056. * acache entries when the node can be freed.
  1057. */
  1058. for (i = 0; i < count; i++)
  1059. INSIST(array[i].entry == NULL && array[i].cbarg == NULL);
  1060. isc_mem_put(mctx, array, count * sizeof(acachectl_t));
  1061. }
  1062. static inline void
  1063. free_noqname(isc_mem_t *mctx, struct noqname **noqname) {
  1064. if (dns_name_dynamic(&(*noqname)->name))
  1065. dns_name_free(&(*noqname)->name, mctx);
  1066. if ((*noqname)->neg != NULL)
  1067. isc_mem_put(mctx, (*noqname)->neg,
  1068. dns_rdataslab_size((*noqname)->neg, 0));
  1069. if ((*noqname)->negsig != NULL)
  1070. isc_mem_put(mctx, (*noqname)->negsig,
  1071. dns_rdataslab_size((*noqname)->negsig, 0));
  1072. isc_mem_put(mctx, *noqname, sizeof(**noqname));
  1073. *noqname = NULL;
  1074. }
  1075. static inline void
  1076. init_rdataset(dns_rbtdb_t *rbtdb, rdatasetheader_t *h)
  1077. {
  1078. ISC_LINK_INIT(h, link);
  1079. h->heap_index = 0;
  1080. #if TRACE_HEADER
  1081. if (IS_CACHE(rbtdb) && rbtdb->common.rdclass == dns_rdataclass_in)
  1082. fprintf(stderr, "initialized header: %p\n", h);
  1083. #else
  1084. UNUSED(rbtdb);
  1085. #endif
  1086. }
  1087. static inline rdatasetheader_t *
  1088. new_rdataset(dns_rbtdb_t *rbtdb, isc_mem_t *mctx)
  1089. {
  1090. rdatasetheader_t *h;
  1091. h = isc_mem_get(mctx, sizeof(*h));
  1092. if (h == NULL)
  1093. return (NULL);
  1094. #if TRACE_HEADER
  1095. if (IS_CACHE(rbtdb) && rbtdb->common.rdclass == dns_rdataclass_in)
  1096. fprintf(stderr, "allocated header: %p\n", h);
  1097. #endif
  1098. init_rdataset(rbtdb, h);
  1099. return (h);
  1100. }
  1101. static inline void
  1102. free_rdataset(dns_rbtdb_t *rbtdb, isc_mem_t *mctx, rdatasetheader_t *rdataset)
  1103. {
  1104. unsigned int size;
  1105. int idx;
  1106. if (EXISTS(rdataset) &&
  1107. (rdataset->attributes & RDATASET_ATTR_STATCOUNT) != 0) {
  1108. update_rrsetstats(rbtdb, rdataset, ISC_FALSE);
  1109. }
  1110. idx = rdataset->node->locknum;
  1111. if (ISC_LINK_LINKED(rdataset, link)) {
  1112. INSIST(IS_CACHE(rbtdb));
  1113. ISC_LIST_UNLINK(rbtdb->rdatasets[idx], rdataset, link);
  1114. }
  1115. if (rdataset->heap_index != 0)
  1116. isc_heap_delete(rbtdb->heaps[idx], rdataset->heap_index);
  1117. rdataset->heap_index = 0;
  1118. if (rdataset->noqname != NULL)
  1119. free_noqname(mctx, &rdataset->noqname);
  1120. if (rdataset->closest != NULL)
  1121. free_noqname(mctx, &rdataset->closest);
  1122. free_acachearray(mctx, rdataset, rdataset->additional_auth);
  1123. free_acachearray(mctx, rdataset, rdataset->additional_glue);
  1124. if ((rdataset->attributes & RDATASET_ATTR_NONEXISTENT) != 0)
  1125. size = sizeof(*rdataset);
  1126. else
  1127. size = dns_rdataslab_size((unsigned char *)rdataset,
  1128. sizeof(*rdataset));
  1129. isc_mem_put(mctx, rdataset, size);
  1130. }
  1131. static inline void
  1132. rollback_node(dns_rbtnode_t *node, rbtdb_serial_t serial) {
  1133. rdatasetheader_t *header, *dcurrent;
  1134. isc_boolean_t make_dirty = ISC_FALSE;
  1135. /*
  1136. * Caller must hold the node lock.
  1137. */
  1138. /*
  1139. * We set the IGNORE attribute on rdatasets with serial number
  1140. * 'serial'. When the reference count goes to zero, these rdatasets
  1141. * will be cleaned up; until that time, they will be ignored.
  1142. */
  1143. for (header = node->data; header != NULL; header = header->next) {
  1144. if (header->serial == serial) {
  1145. header->attributes |= RDATASET_ATTR_IGNORE;
  1146. make_dirty = ISC_TRUE;
  1147. }
  1148. for (dcurrent = header->down;
  1149. dcurrent != NULL;
  1150. dcurrent = dcurrent->down) {
  1151. if (dcurrent->serial == serial) {
  1152. dcurrent->attributes |= RDATASET_ATTR_IGNORE;
  1153. make_dirty = ISC_TRUE;
  1154. }
  1155. }
  1156. }
  1157. if (make_dirty)
  1158. node->dirty = 1;
  1159. }
  1160. static inline void
  1161. clean_stale_headers(dns_rbtdb_t *rbtdb, isc_mem_t *mctx, rdatasetheader_t *top)
  1162. {
  1163. rdatasetheader_t *d, *down_next;
  1164. for (d = top->down; d != NULL; d = down_next) {
  1165. down_next = d->down;
  1166. free_rdataset(rbtdb, mctx, d);
  1167. }
  1168. top->down = NULL;
  1169. }
  1170. static inline void
  1171. clean_cache_node(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node) {
  1172. rdatasetheader_t *current, *top_prev, *top_next;
  1173. isc_mem_t *mctx = rbtdb->common.mctx;
  1174. /*
  1175. * Caller must be holding the node lock.
  1176. */
  1177. top_prev = NULL;
  1178. for (current = node->data; current != NULL; current = top_next) {
  1179. top_next = current->next;
  1180. clean_stale_headers(rbtdb, mctx, current);
  1181. /*
  1182. * If current is nonexistent or stale, we can clean it up.
  1183. */
  1184. if ((current->attributes &
  1185. (RDATASET_ATTR_NONEXISTENT|RDATASET_ATTR_STALE)) != 0) {
  1186. if (top_prev != NULL)
  1187. top_prev->next = current->next;
  1188. else
  1189. node->data = current->next;
  1190. free_rdataset(rbtdb, mctx, current);
  1191. } else
  1192. top_prev = current;
  1193. }
  1194. node->dirty = 0;
  1195. }
  1196. static inline void
  1197. clean_zone_node(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node,
  1198. rbtdb_serial_t least_serial)
  1199. {
  1200. rdatasetheader_t *current, *dcurrent, *down_next, *dparent;
  1201. rdatasetheader_t *top_prev, *top_next;
  1202. isc_mem_t *mctx = rbtdb->common.mctx;
  1203. isc_boolean_t still_dirty = ISC_FALSE;
  1204. /*
  1205. * Caller must be holding the node lock.
  1206. */
  1207. REQUIRE(least_serial != 0);
  1208. top_prev = NULL;
  1209. for (current = node->data; current != NULL; current = top_next) {
  1210. top_next = current->next;
  1211. /*
  1212. * First, we clean up any instances of multiple rdatasets
  1213. * with the same serial number, or that have the IGNORE
  1214. * attribute.
  1215. */
  1216. dparent = current;
  1217. for (dcurrent = current->down;
  1218. dcurrent != NULL;
  1219. dcurrent = down_next) {
  1220. down_next = dcurrent->down;
  1221. INSIST(dcurrent->serial <= dparent->serial);
  1222. if (dcurrent->serial == dparent->serial ||
  1223. IGNORE(dcurrent)) {
  1224. if (down_next != NULL)
  1225. down_next->next = dparent;
  1226. dparent->down = down_next;
  1227. free_rdataset(rbtdb, mctx, dcurrent);
  1228. } else
  1229. dparent = dcurrent;
  1230. }
  1231. /*
  1232. * We've now eliminated all IGNORE datasets with the possible
  1233. * exception of current, which we now check.
  1234. */
  1235. if (IGNORE(current)) {
  1236. down_next = current->down;
  1237. if (down_next == NULL) {
  1238. if (top_prev != NULL)
  1239. top_prev->next = current->next;
  1240. else
  1241. node->data = current->next;
  1242. free_rdataset(rbtdb, mctx, current);
  1243. /*
  1244. * current no longer exists, so we can
  1245. * just continue with the loop.
  1246. */
  1247. continue;
  1248. } else {
  1249. /*
  1250. * Pull up current->down, making it the new
  1251. * current.
  1252. */
  1253. if (top_prev != NULL)
  1254. top_prev->next = down_next;
  1255. else
  1256. node->data = down_next;
  1257. down_next->next = top_next;
  1258. free_rdataset(rbtdb, mctx, current);
  1259. current = down_next;
  1260. }
  1261. }
  1262. /*
  1263. * We now try to find the first down node less than the
  1264. * least serial.
  1265. */
  1266. dparent = current;
  1267. for (dcurrent = current->down;
  1268. dcurrent != NULL;
  1269. dcurrent = down_next) {
  1270. down_next = dcurrent->down;
  1271. if (dcurrent->serial < least_serial)
  1272. break;
  1273. dparent = dcurrent;
  1274. }
  1275. /*
  1276. * If there is a such an rdataset, delete it and any older
  1277. * versions.
  1278. */
  1279. if (dcurrent != NULL) {
  1280. do {
  1281. down_next = dcurrent->down;
  1282. INSIST(dcurrent->serial <= least_serial);
  1283. free_rdataset(rbtdb, mctx, dcurrent);
  1284. dcurrent = down_next;
  1285. } while (dcurrent != NULL);
  1286. dparent->down = NULL;
  1287. }
  1288. /*
  1289. * Note. The serial number of 'current' might be less than
  1290. * least_serial too, but we cannot delete it because it is
  1291. * the most recent version, unless it is a NONEXISTENT
  1292. * rdataset.
  1293. */
  1294. if (current->down != NULL) {
  1295. still_dirty = ISC_TRUE;
  1296. top_prev = current;
  1297. } else {
  1298. /*
  1299. * If this is a NONEXISTENT rdataset, we can delete it.
  1300. */
  1301. if (NONEXISTENT(current)) {
  1302. if (top_prev != NULL)
  1303. top_prev->next = current->next;
  1304. else
  1305. node->data = current->next;
  1306. free_rdataset(rbtdb, mctx, current);
  1307. } else
  1308. top_prev = current;
  1309. }
  1310. }
  1311. if (!still_dirty)
  1312. node->dirty = 0;
  1313. }
  1314. static void
  1315. delete_node(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node)
  1316. {
  1317. dns_rbtnode_t *nsecnode;
  1318. dns_fixedname_t fname;
  1319. dns_name_t *name;
  1320. isc_result_t result = ISC_R_UNEXPECTED;
  1321. INSIST(!ISC_LINK_LINKED(node, deadlink));
  1322. switch (node->nsec) {
  1323. case DNS_RBT_NSEC_NORMAL:
  1324. #ifdef BIND9
  1325. if (rbtdb->rpz_cidr != NULL) {
  1326. dns_fixedname_init(&fname);
  1327. name = dns_fixedname_name(&fname);
  1328. dns_rbt_fullnamefromnode(node, name);
  1329. dns_rpz_cidr_deleteip(rbtdb->rpz_cidr, name);
  1330. }
  1331. #endif
  1332. result = dns_rbt_deletenode(rbtdb->tree, node, ISC_FALSE);
  1333. break;
  1334. case DNS_RBT_NSEC_HAS_NSEC:
  1335. dns_fixedname_init(&fname);
  1336. name = dns_fixedname_name(&fname);
  1337. dns_rbt_fullnamefromnode(node, name);
  1338. /*
  1339. * Delete the corresponding node from the auxiliary NSEC
  1340. * tree before deleting from the main tree.
  1341. */
  1342. nsecnode = NULL;
  1343. result = dns_rbt_findnode(rbtdb->nsec, name, NULL, &nsecnode,
  1344. NULL, DNS_RBTFIND_EMPTYDATA,
  1345. NULL, NULL);
  1346. if (result != ISC_R_SUCCESS) {
  1347. isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
  1348. DNS_LOGMODULE_CACHE, ISC_LOG_WARNING,
  1349. "delete_node: "
  1350. "dns_rbt_findnode(nsec): %s",
  1351. isc_result_totext(result));
  1352. } else {
  1353. result = dns_rbt_deletenode(rbtdb->nsec, nsecnode,
  1354. ISC_FALSE);
  1355. if (result != ISC_R_SUCCESS) {
  1356. isc_log_write(dns_lctx,
  1357. DNS_LOGCATEGORY_DATABASE,
  1358. DNS_LOGMODULE_CACHE,
  1359. ISC_LOG_WARNING,
  1360. "delete_nsecnode(): "
  1361. "dns_rbt_deletenode(nsecnode): %s",
  1362. isc_result_totext(result));
  1363. }
  1364. }
  1365. result = dns_rbt_deletenode(rbtdb->tree, node, ISC_FALSE);
  1366. #ifdef BIND9
  1367. dns_rpz_cidr_deleteip(rbtdb->rpz_cidr, name);
  1368. #endif
  1369. break;
  1370. case DNS_RBT_NSEC_NSEC:
  1371. result = dns_rbt_deletenode(rbtdb->nsec, node, ISC_FALSE);
  1372. break;
  1373. case DNS_RBT_NSEC_NSEC3:
  1374. result = dns_rbt_deletenode(rbtdb->nsec3, node, ISC_FALSE);
  1375. break;
  1376. }
  1377. if (result != ISC_R_SUCCESS) {
  1378. isc_log_write(dns_lctx,
  1379. DNS_LOGCATEGORY_DATABASE,
  1380. DNS_LOGMODULE_CACHE,
  1381. ISC_LOG_WARNING,
  1382. "delete_nsecnode(): "
  1383. "dns_rbt_deletenode: %s",
  1384. isc_result_totext(result));
  1385. }
  1386. }
  1387. /*%
  1388. * Clean up dead nodes. These are nodes which have no references, and
  1389. * have no data. They are dead but we could not or chose not to delete
  1390. * them when we deleted all the data at that node because we did not want
  1391. * to wait for the tree write lock.
  1392. *
  1393. * The caller must hold a tree write lock and bucketnum'th node (write) lock.
  1394. */
  1395. static void
  1396. cleanup_dead_nodes(dns_rbtdb_t *rbtdb, int bucketnum) {
  1397. dns_rbtnode_t *node;
  1398. int count = 10; /* XXXJT: should be adjustable */
  1399. node = ISC_LIST_HEAD(rbtdb->deadnodes[bucketnum]);
  1400. while (node != NULL && count > 0) {
  1401. ISC_LIST_UNLINK(rbtdb->deadnodes[bucketnum], node, deadlink);
  1402. /*
  1403. * Since we're holding a tree write lock, it should be
  1404. * impossible for this node to be referenced by others.
  1405. */
  1406. INSIST(dns_rbtnode_refcurrent(node) == 0 &&
  1407. node->data == NULL);
  1408. delete_node(rbtdb, node);
  1409. node = ISC_LIST_HEAD(rbtdb->deadnodes[bucketnum]);
  1410. count--;
  1411. }
  1412. }
  1413. /*
  1414. * Caller must be holding the node lock.
  1415. */
  1416. static inline void
  1417. new_reference(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node) {
  1418. unsigned int lockrefs, noderefs;
  1419. isc_refcount_t *lockref;
  1420. INSIST(!ISC_LINK_LINKED(node, deadlink));
  1421. dns_rbtnode_refincrement0(node, &noderefs);
  1422. if (noderefs == 1) { /* this is the first reference to the node */
  1423. lockref = &rbtdb->node_locks[node->locknum].references;
  1424. isc_refcount_increment0(lockref, &lockrefs);
  1425. INSIST(lockrefs != 0);
  1426. }
  1427. INSIST(noderefs != 0);
  1428. }
  1429. /*
  1430. * This function is assumed to be called when a node is newly referenced
  1431. * and can be in the deadnode list. In that case the node must be retrieved
  1432. * from the list because it is going to be used. In addition, if the caller
  1433. * happens to hold a write lock on the tree, it's a good chance to purge dead
  1434. * nodes.
  1435. * Note: while a new reference is gained in multiple places, there are only very
  1436. * few cases where the node can be in the deadnode list (only empty nodes can
  1437. * have been added to the list).
  1438. */
  1439. static inline void
  1440. reactivate_node(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node,
  1441. isc_rwlocktype_t treelocktype)
  1442. {
  1443. isc_rwlocktype_t locktype = isc_rwlocktype_read;
  1444. nodelock_t *nodelock = &rbtdb->node_locks[node->locknum].lock;
  1445. isc_boolean_t maybe_cleanup = ISC_FALSE;
  1446. POST(locktype);
  1447. NODE_STRONGLOCK(nodelock);
  1448. NODE_WEAKLOCK(nodelock, locktype);
  1449. /*
  1450. * Check if we can possibly cleanup the dead node. If so, upgrade
  1451. * the node lock below to perform the cleanup.
  1452. */
  1453. if (!ISC_LIST_EMPTY(rbtdb->deadnodes[node->locknum]) &&
  1454. treelocktype == isc_rwlocktype_write) {
  1455. maybe_cleanup = ISC_TRUE;
  1456. }
  1457. if (ISC_LINK_LINKED(node, deadlink) || maybe_cleanup) {
  1458. /*
  1459. * Upgrade the lock and test if we still need to unlink.
  1460. */
  1461. NODE_WEAKUNLOCK(nodelock, locktype);
  1462. locktype = isc_rwlocktype_write;
  1463. POST(locktype);
  1464. NODE_WEAKLOCK(nodelock, locktype);
  1465. if (ISC_LINK_LINKED(node, deadlink))
  1466. ISC_LIST_UNLINK(rbtdb->deadnodes[node->locknum],
  1467. node, deadlink);
  1468. if (maybe_cleanup)
  1469. cleanup_dead_nodes(rbtdb, node->locknum);
  1470. }
  1471. new_reference(rbtdb, node);
  1472. NODE_WEAKUNLOCK(nodelock, locktype);
  1473. NODE_STRONGUNLOCK(nodelock);
  1474. }
  1475. /*
  1476. * Caller must be holding the node lock; either the "strong", read or write
  1477. * lock. Note that the lock must be held even when node references are
  1478. * atomically modified; in that case the decrement operation itself does not
  1479. * have to be protected, but we must avoid a race condition where multiple
  1480. * threads are decreasing the reference to zero simultaneously and at least
  1481. * one of them is going to free the node.
  1482. * This function returns ISC_TRUE if and only if the node reference decreases
  1483. * to zero.
  1484. */
  1485. static isc_boolean_t
  1486. decrement_reference(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node,
  1487. rbtdb_serial_t least_serial,
  1488. isc_rwlocktype_t nlock, isc_rwlocktype_t tlock,
  1489. isc_boolean_t pruning)
  1490. {
  1491. isc_result_t result;
  1492. isc_boolean_t write_locked;
  1493. rbtdb_nodelock_t *nodelock;
  1494. unsigned int refs, nrefs;
  1495. int bucket = node->locknum;
  1496. isc_boolean_t no_reference = ISC_TRUE;
  1497. nodelock = &rbtdb->node_locks[bucket];
  1498. /* Handle easy and typical case first. */
  1499. if (!node->dirty && (node->data != NULL || node->down != NULL)) {
  1500. dns_rbtnode_refdecrement(node, &nrefs);
  1501. INSIST((int)nrefs >= 0);
  1502. if (nrefs == 0) {
  1503. isc_refcount_decrement(&nodelock->references, &refs);
  1504. INSIST((int)refs >= 0);
  1505. }
  1506. return ((nrefs == 0) ? ISC_TRUE : ISC_FALSE);
  1507. }
  1508. /* Upgrade the lock? */
  1509. if (nlock == isc_rwlocktype_read) {
  1510. NODE_WEAKUNLOCK(&nodelock->lock, isc_rwlocktype_read);
  1511. NODE_WEAKLOCK(&nodelock->lock, isc_rwlocktype_write);
  1512. }
  1513. dns_rbtnode_refdecrement(node, &nrefs);
  1514. INSIST((int)nrefs >= 0);
  1515. if (nrefs > 0) {
  1516. /* Restore the lock? */
  1517. if (nlock == isc_rwlocktype_read)
  1518. NODE_WEAKDOWNGRADE(&nodelock->lock);
  1519. return (ISC_FALSE);
  1520. }
  1521. if (node->dirty) {
  1522. if (IS_CACHE(rbtdb))
  1523. clean_cache_node(rbtdb, node);
  1524. else {
  1525. if (least_serial == 0) {
  1526. /*
  1527. * Caller doesn't know the least serial.
  1528. * Get it.
  1529. */
  1530. RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_read);
  1531. least_serial = rbtdb->least_serial;
  1532. RBTDB_UNLOCK(&rbtdb->lock,
  1533. isc_rwlocktype_read);
  1534. }
  1535. clean_zone_node(rbtdb, node, least_serial);
  1536. }
  1537. }
  1538. /*
  1539. * Attempt to switch to a write lock on the tree. If this fails,
  1540. * we will add this node to a linked list of nodes in this locking
  1541. * bucket which we will free later.
  1542. */
  1543. if (tlock != isc_rwlocktype_write) {
  1544. /*
  1545. * Locking hierarchy notwithstanding, we don't need to free
  1546. * the node lock before acquiring the tree write lock because
  1547. * we only do a trylock.
  1548. */
  1549. if (tlock == isc_rwlocktype_read)
  1550. result = isc_rwlock_tryupgrade(&rbtdb->tree_lock);
  1551. else
  1552. result = isc_rwlock_trylock(&rbtdb->tree_lock,
  1553. isc_rwlocktype_write);
  1554. RUNTIME_CHECK(result == ISC_R_SUCCESS ||
  1555. result == ISC_R_LOCKBUSY);
  1556. write_locked = ISC_TF(result == ISC_R_SUCCESS);
  1557. } else
  1558. write_locked = ISC_TRUE;
  1559. isc_refcount_decrement(&nodelock->references, &refs);
  1560. INSIST((int)refs >= 0);
  1561. /*
  1562. * XXXDCL should this only be done for cache zones?
  1563. */
  1564. if (node->data != NULL || node->down != NULL)
  1565. goto restore_locks;
  1566. if (write_locked) {
  1567. /*
  1568. * We can now delete the node.
  1569. */
  1570. /*
  1571. * If this node is the only one in the level it's in, deleting
  1572. * this node may recursively make its parent the only node in
  1573. * the parent level; if so, and if no one is currently using
  1574. * the parent node, this is almost the only opportunity to
  1575. * clean it up. But the recursive cleanup is not that trivial
  1576. * since the child and parent may be in different lock buckets,
  1577. * which would cause a lock order reversal problem. To avoid
  1578. * the trouble, we'll dispatch a separate event for batch
  1579. * cleaning. We need to check whether we're deleting the node
  1580. * as a result of pruning to avoid infinite dispatching.
  1581. * Note: pruning happens only when a task has been set for the
  1582. * rbtdb. If the user of the rbtdb chooses not to set a task,
  1583. * it's their responsibility to purge stale leaves (e.g. by
  1584. * periodic walk-through).
  1585. */
  1586. if (!pruning && node->parent != NULL &&
  1587. node->parent->down == node && node->left == NULL &&
  1588. node->right == NULL && rbtdb->task != NULL) {
  1589. isc_event_t *ev;
  1590. dns_db_t *db;
  1591. ev = isc_event_allocate(rbtdb->common.mctx, NULL,
  1592. DNS_EVENT_RBTPRUNE,
  1593. prune_tree, node,
  1594. sizeof(isc_event_t));
  1595. if (ev != NULL) {
  1596. new_reference(rbtdb, node);
  1597. db = NULL;
  1598. attach((dns_db_t *)rbtdb, &db);
  1599. ev->ev_sender = db;
  1600. isc_task_send(rbtdb->task, &ev);
  1601. no_reference = ISC_FALSE;
  1602. } else {
  1603. /*
  1604. * XXX: this is a weird situation. We could
  1605. * ignore this error case, but then the stale
  1606. * node will unlikely be purged except via a
  1607. * rare condition such as manual cleanup. So
  1608. * we queue it in the deadnodes list, hoping
  1609. * the memory shortage is temporary and the node
  1610. * will be deleted later.
  1611. */
  1612. isc_log_write(dns_lctx,
  1613. DNS_LOGCATEGORY_DATABASE,
  1614. DNS_LOGMODULE_CACHE,
  1615. ISC_LOG_INFO,
  1616. "decrement_reference: failed to "
  1617. "allocate pruning event");
  1618. INSIST(node->data == NULL);
  1619. INSIST(!ISC_LINK_LINKED(node, deadlink));
  1620. ISC_LIST_APPEND(rbtdb->deadnodes[bucket], node,
  1621. deadlink);
  1622. }
  1623. } else {
  1624. if (isc_log_wouldlog(dns_lctx, ISC_LOG_DEBUG(1))) {
  1625. char printname[DNS_NAME_FORMATSIZE];
  1626. isc_log_write(dns_lctx,
  1627. DNS_LOGCATEGORY_DATABASE,
  1628. DNS_LOGMODULE_CACHE,
  1629. ISC_LOG_DEBUG(1),
  1630. "decrement_reference: "
  1631. "delete from rbt: %p %s",
  1632. node,
  1633. dns_rbt_formatnodename(node,
  1634. printname,
  1635. sizeof(printname)));
  1636. }
  1637. delete_node(rbtdb, node);
  1638. }
  1639. } else {
  1640. INSIST(node->data == NULL);
  1641. INSIST(!ISC_LINK_LINKED(node, deadlink));
  1642. ISC_LIST_APPEND(rbtdb->deadnodes[bucket], node, deadlink);
  1643. }
  1644. restore_locks:
  1645. /* Restore the lock? */
  1646. if (nlock == isc_rwlocktype_read)
  1647. NODE_WEAKDOWNGRADE(&nodelock->lock);
  1648. /*
  1649. * Relock a read lock, or unlock the write lock if no lock was held.
  1650. */
  1651. if (tlock == isc_rwlocktype_none)
  1652. if (write_locked)
  1653. RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
  1654. if (tlock == isc_rwlocktype_read)
  1655. if (write_locked)
  1656. isc_rwlock_downgrade(&rbtdb->tree_lock);
  1657. return (no_reference);
  1658. }
  1659. /*
  1660. * Prune the tree by recursively cleaning-up single leaves. In the worst
  1661. * case, the number of iteration is the number of tree levels, which is at
  1662. * most the maximum number of domain name labels, i.e, 127. In practice, this
  1663. * should be much smaller (only a few times), and even the worst case would be
  1664. * acceptable for a single event.
  1665. */
  1666. static void
  1667. prune_tree(isc_task_t *task, isc_event_t *event) {
  1668. dns_rbtdb_t *rbtdb = event->ev_sender;
  1669. dns_rbtnode_t *node = event->ev_arg;
  1670. dns_rbtnode_t *parent;
  1671. unsigned int locknum;
  1672. UNUSED(task);
  1673. isc_event_free(&event);
  1674. RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
  1675. locknum = node->locknum;
  1676. NODE_LOCK(&rbtdb->node_locks[locknum].lock, isc_rwlocktype_write);
  1677. do {
  1678. parent = node->parent;
  1679. decrement_reference(rbtdb, node, 0, isc_rwlocktype_write,
  1680. isc_rwlocktype_write, ISC_TRUE);
  1681. if (parent != NULL && parent->down == NULL) {
  1682. /*
  1683. * node was the only down child of the parent and has
  1684. * just been removed. We'll then need to examine the
  1685. * parent. Keep the lock if possible; otherwise,
  1686. * release the old lock and acquire one for the parent.
  1687. */
  1688. if (parent->locknum != locknum) {
  1689. NODE_UNLOCK(&rbtdb->node_locks[locknum].lock,
  1690. isc_rwlocktype_write);
  1691. locknum = parent->locknum;
  1692. NODE_LOCK(&rbtdb->node_locks[locknum].lock,
  1693. isc_rwlocktype_write);
  1694. }
  1695. /*
  1696. * We need to gain a reference to the node before
  1697. * decrementing it in the next iteration. In addition,
  1698. * if the node is in the dead-nodes list, extract it
  1699. * from the list beforehand as we do in
  1700. * reactivate_node().
  1701. */
  1702. if (ISC_LINK_LINKED(parent, deadlink))
  1703. ISC_LIST_UNLINK(rbtdb->deadnodes[locknum],
  1704. parent, deadlink);
  1705. new_reference(rbtdb, parent);
  1706. } else
  1707. parent = NULL;
  1708. node = parent;
  1709. } while (node != NULL);
  1710. NODE_UNLOCK(&rbtdb->node_locks[locknum].lock, isc_rwlocktype_write);
  1711. RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
  1712. detach((dns_db_t **)&rbtdb);
  1713. }
  1714. static inline void
  1715. make_least_version(dns_rbtdb_t *rbtdb, rbtdb_version_t *version,
  1716. rbtdb_changedlist_t *cleanup_list)
  1717. {
  1718. /*
  1719. * Caller must be holding the database lock.
  1720. */
  1721. rbtdb->least_serial = version->serial;
  1722. *cleanup_list = version->changed_list;
  1723. ISC_LIST_INIT(version->changed_list);
  1724. }
  1725. static inline void
  1726. cleanup_nondirty(rbtdb_version_t *version, rbtdb_changedlist_t *cleanup_list) {
  1727. rbtdb_changed_t *changed, *next_changed;
  1728. /*
  1729. * If the changed record is dirty, then
  1730. * an update created multiple versions of
  1731. * a given rdataset. We keep this list
  1732. * until we're the least open version, at
  1733. * which point it's safe to get rid of any
  1734. * older versions.
  1735. *
  1736. * If the changed record isn't dirty, then
  1737. * we don't need it anymore since we're
  1738. * committing and not rolling back.
  1739. *
  1740. * The caller must be holding the database lock.
  1741. */
  1742. for (changed = HEAD(version->changed_list);
  1743. changed != NULL;
  1744. changed = next_changed) {
  1745. next_changed = NEXT(changed, link);
  1746. if (!changed->dirty) {
  1747. UNLINK(version->changed_list,
  1748. changed, link);
  1749. APPEND(*cleanup_list,
  1750. changed, link);
  1751. }
  1752. }
  1753. }
  1754. static void
  1755. iszonesecure(dns_db_t *db, rbtdb_version_t *version, dns_dbnode_t *origin) {
  1756. #ifndef BIND9
  1757. UNUSED(db);
  1758. UNUSED(version);
  1759. UNUSED(origin);
  1760. return;
  1761. #else
  1762. dns_rdataset_t keyset;
  1763. dns_rdataset_t nsecset, signsecset;
  1764. isc_boolean_t haszonekey = ISC_FALSE;
  1765. isc_boolean_t hasnsec = ISC_FALSE;
  1766. isc_result_t result;
  1767. dns_rdataset_init(&keyset);
  1768. result = dns_db_findrdataset(db, origin, version, dns_rdatatype_dnskey,
  1769. 0, 0, &keyset, NULL);
  1770. if (result == ISC_R_SUCCESS) {
  1771. result = dns_rdataset_first(&keyset);
  1772. while (result == ISC_R_SUCCESS) {
  1773. dns_rdata_t keyrdata = DNS_RDATA_INIT;
  1774. dns_rdataset_current(&keyset, &keyrdata);
  1775. if (dns_zonekey_iszonekey(&keyrdata)) {
  1776. haszonekey = ISC_TRUE;
  1777. break;
  1778. }
  1779. result = dns_rdataset_next(&keyset);
  1780. }
  1781. dns_rdataset_disassociate(&keyset);
  1782. }
  1783. if (!haszonekey) {
  1784. version->secure = dns_db_insecure;
  1785. version->havensec3 = ISC_FALSE;
  1786. return;
  1787. }
  1788. dns_rdataset_init(&nsecset);
  1789. dns_rdataset_init(&signsecset);
  1790. result = dns_db_findrdataset(db, origin, version, dns_rdatatype_nsec,
  1791. 0, 0, &nsecset, &signsecset);
  1792. if (result == ISC_R_SUCCESS) {
  1793. if (dns_rdataset_isassociated(&signsecset)) {
  1794. hasnsec = ISC_TRUE;
  1795. dns_rdataset_disassociate(&signsecset);
  1796. }
  1797. dns_rdataset_disassociate(&nsecset);
  1798. }
  1799. setnsec3parameters(db, version);
  1800. /*
  1801. * Do we have a valid NSEC/NSEC3 chain?
  1802. */
  1803. if (version->havensec3 || hasnsec)
  1804. version->secure = dns_db_secure;
  1805. else
  1806. version->secure = dns_db_insecure;
  1807. #endif
  1808. }
  1809. /*%<
  1810. * Walk the origin node looking for NSEC3PARAM records.
  1811. * Cache the nsec3 parameters.
  1812. */
  1813. #ifdef BIND9
  1814. static void
  1815. setnsec3parameters(dns_db_t *db, rbtdb_version_t *version) {
  1816. dns_rbtnode_t *node;
  1817. dns_rdata_nsec3param_t nsec3param;
  1818. dns_rdata_t rdata = DNS_RDATA_INIT;
  1819. isc_region_t region;
  1820. isc_result_t result;
  1821. rdatasetheader_t *header, *header_next;
  1822. unsigned char *raw; /* RDATASLAB */
  1823. unsigned int count, length;
  1824. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
  1825. RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
  1826. version->havensec3 = ISC_FALSE;
  1827. node = rbtdb->origin_node;
  1828. NODE_LOCK(&(rbtdb->node_locks[node->locknum].lock),
  1829. isc_rwlocktype_read);
  1830. for (header = node->data;
  1831. header != NULL;
  1832. header = header_next) {
  1833. header_next = header->next;
  1834. do {
  1835. if (header->serial <= version->serial &&
  1836. !IGNORE(header)) {
  1837. if (NONEXISTENT(header))
  1838. header = NULL;
  1839. break;
  1840. } else
  1841. header = header->down;
  1842. } while (header != NULL);
  1843. if (header != NULL &&
  1844. (header->type == dns_rdatatype_nsec3param)) {
  1845. /*
  1846. * Find A NSEC3PARAM with a supported algorithm.
  1847. */
  1848. raw = (unsigned char *)header + sizeof(*header);
  1849. count = raw[0] * 256 + raw[1]; /* count */
  1850. #if DNS_RDATASET_FIXED
  1851. raw += count * 4 + 2;
  1852. #else
  1853. raw += 2;
  1854. #endif
  1855. while (count-- > 0U) {
  1856. length = raw[0] * 256 + raw[1];
  1857. #if DNS_RDATASET_FIXED
  1858. raw += 4;
  1859. #else
  1860. raw += 2;
  1861. #endif
  1862. region.base = raw;
  1863. region.length = length;
  1864. raw += length;
  1865. dns_rdata_fromregion(&rdata,
  1866. rbtdb->common.rdclass,
  1867. dns_rdatatype_nsec3param,
  1868. &region);
  1869. result = dns_rdata_tostruct(&rdata,
  1870. &nsec3param,
  1871. NULL);
  1872. INSIST(result == ISC_R_SUCCESS);
  1873. dns_rdata_reset(&rdata);
  1874. if (nsec3param.hash != DNS_NSEC3_UNKNOWNALG &&
  1875. !dns_nsec3_supportedhash(nsec3param.hash))
  1876. continue;
  1877. if (nsec3param.flags != 0)
  1878. continue;
  1879. memcpy(version->salt, nsec3param.salt,
  1880. nsec3param.salt_length);
  1881. version->hash = nsec3param.hash;
  1882. version->salt_length = nsec3param.salt_length;
  1883. version->iterations = nsec3param.iterations;
  1884. version->flags = nsec3param.flags;
  1885. version->havensec3 = ISC_TRUE;
  1886. /*
  1887. * Look for a better algorithm than the
  1888. * unknown test algorithm.
  1889. */
  1890. if (nsec3param.hash != DNS_NSEC3_UNKNOWNALG)
  1891. goto unlock;
  1892. }
  1893. }
  1894. }
  1895. unlock:
  1896. NODE_UNLOCK(&(rbtdb->node_locks[node->locknum].lock),
  1897. isc_rwlocktype_read);
  1898. RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
  1899. }
  1900. #endif
  1901. static void
  1902. cleanup_dead_nodes_callback(isc_task_t *task, isc_event_t *event) {
  1903. dns_rbtdb_t *rbtdb = event->ev_arg;
  1904. isc_boolean_t again = ISC_FALSE;
  1905. unsigned int locknum;
  1906. unsigned int refs;
  1907. RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
  1908. for (locknum = 0; locknum < rbtdb->node_lock_count; locknum++) {
  1909. NODE_LOCK(&rbtdb->node_locks[locknum].lock,
  1910. isc_rwlocktype_write);
  1911. cleanup_dead_nodes(rbtdb, locknum);
  1912. if (ISC_LIST_HEAD(rbtdb->deadnodes[locknum]) != NULL)
  1913. again = ISC_TRUE;
  1914. NODE_UNLOCK(&rbtdb->node_locks[locknum].lock,
  1915. isc_rwlocktype_write);
  1916. }
  1917. RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
  1918. if (again)
  1919. isc_task_send(task, &event);
  1920. else {
  1921. isc_event_free(&event);
  1922. isc_refcount_decrement(&rbtdb->references, &refs);
  1923. if (refs == 0)
  1924. maybe_free_rbtdb(rbtdb);
  1925. }
  1926. }
  1927. static void
  1928. closeversion(dns_db_t *db, dns_dbversion_t **versionp, isc_boolean_t commit) {
  1929. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
  1930. rbtdb_version_t *version, *cleanup_version, *least_greater;
  1931. isc_boolean_t rollback = ISC_FALSE;
  1932. rbtdb_changedlist_t cleanup_list;
  1933. rdatasetheaderlist_t resigned_list;
  1934. rbtdb_changed_t *changed, *next_changed;
  1935. rbtdb_serial_t serial, least_serial;
  1936. dns_rbtnode_t *rbtnode;
  1937. unsigned int refs;
  1938. rdatasetheader_t *header;
  1939. isc_boolean_t writer;
  1940. REQUIRE(VALID_RBTDB(rbtdb));
  1941. version = (rbtdb_version_t *)*versionp;
  1942. INSIST(version->rbtdb == rbtdb);
  1943. cleanup_version = NULL;
  1944. ISC_LIST_INIT(cleanup_list);
  1945. ISC_LIST_INIT(resigned_list);
  1946. isc_refcount_decrement(&version->references, &refs);
  1947. if (refs > 0) { /* typical and easy case first */
  1948. if (commit) {
  1949. RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_read);
  1950. INSIST(!version->writer);
  1951. RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_read);
  1952. }
  1953. goto end;
  1954. }
  1955. RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_write);
  1956. serial = version->serial;
  1957. writer = version->writer;
  1958. if (version->writer) {
  1959. if (commit) {
  1960. unsigned cur_ref;
  1961. rbtdb_version_t *cur_version;
  1962. INSIST(version->commit_ok);
  1963. INSIST(version == rbtdb->future_version);
  1964. /*
  1965. * The current version is going to be replaced.
  1966. * Release the (likely last) reference to it from the
  1967. * DB itself and unlink it from the open list.
  1968. */
  1969. cur_version = rbtdb->current_version;
  1970. isc_refcount_decrement(&cur_version->references,
  1971. &cur_ref);
  1972. if (cur_ref == 0) {
  1973. if (cur_version->serial == rbtdb->least_serial)
  1974. INSIST(EMPTY(cur_version->changed_list));
  1975. UNLINK(rbtdb->open_versions,
  1976. cur_version, link);
  1977. }
  1978. if (EMPTY(rbtdb->open_versions)) {
  1979. /*
  1980. * We're going to become the least open
  1981. * version.
  1982. */
  1983. make_least_version(rbtdb, version,
  1984. &cleanup_list);
  1985. } else {
  1986. /*
  1987. * Some other open version is the
  1988. * least version. We can't cleanup
  1989. * records that were changed in this
  1990. * version because the older versions
  1991. * may still be in use by an open
  1992. * version.
  1993. *
  1994. * We can, however, discard the
  1995. * changed records for things that
  1996. * we've added that didn't exist in
  1997. * prior versions.
  1998. */
  1999. cleanup_nondirty(version, &cleanup_list);
  2000. }
  2001. /*
  2002. * If the (soon to be former) current version
  2003. * isn't being used by anyone, we can clean
  2004. * it up.
  2005. */
  2006. if (cur_ref == 0) {
  2007. cleanup_version = cur_version;
  2008. APPENDLIST(version->changed_list,
  2009. cleanup_version->changed_list,
  2010. link);
  2011. }
  2012. /*
  2013. * Become the current version.
  2014. */
  2015. version->writer = ISC_FALSE;
  2016. rbtdb->current_version = version;
  2017. rbtdb->current_serial = version->serial;
  2018. rbtdb->future_version = NULL;
  2019. /*
  2020. * Keep the current version in the open list, and
  2021. * gain a reference for the DB itself (see the DB
  2022. * creation function below). This must be the only
  2023. * case where we need to increment the counter from
  2024. * zero and need to use isc_refcount_increment0().
  2025. */
  2026. isc_refcount_increment0(&version->references,
  2027. &cur_ref);
  2028. INSIST(cur_ref == 1);
  2029. PREPEND(rbtdb->open_versions,
  2030. rbtdb->current_version, link);
  2031. resigned_list = version->resigned_list;
  2032. ISC_LIST_INIT(version->resigned_list);
  2033. } else {
  2034. /*
  2035. * We're rolling back this transaction.
  2036. */
  2037. cleanup_list = version->changed_list;
  2038. ISC_LIST_INIT(version->changed_list);
  2039. resigned_list = version->resigned_list;
  2040. ISC_LIST_INIT(version->resigned_list);
  2041. rollback = ISC_TRUE;
  2042. cleanup_version = version;
  2043. rbtdb->future_version = NULL;
  2044. }
  2045. } else {
  2046. if (version != rbtdb->current_version) {
  2047. /*
  2048. * There are no external or internal references
  2049. * to this version and it can be cleaned up.
  2050. */
  2051. cleanup_version = version;
  2052. /*
  2053. * Find the version with the least serial
  2054. * number greater than ours.
  2055. */
  2056. least_greater = PREV(version, link);
  2057. if (least_greater == NULL)
  2058. least_greater = rbtdb->current_version;
  2059. INSIST(version->serial < least_greater->serial);
  2060. /*
  2061. * Is this the least open version?
  2062. */
  2063. if (version->serial == rbtdb->least_serial) {
  2064. /*
  2065. * Yes. Install the new least open
  2066. * version.
  2067. */
  2068. make_least_version(rbtdb,
  2069. least_greater,
  2070. &cleanup_list);
  2071. } else {
  2072. /*
  2073. * Add any unexecuted cleanups to
  2074. * those of the least greater version.
  2075. */
  2076. APPENDLIST(least_greater->changed_list,
  2077. version->changed_list,
  2078. link);
  2079. }
  2080. } else if (version->serial == rbtdb->least_serial)
  2081. INSIST(EMPTY(version->changed_list));
  2082. UNLINK(rbtdb->open_versions, version, link);
  2083. }
  2084. least_serial = rbtdb->least_serial;
  2085. RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_write);
  2086. /*
  2087. * Update the zone's secure status.
  2088. */
  2089. if (writer && commit && !IS_CACHE(rbtdb))
  2090. iszonesecure(db, version, rbtdb->origin_node);
  2091. if (cleanup_version != NULL) {
  2092. INSIST(EMPTY(cleanup_version->changed_list));
  2093. isc_mem_put(rbtdb->common.mctx, cleanup_version,
  2094. sizeof(*cleanup_version));
  2095. }
  2096. /*
  2097. * Commit/rollback re-signed headers.
  2098. */
  2099. for (header = HEAD(resigned_list);
  2100. header != NULL;
  2101. header = HEAD(resigned_list)) {
  2102. nodelock_t *lock;
  2103. ISC_LIST_UNLINK(resigned_list, header, link);
  2104. lock = &rbtdb->node_locks[header->node->locknum].lock;
  2105. NODE_LOCK(lock, isc_rwlocktype_write);
  2106. if (rollback)
  2107. resign_insert(rbtdb, header->node->locknum, header);
  2108. decrement_reference(rbtdb, header->node, least_serial,
  2109. isc_rwlocktype_write, isc_rwlocktype_none,
  2110. ISC_FALSE);
  2111. NODE_UNLOCK(lock, isc_rwlocktype_write);
  2112. }
  2113. if (!EMPTY(cleanup_list)) {
  2114. isc_event_t *event = NULL;
  2115. isc_rwlocktype_t tlock = isc_rwlocktype_none;
  2116. if (rbtdb->task != NULL)
  2117. event = isc_event_allocate(rbtdb->common.mctx, NULL,
  2118. DNS_EVENT_RBTDEADNODES,
  2119. cleanup_dead_nodes_callback,
  2120. rbtdb, sizeof(isc_event_t));
  2121. if (event == NULL) {
  2122. /*
  2123. * We acquire a tree write lock here in order to make
  2124. * sure that stale nodes will be removed in
  2125. * decrement_reference(). If we didn't have the lock,
  2126. * those nodes could miss the chance to be removed
  2127. * until the server stops. The write lock is
  2128. * expensive, but this event should be rare enough
  2129. * to justify the cost.
  2130. */
  2131. RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
  2132. tlock = isc_rwlocktype_write;
  2133. }
  2134. for (changed = HEAD(cleanup_list);
  2135. changed != NULL;
  2136. changed = next_changed) {
  2137. nodelock_t *lock;
  2138. next_changed = NEXT(changed, link);
  2139. rbtnode = changed->node;
  2140. lock = &rbtdb->node_locks[rbtnode->locknum].lock;
  2141. NODE_LOCK(lock, isc_rwlocktype_write);
  2142. /*
  2143. * This is a good opportunity to purge any dead nodes,
  2144. * so use it.
  2145. */
  2146. if (event == NULL)
  2147. cleanup_dead_nodes(rbtdb, rbtnode->locknum);
  2148. if (rollback)
  2149. rollback_node(rbtnode, serial);
  2150. decrement_reference(rbtdb, rbtnode, least_serial,
  2151. isc_rwlocktype_write, tlock,
  2152. ISC_FALSE);
  2153. NODE_UNLOCK(lock, isc_rwlocktype_write);
  2154. isc_mem_put(rbtdb->common.mctx, changed,
  2155. sizeof(*changed));
  2156. }
  2157. if (event != NULL) {
  2158. isc_refcount_increment(&rbtdb->references, NULL);
  2159. isc_task_send(rbtdb->task, &event);
  2160. } else
  2161. RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
  2162. }
  2163. end:
  2164. *versionp = NULL;
  2165. }
  2166. /*
  2167. * Add the necessary magic for the wildcard name 'name'
  2168. * to be found in 'rbtdb'.
  2169. *
  2170. * In order for wildcard matching to work correctly in
  2171. * zone_find(), we must ensure that a node for the wildcarding
  2172. * level exists in the database, and has its 'find_callback'
  2173. * and 'wild' bits set.
  2174. *
  2175. * E.g. if the wildcard name is "*.sub.example." then we
  2176. * must ensure that "sub.example." exists and is marked as
  2177. * a wildcard level.
  2178. */
  2179. static isc_result_t
  2180. add_wildcard_magic(dns_rbtdb_t *rbtdb, dns_name_t *name) {
  2181. isc_result_t result;
  2182. dns_name_t foundname;
  2183. dns_offsets_t offsets;
  2184. unsigned int n;
  2185. dns_rbtnode_t *node = NULL;
  2186. dns_name_init(&foundname, offsets);
  2187. n = dns_name_countlabels(name);
  2188. INSIST(n >= 2);
  2189. n--;
  2190. dns_name_getlabelsequence(name, 1, n, &foundname);
  2191. result = dns_rbt_addnode(rbtdb->tree, &foundname, &node);
  2192. if (result != ISC_R_SUCCESS && result != ISC_R_EXISTS)
  2193. return (result);
  2194. if (result == ISC_R_SUCCESS)
  2195. node->nsec = DNS_RBT_NSEC_NORMAL;
  2196. node->find_callback = 1;
  2197. node->wild = 1;
  2198. return (ISC_R_SUCCESS);
  2199. }
  2200. static isc_result_t
  2201. add_empty_wildcards(dns_rbtdb_t *rbtdb, dns_name_t *name) {
  2202. isc_result_t result;
  2203. dns_name_t foundname;
  2204. dns_offsets_t offsets;
  2205. unsigned int n, l, i;
  2206. dns_name_init(&foundname, offsets);
  2207. n = dns_name_countlabels(name);
  2208. l = dns_name_countlabels(&rbtdb->common.origin);
  2209. i = l + 1;
  2210. while (i < n) {
  2211. dns_rbtnode_t *node = NULL; /* dummy */
  2212. dns_name_getlabelsequence(name, n - i, i, &foundname);
  2213. if (dns_name_iswildcard(&foundname)) {
  2214. result = add_wildcard_magic(rbtdb, &foundname);
  2215. if (result != ISC_R_SUCCESS)
  2216. return (result);
  2217. result = dns_rbt_addnode(rbtdb->tree, &foundname,
  2218. &node);
  2219. if (result != ISC_R_SUCCESS && result != ISC_R_EXISTS)
  2220. return (result);
  2221. if (result == ISC_R_SUCCESS)
  2222. node->nsec = DNS_RBT_NSEC_NORMAL;
  2223. }
  2224. i++;
  2225. }
  2226. return (ISC_R_SUCCESS);
  2227. }
  2228. static isc_result_t
  2229. findnodeintree(dns_rbtdb_t *rbtdb, dns_rbt_t *tree, dns_name_t *name,
  2230. isc_boolean_t create, dns_dbnode_t **nodep)
  2231. {
  2232. dns_rbtnode_t *node = NULL;
  2233. dns_name_t nodename;
  2234. isc_result_t result;
  2235. isc_rwlocktype_t locktype = isc_rwlocktype_read;
  2236. INSIST(tree == rbtdb->tree || tree == rbtdb->nsec3);
  2237. dns_name_init(&nodename, NULL);
  2238. RWLOCK(&rbtdb->tree_lock, locktype);
  2239. result = dns_rbt_findnode(tree, name, NULL, &node, NULL,
  2240. DNS_RBTFIND_EMPTYDATA, NULL, NULL);
  2241. if (result != ISC_R_SUCCESS) {
  2242. RWUNLOCK(&rbtdb->tree_lock, locktype);
  2243. if (!create) {
  2244. if (result == DNS_R_PARTIALMATCH)
  2245. result = ISC_R_NOTFOUND;
  2246. return (result);
  2247. }
  2248. /*
  2249. * It would be nice to try to upgrade the lock instead of
  2250. * unlocking then relocking.
  2251. */
  2252. locktype = isc_rwlocktype_write;
  2253. RWLOCK(&rbtdb->tree_lock, locktype);
  2254. node = NULL;
  2255. result = dns_rbt_addnode(tree, name, &node);
  2256. if (result == ISC_R_SUCCESS) {
  2257. #ifdef BIND9
  2258. if (tree == rbtdb->tree && rbtdb->rpz_cidr != NULL) {
  2259. dns_fixedname_t fnamef;
  2260. dns_name_t *fname;
  2261. dns_fixedname_init(&fnamef);
  2262. fname = dns_fixedname_name(&fnamef);
  2263. dns_rbt_fullnamefromnode(node, fname);
  2264. dns_rpz_cidr_addip(rbtdb->rpz_cidr, fname);
  2265. }
  2266. #endif
  2267. dns_rbt_namefromnode(node, &nodename);
  2268. #ifdef DNS_RBT_USEHASH
  2269. node->locknum = node->hashval % rbtdb->node_lock_count;
  2270. #else
  2271. node->locknum = dns_name_hash(&nodename, ISC_TRUE) %
  2272. rbtdb->node_lock_count;
  2273. #endif
  2274. if (tree == rbtdb->tree) {
  2275. add_empty_wildcards(rbtdb, name);
  2276. if (dns_name_iswildcard(name)) {
  2277. result = add_wildcard_magic(rbtdb, name);
  2278. if (result != ISC_R_SUCCESS) {
  2279. RWUNLOCK(&rbtdb->tree_lock, locktype);
  2280. return (result);
  2281. }
  2282. }
  2283. }
  2284. if (tree == rbtdb->nsec3)
  2285. node->nsec = DNS_RBT_NSEC_NSEC3;
  2286. } else if (result != ISC_R_EXISTS) {
  2287. RWUNLOCK(&rbtdb->tree_lock, locktype);
  2288. return (result);
  2289. }
  2290. }
  2291. if (tree == rbtdb->nsec3)
  2292. INSIST(node->nsec == DNS_RBT_NSEC_NSEC3);
  2293. reactivate_node(rbtdb, node, locktype);
  2294. RWUNLOCK(&rbtdb->tree_lock, locktype);
  2295. *nodep = (dns_dbnode_t *)node;
  2296. return (ISC_R_SUCCESS);
  2297. }
  2298. static isc_result_t
  2299. findnode(dns_db_t *db, dns_name_t *name, isc_boolean_t create,
  2300. dns_dbnode_t **nodep)
  2301. {
  2302. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
  2303. REQUIRE(VALID_RBTDB(rbtdb));
  2304. return (findnodeintree(rbtdb, rbtdb->tree, name, create, nodep));
  2305. }
  2306. static isc_result_t
  2307. findnsec3node(dns_db_t *db, dns_name_t *name, isc_boolean_t create,
  2308. dns_dbnode_t **nodep)
  2309. {
  2310. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
  2311. REQUIRE(VALID_RBTDB(rbtdb));
  2312. return (findnodeintree(rbtdb, rbtdb->nsec3, name, create, nodep));
  2313. }
  2314. static isc_result_t
  2315. zone_zonecut_callback(dns_rbtnode_t *node, dns_name_t *name, void *arg) {
  2316. rbtdb_search_t *search = arg;
  2317. rdatasetheader_t *header, *header_next;
  2318. rdatasetheader_t *dname_header, *sigdname_header, *ns_header;
  2319. rdatasetheader_t *found;
  2320. isc_result_t result;
  2321. dns_rbtnode_t *onode;
  2322. /*
  2323. * We only want to remember the topmost zone cut, since it's the one
  2324. * that counts, so we'll just continue if we've already found a
  2325. * zonecut.
  2326. */
  2327. if (search->zonecut != NULL)
  2328. return (DNS_R_CONTINUE);
  2329. found = NULL;
  2330. result = DNS_R_CONTINUE;
  2331. onode = search->rbtdb->origin_node;
  2332. NODE_LOCK(&(search->rbtdb->node_locks[node->locknum].lock),
  2333. isc_rwlocktype_read);
  2334. /*
  2335. * Look for an NS or DNAME rdataset active in our version.
  2336. */
  2337. ns_header = NULL;
  2338. dname_header = NULL;
  2339. sigdname_header = NULL;
  2340. for (header = node->data; header != NULL; header = header_next) {
  2341. header_next = header->next;
  2342. if (header->type == dns_rdatatype_ns ||
  2343. header->type == dns_rdatatype_dname ||
  2344. header->type == RBTDB_RDATATYPE_SIGDNAME) {
  2345. do {
  2346. if (header->serial <= search->serial &&
  2347. !IGNORE(header)) {
  2348. /*
  2349. * Is this a "this rdataset doesn't
  2350. * exist" record?
  2351. */
  2352. if (NONEXISTENT(header))
  2353. header = NULL;
  2354. break;
  2355. } else
  2356. header = header->down;
  2357. } while (header != NULL);
  2358. if (header != NULL) {
  2359. if (header->type == dns_rdatatype_dname)
  2360. dname_header = header;
  2361. else if (header->type ==
  2362. RBTDB_RDATATYPE_SIGDNAME)
  2363. sigdname_header = header;
  2364. else if (node != onode ||
  2365. IS_STUB(search->rbtdb)) {
  2366. /*
  2367. * We've found an NS rdataset that
  2368. * isn't at the origin node. We check
  2369. * that they're not at the origin node,
  2370. * because otherwise we'd erroneously
  2371. * treat the zone top as if it were
  2372. * a delegation.
  2373. */
  2374. ns_header = header;
  2375. }
  2376. }
  2377. }
  2378. }
  2379. /*
  2380. * Did we find anything?
  2381. */
  2382. if (!IS_CACHE(search->rbtdb) && !IS_STUB(search->rbtdb) &&
  2383. ns_header != NULL) {
  2384. /*
  2385. * Note that NS has precedence over DNAME if both exist
  2386. * in a zone. Otherwise DNAME take precedence over NS.
  2387. */
  2388. found = ns_header;
  2389. search->zonecut_sigrdataset = NULL;
  2390. } else if (dname_header != NULL) {
  2391. found = dname_header;
  2392. search->zonecut_sigrdataset = sigdname_header;
  2393. } else if (ns_header != NULL) {
  2394. found = ns_header;
  2395. search->zonecut_sigrdataset = NULL;
  2396. }
  2397. if (found != NULL) {
  2398. /*
  2399. * We increment the reference count on node to ensure that
  2400. * search->zonecut_rdataset will still be valid later.
  2401. */
  2402. new_reference(search->rbtdb, node);
  2403. search->zonecut = node;
  2404. search->zonecut_rdataset = found;
  2405. search->need_cleanup = ISC_TRUE;
  2406. /*
  2407. * Since we've found a zonecut, anything beneath it is
  2408. * glue and is not subject to wildcard matching, so we
  2409. * may clear search->wild.
  2410. */
  2411. search->wild = ISC_FALSE;
  2412. if ((search->options & DNS_DBFIND_GLUEOK) == 0) {
  2413. /*
  2414. * If the caller does not want to find glue, then
  2415. * this is the best answer and the search should
  2416. * stop now.
  2417. */
  2418. result = DNS_R_PARTIALMATCH;
  2419. } else {
  2420. dns_name_t *zcname;
  2421. /*
  2422. * The search will continue beneath the zone cut.
  2423. * This may or may not be the best match. In case it
  2424. * is, we need to remember the node name.
  2425. */
  2426. zcname = dns_fixedname_name(&search->zonecut_name);
  2427. RUNTIME_CHECK(dns_name_copy(name, zcname, NULL) ==
  2428. ISC_R_SUCCESS);
  2429. search->copy_name = ISC_TRUE;
  2430. }
  2431. } else {
  2432. /*
  2433. * There is no zonecut at this node which is active in this
  2434. * version.
  2435. *
  2436. * If this is a "wild" node and the caller hasn't disabled
  2437. * wildcard matching, remember that we've seen a wild node
  2438. * in case we need to go searching for wildcard matches
  2439. * later on.
  2440. */
  2441. if (node->wild && (search->options & DNS_DBFIND_NOWILD) == 0)
  2442. search->wild = ISC_TRUE;
  2443. }
  2444. NODE_UNLOCK(&(search->rbtdb->node_locks[node->locknum].lock),
  2445. isc_rwlocktype_read);
  2446. return (result);
  2447. }
  2448. static inline void
  2449. bind_rdataset(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node,
  2450. rdatasetheader_t *header, isc_stdtime_t now,
  2451. dns_rdataset_t *rdataset)
  2452. {
  2453. unsigned char *raw; /* RDATASLAB */
  2454. /*
  2455. * Caller must be holding the node reader lock.
  2456. * XXXJT: technically, we need a writer lock, since we'll increment
  2457. * the header count below. However, since the actual counter value
  2458. * doesn't matter, we prioritize performance here. (We may want to
  2459. * use atomic increment when available).
  2460. */
  2461. if (rdataset == NULL)
  2462. return;
  2463. new_reference(rbtdb, node);
  2464. INSIST(rdataset->methods == NULL); /* We must be disassociated. */
  2465. rdataset->methods = &rdataset_methods;
  2466. rdataset->rdclass = rbtdb->common.rdclass;
  2467. rdataset->type = RBTDB_RDATATYPE_BASE(header->type);
  2468. rdataset->covers = RBTDB_RDATATYPE_EXT(header->type);
  2469. rdataset->ttl = header->rdh_ttl - now;
  2470. rdataset->trust = header->trust;
  2471. if (NEGATIVE(header))
  2472. rdataset->attributes |= DNS_RDATASETATTR_NEGATIVE;
  2473. if (NXDOMAIN(header))
  2474. rdataset->attributes |= DNS_RDATASETATTR_NXDOMAIN;
  2475. if (OPTOUT(header))
  2476. rdataset->attributes |= DNS_RDATASETATTR_OPTOUT;
  2477. rdataset->private1 = rbtdb;
  2478. rdataset->private2 = node;
  2479. raw = (unsigned char *)header + sizeof(*header);
  2480. rdataset->private3 = raw;
  2481. rdataset->count = header->count++;
  2482. if (rdataset->count == ISC_UINT32_MAX)
  2483. rdataset->count = 0;
  2484. /*
  2485. * Reset iterator state.
  2486. */
  2487. rdataset->privateuint4 = 0;
  2488. rdataset->private5 = NULL;
  2489. /*
  2490. * Add noqname proof.
  2491. */
  2492. rdataset->private6 = header->noqname;
  2493. if (rdataset->private6 != NULL)
  2494. rdataset->attributes |= DNS_RDATASETATTR_NOQNAME;
  2495. rdataset->private7 = header->closest;
  2496. if (rdataset->private7 != NULL)
  2497. rdataset->attributes |= DNS_RDATASETATTR_CLOSEST;
  2498. /*
  2499. * Copy out re-signing information.
  2500. */
  2501. if (RESIGN(header)) {
  2502. rdataset->attributes |= DNS_RDATASETATTR_RESIGN;
  2503. rdataset->resign = header->resign;
  2504. } else
  2505. rdataset->resign = 0;
  2506. }
  2507. static inline isc_result_t
  2508. setup_delegation(rbtdb_search_t *search, dns_dbnode_t **nodep,
  2509. dns_name_t *foundname, dns_rdataset_t *rdataset,
  2510. dns_rdataset_t *sigrdataset)
  2511. {
  2512. isc_result_t result;
  2513. dns_name_t *zcname;
  2514. rbtdb_rdatatype_t type;
  2515. dns_rbtnode_t *node;
  2516. /*
  2517. * The caller MUST NOT be holding any node locks.
  2518. */
  2519. node = search->zonecut;
  2520. type = search->zonecut_rdataset->type;
  2521. /*
  2522. * If we have to set foundname, we do it before anything else.
  2523. * If we were to set foundname after we had set nodep or bound the
  2524. * rdataset, then we'd have to undo that work if dns_name_copy()
  2525. * failed. By setting foundname first, there's nothing to undo if
  2526. * we have trouble.
  2527. */
  2528. if (foundname != NULL && search->copy_name) {
  2529. zcname = dns_fixedname_name(&search->zonecut_name);
  2530. result = dns_name_copy(zcname, foundname, NULL);
  2531. if (result != ISC_R_SUCCESS)
  2532. return (result);
  2533. }
  2534. if (nodep != NULL) {
  2535. /*
  2536. * Note that we don't have to increment the node's reference
  2537. * count here because we're going to use the reference we
  2538. * already have in the search block.
  2539. */
  2540. *nodep = node;
  2541. search->need_cleanup = ISC_FALSE;
  2542. }
  2543. if (rdataset != NULL) {
  2544. NODE_LOCK(&(search->rbtdb->node_locks[node->locknum].lock),
  2545. isc_rwlocktype_read);
  2546. bind_rdataset(search->rbtdb, node, search->zonecut_rdataset,
  2547. search->now, rdataset);
  2548. if (sigrdataset != NULL && search->zonecut_sigrdataset != NULL)
  2549. bind_rdataset(search->rbtdb, node,
  2550. search->zonecut_sigrdataset,
  2551. search->now, sigrdataset);
  2552. NODE_UNLOCK(&(search->rbtdb->node_locks[node->locknum].lock),
  2553. isc_rwlocktype_read);
  2554. }
  2555. if (type == dns_rdatatype_dname)
  2556. return (DNS_R_DNAME);
  2557. return (DNS_R_DELEGATION);
  2558. }
  2559. static inline isc_boolean_t
  2560. valid_glue(rbtdb_search_t *search, dns_name_t *name, rbtdb_rdatatype_t type,
  2561. dns_rbtnode_t *node)
  2562. {
  2563. unsigned char *raw; /* RDATASLAB */
  2564. unsigned int count, size;
  2565. dns_name_t ns_name;
  2566. isc_boolean_t valid = ISC_FALSE;
  2567. dns_offsets_t offsets;
  2568. isc_region_t region;
  2569. rdatasetheader_t *header;
  2570. /*
  2571. * No additional locking is required.
  2572. */
  2573. /*
  2574. * Valid glue types are A, AAAA, A6. NS is also a valid glue type
  2575. * if it occurs at a zone cut, but is not valid below it.
  2576. */
  2577. if (type == dns_rdatatype_ns) {
  2578. if (node != search->zonecut) {
  2579. return (ISC_FALSE);
  2580. }
  2581. } else if (type != dns_rdatatype_a &&
  2582. type != dns_rdatatype_aaaa &&
  2583. type != dns_rdatatype_a6) {
  2584. return (ISC_FALSE);
  2585. }
  2586. header = search->zonecut_rdataset;
  2587. raw = (unsigned char *)header + sizeof(*header);
  2588. count = raw[0] * 256 + raw[1];
  2589. #if DNS_RDATASET_FIXED
  2590. raw += 2 + (4 * count);
  2591. #else
  2592. raw += 2;
  2593. #endif
  2594. while (count > 0) {
  2595. count--;
  2596. size = raw[0] * 256 + raw[1];
  2597. #if DNS_RDATASET_FIXED
  2598. raw += 4;
  2599. #else
  2600. raw += 2;
  2601. #endif
  2602. region.base = raw;
  2603. region.length = size;
  2604. raw += size;
  2605. /*
  2606. * XXX Until we have rdata structures, we have no choice but
  2607. * to directly access the rdata format.
  2608. */
  2609. dns_name_init(&ns_name, offsets);
  2610. dns_name_fromregion(&ns_name, &region);
  2611. if (dns_name_compare(&ns_name, name) == 0) {
  2612. valid = ISC_TRUE;
  2613. break;
  2614. }
  2615. }
  2616. return (valid);
  2617. }
  2618. static inline isc_boolean_t
  2619. activeempty(rbtdb_search_t *search, dns_rbtnodechain_t *chain,
  2620. dns_name_t *name)
  2621. {
  2622. dns_fixedname_t fnext;
  2623. dns_fixedname_t forigin;
  2624. dns_name_t *next;
  2625. dns_name_t *origin;
  2626. dns_name_t prefix;
  2627. dns_rbtdb_t *rbtdb;
  2628. dns_rbtnode_t *node;
  2629. isc_result_t result;
  2630. isc_boolean_t answer = ISC_FALSE;
  2631. rdatasetheader_t *header;
  2632. rbtdb = search->rbtdb;
  2633. dns_name_init(&prefix, NULL);
  2634. dns_fixedname_init(&fnext);
  2635. next = dns_fixedname_name(&fnext);
  2636. dns_fixedname_init(&forigin);
  2637. origin = dns_fixedname_name(&forigin);
  2638. result = dns_rbtnodechain_next(chain, NULL, NULL);
  2639. while (result == ISC_R_SUCCESS || result == DNS_R_NEWORIGIN) {
  2640. node = NULL;
  2641. result = dns_rbtnodechain_current(chain, &prefix,
  2642. origin, &node);
  2643. if (result != ISC_R_SUCCESS)
  2644. break;
  2645. NODE_LOCK(&(rbtdb->node_locks[node->locknum].lock),
  2646. isc_rwlocktype_read);
  2647. for (header = node->data;
  2648. header != NULL;
  2649. header = header->next) {
  2650. if (header->serial <= search->serial &&
  2651. !IGNORE(header) && EXISTS(header))
  2652. break;
  2653. }
  2654. NODE_UNLOCK(&(rbtdb->node_locks[node->locknum].lock),
  2655. isc_rwlocktype_read);
  2656. if (header != NULL)
  2657. break;
  2658. result = dns_rbtnodechain_next(chain, NULL, NULL);
  2659. }
  2660. if (result == ISC_R_SUCCESS)
  2661. result = dns_name_concatenate(&prefix, origin, next, NULL);
  2662. if (result == ISC_R_SUCCESS && dns_name_issubdomain(next, name))
  2663. answer = ISC_TRUE;
  2664. return (answer);
  2665. }
  2666. static inline isc_boolean_t
  2667. activeemtpynode(rbtdb_search_t *search, dns_name_t *qname, dns_name_t *wname) {
  2668. dns_fixedname_t fnext;
  2669. dns_fixedname_t forigin;
  2670. dns_fixedname_t fprev;
  2671. dns_name_t *next;
  2672. dns_name_t *origin;
  2673. dns_name_t *prev;
  2674. dns_name_t name;
  2675. dns_name_t rname;
  2676. dns_name_t tname;
  2677. dns_rbtdb_t *rbtdb;
  2678. dns_rbtnode_t *node;
  2679. dns_rbtnodechain_t chain;
  2680. isc_boolean_t check_next = ISC_TRUE;
  2681. isc_boolean_t check_prev = ISC_TRUE;
  2682. isc_boolean_t answer = ISC_FALSE;
  2683. isc_result_t result;
  2684. rdatasetheader_t *header;
  2685. unsigned int n;
  2686. rbtdb = search->rbtdb;
  2687. dns_name_init(&name, NULL);
  2688. dns_name_init(&tname, NULL);
  2689. dns_name_init(&rname, NULL);
  2690. dns_fixedname_init(&fnext);
  2691. next = dns_fixedname_name(&fnext);
  2692. dns_fixedname_init(&fprev);
  2693. prev = dns_fixedname_name(&fprev);
  2694. dns_fixedname_init(&forigin);
  2695. origin = dns_fixedname_name(&forigin);
  2696. /*
  2697. * Find if qname is at or below a empty node.
  2698. * Use our own copy of the chain.
  2699. */
  2700. chain = search->chain;
  2701. do {
  2702. node = NULL;
  2703. result = dns_rbtnodechain_current(&chain, &name,
  2704. origin, &node);
  2705. if (result != ISC_R_SUCCESS)
  2706. break;
  2707. NODE_LOCK(&(rbtdb->node_locks[node->locknum].lock),
  2708. isc_rwlocktype_read);
  2709. for (header = node->data;
  2710. header != NULL;
  2711. header = header->next) {
  2712. if (header->serial <= search->serial &&
  2713. !IGNORE(header) && EXISTS(header))
  2714. break;
  2715. }
  2716. NODE_UNLOCK(&(rbtdb->node_locks[node->locknum].lock),
  2717. isc_rwlocktype_read);
  2718. if (header != NULL)
  2719. break;
  2720. result = dns_rbtnodechain_prev(&chain, NULL, NULL);
  2721. } while (result == ISC_R_SUCCESS || result == DNS_R_NEWORIGIN);
  2722. if (result == ISC_R_SUCCESS)
  2723. result = dns_name_concatenate(&name, origin, prev, NULL);
  2724. if (result != ISC_R_SUCCESS)
  2725. check_prev = ISC_FALSE;
  2726. result = dns_rbtnodechain_next(&chain, NULL, NULL);
  2727. while (result == ISC_R_SUCCESS || result == DNS_R_NEWORIGIN) {
  2728. node = NULL;
  2729. result = dns_rbtnodechain_current(&chain, &name,
  2730. origin, &node);
  2731. if (result != ISC_R_SUCCESS)
  2732. break;
  2733. NODE_LOCK(&(rbtdb->node_locks[node->locknum].lock),
  2734. isc_rwlocktype_read);
  2735. for (header = node->data;
  2736. header != NULL;
  2737. header = header->next) {
  2738. if (header->serial <= search->serial &&
  2739. !IGNORE(header) && EXISTS(header))
  2740. break;
  2741. }
  2742. NODE_UNLOCK(&(rbtdb->node_locks[node->locknum].lock),
  2743. isc_rwlocktype_read);
  2744. if (header != NULL)
  2745. break;
  2746. result = dns_rbtnodechain_next(&chain, NULL, NULL);
  2747. }
  2748. if (result == ISC_R_SUCCESS)
  2749. result = dns_name_concatenate(&name, origin, next, NULL);
  2750. if (result != ISC_R_SUCCESS)
  2751. check_next = ISC_FALSE;
  2752. dns_name_clone(qname, &rname);
  2753. /*
  2754. * Remove the wildcard label to find the terminal name.
  2755. */
  2756. n = dns_name_countlabels(wname);
  2757. dns_name_getlabelsequence(wname, 1, n - 1, &tname);
  2758. do {
  2759. if ((check_prev && dns_name_issubdomain(prev, &rname)) ||
  2760. (check_next && dns_name_issubdomain(next, &rname))) {
  2761. answer = ISC_TRUE;
  2762. break;
  2763. }
  2764. /*
  2765. * Remove the left hand label.
  2766. */
  2767. n = dns_name_countlabels(&rname);
  2768. dns_name_getlabelsequence(&rname, 1, n - 1, &rname);
  2769. } while (!dns_name_equal(&rname, &tname));
  2770. return (answer);
  2771. }
  2772. static inline isc_result_t
  2773. find_wildcard(rbtdb_search_t *search, dns_rbtnode_t **nodep,
  2774. dns_name_t *qname)
  2775. {
  2776. unsigned int i, j;
  2777. dns_rbtnode_t *node, *level_node, *wnode;
  2778. rdatasetheader_t *header;
  2779. isc_result_t result = ISC_R_NOTFOUND;
  2780. dns_name_t name;
  2781. dns_name_t *wname;
  2782. dns_fixedname_t fwname;
  2783. dns_rbtdb_t *rbtdb;
  2784. isc_boolean_t done, wild, active;
  2785. dns_rbtnodechain_t wchain;
  2786. /*
  2787. * Caller must be holding the tree lock and MUST NOT be holding
  2788. * any node locks.
  2789. */
  2790. /*
  2791. * Examine each ancestor level. If the level's wild bit
  2792. * is set, then construct the corresponding wildcard name and
  2793. * search for it. If the wildcard node exists, and is active in
  2794. * this version, we're done. If not, then we next check to see
  2795. * if the ancestor is active in this version. If so, then there
  2796. * can be no possible wildcard match and again we're done. If not,
  2797. * continue the search.
  2798. */
  2799. rbtdb = search->rbtdb;
  2800. i = search->chain.level_matches;
  2801. done = ISC_FALSE;
  2802. node = *nodep;
  2803. do {
  2804. NODE_LOCK(&(rbtdb->node_locks[node->locknum].lock),
  2805. isc_rwlocktype_read);
  2806. /*
  2807. * First we try to figure out if this node is active in
  2808. * the search's version. We do this now, even though we
  2809. * may not need the information, because it simplifies the
  2810. * locking and code flow.
  2811. */
  2812. for (header = node->data;
  2813. header != NULL;
  2814. header = header->next) {
  2815. if (header->serial <= search->serial &&
  2816. !IGNORE(header) && EXISTS(header))
  2817. break;
  2818. }
  2819. if (header != NULL)
  2820. active = ISC_TRUE;
  2821. else
  2822. active = ISC_FALSE;
  2823. if (node->wild)
  2824. wild = ISC_TRUE;
  2825. else
  2826. wild = ISC_FALSE;
  2827. NODE_UNLOCK(&(rbtdb->node_locks[node->locknum].lock),
  2828. isc_rwlocktype_read);
  2829. if (wild) {
  2830. /*
  2831. * Construct the wildcard name for this level.
  2832. */
  2833. dns_name_init(&name, NULL);
  2834. dns_rbt_namefromnode(node, &name);
  2835. dns_fixedname_init(&fwname);
  2836. wname = dns_fixedname_name(&fwname);
  2837. result = dns_name_concatenate(dns_wildcardname, &name,
  2838. wname, NULL);
  2839. j = i;
  2840. while (result == ISC_R_SUCCESS && j != 0) {
  2841. j--;
  2842. level_node = search->chain.levels[j];
  2843. dns_name_init(&name, NULL);
  2844. dns_rbt_namefromnode(level_node, &name);
  2845. result = dns_name_concatenate(wname,
  2846. &name,
  2847. wname,
  2848. NULL);
  2849. }
  2850. if (result != ISC_R_SUCCESS)
  2851. break;
  2852. wnode = NULL;
  2853. dns_rbtnodechain_init(&wchain, NULL);
  2854. result = dns_rbt_findnode(rbtdb->tree, wname,
  2855. NULL, &wnode, &wchain,
  2856. DNS_RBTFIND_EMPTYDATA,
  2857. NULL, NULL);
  2858. if (result == ISC_R_SUCCESS) {
  2859. nodelock_t *lock;
  2860. /*
  2861. * We have found the wildcard node. If it
  2862. * is active in the search's version, we're
  2863. * done.
  2864. */
  2865. lock = &rbtdb->node_locks[wnode->locknum].lock;
  2866. NODE_LOCK(lock, isc_rwlocktype_read);
  2867. for (header = wnode->data;
  2868. header != NULL;
  2869. header = header->next) {
  2870. if (header->serial <= search->serial &&
  2871. !IGNORE(header) && EXISTS(header))
  2872. break;
  2873. }
  2874. NODE_UNLOCK(lock, isc_rwlocktype_read);
  2875. if (header != NULL ||
  2876. activeempty(search, &wchain, wname)) {
  2877. if (activeemtpynode(search, qname,
  2878. wname)) {
  2879. return (ISC_R_NOTFOUND);
  2880. }
  2881. /*
  2882. * The wildcard node is active!
  2883. *
  2884. * Note: result is still ISC_R_SUCCESS
  2885. * so we don't have to set it.
  2886. */
  2887. *nodep = wnode;
  2888. break;
  2889. }
  2890. } else if (result != ISC_R_NOTFOUND &&
  2891. result != DNS_R_PARTIALMATCH) {
  2892. /*
  2893. * An error has occurred. Bail out.
  2894. */
  2895. break;
  2896. }
  2897. }
  2898. if (active) {
  2899. /*
  2900. * The level node is active. Any wildcarding
  2901. * present at higher levels has no
  2902. * effect and we're done.
  2903. */
  2904. result = ISC_R_NOTFOUND;
  2905. break;
  2906. }
  2907. if (i > 0) {
  2908. i--;
  2909. node = search->chain.levels[i];
  2910. } else
  2911. done = ISC_TRUE;
  2912. } while (!done);
  2913. return (result);
  2914. }
  2915. static isc_boolean_t
  2916. matchparams(rdatasetheader_t *header, rbtdb_search_t *search)
  2917. {
  2918. dns_rdata_t rdata = DNS_RDATA_INIT;
  2919. dns_rdata_nsec3_t nsec3;
  2920. unsigned char *raw; /* RDATASLAB */
  2921. unsigned int rdlen, count;
  2922. isc_region_t region;
  2923. isc_result_t result;
  2924. REQUIRE(header->type == dns_rdatatype_nsec3);
  2925. raw = (unsigned char *)header + sizeof(*header);
  2926. count = raw[0] * 256 + raw[1]; /* count */
  2927. #if DNS_RDATASET_FIXED
  2928. raw += count * 4 + 2;
  2929. #else
  2930. raw += 2;
  2931. #endif
  2932. while (count-- > 0) {
  2933. rdlen = raw[0] * 256 + raw[1];
  2934. #if DNS_RDATASET_FIXED
  2935. raw += 4;
  2936. #else
  2937. raw += 2;
  2938. #endif
  2939. region.base = raw;
  2940. region.length = rdlen;
  2941. dns_rdata_fromregion(&rdata, search->rbtdb->common.rdclass,
  2942. dns_rdatatype_nsec3, &region);
  2943. raw += rdlen;
  2944. result = dns_rdata_tostruct(&rdata, &nsec3, NULL);
  2945. INSIST(result == ISC_R_SUCCESS);
  2946. if (nsec3.hash == search->rbtversion->hash &&
  2947. nsec3.iterations == search->rbtversion->iterations &&
  2948. nsec3.salt_length == search->rbtversion->salt_length &&
  2949. memcmp(nsec3.salt, search->rbtversion->salt,
  2950. nsec3.salt_length) == 0)
  2951. return (ISC_TRUE);
  2952. dns_rdata_reset(&rdata);
  2953. }
  2954. return (ISC_FALSE);
  2955. }
  2956. /*
  2957. * Find node of the NSEC/NSEC3 record that is 'name'.
  2958. */
  2959. static inline isc_result_t
  2960. previous_closest_nsec(dns_rdatatype_t type, rbtdb_search_t *search,
  2961. dns_name_t *name, dns_name_t *origin,
  2962. dns_rbtnode_t **nodep, dns_rbtnodechain_t *nsecchain,
  2963. isc_boolean_t *firstp)
  2964. {
  2965. dns_fixedname_t ftarget;
  2966. dns_name_t *target;
  2967. dns_rbtnode_t *nsecnode;
  2968. isc_result_t result;
  2969. REQUIRE(nodep != NULL && *nodep == NULL);
  2970. if (type == dns_rdatatype_nsec3) {
  2971. result = dns_rbtnodechain_prev(&search->chain, NULL, NULL);
  2972. if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN)
  2973. return (result);
  2974. result = dns_rbtnodechain_current(&search->chain, name, origin,
  2975. nodep);
  2976. return (result);
  2977. }
  2978. dns_fixedname_init(&ftarget);
  2979. target = dns_fixedname_name(&ftarget);
  2980. for (;;) {
  2981. if (*firstp) {
  2982. /*
  2983. * Construct the name of the second node to check.
  2984. * It is the first node sought in the NSEC tree.
  2985. */
  2986. *firstp = ISC_FALSE;
  2987. dns_rbtnodechain_init(nsecchain, NULL);
  2988. result = dns_name_concatenate(name, origin,
  2989. target, NULL);
  2990. if (result != ISC_R_SUCCESS)
  2991. return (result);
  2992. nsecnode = NULL;
  2993. result = dns_rbt_findnode(search->rbtdb->nsec,
  2994. target, NULL,
  2995. &nsecnode, nsecchain,
  2996. DNS_RBTFIND_NOOPTIONS,
  2997. NULL, NULL);
  2998. if (result == ISC_R_SUCCESS) {
  2999. /*
  3000. * Since this was the first loop, finding the
  3001. * name in the NSEC tree implies that the first
  3002. * node checked in the main tree had an
  3003. * unacceptable NSEC record.
  3004. * Try the previous node in the NSEC tree.
  3005. */
  3006. result = dns_rbtnodechain_prev(nsecchain,
  3007. name, origin);
  3008. if (result == DNS_R_NEWORIGIN)
  3009. result = ISC_R_SUCCESS;
  3010. } else if (result == ISC_R_NOTFOUND ||
  3011. result == DNS_R_PARTIALMATCH) {
  3012. result = dns_rbtnodechain_current(nsecchain,
  3013. name, origin, NULL);
  3014. if (result == ISC_R_NOTFOUND)
  3015. result = ISC_R_NOMORE;
  3016. }
  3017. } else {
  3018. /*
  3019. * This is a second or later trip through the auxiliary
  3020. * tree for the name of a third or earlier NSEC node in
  3021. * the main tree. Previous trips through the NSEC tree
  3022. * must have found nodes in the main tree with NSEC
  3023. * records. Perhaps they lacked signature records.
  3024. */
  3025. result = dns_rbtnodechain_prev(nsecchain, name, origin);
  3026. if (result == DNS_R_NEWORIGIN)
  3027. result = ISC_R_SUCCESS;
  3028. }
  3029. if (result != ISC_R_SUCCESS)
  3030. return (result);
  3031. /*
  3032. * Construct the name to seek in the main tree.
  3033. */
  3034. result = dns_name_concatenate(name, origin, target, NULL);
  3035. if (result != ISC_R_SUCCESS)
  3036. return (result);
  3037. *nodep = NULL;
  3038. result = dns_rbt_findnode(search->rbtdb->tree, target, NULL,
  3039. nodep, &search->chain,
  3040. DNS_RBTFIND_NOOPTIONS, NULL, NULL);
  3041. if (result == ISC_R_SUCCESS)
  3042. return (result);
  3043. /*
  3044. * There should always be a node in the main tree with the
  3045. * same name as the node in the auxiliary NSEC tree, except for
  3046. * nodes in the auxiliary tree that are awaiting deletion.
  3047. */
  3048. if (result != DNS_R_PARTIALMATCH && result != ISC_R_NOTFOUND) {
  3049. isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
  3050. DNS_LOGMODULE_CACHE, ISC_LOG_ERROR,
  3051. "previous_closest_nsec(): %s",
  3052. isc_result_totext(result));
  3053. return (DNS_R_BADDB);
  3054. }
  3055. }
  3056. }
  3057. /*
  3058. * Find the NSEC/NSEC3 which is or before the current point on the
  3059. * search chain. For NSEC3 records only NSEC3 records that match the
  3060. * current NSEC3PARAM record are considered.
  3061. */
  3062. static inline isc_result_t
  3063. find_closest_nsec(rbtdb_search_t *search, dns_dbnode_t **nodep,
  3064. dns_name_t *foundname, dns_rdataset_t *rdataset,
  3065. dns_rdataset_t *sigrdataset, dns_rbt_t *tree,
  3066. dns_db_secure_t secure)
  3067. {
  3068. dns_rbtnode_t *node, *prevnode;
  3069. rdatasetheader_t *header, *header_next, *found, *foundsig;
  3070. dns_rbtnodechain_t nsecchain;
  3071. isc_boolean_t empty_node;
  3072. isc_result_t result;
  3073. dns_fixedname_t fname, forigin;
  3074. dns_name_t *name, *origin;
  3075. dns_rdatatype_t type;
  3076. rbtdb_rdatatype_t sigtype;
  3077. isc_boolean_t wraps;
  3078. isc_boolean_t first = ISC_TRUE;
  3079. isc_boolean_t need_sig = ISC_TF(secure == dns_db_secure);
  3080. if (tree == search->rbtdb->nsec3) {
  3081. type = dns_rdatatype_nsec3;
  3082. sigtype = RBTDB_RDATATYPE_SIGNSEC3;
  3083. wraps = ISC_TRUE;
  3084. } else {
  3085. type = dns_rdatatype_nsec;
  3086. sigtype = RBTDB_RDATATYPE_SIGNSEC;
  3087. wraps = ISC_FALSE;
  3088. }
  3089. /*
  3090. * Use the auxiliary tree only starting with the second node in the
  3091. * hope that the original node will be right much of the time.
  3092. */
  3093. dns_fixedname_init(&fname);
  3094. name = dns_fixedname_name(&fname);
  3095. dns_fixedname_init(&forigin);
  3096. origin = dns_fixedname_name(&forigin);
  3097. again:
  3098. node = NULL;
  3099. prevnode = NULL;
  3100. result = dns_rbtnodechain_current(&search->chain, name, origin, &node);
  3101. if (result != ISC_R_SUCCESS)
  3102. return (result);
  3103. do {
  3104. NODE_LOCK(&(search->rbtdb->node_locks[node->locknum].lock),
  3105. isc_rwlocktype_read);
  3106. found = NULL;
  3107. foundsig = NULL;
  3108. empty_node = ISC_TRUE;
  3109. for (header = node->data;
  3110. header != NULL;
  3111. header = header_next) {
  3112. header_next = header->next;
  3113. /*
  3114. * Look for an active, extant NSEC or RRSIG NSEC.
  3115. */
  3116. do {
  3117. if (header->serial <= search->serial &&
  3118. !IGNORE(header)) {
  3119. /*
  3120. * Is this a "this rdataset doesn't
  3121. * exist" record?
  3122. */
  3123. if (NONEXISTENT(header))
  3124. header = NULL;
  3125. break;
  3126. } else
  3127. header = header->down;
  3128. } while (header != NULL);
  3129. if (header != NULL) {
  3130. /*
  3131. * We now know that there is at least one
  3132. * active rdataset at this node.
  3133. */
  3134. empty_node = ISC_FALSE;
  3135. if (header->type == type) {
  3136. found = header;
  3137. if (foundsig != NULL)
  3138. break;
  3139. } else if (header->type == sigtype) {
  3140. foundsig = header;
  3141. if (found != NULL)
  3142. break;
  3143. }
  3144. }
  3145. }
  3146. if (!empty_node) {
  3147. if (found != NULL && search->rbtversion->havensec3 &&
  3148. found->type == dns_rdatatype_nsec3 &&
  3149. !matchparams(found, search)) {
  3150. empty_node = ISC_TRUE;
  3151. found = NULL;
  3152. foundsig = NULL;
  3153. result = previous_closest_nsec(type, search,
  3154. name, origin,
  3155. &prevnode, NULL,
  3156. NULL);
  3157. } else if (found != NULL &&
  3158. (foundsig != NULL || !need_sig)) {
  3159. /*
  3160. * We've found the right NSEC/NSEC3 record.
  3161. *
  3162. * Note: for this to really be the right
  3163. * NSEC record, it's essential that the NSEC
  3164. * records of any nodes obscured by a zone
  3165. * cut have been removed; we assume this is
  3166. * the case.
  3167. */
  3168. result = dns_name_concatenate(name, origin,
  3169. foundname, NULL);
  3170. if (result == ISC_R_SUCCESS) {
  3171. if (nodep != NULL) {
  3172. new_reference(search->rbtdb,
  3173. node);
  3174. *nodep = node;
  3175. }
  3176. bind_rdataset(search->rbtdb, node,
  3177. found, search->now,
  3178. rdataset);
  3179. if (foundsig != NULL)
  3180. bind_rdataset(search->rbtdb,
  3181. node,
  3182. foundsig,
  3183. search->now,
  3184. sigrdataset);
  3185. }
  3186. } else if (found == NULL && foundsig == NULL) {
  3187. /*
  3188. * This node is active, but has no NSEC or
  3189. * RRSIG NSEC. That means it's glue or
  3190. * other obscured zone data that isn't
  3191. * relevant for our search. Treat the
  3192. * node as if it were empty and keep looking.
  3193. */
  3194. empty_node = ISC_TRUE;
  3195. result = previous_closest_nsec(type, search,
  3196. name, origin,
  3197. &prevnode,
  3198. &nsecchain,
  3199. &first);
  3200. } else {
  3201. /*
  3202. * We found an active node, but either the
  3203. * NSEC or the RRSIG NSEC is missing. This
  3204. * shouldn't happen.
  3205. */
  3206. result = DNS_R_BADDB;
  3207. }
  3208. } else {
  3209. /*
  3210. * This node isn't active. We've got to keep
  3211. * looking.
  3212. */
  3213. result = previous_closest_nsec(type, search,
  3214. name, origin, &prevnode,
  3215. &nsecchain, &first);
  3216. }
  3217. NODE_UNLOCK(&(search->rbtdb->node_locks[node->locknum].lock),
  3218. isc_rwlocktype_read);
  3219. node = prevnode;
  3220. prevnode = NULL;
  3221. } while (empty_node && result == ISC_R_SUCCESS);
  3222. if (!first)
  3223. dns_rbtnodechain_invalidate(&nsecchain);
  3224. if (result == ISC_R_NOMORE && wraps) {
  3225. result = dns_rbtnodechain_last(&search->chain, tree,
  3226. NULL, NULL);
  3227. if (result == ISC_R_SUCCESS || result == DNS_R_NEWORIGIN) {
  3228. wraps = ISC_FALSE;
  3229. goto again;
  3230. }
  3231. }
  3232. /*
  3233. * If the result is ISC_R_NOMORE, then we got to the beginning of
  3234. * the database and didn't find a NSEC record. This shouldn't
  3235. * happen.
  3236. */
  3237. if (result == ISC_R_NOMORE)
  3238. result = DNS_R_BADDB;
  3239. return (result);
  3240. }
  3241. static isc_result_t
  3242. zone_find(dns_db_t *db, dns_name_t *name, dns_dbversion_t *version,
  3243. dns_rdatatype_t type, unsigned int options, isc_stdtime_t now,
  3244. dns_dbnode_t **nodep, dns_name_t *foundname,
  3245. dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
  3246. {
  3247. dns_rbtnode_t *node = NULL;
  3248. isc_result_t result;
  3249. rbtdb_search_t search;
  3250. isc_boolean_t cname_ok = ISC_TRUE;
  3251. isc_boolean_t close_version = ISC_FALSE;
  3252. isc_boolean_t maybe_zonecut = ISC_FALSE;
  3253. isc_boolean_t at_zonecut = ISC_FALSE;
  3254. isc_boolean_t wild;
  3255. isc_boolean_t empty_node;
  3256. rdatasetheader_t *header, *header_next, *found, *nsecheader;
  3257. rdatasetheader_t *foundsig, *cnamesig, *nsecsig;
  3258. rbtdb_rdatatype_t sigtype;
  3259. isc_boolean_t active;
  3260. dns_rbtnodechain_t chain;
  3261. nodelock_t *lock;
  3262. dns_rbt_t *tree;
  3263. search.rbtdb = (dns_rbtdb_t *)db;
  3264. REQUIRE(VALID_RBTDB(search.rbtdb));
  3265. INSIST(version == NULL ||
  3266. ((rbtdb_version_t *)version)->rbtdb == (dns_rbtdb_t *)db);
  3267. /*
  3268. * We don't care about 'now'.
  3269. */
  3270. UNUSED(now);
  3271. /*
  3272. * If the caller didn't supply a version, attach to the current
  3273. * version.
  3274. */
  3275. if (version == NULL) {
  3276. currentversion(db, &version);
  3277. close_version = ISC_TRUE;
  3278. }
  3279. search.rbtversion = version;
  3280. search.serial = search.rbtversion->serial;
  3281. search.options = options;
  3282. search.copy_name = ISC_FALSE;
  3283. search.need_cleanup = ISC_FALSE;
  3284. search.wild = ISC_FALSE;
  3285. search.zonecut = NULL;
  3286. dns_fixedname_init(&search.zonecut_name);
  3287. dns_rbtnodechain_init(&search.chain, search.rbtdb->common.mctx);
  3288. search.now = 0;
  3289. /*
  3290. * 'wild' will be true iff. we've matched a wildcard.
  3291. */
  3292. wild = ISC_FALSE;
  3293. RWLOCK(&search.rbtdb->tree_lock, isc_rwlocktype_read);
  3294. /*
  3295. * Search down from the root of the tree. If, while going down, we
  3296. * encounter a callback node, zone_zonecut_callback() will search the
  3297. * rdatasets at the zone cut for active DNAME or NS rdatasets.
  3298. */
  3299. tree = (options & DNS_DBFIND_FORCENSEC3) != 0 ? search.rbtdb->nsec3 :
  3300. search.rbtdb->tree;
  3301. result = dns_rbt_findnode(tree, name, foundname, &node,
  3302. &search.chain, DNS_RBTFIND_EMPTYDATA,
  3303. zone_zonecut_callback, &search);
  3304. if (result == DNS_R_PARTIALMATCH) {
  3305. partial_match:
  3306. if (search.zonecut != NULL) {
  3307. result = setup_delegation(&search, nodep, foundname,
  3308. rdataset, sigrdataset);
  3309. goto tree_exit;
  3310. }
  3311. if (search.wild) {
  3312. /*
  3313. * At least one of the levels in the search chain
  3314. * potentially has a wildcard. For each such level,
  3315. * we must see if there's a matching wildcard active
  3316. * in the current version.
  3317. */
  3318. result = find_wildcard(&search, &node, name);
  3319. if (result == ISC_R_SUCCESS) {
  3320. result = dns_name_copy(name, foundname, NULL);
  3321. if (result != ISC_R_SUCCESS)
  3322. goto tree_exit;
  3323. wild = ISC_TRUE;
  3324. goto found;
  3325. }
  3326. else if (result != ISC_R_NOTFOUND)
  3327. goto tree_exit;
  3328. }
  3329. chain = search.chain;
  3330. active = activeempty(&search, &chain, name);
  3331. /*
  3332. * If we're here, then the name does not exist, is not
  3333. * beneath a zonecut, and there's no matching wildcard.
  3334. */
  3335. if ((search.rbtversion->secure == dns_db_secure &&
  3336. !search.rbtversion->havensec3) ||
  3337. (search.options & DNS_DBFIND_FORCENSEC) != 0 ||
  3338. (search.options & DNS_DBFIND_FORCENSEC3) != 0)
  3339. {
  3340. result = find_closest_nsec(&search, nodep, foundname,
  3341. rdataset, sigrdataset, tree,
  3342. search.rbtversion->secure);
  3343. if (result == ISC_R_SUCCESS)
  3344. result = active ? DNS_R_EMPTYNAME :
  3345. DNS_R_NXDOMAIN;
  3346. } else
  3347. result = active ? DNS_R_EMPTYNAME : DNS_R_NXDOMAIN;
  3348. goto tree_exit;
  3349. } else if (result != ISC_R_SUCCESS)
  3350. goto tree_exit;
  3351. found:
  3352. /*
  3353. * We have found a node whose name is the desired name, or we
  3354. * have matched a wildcard.
  3355. */
  3356. if (search.zonecut != NULL) {
  3357. /*
  3358. * If we're beneath a zone cut, we don't want to look for
  3359. * CNAMEs because they're not legitimate zone glue.
  3360. */
  3361. cname_ok = ISC_FALSE;
  3362. } else {
  3363. /*
  3364. * The node may be a zone cut itself. If it might be one,
  3365. * make sure we check for it later.
  3366. *
  3367. * DS records live above the zone cut in ordinary zone so
  3368. * we want to ignore any referral.
  3369. *
  3370. * Stub zones don't have anything "above" the delgation so
  3371. * we always return a referral.
  3372. */
  3373. if (node->find_callback &&
  3374. ((node != search.rbtdb->origin_node &&
  3375. !dns_rdatatype_atparent(type)) ||
  3376. IS_STUB(search.rbtdb)))
  3377. maybe_zonecut = ISC_TRUE;
  3378. }
  3379. /*
  3380. * Certain DNSSEC types are not subject to CNAME matching
  3381. * (RFC4035, section 2.5 and RFC3007).
  3382. *
  3383. * We don't check for RRSIG, because we don't store RRSIG records
  3384. * directly.
  3385. */
  3386. if (type == dns_rdatatype_key || type == dns_rdatatype_nsec)
  3387. cname_ok = ISC_FALSE;
  3388. /*
  3389. * We now go looking for rdata...
  3390. */
  3391. lock = &search.rbtdb->node_locks[node->locknum].lock;
  3392. NODE_LOCK(lock, isc_rwlocktype_read);
  3393. found = NULL;
  3394. foundsig = NULL;
  3395. sigtype = RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig, type);
  3396. nsecheader = NULL;
  3397. nsecsig = NULL;
  3398. cnamesig = NULL;
  3399. empty_node = ISC_TRUE;
  3400. for (header = node->data; header != NULL; header = header_next) {
  3401. header_next = header->next;
  3402. /*
  3403. * Look for an active, extant rdataset.
  3404. */
  3405. do {
  3406. if (header->serial <= search.serial &&
  3407. !IGNORE(header)) {
  3408. /*
  3409. * Is this a "this rdataset doesn't
  3410. * exist" record?
  3411. */
  3412. if (NONEXISTENT(header))
  3413. header = NULL;
  3414. break;
  3415. } else
  3416. header = header->down;
  3417. } while (header != NULL);
  3418. if (header != NULL) {
  3419. /*
  3420. * We now know that there is at least one active
  3421. * rdataset at this node.
  3422. */
  3423. empty_node = ISC_FALSE;
  3424. /*
  3425. * Do special zone cut handling, if requested.
  3426. */
  3427. if (maybe_zonecut &&
  3428. header->type == dns_rdatatype_ns) {
  3429. /*
  3430. * We increment the reference count on node to
  3431. * ensure that search->zonecut_rdataset will
  3432. * still be valid later.
  3433. */
  3434. new_reference(search.rbtdb, node);
  3435. search.zonecut = node;
  3436. search.zonecut_rdataset = header;
  3437. search.zonecut_sigrdataset = NULL;
  3438. search.need_cleanup = ISC_TRUE;
  3439. maybe_zonecut = ISC_FALSE;
  3440. at_zonecut = ISC_TRUE;
  3441. /*
  3442. * It is not clear if KEY should still be
  3443. * allowed at the parent side of the zone
  3444. * cut or not. It is needed for RFC3007
  3445. * validated updates.
  3446. */
  3447. if ((search.options & DNS_DBFIND_GLUEOK) == 0
  3448. && type != dns_rdatatype_nsec
  3449. && type != dns_rdatatype_key) {
  3450. /*
  3451. * Glue is not OK, but any answer we
  3452. * could return would be glue. Return
  3453. * the delegation.
  3454. */
  3455. found = NULL;
  3456. break;
  3457. }
  3458. if (found != NULL && foundsig != NULL)
  3459. break;
  3460. }
  3461. /*
  3462. * If the NSEC3 record doesn't match the chain
  3463. * we are using behave as if it isn't here.
  3464. */
  3465. if (header->type == dns_rdatatype_nsec3 &&
  3466. !matchparams(header, &search)) {
  3467. NODE_UNLOCK(lock, isc_rwlocktype_read);
  3468. goto partial_match;
  3469. }
  3470. /*
  3471. * If we found a type we were looking for,
  3472. * remember it.
  3473. */
  3474. if (header->type == type ||
  3475. type == dns_rdatatype_any ||
  3476. (header->type == dns_rdatatype_cname &&
  3477. cname_ok)) {
  3478. /*
  3479. * We've found the answer!
  3480. */
  3481. found = header;
  3482. if (header->type == dns_rdatatype_cname &&
  3483. cname_ok) {
  3484. /*
  3485. * We may be finding a CNAME instead
  3486. * of the desired type.
  3487. *
  3488. * If we've already got the CNAME RRSIG,
  3489. * use it, otherwise change sigtype
  3490. * so that we find it.
  3491. */
  3492. if (cnamesig != NULL)
  3493. foundsig = cnamesig;
  3494. else
  3495. sigtype =
  3496. RBTDB_RDATATYPE_SIGCNAME;
  3497. }
  3498. /*
  3499. * If we've got all we need, end the search.
  3500. */
  3501. if (!maybe_zonecut && foundsig != NULL)
  3502. break;
  3503. } else if (header->type == sigtype) {
  3504. /*
  3505. * We've found the RRSIG rdataset for our
  3506. * target type. Remember it.
  3507. */
  3508. foundsig = header;
  3509. /*
  3510. * If we've got all we need, end the search.
  3511. */
  3512. if (!maybe_zonecut && found != NULL)
  3513. break;
  3514. } else if (header->type == dns_rdatatype_nsec &&
  3515. !search.rbtversion->havensec3) {
  3516. /*
  3517. * Remember a NSEC rdataset even if we're
  3518. * not specifically looking for it, because
  3519. * we might need it later.
  3520. */
  3521. nsecheader = header;
  3522. } else if (header->type == RBTDB_RDATATYPE_SIGNSEC &&
  3523. !search.rbtversion->havensec3) {
  3524. /*
  3525. * If we need the NSEC rdataset, we'll also
  3526. * need its signature.
  3527. */
  3528. nsecsig = header;
  3529. } else if (cname_ok &&
  3530. header->type == RBTDB_RDATATYPE_SIGCNAME) {
  3531. /*
  3532. * If we get a CNAME match, we'll also need
  3533. * its signature.
  3534. */
  3535. cnamesig = header;
  3536. }
  3537. }
  3538. }
  3539. if (empty_node) {
  3540. /*
  3541. * We have an exact match for the name, but there are no
  3542. * active rdatasets in the desired version. That means that
  3543. * this node doesn't exist in the desired version, and that
  3544. * we really have a partial match.
  3545. */
  3546. if (!wild) {
  3547. NODE_UNLOCK(lock, isc_rwlocktype_read);
  3548. goto partial_match;
  3549. }
  3550. }
  3551. /*
  3552. * If we didn't find what we were looking for...
  3553. */
  3554. if (found == NULL) {
  3555. if (search.zonecut != NULL) {
  3556. /*
  3557. * We were trying to find glue at a node beneath a
  3558. * zone cut, but didn't.
  3559. *
  3560. * Return the delegation.
  3561. */
  3562. NODE_UNLOCK(lock, isc_rwlocktype_read);
  3563. result = setup_delegation(&search, nodep, foundname,
  3564. rdataset, sigrdataset);
  3565. goto tree_exit;
  3566. }
  3567. /*
  3568. * The desired type doesn't exist.
  3569. */
  3570. result = DNS_R_NXRRSET;
  3571. if (search.rbtversion->secure == dns_db_secure &&
  3572. !search.rbtversion->havensec3 &&
  3573. (nsecheader == NULL || nsecsig == NULL)) {
  3574. /*
  3575. * The zone is secure but there's no NSEC,
  3576. * or the NSEC has no signature!
  3577. */
  3578. if (!wild) {
  3579. result = DNS_R_BADDB;
  3580. goto node_exit;
  3581. }
  3582. NODE_UNLOCK(lock, isc_rwlocktype_read);
  3583. result = find_closest_nsec(&search, nodep, foundname,
  3584. rdataset, sigrdataset,
  3585. search.rbtdb->tree,
  3586. search.rbtversion->secure);
  3587. if (result == ISC_R_SUCCESS)
  3588. result = DNS_R_EMPTYWILD;
  3589. goto tree_exit;
  3590. }
  3591. if ((search.options & DNS_DBFIND_FORCENSEC) != 0 &&
  3592. nsecheader == NULL)
  3593. {
  3594. /*
  3595. * There's no NSEC record, and we were told
  3596. * to find one.
  3597. */
  3598. result = DNS_R_BADDB;
  3599. goto node_exit;
  3600. }
  3601. if (nodep != NULL) {
  3602. new_reference(search.rbtdb, node);
  3603. *nodep = node;
  3604. }
  3605. if ((search.rbtversion->secure == dns_db_secure &&
  3606. !search.rbtversion->havensec3) ||
  3607. (search.options & DNS_DBFIND_FORCENSEC) != 0)
  3608. {
  3609. bind_rdataset(search.rbtdb, node, nsecheader,
  3610. 0, rdataset);
  3611. if (nsecsig != NULL)
  3612. bind_rdataset(search.rbtdb, node,
  3613. nsecsig, 0, sigrdataset);
  3614. }
  3615. if (wild)
  3616. foundname->attributes |= DNS_NAMEATTR_WILDCARD;
  3617. goto node_exit;
  3618. }
  3619. /*
  3620. * We found what we were looking for, or we found a CNAME.
  3621. */
  3622. if (type != found->type &&
  3623. type != dns_rdatatype_any &&
  3624. found->type == dns_rdatatype_cname) {
  3625. /*
  3626. * We weren't doing an ANY query and we found a CNAME instead
  3627. * of the type we were looking for, so we need to indicate
  3628. * that result to the caller.
  3629. */
  3630. result = DNS_R_CNAME;
  3631. } else if (search.zonecut != NULL) {
  3632. /*
  3633. * If we're beneath a zone cut, we must indicate that the
  3634. * result is glue, unless we're actually at the zone cut
  3635. * and the type is NSEC or KEY.
  3636. */
  3637. if (search.zonecut == node) {
  3638. /*
  3639. * It is not clear if KEY should still be
  3640. * allowed at the parent side of the zone
  3641. * cut or not. It is needed for RFC3007
  3642. * validated updates.
  3643. */
  3644. if (type == dns_rdatatype_nsec ||
  3645. type == dns_rdatatype_nsec3 ||
  3646. type == dns_rdatatype_key)
  3647. result = ISC_R_SUCCESS;
  3648. else if (type == dns_rdatatype_any)
  3649. result = DNS_R_ZONECUT;
  3650. else
  3651. result = DNS_R_GLUE;
  3652. } else
  3653. result = DNS_R_GLUE;
  3654. /*
  3655. * We might have found data that isn't glue, but was occluded
  3656. * by a dynamic update. If the caller cares about this, they
  3657. * will have told us to validate glue.
  3658. *
  3659. * XXX We should cache the glue validity state!
  3660. */
  3661. if (result == DNS_R_GLUE &&
  3662. (search.options & DNS_DBFIND_VALIDATEGLUE) != 0 &&
  3663. !valid_glue(&search, foundname, type, node)) {
  3664. NODE_UNLOCK(lock, isc_rwlocktype_read);
  3665. result = setup_delegation(&search, nodep, foundname,
  3666. rdataset, sigrdataset);
  3667. goto tree_exit;
  3668. }
  3669. } else {
  3670. /*
  3671. * An ordinary successful query!
  3672. */
  3673. result = ISC_R_SUCCESS;
  3674. }
  3675. if (nodep != NULL) {
  3676. if (!at_zonecut)
  3677. new_reference(search.rbtdb, node);
  3678. else
  3679. search.need_cleanup = ISC_FALSE;
  3680. *nodep = node;
  3681. }
  3682. if (type != dns_rdatatype_any) {
  3683. bind_rdataset(search.rbtdb, node, found, 0, rdataset);
  3684. if (foundsig != NULL)
  3685. bind_rdataset(search.rbtdb, node, foundsig, 0,
  3686. sigrdataset);
  3687. }
  3688. if (wild)
  3689. foundname->attributes |= DNS_NAMEATTR_WILDCARD;
  3690. node_exit:
  3691. NODE_UNLOCK(lock, isc_rwlocktype_read);
  3692. tree_exit:
  3693. RWUNLOCK(&search.rbtdb->tree_lock, isc_rwlocktype_read);
  3694. /*
  3695. * If we found a zonecut but aren't going to use it, we have to
  3696. * let go of it.
  3697. */
  3698. if (search.need_cleanup) {
  3699. node = search.zonecut;
  3700. INSIST(node != NULL);
  3701. lock = &(search.rbtdb->node_locks[node->locknum].lock);
  3702. NODE_LOCK(lock, isc_rwlocktype_read);
  3703. decrement_reference(search.rbtdb, node, 0,
  3704. isc_rwlocktype_read, isc_rwlocktype_none,
  3705. ISC_FALSE);
  3706. NODE_UNLOCK(lock, isc_rwlocktype_read);
  3707. }
  3708. if (close_version)
  3709. closeversion(db, &version, ISC_FALSE);
  3710. dns_rbtnodechain_reset(&search.chain);
  3711. return (result);
  3712. }
  3713. static isc_result_t
  3714. zone_findzonecut(dns_db_t *db, dns_name_t *name, unsigned int options,
  3715. isc_stdtime_t now, dns_dbnode_t **nodep,
  3716. dns_name_t *foundname,
  3717. dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
  3718. {
  3719. UNUSED(db);
  3720. UNUSED(name);
  3721. UNUSED(options);
  3722. UNUSED(now);
  3723. UNUSED(nodep);
  3724. UNUSED(foundname);
  3725. UNUSED(rdataset);
  3726. UNUSED(sigrdataset);
  3727. FATAL_ERROR(__FILE__, __LINE__, "zone_findzonecut() called!");
  3728. /* NOTREACHED */
  3729. return (ISC_R_NOTIMPLEMENTED);
  3730. }
  3731. static isc_result_t
  3732. cache_zonecut_callback(dns_rbtnode_t *node, dns_name_t *name, void *arg) {
  3733. rbtdb_search_t *search = arg;
  3734. rdatasetheader_t *header, *header_prev, *header_next;
  3735. rdatasetheader_t *dname_header, *sigdname_header;
  3736. isc_result_t result;
  3737. nodelock_t *lock;
  3738. isc_rwlocktype_t locktype;
  3739. /* XXX comment */
  3740. REQUIRE(search->zonecut == NULL);
  3741. /*
  3742. * Keep compiler silent.
  3743. */
  3744. UNUSED(name);
  3745. lock = &(search->rbtdb->node_locks[node->locknum].lock);
  3746. locktype = isc_rwlocktype_read;
  3747. NODE_LOCK(lock, locktype);
  3748. /*
  3749. * Look for a DNAME or RRSIG DNAME rdataset.
  3750. */
  3751. dname_header = NULL;
  3752. sigdname_header = NULL;
  3753. header_prev = NULL;
  3754. for (header = node->data; header != NULL; header = header_next) {
  3755. header_next = header->next;
  3756. if (header->rdh_ttl <= search->now) {
  3757. /*
  3758. * This rdataset is stale. If no one else is
  3759. * using the node, we can clean it up right
  3760. * now, otherwise we mark it as stale, and
  3761. * the node as dirty, so it will get cleaned
  3762. * up later.
  3763. */
  3764. if ((header->rdh_ttl <= search->now - RBTDB_VIRTUAL) &&
  3765. (locktype == isc_rwlocktype_write ||
  3766. NODE_TRYUPGRADE(lock) == ISC_R_SUCCESS)) {
  3767. /*
  3768. * We update the node's status only when we
  3769. * can get write access; otherwise, we leave
  3770. * others to this work. Periodical cleaning
  3771. * will eventually take the job as the last
  3772. * resort.
  3773. * We won't downgrade the lock, since other
  3774. * rdatasets are probably stale, too.
  3775. */
  3776. locktype = isc_rwlocktype_write;
  3777. if (dns_rbtnode_refcurrent(node) == 0) {
  3778. isc_mem_t *mctx;
  3779. /*
  3780. * header->down can be non-NULL if the
  3781. * refcount has just decremented to 0
  3782. * but decrement_reference() has not
  3783. * performed clean_cache_node(), in
  3784. * which case we need to purge the
  3785. * stale headers first.
  3786. */
  3787. mctx = search->rbtdb->common.mctx;
  3788. clean_stale_headers(search->rbtdb,
  3789. mctx,
  3790. header);
  3791. if (header_prev != NULL)
  3792. header_prev->next =
  3793. header->next;
  3794. else
  3795. node->data = header->next;
  3796. free_rdataset(search->rbtdb, mctx,
  3797. header);
  3798. } else {
  3799. header->attributes |=
  3800. RDATASET_ATTR_STALE;
  3801. node->dirty = 1;
  3802. header_prev = header;
  3803. }
  3804. } else
  3805. header_prev = header;
  3806. } else if (header->type == dns_rdatatype_dname &&
  3807. EXISTS(header)) {
  3808. dname_header = header;
  3809. header_prev = header;
  3810. } else if (header->type == RBTDB_RDATATYPE_SIGDNAME &&
  3811. EXISTS(header)) {
  3812. sigdname_header = header;
  3813. header_prev = header;
  3814. } else
  3815. header_prev = header;
  3816. }
  3817. if (dname_header != NULL &&
  3818. (!DNS_TRUST_PENDING(dname_header->trust) ||
  3819. (search->options & DNS_DBFIND_PENDINGOK) != 0)) {
  3820. /*
  3821. * We increment the reference count on node to ensure that
  3822. * search->zonecut_rdataset will still be valid later.
  3823. */
  3824. new_reference(search->rbtdb, node);
  3825. INSIST(!ISC_LINK_LINKED(node, deadlink));
  3826. search->zonecut = node;
  3827. search->zonecut_rdataset = dname_header;
  3828. search->zonecut_sigrdataset = sigdname_header;
  3829. search->need_cleanup = ISC_TRUE;
  3830. result = DNS_R_PARTIALMATCH;
  3831. } else
  3832. result = DNS_R_CONTINUE;
  3833. NODE_UNLOCK(lock, locktype);
  3834. return (result);
  3835. }
  3836. static inline isc_result_t
  3837. find_deepest_zonecut(rbtdb_search_t *search, dns_rbtnode_t *node,
  3838. dns_dbnode_t **nodep, dns_name_t *foundname,
  3839. dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
  3840. {
  3841. unsigned int i;
  3842. dns_rbtnode_t *level_node;
  3843. rdatasetheader_t *header, *header_prev, *header_next;
  3844. rdatasetheader_t *found, *foundsig;
  3845. isc_result_t result = ISC_R_NOTFOUND;
  3846. dns_name_t name;
  3847. dns_rbtdb_t *rbtdb;
  3848. isc_boolean_t done;
  3849. nodelock_t *lock;
  3850. isc_rwlocktype_t locktype;
  3851. /*
  3852. * Caller must be holding the tree lock.
  3853. */
  3854. rbtdb = search->rbtdb;
  3855. i = search->chain.level_matches;
  3856. done = ISC_FALSE;
  3857. do {
  3858. locktype = isc_rwlocktype_read;
  3859. lock = &rbtdb->node_locks[node->locknum].lock;
  3860. NODE_LOCK(lock, locktype);
  3861. /*
  3862. * Look for NS and RRSIG NS rdatasets.
  3863. */
  3864. found = NULL;
  3865. foundsig = NULL;
  3866. header_prev = NULL;
  3867. for (header = node->data;
  3868. header != NULL;
  3869. header = header_next) {
  3870. header_next = header->next;
  3871. if (header->rdh_ttl <= search->now) {
  3872. /*
  3873. * This rdataset is stale. If no one else is
  3874. * using the node, we can clean it up right
  3875. * now, otherwise we mark it as stale, and
  3876. * the node as dirty, so it will get cleaned
  3877. * up later.
  3878. */
  3879. if ((header->rdh_ttl <= search->now -
  3880. RBTDB_VIRTUAL) &&
  3881. (locktype == isc_rwlocktype_write ||
  3882. NODE_TRYUPGRADE(lock) == ISC_R_SUCCESS)) {
  3883. /*
  3884. * We update the node's status only
  3885. * when we can get write access.
  3886. */
  3887. locktype = isc_rwlocktype_write;
  3888. if (dns_rbtnode_refcurrent(node)
  3889. == 0) {
  3890. isc_mem_t *m;
  3891. m = search->rbtdb->common.mctx;
  3892. clean_stale_headers(
  3893. search->rbtdb,
  3894. m, header);
  3895. if (header_prev != NULL)
  3896. header_prev->next =
  3897. header->next;
  3898. else
  3899. node->data =
  3900. header->next;
  3901. free_rdataset(rbtdb, m,
  3902. header);
  3903. } else {
  3904. header->attributes |=
  3905. RDATASET_ATTR_STALE;
  3906. node->dirty = 1;
  3907. header_prev = header;
  3908. }
  3909. } else
  3910. header_prev = header;
  3911. } else if (EXISTS(header)) {
  3912. /*
  3913. * We've found an extant rdataset. See if
  3914. * we're interested in it.
  3915. */
  3916. if (header->type == dns_rdatatype_ns) {
  3917. found = header;
  3918. if (foundsig != NULL)
  3919. break;
  3920. } else if (header->type ==
  3921. RBTDB_RDATATYPE_SIGNS) {
  3922. foundsig = header;
  3923. if (found != NULL)
  3924. break;
  3925. }
  3926. header_prev = header;
  3927. } else
  3928. header_prev = header;
  3929. }
  3930. if (found != NULL) {
  3931. /*
  3932. * If we have to set foundname, we do it before
  3933. * anything else. If we were to set foundname after
  3934. * we had set nodep or bound the rdataset, then we'd
  3935. * have to undo that work if dns_name_concatenate()
  3936. * failed. By setting foundname first, there's
  3937. * nothing to undo if we have trouble.
  3938. */
  3939. if (foundname != NULL) {
  3940. dns_name_init(&name, NULL);
  3941. dns_rbt_namefromnode(node, &name);
  3942. result = dns_name_copy(&name, foundname, NULL);
  3943. while (result == ISC_R_SUCCESS && i > 0) {
  3944. i--;
  3945. level_node = search->chain.levels[i];
  3946. dns_name_init(&name, NULL);
  3947. dns_rbt_namefromnode(level_node,
  3948. &name);
  3949. result =
  3950. dns_name_concatenate(foundname,
  3951. &name,
  3952. foundname,
  3953. NULL);
  3954. }
  3955. if (result != ISC_R_SUCCESS) {
  3956. *nodep = NULL;
  3957. goto node_exit;
  3958. }
  3959. }
  3960. result = DNS_R_DELEGATION;
  3961. if (nodep != NULL) {
  3962. new_reference(search->rbtdb, node);
  3963. *nodep = node;
  3964. }
  3965. bind_rdataset(search->rbtdb, node, found, search->now,
  3966. rdataset);
  3967. if (foundsig != NULL)
  3968. bind_rdataset(search->rbtdb, node, foundsig,
  3969. search->now, sigrdataset);
  3970. if (need_headerupdate(found, search->now) ||
  3971. (foundsig != NULL &&
  3972. need_headerupdate(foundsig, search->now))) {
  3973. if (locktype != isc_rwlocktype_write) {
  3974. NODE_UNLOCK(lock, locktype);
  3975. NODE_LOCK(lock, isc_rwlocktype_write);
  3976. locktype = isc_rwlocktype_write;
  3977. POST(locktype);
  3978. }
  3979. if (need_headerupdate(found, search->now))
  3980. update_header(search->rbtdb, found,
  3981. search->now);
  3982. if (foundsig != NULL &&
  3983. need_headerupdate(foundsig, search->now)) {
  3984. update_header(search->rbtdb, foundsig,
  3985. search->now);
  3986. }
  3987. }
  3988. }
  3989. node_exit:
  3990. NODE_UNLOCK(lock, locktype);
  3991. if (found == NULL && i > 0) {
  3992. i--;
  3993. node = search->chain.levels[i];
  3994. } else
  3995. done = ISC_TRUE;
  3996. } while (!done);
  3997. return (result);
  3998. }
  3999. static isc_result_t
  4000. find_coveringnsec(rbtdb_search_t *search, dns_dbnode_t **nodep,
  4001. isc_stdtime_t now, dns_name_t *foundname,
  4002. dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
  4003. {
  4004. dns_rbtnode_t *node;
  4005. rdatasetheader_t *header, *header_next, *header_prev;
  4006. rdatasetheader_t *found, *foundsig;
  4007. isc_boolean_t empty_node;
  4008. isc_result_t result;
  4009. dns_fixedname_t fname, forigin;
  4010. dns_name_t *name, *origin;
  4011. rbtdb_rdatatype_t matchtype, sigmatchtype;
  4012. nodelock_t *lock;
  4013. isc_rwlocktype_t locktype;
  4014. matchtype = RBTDB_RDATATYPE_VALUE(dns_rdatatype_nsec, 0);
  4015. sigmatchtype = RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig,
  4016. dns_rdatatype_nsec);
  4017. do {
  4018. node = NULL;
  4019. dns_fixedname_init(&fname);
  4020. name = dns_fixedname_name(&fname);
  4021. dns_fixedname_init(&forigin);
  4022. origin = dns_fixedname_name(&forigin);
  4023. result = dns_rbtnodechain_current(&search->chain, name,
  4024. origin, &node);
  4025. if (result != ISC_R_SUCCESS)
  4026. return (result);
  4027. locktype = isc_rwlocktype_read;
  4028. lock = &(search->rbtdb->node_locks[node->locknum].lock);
  4029. NODE_LOCK(lock, locktype);
  4030. found = NULL;
  4031. foundsig = NULL;
  4032. empty_node = ISC_TRUE;
  4033. header_prev = NULL;
  4034. for (header = node->data;
  4035. header != NULL;
  4036. header = header_next) {
  4037. header_next = header->next;
  4038. if (header->rdh_ttl <= now) {
  4039. /*
  4040. * This rdataset is stale. If no one else is
  4041. * using the node, we can clean it up right
  4042. * now, otherwise we mark it as stale, and the
  4043. * node as dirty, so it will get cleaned up
  4044. * later.
  4045. */
  4046. if ((header->rdh_ttl <= now - RBTDB_VIRTUAL) &&
  4047. (locktype == isc_rwlocktype_write ||
  4048. NODE_TRYUPGRADE(lock) == ISC_R_SUCCESS)) {
  4049. /*
  4050. * We update the node's status only
  4051. * when we can get write access.
  4052. */
  4053. locktype = isc_rwlocktype_write;
  4054. if (dns_rbtnode_refcurrent(node)
  4055. == 0) {
  4056. isc_mem_t *m;
  4057. m = search->rbtdb->common.mctx;
  4058. clean_stale_headers(
  4059. search->rbtdb,
  4060. m, header);
  4061. if (header_prev != NULL)
  4062. header_prev->next =
  4063. header->next;
  4064. else
  4065. node->data = header->next;
  4066. free_rdataset(search->rbtdb, m,
  4067. header);
  4068. } else {
  4069. header->attributes |=
  4070. RDATASET_ATTR_STALE;
  4071. node->dirty = 1;
  4072. header_prev = header;
  4073. }
  4074. } else
  4075. header_prev = header;
  4076. continue;
  4077. }
  4078. if (NONEXISTENT(header) ||
  4079. RBTDB_RDATATYPE_BASE(header->type) == 0) {
  4080. header_prev = header;
  4081. continue;
  4082. }
  4083. empty_node = ISC_FALSE;
  4084. if (header->type == matchtype)
  4085. found = header;
  4086. else if (header->type == sigmatchtype)
  4087. foundsig = header;
  4088. header_prev = header;
  4089. }
  4090. if (found != NULL) {
  4091. result = dns_name_concatenate(name, origin,
  4092. foundname, NULL);
  4093. if (result != ISC_R_SUCCESS)
  4094. goto unlock_node;
  4095. bind_rdataset(search->rbtdb, node, found,
  4096. now, rdataset);
  4097. if (foundsig != NULL)
  4098. bind_rdataset(search->rbtdb, node, foundsig,
  4099. now, sigrdataset);
  4100. new_reference(search->rbtdb, node);
  4101. *nodep = node;
  4102. result = DNS_R_COVERINGNSEC;
  4103. } else if (!empty_node) {
  4104. result = ISC_R_NOTFOUND;
  4105. } else
  4106. result = dns_rbtnodechain_prev(&search->chain, NULL,
  4107. NULL);
  4108. unlock_node:
  4109. NODE_UNLOCK(lock, locktype);
  4110. } while (empty_node && result == ISC_R_SUCCESS);
  4111. return (result);
  4112. }
  4113. /*
  4114. * Mark a database for response policy rewriting.
  4115. */
  4116. #ifdef BIND9
  4117. static void
  4118. get_rpz_enabled(dns_db_t *db, dns_rpz_st_t *st)
  4119. {
  4120. dns_rbtdb_t *rbtdb;
  4121. rbtdb = (dns_rbtdb_t *)db;
  4122. REQUIRE(VALID_RBTDB(rbtdb));
  4123. RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
  4124. dns_rpz_enabled(rbtdb->rpz_cidr, st);
  4125. RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
  4126. }
  4127. /*
  4128. * Search the CDIR block tree of a response policy tree of trees for all of
  4129. * the IP addresses in an A or AAAA rdataset.
  4130. * Among the policies for all IPv4 and IPv6 addresses for a name, choose
  4131. * the earliest configured policy,
  4132. * QNAME over IP over NSDNAME over NSIP,
  4133. * the longest prefix,
  4134. * the lexically smallest address.
  4135. * The caller must have already checked that any existing policy was not
  4136. * configured earlier than this policy zone and does not have a higher
  4137. * precedence type.
  4138. */
  4139. static isc_result_t
  4140. rpz_findips(dns_rpz_zone_t *rpz, dns_rpz_type_t rpz_type,
  4141. dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *version,
  4142. dns_rdataset_t *ardataset, dns_rpz_st_t *st,
  4143. dns_name_t *query_qname)
  4144. {
  4145. dns_rbtdb_t *rbtdb;
  4146. struct in_addr ina;
  4147. struct in6_addr in6a;
  4148. isc_netaddr_t netaddr;
  4149. dns_fixedname_t selfnamef, qnamef;
  4150. dns_name_t *selfname, *qname;
  4151. dns_rbtnode_t *node;
  4152. dns_rdataset_t zrdataset;
  4153. dns_rpz_cidr_bits_t prefix;
  4154. isc_result_t result;
  4155. dns_rpz_policy_t rpz_policy;
  4156. dns_ttl_t ttl;
  4157. rbtdb = (dns_rbtdb_t *)db;
  4158. REQUIRE(VALID_RBTDB(rbtdb));
  4159. RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
  4160. if (rbtdb->rpz_cidr == NULL) {
  4161. RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
  4162. return (ISC_R_UNEXPECTED);
  4163. }
  4164. dns_fixedname_init(&selfnamef);
  4165. dns_fixedname_init(&qnamef);
  4166. selfname = dns_fixedname_name(&selfnamef);
  4167. qname = dns_fixedname_name(&qnamef);
  4168. for (result = dns_rdataset_first(ardataset);
  4169. result == ISC_R_SUCCESS;
  4170. result = dns_rdataset_next(ardataset)) {
  4171. dns_rdata_t rdata = DNS_RDATA_INIT;
  4172. dns_rdataset_current(ardataset, &rdata);
  4173. switch (rdata.type) {
  4174. case dns_rdatatype_a:
  4175. INSIST(rdata.length == 4);
  4176. memcpy(&ina.s_addr, rdata.data, 4);
  4177. isc_netaddr_fromin(&netaddr, &ina);
  4178. break;
  4179. case dns_rdatatype_aaaa:
  4180. INSIST(rdata.length == 16);
  4181. memcpy(in6a.s6_addr, rdata.data, 16);
  4182. isc_netaddr_fromin6(&netaddr, &in6a);
  4183. break;
  4184. default:
  4185. continue;
  4186. }
  4187. result = dns_rpz_cidr_find(rbtdb->rpz_cidr, &netaddr, rpz_type,
  4188. selfname, qname, &prefix);
  4189. if (result != ISC_R_SUCCESS)
  4190. continue;
  4191. /*
  4192. * If we already have a rule, discard this new rule if
  4193. * is not better.
  4194. * The caller has checked that st->m.rpz->num > rpz->num
  4195. * or st->m.rpz->num == rpz->num and st->m.type >= rpz_type
  4196. */
  4197. if (st->m.policy != DNS_RPZ_POLICY_MISS &&
  4198. st->m.rpz->num == rpz->num &&
  4199. (st->m.type < rpz_type ||
  4200. (st->m.type == rpz_type &&
  4201. (st->m.prefix > prefix ||
  4202. (st->m.prefix == prefix &&
  4203. 0 > dns_name_rdatacompare(st->qname, qname))))))
  4204. continue;
  4205. /*
  4206. * We have rpz_st an entry with a prefix at least as long as
  4207. * the prefix of the entry we had before. Find the node
  4208. * corresponding to CDIR tree entry.
  4209. */
  4210. node = NULL;
  4211. result = dns_rbt_findnode(rbtdb->tree, qname, NULL,
  4212. &node, NULL, 0, NULL, NULL);
  4213. if (result != ISC_R_SUCCESS) {
  4214. char namebuf[DNS_NAME_FORMATSIZE];
  4215. dns_name_format(qname, namebuf, sizeof(namebuf));
  4216. isc_log_write(dns_lctx, DNS_LOGCATEGORY_RPZ,
  4217. DNS_LOGMODULE_RBTDB, DNS_RPZ_ERROR_LEVEL,
  4218. "rpz_findips findnode(%s): %s",
  4219. namebuf, isc_result_totext(result));
  4220. continue;
  4221. }
  4222. /*
  4223. * First look for a simple rewrite of the IP address.
  4224. * If that fails, look for a CNAME. If we cannot find
  4225. * a CNAME or the CNAME is neither of the special forms
  4226. * "*" or ".", treat it like a real CNAME.
  4227. */
  4228. dns_rdataset_init(&zrdataset);
  4229. result = dns_db_findrdataset(db, node, version, ardataset->type,
  4230. 0, 0, &zrdataset, NULL);
  4231. if (result != ISC_R_SUCCESS)
  4232. result = dns_db_findrdataset(db, node, version,
  4233. dns_rdatatype_cname,
  4234. 0, 0, &zrdataset, NULL);
  4235. if (result == ISC_R_SUCCESS) {
  4236. if (zrdataset.type != dns_rdatatype_cname) {
  4237. rpz_policy = DNS_RPZ_POLICY_RECORD;
  4238. } else {
  4239. rpz_policy = dns_rpz_decode_cname(&zrdataset,
  4240. selfname);
  4241. if (rpz_policy == DNS_RPZ_POLICY_RECORD ||
  4242. rpz_policy == DNS_RPZ_POLICY_WILDCNAME)
  4243. result = DNS_R_CNAME;
  4244. }
  4245. ttl = zrdataset.ttl;
  4246. } else {
  4247. rpz_policy = DNS_RPZ_POLICY_RECORD;
  4248. result = DNS_R_NXRRSET;
  4249. ttl = DNS_RPZ_TTL_DEFAULT;
  4250. }
  4251. /*
  4252. * Use an overriding action specified in the configuration file
  4253. */
  4254. if (rpz->policy != DNS_RPZ_POLICY_GIVEN) {
  4255. /*
  4256. * only log DNS_RPZ_POLICY_DISABLED hits
  4257. */
  4258. if (rpz->policy == DNS_RPZ_POLICY_DISABLED) {
  4259. if (isc_log_wouldlog(dns_lctx,
  4260. DNS_RPZ_INFO_LEVEL)) {
  4261. char qname_buf[DNS_NAME_FORMATSIZE];
  4262. char rpz_qname_buf[DNS_NAME_FORMATSIZE];
  4263. dns_name_format(query_qname, qname_buf,
  4264. sizeof(qname_buf));
  4265. dns_name_format(qname, rpz_qname_buf,
  4266. sizeof(rpz_qname_buf));
  4267. isc_log_write(dns_lctx,
  4268. DNS_LOGCATEGORY_RPZ,
  4269. DNS_LOGMODULE_RBTDB,
  4270. DNS_RPZ_INFO_LEVEL,
  4271. "disabled rpz %s %s rewrite"
  4272. " %s via %s",
  4273. dns_rpz_type2str(rpz_type),
  4274. dns_rpz_policy2str(rpz_policy),
  4275. qname_buf, rpz_qname_buf);
  4276. }
  4277. continue;
  4278. }
  4279. rpz_policy = rpz->policy;
  4280. }
  4281. if (dns_rdataset_isassociated(st->m.rdataset))
  4282. dns_rdataset_disassociate(st->m.rdataset);
  4283. if (st->m.node != NULL)
  4284. dns_db_detachnode(st->m.db, &st->m.node);
  4285. if (st->m.db != NULL)
  4286. dns_db_detach(&st->m.db);
  4287. if (st->m.zone != NULL)
  4288. dns_zone_detach(&st->m.zone);
  4289. st->m.rpz = rpz;
  4290. st->m.type = rpz_type;
  4291. st->m.prefix = prefix;
  4292. st->m.policy = rpz_policy;
  4293. st->m.ttl = ttl;
  4294. st->m.result = result;
  4295. dns_name_copy(qname, st->qname, NULL);
  4296. if ((rpz_policy == DNS_RPZ_POLICY_RECORD ||
  4297. rpz_policy == DNS_RPZ_POLICY_WILDCNAME) &&
  4298. result != DNS_R_NXRRSET) {
  4299. dns_rdataset_clone(&zrdataset,st->m.rdataset);
  4300. dns_db_attachnode(db, node, &st->m.node);
  4301. }
  4302. dns_db_attach(db, &st->m.db);
  4303. st->m.version = version;
  4304. dns_zone_attach(zone, &st->m.zone);
  4305. if (dns_rdataset_isassociated(&zrdataset))
  4306. dns_rdataset_disassociate(&zrdataset);
  4307. }
  4308. RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
  4309. return (ISC_R_SUCCESS);
  4310. }
  4311. #endif
  4312. static isc_result_t
  4313. cache_find(dns_db_t *db, dns_name_t *name, dns_dbversion_t *version,
  4314. dns_rdatatype_t type, unsigned int options, isc_stdtime_t now,
  4315. dns_dbnode_t **nodep, dns_name_t *foundname,
  4316. dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
  4317. {
  4318. dns_rbtnode_t *node = NULL;
  4319. isc_result_t result;
  4320. rbtdb_search_t search;
  4321. isc_boolean_t cname_ok = ISC_TRUE;
  4322. isc_boolean_t empty_node;
  4323. nodelock_t *lock;
  4324. isc_rwlocktype_t locktype;
  4325. rdatasetheader_t *header, *header_prev, *header_next;
  4326. rdatasetheader_t *found, *nsheader;
  4327. rdatasetheader_t *foundsig, *nssig, *cnamesig;
  4328. rdatasetheader_t *update, *updatesig;
  4329. rbtdb_rdatatype_t sigtype, negtype;
  4330. UNUSED(version);
  4331. search.rbtdb = (dns_rbtdb_t *)db;
  4332. REQUIRE(VALID_RBTDB(search.rbtdb));
  4333. REQUIRE(version == NULL);
  4334. if (now == 0)
  4335. isc_stdtime_get(&now);
  4336. search.rbtversion = NULL;
  4337. search.serial = 1;
  4338. search.options = options;
  4339. search.copy_name = ISC_FALSE;
  4340. search.need_cleanup = ISC_FALSE;
  4341. search.wild = ISC_FALSE;
  4342. search.zonecut = NULL;
  4343. dns_fixedname_init(&search.zonecut_name);
  4344. dns_rbtnodechain_init(&search.chain, search.rbtdb->common.mctx);
  4345. search.now = now;
  4346. update = NULL;
  4347. updatesig = NULL;
  4348. RWLOCK(&search.rbtdb->tree_lock, isc_rwlocktype_read);
  4349. /*
  4350. * Search down from the root of the tree. If, while going down, we
  4351. * encounter a callback node, cache_zonecut_callback() will search the
  4352. * rdatasets at the zone cut for a DNAME rdataset.
  4353. */
  4354. result = dns_rbt_findnode(search.rbtdb->tree, name, foundname, &node,
  4355. &search.chain, DNS_RBTFIND_EMPTYDATA,
  4356. cache_zonecut_callback, &search);
  4357. if (result == DNS_R_PARTIALMATCH) {
  4358. if ((search.options & DNS_DBFIND_COVERINGNSEC) != 0) {
  4359. result = find_coveringnsec(&search, nodep, now,
  4360. foundname, rdataset,
  4361. sigrdataset);
  4362. if (result == DNS_R_COVERINGNSEC)
  4363. goto tree_exit;
  4364. }
  4365. if (search.zonecut != NULL) {
  4366. result = setup_delegation(&search, nodep, foundname,
  4367. rdataset, sigrdataset);
  4368. goto tree_exit;
  4369. } else {
  4370. find_ns:
  4371. result = find_deepest_zonecut(&search, node, nodep,
  4372. foundname, rdataset,
  4373. sigrdataset);
  4374. goto tree_exit;
  4375. }
  4376. } else if (result != ISC_R_SUCCESS)
  4377. goto tree_exit;
  4378. /*
  4379. * Certain DNSSEC types are not subject to CNAME matching
  4380. * (RFC4035, section 2.5 and RFC3007).
  4381. *
  4382. * We don't check for RRSIG, because we don't store RRSIG records
  4383. * directly.
  4384. */
  4385. if (type == dns_rdatatype_key || type == dns_rdatatype_nsec)
  4386. cname_ok = ISC_FALSE;
  4387. /*
  4388. * We now go looking for rdata...
  4389. */
  4390. lock = &(search.rbtdb->node_locks[node->locknum].lock);
  4391. locktype = isc_rwlocktype_read;
  4392. NODE_LOCK(lock, locktype);
  4393. found = NULL;
  4394. foundsig = NULL;
  4395. sigtype = RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig, type);
  4396. negtype = RBTDB_RDATATYPE_VALUE(0, type);
  4397. nsheader = NULL;
  4398. nssig = NULL;
  4399. cnamesig = NULL;
  4400. empty_node = ISC_TRUE;
  4401. header_prev = NULL;
  4402. for (header = node->data; header != NULL; header = header_next) {
  4403. header_next = header->next;
  4404. if (header->rdh_ttl <= now) {
  4405. /*
  4406. * This rdataset is stale. If no one else is using the
  4407. * node, we can clean it up right now, otherwise we
  4408. * mark it as stale, and the node as dirty, so it will
  4409. * get cleaned up later.
  4410. */
  4411. if ((header->rdh_ttl <= now - RBTDB_VIRTUAL) &&
  4412. (locktype == isc_rwlocktype_write ||
  4413. NODE_TRYUPGRADE(lock) == ISC_R_SUCCESS)) {
  4414. /*
  4415. * We update the node's status only when we
  4416. * can get write access.
  4417. */
  4418. locktype = isc_rwlocktype_write;
  4419. if (dns_rbtnode_refcurrent(node) == 0) {
  4420. isc_mem_t *mctx;
  4421. mctx = search.rbtdb->common.mctx;
  4422. clean_stale_headers(search.rbtdb, mctx,
  4423. header);
  4424. if (header_prev != NULL)
  4425. header_prev->next =
  4426. header->next;
  4427. else
  4428. node->data = header->next;
  4429. free_rdataset(search.rbtdb, mctx,
  4430. header);
  4431. } else {
  4432. header->attributes |=
  4433. RDATASET_ATTR_STALE;
  4434. node->dirty = 1;
  4435. header_prev = header;
  4436. }
  4437. } else
  4438. header_prev = header;
  4439. } else if (EXISTS(header)) {
  4440. /*
  4441. * We now know that there is at least one active
  4442. * non-stale rdataset at this node.
  4443. */
  4444. empty_node = ISC_FALSE;
  4445. /*
  4446. * If we found a type we were looking for, remember
  4447. * it.
  4448. */
  4449. if (header->type == type ||
  4450. (type == dns_rdatatype_any &&
  4451. RBTDB_RDATATYPE_BASE(header->type) != 0) ||
  4452. (cname_ok && header->type ==
  4453. dns_rdatatype_cname)) {
  4454. /*
  4455. * We've found the answer.
  4456. */
  4457. found = header;
  4458. if (header->type == dns_rdatatype_cname &&
  4459. cname_ok &&
  4460. cnamesig != NULL) {
  4461. /*
  4462. * If we've already got the
  4463. * CNAME RRSIG, use it.
  4464. */
  4465. foundsig = cnamesig;
  4466. }
  4467. } else if (header->type == sigtype) {
  4468. /*
  4469. * We've found the RRSIG rdataset for our
  4470. * target type. Remember it.
  4471. */
  4472. foundsig = header;
  4473. } else if (header->type == RBTDB_RDATATYPE_NCACHEANY ||
  4474. header->type == negtype) {
  4475. /*
  4476. * We've found a negative cache entry.
  4477. */
  4478. found = header;
  4479. } else if (header->type == dns_rdatatype_ns) {
  4480. /*
  4481. * Remember a NS rdataset even if we're
  4482. * not specifically looking for it, because
  4483. * we might need it later.
  4484. */
  4485. nsheader = header;
  4486. } else if (header->type == RBTDB_RDATATYPE_SIGNS) {
  4487. /*
  4488. * If we need the NS rdataset, we'll also
  4489. * need its signature.
  4490. */
  4491. nssig = header;
  4492. } else if (cname_ok &&
  4493. header->type == RBTDB_RDATATYPE_SIGCNAME) {
  4494. /*
  4495. * If we get a CNAME match, we'll also need
  4496. * its signature.
  4497. */
  4498. cnamesig = header;
  4499. }
  4500. header_prev = header;
  4501. } else
  4502. header_prev = header;
  4503. }
  4504. if (empty_node) {
  4505. /*
  4506. * We have an exact match for the name, but there are no
  4507. * extant rdatasets. That means that this node doesn't
  4508. * meaningfully exist, and that we really have a partial match.
  4509. */
  4510. NODE_UNLOCK(lock, locktype);
  4511. goto find_ns;
  4512. }
  4513. /*
  4514. * If we didn't find what we were looking for...
  4515. */
  4516. if (found == NULL ||
  4517. (DNS_TRUST_ADDITIONAL(found->trust) &&
  4518. ((options & DNS_DBFIND_ADDITIONALOK) == 0)) ||
  4519. (found->trust == dns_trust_glue &&
  4520. ((options & DNS_DBFIND_GLUEOK) == 0)) ||
  4521. (DNS_TRUST_PENDING(found->trust) &&
  4522. ((options & DNS_DBFIND_PENDINGOK) == 0))) {
  4523. /*
  4524. * If there is an NS rdataset at this node, then this is the
  4525. * deepest zone cut.
  4526. */
  4527. if (nsheader != NULL) {
  4528. if (nodep != NULL) {
  4529. new_reference(search.rbtdb, node);
  4530. INSIST(!ISC_LINK_LINKED(node, deadlink));
  4531. *nodep = node;
  4532. }
  4533. bind_rdataset(search.rbtdb, node, nsheader, search.now,
  4534. rdataset);
  4535. if (need_headerupdate(nsheader, search.now))
  4536. update = nsheader;
  4537. if (nssig != NULL) {
  4538. bind_rdataset(search.rbtdb, node, nssig,
  4539. search.now, sigrdataset);
  4540. if (need_headerupdate(nssig, search.now))
  4541. updatesig = nssig;
  4542. }
  4543. result = DNS_R_DELEGATION;
  4544. goto node_exit;
  4545. }
  4546. /*
  4547. * Go find the deepest zone cut.
  4548. */
  4549. NODE_UNLOCK(lock, locktype);
  4550. goto find_ns;
  4551. }
  4552. /*
  4553. * We found what we were looking for, or we found a CNAME.
  4554. */
  4555. if (nodep != NULL) {
  4556. new_reference(search.rbtdb, node);
  4557. INSIST(!ISC_LINK_LINKED(node, deadlink));
  4558. *nodep = node;
  4559. }
  4560. if (NEGATIVE(found)) {
  4561. /*
  4562. * We found a negative cache entry.
  4563. */
  4564. if (NXDOMAIN(found))
  4565. result = DNS_R_NCACHENXDOMAIN;
  4566. else
  4567. result = DNS_R_NCACHENXRRSET;
  4568. } else if (type != found->type &&
  4569. type != dns_rdatatype_any &&
  4570. found->type == dns_rdatatype_cname) {
  4571. /*
  4572. * We weren't doing an ANY query and we found a CNAME instead
  4573. * of the type we were looking for, so we need to indicate
  4574. * that result to the caller.
  4575. */
  4576. result = DNS_R_CNAME;
  4577. } else {
  4578. /*
  4579. * An ordinary successful query!
  4580. */
  4581. result = ISC_R_SUCCESS;
  4582. }
  4583. if (type != dns_rdatatype_any || result == DNS_R_NCACHENXDOMAIN ||
  4584. result == DNS_R_NCACHENXRRSET) {
  4585. bind_rdataset(search.rbtdb, node, found, search.now,
  4586. rdataset);
  4587. if (need_headerupdate(found, search.now))
  4588. update = found;
  4589. if (!NEGATIVE(found) && foundsig != NULL) {
  4590. bind_rdataset(search.rbtdb, node, foundsig, search.now,
  4591. sigrdataset);
  4592. if (need_headerupdate(foundsig, search.now))
  4593. updatesig = foundsig;
  4594. }
  4595. }
  4596. node_exit:
  4597. if ((update != NULL || updatesig != NULL) &&
  4598. locktype != isc_rwlocktype_write) {
  4599. NODE_UNLOCK(lock, locktype);
  4600. NODE_LOCK(lock, isc_rwlocktype_write);
  4601. locktype = isc_rwlocktype_write;
  4602. POST(locktype);
  4603. }
  4604. if (update != NULL && need_headerupdate(update, search.now))
  4605. update_header(search.rbtdb, update, search.now);
  4606. if (updatesig != NULL && need_headerupdate(updatesig, search.now))
  4607. update_header(search.rbtdb, updatesig, search.now);
  4608. NODE_UNLOCK(lock, locktype);
  4609. tree_exit:
  4610. RWUNLOCK(&search.rbtdb->tree_lock, isc_rwlocktype_read);
  4611. /*
  4612. * If we found a zonecut but aren't going to use it, we have to
  4613. * let go of it.
  4614. */
  4615. if (search.need_cleanup) {
  4616. node = search.zonecut;
  4617. INSIST(node != NULL);
  4618. lock = &(search.rbtdb->node_locks[node->locknum].lock);
  4619. NODE_LOCK(lock, isc_rwlocktype_read);
  4620. decrement_reference(search.rbtdb, node, 0,
  4621. isc_rwlocktype_read, isc_rwlocktype_none,
  4622. ISC_FALSE);
  4623. NODE_UNLOCK(lock, isc_rwlocktype_read);
  4624. }
  4625. dns_rbtnodechain_reset(&search.chain);
  4626. return (result);
  4627. }
  4628. static isc_result_t
  4629. cache_findzonecut(dns_db_t *db, dns_name_t *name, unsigned int options,
  4630. isc_stdtime_t now, dns_dbnode_t **nodep,
  4631. dns_name_t *foundname,
  4632. dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
  4633. {
  4634. dns_rbtnode_t *node = NULL;
  4635. nodelock_t *lock;
  4636. isc_result_t result;
  4637. rbtdb_search_t search;
  4638. rdatasetheader_t *header, *header_prev, *header_next;
  4639. rdatasetheader_t *found, *foundsig;
  4640. unsigned int rbtoptions = DNS_RBTFIND_EMPTYDATA;
  4641. isc_rwlocktype_t locktype;
  4642. search.rbtdb = (dns_rbtdb_t *)db;
  4643. REQUIRE(VALID_RBTDB(search.rbtdb));
  4644. if (now == 0)
  4645. isc_stdtime_get(&now);
  4646. search.rbtversion = NULL;
  4647. search.serial = 1;
  4648. search.options = options;
  4649. search.copy_name = ISC_FALSE;
  4650. search.need_cleanup = ISC_FALSE;
  4651. search.wild = ISC_FALSE;
  4652. search.zonecut = NULL;
  4653. dns_fixedname_init(&search.zonecut_name);
  4654. dns_rbtnodechain_init(&search.chain, search.rbtdb->common.mctx);
  4655. search.now = now;
  4656. if ((options & DNS_DBFIND_NOEXACT) != 0)
  4657. rbtoptions |= DNS_RBTFIND_NOEXACT;
  4658. RWLOCK(&search.rbtdb->tree_lock, isc_rwlocktype_read);
  4659. /*
  4660. * Search down from the root of the tree.
  4661. */
  4662. result = dns_rbt_findnode(search.rbtdb->tree, name, foundname, &node,
  4663. &search.chain, rbtoptions, NULL, &search);
  4664. if (result == DNS_R_PARTIALMATCH) {
  4665. find_ns:
  4666. result = find_deepest_zonecut(&search, node, nodep, foundname,
  4667. rdataset, sigrdataset);
  4668. goto tree_exit;
  4669. } else if (result != ISC_R_SUCCESS)
  4670. goto tree_exit;
  4671. /*
  4672. * We now go looking for an NS rdataset at the node.
  4673. */
  4674. lock = &(search.rbtdb->node_locks[node->locknum].lock);
  4675. locktype = isc_rwlocktype_read;
  4676. NODE_LOCK(lock, locktype);
  4677. found = NULL;
  4678. foundsig = NULL;
  4679. header_prev = NULL;
  4680. for (header = node->data; header != NULL; header = header_next) {
  4681. header_next = header->next;
  4682. if (header->rdh_ttl <= now) {
  4683. /*
  4684. * This rdataset is stale. If no one else is using the
  4685. * node, we can clean it up right now, otherwise we
  4686. * mark it as stale, and the node as dirty, so it will
  4687. * get cleaned up later.
  4688. */
  4689. if ((header->rdh_ttl <= now - RBTDB_VIRTUAL) &&
  4690. (locktype == isc_rwlocktype_write ||
  4691. NODE_TRYUPGRADE(lock) == ISC_R_SUCCESS)) {
  4692. /*
  4693. * We update the node's status only when we
  4694. * can get write access.
  4695. */
  4696. locktype = isc_rwlocktype_write;
  4697. if (dns_rbtnode_refcurrent(node) == 0) {
  4698. isc_mem_t *mctx;
  4699. mctx = search.rbtdb->common.mctx;
  4700. clean_stale_headers(search.rbtdb, mctx,
  4701. header);
  4702. if (header_prev != NULL)
  4703. header_prev->next =
  4704. header->next;
  4705. else
  4706. node->data = header->next;
  4707. free_rdataset(search.rbtdb, mctx,
  4708. header);
  4709. } else {
  4710. header->attributes |=
  4711. RDATASET_ATTR_STALE;
  4712. node->dirty = 1;
  4713. header_prev = header;
  4714. }
  4715. } else
  4716. header_prev = header;
  4717. } else if (EXISTS(header)) {
  4718. /*
  4719. * If we found a type we were looking for, remember
  4720. * it.
  4721. */
  4722. if (header->type == dns_rdatatype_ns) {
  4723. /*
  4724. * Remember a NS rdataset even if we're
  4725. * not specifically looking for it, because
  4726. * we might need it later.
  4727. */
  4728. found = header;
  4729. } else if (header->type == RBTDB_RDATATYPE_SIGNS) {
  4730. /*
  4731. * If we need the NS rdataset, we'll also
  4732. * need its signature.
  4733. */
  4734. foundsig = header;
  4735. }
  4736. header_prev = header;
  4737. } else
  4738. header_prev = header;
  4739. }
  4740. if (found == NULL) {
  4741. /*
  4742. * No NS records here.
  4743. */
  4744. NODE_UNLOCK(lock, locktype);
  4745. goto find_ns;
  4746. }
  4747. if (nodep != NULL) {
  4748. new_reference(search.rbtdb, node);
  4749. INSIST(!ISC_LINK_LINKED(node, deadlink));
  4750. *nodep = node;
  4751. }
  4752. bind_rdataset(search.rbtdb, node, found, search.now, rdataset);
  4753. if (foundsig != NULL)
  4754. bind_rdataset(search.rbtdb, node, foundsig, search.now,
  4755. sigrdataset);
  4756. if (need_headerupdate(found, search.now) ||
  4757. (foundsig != NULL && need_headerupdate(foundsig, search.now))) {
  4758. if (locktype != isc_rwlocktype_write) {
  4759. NODE_UNLOCK(lock, locktype);
  4760. NODE_LOCK(lock, isc_rwlocktype_write);
  4761. locktype = isc_rwlocktype_write;
  4762. POST(locktype);
  4763. }
  4764. if (need_headerupdate(found, search.now))
  4765. update_header(search.rbtdb, found, search.now);
  4766. if (foundsig != NULL &&
  4767. need_headerupdate(foundsig, search.now)) {
  4768. update_header(search.rbtdb, foundsig, search.now);
  4769. }
  4770. }
  4771. NODE_UNLOCK(lock, locktype);
  4772. tree_exit:
  4773. RWUNLOCK(&search.rbtdb->tree_lock, isc_rwlocktype_read);
  4774. INSIST(!search.need_cleanup);
  4775. dns_rbtnodechain_reset(&search.chain);
  4776. if (result == DNS_R_DELEGATION)
  4777. result = ISC_R_SUCCESS;
  4778. return (result);
  4779. }
  4780. static void
  4781. attachnode(dns_db_t *db, dns_dbnode_t *source, dns_dbnode_t **targetp) {
  4782. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
  4783. dns_rbtnode_t *node = (dns_rbtnode_t *)source;
  4784. unsigned int refs;
  4785. REQUIRE(VALID_RBTDB(rbtdb));
  4786. REQUIRE(targetp != NULL && *targetp == NULL);
  4787. NODE_STRONGLOCK(&rbtdb->node_locks[node->locknum].lock);
  4788. dns_rbtnode_refincrement(node, &refs);
  4789. INSIST(refs != 0);
  4790. NODE_STRONGUNLOCK(&rbtdb->node_locks[node->locknum].lock);
  4791. *targetp = source;
  4792. }
  4793. static void
  4794. detachnode(dns_db_t *db, dns_dbnode_t **targetp) {
  4795. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
  4796. dns_rbtnode_t *node;
  4797. isc_boolean_t want_free = ISC_FALSE;
  4798. isc_boolean_t inactive = ISC_FALSE;
  4799. rbtdb_nodelock_t *nodelock;
  4800. REQUIRE(VALID_RBTDB(rbtdb));
  4801. REQUIRE(targetp != NULL && *targetp != NULL);
  4802. node = (dns_rbtnode_t *)(*targetp);
  4803. nodelock = &rbtdb->node_locks[node->locknum];
  4804. NODE_LOCK(&nodelock->lock, isc_rwlocktype_read);
  4805. if (decrement_reference(rbtdb, node, 0, isc_rwlocktype_read,
  4806. isc_rwlocktype_none, ISC_FALSE)) {
  4807. if (isc_refcount_current(&nodelock->references) == 0 &&
  4808. nodelock->exiting) {
  4809. inactive = ISC_TRUE;
  4810. }
  4811. }
  4812. NODE_UNLOCK(&nodelock->lock, isc_rwlocktype_read);
  4813. *targetp = NULL;
  4814. if (inactive) {
  4815. RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_write);
  4816. rbtdb->active--;
  4817. if (rbtdb->active == 0)
  4818. want_free = ISC_TRUE;
  4819. RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_write);
  4820. if (want_free) {
  4821. char buf[DNS_NAME_FORMATSIZE];
  4822. if (dns_name_dynamic(&rbtdb->common.origin))
  4823. dns_name_format(&rbtdb->common.origin, buf,
  4824. sizeof(buf));
  4825. else
  4826. strcpy(buf, "<UNKNOWN>");
  4827. isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
  4828. DNS_LOGMODULE_CACHE, ISC_LOG_DEBUG(1),
  4829. "calling free_rbtdb(%s)", buf);
  4830. free_rbtdb(rbtdb, ISC_TRUE, NULL);
  4831. }
  4832. }
  4833. }
  4834. static isc_result_t
  4835. expirenode(dns_db_t *db, dns_dbnode_t *node, isc_stdtime_t now) {
  4836. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
  4837. dns_rbtnode_t *rbtnode = node;
  4838. rdatasetheader_t *header;
  4839. isc_boolean_t force_expire = ISC_FALSE;
  4840. /*
  4841. * These are the category and module used by the cache cleaner.
  4842. */
  4843. isc_boolean_t log = ISC_FALSE;
  4844. isc_logcategory_t *category = DNS_LOGCATEGORY_DATABASE;
  4845. isc_logmodule_t *module = DNS_LOGMODULE_CACHE;
  4846. int level = ISC_LOG_DEBUG(2);
  4847. char printname[DNS_NAME_FORMATSIZE];
  4848. REQUIRE(VALID_RBTDB(rbtdb));
  4849. /*
  4850. * Caller must hold a tree lock.
  4851. */
  4852. if (now == 0)
  4853. isc_stdtime_get(&now);
  4854. if (isc_mem_isovermem(rbtdb->common.mctx)) {
  4855. isc_uint32_t val;
  4856. isc_random_get(&val);
  4857. /*
  4858. * XXXDCL Could stand to have a better policy, like LRU.
  4859. */
  4860. force_expire = ISC_TF(rbtnode->down == NULL && val % 4 == 0);
  4861. /*
  4862. * Note that 'log' can be true IFF overmem is also true.
  4863. * overmem can currently only be true for cache
  4864. * databases -- hence all of the "overmem cache" log strings.
  4865. */
  4866. log = ISC_TF(isc_log_wouldlog(dns_lctx, level));
  4867. if (log)
  4868. isc_log_write(dns_lctx, category, module, level,
  4869. "overmem cache: %s %s",
  4870. force_expire ? "FORCE" : "check",
  4871. dns_rbt_formatnodename(rbtnode,
  4872. printname,
  4873. sizeof(printname)));
  4874. }
  4875. /*
  4876. * We may not need write access, but this code path is not performance
  4877. * sensitive, so it should be okay to always lock as a writer.
  4878. */
  4879. NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
  4880. isc_rwlocktype_write);
  4881. for (header = rbtnode->data; header != NULL; header = header->next)
  4882. if (header->rdh_ttl <= now - RBTDB_VIRTUAL) {
  4883. /*
  4884. * We don't check if refcurrent(rbtnode) == 0 and try
  4885. * to free like we do in cache_find(), because
  4886. * refcurrent(rbtnode) must be non-zero. This is so
  4887. * because 'node' is an argument to the function.
  4888. */
  4889. header->attributes |= RDATASET_ATTR_STALE;
  4890. rbtnode->dirty = 1;
  4891. if (log)
  4892. isc_log_write(dns_lctx, category, module,
  4893. level, "overmem cache: stale %s",
  4894. printname);
  4895. } else if (force_expire) {
  4896. if (! RETAIN(header)) {
  4897. set_ttl(rbtdb, header, 0);
  4898. header->attributes |= RDATASET_ATTR_STALE;
  4899. rbtnode->dirty = 1;
  4900. } else if (log) {
  4901. isc_log_write(dns_lctx, category, module,
  4902. level, "overmem cache: "
  4903. "reprieve by RETAIN() %s",
  4904. printname);
  4905. }
  4906. } else if (isc_mem_isovermem(rbtdb->common.mctx) && log)
  4907. isc_log_write(dns_lctx, category, module, level,
  4908. "overmem cache: saved %s", printname);
  4909. NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
  4910. isc_rwlocktype_write);
  4911. return (ISC_R_SUCCESS);
  4912. }
  4913. static void
  4914. overmem(dns_db_t *db, isc_boolean_t overmem) {
  4915. /* This is an empty callback. See adb.c:water() */
  4916. UNUSED(db);
  4917. UNUSED(overmem);
  4918. return;
  4919. }
  4920. static void
  4921. printnode(dns_db_t *db, dns_dbnode_t *node, FILE *out) {
  4922. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
  4923. dns_rbtnode_t *rbtnode = node;
  4924. isc_boolean_t first;
  4925. REQUIRE(VALID_RBTDB(rbtdb));
  4926. NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
  4927. isc_rwlocktype_read);
  4928. fprintf(out, "node %p, %u references, locknum = %u\n",
  4929. rbtnode, dns_rbtnode_refcurrent(rbtnode),
  4930. rbtnode->locknum);
  4931. if (rbtnode->data != NULL) {
  4932. rdatasetheader_t *current, *top_next;
  4933. for (current = rbtnode->data; current != NULL;
  4934. current = top_next) {
  4935. top_next = current->next;
  4936. first = ISC_TRUE;
  4937. fprintf(out, "\ttype %u", current->type);
  4938. do {
  4939. if (!first)
  4940. fprintf(out, "\t");
  4941. first = ISC_FALSE;
  4942. fprintf(out,
  4943. "\tserial = %lu, ttl = %u, "
  4944. "trust = %u, attributes = %u, "
  4945. "resign = %u\n",
  4946. (unsigned long)current->serial,
  4947. current->rdh_ttl,
  4948. current->trust,
  4949. current->attributes,
  4950. current->resign);
  4951. current = current->down;
  4952. } while (current != NULL);
  4953. }
  4954. } else
  4955. fprintf(out, "(empty)\n");
  4956. NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
  4957. isc_rwlocktype_read);
  4958. }
  4959. static isc_result_t
  4960. createiterator(dns_db_t *db, unsigned int options, dns_dbiterator_t **iteratorp)
  4961. {
  4962. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
  4963. rbtdb_dbiterator_t *rbtdbiter;
  4964. REQUIRE(VALID_RBTDB(rbtdb));
  4965. rbtdbiter = isc_mem_get(rbtdb->common.mctx, sizeof(*rbtdbiter));
  4966. if (rbtdbiter == NULL)
  4967. return (ISC_R_NOMEMORY);
  4968. rbtdbiter->common.methods = &dbiterator_methods;
  4969. rbtdbiter->common.db = NULL;
  4970. dns_db_attach(db, &rbtdbiter->common.db);
  4971. rbtdbiter->common.relative_names =
  4972. ISC_TF((options & DNS_DB_RELATIVENAMES) != 0);
  4973. rbtdbiter->common.magic = DNS_DBITERATOR_MAGIC;
  4974. rbtdbiter->common.cleaning = ISC_FALSE;
  4975. rbtdbiter->paused = ISC_TRUE;
  4976. rbtdbiter->tree_locked = isc_rwlocktype_none;
  4977. rbtdbiter->result = ISC_R_SUCCESS;
  4978. dns_fixedname_init(&rbtdbiter->name);
  4979. dns_fixedname_init(&rbtdbiter->origin);
  4980. rbtdbiter->node = NULL;
  4981. rbtdbiter->delete = 0;
  4982. rbtdbiter->nsec3only = ISC_TF((options & DNS_DB_NSEC3ONLY) != 0);
  4983. rbtdbiter->nonsec3 = ISC_TF((options & DNS_DB_NONSEC3) != 0);
  4984. memset(rbtdbiter->deletions, 0, sizeof(rbtdbiter->deletions));
  4985. dns_rbtnodechain_init(&rbtdbiter->chain, db->mctx);
  4986. dns_rbtnodechain_init(&rbtdbiter->nsec3chain, db->mctx);
  4987. if (rbtdbiter->nsec3only)
  4988. rbtdbiter->current = &rbtdbiter->nsec3chain;
  4989. else
  4990. rbtdbiter->current = &rbtdbiter->chain;
  4991. *iteratorp = (dns_dbiterator_t *)rbtdbiter;
  4992. return (ISC_R_SUCCESS);
  4993. }
  4994. static isc_result_t
  4995. zone_findrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
  4996. dns_rdatatype_t type, dns_rdatatype_t covers,
  4997. isc_stdtime_t now, dns_rdataset_t *rdataset,
  4998. dns_rdataset_t *sigrdataset)
  4999. {
  5000. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
  5001. dns_rbtnode_t *rbtnode = (dns_rbtnode_t *)node;
  5002. rdatasetheader_t *header, *header_next, *found, *foundsig;
  5003. rbtdb_serial_t serial;
  5004. rbtdb_version_t *rbtversion = version;
  5005. isc_boolean_t close_version = ISC_FALSE;
  5006. rbtdb_rdatatype_t matchtype, sigmatchtype;
  5007. REQUIRE(VALID_RBTDB(rbtdb));
  5008. REQUIRE(type != dns_rdatatype_any);
  5009. INSIST(rbtversion == NULL || rbtversion->rbtdb == rbtdb);
  5010. if (rbtversion == NULL) {
  5011. currentversion(db, (dns_dbversion_t **) (void *)(&rbtversion));
  5012. close_version = ISC_TRUE;
  5013. }
  5014. serial = rbtversion->serial;
  5015. now = 0;
  5016. NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
  5017. isc_rwlocktype_read);
  5018. found = NULL;
  5019. foundsig = NULL;
  5020. matchtype = RBTDB_RDATATYPE_VALUE(type, covers);
  5021. if (covers == 0)
  5022. sigmatchtype = RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig, type);
  5023. else
  5024. sigmatchtype = 0;
  5025. for (header = rbtnode->data; header != NULL; header = header_next) {
  5026. header_next = header->next;
  5027. do {
  5028. if (header->serial <= serial &&
  5029. !IGNORE(header)) {
  5030. /*
  5031. * Is this a "this rdataset doesn't
  5032. * exist" record?
  5033. */
  5034. if (NONEXISTENT(header))
  5035. header = NULL;
  5036. break;
  5037. } else
  5038. header = header->down;
  5039. } while (header != NULL);
  5040. if (header != NULL) {
  5041. /*
  5042. * We have an active, extant rdataset. If it's a
  5043. * type we're looking for, remember it.
  5044. */
  5045. if (header->type == matchtype) {
  5046. found = header;
  5047. if (foundsig != NULL)
  5048. break;
  5049. } else if (header->type == sigmatchtype) {
  5050. foundsig = header;
  5051. if (found != NULL)
  5052. break;
  5053. }
  5054. }
  5055. }
  5056. if (found != NULL) {
  5057. bind_rdataset(rbtdb, rbtnode, found, now, rdataset);
  5058. if (foundsig != NULL)
  5059. bind_rdataset(rbtdb, rbtnode, foundsig, now,
  5060. sigrdataset);
  5061. }
  5062. NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
  5063. isc_rwlocktype_read);
  5064. if (close_version)
  5065. closeversion(db, (dns_dbversion_t **) (void *)(&rbtversion),
  5066. ISC_FALSE);
  5067. if (found == NULL)
  5068. return (ISC_R_NOTFOUND);
  5069. return (ISC_R_SUCCESS);
  5070. }
  5071. static isc_result_t
  5072. cache_findrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
  5073. dns_rdatatype_t type, dns_rdatatype_t covers,
  5074. isc_stdtime_t now, dns_rdataset_t *rdataset,
  5075. dns_rdataset_t *sigrdataset)
  5076. {
  5077. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
  5078. dns_rbtnode_t *rbtnode = (dns_rbtnode_t *)node;
  5079. rdatasetheader_t *header, *header_next, *found, *foundsig;
  5080. rbtdb_rdatatype_t matchtype, sigmatchtype, negtype;
  5081. isc_result_t result;
  5082. nodelock_t *lock;
  5083. isc_rwlocktype_t locktype;
  5084. REQUIRE(VALID_RBTDB(rbtdb));
  5085. REQUIRE(type != dns_rdatatype_any);
  5086. UNUSED(version);
  5087. result = ISC_R_SUCCESS;
  5088. if (now == 0)
  5089. isc_stdtime_get(&now);
  5090. lock = &rbtdb->node_locks[rbtnode->locknum].lock;
  5091. locktype = isc_rwlocktype_read;
  5092. NODE_LOCK(lock, locktype);
  5093. found = NULL;
  5094. foundsig = NULL;
  5095. matchtype = RBTDB_RDATATYPE_VALUE(type, covers);
  5096. negtype = RBTDB_RDATATYPE_VALUE(0, type);
  5097. if (covers == 0)
  5098. sigmatchtype = RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig, type);
  5099. else
  5100. sigmatchtype = 0;
  5101. for (header = rbtnode->data; header != NULL; header = header_next) {
  5102. header_next = header->next;
  5103. if (header->rdh_ttl <= now) {
  5104. if ((header->rdh_ttl <= now - RBTDB_VIRTUAL) &&
  5105. (locktype == isc_rwlocktype_write ||
  5106. NODE_TRYUPGRADE(lock) == ISC_R_SUCCESS)) {
  5107. /*
  5108. * We update the node's status only when we
  5109. * can get write access.
  5110. */
  5111. locktype = isc_rwlocktype_write;
  5112. /*
  5113. * We don't check if refcurrent(rbtnode) == 0
  5114. * and try to free like we do in cache_find(),
  5115. * because refcurrent(rbtnode) must be
  5116. * non-zero. This is so because 'node' is an
  5117. * argument to the function.
  5118. */
  5119. header->attributes |= RDATASET_ATTR_STALE;
  5120. rbtnode->dirty = 1;
  5121. }
  5122. } else if (EXISTS(header)) {
  5123. if (header->type == matchtype)
  5124. found = header;
  5125. else if (header->type == RBTDB_RDATATYPE_NCACHEANY ||
  5126. header->type == negtype)
  5127. found = header;
  5128. else if (header->type == sigmatchtype)
  5129. foundsig = header;
  5130. }
  5131. }
  5132. if (found != NULL) {
  5133. bind_rdataset(rbtdb, rbtnode, found, now, rdataset);
  5134. if (!NEGATIVE(found) && foundsig != NULL)
  5135. bind_rdataset(rbtdb, rbtnode, foundsig, now,
  5136. sigrdataset);
  5137. }
  5138. NODE_UNLOCK(lock, locktype);
  5139. if (found == NULL)
  5140. return (ISC_R_NOTFOUND);
  5141. if (NEGATIVE(found)) {
  5142. /*
  5143. * We found a negative cache entry.
  5144. */
  5145. if (NXDOMAIN(found))
  5146. result = DNS_R_NCACHENXDOMAIN;
  5147. else
  5148. result = DNS_R_NCACHENXRRSET;
  5149. }
  5150. return (result);
  5151. }
  5152. static isc_result_t
  5153. allrdatasets(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
  5154. isc_stdtime_t now, dns_rdatasetiter_t **iteratorp)
  5155. {
  5156. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
  5157. dns_rbtnode_t *rbtnode = (dns_rbtnode_t *)node;
  5158. rbtdb_version_t *rbtversion = version;
  5159. rbtdb_rdatasetiter_t *iterator;
  5160. unsigned int refs;
  5161. REQUIRE(VALID_RBTDB(rbtdb));
  5162. iterator = isc_mem_get(rbtdb->common.mctx, sizeof(*iterator));
  5163. if (iterator == NULL)
  5164. return (ISC_R_NOMEMORY);
  5165. if ((db->attributes & DNS_DBATTR_CACHE) == 0) {
  5166. now = 0;
  5167. if (rbtversion == NULL)
  5168. currentversion(db,
  5169. (dns_dbversion_t **) (void *)(&rbtversion));
  5170. else {
  5171. unsigned int refs;
  5172. INSIST(rbtversion->rbtdb == rbtdb);
  5173. isc_refcount_increment(&rbtversion->references,
  5174. &refs);
  5175. INSIST(refs > 1);
  5176. }
  5177. } else {
  5178. if (now == 0)
  5179. isc_stdtime_get(&now);
  5180. rbtversion = NULL;
  5181. }
  5182. iterator->common.magic = DNS_RDATASETITER_MAGIC;
  5183. iterator->common.methods = &rdatasetiter_methods;
  5184. iterator->common.db = db;
  5185. iterator->common.node = node;
  5186. iterator->common.version = (dns_dbversion_t *)rbtversion;
  5187. iterator->common.now = now;
  5188. NODE_STRONGLOCK(&rbtdb->node_locks[rbtnode->locknum].lock);
  5189. dns_rbtnode_refincrement(rbtnode, &refs);
  5190. INSIST(refs != 0);
  5191. iterator->current = NULL;
  5192. NODE_STRONGUNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock);
  5193. *iteratorp = (dns_rdatasetiter_t *)iterator;
  5194. return (ISC_R_SUCCESS);
  5195. }
  5196. static isc_boolean_t
  5197. cname_and_other_data(dns_rbtnode_t *node, rbtdb_serial_t serial) {
  5198. rdatasetheader_t *header, *header_next;
  5199. isc_boolean_t cname, other_data;
  5200. dns_rdatatype_t rdtype;
  5201. /*
  5202. * The caller must hold the node lock.
  5203. */
  5204. /*
  5205. * Look for CNAME and "other data" rdatasets active in our version.
  5206. */
  5207. cname = ISC_FALSE;
  5208. other_data = ISC_FALSE;
  5209. for (header = node->data; header != NULL; header = header_next) {
  5210. header_next = header->next;
  5211. if (header->type == dns_rdatatype_cname) {
  5212. /*
  5213. * Look for an active extant CNAME.
  5214. */
  5215. do {
  5216. if (header->serial <= serial &&
  5217. !IGNORE(header)) {
  5218. /*
  5219. * Is this a "this rdataset doesn't
  5220. * exist" record?
  5221. */
  5222. if (NONEXISTENT(header))
  5223. header = NULL;
  5224. break;
  5225. } else
  5226. header = header->down;
  5227. } while (header != NULL);
  5228. if (header != NULL)
  5229. cname = ISC_TRUE;
  5230. } else {
  5231. /*
  5232. * Look for active extant "other data".
  5233. *
  5234. * "Other data" is any rdataset whose type is not
  5235. * KEY, NSEC, SIG or RRSIG.
  5236. */
  5237. rdtype = RBTDB_RDATATYPE_BASE(header->type);
  5238. if (rdtype != dns_rdatatype_key &&
  5239. rdtype != dns_rdatatype_sig &&
  5240. rdtype != dns_rdatatype_nsec &&
  5241. rdtype != dns_rdatatype_rrsig) {
  5242. /*
  5243. * Is it active and extant?
  5244. */
  5245. do {
  5246. if (header->serial <= serial &&
  5247. !IGNORE(header)) {
  5248. /*
  5249. * Is this a "this rdataset
  5250. * doesn't exist" record?
  5251. */
  5252. if (NONEXISTENT(header))
  5253. header = NULL;
  5254. break;
  5255. } else
  5256. header = header->down;
  5257. } while (header != NULL);
  5258. if (header != NULL)
  5259. other_data = ISC_TRUE;
  5260. }
  5261. }
  5262. }
  5263. if (cname && other_data)
  5264. return (ISC_TRUE);
  5265. return (ISC_FALSE);
  5266. }
  5267. static isc_result_t
  5268. resign_insert(dns_rbtdb_t *rbtdb, int idx, rdatasetheader_t *newheader) {
  5269. isc_result_t result;
  5270. INSIST(!IS_CACHE(rbtdb));
  5271. INSIST(newheader->heap_index == 0);
  5272. INSIST(!ISC_LINK_LINKED(newheader, link));
  5273. result = isc_heap_insert(rbtdb->heaps[idx], newheader);
  5274. return (result);
  5275. }
  5276. static isc_result_t
  5277. add(dns_rbtdb_t *rbtdb, dns_rbtnode_t *rbtnode, rbtdb_version_t *rbtversion,
  5278. rdatasetheader_t *newheader, unsigned int options, isc_boolean_t loading,
  5279. dns_rdataset_t *addedrdataset, isc_stdtime_t now)
  5280. {
  5281. rbtdb_changed_t *changed = NULL;
  5282. rdatasetheader_t *topheader, *topheader_prev, *header, *sigheader;
  5283. unsigned char *merged;
  5284. isc_result_t result;
  5285. isc_boolean_t header_nx;
  5286. isc_boolean_t newheader_nx;
  5287. isc_boolean_t merge;
  5288. dns_rdatatype_t rdtype, covers;
  5289. rbtdb_rdatatype_t negtype, sigtype;
  5290. dns_trust_t trust;
  5291. int idx;
  5292. /*
  5293. * Add an rdatasetheader_t to a node.
  5294. */
  5295. /*
  5296. * Caller must be holding the node lock.
  5297. */
  5298. if ((options & DNS_DBADD_MERGE) != 0) {
  5299. REQUIRE(rbtversion != NULL);
  5300. merge = ISC_TRUE;
  5301. } else
  5302. merge = ISC_FALSE;
  5303. if ((options & DNS_DBADD_FORCE) != 0)
  5304. trust = dns_trust_ultimate;
  5305. else
  5306. trust = newheader->trust;
  5307. if (rbtversion != NULL && !loading) {
  5308. /*
  5309. * We always add a changed record, even if no changes end up
  5310. * being made to this node, because it's harmless and
  5311. * simplifies the code.
  5312. */
  5313. changed = add_changed(rbtdb, rbtversion, rbtnode);
  5314. if (changed == NULL) {
  5315. free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
  5316. return (ISC_R_NOMEMORY);
  5317. }
  5318. }
  5319. newheader_nx = NONEXISTENT(newheader) ? ISC_TRUE : ISC_FALSE;
  5320. topheader_prev = NULL;
  5321. sigheader = NULL;
  5322. negtype = 0;
  5323. if (rbtversion == NULL && !newheader_nx) {
  5324. rdtype = RBTDB_RDATATYPE_BASE(newheader->type);
  5325. if (NEGATIVE(newheader)) {
  5326. /*
  5327. * We're adding a negative cache entry.
  5328. */
  5329. covers = RBTDB_RDATATYPE_EXT(newheader->type);
  5330. sigtype = RBTDB_RDATATYPE_VALUE(dns_rdatatype_rrsig,
  5331. covers);
  5332. for (topheader = rbtnode->data;
  5333. topheader != NULL;
  5334. topheader = topheader->next) {
  5335. /*
  5336. * If we're adding an negative cache entry
  5337. * which covers all types (NXDOMAIN,
  5338. * NODATA(QTYPE=ANY)).
  5339. *
  5340. * We make all other data stale so that the
  5341. * only rdataset that can be found at this
  5342. * node is the negative cache entry.
  5343. *
  5344. * Otherwise look for any RRSIGs of the
  5345. * given type so they can be marked stale
  5346. * later.
  5347. */
  5348. if (covers == dns_rdatatype_any) {
  5349. set_ttl(rbtdb, topheader, 0);
  5350. topheader->attributes |=
  5351. RDATASET_ATTR_STALE;
  5352. rbtnode->dirty = 1;
  5353. } else if (topheader->type == sigtype)
  5354. sigheader = topheader;
  5355. }
  5356. if (covers == dns_rdatatype_any)
  5357. goto find_header;
  5358. negtype = RBTDB_RDATATYPE_VALUE(covers, 0);
  5359. } else {
  5360. /*
  5361. * We're adding something that isn't a
  5362. * negative cache entry. Look for an extant
  5363. * non-stale NXDOMAIN/NODATA(QTYPE=ANY) negative
  5364. * cache entry.
  5365. */
  5366. for (topheader = rbtnode->data;
  5367. topheader != NULL;
  5368. topheader = topheader->next) {
  5369. if (topheader->type ==
  5370. RBTDB_RDATATYPE_NCACHEANY)
  5371. break;
  5372. }
  5373. if (topheader != NULL && EXISTS(topheader) &&
  5374. topheader->rdh_ttl > now) {
  5375. /*
  5376. * Found one.
  5377. */
  5378. if (trust < topheader->trust) {
  5379. /*
  5380. * The NXDOMAIN/NODATA(QTYPE=ANY)
  5381. * is more trusted.
  5382. */
  5383. free_rdataset(rbtdb,
  5384. rbtdb->common.mctx,
  5385. newheader);
  5386. if (addedrdataset != NULL)
  5387. bind_rdataset(rbtdb, rbtnode,
  5388. topheader, now,
  5389. addedrdataset);
  5390. return (DNS_R_UNCHANGED);
  5391. }
  5392. /*
  5393. * The new rdataset is better. Expire the
  5394. * NXDOMAIN/NODATA(QTYPE=ANY).
  5395. */
  5396. set_ttl(rbtdb, topheader, 0);
  5397. topheader->attributes |= RDATASET_ATTR_STALE;
  5398. rbtnode->dirty = 1;
  5399. topheader = NULL;
  5400. goto find_header;
  5401. }
  5402. negtype = RBTDB_RDATATYPE_VALUE(0, rdtype);
  5403. }
  5404. }
  5405. for (topheader = rbtnode->data;
  5406. topheader != NULL;
  5407. topheader = topheader->next) {
  5408. if (topheader->type == newheader->type ||
  5409. topheader->type == negtype)
  5410. break;
  5411. topheader_prev = topheader;
  5412. }
  5413. find_header:
  5414. /*
  5415. * If header isn't NULL, we've found the right type. There may be
  5416. * IGNORE rdatasets between the top of the chain and the first real
  5417. * data. We skip over them.
  5418. */
  5419. header = topheader;
  5420. while (header != NULL && IGNORE(header))
  5421. header = header->down;
  5422. if (header != NULL) {
  5423. header_nx = NONEXISTENT(header) ? ISC_TRUE : ISC_FALSE;
  5424. /*
  5425. * Deleting an already non-existent rdataset has no effect.
  5426. */
  5427. if (header_nx && newheader_nx) {
  5428. free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
  5429. return (DNS_R_UNCHANGED);
  5430. }
  5431. /*
  5432. * Trying to add an rdataset with lower trust to a cache DB
  5433. * has no effect, provided that the cache data isn't stale.
  5434. */
  5435. if (rbtversion == NULL && trust < header->trust &&
  5436. (header->rdh_ttl > now || header_nx)) {
  5437. free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
  5438. if (addedrdataset != NULL)
  5439. bind_rdataset(rbtdb, rbtnode, header, now,
  5440. addedrdataset);
  5441. return (DNS_R_UNCHANGED);
  5442. }
  5443. /*
  5444. * Don't merge if a nonexistent rdataset is involved.
  5445. */
  5446. if (merge && (header_nx || newheader_nx))
  5447. merge = ISC_FALSE;
  5448. /*
  5449. * If 'merge' is ISC_TRUE, we'll try to create a new rdataset
  5450. * that is the union of 'newheader' and 'header'.
  5451. */
  5452. if (merge) {
  5453. unsigned int flags = 0;
  5454. INSIST(rbtversion->serial >= header->serial);
  5455. merged = NULL;
  5456. result = ISC_R_SUCCESS;
  5457. if ((options & DNS_DBADD_EXACT) != 0)
  5458. flags |= DNS_RDATASLAB_EXACT;
  5459. if ((options & DNS_DBADD_EXACTTTL) != 0 &&
  5460. newheader->rdh_ttl != header->rdh_ttl)
  5461. result = DNS_R_NOTEXACT;
  5462. else if (newheader->rdh_ttl != header->rdh_ttl)
  5463. flags |= DNS_RDATASLAB_FORCE;
  5464. if (result == ISC_R_SUCCESS)
  5465. result = dns_rdataslab_merge(
  5466. (unsigned char *)header,
  5467. (unsigned char *)newheader,
  5468. (unsigned int)(sizeof(*newheader)),
  5469. rbtdb->common.mctx,
  5470. rbtdb->common.rdclass,
  5471. (dns_rdatatype_t)header->type,
  5472. flags, &merged);
  5473. if (result == ISC_R_SUCCESS) {
  5474. /*
  5475. * If 'header' has the same serial number as
  5476. * we do, we could clean it up now if we knew
  5477. * that our caller had no references to it.
  5478. * We don't know this, however, so we leave it
  5479. * alone. It will get cleaned up when
  5480. * clean_zone_node() runs.
  5481. */
  5482. free_rdataset(rbtdb, rbtdb->common.mctx,
  5483. newheader);
  5484. newheader = (rdatasetheader_t *)merged;
  5485. init_rdataset(rbtdb, newheader);
  5486. if (loading && RESIGN(newheader) &&
  5487. RESIGN(header) &&
  5488. header->resign < newheader->resign)
  5489. newheader->resign = header->resign;
  5490. } else {
  5491. free_rdataset(rbtdb, rbtdb->common.mctx,
  5492. newheader);
  5493. return (result);
  5494. }
  5495. }
  5496. /*
  5497. * Don't replace existing NS, A and AAAA RRsets
  5498. * in the cache if they are already exist. This
  5499. * prevents named being locked to old servers.
  5500. * Don't lower trust of existing record if the
  5501. * update is forced.
  5502. */
  5503. if (IS_CACHE(rbtdb) && header->rdh_ttl > now &&
  5504. header->type == dns_rdatatype_ns &&
  5505. !header_nx && !newheader_nx &&
  5506. header->trust >= newheader->trust &&
  5507. dns_rdataslab_equalx((unsigned char *)header,
  5508. (unsigned char *)newheader,
  5509. (unsigned int)(sizeof(*newheader)),
  5510. rbtdb->common.rdclass,
  5511. (dns_rdatatype_t)header->type)) {
  5512. /*
  5513. * Honour the new ttl if it is less than the
  5514. * older one.
  5515. */
  5516. if (header->rdh_ttl > newheader->rdh_ttl)
  5517. set_ttl(rbtdb, header, newheader->rdh_ttl);
  5518. if (header->noqname == NULL &&
  5519. newheader->noqname != NULL) {
  5520. header->noqname = newheader->noqname;
  5521. newheader->noqname = NULL;
  5522. }
  5523. if (header->closest == NULL &&
  5524. newheader->closest != NULL) {
  5525. header->closest = newheader->closest;
  5526. newheader->closest = NULL;
  5527. }
  5528. free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
  5529. if (addedrdataset != NULL)
  5530. bind_rdataset(rbtdb, rbtnode, header, now,
  5531. addedrdataset);
  5532. return (ISC_R_SUCCESS);
  5533. }
  5534. /*
  5535. * If we have will be replacing a NS RRset force its TTL
  5536. * to be no more than the current NS RRset's TTL. This
  5537. * ensures the delegations that are withdrawn are honoured.
  5538. */
  5539. if (IS_CACHE(rbtdb) && header->rdh_ttl > now &&
  5540. header->type == dns_rdatatype_ns &&
  5541. !header_nx && !newheader_nx &&
  5542. header->trust <= newheader->trust) {
  5543. if (newheader->rdh_ttl > header->rdh_ttl) {
  5544. newheader->rdh_ttl = header->rdh_ttl;
  5545. }
  5546. }
  5547. if (IS_CACHE(rbtdb) && header->rdh_ttl > now &&
  5548. (header->type == dns_rdatatype_a ||
  5549. header->type == dns_rdatatype_aaaa) &&
  5550. !header_nx && !newheader_nx &&
  5551. header->trust >= newheader->trust &&
  5552. dns_rdataslab_equal((unsigned char *)header,
  5553. (unsigned char *)newheader,
  5554. (unsigned int)(sizeof(*newheader)))) {
  5555. /*
  5556. * Honour the new ttl if it is less than the
  5557. * older one.
  5558. */
  5559. if (header->rdh_ttl > newheader->rdh_ttl)
  5560. set_ttl(rbtdb, header, newheader->rdh_ttl);
  5561. if (header->noqname == NULL &&
  5562. newheader->noqname != NULL) {
  5563. header->noqname = newheader->noqname;
  5564. newheader->noqname = NULL;
  5565. }
  5566. if (header->closest == NULL &&
  5567. newheader->closest != NULL) {
  5568. header->closest = newheader->closest;
  5569. newheader->closest = NULL;
  5570. }
  5571. free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
  5572. if (addedrdataset != NULL)
  5573. bind_rdataset(rbtdb, rbtnode, header, now,
  5574. addedrdataset);
  5575. return (ISC_R_SUCCESS);
  5576. }
  5577. INSIST(rbtversion == NULL ||
  5578. rbtversion->serial >= topheader->serial);
  5579. if (topheader_prev != NULL)
  5580. topheader_prev->next = newheader;
  5581. else
  5582. rbtnode->data = newheader;
  5583. newheader->next = topheader->next;
  5584. if (loading) {
  5585. /*
  5586. * There are no other references to 'header' when
  5587. * loading, so we MAY clean up 'header' now.
  5588. * Since we don't generate changed records when
  5589. * loading, we MUST clean up 'header' now.
  5590. */
  5591. newheader->down = NULL;
  5592. free_rdataset(rbtdb, rbtdb->common.mctx, header);
  5593. } else {
  5594. newheader->down = topheader;
  5595. topheader->next = newheader;
  5596. rbtnode->dirty = 1;
  5597. if (changed != NULL)
  5598. changed->dirty = ISC_TRUE;
  5599. if (rbtversion == NULL) {
  5600. set_ttl(rbtdb, header, 0);
  5601. header->attributes |= RDATASET_ATTR_STALE;
  5602. if (sigheader != NULL) {
  5603. set_ttl(rbtdb, sigheader, 0);
  5604. sigheader->attributes |=
  5605. RDATASET_ATTR_STALE;
  5606. }
  5607. }
  5608. idx = newheader->node->locknum;
  5609. if (IS_CACHE(rbtdb)) {
  5610. ISC_LIST_PREPEND(rbtdb->rdatasets[idx],
  5611. newheader, link);
  5612. /*
  5613. * XXXMLG We don't check the return value
  5614. * here. If it fails, we will not do TTL
  5615. * based expiry on this node. However, we
  5616. * will do it on the LRU side, so memory
  5617. * will not leak... for long.
  5618. */
  5619. isc_heap_insert(rbtdb->heaps[idx], newheader);
  5620. } else if (RESIGN(newheader))
  5621. resign_insert(rbtdb, idx, newheader);
  5622. }
  5623. } else {
  5624. /*
  5625. * No non-IGNORED rdatasets of the given type exist at
  5626. * this node.
  5627. */
  5628. /*
  5629. * If we're trying to delete the type, don't bother.
  5630. */
  5631. if (newheader_nx) {
  5632. free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
  5633. return (DNS_R_UNCHANGED);
  5634. }
  5635. if (topheader != NULL) {
  5636. /*
  5637. * We have an list of rdatasets of the given type,
  5638. * but they're all marked IGNORE. We simply insert
  5639. * the new rdataset at the head of the list.
  5640. *
  5641. * Ignored rdatasets cannot occur during loading, so
  5642. * we INSIST on it.
  5643. */
  5644. INSIST(!loading);
  5645. INSIST(rbtversion == NULL ||
  5646. rbtversion->serial >= topheader->serial);
  5647. if (topheader_prev != NULL)
  5648. topheader_prev->next = newheader;
  5649. else
  5650. rbtnode->data = newheader;
  5651. newheader->next = topheader->next;
  5652. newheader->down = topheader;
  5653. topheader->next = newheader;
  5654. rbtnode->dirty = 1;
  5655. if (changed != NULL)
  5656. changed->dirty = ISC_TRUE;
  5657. } else {
  5658. /*
  5659. * No rdatasets of the given type exist at the node.
  5660. */
  5661. newheader->next = rbtnode->data;
  5662. newheader->down = NULL;
  5663. rbtnode->data = newheader;
  5664. }
  5665. idx = newheader->node->locknum;
  5666. if (IS_CACHE(rbtdb)) {
  5667. ISC_LIST_PREPEND(rbtdb->rdatasets[idx],
  5668. newheader, link);
  5669. isc_heap_insert(rbtdb->heaps[idx], newheader);
  5670. } else if (RESIGN(newheader)) {
  5671. resign_insert(rbtdb, idx, newheader);
  5672. }
  5673. }
  5674. /*
  5675. * Check if the node now contains CNAME and other data.
  5676. */
  5677. if (rbtversion != NULL &&
  5678. cname_and_other_data(rbtnode, rbtversion->serial))
  5679. return (DNS_R_CNAMEANDOTHER);
  5680. if (addedrdataset != NULL)
  5681. bind_rdataset(rbtdb, rbtnode, newheader, now, addedrdataset);
  5682. return (ISC_R_SUCCESS);
  5683. }
  5684. static inline isc_boolean_t
  5685. delegating_type(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node,
  5686. rbtdb_rdatatype_t type)
  5687. {
  5688. if (IS_CACHE(rbtdb)) {
  5689. if (type == dns_rdatatype_dname)
  5690. return (ISC_TRUE);
  5691. else
  5692. return (ISC_FALSE);
  5693. } else if (type == dns_rdatatype_dname ||
  5694. (type == dns_rdatatype_ns &&
  5695. (node != rbtdb->origin_node || IS_STUB(rbtdb))))
  5696. return (ISC_TRUE);
  5697. return (ISC_FALSE);
  5698. }
  5699. static inline isc_result_t
  5700. addnoqname(dns_rbtdb_t *rbtdb, rdatasetheader_t *newheader,
  5701. dns_rdataset_t *rdataset)
  5702. {
  5703. struct noqname *noqname;
  5704. isc_mem_t *mctx = rbtdb->common.mctx;
  5705. dns_name_t name;
  5706. dns_rdataset_t neg, negsig;
  5707. isc_result_t result;
  5708. isc_region_t r;
  5709. dns_name_init(&name, NULL);
  5710. dns_rdataset_init(&neg);
  5711. dns_rdataset_init(&negsig);
  5712. result = dns_rdataset_getnoqname(rdataset, &name, &neg, &negsig);
  5713. RUNTIME_CHECK(result == ISC_R_SUCCESS);
  5714. noqname = isc_mem_get(mctx, sizeof(*noqname));
  5715. if (noqname == NULL) {
  5716. result = ISC_R_NOMEMORY;
  5717. goto cleanup;
  5718. }
  5719. dns_name_init(&noqname->name, NULL);
  5720. noqname->neg = NULL;
  5721. noqname->negsig = NULL;
  5722. noqname->type = neg.type;
  5723. result = dns_name_dup(&name, mctx, &noqname->name);
  5724. if (result != ISC_R_SUCCESS)
  5725. goto cleanup;
  5726. result = dns_rdataslab_fromrdataset(&neg, mctx, &r, 0);
  5727. if (result != ISC_R_SUCCESS)
  5728. goto cleanup;
  5729. noqname->neg = r.base;
  5730. result = dns_rdataslab_fromrdataset(&negsig, mctx, &r, 0);
  5731. if (result != ISC_R_SUCCESS)
  5732. goto cleanup;
  5733. noqname->negsig = r.base;
  5734. dns_rdataset_disassociate(&neg);
  5735. dns_rdataset_disassociate(&negsig);
  5736. newheader->noqname = noqname;
  5737. return (ISC_R_SUCCESS);
  5738. cleanup:
  5739. dns_rdataset_disassociate(&neg);
  5740. dns_rdataset_disassociate(&negsig);
  5741. free_noqname(mctx, &noqname);
  5742. return(result);
  5743. }
  5744. static inline isc_result_t
  5745. addclosest(dns_rbtdb_t *rbtdb, rdatasetheader_t *newheader,
  5746. dns_rdataset_t *rdataset)
  5747. {
  5748. struct noqname *closest;
  5749. isc_mem_t *mctx = rbtdb->common.mctx;
  5750. dns_name_t name;
  5751. dns_rdataset_t neg, negsig;
  5752. isc_result_t result;
  5753. isc_region_t r;
  5754. dns_name_init(&name, NULL);
  5755. dns_rdataset_init(&neg);
  5756. dns_rdataset_init(&negsig);
  5757. result = dns_rdataset_getclosest(rdataset, &name, &neg, &negsig);
  5758. RUNTIME_CHECK(result == ISC_R_SUCCESS);
  5759. closest = isc_mem_get(mctx, sizeof(*closest));
  5760. if (closest == NULL) {
  5761. result = ISC_R_NOMEMORY;
  5762. goto cleanup;
  5763. }
  5764. dns_name_init(&closest->name, NULL);
  5765. closest->neg = NULL;
  5766. closest->negsig = NULL;
  5767. closest->type = neg.type;
  5768. result = dns_name_dup(&name, mctx, &closest->name);
  5769. if (result != ISC_R_SUCCESS)
  5770. goto cleanup;
  5771. result = dns_rdataslab_fromrdataset(&neg, mctx, &r, 0);
  5772. if (result != ISC_R_SUCCESS)
  5773. goto cleanup;
  5774. closest->neg = r.base;
  5775. result = dns_rdataslab_fromrdataset(&negsig, mctx, &r, 0);
  5776. if (result != ISC_R_SUCCESS)
  5777. goto cleanup;
  5778. closest->negsig = r.base;
  5779. dns_rdataset_disassociate(&neg);
  5780. dns_rdataset_disassociate(&negsig);
  5781. newheader->closest = closest;
  5782. return (ISC_R_SUCCESS);
  5783. cleanup:
  5784. dns_rdataset_disassociate(&neg);
  5785. dns_rdataset_disassociate(&negsig);
  5786. free_noqname(mctx, &closest);
  5787. return(result);
  5788. }
  5789. static dns_dbmethods_t zone_methods;
  5790. static isc_result_t
  5791. addrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
  5792. isc_stdtime_t now, dns_rdataset_t *rdataset, unsigned int options,
  5793. dns_rdataset_t *addedrdataset)
  5794. {
  5795. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
  5796. dns_rbtnode_t *rbtnode = (dns_rbtnode_t *)node;
  5797. rbtdb_version_t *rbtversion = version;
  5798. isc_region_t region;
  5799. rdatasetheader_t *newheader;
  5800. rdatasetheader_t *header;
  5801. isc_result_t result;
  5802. isc_boolean_t delegating;
  5803. isc_boolean_t newnsec;
  5804. isc_boolean_t tree_locked = ISC_FALSE;
  5805. isc_boolean_t cache_is_overmem = ISC_FALSE;
  5806. REQUIRE(VALID_RBTDB(rbtdb));
  5807. INSIST(rbtversion == NULL || rbtversion->rbtdb == rbtdb);
  5808. if (rbtdb->common.methods == &zone_methods)
  5809. REQUIRE(((rbtnode->nsec == DNS_RBT_NSEC_NSEC3 &&
  5810. (rdataset->type == dns_rdatatype_nsec3 ||
  5811. rdataset->covers == dns_rdatatype_nsec3)) ||
  5812. (rbtnode->nsec != DNS_RBT_NSEC_NSEC3 &&
  5813. rdataset->type != dns_rdatatype_nsec3 &&
  5814. rdataset->covers != dns_rdatatype_nsec3)));
  5815. if (rbtversion == NULL) {
  5816. if (now == 0)
  5817. isc_stdtime_get(&now);
  5818. } else
  5819. now = 0;
  5820. result = dns_rdataslab_fromrdataset(rdataset, rbtdb->common.mctx,
  5821. &region, sizeof(rdatasetheader_t));
  5822. if (result != ISC_R_SUCCESS)
  5823. return (result);
  5824. newheader = (rdatasetheader_t *)region.base;
  5825. init_rdataset(rbtdb, newheader);
  5826. set_ttl(rbtdb, newheader, rdataset->ttl + now);
  5827. newheader->type = RBTDB_RDATATYPE_VALUE(rdataset->type,
  5828. rdataset->covers);
  5829. newheader->attributes = 0;
  5830. newheader->noqname = NULL;
  5831. newheader->closest = NULL;
  5832. newheader->count = init_count++;
  5833. newheader->trust = rdataset->trust;
  5834. newheader->additional_auth = NULL;
  5835. newheader->additional_glue = NULL;
  5836. newheader->last_used = now;
  5837. newheader->node = rbtnode;
  5838. if (rbtversion != NULL) {
  5839. newheader->serial = rbtversion->serial;
  5840. now = 0;
  5841. if ((rdataset->attributes & DNS_RDATASETATTR_RESIGN) != 0) {
  5842. newheader->attributes |= RDATASET_ATTR_RESIGN;
  5843. newheader->resign = rdataset->resign;
  5844. } else
  5845. newheader->resign = 0;
  5846. } else {
  5847. newheader->serial = 1;
  5848. newheader->resign = 0;
  5849. if ((rdataset->attributes & DNS_RDATASETATTR_NEGATIVE) != 0)
  5850. newheader->attributes |= RDATASET_ATTR_NEGATIVE;
  5851. if ((rdataset->attributes & DNS_RDATASETATTR_NXDOMAIN) != 0)
  5852. newheader->attributes |= RDATASET_ATTR_NXDOMAIN;
  5853. if ((rdataset->attributes & DNS_RDATASETATTR_OPTOUT) != 0)
  5854. newheader->attributes |= RDATASET_ATTR_OPTOUT;
  5855. if ((rdataset->attributes & DNS_RDATASETATTR_NOQNAME) != 0) {
  5856. result = addnoqname(rbtdb, newheader, rdataset);
  5857. if (result != ISC_R_SUCCESS) {
  5858. free_rdataset(rbtdb, rbtdb->common.mctx,
  5859. newheader);
  5860. return (result);
  5861. }
  5862. }
  5863. if ((rdataset->attributes & DNS_RDATASETATTR_CLOSEST) != 0) {
  5864. result = addclosest(rbtdb, newheader, rdataset);
  5865. if (result != ISC_R_SUCCESS) {
  5866. free_rdataset(rbtdb, rbtdb->common.mctx,
  5867. newheader);
  5868. return (result);
  5869. }
  5870. }
  5871. }
  5872. /*
  5873. * If we're adding a delegation type (e.g. NS or DNAME for a zone,
  5874. * just DNAME for the cache), then we need to set the callback bit
  5875. * on the node.
  5876. */
  5877. if (delegating_type(rbtdb, rbtnode, rdataset->type))
  5878. delegating = ISC_TRUE;
  5879. else
  5880. delegating = ISC_FALSE;
  5881. /*
  5882. * Add to the auxiliary NSEC tree if we're adding an NSEC record.
  5883. */
  5884. if (rbtnode->nsec != DNS_RBT_NSEC_HAS_NSEC &&
  5885. rdataset->type == dns_rdatatype_nsec)
  5886. newnsec = ISC_TRUE;
  5887. else
  5888. newnsec = ISC_FALSE;
  5889. /*
  5890. * If we're adding a delegation type, adding to the auxiliary NSEC tree,
  5891. * or the DB is a cache in an overmem state, hold an exclusive lock on
  5892. * the tree. In the latter case the lock does not necessarily have to
  5893. * be acquired but it will help purge stale entries more effectively.
  5894. */
  5895. if (IS_CACHE(rbtdb) && isc_mem_isovermem(rbtdb->common.mctx))
  5896. cache_is_overmem = ISC_TRUE;
  5897. if (delegating || newnsec || cache_is_overmem) {
  5898. tree_locked = ISC_TRUE;
  5899. RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
  5900. }
  5901. if (cache_is_overmem)
  5902. overmem_purge(rbtdb, rbtnode->locknum, now, tree_locked);
  5903. NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
  5904. isc_rwlocktype_write);
  5905. if (rbtdb->rrsetstats != NULL) {
  5906. newheader->attributes |= RDATASET_ATTR_STATCOUNT;
  5907. update_rrsetstats(rbtdb, newheader, ISC_TRUE);
  5908. }
  5909. if (IS_CACHE(rbtdb)) {
  5910. if (tree_locked)
  5911. cleanup_dead_nodes(rbtdb, rbtnode->locknum);
  5912. header = isc_heap_element(rbtdb->heaps[rbtnode->locknum], 1);
  5913. if (header && header->rdh_ttl <= now - RBTDB_VIRTUAL)
  5914. expire_header(rbtdb, header, tree_locked);
  5915. /*
  5916. * If we've been holding a write lock on the tree just for
  5917. * cleaning, we can release it now. However, we still need the
  5918. * node lock.
  5919. */
  5920. if (tree_locked && !delegating && !newnsec) {
  5921. RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
  5922. tree_locked = ISC_FALSE;
  5923. }
  5924. }
  5925. result = ISC_R_SUCCESS;
  5926. if (newnsec) {
  5927. dns_fixedname_t fname;
  5928. dns_name_t *name;
  5929. dns_rbtnode_t *nsecnode;
  5930. dns_fixedname_init(&fname);
  5931. name = dns_fixedname_name(&fname);
  5932. dns_rbt_fullnamefromnode(rbtnode, name);
  5933. nsecnode = NULL;
  5934. result = dns_rbt_addnode(rbtdb->nsec, name, &nsecnode);
  5935. if (result == ISC_R_SUCCESS) {
  5936. nsecnode->nsec = DNS_RBT_NSEC_NSEC;
  5937. rbtnode->nsec = DNS_RBT_NSEC_HAS_NSEC;
  5938. } else if (result == ISC_R_EXISTS) {
  5939. rbtnode->nsec = DNS_RBT_NSEC_HAS_NSEC;
  5940. result = ISC_R_SUCCESS;
  5941. }
  5942. }
  5943. if (result == ISC_R_SUCCESS)
  5944. result = add(rbtdb, rbtnode, rbtversion, newheader, options,
  5945. ISC_FALSE, addedrdataset, now);
  5946. if (result == ISC_R_SUCCESS && delegating)
  5947. rbtnode->find_callback = 1;
  5948. NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
  5949. isc_rwlocktype_write);
  5950. if (tree_locked)
  5951. RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
  5952. /*
  5953. * Update the zone's secure status. If version is non-NULL
  5954. * this is deferred until closeversion() is called.
  5955. */
  5956. if (result == ISC_R_SUCCESS && version == NULL && !IS_CACHE(rbtdb))
  5957. iszonesecure(db, version, rbtdb->origin_node);
  5958. return (result);
  5959. }
  5960. static isc_result_t
  5961. subtractrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
  5962. dns_rdataset_t *rdataset, unsigned int options,
  5963. dns_rdataset_t *newrdataset)
  5964. {
  5965. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
  5966. dns_rbtnode_t *rbtnode = (dns_rbtnode_t *)node;
  5967. rbtdb_version_t *rbtversion = version;
  5968. rdatasetheader_t *topheader, *topheader_prev, *header, *newheader;
  5969. unsigned char *subresult;
  5970. isc_region_t region;
  5971. isc_result_t result;
  5972. rbtdb_changed_t *changed;
  5973. REQUIRE(VALID_RBTDB(rbtdb));
  5974. REQUIRE(rbtversion != NULL && rbtversion->rbtdb == rbtdb);
  5975. if (rbtdb->common.methods == &zone_methods)
  5976. REQUIRE(((rbtnode->nsec == DNS_RBT_NSEC_NSEC3 &&
  5977. (rdataset->type == dns_rdatatype_nsec3 ||
  5978. rdataset->covers == dns_rdatatype_nsec3)) ||
  5979. (rbtnode->nsec != DNS_RBT_NSEC_NSEC3 &&
  5980. rdataset->type != dns_rdatatype_nsec3 &&
  5981. rdataset->covers != dns_rdatatype_nsec3)));
  5982. result = dns_rdataslab_fromrdataset(rdataset, rbtdb->common.mctx,
  5983. &region,
  5984. sizeof(rdatasetheader_t));
  5985. if (result != ISC_R_SUCCESS)
  5986. return (result);
  5987. newheader = (rdatasetheader_t *)region.base;
  5988. init_rdataset(rbtdb, newheader);
  5989. set_ttl(rbtdb, newheader, rdataset->ttl);
  5990. newheader->type = RBTDB_RDATATYPE_VALUE(rdataset->type,
  5991. rdataset->covers);
  5992. newheader->attributes = 0;
  5993. newheader->serial = rbtversion->serial;
  5994. newheader->trust = 0;
  5995. newheader->noqname = NULL;
  5996. newheader->closest = NULL;
  5997. newheader->count = init_count++;
  5998. newheader->additional_auth = NULL;
  5999. newheader->additional_glue = NULL;
  6000. newheader->last_used = 0;
  6001. newheader->node = rbtnode;
  6002. if ((rdataset->attributes & DNS_RDATASETATTR_RESIGN) != 0) {
  6003. newheader->attributes |= RDATASET_ATTR_RESIGN;
  6004. newheader->resign = rdataset->resign;
  6005. } else
  6006. newheader->resign = 0;
  6007. NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
  6008. isc_rwlocktype_write);
  6009. changed = add_changed(rbtdb, rbtversion, rbtnode);
  6010. if (changed == NULL) {
  6011. free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
  6012. NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
  6013. isc_rwlocktype_write);
  6014. return (ISC_R_NOMEMORY);
  6015. }
  6016. topheader_prev = NULL;
  6017. for (topheader = rbtnode->data;
  6018. topheader != NULL;
  6019. topheader = topheader->next) {
  6020. if (topheader->type == newheader->type)
  6021. break;
  6022. topheader_prev = topheader;
  6023. }
  6024. /*
  6025. * If header isn't NULL, we've found the right type. There may be
  6026. * IGNORE rdatasets between the top of the chain and the first real
  6027. * data. We skip over them.
  6028. */
  6029. header = topheader;
  6030. while (header != NULL && IGNORE(header))
  6031. header = header->down;
  6032. if (header != NULL && EXISTS(header)) {
  6033. unsigned int flags = 0;
  6034. subresult = NULL;
  6035. result = ISC_R_SUCCESS;
  6036. if ((options & DNS_DBSUB_EXACT) != 0) {
  6037. flags |= DNS_RDATASLAB_EXACT;
  6038. if (newheader->rdh_ttl != header->rdh_ttl)
  6039. result = DNS_R_NOTEXACT;
  6040. }
  6041. if (result == ISC_R_SUCCESS)
  6042. result = dns_rdataslab_subtract(
  6043. (unsigned char *)header,
  6044. (unsigned char *)newheader,
  6045. (unsigned int)(sizeof(*newheader)),
  6046. rbtdb->common.mctx,
  6047. rbtdb->common.rdclass,
  6048. (dns_rdatatype_t)header->type,
  6049. flags, &subresult);
  6050. if (result == ISC_R_SUCCESS) {
  6051. free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
  6052. newheader = (rdatasetheader_t *)subresult;
  6053. init_rdataset(rbtdb, newheader);
  6054. /*
  6055. * We have to set the serial since the rdataslab
  6056. * subtraction routine copies the reserved portion of
  6057. * header, not newheader.
  6058. */
  6059. newheader->serial = rbtversion->serial;
  6060. /*
  6061. * XXXJT: dns_rdataslab_subtract() copied the pointers
  6062. * to additional info. We need to clear these fields
  6063. * to avoid having duplicated references.
  6064. */
  6065. newheader->additional_auth = NULL;
  6066. newheader->additional_glue = NULL;
  6067. } else if (result == DNS_R_NXRRSET) {
  6068. /*
  6069. * This subtraction would remove all of the rdata;
  6070. * add a nonexistent header instead.
  6071. */
  6072. free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
  6073. newheader = new_rdataset(rbtdb, rbtdb->common.mctx);
  6074. if (newheader == NULL) {
  6075. result = ISC_R_NOMEMORY;
  6076. goto unlock;
  6077. }
  6078. set_ttl(rbtdb, newheader, 0);
  6079. newheader->type = topheader->type;
  6080. newheader->attributes = RDATASET_ATTR_NONEXISTENT;
  6081. newheader->trust = 0;
  6082. newheader->serial = rbtversion->serial;
  6083. newheader->noqname = NULL;
  6084. newheader->closest = NULL;
  6085. newheader->count = 0;
  6086. newheader->additional_auth = NULL;
  6087. newheader->additional_glue = NULL;
  6088. newheader->node = rbtnode;
  6089. newheader->resign = 0;
  6090. newheader->last_used = 0;
  6091. } else {
  6092. free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
  6093. goto unlock;
  6094. }
  6095. /*
  6096. * If we're here, we want to link newheader in front of
  6097. * topheader.
  6098. */
  6099. INSIST(rbtversion->serial >= topheader->serial);
  6100. if (topheader_prev != NULL)
  6101. topheader_prev->next = newheader;
  6102. else
  6103. rbtnode->data = newheader;
  6104. newheader->next = topheader->next;
  6105. newheader->down = topheader;
  6106. topheader->next = newheader;
  6107. rbtnode->dirty = 1;
  6108. changed->dirty = ISC_TRUE;
  6109. } else {
  6110. /*
  6111. * The rdataset doesn't exist, so we don't need to do anything
  6112. * to satisfy the deletion request.
  6113. */
  6114. free_rdataset(rbtdb, rbtdb->common.mctx, newheader);
  6115. if ((options & DNS_DBSUB_EXACT) != 0)
  6116. result = DNS_R_NOTEXACT;
  6117. else
  6118. result = DNS_R_UNCHANGED;
  6119. }
  6120. if (result == ISC_R_SUCCESS && newrdataset != NULL)
  6121. bind_rdataset(rbtdb, rbtnode, newheader, 0, newrdataset);
  6122. unlock:
  6123. NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
  6124. isc_rwlocktype_write);
  6125. /*
  6126. * Update the zone's secure status. If version is non-NULL
  6127. * this is deferred until closeversion() is called.
  6128. */
  6129. if (result == ISC_R_SUCCESS && version == NULL && !IS_CACHE(rbtdb))
  6130. iszonesecure(db, rbtdb->current_version, rbtdb->origin_node);
  6131. return (result);
  6132. }
  6133. static isc_result_t
  6134. deleterdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
  6135. dns_rdatatype_t type, dns_rdatatype_t covers)
  6136. {
  6137. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
  6138. dns_rbtnode_t *rbtnode = (dns_rbtnode_t *)node;
  6139. rbtdb_version_t *rbtversion = version;
  6140. isc_result_t result;
  6141. rdatasetheader_t *newheader;
  6142. REQUIRE(VALID_RBTDB(rbtdb));
  6143. INSIST(rbtversion == NULL || rbtversion->rbtdb == rbtdb);
  6144. if (type == dns_rdatatype_any)
  6145. return (ISC_R_NOTIMPLEMENTED);
  6146. if (type == dns_rdatatype_rrsig && covers == 0)
  6147. return (ISC_R_NOTIMPLEMENTED);
  6148. newheader = new_rdataset(rbtdb, rbtdb->common.mctx);
  6149. if (newheader == NULL)
  6150. return (ISC_R_NOMEMORY);
  6151. set_ttl(rbtdb, newheader, 0);
  6152. newheader->type = RBTDB_RDATATYPE_VALUE(type, covers);
  6153. newheader->attributes = RDATASET_ATTR_NONEXISTENT;
  6154. newheader->trust = 0;
  6155. newheader->noqname = NULL;
  6156. newheader->closest = NULL;
  6157. newheader->additional_auth = NULL;
  6158. newheader->additional_glue = NULL;
  6159. if (rbtversion != NULL)
  6160. newheader->serial = rbtversion->serial;
  6161. else
  6162. newheader->serial = 0;
  6163. newheader->count = 0;
  6164. newheader->last_used = 0;
  6165. newheader->node = rbtnode;
  6166. NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
  6167. isc_rwlocktype_write);
  6168. result = add(rbtdb, rbtnode, rbtversion, newheader, DNS_DBADD_FORCE,
  6169. ISC_FALSE, NULL, 0);
  6170. NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
  6171. isc_rwlocktype_write);
  6172. /*
  6173. * Update the zone's secure status. If version is non-NULL
  6174. * this is deferred until closeversion() is called.
  6175. */
  6176. if (result == ISC_R_SUCCESS && version == NULL && !IS_CACHE(rbtdb))
  6177. iszonesecure(db, rbtdb->current_version, rbtdb->origin_node);
  6178. return (result);
  6179. }
  6180. /*
  6181. * load a non-NSEC3 node in the main tree and optionally to the auxiliary NSEC
  6182. */
  6183. static isc_result_t
  6184. loadnode(dns_rbtdb_t *rbtdb, dns_name_t *name, dns_rbtnode_t **nodep,
  6185. isc_boolean_t hasnsec)
  6186. {
  6187. isc_result_t noderesult, nsecresult;
  6188. dns_rbtnode_t *nsecnode;
  6189. noderesult = dns_rbt_addnode(rbtdb->tree, name, nodep);
  6190. #ifdef BIND9
  6191. if (noderesult == ISC_R_SUCCESS)
  6192. dns_rpz_cidr_addip(rbtdb->rpz_cidr, name);
  6193. #endif
  6194. if (!hasnsec)
  6195. return (noderesult);
  6196. if (noderesult == ISC_R_EXISTS) {
  6197. /*
  6198. * Add a node to the auxiliary NSEC tree for an old node
  6199. * just now getting an NSEC record.
  6200. */
  6201. if ((*nodep)->nsec == DNS_RBT_NSEC_HAS_NSEC)
  6202. return (noderesult);
  6203. } else if (noderesult != ISC_R_SUCCESS) {
  6204. return (noderesult);
  6205. }
  6206. /*
  6207. * Build the auxiliary tree for NSECs as we go.
  6208. * This tree speeds searches for closest NSECs that would otherwise
  6209. * need to examine many irrelevant nodes in large TLDs.
  6210. *
  6211. * Add nodes to the auxiliary tree after corresponding nodes have
  6212. * been added to the main tree.
  6213. */
  6214. nsecnode = NULL;
  6215. nsecresult = dns_rbt_addnode(rbtdb->nsec, name, &nsecnode);
  6216. if (nsecresult == ISC_R_SUCCESS) {
  6217. nsecnode->nsec = DNS_RBT_NSEC_NSEC;
  6218. (*nodep)->nsec = DNS_RBT_NSEC_HAS_NSEC;
  6219. return (noderesult);
  6220. }
  6221. if (nsecresult == ISC_R_EXISTS) {
  6222. #if 1 /* 0 */
  6223. isc_log_write(dns_lctx,
  6224. DNS_LOGCATEGORY_DATABASE,
  6225. DNS_LOGMODULE_CACHE,
  6226. ISC_LOG_WARNING,
  6227. "addnode: NSEC node already exists");
  6228. #endif
  6229. (*nodep)->nsec = DNS_RBT_NSEC_HAS_NSEC;
  6230. return (noderesult);
  6231. }
  6232. nsecresult = dns_rbt_deletenode(rbtdb->tree, *nodep, ISC_FALSE);
  6233. if (nsecresult != ISC_R_SUCCESS)
  6234. isc_log_write(dns_lctx,
  6235. DNS_LOGCATEGORY_DATABASE,
  6236. DNS_LOGMODULE_CACHE,
  6237. ISC_LOG_WARNING,
  6238. "loading_addrdataset: "
  6239. "dns_rbt_deletenode: %s after "
  6240. "dns_rbt_addnode(NSEC): %s",
  6241. isc_result_totext(nsecresult),
  6242. isc_result_totext(noderesult));
  6243. return (noderesult);
  6244. }
  6245. static isc_result_t
  6246. loading_addrdataset(void *arg, dns_name_t *name, dns_rdataset_t *rdataset) {
  6247. rbtdb_load_t *loadctx = arg;
  6248. dns_rbtdb_t *rbtdb = loadctx->rbtdb;
  6249. dns_rbtnode_t *node;
  6250. isc_result_t result;
  6251. isc_region_t region;
  6252. rdatasetheader_t *newheader;
  6253. /*
  6254. * This routine does no node locking. See comments in
  6255. * 'load' below for more information on loading and
  6256. * locking.
  6257. */
  6258. /*
  6259. * SOA records are only allowed at top of zone.
  6260. */
  6261. if (rdataset->type == dns_rdatatype_soa &&
  6262. !IS_CACHE(rbtdb) && !dns_name_equal(name, &rbtdb->common.origin))
  6263. return (DNS_R_NOTZONETOP);
  6264. if (rdataset->type != dns_rdatatype_nsec3 &&
  6265. rdataset->covers != dns_rdatatype_nsec3)
  6266. add_empty_wildcards(rbtdb, name);
  6267. if (dns_name_iswildcard(name)) {
  6268. /*
  6269. * NS record owners cannot legally be wild cards.
  6270. */
  6271. if (rdataset->type == dns_rdatatype_ns)
  6272. return (DNS_R_INVALIDNS);
  6273. /*
  6274. * NSEC3 record owners cannot legally be wild cards.
  6275. */
  6276. if (rdataset->type == dns_rdatatype_nsec3)
  6277. return (DNS_R_INVALIDNSEC3);
  6278. result = add_wildcard_magic(rbtdb, name);
  6279. if (result != ISC_R_SUCCESS)
  6280. return (result);
  6281. }
  6282. node = NULL;
  6283. if (rdataset->type == dns_rdatatype_nsec3 ||
  6284. rdataset->covers == dns_rdatatype_nsec3) {
  6285. result = dns_rbt_addnode(rbtdb->nsec3, name, &node);
  6286. if (result == ISC_R_SUCCESS)
  6287. node->nsec = DNS_RBT_NSEC_NSEC3;
  6288. } else if (rdataset->type == dns_rdatatype_nsec) {
  6289. result = loadnode(rbtdb, name, &node, ISC_TRUE);
  6290. } else {
  6291. result = loadnode(rbtdb, name, &node, ISC_FALSE);
  6292. }
  6293. if (result != ISC_R_SUCCESS && result != ISC_R_EXISTS)
  6294. return (result);
  6295. if (result == ISC_R_SUCCESS) {
  6296. dns_name_t foundname;
  6297. dns_name_init(&foundname, NULL);
  6298. dns_rbt_namefromnode(node, &foundname);
  6299. #ifdef DNS_RBT_USEHASH
  6300. node->locknum = node->hashval % rbtdb->node_lock_count;
  6301. #else
  6302. node->locknum = dns_name_hash(&foundname, ISC_TRUE) %
  6303. rbtdb->node_lock_count;
  6304. #endif
  6305. }
  6306. result = dns_rdataslab_fromrdataset(rdataset, rbtdb->common.mctx,
  6307. &region,
  6308. sizeof(rdatasetheader_t));
  6309. if (result != ISC_R_SUCCESS)
  6310. return (result);
  6311. newheader = (rdatasetheader_t *)region.base;
  6312. init_rdataset(rbtdb, newheader);
  6313. set_ttl(rbtdb, newheader,
  6314. rdataset->ttl + loadctx->now); /* XXX overflow check */
  6315. newheader->type = RBTDB_RDATATYPE_VALUE(rdataset->type,
  6316. rdataset->covers);
  6317. newheader->attributes = 0;
  6318. newheader->trust = rdataset->trust;
  6319. newheader->serial = 1;
  6320. newheader->noqname = NULL;
  6321. newheader->closest = NULL;
  6322. newheader->count = init_count++;
  6323. newheader->additional_auth = NULL;
  6324. newheader->additional_glue = NULL;
  6325. newheader->last_used = 0;
  6326. newheader->node = node;
  6327. if ((rdataset->attributes & DNS_RDATASETATTR_RESIGN) != 0) {
  6328. newheader->attributes |= RDATASET_ATTR_RESIGN;
  6329. newheader->resign = rdataset->resign;
  6330. } else
  6331. newheader->resign = 0;
  6332. result = add(rbtdb, node, rbtdb->current_version, newheader,
  6333. DNS_DBADD_MERGE, ISC_TRUE, NULL, 0);
  6334. if (result == ISC_R_SUCCESS &&
  6335. delegating_type(rbtdb, node, rdataset->type))
  6336. node->find_callback = 1;
  6337. else if (result == DNS_R_UNCHANGED)
  6338. result = ISC_R_SUCCESS;
  6339. return (result);
  6340. }
  6341. static isc_result_t
  6342. beginload(dns_db_t *db, dns_addrdatasetfunc_t *addp, dns_dbload_t **dbloadp) {
  6343. rbtdb_load_t *loadctx;
  6344. dns_rbtdb_t *rbtdb;
  6345. rbtdb = (dns_rbtdb_t *)db;
  6346. REQUIRE(VALID_RBTDB(rbtdb));
  6347. loadctx = isc_mem_get(rbtdb->common.mctx, sizeof(*loadctx));
  6348. if (loadctx == NULL)
  6349. return (ISC_R_NOMEMORY);
  6350. loadctx->rbtdb = rbtdb;
  6351. if (IS_CACHE(rbtdb))
  6352. isc_stdtime_get(&loadctx->now);
  6353. else
  6354. loadctx->now = 0;
  6355. RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_write);
  6356. REQUIRE((rbtdb->attributes & (RBTDB_ATTR_LOADED|RBTDB_ATTR_LOADING))
  6357. == 0);
  6358. rbtdb->attributes |= RBTDB_ATTR_LOADING;
  6359. RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_write);
  6360. *addp = loading_addrdataset;
  6361. *dbloadp = loadctx;
  6362. return (ISC_R_SUCCESS);
  6363. }
  6364. static isc_result_t
  6365. endload(dns_db_t *db, dns_dbload_t **dbloadp) {
  6366. rbtdb_load_t *loadctx;
  6367. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
  6368. REQUIRE(VALID_RBTDB(rbtdb));
  6369. REQUIRE(dbloadp != NULL);
  6370. loadctx = *dbloadp;
  6371. REQUIRE(loadctx->rbtdb == rbtdb);
  6372. RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_write);
  6373. REQUIRE((rbtdb->attributes & RBTDB_ATTR_LOADING) != 0);
  6374. REQUIRE((rbtdb->attributes & RBTDB_ATTR_LOADED) == 0);
  6375. rbtdb->attributes &= ~RBTDB_ATTR_LOADING;
  6376. rbtdb->attributes |= RBTDB_ATTR_LOADED;
  6377. RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_write);
  6378. /*
  6379. * If there's a KEY rdataset at the zone origin containing a
  6380. * zone key, we consider the zone secure.
  6381. */
  6382. if (! IS_CACHE(rbtdb))
  6383. iszonesecure(db, rbtdb->current_version, rbtdb->origin_node);
  6384. *dbloadp = NULL;
  6385. isc_mem_put(rbtdb->common.mctx, loadctx, sizeof(*loadctx));
  6386. return (ISC_R_SUCCESS);
  6387. }
  6388. static isc_result_t
  6389. dump(dns_db_t *db, dns_dbversion_t *version, const char *filename,
  6390. dns_masterformat_t masterformat) {
  6391. dns_rbtdb_t *rbtdb;
  6392. rbtdb_version_t *rbtversion = version;
  6393. rbtdb = (dns_rbtdb_t *)db;
  6394. REQUIRE(VALID_RBTDB(rbtdb));
  6395. INSIST(rbtversion == NULL || rbtversion->rbtdb == rbtdb);
  6396. #ifdef BIND9
  6397. return (dns_master_dump2(rbtdb->common.mctx, db, version,
  6398. &dns_master_style_default,
  6399. filename, masterformat));
  6400. #else
  6401. UNUSED(version);
  6402. UNUSED(filename);
  6403. UNUSED(masterformat);
  6404. return (ISC_R_NOTIMPLEMENTED);
  6405. #endif /* BIND9 */
  6406. }
  6407. static void
  6408. delete_callback(void *data, void *arg) {
  6409. dns_rbtdb_t *rbtdb = arg;
  6410. rdatasetheader_t *current, *next;
  6411. unsigned int locknum;
  6412. current = data;
  6413. locknum = current->node->locknum;
  6414. NODE_LOCK(&rbtdb->node_locks[locknum].lock, isc_rwlocktype_write);
  6415. while (current != NULL) {
  6416. next = current->next;
  6417. free_rdataset(rbtdb, rbtdb->common.mctx, current);
  6418. current = next;
  6419. }
  6420. NODE_UNLOCK(&rbtdb->node_locks[locknum].lock, isc_rwlocktype_write);
  6421. }
  6422. static isc_boolean_t
  6423. issecure(dns_db_t *db) {
  6424. dns_rbtdb_t *rbtdb;
  6425. isc_boolean_t secure;
  6426. rbtdb = (dns_rbtdb_t *)db;
  6427. REQUIRE(VALID_RBTDB(rbtdb));
  6428. RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
  6429. secure = ISC_TF(rbtdb->current_version->secure == dns_db_secure);
  6430. RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
  6431. return (secure);
  6432. }
  6433. static isc_boolean_t
  6434. isdnssec(dns_db_t *db) {
  6435. dns_rbtdb_t *rbtdb;
  6436. isc_boolean_t dnssec;
  6437. rbtdb = (dns_rbtdb_t *)db;
  6438. REQUIRE(VALID_RBTDB(rbtdb));
  6439. RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
  6440. dnssec = ISC_TF(rbtdb->current_version->secure != dns_db_insecure);
  6441. RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
  6442. return (dnssec);
  6443. }
  6444. static unsigned int
  6445. nodecount(dns_db_t *db) {
  6446. dns_rbtdb_t *rbtdb;
  6447. unsigned int count;
  6448. rbtdb = (dns_rbtdb_t *)db;
  6449. REQUIRE(VALID_RBTDB(rbtdb));
  6450. RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
  6451. count = dns_rbt_nodecount(rbtdb->tree);
  6452. RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
  6453. return (count);
  6454. }
  6455. static void
  6456. settask(dns_db_t *db, isc_task_t *task) {
  6457. dns_rbtdb_t *rbtdb;
  6458. rbtdb = (dns_rbtdb_t *)db;
  6459. REQUIRE(VALID_RBTDB(rbtdb));
  6460. RBTDB_LOCK(&rbtdb->lock, isc_rwlocktype_write);
  6461. if (rbtdb->task != NULL)
  6462. isc_task_detach(&rbtdb->task);
  6463. if (task != NULL)
  6464. isc_task_attach(task, &rbtdb->task);
  6465. RBTDB_UNLOCK(&rbtdb->lock, isc_rwlocktype_write);
  6466. }
  6467. static isc_boolean_t
  6468. ispersistent(dns_db_t *db) {
  6469. UNUSED(db);
  6470. return (ISC_FALSE);
  6471. }
  6472. static isc_result_t
  6473. getoriginnode(dns_db_t *db, dns_dbnode_t **nodep) {
  6474. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
  6475. dns_rbtnode_t *onode;
  6476. isc_result_t result = ISC_R_SUCCESS;
  6477. REQUIRE(VALID_RBTDB(rbtdb));
  6478. REQUIRE(nodep != NULL && *nodep == NULL);
  6479. /* Note that the access to origin_node doesn't require a DB lock */
  6480. onode = (dns_rbtnode_t *)rbtdb->origin_node;
  6481. if (onode != NULL) {
  6482. NODE_STRONGLOCK(&rbtdb->node_locks[onode->locknum].lock);
  6483. new_reference(rbtdb, onode);
  6484. NODE_STRONGUNLOCK(&rbtdb->node_locks[onode->locknum].lock);
  6485. *nodep = rbtdb->origin_node;
  6486. } else {
  6487. INSIST(IS_CACHE(rbtdb));
  6488. result = ISC_R_NOTFOUND;
  6489. }
  6490. return (result);
  6491. }
  6492. static isc_result_t
  6493. getnsec3parameters(dns_db_t *db, dns_dbversion_t *version, dns_hash_t *hash,
  6494. isc_uint8_t *flags, isc_uint16_t *iterations,
  6495. unsigned char *salt, size_t *salt_length)
  6496. {
  6497. dns_rbtdb_t *rbtdb;
  6498. isc_result_t result = ISC_R_NOTFOUND;
  6499. rbtdb_version_t *rbtversion = version;
  6500. rbtdb = (dns_rbtdb_t *)db;
  6501. REQUIRE(VALID_RBTDB(rbtdb));
  6502. INSIST(rbtversion == NULL || rbtversion->rbtdb == rbtdb);
  6503. RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
  6504. if (rbtversion == NULL)
  6505. rbtversion = rbtdb->current_version;
  6506. if (rbtversion->havensec3) {
  6507. if (hash != NULL)
  6508. *hash = rbtversion->hash;
  6509. if (salt != NULL && salt_length != NULL) {
  6510. REQUIRE(*salt_length >= rbtversion->salt_length);
  6511. memcpy(salt, rbtversion->salt, rbtversion->salt_length);
  6512. }
  6513. if (salt_length != NULL)
  6514. *salt_length = rbtversion->salt_length;
  6515. if (iterations != NULL)
  6516. *iterations = rbtversion->iterations;
  6517. if (flags != NULL)
  6518. *flags = rbtversion->flags;
  6519. result = ISC_R_SUCCESS;
  6520. }
  6521. RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
  6522. return (result);
  6523. }
  6524. static isc_result_t
  6525. setsigningtime(dns_db_t *db, dns_rdataset_t *rdataset, isc_stdtime_t resign) {
  6526. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
  6527. isc_stdtime_t oldresign;
  6528. isc_result_t result = ISC_R_SUCCESS;
  6529. rdatasetheader_t *header;
  6530. REQUIRE(VALID_RBTDB(rbtdb));
  6531. REQUIRE(!IS_CACHE(rbtdb));
  6532. REQUIRE(rdataset != NULL);
  6533. header = rdataset->private3;
  6534. header--;
  6535. NODE_LOCK(&rbtdb->node_locks[header->node->locknum].lock,
  6536. isc_rwlocktype_write);
  6537. oldresign = header->resign;
  6538. header->resign = resign;
  6539. if (header->heap_index != 0) {
  6540. INSIST(RESIGN(header));
  6541. if (resign == 0) {
  6542. isc_heap_delete(rbtdb->heaps[header->node->locknum],
  6543. header->heap_index);
  6544. header->heap_index = 0;
  6545. } else if (resign < oldresign)
  6546. isc_heap_increased(rbtdb->heaps[header->node->locknum],
  6547. header->heap_index);
  6548. else if (resign > oldresign)
  6549. isc_heap_decreased(rbtdb->heaps[header->node->locknum],
  6550. header->heap_index);
  6551. } else if (resign && header->heap_index == 0) {
  6552. header->attributes |= RDATASET_ATTR_RESIGN;
  6553. result = resign_insert(rbtdb, header->node->locknum, header);
  6554. }
  6555. NODE_UNLOCK(&rbtdb->node_locks[header->node->locknum].lock,
  6556. isc_rwlocktype_write);
  6557. return (result);
  6558. }
  6559. static isc_result_t
  6560. getsigningtime(dns_db_t *db, dns_rdataset_t *rdataset,
  6561. dns_name_t *foundname)
  6562. {
  6563. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
  6564. rdatasetheader_t *header = NULL, *this;
  6565. unsigned int i;
  6566. isc_result_t result = ISC_R_NOTFOUND;
  6567. unsigned int locknum;
  6568. REQUIRE(VALID_RBTDB(rbtdb));
  6569. RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
  6570. for (i = 0; i < rbtdb->node_lock_count; i++) {
  6571. NODE_LOCK(&rbtdb->node_locks[i].lock, isc_rwlocktype_read);
  6572. this = isc_heap_element(rbtdb->heaps[i], 1);
  6573. if (this == NULL) {
  6574. NODE_UNLOCK(&rbtdb->node_locks[i].lock,
  6575. isc_rwlocktype_read);
  6576. continue;
  6577. }
  6578. if (header == NULL)
  6579. header = this;
  6580. else if (isc_serial_lt(this->resign, header->resign)) {
  6581. locknum = header->node->locknum;
  6582. NODE_UNLOCK(&rbtdb->node_locks[locknum].lock,
  6583. isc_rwlocktype_read);
  6584. header = this;
  6585. } else
  6586. NODE_UNLOCK(&rbtdb->node_locks[i].lock,
  6587. isc_rwlocktype_read);
  6588. }
  6589. if (header == NULL)
  6590. goto unlock;
  6591. bind_rdataset(rbtdb, header->node, header, 0, rdataset);
  6592. if (foundname != NULL)
  6593. dns_rbt_fullnamefromnode(header->node, foundname);
  6594. NODE_UNLOCK(&rbtdb->node_locks[header->node->locknum].lock,
  6595. isc_rwlocktype_read);
  6596. result = ISC_R_SUCCESS;
  6597. unlock:
  6598. RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
  6599. return (result);
  6600. }
  6601. static void
  6602. resigned(dns_db_t *db, dns_rdataset_t *rdataset, dns_dbversion_t *version)
  6603. {
  6604. rbtdb_version_t *rbtversion = (rbtdb_version_t *)version;
  6605. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
  6606. dns_rbtnode_t *node;
  6607. rdatasetheader_t *header;
  6608. REQUIRE(VALID_RBTDB(rbtdb));
  6609. REQUIRE(rdataset != NULL);
  6610. REQUIRE(rdataset->methods == &rdataset_methods);
  6611. REQUIRE(rbtdb->future_version == rbtversion);
  6612. REQUIRE(rbtversion != NULL);
  6613. REQUIRE(rbtversion->writer);
  6614. REQUIRE(rbtversion->rbtdb == rbtdb);
  6615. node = rdataset->private2;
  6616. INSIST(node != NULL);
  6617. header = rdataset->private3;
  6618. INSIST(header != NULL);
  6619. header--;
  6620. RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
  6621. NODE_LOCK(&rbtdb->node_locks[node->locknum].lock,
  6622. isc_rwlocktype_write);
  6623. /*
  6624. * Delete from heap and save to re-signed list so that it can
  6625. * be restored if we backout of this change.
  6626. */
  6627. new_reference(rbtdb, node);
  6628. isc_heap_delete(rbtdb->heaps[node->locknum], header->heap_index);
  6629. header->heap_index = 0;
  6630. ISC_LIST_APPEND(rbtversion->resigned_list, header, link);
  6631. NODE_UNLOCK(&rbtdb->node_locks[node->locknum].lock,
  6632. isc_rwlocktype_write);
  6633. RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
  6634. }
  6635. static dns_stats_t *
  6636. getrrsetstats(dns_db_t *db) {
  6637. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
  6638. REQUIRE(VALID_RBTDB(rbtdb));
  6639. REQUIRE(IS_CACHE(rbtdb)); /* current restriction */
  6640. return (rbtdb->rrsetstats);
  6641. }
  6642. static dns_dbmethods_t zone_methods = {
  6643. attach,
  6644. detach,
  6645. beginload,
  6646. endload,
  6647. dump,
  6648. currentversion,
  6649. newversion,
  6650. attachversion,
  6651. closeversion,
  6652. findnode,
  6653. zone_find,
  6654. zone_findzonecut,
  6655. attachnode,
  6656. detachnode,
  6657. expirenode,
  6658. printnode,
  6659. createiterator,
  6660. zone_findrdataset,
  6661. allrdatasets,
  6662. addrdataset,
  6663. subtractrdataset,
  6664. deleterdataset,
  6665. issecure,
  6666. nodecount,
  6667. ispersistent,
  6668. overmem,
  6669. settask,
  6670. getoriginnode,
  6671. NULL,
  6672. getnsec3parameters,
  6673. findnsec3node,
  6674. setsigningtime,
  6675. getsigningtime,
  6676. resigned,
  6677. isdnssec,
  6678. NULL,
  6679. #ifdef BIND9
  6680. get_rpz_enabled,
  6681. rpz_findips
  6682. #else
  6683. NULL,
  6684. NULL
  6685. #endif
  6686. };
  6687. static dns_dbmethods_t cache_methods = {
  6688. attach,
  6689. detach,
  6690. beginload,
  6691. endload,
  6692. dump,
  6693. currentversion,
  6694. newversion,
  6695. attachversion,
  6696. closeversion,
  6697. findnode,
  6698. cache_find,
  6699. cache_findzonecut,
  6700. attachnode,
  6701. detachnode,
  6702. expirenode,
  6703. printnode,
  6704. createiterator,
  6705. cache_findrdataset,
  6706. allrdatasets,
  6707. addrdataset,
  6708. subtractrdataset,
  6709. deleterdataset,
  6710. issecure,
  6711. nodecount,
  6712. ispersistent,
  6713. overmem,
  6714. settask,
  6715. getoriginnode,
  6716. NULL,
  6717. NULL,
  6718. NULL,
  6719. NULL,
  6720. NULL,
  6721. NULL,
  6722. isdnssec,
  6723. getrrsetstats,
  6724. NULL,
  6725. NULL
  6726. };
  6727. isc_result_t
  6728. #ifdef DNS_RBTDB_VERSION64
  6729. dns_rbtdb64_create
  6730. #else
  6731. dns_rbtdb_create
  6732. #endif
  6733. (isc_mem_t *mctx, dns_name_t *origin, dns_dbtype_t type,
  6734. dns_rdataclass_t rdclass, unsigned int argc, char *argv[],
  6735. void *driverarg, dns_db_t **dbp)
  6736. {
  6737. dns_rbtdb_t *rbtdb;
  6738. isc_result_t result;
  6739. int i;
  6740. dns_name_t name;
  6741. isc_boolean_t (*sooner)(void *, void *);
  6742. isc_mem_t *hmctx = mctx;
  6743. /* Keep the compiler happy. */
  6744. UNUSED(driverarg);
  6745. rbtdb = isc_mem_get(mctx, sizeof(*rbtdb));
  6746. if (rbtdb == NULL)
  6747. return (ISC_R_NOMEMORY);
  6748. /*
  6749. * If argv[0] exists, it points to a memory context to use for heap
  6750. */
  6751. if (argc != 0)
  6752. hmctx = (isc_mem_t *) argv[0];
  6753. memset(rbtdb, '\0', sizeof(*rbtdb));
  6754. dns_name_init(&rbtdb->common.origin, NULL);
  6755. rbtdb->common.attributes = 0;
  6756. if (type == dns_dbtype_cache) {
  6757. rbtdb->common.methods = &cache_methods;
  6758. rbtdb->common.attributes |= DNS_DBATTR_CACHE;
  6759. } else if (type == dns_dbtype_stub) {
  6760. rbtdb->common.methods = &zone_methods;
  6761. rbtdb->common.attributes |= DNS_DBATTR_STUB;
  6762. } else
  6763. rbtdb->common.methods = &zone_methods;
  6764. rbtdb->common.rdclass = rdclass;
  6765. rbtdb->common.mctx = NULL;
  6766. result = RBTDB_INITLOCK(&rbtdb->lock);
  6767. if (result != ISC_R_SUCCESS)
  6768. goto cleanup_rbtdb;
  6769. result = isc_rwlock_init(&rbtdb->tree_lock, 0, 0);
  6770. if (result != ISC_R_SUCCESS)
  6771. goto cleanup_lock;
  6772. /*
  6773. * Initialize node_lock_count in a generic way to support future
  6774. * extension which allows the user to specify this value on creation.
  6775. * Note that when specified for a cache DB it must be larger than 1
  6776. * as commented with the definition of DEFAULT_CACHE_NODE_LOCK_COUNT.
  6777. */
  6778. if (rbtdb->node_lock_count == 0) {
  6779. if (IS_CACHE(rbtdb))
  6780. rbtdb->node_lock_count = DEFAULT_CACHE_NODE_LOCK_COUNT;
  6781. else
  6782. rbtdb->node_lock_count = DEFAULT_NODE_LOCK_COUNT;
  6783. } else if (rbtdb->node_lock_count < 2 && IS_CACHE(rbtdb)) {
  6784. result = ISC_R_RANGE;
  6785. goto cleanup_tree_lock;
  6786. }
  6787. INSIST(rbtdb->node_lock_count < (1 << DNS_RBT_LOCKLENGTH));
  6788. rbtdb->node_locks = isc_mem_get(mctx, rbtdb->node_lock_count *
  6789. sizeof(rbtdb_nodelock_t));
  6790. if (rbtdb->node_locks == NULL) {
  6791. result = ISC_R_NOMEMORY;
  6792. goto cleanup_tree_lock;
  6793. }
  6794. rbtdb->rrsetstats = NULL;
  6795. if (IS_CACHE(rbtdb)) {
  6796. result = dns_rdatasetstats_create(mctx, &rbtdb->rrsetstats);
  6797. if (result != ISC_R_SUCCESS)
  6798. goto cleanup_node_locks;
  6799. rbtdb->rdatasets = isc_mem_get(mctx, rbtdb->node_lock_count *
  6800. sizeof(rdatasetheaderlist_t));
  6801. if (rbtdb->rdatasets == NULL) {
  6802. result = ISC_R_NOMEMORY;
  6803. goto cleanup_rrsetstats;
  6804. }
  6805. for (i = 0; i < (int)rbtdb->node_lock_count; i++)
  6806. ISC_LIST_INIT(rbtdb->rdatasets[i]);
  6807. } else
  6808. rbtdb->rdatasets = NULL;
  6809. /*
  6810. * Create the heaps.
  6811. */
  6812. rbtdb->heaps = isc_mem_get(hmctx, rbtdb->node_lock_count *
  6813. sizeof(isc_heap_t *));
  6814. if (rbtdb->heaps == NULL) {
  6815. result = ISC_R_NOMEMORY;
  6816. goto cleanup_rdatasets;
  6817. }
  6818. for (i = 0; i < (int)rbtdb->node_lock_count; i++)
  6819. rbtdb->heaps[i] = NULL;
  6820. sooner = IS_CACHE(rbtdb) ? ttl_sooner : resign_sooner;
  6821. for (i = 0; i < (int)rbtdb->node_lock_count; i++) {
  6822. result = isc_heap_create(hmctx, sooner, set_index, 0,
  6823. &rbtdb->heaps[i]);
  6824. if (result != ISC_R_SUCCESS)
  6825. goto cleanup_heaps;
  6826. }
  6827. /*
  6828. * Create deadnode lists.
  6829. */
  6830. rbtdb->deadnodes = isc_mem_get(mctx, rbtdb->node_lock_count *
  6831. sizeof(rbtnodelist_t));
  6832. if (rbtdb->deadnodes == NULL) {
  6833. result = ISC_R_NOMEMORY;
  6834. goto cleanup_heaps;
  6835. }
  6836. for (i = 0; i < (int)rbtdb->node_lock_count; i++)
  6837. ISC_LIST_INIT(rbtdb->deadnodes[i]);
  6838. rbtdb->active = rbtdb->node_lock_count;
  6839. for (i = 0; i < (int)(rbtdb->node_lock_count); i++) {
  6840. result = NODE_INITLOCK(&rbtdb->node_locks[i].lock);
  6841. if (result == ISC_R_SUCCESS) {
  6842. result = isc_refcount_init(&rbtdb->node_locks[i].references, 0);
  6843. if (result != ISC_R_SUCCESS)
  6844. NODE_DESTROYLOCK(&rbtdb->node_locks[i].lock);
  6845. }
  6846. if (result != ISC_R_SUCCESS) {
  6847. while (i-- > 0) {
  6848. NODE_DESTROYLOCK(&rbtdb->node_locks[i].lock);
  6849. isc_refcount_decrement(&rbtdb->node_locks[i].references, NULL);
  6850. isc_refcount_destroy(&rbtdb->node_locks[i].references);
  6851. }
  6852. goto cleanup_deadnodes;
  6853. }
  6854. rbtdb->node_locks[i].exiting = ISC_FALSE;
  6855. }
  6856. /*
  6857. * Attach to the mctx. The database will persist so long as there
  6858. * are references to it, and attaching to the mctx ensures that our
  6859. * mctx won't disappear out from under us.
  6860. */
  6861. isc_mem_attach(mctx, &rbtdb->common.mctx);
  6862. isc_mem_attach(hmctx, &rbtdb->hmctx);
  6863. /*
  6864. * Must be initialized before free_rbtdb() is called.
  6865. */
  6866. isc_ondestroy_init(&rbtdb->common.ondest);
  6867. /*
  6868. * Make a copy of the origin name.
  6869. */
  6870. result = dns_name_dupwithoffsets(origin, mctx, &rbtdb->common.origin);
  6871. if (result != ISC_R_SUCCESS) {
  6872. free_rbtdb(rbtdb, ISC_FALSE, NULL);
  6873. return (result);
  6874. }
  6875. /*
  6876. * Make the Red-Black Trees.
  6877. */
  6878. result = dns_rbt_create(mctx, delete_callback, rbtdb, &rbtdb->tree);
  6879. if (result != ISC_R_SUCCESS) {
  6880. free_rbtdb(rbtdb, ISC_FALSE, NULL);
  6881. return (result);
  6882. }
  6883. result = dns_rbt_create(mctx, delete_callback, rbtdb, &rbtdb->nsec);
  6884. if (result != ISC_R_SUCCESS) {
  6885. free_rbtdb(rbtdb, ISC_FALSE, NULL);
  6886. return (result);
  6887. }
  6888. result = dns_rbt_create(mctx, delete_callback, rbtdb, &rbtdb->nsec3);
  6889. if (result != ISC_R_SUCCESS) {
  6890. free_rbtdb(rbtdb, ISC_FALSE, NULL);
  6891. return (result);
  6892. }
  6893. #ifdef BIND9
  6894. /*
  6895. * Get ready for response policy IP address searching if at least one
  6896. * zone has been configured as a response policy zone and this
  6897. * is not a cache zone.
  6898. * It would be better to know that this database is for a policy
  6899. * zone named for a view, but that would require knowledge from
  6900. * above such as an argv[] set from data in the zone.
  6901. */
  6902. if (type == dns_dbtype_zone && !dns_name_equal(origin, dns_rootname)) {
  6903. result = dns_rpz_new_cidr(mctx, origin, &rbtdb->rpz_cidr);
  6904. if (result != ISC_R_SUCCESS) {
  6905. free_rbtdb(rbtdb, ISC_FALSE, NULL);
  6906. return (result);
  6907. }
  6908. }
  6909. #endif
  6910. /*
  6911. * In order to set the node callback bit correctly in zone databases,
  6912. * we need to know if the node has the origin name of the zone.
  6913. * In loading_addrdataset() we could simply compare the new name
  6914. * to the origin name, but this is expensive. Also, we don't know the
  6915. * node name in addrdataset(), so we need another way of knowing the
  6916. * zone's top.
  6917. *
  6918. * We now explicitly create a node for the zone's origin, and then
  6919. * we simply remember the node's address. This is safe, because
  6920. * the top-of-zone node can never be deleted, nor can its address
  6921. * change.
  6922. */
  6923. if (!IS_CACHE(rbtdb)) {
  6924. dns_rbtnode_t *nsec3node;
  6925. rbtdb->origin_node = NULL;
  6926. result = dns_rbt_addnode(rbtdb->tree, &rbtdb->common.origin,
  6927. &rbtdb->origin_node);
  6928. if (result != ISC_R_SUCCESS) {
  6929. INSIST(result != ISC_R_EXISTS);
  6930. free_rbtdb(rbtdb, ISC_FALSE, NULL);
  6931. return (result);
  6932. }
  6933. rbtdb->origin_node->nsec = DNS_RBT_NSEC_NORMAL;
  6934. /*
  6935. * We need to give the origin node the right locknum.
  6936. */
  6937. dns_name_init(&name, NULL);
  6938. dns_rbt_namefromnode(rbtdb->origin_node, &name);
  6939. #ifdef DNS_RBT_USEHASH
  6940. rbtdb->origin_node->locknum =
  6941. rbtdb->origin_node->hashval %
  6942. rbtdb->node_lock_count;
  6943. #else
  6944. rbtdb->origin_node->locknum =
  6945. dns_name_hash(&name, ISC_TRUE) %
  6946. rbtdb->node_lock_count;
  6947. #endif
  6948. /*
  6949. * Add an apex node to the NSEC3 tree so that NSEC3 searches
  6950. * return partial matches when there is only a single NSEC3
  6951. * record in the tree.
  6952. */
  6953. nsec3node = NULL;
  6954. result = dns_rbt_addnode(rbtdb->nsec3, &rbtdb->common.origin,
  6955. &nsec3node);
  6956. if (result != ISC_R_SUCCESS) {
  6957. INSIST(result != ISC_R_EXISTS);
  6958. free_rbtdb(rbtdb, ISC_FALSE, NULL);
  6959. return (result);
  6960. }
  6961. nsec3node->nsec = DNS_RBT_NSEC_NSEC3;
  6962. /*
  6963. * We need to give the nsec3 origin node the right locknum.
  6964. */
  6965. dns_name_init(&name, NULL);
  6966. dns_rbt_namefromnode(nsec3node, &name);
  6967. #ifdef DNS_RBT_USEHASH
  6968. nsec3node->locknum = nsec3node->hashval %
  6969. rbtdb->node_lock_count;
  6970. #else
  6971. nsec3node->locknum = dns_name_hash(&name, ISC_TRUE) %
  6972. rbtdb->node_lock_count;
  6973. #endif
  6974. }
  6975. /*
  6976. * Misc. Initialization.
  6977. */
  6978. result = isc_refcount_init(&rbtdb->references, 1);
  6979. if (result != ISC_R_SUCCESS) {
  6980. free_rbtdb(rbtdb, ISC_FALSE, NULL);
  6981. return (result);
  6982. }
  6983. rbtdb->attributes = 0;
  6984. rbtdb->task = NULL;
  6985. /*
  6986. * Version Initialization.
  6987. */
  6988. rbtdb->current_serial = 1;
  6989. rbtdb->least_serial = 1;
  6990. rbtdb->next_serial = 2;
  6991. rbtdb->current_version = allocate_version(mctx, 1, 1, ISC_FALSE);
  6992. if (rbtdb->current_version == NULL) {
  6993. isc_refcount_decrement(&rbtdb->references, NULL);
  6994. isc_refcount_destroy(&rbtdb->references);
  6995. free_rbtdb(rbtdb, ISC_FALSE, NULL);
  6996. return (ISC_R_NOMEMORY);
  6997. }
  6998. rbtdb->current_version->rbtdb = rbtdb;
  6999. rbtdb->current_version->secure = dns_db_insecure;
  7000. rbtdb->current_version->havensec3 = ISC_FALSE;
  7001. rbtdb->current_version->flags = 0;
  7002. rbtdb->current_version->iterations = 0;
  7003. rbtdb->current_version->hash = 0;
  7004. rbtdb->current_version->salt_length = 0;
  7005. memset(rbtdb->current_version->salt, 0,
  7006. sizeof(rbtdb->current_version->salt));
  7007. rbtdb->future_version = NULL;
  7008. ISC_LIST_INIT(rbtdb->open_versions);
  7009. /*
  7010. * Keep the current version in the open list so that list operation
  7011. * won't happen in normal lookup operations.
  7012. */
  7013. PREPEND(rbtdb->open_versions, rbtdb->current_version, link);
  7014. rbtdb->common.magic = DNS_DB_MAGIC;
  7015. rbtdb->common.impmagic = RBTDB_MAGIC;
  7016. *dbp = (dns_db_t *)rbtdb;
  7017. return (ISC_R_SUCCESS);
  7018. cleanup_deadnodes:
  7019. isc_mem_put(mctx, rbtdb->deadnodes,
  7020. rbtdb->node_lock_count * sizeof(rbtnodelist_t));
  7021. cleanup_heaps:
  7022. if (rbtdb->heaps != NULL) {
  7023. for (i = 0 ; i < (int)rbtdb->node_lock_count ; i++)
  7024. if (rbtdb->heaps[i] != NULL)
  7025. isc_heap_destroy(&rbtdb->heaps[i]);
  7026. isc_mem_put(hmctx, rbtdb->heaps,
  7027. rbtdb->node_lock_count * sizeof(isc_heap_t *));
  7028. }
  7029. cleanup_rdatasets:
  7030. if (rbtdb->rdatasets != NULL)
  7031. isc_mem_put(mctx, rbtdb->rdatasets, rbtdb->node_lock_count *
  7032. sizeof(rdatasetheaderlist_t));
  7033. cleanup_rrsetstats:
  7034. if (rbtdb->rrsetstats != NULL)
  7035. dns_stats_detach(&rbtdb->rrsetstats);
  7036. cleanup_node_locks:
  7037. isc_mem_put(mctx, rbtdb->node_locks,
  7038. rbtdb->node_lock_count * sizeof(rbtdb_nodelock_t));
  7039. cleanup_tree_lock:
  7040. isc_rwlock_destroy(&rbtdb->tree_lock);
  7041. cleanup_lock:
  7042. RBTDB_DESTROYLOCK(&rbtdb->lock);
  7043. cleanup_rbtdb:
  7044. isc_mem_put(mctx, rbtdb, sizeof(*rbtdb));
  7045. return (result);
  7046. }
  7047. /*
  7048. * Slabbed Rdataset Methods
  7049. */
  7050. static void
  7051. rdataset_disassociate(dns_rdataset_t *rdataset) {
  7052. dns_db_t *db = rdataset->private1;
  7053. dns_dbnode_t *node = rdataset->private2;
  7054. detachnode(db, &node);
  7055. }
  7056. static isc_result_t
  7057. rdataset_first(dns_rdataset_t *rdataset) {
  7058. unsigned char *raw = rdataset->private3; /* RDATASLAB */
  7059. unsigned int count;
  7060. count = raw[0] * 256 + raw[1];
  7061. if (count == 0) {
  7062. rdataset->private5 = NULL;
  7063. return (ISC_R_NOMORE);
  7064. }
  7065. #if DNS_RDATASET_FIXED
  7066. if ((rdataset->attributes & DNS_RDATASETATTR_LOADORDER) == 0)
  7067. raw += 2 + (4 * count);
  7068. else
  7069. #endif
  7070. raw += 2;
  7071. /*
  7072. * The privateuint4 field is the number of rdata beyond the
  7073. * cursor position, so we decrement the total count by one
  7074. * before storing it.
  7075. *
  7076. * If DNS_RDATASETATTR_LOADORDER is not set 'raw' points to the
  7077. * first record. If DNS_RDATASETATTR_LOADORDER is set 'raw' points
  7078. * to the first entry in the offset table.
  7079. */
  7080. count--;
  7081. rdataset->privateuint4 = count;
  7082. rdataset->private5 = raw;
  7083. return (ISC_R_SUCCESS);
  7084. }
  7085. static isc_result_t
  7086. rdataset_next(dns_rdataset_t *rdataset) {
  7087. unsigned int count;
  7088. unsigned int length;
  7089. unsigned char *raw; /* RDATASLAB */
  7090. count = rdataset->privateuint4;
  7091. if (count == 0)
  7092. return (ISC_R_NOMORE);
  7093. count--;
  7094. rdataset->privateuint4 = count;
  7095. /*
  7096. * Skip forward one record (length + 4) or one offset (4).
  7097. */
  7098. raw = rdataset->private5;
  7099. #if DNS_RDATASET_FIXED
  7100. if ((rdataset->attributes & DNS_RDATASETATTR_LOADORDER) == 0) {
  7101. #endif
  7102. length = raw[0] * 256 + raw[1];
  7103. raw += length;
  7104. #if DNS_RDATASET_FIXED
  7105. }
  7106. rdataset->private5 = raw + 4; /* length(2) + order(2) */
  7107. #else
  7108. rdataset->private5 = raw + 2; /* length(2) */
  7109. #endif
  7110. return (ISC_R_SUCCESS);
  7111. }
  7112. static void
  7113. rdataset_current(dns_rdataset_t *rdataset, dns_rdata_t *rdata) {
  7114. unsigned char *raw = rdataset->private5; /* RDATASLAB */
  7115. #if DNS_RDATASET_FIXED
  7116. unsigned int offset;
  7117. #endif
  7118. unsigned int length;
  7119. isc_region_t r;
  7120. unsigned int flags = 0;
  7121. REQUIRE(raw != NULL);
  7122. /*
  7123. * Find the start of the record if not already in private5
  7124. * then skip the length and order fields.
  7125. */
  7126. #if DNS_RDATASET_FIXED
  7127. if ((rdataset->attributes & DNS_RDATASETATTR_LOADORDER) != 0) {
  7128. offset = (raw[0] << 24) + (raw[1] << 16) +
  7129. (raw[2] << 8) + raw[3];
  7130. raw = rdataset->private3;
  7131. raw += offset;
  7132. }
  7133. #endif
  7134. length = raw[0] * 256 + raw[1];
  7135. #if DNS_RDATASET_FIXED
  7136. raw += 4;
  7137. #else
  7138. raw += 2;
  7139. #endif
  7140. if (rdataset->type == dns_rdatatype_rrsig) {
  7141. if (*raw & DNS_RDATASLAB_OFFLINE)
  7142. flags |= DNS_RDATA_OFFLINE;
  7143. length--;
  7144. raw++;
  7145. }
  7146. r.length = length;
  7147. r.base = raw;
  7148. dns_rdata_fromregion(rdata, rdataset->rdclass, rdataset->type, &r);
  7149. rdata->flags |= flags;
  7150. }
  7151. static void
  7152. rdataset_clone(dns_rdataset_t *source, dns_rdataset_t *target) {
  7153. dns_db_t *db = source->private1;
  7154. dns_dbnode_t *node = source->private2;
  7155. dns_dbnode_t *cloned_node = NULL;
  7156. attachnode(db, node, &cloned_node);
  7157. *target = *source;
  7158. /*
  7159. * Reset iterator state.
  7160. */
  7161. target->privateuint4 = 0;
  7162. target->private5 = NULL;
  7163. }
  7164. static unsigned int
  7165. rdataset_count(dns_rdataset_t *rdataset) {
  7166. unsigned char *raw = rdataset->private3; /* RDATASLAB */
  7167. unsigned int count;
  7168. count = raw[0] * 256 + raw[1];
  7169. return (count);
  7170. }
  7171. static isc_result_t
  7172. rdataset_getnoqname(dns_rdataset_t *rdataset, dns_name_t *name,
  7173. dns_rdataset_t *nsec, dns_rdataset_t *nsecsig)
  7174. {
  7175. dns_db_t *db = rdataset->private1;
  7176. dns_dbnode_t *node = rdataset->private2;
  7177. dns_dbnode_t *cloned_node;
  7178. struct noqname *noqname = rdataset->private6;
  7179. cloned_node = NULL;
  7180. attachnode(db, node, &cloned_node);
  7181. nsec->methods = &rdataset_methods;
  7182. nsec->rdclass = db->rdclass;
  7183. nsec->type = noqname->type;
  7184. nsec->covers = 0;
  7185. nsec->ttl = rdataset->ttl;
  7186. nsec->trust = rdataset->trust;
  7187. nsec->private1 = rdataset->private1;
  7188. nsec->private2 = rdataset->private2;
  7189. nsec->private3 = noqname->neg;
  7190. nsec->privateuint4 = 0;
  7191. nsec->private5 = NULL;
  7192. nsec->private6 = NULL;
  7193. nsec->private7 = NULL;
  7194. cloned_node = NULL;
  7195. attachnode(db, node, &cloned_node);
  7196. nsecsig->methods = &rdataset_methods;
  7197. nsecsig->rdclass = db->rdclass;
  7198. nsecsig->type = dns_rdatatype_rrsig;
  7199. nsecsig->covers = noqname->type;
  7200. nsecsig->ttl = rdataset->ttl;
  7201. nsecsig->trust = rdataset->trust;
  7202. nsecsig->private1 = rdataset->private1;
  7203. nsecsig->private2 = rdataset->private2;
  7204. nsecsig->private3 = noqname->negsig;
  7205. nsecsig->privateuint4 = 0;
  7206. nsecsig->private5 = NULL;
  7207. nsec->private6 = NULL;
  7208. nsec->private7 = NULL;
  7209. dns_name_clone(&noqname->name, name);
  7210. return (ISC_R_SUCCESS);
  7211. }
  7212. static isc_result_t
  7213. rdataset_getclosest(dns_rdataset_t *rdataset, dns_name_t *name,
  7214. dns_rdataset_t *nsec, dns_rdataset_t *nsecsig)
  7215. {
  7216. dns_db_t *db = rdataset->private1;
  7217. dns_dbnode_t *node = rdataset->private2;
  7218. dns_dbnode_t *cloned_node;
  7219. struct noqname *closest = rdataset->private7;
  7220. cloned_node = NULL;
  7221. attachnode(db, node, &cloned_node);
  7222. nsec->methods = &rdataset_methods;
  7223. nsec->rdclass = db->rdclass;
  7224. nsec->type = closest->type;
  7225. nsec->covers = 0;
  7226. nsec->ttl = rdataset->ttl;
  7227. nsec->trust = rdataset->trust;
  7228. nsec->private1 = rdataset->private1;
  7229. nsec->private2 = rdataset->private2;
  7230. nsec->private3 = closest->neg;
  7231. nsec->privateuint4 = 0;
  7232. nsec->private5 = NULL;
  7233. nsec->private6 = NULL;
  7234. nsec->private7 = NULL;
  7235. cloned_node = NULL;
  7236. attachnode(db, node, &cloned_node);
  7237. nsecsig->methods = &rdataset_methods;
  7238. nsecsig->rdclass = db->rdclass;
  7239. nsecsig->type = dns_rdatatype_rrsig;
  7240. nsecsig->covers = closest->type;
  7241. nsecsig->ttl = rdataset->ttl;
  7242. nsecsig->trust = rdataset->trust;
  7243. nsecsig->private1 = rdataset->private1;
  7244. nsecsig->private2 = rdataset->private2;
  7245. nsecsig->private3 = closest->negsig;
  7246. nsecsig->privateuint4 = 0;
  7247. nsecsig->private5 = NULL;
  7248. nsec->private6 = NULL;
  7249. nsec->private7 = NULL;
  7250. dns_name_clone(&closest->name, name);
  7251. return (ISC_R_SUCCESS);
  7252. }
  7253. static void
  7254. rdataset_settrust(dns_rdataset_t *rdataset, dns_trust_t trust) {
  7255. dns_rbtdb_t *rbtdb = rdataset->private1;
  7256. dns_rbtnode_t *rbtnode = rdataset->private2;
  7257. rdatasetheader_t *header = rdataset->private3;
  7258. header--;
  7259. NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
  7260. isc_rwlocktype_write);
  7261. header->trust = rdataset->trust = trust;
  7262. NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
  7263. isc_rwlocktype_write);
  7264. }
  7265. static void
  7266. rdataset_expire(dns_rdataset_t *rdataset) {
  7267. dns_rbtdb_t *rbtdb = rdataset->private1;
  7268. dns_rbtnode_t *rbtnode = rdataset->private2;
  7269. rdatasetheader_t *header = rdataset->private3;
  7270. header--;
  7271. NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
  7272. isc_rwlocktype_write);
  7273. expire_header(rbtdb, header, ISC_FALSE);
  7274. NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
  7275. isc_rwlocktype_write);
  7276. }
  7277. /*
  7278. * Rdataset Iterator Methods
  7279. */
  7280. static void
  7281. rdatasetiter_destroy(dns_rdatasetiter_t **iteratorp) {
  7282. rbtdb_rdatasetiter_t *rbtiterator;
  7283. rbtiterator = (rbtdb_rdatasetiter_t *)(*iteratorp);
  7284. if (rbtiterator->common.version != NULL)
  7285. closeversion(rbtiterator->common.db,
  7286. &rbtiterator->common.version, ISC_FALSE);
  7287. detachnode(rbtiterator->common.db, &rbtiterator->common.node);
  7288. isc_mem_put(rbtiterator->common.db->mctx, rbtiterator,
  7289. sizeof(*rbtiterator));
  7290. *iteratorp = NULL;
  7291. }
  7292. static isc_result_t
  7293. rdatasetiter_first(dns_rdatasetiter_t *iterator) {
  7294. rbtdb_rdatasetiter_t *rbtiterator = (rbtdb_rdatasetiter_t *)iterator;
  7295. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)(rbtiterator->common.db);
  7296. dns_rbtnode_t *rbtnode = rbtiterator->common.node;
  7297. rbtdb_version_t *rbtversion = rbtiterator->common.version;
  7298. rdatasetheader_t *header, *top_next;
  7299. rbtdb_serial_t serial;
  7300. isc_stdtime_t now;
  7301. if (IS_CACHE(rbtdb)) {
  7302. serial = 1;
  7303. now = rbtiterator->common.now;
  7304. } else {
  7305. serial = rbtversion->serial;
  7306. now = 0;
  7307. }
  7308. NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
  7309. isc_rwlocktype_read);
  7310. for (header = rbtnode->data; header != NULL; header = top_next) {
  7311. top_next = header->next;
  7312. do {
  7313. if (header->serial <= serial && !IGNORE(header)) {
  7314. /*
  7315. * Is this a "this rdataset doesn't exist"
  7316. * record? Or is it too old in the cache?
  7317. *
  7318. * Note: unlike everywhere else, we
  7319. * check for now > header->rdh_ttl instead
  7320. * of now >= header->rdh_ttl. This allows
  7321. * ANY and RRSIG queries for 0 TTL
  7322. * rdatasets to work.
  7323. */
  7324. if (NONEXISTENT(header) ||
  7325. (now != 0 && now > header->rdh_ttl))
  7326. header = NULL;
  7327. break;
  7328. } else
  7329. header = header->down;
  7330. } while (header != NULL);
  7331. if (header != NULL)
  7332. break;
  7333. }
  7334. NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
  7335. isc_rwlocktype_read);
  7336. rbtiterator->current = header;
  7337. if (header == NULL)
  7338. return (ISC_R_NOMORE);
  7339. return (ISC_R_SUCCESS);
  7340. }
  7341. static isc_result_t
  7342. rdatasetiter_next(dns_rdatasetiter_t *iterator) {
  7343. rbtdb_rdatasetiter_t *rbtiterator = (rbtdb_rdatasetiter_t *)iterator;
  7344. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)(rbtiterator->common.db);
  7345. dns_rbtnode_t *rbtnode = rbtiterator->common.node;
  7346. rbtdb_version_t *rbtversion = rbtiterator->common.version;
  7347. rdatasetheader_t *header, *top_next;
  7348. rbtdb_serial_t serial;
  7349. isc_stdtime_t now;
  7350. rbtdb_rdatatype_t type, negtype;
  7351. dns_rdatatype_t rdtype, covers;
  7352. header = rbtiterator->current;
  7353. if (header == NULL)
  7354. return (ISC_R_NOMORE);
  7355. if (IS_CACHE(rbtdb)) {
  7356. serial = 1;
  7357. now = rbtiterator->common.now;
  7358. } else {
  7359. serial = rbtversion->serial;
  7360. now = 0;
  7361. }
  7362. NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
  7363. isc_rwlocktype_read);
  7364. type = header->type;
  7365. rdtype = RBTDB_RDATATYPE_BASE(header->type);
  7366. if (NEGATIVE(header)) {
  7367. covers = RBTDB_RDATATYPE_EXT(header->type);
  7368. negtype = RBTDB_RDATATYPE_VALUE(covers, 0);
  7369. } else
  7370. negtype = RBTDB_RDATATYPE_VALUE(0, rdtype);
  7371. for (header = header->next; header != NULL; header = top_next) {
  7372. top_next = header->next;
  7373. /*
  7374. * If not walking back up the down list.
  7375. */
  7376. if (header->type != type && header->type != negtype) {
  7377. do {
  7378. if (header->serial <= serial &&
  7379. !IGNORE(header)) {
  7380. /*
  7381. * Is this a "this rdataset doesn't
  7382. * exist" record?
  7383. *
  7384. * Note: unlike everywhere else, we
  7385. * check for now > header->ttl instead
  7386. * of now >= header->ttl. This allows
  7387. * ANY and RRSIG queries for 0 TTL
  7388. * rdatasets to work.
  7389. */
  7390. if ((header->attributes &
  7391. RDATASET_ATTR_NONEXISTENT) != 0 ||
  7392. (now != 0 && now > header->rdh_ttl))
  7393. header = NULL;
  7394. break;
  7395. } else
  7396. header = header->down;
  7397. } while (header != NULL);
  7398. if (header != NULL)
  7399. break;
  7400. }
  7401. }
  7402. NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
  7403. isc_rwlocktype_read);
  7404. rbtiterator->current = header;
  7405. if (header == NULL)
  7406. return (ISC_R_NOMORE);
  7407. return (ISC_R_SUCCESS);
  7408. }
  7409. static void
  7410. rdatasetiter_current(dns_rdatasetiter_t *iterator, dns_rdataset_t *rdataset) {
  7411. rbtdb_rdatasetiter_t *rbtiterator = (rbtdb_rdatasetiter_t *)iterator;
  7412. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)(rbtiterator->common.db);
  7413. dns_rbtnode_t *rbtnode = rbtiterator->common.node;
  7414. rdatasetheader_t *header;
  7415. header = rbtiterator->current;
  7416. REQUIRE(header != NULL);
  7417. NODE_LOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
  7418. isc_rwlocktype_read);
  7419. bind_rdataset(rbtdb, rbtnode, header, rbtiterator->common.now,
  7420. rdataset);
  7421. NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock,
  7422. isc_rwlocktype_read);
  7423. }
  7424. /*
  7425. * Database Iterator Methods
  7426. */
  7427. static inline void
  7428. reference_iter_node(rbtdb_dbiterator_t *rbtdbiter) {
  7429. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)rbtdbiter->common.db;
  7430. dns_rbtnode_t *node = rbtdbiter->node;
  7431. if (node == NULL)
  7432. return;
  7433. INSIST(rbtdbiter->tree_locked != isc_rwlocktype_none);
  7434. reactivate_node(rbtdb, node, rbtdbiter->tree_locked);
  7435. }
  7436. static inline void
  7437. dereference_iter_node(rbtdb_dbiterator_t *rbtdbiter) {
  7438. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)rbtdbiter->common.db;
  7439. dns_rbtnode_t *node = rbtdbiter->node;
  7440. nodelock_t *lock;
  7441. if (node == NULL)
  7442. return;
  7443. lock = &rbtdb->node_locks[node->locknum].lock;
  7444. NODE_LOCK(lock, isc_rwlocktype_read);
  7445. decrement_reference(rbtdb, node, 0, isc_rwlocktype_read,
  7446. rbtdbiter->tree_locked, ISC_FALSE);
  7447. NODE_UNLOCK(lock, isc_rwlocktype_read);
  7448. rbtdbiter->node = NULL;
  7449. }
  7450. static void
  7451. flush_deletions(rbtdb_dbiterator_t *rbtdbiter) {
  7452. dns_rbtnode_t *node;
  7453. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)rbtdbiter->common.db;
  7454. isc_boolean_t was_read_locked = ISC_FALSE;
  7455. nodelock_t *lock;
  7456. int i;
  7457. if (rbtdbiter->delete != 0) {
  7458. /*
  7459. * Note that "%d node of %d in tree" can report things like
  7460. * "flush_deletions: 59 nodes of 41 in tree". This means
  7461. * That some nodes appear on the deletions list more than
  7462. * once. Only the last occurence will actually be deleted.
  7463. */
  7464. isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
  7465. DNS_LOGMODULE_CACHE, ISC_LOG_DEBUG(1),
  7466. "flush_deletions: %d nodes of %d in tree",
  7467. rbtdbiter->delete,
  7468. dns_rbt_nodecount(rbtdb->tree));
  7469. if (rbtdbiter->tree_locked == isc_rwlocktype_read) {
  7470. RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
  7471. was_read_locked = ISC_TRUE;
  7472. }
  7473. RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
  7474. rbtdbiter->tree_locked = isc_rwlocktype_write;
  7475. for (i = 0; i < rbtdbiter->delete; i++) {
  7476. node = rbtdbiter->deletions[i];
  7477. lock = &rbtdb->node_locks[node->locknum].lock;
  7478. NODE_LOCK(lock, isc_rwlocktype_read);
  7479. decrement_reference(rbtdb, node, 0,
  7480. isc_rwlocktype_read,
  7481. rbtdbiter->tree_locked, ISC_FALSE);
  7482. NODE_UNLOCK(lock, isc_rwlocktype_read);
  7483. }
  7484. rbtdbiter->delete = 0;
  7485. RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_write);
  7486. if (was_read_locked) {
  7487. RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
  7488. rbtdbiter->tree_locked = isc_rwlocktype_read;
  7489. } else {
  7490. rbtdbiter->tree_locked = isc_rwlocktype_none;
  7491. }
  7492. }
  7493. }
  7494. static inline void
  7495. resume_iteration(rbtdb_dbiterator_t *rbtdbiter) {
  7496. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)rbtdbiter->common.db;
  7497. REQUIRE(rbtdbiter->paused);
  7498. REQUIRE(rbtdbiter->tree_locked == isc_rwlocktype_none);
  7499. RWLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
  7500. rbtdbiter->tree_locked = isc_rwlocktype_read;
  7501. rbtdbiter->paused = ISC_FALSE;
  7502. }
  7503. static void
  7504. dbiterator_destroy(dns_dbiterator_t **iteratorp) {
  7505. rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)(*iteratorp);
  7506. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)rbtdbiter->common.db;
  7507. dns_db_t *db = NULL;
  7508. if (rbtdbiter->tree_locked == isc_rwlocktype_read) {
  7509. RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
  7510. rbtdbiter->tree_locked = isc_rwlocktype_none;
  7511. } else
  7512. INSIST(rbtdbiter->tree_locked == isc_rwlocktype_none);
  7513. dereference_iter_node(rbtdbiter);
  7514. flush_deletions(rbtdbiter);
  7515. dns_db_attach(rbtdbiter->common.db, &db);
  7516. dns_db_detach(&rbtdbiter->common.db);
  7517. dns_rbtnodechain_reset(&rbtdbiter->chain);
  7518. dns_rbtnodechain_reset(&rbtdbiter->nsec3chain);
  7519. isc_mem_put(db->mctx, rbtdbiter, sizeof(*rbtdbiter));
  7520. dns_db_detach(&db);
  7521. *iteratorp = NULL;
  7522. }
  7523. static isc_result_t
  7524. dbiterator_first(dns_dbiterator_t *iterator) {
  7525. isc_result_t result;
  7526. rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)iterator;
  7527. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)iterator->db;
  7528. dns_name_t *name, *origin;
  7529. if (rbtdbiter->result != ISC_R_SUCCESS &&
  7530. rbtdbiter->result != ISC_R_NOMORE)
  7531. return (rbtdbiter->result);
  7532. if (rbtdbiter->paused)
  7533. resume_iteration(rbtdbiter);
  7534. dereference_iter_node(rbtdbiter);
  7535. name = dns_fixedname_name(&rbtdbiter->name);
  7536. origin = dns_fixedname_name(&rbtdbiter->origin);
  7537. dns_rbtnodechain_reset(&rbtdbiter->chain);
  7538. dns_rbtnodechain_reset(&rbtdbiter->nsec3chain);
  7539. if (rbtdbiter->nsec3only) {
  7540. rbtdbiter->current = &rbtdbiter->nsec3chain;
  7541. result = dns_rbtnodechain_first(rbtdbiter->current,
  7542. rbtdb->nsec3, name, origin);
  7543. } else {
  7544. rbtdbiter->current = &rbtdbiter->chain;
  7545. result = dns_rbtnodechain_first(rbtdbiter->current,
  7546. rbtdb->tree, name, origin);
  7547. if (!rbtdbiter->nonsec3 && result == ISC_R_NOTFOUND) {
  7548. rbtdbiter->current = &rbtdbiter->nsec3chain;
  7549. result = dns_rbtnodechain_first(rbtdbiter->current,
  7550. rbtdb->nsec3, name,
  7551. origin);
  7552. }
  7553. }
  7554. if (result == ISC_R_SUCCESS || result == DNS_R_NEWORIGIN) {
  7555. result = dns_rbtnodechain_current(rbtdbiter->current, NULL,
  7556. NULL, &rbtdbiter->node);
  7557. if (result == ISC_R_SUCCESS) {
  7558. rbtdbiter->new_origin = ISC_TRUE;
  7559. reference_iter_node(rbtdbiter);
  7560. }
  7561. } else {
  7562. INSIST(result == ISC_R_NOTFOUND);
  7563. result = ISC_R_NOMORE; /* The tree is empty. */
  7564. }
  7565. rbtdbiter->result = result;
  7566. return (result);
  7567. }
  7568. static isc_result_t
  7569. dbiterator_last(dns_dbiterator_t *iterator) {
  7570. isc_result_t result;
  7571. rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)iterator;
  7572. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)iterator->db;
  7573. dns_name_t *name, *origin;
  7574. if (rbtdbiter->result != ISC_R_SUCCESS &&
  7575. rbtdbiter->result != ISC_R_NOMORE)
  7576. return (rbtdbiter->result);
  7577. if (rbtdbiter->paused)
  7578. resume_iteration(rbtdbiter);
  7579. dereference_iter_node(rbtdbiter);
  7580. name = dns_fixedname_name(&rbtdbiter->name);
  7581. origin = dns_fixedname_name(&rbtdbiter->origin);
  7582. dns_rbtnodechain_reset(&rbtdbiter->chain);
  7583. dns_rbtnodechain_reset(&rbtdbiter->nsec3chain);
  7584. result = ISC_R_NOTFOUND;
  7585. if (rbtdbiter->nsec3only && !rbtdbiter->nonsec3) {
  7586. rbtdbiter->current = &rbtdbiter->nsec3chain;
  7587. result = dns_rbtnodechain_last(rbtdbiter->current,
  7588. rbtdb->nsec3, name, origin);
  7589. }
  7590. if (!rbtdbiter->nsec3only && result == ISC_R_NOTFOUND) {
  7591. rbtdbiter->current = &rbtdbiter->chain;
  7592. result = dns_rbtnodechain_last(rbtdbiter->current, rbtdb->tree,
  7593. name, origin);
  7594. }
  7595. if (result == ISC_R_SUCCESS || result == DNS_R_NEWORIGIN) {
  7596. result = dns_rbtnodechain_current(rbtdbiter->current, NULL,
  7597. NULL, &rbtdbiter->node);
  7598. if (result == ISC_R_SUCCESS) {
  7599. rbtdbiter->new_origin = ISC_TRUE;
  7600. reference_iter_node(rbtdbiter);
  7601. }
  7602. } else {
  7603. INSIST(result == ISC_R_NOTFOUND);
  7604. result = ISC_R_NOMORE; /* The tree is empty. */
  7605. }
  7606. rbtdbiter->result = result;
  7607. return (result);
  7608. }
  7609. static isc_result_t
  7610. dbiterator_seek(dns_dbiterator_t *iterator, dns_name_t *name) {
  7611. isc_result_t result, tresult;
  7612. rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)iterator;
  7613. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)iterator->db;
  7614. dns_name_t *iname, *origin;
  7615. if (rbtdbiter->result != ISC_R_SUCCESS &&
  7616. rbtdbiter->result != ISC_R_NOTFOUND &&
  7617. rbtdbiter->result != ISC_R_NOMORE)
  7618. return (rbtdbiter->result);
  7619. if (rbtdbiter->paused)
  7620. resume_iteration(rbtdbiter);
  7621. dereference_iter_node(rbtdbiter);
  7622. iname = dns_fixedname_name(&rbtdbiter->name);
  7623. origin = dns_fixedname_name(&rbtdbiter->origin);
  7624. dns_rbtnodechain_reset(&rbtdbiter->chain);
  7625. dns_rbtnodechain_reset(&rbtdbiter->nsec3chain);
  7626. if (rbtdbiter->nsec3only) {
  7627. rbtdbiter->current = &rbtdbiter->nsec3chain;
  7628. result = dns_rbt_findnode(rbtdb->nsec3, name, NULL,
  7629. &rbtdbiter->node,
  7630. rbtdbiter->current,
  7631. DNS_RBTFIND_EMPTYDATA, NULL, NULL);
  7632. } else if (rbtdbiter->nonsec3) {
  7633. rbtdbiter->current = &rbtdbiter->chain;
  7634. result = dns_rbt_findnode(rbtdb->tree, name, NULL,
  7635. &rbtdbiter->node,
  7636. rbtdbiter->current,
  7637. DNS_RBTFIND_EMPTYDATA, NULL, NULL);
  7638. } else {
  7639. /*
  7640. * Stay on main chain if not found on either chain.
  7641. */
  7642. rbtdbiter->current = &rbtdbiter->chain;
  7643. result = dns_rbt_findnode(rbtdb->tree, name, NULL,
  7644. &rbtdbiter->node,
  7645. rbtdbiter->current,
  7646. DNS_RBTFIND_EMPTYDATA, NULL, NULL);
  7647. if (result == DNS_R_PARTIALMATCH) {
  7648. dns_rbtnode_t *node = NULL;
  7649. tresult = dns_rbt_findnode(rbtdb->nsec3, name, NULL,
  7650. &node, &rbtdbiter->nsec3chain,
  7651. DNS_RBTFIND_EMPTYDATA,
  7652. NULL, NULL);
  7653. if (tresult == ISC_R_SUCCESS) {
  7654. rbtdbiter->node = node;
  7655. rbtdbiter->current = &rbtdbiter->nsec3chain;
  7656. result = tresult;
  7657. }
  7658. }
  7659. }
  7660. #if 1
  7661. if (result == ISC_R_SUCCESS) {
  7662. result = dns_rbtnodechain_current(rbtdbiter->current, iname,
  7663. origin, NULL);
  7664. if (result == ISC_R_SUCCESS) {
  7665. rbtdbiter->new_origin = ISC_TRUE;
  7666. reference_iter_node(rbtdbiter);
  7667. }
  7668. } else if (result == DNS_R_PARTIALMATCH) {
  7669. result = ISC_R_NOTFOUND;
  7670. rbtdbiter->node = NULL;
  7671. }
  7672. rbtdbiter->result = result;
  7673. #else
  7674. if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
  7675. isc_result_t tresult;
  7676. tresult = dns_rbtnodechain_current(rbtdbiter->current, iname,
  7677. origin, NULL);
  7678. if (tresult == ISC_R_SUCCESS) {
  7679. rbtdbiter->new_origin = ISC_TRUE;
  7680. reference_iter_node(rbtdbiter);
  7681. } else {
  7682. result = tresult;
  7683. rbtdbiter->node = NULL;
  7684. }
  7685. } else
  7686. rbtdbiter->node = NULL;
  7687. rbtdbiter->result = (result == DNS_R_PARTIALMATCH) ?
  7688. ISC_R_SUCCESS : result;
  7689. #endif
  7690. return (result);
  7691. }
  7692. static isc_result_t
  7693. dbiterator_prev(dns_dbiterator_t *iterator) {
  7694. isc_result_t result;
  7695. rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)iterator;
  7696. dns_name_t *name, *origin;
  7697. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)iterator->db;
  7698. REQUIRE(rbtdbiter->node != NULL);
  7699. if (rbtdbiter->result != ISC_R_SUCCESS)
  7700. return (rbtdbiter->result);
  7701. if (rbtdbiter->paused)
  7702. resume_iteration(rbtdbiter);
  7703. name = dns_fixedname_name(&rbtdbiter->name);
  7704. origin = dns_fixedname_name(&rbtdbiter->origin);
  7705. result = dns_rbtnodechain_prev(rbtdbiter->current, name, origin);
  7706. if (result == ISC_R_NOMORE && !rbtdbiter->nsec3only &&
  7707. !rbtdbiter->nonsec3 &&
  7708. &rbtdbiter->nsec3chain == rbtdbiter->current) {
  7709. rbtdbiter->current = &rbtdbiter->chain;
  7710. dns_rbtnodechain_reset(rbtdbiter->current);
  7711. result = dns_rbtnodechain_last(rbtdbiter->current, rbtdb->tree,
  7712. name, origin);
  7713. if (result == ISC_R_NOTFOUND)
  7714. result = ISC_R_NOMORE;
  7715. }
  7716. dereference_iter_node(rbtdbiter);
  7717. if (result == DNS_R_NEWORIGIN || result == ISC_R_SUCCESS) {
  7718. rbtdbiter->new_origin = ISC_TF(result == DNS_R_NEWORIGIN);
  7719. result = dns_rbtnodechain_current(rbtdbiter->current, NULL,
  7720. NULL, &rbtdbiter->node);
  7721. }
  7722. if (result == ISC_R_SUCCESS)
  7723. reference_iter_node(rbtdbiter);
  7724. rbtdbiter->result = result;
  7725. return (result);
  7726. }
  7727. static isc_result_t
  7728. dbiterator_next(dns_dbiterator_t *iterator) {
  7729. isc_result_t result;
  7730. rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)iterator;
  7731. dns_name_t *name, *origin;
  7732. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)iterator->db;
  7733. REQUIRE(rbtdbiter->node != NULL);
  7734. if (rbtdbiter->result != ISC_R_SUCCESS)
  7735. return (rbtdbiter->result);
  7736. if (rbtdbiter->paused)
  7737. resume_iteration(rbtdbiter);
  7738. name = dns_fixedname_name(&rbtdbiter->name);
  7739. origin = dns_fixedname_name(&rbtdbiter->origin);
  7740. result = dns_rbtnodechain_next(rbtdbiter->current, name, origin);
  7741. if (result == ISC_R_NOMORE && !rbtdbiter->nsec3only &&
  7742. !rbtdbiter->nonsec3 && &rbtdbiter->chain == rbtdbiter->current) {
  7743. rbtdbiter->current = &rbtdbiter->nsec3chain;
  7744. dns_rbtnodechain_reset(rbtdbiter->current);
  7745. result = dns_rbtnodechain_first(rbtdbiter->current,
  7746. rbtdb->nsec3, name, origin);
  7747. if (result == ISC_R_NOTFOUND)
  7748. result = ISC_R_NOMORE;
  7749. }
  7750. dereference_iter_node(rbtdbiter);
  7751. if (result == DNS_R_NEWORIGIN || result == ISC_R_SUCCESS) {
  7752. rbtdbiter->new_origin = ISC_TF(result == DNS_R_NEWORIGIN);
  7753. result = dns_rbtnodechain_current(rbtdbiter->current, NULL,
  7754. NULL, &rbtdbiter->node);
  7755. }
  7756. if (result == ISC_R_SUCCESS)
  7757. reference_iter_node(rbtdbiter);
  7758. rbtdbiter->result = result;
  7759. return (result);
  7760. }
  7761. static isc_result_t
  7762. dbiterator_current(dns_dbiterator_t *iterator, dns_dbnode_t **nodep,
  7763. dns_name_t *name)
  7764. {
  7765. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)iterator->db;
  7766. rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)iterator;
  7767. dns_rbtnode_t *node = rbtdbiter->node;
  7768. isc_result_t result;
  7769. dns_name_t *nodename = dns_fixedname_name(&rbtdbiter->name);
  7770. dns_name_t *origin = dns_fixedname_name(&rbtdbiter->origin);
  7771. REQUIRE(rbtdbiter->result == ISC_R_SUCCESS);
  7772. REQUIRE(rbtdbiter->node != NULL);
  7773. if (rbtdbiter->paused)
  7774. resume_iteration(rbtdbiter);
  7775. if (name != NULL) {
  7776. if (rbtdbiter->common.relative_names)
  7777. origin = NULL;
  7778. result = dns_name_concatenate(nodename, origin, name, NULL);
  7779. if (result != ISC_R_SUCCESS)
  7780. return (result);
  7781. if (rbtdbiter->common.relative_names && rbtdbiter->new_origin)
  7782. result = DNS_R_NEWORIGIN;
  7783. } else
  7784. result = ISC_R_SUCCESS;
  7785. NODE_STRONGLOCK(&rbtdb->node_locks[node->locknum].lock);
  7786. new_reference(rbtdb, node);
  7787. NODE_STRONGUNLOCK(&rbtdb->node_locks[node->locknum].lock);
  7788. *nodep = rbtdbiter->node;
  7789. if (iterator->cleaning && result == ISC_R_SUCCESS) {
  7790. isc_result_t expire_result;
  7791. /*
  7792. * If the deletion array is full, flush it before trying
  7793. * to expire the current node. The current node can't
  7794. * fully deleted while the iteration cursor is still on it.
  7795. */
  7796. if (rbtdbiter->delete == DELETION_BATCH_MAX)
  7797. flush_deletions(rbtdbiter);
  7798. expire_result = expirenode(iterator->db, *nodep, 0);
  7799. /*
  7800. * expirenode() currently always returns success.
  7801. */
  7802. if (expire_result == ISC_R_SUCCESS && node->down == NULL) {
  7803. unsigned int refs;
  7804. rbtdbiter->deletions[rbtdbiter->delete++] = node;
  7805. NODE_STRONGLOCK(&rbtdb->node_locks[node->locknum].lock);
  7806. dns_rbtnode_refincrement(node, &refs);
  7807. INSIST(refs != 0);
  7808. NODE_STRONGUNLOCK(&rbtdb->node_locks[node->locknum].lock);
  7809. }
  7810. }
  7811. return (result);
  7812. }
  7813. static isc_result_t
  7814. dbiterator_pause(dns_dbiterator_t *iterator) {
  7815. dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)iterator->db;
  7816. rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)iterator;
  7817. if (rbtdbiter->result != ISC_R_SUCCESS &&
  7818. rbtdbiter->result != ISC_R_NOMORE)
  7819. return (rbtdbiter->result);
  7820. if (rbtdbiter->paused)
  7821. return (ISC_R_SUCCESS);
  7822. rbtdbiter->paused = ISC_TRUE;
  7823. if (rbtdbiter->tree_locked != isc_rwlocktype_none) {
  7824. INSIST(rbtdbiter->tree_locked == isc_rwlocktype_read);
  7825. RWUNLOCK(&rbtdb->tree_lock, isc_rwlocktype_read);
  7826. rbtdbiter->tree_locked = isc_rwlocktype_none;
  7827. }
  7828. flush_deletions(rbtdbiter);
  7829. return (ISC_R_SUCCESS);
  7830. }
  7831. static isc_result_t
  7832. dbiterator_origin(dns_dbiterator_t *iterator, dns_name_t *name) {
  7833. rbtdb_dbiterator_t *rbtdbiter = (rbtdb_dbiterator_t *)iterator;
  7834. dns_name_t *origin = dns_fixedname_name(&rbtdbiter->origin);
  7835. if (rbtdbiter->result != ISC_R_SUCCESS)
  7836. return (rbtdbiter->result);
  7837. return (dns_name_copy(origin, name, NULL));
  7838. }
  7839. /*%
  7840. * Additional cache routines.
  7841. */
  7842. static isc_result_t
  7843. rdataset_getadditional(dns_rdataset_t *rdataset, dns_rdatasetadditional_t type,
  7844. dns_rdatatype_t qtype, dns_acache_t *acache,
  7845. dns_zone_t **zonep, dns_db_t **dbp,
  7846. dns_dbversion_t **versionp, dns_dbnode_t **nodep,
  7847. dns_name_t *fname, dns_message_t *msg,
  7848. isc_stdtime_t now)
  7849. {
  7850. #ifndef BIND9
  7851. UNUSED(rdataset);
  7852. UNUSED(type);
  7853. UNUSED(qtype);
  7854. UNUSED(acache);
  7855. UNUSED(zonep);
  7856. UNUSED(dbp);
  7857. UNUSED(versionp);
  7858. UNUSED(nodep);
  7859. UNUSED(fname);
  7860. UNUSED(msg);
  7861. UNUSED(now);
  7862. return (ISC_R_NOTIMPLEMENTED);
  7863. #else
  7864. dns_rbtdb_t *rbtdb = rdataset->private1;
  7865. dns_rbtnode_t *rbtnode = rdataset->private2;
  7866. unsigned char *raw = rdataset->private3; /* RDATASLAB */
  7867. unsigned int current_count = rdataset->privateuint4;
  7868. unsigned int count;
  7869. rdatasetheader_t *header;
  7870. nodelock_t *nodelock;
  7871. unsigned int total_count;
  7872. acachectl_t *acarray;
  7873. dns_acacheentry_t *entry;
  7874. isc_result_t result;
  7875. UNUSED(qtype); /* we do not use this value at least for now */
  7876. UNUSED(acache);
  7877. header = (struct rdatasetheader *)(raw - sizeof(*header));
  7878. total_count = raw[0] * 256 + raw[1];
  7879. INSIST(total_count > current_count);
  7880. count = total_count - current_count - 1;
  7881. acarray = NULL;
  7882. nodelock = &rbtdb->node_locks[rbtnode->locknum].lock;
  7883. NODE_LOCK(nodelock, isc_rwlocktype_read);
  7884. switch (type) {
  7885. case dns_rdatasetadditional_fromauth:
  7886. acarray = header->additional_auth;
  7887. break;
  7888. case dns_rdatasetadditional_fromcache:
  7889. acarray = NULL;
  7890. break;
  7891. case dns_rdatasetadditional_fromglue:
  7892. acarray = header->additional_glue;
  7893. break;
  7894. default:
  7895. INSIST(0);
  7896. }
  7897. if (acarray == NULL) {
  7898. if (type != dns_rdatasetadditional_fromcache)
  7899. dns_acache_countquerymiss(acache);
  7900. NODE_UNLOCK(nodelock, isc_rwlocktype_read);
  7901. return (ISC_R_NOTFOUND);
  7902. }
  7903. if (acarray[count].entry == NULL) {
  7904. dns_acache_countquerymiss(acache);
  7905. NODE_UNLOCK(nodelock, isc_rwlocktype_read);
  7906. return (ISC_R_NOTFOUND);
  7907. }
  7908. entry = NULL;
  7909. dns_acache_attachentry(acarray[count].entry, &entry);
  7910. NODE_UNLOCK(nodelock, isc_rwlocktype_read);
  7911. result = dns_acache_getentry(entry, zonep, dbp, versionp,
  7912. nodep, fname, msg, now);
  7913. dns_acache_detachentry(&entry);
  7914. return (result);
  7915. }
  7916. static void
  7917. acache_callback(dns_acacheentry_t *entry, void **arg) {
  7918. dns_rbtdb_t *rbtdb;
  7919. dns_rbtnode_t *rbtnode;
  7920. nodelock_t *nodelock;
  7921. acachectl_t *acarray = NULL;
  7922. acache_cbarg_t *cbarg;
  7923. unsigned int count;
  7924. REQUIRE(arg != NULL);
  7925. cbarg = *arg;
  7926. /*
  7927. * The caller must hold the entry lock.
  7928. */
  7929. rbtdb = (dns_rbtdb_t *)cbarg->db;
  7930. rbtnode = (dns_rbtnode_t *)cbarg->node;
  7931. nodelock = &rbtdb->node_locks[rbtnode->locknum].lock;
  7932. NODE_LOCK(nodelock, isc_rwlocktype_write);
  7933. switch (cbarg->type) {
  7934. case dns_rdatasetadditional_fromauth:
  7935. acarray = cbarg->header->additional_auth;
  7936. break;
  7937. case dns_rdatasetadditional_fromglue:
  7938. acarray = cbarg->header->additional_glue;
  7939. break;
  7940. default:
  7941. INSIST(0);
  7942. }
  7943. count = cbarg->count;
  7944. if (acarray != NULL && acarray[count].entry == entry) {
  7945. acarray[count].entry = NULL;
  7946. INSIST(acarray[count].cbarg == cbarg);
  7947. isc_mem_put(rbtdb->common.mctx, cbarg, sizeof(acache_cbarg_t));
  7948. acarray[count].cbarg = NULL;
  7949. } else
  7950. isc_mem_put(rbtdb->common.mctx, cbarg, sizeof(acache_cbarg_t));
  7951. dns_acache_detachentry(&entry);
  7952. NODE_UNLOCK(nodelock, isc_rwlocktype_write);
  7953. dns_db_detachnode((dns_db_t *)rbtdb, (dns_dbnode_t **)(void*)&rbtnode);
  7954. dns_db_detach((dns_db_t **)(void*)&rbtdb);
  7955. *arg = NULL;
  7956. #endif /* BIND9 */
  7957. }
  7958. #ifdef BIND9
  7959. static void
  7960. acache_cancelentry(isc_mem_t *mctx, dns_acacheentry_t *entry,
  7961. acache_cbarg_t **cbargp)
  7962. {
  7963. acache_cbarg_t *cbarg;
  7964. REQUIRE(mctx != NULL);
  7965. REQUIRE(entry != NULL);
  7966. REQUIRE(cbargp != NULL && *cbargp != NULL);
  7967. cbarg = *cbargp;
  7968. dns_acache_cancelentry(entry);
  7969. dns_db_detachnode(cbarg->db, &cbarg->node);
  7970. dns_db_detach(&cbarg->db);
  7971. isc_mem_put(mctx, cbarg, sizeof(acache_cbarg_t));
  7972. *cbargp = NULL;
  7973. }
  7974. #endif /* BIND9 */
  7975. static isc_result_t
  7976. rdataset_setadditional(dns_rdataset_t *rdataset, dns_rdatasetadditional_t type,
  7977. dns_rdatatype_t qtype, dns_acache_t *acache,
  7978. dns_zone_t *zone, dns_db_t *db,
  7979. dns_dbversion_t *version, dns_dbnode_t *node,
  7980. dns_name_t *fname)
  7981. {
  7982. #ifndef BIND9
  7983. UNUSED(rdataset);
  7984. UNUSED(type);
  7985. UNUSED(qtype);
  7986. UNUSED(acache);
  7987. UNUSED(zone);
  7988. UNUSED(db);
  7989. UNUSED(version);
  7990. UNUSED(node);
  7991. UNUSED(fname);
  7992. return (ISC_R_NOTIMPLEMENTED);
  7993. #else
  7994. dns_rbtdb_t *rbtdb = rdataset->private1;
  7995. dns_rbtnode_t *rbtnode = rdataset->private2;
  7996. unsigned char *raw = rdataset->private3; /* RDATASLAB */
  7997. unsigned int current_count = rdataset->privateuint4;
  7998. rdatasetheader_t *header;
  7999. unsigned int total_count, count;
  8000. nodelock_t *nodelock;
  8001. isc_result_t result;
  8002. acachectl_t *acarray;
  8003. dns_acacheentry_t *newentry, *oldentry = NULL;
  8004. acache_cbarg_t *newcbarg, *oldcbarg = NULL;
  8005. UNUSED(qtype);
  8006. if (type == dns_rdatasetadditional_fromcache)
  8007. return (ISC_R_SUCCESS);
  8008. header = (struct rdatasetheader *)(raw - sizeof(*header));
  8009. total_count = raw[0] * 256 + raw[1];
  8010. INSIST(total_count > current_count);
  8011. count = total_count - current_count - 1; /* should be private data */
  8012. newcbarg = isc_mem_get(rbtdb->common.mctx, sizeof(*newcbarg));
  8013. if (newcbarg == NULL)
  8014. return (ISC_R_NOMEMORY);
  8015. newcbarg->type = type;
  8016. newcbarg->count = count;
  8017. newcbarg->header = header;
  8018. newcbarg->db = NULL;
  8019. dns_db_attach((dns_db_t *)rbtdb, &newcbarg->db);
  8020. newcbarg->node = NULL;
  8021. dns_db_attachnode((dns_db_t *)rbtdb, (dns_dbnode_t *)rbtnode,
  8022. &newcbarg->node);
  8023. newentry = NULL;
  8024. result = dns_acache_createentry(acache, (dns_db_t *)rbtdb,
  8025. acache_callback, newcbarg, &newentry);
  8026. if (result != ISC_R_SUCCESS)
  8027. goto fail;
  8028. /* Set cache data in the new entry. */
  8029. result = dns_acache_setentry(acache, newentry, zone, db,
  8030. version, node, fname);
  8031. if (result != ISC_R_SUCCESS)
  8032. goto fail;
  8033. nodelock = &rbtdb->node_locks[rbtnode->locknum].lock;
  8034. NODE_LOCK(nodelock, isc_rwlocktype_write);
  8035. acarray = NULL;
  8036. switch (type) {
  8037. case dns_rdatasetadditional_fromauth:
  8038. acarray = header->additional_auth;
  8039. break;
  8040. case dns_rdatasetadditional_fromglue:
  8041. acarray = header->additional_glue;
  8042. break;
  8043. default:
  8044. INSIST(0);
  8045. }
  8046. if (acarray == NULL) {
  8047. unsigned int i;
  8048. acarray = isc_mem_get(rbtdb->common.mctx, total_count *
  8049. sizeof(acachectl_t));
  8050. if (acarray == NULL) {
  8051. NODE_UNLOCK(nodelock, isc_rwlocktype_write);
  8052. goto fail;
  8053. }
  8054. for (i = 0; i < total_count; i++) {
  8055. acarray[i].entry = NULL;
  8056. acarray[i].cbarg = NULL;
  8057. }
  8058. }
  8059. switch (type) {
  8060. case dns_rdatasetadditional_fromauth:
  8061. header->additional_auth = acarray;
  8062. break;
  8063. case dns_rdatasetadditional_fromglue:
  8064. header->additional_glue = acarray;
  8065. break;
  8066. default:
  8067. INSIST(0);
  8068. }
  8069. if (acarray[count].entry != NULL) {
  8070. /*
  8071. * Swap the entry. Delay cleaning-up the old entry since
  8072. * it would require a node lock.
  8073. */
  8074. oldentry = acarray[count].entry;
  8075. INSIST(acarray[count].cbarg != NULL);
  8076. oldcbarg = acarray[count].cbarg;
  8077. }
  8078. acarray[count].entry = newentry;
  8079. acarray[count].cbarg = newcbarg;
  8080. NODE_UNLOCK(nodelock, isc_rwlocktype_write);
  8081. if (oldentry != NULL) {
  8082. acache_cancelentry(rbtdb->common.mctx, oldentry, &oldcbarg);
  8083. dns_acache_detachentry(&oldentry);
  8084. }
  8085. return (ISC_R_SUCCESS);
  8086. fail:
  8087. if (newcbarg != NULL) {
  8088. if (newentry != NULL) {
  8089. acache_cancelentry(rbtdb->common.mctx, newentry,
  8090. &newcbarg);
  8091. dns_acache_detachentry(&newentry);
  8092. } else {
  8093. dns_db_detachnode((dns_db_t *)rbtdb, &newcbarg->node);
  8094. dns_db_detach(&newcbarg->db);
  8095. isc_mem_put(rbtdb->common.mctx, newcbarg,
  8096. sizeof(*newcbarg));
  8097. }
  8098. }
  8099. return (result);
  8100. #endif
  8101. }
  8102. static isc_result_t
  8103. rdataset_putadditional(dns_acache_t *acache, dns_rdataset_t *rdataset,
  8104. dns_rdatasetadditional_t type, dns_rdatatype_t qtype)
  8105. {
  8106. #ifndef BIND9
  8107. UNUSED(acache);
  8108. UNUSED(rdataset);
  8109. UNUSED(type);
  8110. UNUSED(qtype);
  8111. return (ISC_R_NOTIMPLEMENTED);
  8112. #else
  8113. dns_rbtdb_t *rbtdb = rdataset->private1;
  8114. dns_rbtnode_t *rbtnode = rdataset->private2;
  8115. unsigned char *raw = rdataset->private3; /* RDATASLAB */
  8116. unsigned int current_count = rdataset->privateuint4;
  8117. rdatasetheader_t *header;
  8118. nodelock_t *nodelock;
  8119. unsigned int total_count, count;
  8120. acachectl_t *acarray;
  8121. dns_acacheentry_t *entry;
  8122. acache_cbarg_t *cbarg;
  8123. UNUSED(qtype); /* we do not use this value at least for now */
  8124. UNUSED(acache);
  8125. if (type == dns_rdatasetadditional_fromcache)
  8126. return (ISC_R_SUCCESS);
  8127. header = (struct rdatasetheader *)(raw - sizeof(*header));
  8128. total_count = raw[0] * 256 + raw[1];
  8129. INSIST(total_count > current_count);
  8130. count = total_count - current_count - 1;
  8131. acarray = NULL;
  8132. entry = NULL;
  8133. nodelock = &rbtdb->node_locks[rbtnode->locknum].lock;
  8134. NODE_LOCK(nodelock, isc_rwlocktype_write);
  8135. switch (type) {
  8136. case dns_rdatasetadditional_fromauth:
  8137. acarray = header->additional_auth;
  8138. break;
  8139. case dns_rdatasetadditional_fromglue:
  8140. acarray = header->additional_glue;
  8141. break;
  8142. default:
  8143. INSIST(0);
  8144. }
  8145. if (acarray == NULL) {
  8146. NODE_UNLOCK(nodelock, isc_rwlocktype_write);
  8147. return (ISC_R_NOTFOUND);
  8148. }
  8149. entry = acarray[count].entry;
  8150. if (entry == NULL) {
  8151. NODE_UNLOCK(nodelock, isc_rwlocktype_write);
  8152. return (ISC_R_NOTFOUND);
  8153. }
  8154. acarray[count].entry = NULL;
  8155. cbarg = acarray[count].cbarg;
  8156. acarray[count].cbarg = NULL;
  8157. NODE_UNLOCK(nodelock, isc_rwlocktype_write);
  8158. if (entry != NULL) {
  8159. if (cbarg != NULL)
  8160. acache_cancelentry(rbtdb->common.mctx, entry, &cbarg);
  8161. dns_acache_detachentry(&entry);
  8162. }
  8163. return (ISC_R_SUCCESS);
  8164. #endif
  8165. }
  8166. /*%
  8167. * Routines for LRU-based cache management.
  8168. */
  8169. /*%
  8170. * See if a given cache entry that is being reused needs to be updated
  8171. * in the LRU-list. From the LRU management point of view, this function is
  8172. * expected to return true for almost all cases. When used with threads,
  8173. * however, this may cause a non-negligible performance penalty because a
  8174. * writer lock will have to be acquired before updating the list.
  8175. * If DNS_RBTDB_LIMITLRUUPDATE is defined to be non 0 at compilation time, this
  8176. * function returns true if the entry has not been updated for some period of
  8177. * time. We differentiate the NS or glue address case and the others since
  8178. * experiments have shown that the former tends to be accessed relatively
  8179. * infrequently and the cost of cache miss is higher (e.g., a missing NS records
  8180. * may cause external queries at a higher level zone, involving more
  8181. * transactions).
  8182. *
  8183. * Caller must hold the node (read or write) lock.
  8184. */
  8185. static inline isc_boolean_t
  8186. need_headerupdate(rdatasetheader_t *header, isc_stdtime_t now) {
  8187. if ((header->attributes &
  8188. (RDATASET_ATTR_NONEXISTENT|RDATASET_ATTR_STALE)) != 0)
  8189. return (ISC_FALSE);
  8190. #if DNS_RBTDB_LIMITLRUUPDATE
  8191. if (header->type == dns_rdatatype_ns ||
  8192. (header->trust == dns_trust_glue &&
  8193. (header->type == dns_rdatatype_a ||
  8194. header->type == dns_rdatatype_aaaa))) {
  8195. /*
  8196. * Glue records are updated if at least 60 seconds have passed
  8197. * since the previous update time.
  8198. */
  8199. return (header->last_used + 60 <= now);
  8200. }
  8201. /* Other records are updated if 5 minutes have passed. */
  8202. return (header->last_used + 300 <= now);
  8203. #else
  8204. UNUSED(now);
  8205. return (ISC_TRUE);
  8206. #endif
  8207. }
  8208. /*%
  8209. * Update the timestamp of a given cache entry and move it to the head
  8210. * of the corresponding LRU list.
  8211. *
  8212. * Caller must hold the node (write) lock.
  8213. *
  8214. * Note that the we do NOT touch the heap here, as the TTL has not changed.
  8215. */
  8216. static void
  8217. update_header(dns_rbtdb_t *rbtdb, rdatasetheader_t *header,
  8218. isc_stdtime_t now)
  8219. {
  8220. INSIST(IS_CACHE(rbtdb));
  8221. /* To be checked: can we really assume this? XXXMLG */
  8222. INSIST(ISC_LINK_LINKED(header, link));
  8223. ISC_LIST_UNLINK(rbtdb->rdatasets[header->node->locknum], header, link);
  8224. header->last_used = now;
  8225. ISC_LIST_PREPEND(rbtdb->rdatasets[header->node->locknum], header, link);
  8226. }
  8227. /*%
  8228. * Purge some expired and/or stale (i.e. unused for some period) cache entries
  8229. * under an overmem condition. To recover from this condition quickly, up to
  8230. * 2 entries will be purged. This process is triggered while adding a new
  8231. * entry, and we specifically avoid purging entries in the same LRU bucket as
  8232. * the one to which the new entry will belong. Otherwise, we might purge
  8233. * entries of the same name of different RR types while adding RRsets from a
  8234. * single response (consider the case where we're adding A and AAAA glue records
  8235. * of the same NS name).
  8236. */
  8237. static void
  8238. overmem_purge(dns_rbtdb_t *rbtdb, unsigned int locknum_start,
  8239. isc_stdtime_t now, isc_boolean_t tree_locked)
  8240. {
  8241. rdatasetheader_t *header, *header_prev;
  8242. unsigned int locknum;
  8243. int purgecount = 2;
  8244. for (locknum = (locknum_start + 1) % rbtdb->node_lock_count;
  8245. locknum != locknum_start && purgecount > 0;
  8246. locknum = (locknum + 1) % rbtdb->node_lock_count) {
  8247. NODE_LOCK(&rbtdb->node_locks[locknum].lock,
  8248. isc_rwlocktype_write);
  8249. header = isc_heap_element(rbtdb->heaps[locknum], 1);
  8250. if (header && header->rdh_ttl <= now - RBTDB_VIRTUAL) {
  8251. expire_header(rbtdb, header, tree_locked);
  8252. purgecount--;
  8253. }
  8254. for (header = ISC_LIST_TAIL(rbtdb->rdatasets[locknum]);
  8255. header != NULL && purgecount > 0;
  8256. header = header_prev) {
  8257. header_prev = ISC_LIST_PREV(header, link);
  8258. /*
  8259. * Unlink the entry at this point to avoid checking it
  8260. * again even if it's currently used someone else and
  8261. * cannot be purged at this moment. This entry won't be
  8262. * referenced any more (so unlinking is safe) since the
  8263. * TTL was reset to 0.
  8264. */
  8265. ISC_LIST_UNLINK(rbtdb->rdatasets[locknum], header,
  8266. link);
  8267. expire_header(rbtdb, header, tree_locked);
  8268. purgecount--;
  8269. }
  8270. NODE_UNLOCK(&rbtdb->node_locks[locknum].lock,
  8271. isc_rwlocktype_write);
  8272. }
  8273. }
  8274. static void
  8275. expire_header(dns_rbtdb_t *rbtdb, rdatasetheader_t *header,
  8276. isc_boolean_t tree_locked)
  8277. {
  8278. set_ttl(rbtdb, header, 0);
  8279. header->attributes |= RDATASET_ATTR_STALE;
  8280. header->node->dirty = 1;
  8281. /*
  8282. * Caller must hold the node (write) lock.
  8283. */
  8284. if (dns_rbtnode_refcurrent(header->node) == 0) {
  8285. /*
  8286. * If no one else is using the node, we can clean it up now.
  8287. * We first need to gain a new reference to the node to meet a
  8288. * requirement of decrement_reference().
  8289. */
  8290. new_reference(rbtdb, header->node);
  8291. decrement_reference(rbtdb, header->node, 0,
  8292. isc_rwlocktype_write,
  8293. tree_locked ? isc_rwlocktype_write :
  8294. isc_rwlocktype_none, ISC_FALSE);
  8295. }
  8296. }