2000-02-23 23:31:33 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2021-06-03 08:37:05 +02:00
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2020-09-14 16:20:40 -07:00
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2000-02-23 23:31:33 +00:00
|
|
|
*/
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*! \file */
|
2000-06-22 22:00:42 +00:00
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/mem.h>
|
2018-01-24 18:55:56 +11:00
|
|
|
#include <isc/refcount.h>
|
2021-10-04 17:14:53 +02:00
|
|
|
#include <isc/result.h>
|
2000-02-23 23:31:33 +00:00
|
|
|
#include <isc/rwlock.h>
|
2022-04-11 15:53:34 +01:00
|
|
|
#include <isc/string.h>
|
2000-02-23 23:31:33 +00:00
|
|
|
#include <isc/util.h>
|
|
|
|
|
2019-12-11 00:09:15 -08:00
|
|
|
#include <dns/dnssec.h>
|
2000-02-23 23:31:33 +00:00
|
|
|
#include <dns/fixedname.h>
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <dns/keytable.h>
|
2000-02-23 23:31:33 +00:00
|
|
|
#include <dns/rbt.h>
|
2019-09-16 21:06:23 -07:00
|
|
|
#include <dns/rdata.h>
|
|
|
|
#include <dns/rdatalist.h>
|
|
|
|
#include <dns/rdataset.h>
|
|
|
|
#include <dns/rdatastruct.h>
|
2000-02-23 23:31:33 +00:00
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
#define KEYTABLE_MAGIC ISC_MAGIC('K', 'T', 'b', 'l')
|
2020-02-12 13:59:18 +01:00
|
|
|
#define VALID_KEYTABLE(kt) ISC_MAGIC_VALID(kt, KEYTABLE_MAGIC)
|
2016-07-22 20:02:17 +10:00
|
|
|
|
2020-02-13 14:44:37 -08:00
|
|
|
#define KEYNODE_MAGIC ISC_MAGIC('K', 'N', 'o', 'd')
|
2020-02-12 13:59:18 +01:00
|
|
|
#define VALID_KEYNODE(kn) ISC_MAGIC_VALID(kn, KEYNODE_MAGIC)
|
2016-07-22 20:02:17 +10:00
|
|
|
|
|
|
|
struct dns_keytable {
|
2016-07-22 23:46:17 +00:00
|
|
|
/* Unlocked. */
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int magic;
|
|
|
|
isc_mem_t *mctx;
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_refcount_t references;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_rwlock_t rwlock;
|
2016-07-22 23:46:17 +00:00
|
|
|
/* Locked by rwlock. */
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_rbt_t *table;
|
2016-07-22 20:02:17 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
struct dns_keynode {
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int magic;
|
2020-06-10 17:07:52 +10:00
|
|
|
isc_mem_t *mctx;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_refcount_t refcount;
|
2020-06-10 17:07:52 +10:00
|
|
|
isc_rwlock_t rwlock;
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_rdatalist_t *dslist;
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_rdataset_t dsset;
|
|
|
|
bool managed;
|
|
|
|
bool initial;
|
2016-07-22 20:02:17 +10:00
|
|
|
};
|
|
|
|
|
2020-06-10 17:07:52 +10:00
|
|
|
static dns_keynode_t *
|
|
|
|
new_keynode(dns_rdata_ds_t *ds, dns_keytable_t *keytable, bool managed,
|
|
|
|
bool initial);
|
|
|
|
|
|
|
|
static void
|
2023-01-05 09:12:35 +01:00
|
|
|
keynode_disassociate(dns_rdataset_t *rdataset DNS__DB_FLARG);
|
2020-06-10 17:07:52 +10:00
|
|
|
static isc_result_t
|
|
|
|
keynode_first(dns_rdataset_t *rdataset);
|
|
|
|
static isc_result_t
|
|
|
|
keynode_next(dns_rdataset_t *rdataset);
|
|
|
|
static void
|
|
|
|
keynode_current(dns_rdataset_t *rdataset, dns_rdata_t *rdata);
|
|
|
|
static void
|
2023-01-05 09:12:35 +01:00
|
|
|
keynode_clone(dns_rdataset_t *source, dns_rdataset_t *target DNS__DB_FLARG);
|
2020-06-10 17:07:52 +10:00
|
|
|
|
|
|
|
static dns_rdatasetmethods_t methods = {
|
2023-02-17 11:46:58 -08:00
|
|
|
.disassociate = keynode_disassociate,
|
|
|
|
.first = keynode_first,
|
|
|
|
.next = keynode_next,
|
|
|
|
.current = keynode_current,
|
|
|
|
.clone = keynode_clone,
|
2020-06-10 17:07:52 +10:00
|
|
|
};
|
|
|
|
|
2019-12-23 20:26:03 -08:00
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
keynode_attach(dns_keynode_t *source, dns_keynode_t **target) {
|
2019-12-23 20:26:03 -08:00
|
|
|
REQUIRE(VALID_KEYNODE(source));
|
|
|
|
isc_refcount_increment(&source->refcount);
|
|
|
|
*target = source;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
keynode_detach(isc_mem_t *mctx, dns_keynode_t **keynodep) {
|
2019-12-23 20:26:03 -08:00
|
|
|
REQUIRE(keynodep != NULL && VALID_KEYNODE(*keynodep));
|
|
|
|
dns_keynode_t *knode = *keynodep;
|
|
|
|
*keynodep = NULL;
|
|
|
|
|
|
|
|
if (isc_refcount_decrement(&knode->refcount) == 1) {
|
|
|
|
dns_rdata_t *rdata = NULL;
|
|
|
|
isc_refcount_destroy(&knode->refcount);
|
2020-06-10 17:07:52 +10:00
|
|
|
isc_rwlock_destroy(&knode->rwlock);
|
2019-12-23 20:26:03 -08:00
|
|
|
if (knode->dslist != NULL) {
|
|
|
|
for (rdata = ISC_LIST_HEAD(knode->dslist->rdata);
|
|
|
|
rdata != NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
rdata = ISC_LIST_HEAD(knode->dslist->rdata))
|
|
|
|
{
|
2020-02-12 13:59:18 +01:00
|
|
|
ISC_LIST_UNLINK(knode->dslist->rdata, rdata,
|
|
|
|
link);
|
2019-12-23 20:26:03 -08:00
|
|
|
isc_mem_put(mctx, rdata->data,
|
|
|
|
DNS_DS_BUFFERSIZE);
|
|
|
|
isc_mem_put(mctx, rdata, sizeof(*rdata));
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_mem_put(mctx, knode->dslist,
|
|
|
|
sizeof(*knode->dslist));
|
|
|
|
knode->dslist = NULL;
|
|
|
|
}
|
2020-06-10 17:07:52 +10:00
|
|
|
isc_mem_putanddetach(&knode->mctx, knode,
|
|
|
|
sizeof(dns_keynode_t));
|
2019-12-23 20:26:03 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-04-05 17:19:43 +00:00
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
free_keynode(void *node, void *arg) {
|
2000-04-05 17:19:43 +00:00
|
|
|
dns_keynode_t *keynode = node;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_mem_t *mctx = arg;
|
2000-04-05 17:19:43 +00:00
|
|
|
|
2019-12-23 20:26:03 -08:00
|
|
|
keynode_detach(mctx, &keynode);
|
2000-04-05 17:19:43 +00:00
|
|
|
}
|
|
|
|
|
2000-02-23 23:31:33 +00:00
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_keytable_create(isc_mem_t *mctx, dns_keytable_t **keytablep) {
|
2000-02-23 23:31:33 +00:00
|
|
|
dns_keytable_t *keytable;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_result_t result;
|
2000-02-23 23:31:33 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a keytable.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(keytablep != NULL && *keytablep == NULL);
|
|
|
|
|
2001-11-12 19:05:39 +00:00
|
|
|
keytable = isc_mem_get(mctx, sizeof(*keytable));
|
2000-02-23 23:31:33 +00:00
|
|
|
|
|
|
|
keytable->table = NULL;
|
2000-04-05 17:19:43 +00:00
|
|
|
result = dns_rbt_create(mctx, free_keynode, mctx, &keytable->table);
|
2018-01-24 18:55:56 +11:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2000-02-23 23:31:33 +00:00
|
|
|
goto cleanup_keytable;
|
2018-01-24 18:55:56 +11:00
|
|
|
}
|
2000-02-23 23:31:33 +00:00
|
|
|
|
Add the reader-writer synchronization with modified C-RW-WP
This changes the internal isc_rwlock implementation to:
Irina Calciu, Dave Dice, Yossi Lev, Victor Luchangco, Virendra
J. Marathe, and Nir Shavit. 2013. NUMA-aware reader-writer locks.
SIGPLAN Not. 48, 8 (August 2013), 157–166.
DOI:https://doi.org/10.1145/2517327.24425
(The full article available from:
http://mcg.cs.tau.ac.il/papers/ppopp2013-rwlocks.pdf)
The implementation is based on the The Writer-Preference Lock (C-RW-WP)
variant (see the 3.4 section of the paper for the rationale).
The implemented algorithm has been modified for simplicity and for usage
patterns in rbtdb.c.
The changes compared to the original algorithm:
* We haven't implemented the cohort locks because that would require a
knowledge of NUMA nodes, instead a simple atomic_bool is used as
synchronization point for writer lock.
* The per-thread reader counters are not being used - this would
require the internal thread id (isc_tid_v) to be always initialized,
even in the utilities; the change has a slight performance penalty,
so we might revisit this change in the future. However, this change
also saves a lot of memory, because cache-line aligned counters were
used, so on 32-core machine, the rwlock would be 4096+ bytes big.
* The readers use a writer_barrier that will raise after a while when
readers lock can't be acquired to prevent readers starvation.
* Separate ingress and egress readers counters queues to reduce both
inter and intra-thread contention.
2021-03-24 17:52:56 +01:00
|
|
|
isc_rwlock_init(&keytable->rwlock);
|
2018-08-01 11:46:11 +02:00
|
|
|
isc_refcount_init(&keytable->references, 1);
|
2000-02-23 23:31:33 +00:00
|
|
|
|
2013-02-20 21:39:05 -08:00
|
|
|
keytable->mctx = NULL;
|
|
|
|
isc_mem_attach(mctx, &keytable->mctx);
|
2000-02-24 00:33:02 +00:00
|
|
|
keytable->magic = KEYTABLE_MAGIC;
|
2000-02-23 23:31:33 +00:00
|
|
|
*keytablep = keytable;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
cleanup_keytable:
|
2013-02-20 21:39:05 -08:00
|
|
|
isc_mem_putanddetach(&mctx, keytable, sizeof(*keytable));
|
2000-02-23 23:31:33 +00:00
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_keytable_attach(dns_keytable_t *source, dns_keytable_t **targetp) {
|
2000-02-23 23:31:33 +00:00
|
|
|
REQUIRE(VALID_KEYTABLE(source));
|
|
|
|
REQUIRE(targetp != NULL && *targetp == NULL);
|
|
|
|
|
2018-08-17 15:16:59 +02:00
|
|
|
isc_refcount_increment(&source->references);
|
2000-02-23 23:31:33 +00:00
|
|
|
|
|
|
|
*targetp = source;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_keytable_detach(dns_keytable_t **keytablep) {
|
2000-02-23 23:31:33 +00:00
|
|
|
REQUIRE(keytablep != NULL && VALID_KEYTABLE(*keytablep));
|
2018-08-28 10:18:59 +02:00
|
|
|
dns_keytable_t *keytable = *keytablep;
|
|
|
|
*keytablep = NULL;
|
2000-02-23 23:31:33 +00:00
|
|
|
|
2018-08-17 15:16:59 +02:00
|
|
|
if (isc_refcount_decrement(&keytable->references) == 1) {
|
2018-01-24 18:55:56 +11:00
|
|
|
isc_refcount_destroy(&keytable->references);
|
2000-02-23 23:31:33 +00:00
|
|
|
dns_rbt_destroy(&keytable->table);
|
|
|
|
isc_rwlock_destroy(&keytable->rwlock);
|
|
|
|
keytable->magic = 0;
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_mem_putanddetach(&keytable->mctx, keytable,
|
|
|
|
sizeof(*keytable));
|
2000-02-23 23:31:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-10 17:07:52 +10:00
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
add_ds(dns_keynode_t *knode, dns_rdata_ds_t *ds, isc_mem_t *mctx) {
|
2019-12-20 16:23:25 -08:00
|
|
|
isc_result_t result;
|
2019-12-23 20:26:03 -08:00
|
|
|
dns_rdata_t *dsrdata = NULL, *rdata = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
void *data = NULL;
|
|
|
|
bool exists = false;
|
2019-12-20 16:23:25 -08:00
|
|
|
isc_buffer_t b;
|
|
|
|
|
2019-12-23 20:26:03 -08:00
|
|
|
dsrdata = isc_mem_get(mctx, sizeof(*dsrdata));
|
|
|
|
dns_rdata_init(dsrdata);
|
2017-10-27 15:45:18 -07:00
|
|
|
|
2019-12-20 16:23:25 -08:00
|
|
|
data = isc_mem_get(mctx, DNS_DS_BUFFERSIZE);
|
|
|
|
isc_buffer_init(&b, data, DNS_DS_BUFFERSIZE);
|
2019-09-16 21:06:23 -07:00
|
|
|
|
2019-12-23 20:26:03 -08:00
|
|
|
result = dns_rdata_fromstruct(dsrdata, dns_rdataclass_in,
|
2019-12-20 16:23:25 -08:00
|
|
|
dns_rdatatype_ds, ds, &b);
|
2020-06-10 17:07:52 +10:00
|
|
|
RUNTIME_CHECK(result == ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
RWLOCK(&knode->rwlock, isc_rwlocktype_write);
|
|
|
|
|
|
|
|
if (knode->dslist == NULL) {
|
|
|
|
knode->dslist = isc_mem_get(mctx, sizeof(*knode->dslist));
|
|
|
|
dns_rdatalist_init(knode->dslist);
|
|
|
|
knode->dslist->rdclass = dns_rdataclass_in;
|
|
|
|
knode->dslist->type = dns_rdatatype_ds;
|
|
|
|
|
|
|
|
INSIST(knode->dsset.methods == NULL);
|
|
|
|
knode->dsset.methods = &methods;
|
|
|
|
knode->dsset.rdclass = knode->dslist->rdclass;
|
|
|
|
knode->dsset.type = knode->dslist->type;
|
|
|
|
knode->dsset.covers = knode->dslist->covers;
|
|
|
|
knode->dsset.ttl = knode->dslist->ttl;
|
|
|
|
knode->dsset.private1 = knode;
|
|
|
|
knode->dsset.private2 = NULL;
|
|
|
|
knode->dsset.private3 = NULL;
|
|
|
|
knode->dsset.privateuint4 = 0;
|
|
|
|
knode->dsset.private5 = NULL;
|
|
|
|
knode->dsset.trust = dns_trust_ultimate;
|
2019-12-20 16:23:25 -08:00
|
|
|
}
|
2019-09-16 21:06:23 -07:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
for (rdata = ISC_LIST_HEAD(knode->dslist->rdata); rdata != NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
rdata = ISC_LIST_NEXT(rdata, link))
|
|
|
|
{
|
2019-12-23 20:26:03 -08:00
|
|
|
if (dns_rdata_compare(rdata, dsrdata) == 0) {
|
|
|
|
exists = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exists) {
|
|
|
|
isc_mem_put(mctx, dsrdata->data, DNS_DS_BUFFERSIZE);
|
|
|
|
isc_mem_put(mctx, dsrdata, sizeof(*dsrdata));
|
2020-06-10 17:07:52 +10:00
|
|
|
} else {
|
|
|
|
ISC_LIST_APPEND(knode->dslist->rdata, dsrdata, link);
|
2017-10-27 15:45:18 -07:00
|
|
|
}
|
|
|
|
|
2020-06-10 17:07:52 +10:00
|
|
|
RWUNLOCK(&knode->rwlock, isc_rwlocktype_write);
|
2017-10-27 15:45:18 -07:00
|
|
|
}
|
|
|
|
|
2019-12-23 20:26:03 -08:00
|
|
|
static isc_result_t
|
2020-06-10 17:07:52 +10:00
|
|
|
delete_ds(dns_keytable_t *keytable, dns_rbtnode_t *node, dns_rdata_ds_t *ds) {
|
|
|
|
dns_keynode_t *knode = node->data;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_result_t result;
|
|
|
|
dns_rdata_t dsrdata = DNS_RDATA_INIT;
|
|
|
|
dns_rdata_t *rdata = NULL;
|
2019-12-23 20:26:03 -08:00
|
|
|
unsigned char data[DNS_DS_BUFFERSIZE];
|
2020-02-13 14:44:37 -08:00
|
|
|
bool found = false;
|
|
|
|
isc_buffer_t b;
|
2019-12-23 20:26:03 -08:00
|
|
|
|
2020-06-10 17:07:52 +10:00
|
|
|
RWLOCK(&knode->rwlock, isc_rwlocktype_read);
|
2019-12-23 20:26:03 -08:00
|
|
|
if (knode->dslist == NULL) {
|
2020-06-10 17:07:52 +10:00
|
|
|
RWUNLOCK(&knode->rwlock, isc_rwlocktype_read);
|
2019-12-23 20:26:03 -08:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_buffer_init(&b, data, DNS_DS_BUFFERSIZE);
|
|
|
|
|
|
|
|
result = dns_rdata_fromstruct(&dsrdata, dns_rdataclass_in,
|
|
|
|
dns_rdatatype_ds, ds, &b);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
2020-06-10 17:07:52 +10:00
|
|
|
RWUNLOCK(&knode->rwlock, isc_rwlocktype_write);
|
2019-12-23 20:26:03 -08:00
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
for (rdata = ISC_LIST_HEAD(knode->dslist->rdata); rdata != NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
rdata = ISC_LIST_NEXT(rdata, link))
|
|
|
|
{
|
2019-12-23 20:26:03 -08:00
|
|
|
if (dns_rdata_compare(rdata, &dsrdata) == 0) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-10 17:07:52 +10:00
|
|
|
if (!found) {
|
|
|
|
RWUNLOCK(&knode->rwlock, isc_rwlocktype_read);
|
2019-12-23 20:26:03 -08:00
|
|
|
/*
|
|
|
|
* The keyname must have matched or we wouldn't be here,
|
|
|
|
* so we use DNS_R_PARTIALMATCH instead of ISC_R_NOTFOUND.
|
|
|
|
*/
|
|
|
|
return (DNS_R_PARTIALMATCH);
|
|
|
|
}
|
2020-06-10 17:07:52 +10:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Replace knode with a new instance without the DS.
|
|
|
|
*/
|
|
|
|
node->data = new_keynode(NULL, keytable, knode->managed,
|
|
|
|
knode->initial);
|
|
|
|
for (rdata = ISC_LIST_HEAD(knode->dslist->rdata); rdata != NULL;
|
|
|
|
rdata = ISC_LIST_NEXT(rdata, link))
|
|
|
|
{
|
|
|
|
if (dns_rdata_compare(rdata, &dsrdata) != 0) {
|
|
|
|
dns_rdata_ds_t ds0;
|
|
|
|
result = dns_rdata_tostruct(rdata, &ds0, NULL);
|
|
|
|
RUNTIME_CHECK(result == ISC_R_SUCCESS);
|
|
|
|
add_ds(node->data, &ds0, keytable->mctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
RWUNLOCK(&knode->rwlock, isc_rwlocktype_read);
|
|
|
|
|
|
|
|
keynode_detach(keytable->mctx, &knode);
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
2019-12-23 20:26:03 -08:00
|
|
|
}
|
|
|
|
|
2017-10-27 15:45:18 -07:00
|
|
|
/*%
|
2019-12-20 16:23:25 -08:00
|
|
|
* Create a keynode for "ds" (or a null key node if "ds" is NULL), set
|
|
|
|
* "managed" and "initial" as requested and attach the keynode to
|
|
|
|
* to "node" in "keytable".
|
2017-10-27 15:45:18 -07:00
|
|
|
*/
|
2020-06-10 17:07:52 +10:00
|
|
|
static dns_keynode_t *
|
|
|
|
new_keynode(dns_rdata_ds_t *ds, dns_keytable_t *keytable, bool managed,
|
|
|
|
bool initial) {
|
2009-06-30 02:53:46 +00:00
|
|
|
dns_keynode_t *knode = NULL;
|
2000-02-23 23:31:33 +00:00
|
|
|
|
|
|
|
REQUIRE(VALID_KEYTABLE(keytable));
|
2017-10-27 15:45:18 -07:00
|
|
|
REQUIRE(!initial || managed);
|
2000-02-23 23:31:33 +00:00
|
|
|
|
2019-12-20 16:23:25 -08:00
|
|
|
knode = isc_mem_get(keytable->mctx, sizeof(dns_keynode_t));
|
2020-02-12 13:59:18 +01:00
|
|
|
*knode = (dns_keynode_t){ .magic = KEYNODE_MAGIC };
|
2019-12-20 16:23:25 -08:00
|
|
|
|
|
|
|
dns_rdataset_init(&knode->dsset);
|
|
|
|
isc_refcount_init(&knode->refcount, 1);
|
Add the reader-writer synchronization with modified C-RW-WP
This changes the internal isc_rwlock implementation to:
Irina Calciu, Dave Dice, Yossi Lev, Victor Luchangco, Virendra
J. Marathe, and Nir Shavit. 2013. NUMA-aware reader-writer locks.
SIGPLAN Not. 48, 8 (August 2013), 157–166.
DOI:https://doi.org/10.1145/2517327.24425
(The full article available from:
http://mcg.cs.tau.ac.il/papers/ppopp2013-rwlocks.pdf)
The implementation is based on the The Writer-Preference Lock (C-RW-WP)
variant (see the 3.4 section of the paper for the rationale).
The implemented algorithm has been modified for simplicity and for usage
patterns in rbtdb.c.
The changes compared to the original algorithm:
* We haven't implemented the cohort locks because that would require a
knowledge of NUMA nodes, instead a simple atomic_bool is used as
synchronization point for writer lock.
* The per-thread reader counters are not being used - this would
require the internal thread id (isc_tid_v) to be always initialized,
even in the utilities; the change has a slight performance penalty,
so we might revisit this change in the future. However, this change
also saves a lot of memory, because cache-line aligned counters were
used, so on 32-core machine, the rwlock would be 4096+ bytes big.
* The readers use a writer_barrier that will raise after a while when
readers lock can't be acquired to prevent readers starvation.
* Separate ingress and egress readers counters queues to reduce both
inter and intra-thread contention.
2021-03-24 17:52:56 +01:00
|
|
|
isc_rwlock_init(&knode->rwlock);
|
2000-02-23 23:31:33 +00:00
|
|
|
|
2017-10-27 15:45:18 -07:00
|
|
|
/*
|
2019-12-20 16:23:25 -08:00
|
|
|
* If a DS was supplied, initialize an rdatalist.
|
2017-10-27 15:45:18 -07:00
|
|
|
*/
|
2019-12-20 16:23:25 -08:00
|
|
|
if (ds != NULL) {
|
2020-06-10 17:07:52 +10:00
|
|
|
add_ds(knode, ds, keytable->mctx);
|
2017-10-27 15:45:18 -07:00
|
|
|
}
|
2000-02-23 23:31:33 +00:00
|
|
|
|
2020-06-10 17:07:52 +10:00
|
|
|
isc_mem_attach(keytable->mctx, &knode->mctx);
|
2017-10-27 15:45:18 -07:00
|
|
|
knode->managed = managed;
|
|
|
|
knode->initial = initial;
|
2019-09-16 21:06:23 -07:00
|
|
|
|
2020-06-10 17:07:52 +10:00
|
|
|
return (knode);
|
2017-10-27 15:45:18 -07:00
|
|
|
}
|
2000-02-23 23:31:33 +00:00
|
|
|
|
2017-10-27 15:45:18 -07:00
|
|
|
/*%
|
2019-12-20 16:23:25 -08:00
|
|
|
* Add key trust anchor "ds" at "keyname" in "keytable". If an anchor
|
|
|
|
* already exists at the requested name does not contain "ds", update it.
|
|
|
|
* If "ds" is NULL, add a null key to indicate that "keyname" should be
|
|
|
|
* treated as a secure domain without supplying key data which would allow
|
|
|
|
* the domain to be validated.
|
2017-10-27 15:45:18 -07:00
|
|
|
*/
|
|
|
|
static isc_result_t
|
2018-04-17 08:29:14 -07:00
|
|
|
insert(dns_keytable_t *keytable, bool managed, bool initial,
|
2022-06-17 10:40:47 +10:00
|
|
|
const dns_name_t *keyname, dns_rdata_ds_t *ds,
|
|
|
|
dns_keytable_callback_t callback, void *callback_arg) {
|
2017-10-27 15:45:18 -07:00
|
|
|
dns_rbtnode_t *node = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_result_t result;
|
2009-07-13 21:53:03 +00:00
|
|
|
|
2017-10-27 15:45:18 -07:00
|
|
|
REQUIRE(VALID_KEYTABLE(keytable));
|
2009-06-30 02:53:46 +00:00
|
|
|
|
2017-10-27 15:45:18 -07:00
|
|
|
RWLOCK(&keytable->rwlock, isc_rwlocktype_write);
|
2009-06-30 02:53:46 +00:00
|
|
|
|
2017-10-27 15:45:18 -07:00
|
|
|
result = dns_rbt_addnode(keytable->table, keyname, &node);
|
2009-06-30 02:53:46 +00:00
|
|
|
if (result == ISC_R_SUCCESS) {
|
2017-10-27 15:45:18 -07:00
|
|
|
/*
|
|
|
|
* There was no node for "keyname" in "keytable" yet, so one
|
2019-09-16 21:06:23 -07:00
|
|
|
* was created. Create a new key node for the supplied
|
2019-12-23 20:26:03 -08:00
|
|
|
* trust anchor (or a null key node if "ds" is NULL)
|
2019-12-20 16:23:25 -08:00
|
|
|
* and attach it to the created node.
|
2017-10-27 15:45:18 -07:00
|
|
|
*/
|
2020-06-10 17:07:52 +10:00
|
|
|
node->data = new_keynode(ds, keytable, managed, initial);
|
2022-06-17 10:40:47 +10:00
|
|
|
if (callback != NULL) {
|
|
|
|
(*callback)(keyname, callback_arg);
|
|
|
|
}
|
2017-10-27 15:45:18 -07:00
|
|
|
} else if (result == ISC_R_EXISTS) {
|
|
|
|
/*
|
|
|
|
* A node already exists for "keyname" in "keytable".
|
|
|
|
*/
|
2020-06-10 17:07:52 +10:00
|
|
|
if (ds != NULL) {
|
2019-12-20 16:23:25 -08:00
|
|
|
dns_keynode_t *knode = node->data;
|
|
|
|
if (knode == NULL) {
|
2020-06-10 17:07:52 +10:00
|
|
|
node->data = new_keynode(ds, keytable, managed,
|
|
|
|
initial);
|
2022-06-17 10:40:47 +10:00
|
|
|
if (callback != NULL) {
|
|
|
|
(*callback)(keyname, callback_arg);
|
|
|
|
}
|
2019-12-20 16:23:25 -08:00
|
|
|
} else {
|
2020-06-10 17:07:52 +10:00
|
|
|
add_ds(knode, ds, keytable->mctx);
|
2017-10-27 15:45:18 -07:00
|
|
|
}
|
|
|
|
}
|
2020-06-10 17:07:52 +10:00
|
|
|
result = ISC_R_SUCCESS;
|
2000-02-23 23:31:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RWUNLOCK(&keytable->rwlock, isc_rwlocktype_write);
|
|
|
|
|
2009-06-30 02:53:46 +00:00
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2019-12-20 11:37:11 -08:00
|
|
|
dns_keytable_add(dns_keytable_t *keytable, bool managed, bool initial,
|
2022-06-17 10:40:47 +10:00
|
|
|
dns_name_t *name, dns_rdata_ds_t *ds,
|
|
|
|
dns_keytable_callback_t callback, void *callback_arg) {
|
2019-12-20 11:37:11 -08:00
|
|
|
REQUIRE(ds != NULL);
|
2017-10-27 15:45:18 -07:00
|
|
|
REQUIRE(!initial || managed);
|
2019-12-23 20:26:03 -08:00
|
|
|
|
2022-06-17 10:40:47 +10:00
|
|
|
return (insert(keytable, managed, initial, name, ds, callback,
|
|
|
|
callback_arg));
|
2009-06-30 02:53:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_keytable_marksecure(dns_keytable_t *keytable, const dns_name_t *name) {
|
2022-06-17 10:40:47 +10:00
|
|
|
return (insert(keytable, true, false, name, NULL, NULL, NULL));
|
2009-06-30 02:53:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2022-06-17 10:40:47 +10:00
|
|
|
dns_keytable_delete(dns_keytable_t *keytable, const dns_name_t *keyname,
|
|
|
|
dns_keytable_callback_t callback, void *callback_arg) {
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_result_t result;
|
2009-06-30 02:53:46 +00:00
|
|
|
dns_rbtnode_t *node = NULL;
|
|
|
|
|
|
|
|
REQUIRE(VALID_KEYTABLE(keytable));
|
|
|
|
REQUIRE(keyname != NULL);
|
2009-07-01 23:47:36 +00:00
|
|
|
|
2009-06-30 02:53:46 +00:00
|
|
|
RWLOCK(&keytable->rwlock, isc_rwlocktype_write);
|
|
|
|
result = dns_rbt_findnode(keytable->table, keyname, NULL, &node, NULL,
|
|
|
|
DNS_RBTFIND_NOOPTIONS, NULL, NULL);
|
|
|
|
if (result == ISC_R_SUCCESS) {
|
2019-12-23 20:26:03 -08:00
|
|
|
if (node->data != NULL) {
|
2020-02-12 13:59:18 +01:00
|
|
|
result = dns_rbt_deletenode(keytable->table, node,
|
|
|
|
false);
|
2022-06-17 10:40:47 +10:00
|
|
|
if (callback != NULL) {
|
|
|
|
(*callback)(keyname, callback_arg);
|
|
|
|
}
|
2019-12-23 20:26:03 -08:00
|
|
|
} else {
|
2009-06-30 02:53:46 +00:00
|
|
|
result = ISC_R_NOTFOUND;
|
2019-12-23 20:26:03 -08:00
|
|
|
}
|
|
|
|
} else if (result == DNS_R_PARTIALMATCH) {
|
2009-06-30 02:53:46 +00:00
|
|
|
result = ISC_R_NOTFOUND;
|
2019-12-23 20:26:03 -08:00
|
|
|
}
|
2009-06-30 02:53:46 +00:00
|
|
|
RWUNLOCK(&keytable->rwlock, isc_rwlocktype_write);
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2019-12-11 00:09:15 -08:00
|
|
|
dns_keytable_deletekey(dns_keytable_t *keytable, const dns_name_t *keyname,
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_rdata_dnskey_t *dnskey) {
|
|
|
|
isc_result_t result;
|
2009-06-30 02:53:46 +00:00
|
|
|
dns_rbtnode_t *node = NULL;
|
2019-12-23 20:26:03 -08:00
|
|
|
dns_keynode_t *knode = NULL;
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_rdata_t rdata = DNS_RDATA_INIT;
|
|
|
|
unsigned char data[4096], digest[DNS_DS_BUFFERSIZE];
|
2019-12-23 20:26:03 -08:00
|
|
|
dns_rdata_ds_t ds;
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_buffer_t b;
|
2009-06-30 02:53:46 +00:00
|
|
|
|
|
|
|
REQUIRE(VALID_KEYTABLE(keytable));
|
2019-12-11 00:09:15 -08:00
|
|
|
REQUIRE(dnskey != NULL);
|
|
|
|
|
2019-12-23 20:26:03 -08:00
|
|
|
isc_buffer_init(&b, data, sizeof(data));
|
2019-12-11 00:09:15 -08:00
|
|
|
dns_rdata_fromstruct(&rdata, dnskey->common.rdclass,
|
2019-12-23 20:26:03 -08:00
|
|
|
dns_rdatatype_dnskey, dnskey, &b);
|
2009-06-30 02:53:46 +00:00
|
|
|
|
|
|
|
RWLOCK(&keytable->rwlock, isc_rwlocktype_write);
|
|
|
|
result = dns_rbt_findnode(keytable->table, keyname, NULL, &node, NULL,
|
|
|
|
DNS_RBTFIND_NOOPTIONS, NULL, NULL);
|
|
|
|
|
2019-12-11 00:09:15 -08:00
|
|
|
if (result == DNS_R_PARTIALMATCH) {
|
2009-06-30 02:53:46 +00:00
|
|
|
result = ISC_R_NOTFOUND;
|
2019-12-11 00:09:15 -08:00
|
|
|
}
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
2009-06-30 02:53:46 +00:00
|
|
|
goto finish;
|
2019-12-11 00:09:15 -08:00
|
|
|
}
|
2009-06-30 02:53:46 +00:00
|
|
|
|
|
|
|
if (node->data == NULL) {
|
|
|
|
result = ISC_R_NOTFOUND;
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
knode = node->data;
|
2019-12-23 20:26:03 -08:00
|
|
|
|
2020-06-10 17:07:52 +10:00
|
|
|
RWLOCK(&knode->rwlock, isc_rwlocktype_read);
|
2019-12-23 20:26:03 -08:00
|
|
|
if (knode->dslist == NULL) {
|
2020-06-10 17:07:52 +10:00
|
|
|
RWUNLOCK(&knode->rwlock, isc_rwlocktype_read);
|
2019-12-23 20:26:03 -08:00
|
|
|
result = DNS_R_PARTIALMATCH;
|
2009-06-30 02:53:46 +00:00
|
|
|
goto finish;
|
|
|
|
}
|
2020-06-10 17:07:52 +10:00
|
|
|
RWUNLOCK(&knode->rwlock, isc_rwlocktype_read);
|
2009-07-01 23:47:36 +00:00
|
|
|
|
2019-12-23 20:26:03 -08:00
|
|
|
result = dns_ds_fromkeyrdata(keyname, &rdata, DNS_DSDIGEST_SHA256,
|
|
|
|
digest, &ds);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
goto finish;
|
2009-06-30 02:53:46 +00:00
|
|
|
}
|
|
|
|
|
2020-06-10 17:07:52 +10:00
|
|
|
result = delete_ds(keytable, node, &ds);
|
2019-12-11 00:09:15 -08:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
finish:
|
2009-06-30 02:53:46 +00:00
|
|
|
RWUNLOCK(&keytable->rwlock, isc_rwlocktype_write);
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2016-12-30 15:45:08 +11:00
|
|
|
dns_keytable_find(dns_keytable_t *keytable, const dns_name_t *keyname,
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_keynode_t **keynodep) {
|
|
|
|
isc_result_t result;
|
2009-06-30 02:53:46 +00:00
|
|
|
dns_rbtnode_t *node = NULL;
|
|
|
|
|
|
|
|
REQUIRE(VALID_KEYTABLE(keytable));
|
|
|
|
REQUIRE(keyname != NULL);
|
|
|
|
REQUIRE(keynodep != NULL && *keynodep == NULL);
|
2009-07-01 23:47:36 +00:00
|
|
|
|
2009-06-30 02:53:46 +00:00
|
|
|
RWLOCK(&keytable->rwlock, isc_rwlocktype_read);
|
|
|
|
result = dns_rbt_findnode(keytable->table, keyname, NULL, &node, NULL,
|
|
|
|
DNS_RBTFIND_NOOPTIONS, NULL, NULL);
|
|
|
|
if (result == ISC_R_SUCCESS) {
|
|
|
|
if (node->data != NULL) {
|
2019-12-23 20:26:03 -08:00
|
|
|
keynode_attach(node->data, keynodep);
|
2019-07-23 09:52:02 -04:00
|
|
|
} else {
|
2009-06-30 02:53:46 +00:00
|
|
|
result = ISC_R_NOTFOUND;
|
2019-07-23 09:52:02 -04:00
|
|
|
}
|
|
|
|
} else if (result == DNS_R_PARTIALMATCH) {
|
2009-06-30 02:53:46 +00:00
|
|
|
result = ISC_R_NOTFOUND;
|
2019-07-23 09:52:02 -04:00
|
|
|
}
|
2009-06-30 02:53:46 +00:00
|
|
|
RWUNLOCK(&keytable->rwlock, isc_rwlocktype_read);
|
2000-02-23 23:31:33 +00:00
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2000-04-18 17:47:17 +00:00
|
|
|
isc_result_t
|
2016-12-30 15:45:08 +11:00
|
|
|
dns_keytable_finddeepestmatch(dns_keytable_t *keytable, const dns_name_t *name,
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_name_t *foundname) {
|
2000-04-18 17:47:17 +00:00
|
|
|
isc_result_t result;
|
2020-02-13 14:44:37 -08:00
|
|
|
void *data;
|
2000-04-18 17:47:17 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Search for the deepest match in 'keytable'.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_KEYTABLE(keytable));
|
|
|
|
REQUIRE(dns_name_isabsolute(name));
|
|
|
|
REQUIRE(foundname != NULL);
|
|
|
|
|
|
|
|
RWLOCK(&keytable->rwlock, isc_rwlocktype_read);
|
|
|
|
|
|
|
|
data = NULL;
|
2000-04-19 18:27:24 +00:00
|
|
|
result = dns_rbt_findname(keytable->table, name, 0, foundname, &data);
|
2000-04-18 17:47:17 +00:00
|
|
|
|
2019-09-16 21:06:23 -07:00
|
|
|
if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
|
2000-04-18 17:47:17 +00:00
|
|
|
result = ISC_R_SUCCESS;
|
2019-09-16 21:06:23 -07:00
|
|
|
}
|
2000-04-18 17:47:17 +00:00
|
|
|
|
|
|
|
RWUNLOCK(&keytable->rwlock, isc_rwlocktype_read);
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2009-12-03 15:40:03 +00:00
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_keytable_detachkeynode(dns_keytable_t *keytable, dns_keynode_t **keynodep) {
|
2000-02-23 23:31:33 +00:00
|
|
|
/*
|
|
|
|
* Give back a keynode found via dns_keytable_findkeynode().
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_KEYTABLE(keytable));
|
|
|
|
REQUIRE(keynodep != NULL && VALID_KEYNODE(*keynodep));
|
|
|
|
|
2019-12-23 20:26:03 -08:00
|
|
|
keynode_detach(keytable->mctx, keynodep);
|
2000-02-23 23:31:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2016-12-30 15:45:08 +11:00
|
|
|
dns_keytable_issecuredomain(dns_keytable_t *keytable, const dns_name_t *name,
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_name_t *foundname, bool *wantdnssecp) {
|
|
|
|
isc_result_t result;
|
2014-05-29 22:22:53 -07:00
|
|
|
dns_rbtnode_t *node = NULL;
|
2000-02-23 23:31:33 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Is 'name' at or beneath a trusted key?
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_KEYTABLE(keytable));
|
|
|
|
REQUIRE(dns_name_isabsolute(name));
|
|
|
|
REQUIRE(wantdnssecp != NULL);
|
|
|
|
|
|
|
|
RWLOCK(&keytable->rwlock, isc_rwlocktype_read);
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
result = dns_rbt_findnode(keytable->table, name, foundname, &node, NULL,
|
|
|
|
DNS_RBTFIND_NOOPTIONS, NULL, NULL);
|
2000-02-23 23:31:33 +00:00
|
|
|
if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
|
2014-05-29 22:22:53 -07:00
|
|
|
INSIST(node->data != NULL);
|
2018-04-17 08:29:14 -07:00
|
|
|
*wantdnssecp = true;
|
2000-03-16 23:57:02 +00:00
|
|
|
result = ISC_R_SUCCESS;
|
2000-02-23 23:31:33 +00:00
|
|
|
} else if (result == ISC_R_NOTFOUND) {
|
2018-04-17 08:29:14 -07:00
|
|
|
*wantdnssecp = false;
|
2000-03-16 23:57:02 +00:00
|
|
|
result = ISC_R_SUCCESS;
|
2000-02-23 23:31:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RWUNLOCK(&keytable->rwlock, isc_rwlocktype_read);
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2015-02-05 17:18:15 -08:00
|
|
|
static isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
putstr(isc_buffer_t **b, const char *str) {
|
2015-02-05 17:18:15 -08:00
|
|
|
isc_result_t result;
|
|
|
|
|
2022-12-15 11:54:51 +01:00
|
|
|
result = isc_buffer_reserve(*b, strlen(str));
|
2019-09-16 21:06:23 -07:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2015-02-05 17:18:15 -08:00
|
|
|
return (result);
|
2019-09-16 21:06:23 -07:00
|
|
|
}
|
2015-02-06 23:45:21 +00:00
|
|
|
|
2015-02-05 17:18:15 -08:00
|
|
|
isc_buffer_putstr(*b, str);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2010-06-25 03:24:05 +00:00
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_keytable_dump(dns_keytable_t *keytable, FILE *fp) {
|
|
|
|
isc_result_t result;
|
2015-02-05 17:18:15 -08:00
|
|
|
isc_buffer_t *text = NULL;
|
|
|
|
|
|
|
|
REQUIRE(VALID_KEYTABLE(keytable));
|
|
|
|
REQUIRE(fp != NULL);
|
|
|
|
|
2020-02-02 08:35:46 +01:00
|
|
|
isc_buffer_allocate(keytable->mctx, &text, 4096);
|
2015-02-05 17:18:15 -08:00
|
|
|
|
|
|
|
result = dns_keytable_totext(keytable, &text);
|
|
|
|
|
|
|
|
if (isc_buffer_usedlength(text) != 0) {
|
2020-02-12 13:59:18 +01:00
|
|
|
(void)putstr(&text, "\n");
|
2019-09-16 21:06:23 -07:00
|
|
|
} else if (result == ISC_R_SUCCESS) {
|
2020-02-12 13:59:18 +01:00
|
|
|
(void)putstr(&text, "none");
|
2019-09-16 21:06:23 -07:00
|
|
|
} else {
|
2020-02-12 13:59:18 +01:00
|
|
|
(void)putstr(&text, "could not dump key table: ");
|
|
|
|
(void)putstr(&text, isc_result_totext(result));
|
2015-02-05 17:18:15 -08:00
|
|
|
}
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
fprintf(fp, "%.*s", (int)isc_buffer_usedlength(text),
|
|
|
|
(char *)isc_buffer_base(text));
|
2015-02-05 17:18:15 -08:00
|
|
|
|
|
|
|
isc_buffer_free(&text);
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2019-09-16 21:06:23 -07:00
|
|
|
static isc_result_t
|
|
|
|
keynode_dslist_totext(dns_name_t *name, dns_keynode_t *keynode,
|
2020-02-13 14:44:37 -08:00
|
|
|
isc_buffer_t **text) {
|
|
|
|
isc_result_t result;
|
|
|
|
char namebuf[DNS_NAME_FORMATSIZE];
|
|
|
|
char obuf[DNS_NAME_FORMATSIZE + 200];
|
2020-06-10 17:07:52 +10:00
|
|
|
dns_rdataset_t dsset;
|
2019-09-16 21:06:23 -07:00
|
|
|
|
|
|
|
dns_name_format(name, namebuf, sizeof(namebuf));
|
|
|
|
|
2020-06-10 17:07:52 +10:00
|
|
|
dns_rdataset_init(&dsset);
|
|
|
|
if (!dns_keynode_dsset(keynode, &dsset)) {
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
2019-09-16 21:06:23 -07:00
|
|
|
|
2020-06-10 17:07:52 +10:00
|
|
|
for (result = dns_rdataset_first(&dsset); result == ISC_R_SUCCESS;
|
|
|
|
result = dns_rdataset_next(&dsset))
|
2020-02-13 14:44:37 -08:00
|
|
|
{
|
|
|
|
char algbuf[DNS_SECALG_FORMATSIZE];
|
|
|
|
dns_rdata_t rdata = DNS_RDATA_INIT;
|
2019-09-16 21:06:23 -07:00
|
|
|
dns_rdata_ds_t ds;
|
|
|
|
|
2020-06-10 17:07:52 +10:00
|
|
|
dns_rdataset_current(&dsset, &rdata);
|
2019-09-16 21:06:23 -07:00
|
|
|
result = dns_rdata_tostruct(&rdata, &ds, NULL);
|
|
|
|
RUNTIME_CHECK(result == ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
dns_secalg_format(ds.algorithm, algbuf, sizeof(algbuf));
|
|
|
|
|
2020-10-01 10:39:49 +10:00
|
|
|
RWLOCK(&keynode->rwlock, isc_rwlocktype_read);
|
2020-02-12 13:59:18 +01:00
|
|
|
snprintf(obuf, sizeof(obuf), "%s/%s/%d ; %s%s\n", namebuf,
|
|
|
|
algbuf, ds.key_tag,
|
2019-09-16 21:06:23 -07:00
|
|
|
keynode->initial ? "initializing " : "",
|
|
|
|
keynode->managed ? "managed" : "static");
|
2020-10-01 10:39:49 +10:00
|
|
|
RWUNLOCK(&keynode->rwlock, isc_rwlocktype_read);
|
2019-09-16 21:06:23 -07:00
|
|
|
|
|
|
|
result = putstr(text, obuf);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
2020-06-10 17:07:52 +10:00
|
|
|
dns_rdataset_disassociate(&dsset);
|
2019-09-16 21:06:23 -07:00
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
}
|
2020-06-10 17:07:52 +10:00
|
|
|
dns_rdataset_disassociate(&dsset);
|
2019-09-16 21:06:23 -07:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2015-02-05 17:18:15 -08:00
|
|
|
isc_result_t
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_keytable_totext(dns_keytable_t *keytable, isc_buffer_t **text) {
|
|
|
|
isc_result_t result;
|
|
|
|
dns_keynode_t *knode;
|
|
|
|
dns_rbtnode_t *node;
|
2010-06-25 03:24:05 +00:00
|
|
|
dns_rbtnodechain_t chain;
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_name_t *foundname, *origin, *fullname;
|
|
|
|
dns_fixedname_t fixedfoundname, fixedorigin, fixedfullname;
|
2010-06-25 03:24:05 +00:00
|
|
|
|
|
|
|
REQUIRE(VALID_KEYTABLE(keytable));
|
2015-02-05 17:18:15 -08:00
|
|
|
REQUIRE(text != NULL && *text != NULL);
|
2010-06-25 03:24:05 +00:00
|
|
|
|
2019-09-16 21:06:23 -07:00
|
|
|
origin = dns_fixedname_initname(&fixedorigin);
|
|
|
|
fullname = dns_fixedname_initname(&fixedfullname);
|
|
|
|
foundname = dns_fixedname_initname(&fixedfoundname);
|
|
|
|
|
2010-06-25 03:24:05 +00:00
|
|
|
RWLOCK(&keytable->rwlock, isc_rwlocktype_read);
|
2019-08-27 22:41:18 -07:00
|
|
|
dns_rbtnodechain_init(&chain);
|
2010-06-25 03:24:05 +00:00
|
|
|
result = dns_rbtnodechain_first(&chain, keytable->table, NULL, NULL);
|
2015-02-05 17:18:15 -08:00
|
|
|
if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN) {
|
2019-09-16 21:06:23 -07:00
|
|
|
if (result == ISC_R_NOTFOUND) {
|
2015-02-05 17:18:15 -08:00
|
|
|
result = ISC_R_SUCCESS;
|
2019-09-16 21:06:23 -07:00
|
|
|
}
|
2010-06-25 03:24:05 +00:00
|
|
|
goto cleanup;
|
2015-02-05 17:18:15 -08:00
|
|
|
}
|
2010-06-25 03:24:05 +00:00
|
|
|
for (;;) {
|
2019-09-16 21:06:23 -07:00
|
|
|
dns_rbtnodechain_current(&chain, foundname, origin, &node);
|
|
|
|
|
|
|
|
knode = node->data;
|
|
|
|
if (knode != NULL && knode->dslist != NULL) {
|
|
|
|
result = dns_name_concatenate(foundname, origin,
|
|
|
|
fullname, NULL);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = keynode_dslist_totext(fullname, knode, text);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
2019-12-23 20:26:03 -08:00
|
|
|
goto cleanup;
|
2019-09-16 21:06:23 -07:00
|
|
|
}
|
2010-06-25 03:24:05 +00:00
|
|
|
}
|
2019-09-16 21:06:23 -07:00
|
|
|
|
2010-06-25 03:24:05 +00:00
|
|
|
result = dns_rbtnodechain_next(&chain, NULL, NULL);
|
|
|
|
if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN) {
|
2019-09-16 21:06:23 -07:00
|
|
|
if (result == ISC_R_NOMORE) {
|
2010-06-25 03:24:05 +00:00
|
|
|
result = ISC_R_SUCCESS;
|
2019-09-16 21:06:23 -07:00
|
|
|
}
|
2010-06-25 03:24:05 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
cleanup:
|
2010-06-25 03:24:05 +00:00
|
|
|
dns_rbtnodechain_invalidate(&chain);
|
|
|
|
RWUNLOCK(&keytable->rwlock, isc_rwlocktype_read);
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2016-07-22 20:02:17 +10:00
|
|
|
isc_result_t
|
|
|
|
dns_keytable_forall(dns_keytable_t *keytable,
|
use DS-style trust anchor to verify 5011 key refresh query
note: this also needs further refactoring.
- when initializing RFC 5011 for a name, we populate the managed-keys
zone with KEYDATA records derived from the initial-key trust anchors.
however, with initial-ds trust anchors, there is no key. but the
managed-keys zone still must have a KEYDATA record for the name,
otherwise zone_refreshkeys() won't refresh that key. so, for
initial-ds trust anchors, we now add an empty KEYDATA record and set
the key refresh timer so that the real keys will be looked up as soon
as possible.
- when a key refresh query is done, we verify it against the
trust anchor; this is done in two ways, one with the DS RRset
set up during configuration if present, or with the keys linked
from each keynode in the list if not. because there are two different
verification methods, the loop structure is overly complex and should
be simplified.
- the keyfetch_done() and sync_keyzone() functions are both too long
and should be broken into smaller functions.
2019-09-17 09:09:41 -07:00
|
|
|
void (*func)(dns_keytable_t *, dns_keynode_t *,
|
|
|
|
dns_name_t *, void *),
|
2020-02-13 14:44:37 -08:00
|
|
|
void *arg) {
|
|
|
|
isc_result_t result;
|
|
|
|
dns_rbtnode_t *node;
|
2016-07-22 20:02:17 +10:00
|
|
|
dns_rbtnodechain_t chain;
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_fixedname_t fixedfoundname, fixedorigin, fixedfullname;
|
|
|
|
dns_name_t *foundname, *origin, *fullname;
|
2016-07-22 20:02:17 +10:00
|
|
|
|
|
|
|
REQUIRE(VALID_KEYTABLE(keytable));
|
|
|
|
|
use DS-style trust anchor to verify 5011 key refresh query
note: this also needs further refactoring.
- when initializing RFC 5011 for a name, we populate the managed-keys
zone with KEYDATA records derived from the initial-key trust anchors.
however, with initial-ds trust anchors, there is no key. but the
managed-keys zone still must have a KEYDATA record for the name,
otherwise zone_refreshkeys() won't refresh that key. so, for
initial-ds trust anchors, we now add an empty KEYDATA record and set
the key refresh timer so that the real keys will be looked up as soon
as possible.
- when a key refresh query is done, we verify it against the
trust anchor; this is done in two ways, one with the DS RRset
set up during configuration if present, or with the keys linked
from each keynode in the list if not. because there are two different
verification methods, the loop structure is overly complex and should
be simplified.
- the keyfetch_done() and sync_keyzone() functions are both too long
and should be broken into smaller functions.
2019-09-17 09:09:41 -07:00
|
|
|
origin = dns_fixedname_initname(&fixedorigin);
|
|
|
|
fullname = dns_fixedname_initname(&fixedfullname);
|
|
|
|
foundname = dns_fixedname_initname(&fixedfoundname);
|
|
|
|
|
2016-07-22 20:02:17 +10:00
|
|
|
RWLOCK(&keytable->rwlock, isc_rwlocktype_read);
|
2019-08-27 22:41:18 -07:00
|
|
|
dns_rbtnodechain_init(&chain);
|
2016-07-22 20:02:17 +10:00
|
|
|
result = dns_rbtnodechain_first(&chain, keytable->table, NULL, NULL);
|
|
|
|
if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN) {
|
2019-09-16 21:06:23 -07:00
|
|
|
if (result == ISC_R_NOTFOUND) {
|
2016-07-22 20:02:17 +10:00
|
|
|
result = ISC_R_SUCCESS;
|
2019-09-16 21:06:23 -07:00
|
|
|
}
|
2016-07-22 20:02:17 +10:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2019-12-23 20:26:03 -08:00
|
|
|
|
2016-07-22 20:02:17 +10:00
|
|
|
for (;;) {
|
use DS-style trust anchor to verify 5011 key refresh query
note: this also needs further refactoring.
- when initializing RFC 5011 for a name, we populate the managed-keys
zone with KEYDATA records derived from the initial-key trust anchors.
however, with initial-ds trust anchors, there is no key. but the
managed-keys zone still must have a KEYDATA record for the name,
otherwise zone_refreshkeys() won't refresh that key. so, for
initial-ds trust anchors, we now add an empty KEYDATA record and set
the key refresh timer so that the real keys will be looked up as soon
as possible.
- when a key refresh query is done, we verify it against the
trust anchor; this is done in two ways, one with the DS RRset
set up during configuration if present, or with the keys linked
from each keynode in the list if not. because there are two different
verification methods, the loop structure is overly complex and should
be simplified.
- the keyfetch_done() and sync_keyzone() functions are both too long
and should be broken into smaller functions.
2019-09-17 09:09:41 -07:00
|
|
|
dns_rbtnodechain_current(&chain, foundname, origin, &node);
|
2019-09-16 21:06:23 -07:00
|
|
|
if (node->data != NULL) {
|
use DS-style trust anchor to verify 5011 key refresh query
note: this also needs further refactoring.
- when initializing RFC 5011 for a name, we populate the managed-keys
zone with KEYDATA records derived from the initial-key trust anchors.
however, with initial-ds trust anchors, there is no key. but the
managed-keys zone still must have a KEYDATA record for the name,
otherwise zone_refreshkeys() won't refresh that key. so, for
initial-ds trust anchors, we now add an empty KEYDATA record and set
the key refresh timer so that the real keys will be looked up as soon
as possible.
- when a key refresh query is done, we verify it against the
trust anchor; this is done in two ways, one with the DS RRset
set up during configuration if present, or with the keys linked
from each keynode in the list if not. because there are two different
verification methods, the loop structure is overly complex and should
be simplified.
- the keyfetch_done() and sync_keyzone() functions are both too long
and should be broken into smaller functions.
2019-09-17 09:09:41 -07:00
|
|
|
result = dns_name_concatenate(foundname, origin,
|
|
|
|
fullname, NULL);
|
|
|
|
RUNTIME_CHECK(result == ISC_R_SUCCESS);
|
|
|
|
(*func)(keytable, node->data, fullname, arg);
|
2019-09-16 21:06:23 -07:00
|
|
|
}
|
2016-07-22 20:02:17 +10:00
|
|
|
result = dns_rbtnodechain_next(&chain, NULL, NULL);
|
|
|
|
if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN) {
|
2019-09-16 21:06:23 -07:00
|
|
|
if (result == ISC_R_NOMORE) {
|
2016-07-22 20:02:17 +10:00
|
|
|
result = ISC_R_SUCCESS;
|
2019-09-16 21:06:23 -07:00
|
|
|
}
|
2016-07-22 20:02:17 +10:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
cleanup:
|
2016-07-22 20:02:17 +10:00
|
|
|
dns_rbtnodechain_invalidate(&chain);
|
|
|
|
RWUNLOCK(&keytable->rwlock, isc_rwlocktype_read);
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2020-06-10 17:07:52 +10:00
|
|
|
bool
|
|
|
|
dns_keynode_dsset(dns_keynode_t *keynode, dns_rdataset_t *rdataset) {
|
2020-10-01 10:39:49 +10:00
|
|
|
bool result;
|
2019-09-16 21:06:23 -07:00
|
|
|
REQUIRE(VALID_KEYNODE(keynode));
|
2020-06-10 17:07:52 +10:00
|
|
|
REQUIRE(rdataset == NULL || DNS_RDATASET_VALID(rdataset));
|
2019-09-16 21:06:23 -07:00
|
|
|
|
2020-10-01 10:39:49 +10:00
|
|
|
RWLOCK(&keynode->rwlock, isc_rwlocktype_read);
|
2019-09-16 21:06:23 -07:00
|
|
|
if (keynode->dslist != NULL) {
|
2020-06-10 17:07:52 +10:00
|
|
|
if (rdataset != NULL) {
|
2023-01-05 09:12:35 +01:00
|
|
|
keynode_clone(&keynode->dsset,
|
|
|
|
rdataset DNS__DB_FILELINE);
|
2020-06-10 17:07:52 +10:00
|
|
|
}
|
2020-10-01 10:39:49 +10:00
|
|
|
result = true;
|
|
|
|
} else {
|
|
|
|
result = false;
|
2019-09-16 21:06:23 -07:00
|
|
|
}
|
2020-10-01 10:39:49 +10:00
|
|
|
RWUNLOCK(&keynode->rwlock, isc_rwlocktype_read);
|
|
|
|
return (result);
|
2019-09-16 21:06:23 -07:00
|
|
|
}
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
bool
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_keynode_managed(dns_keynode_t *keynode) {
|
2020-10-01 10:39:49 +10:00
|
|
|
bool managed;
|
|
|
|
|
2009-06-30 02:53:46 +00:00
|
|
|
REQUIRE(VALID_KEYNODE(keynode));
|
|
|
|
|
2020-10-01 10:39:49 +10:00
|
|
|
RWLOCK(&keynode->rwlock, isc_rwlocktype_read);
|
|
|
|
managed = keynode->managed;
|
|
|
|
RWUNLOCK(&keynode->rwlock, isc_rwlocktype_read);
|
|
|
|
|
|
|
|
return (managed);
|
2009-06-30 02:53:46 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
bool
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_keynode_initial(dns_keynode_t *keynode) {
|
2020-10-01 10:39:49 +10:00
|
|
|
bool initial;
|
|
|
|
|
2017-10-27 15:45:18 -07:00
|
|
|
REQUIRE(VALID_KEYNODE(keynode));
|
|
|
|
|
2020-10-01 10:39:49 +10:00
|
|
|
RWLOCK(&keynode->rwlock, isc_rwlocktype_read);
|
|
|
|
initial = keynode->initial;
|
|
|
|
RWUNLOCK(&keynode->rwlock, isc_rwlocktype_read);
|
|
|
|
|
|
|
|
return (initial);
|
2017-10-27 15:45:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_keynode_trust(dns_keynode_t *keynode) {
|
2017-10-27 15:45:18 -07:00
|
|
|
REQUIRE(VALID_KEYNODE(keynode));
|
|
|
|
|
2020-10-01 10:39:49 +10:00
|
|
|
RWLOCK(&keynode->rwlock, isc_rwlocktype_write);
|
2018-04-17 08:29:14 -07:00
|
|
|
keynode->initial = false;
|
2020-10-01 10:39:49 +10:00
|
|
|
RWUNLOCK(&keynode->rwlock, isc_rwlocktype_write);
|
2017-10-27 15:45:18 -07:00
|
|
|
}
|
2020-06-10 17:07:52 +10:00
|
|
|
|
|
|
|
static void
|
2023-01-05 09:12:35 +01:00
|
|
|
keynode_disassociate(dns_rdataset_t *rdataset DNS__DB_FLARG) {
|
2020-06-10 17:07:52 +10:00
|
|
|
dns_keynode_t *keynode;
|
|
|
|
|
|
|
|
REQUIRE(rdataset != NULL);
|
|
|
|
REQUIRE(rdataset->methods == &methods);
|
|
|
|
|
|
|
|
rdataset->methods = NULL;
|
|
|
|
keynode = rdataset->private1;
|
|
|
|
rdataset->private1 = NULL;
|
|
|
|
|
|
|
|
keynode_detach(keynode->mctx, &keynode);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
keynode_first(dns_rdataset_t *rdataset) {
|
|
|
|
dns_keynode_t *keynode;
|
|
|
|
|
|
|
|
REQUIRE(rdataset != NULL);
|
|
|
|
REQUIRE(rdataset->methods == &methods);
|
|
|
|
|
|
|
|
keynode = rdataset->private1;
|
|
|
|
RWLOCK(&keynode->rwlock, isc_rwlocktype_read);
|
|
|
|
rdataset->private2 = ISC_LIST_HEAD(keynode->dslist->rdata);
|
|
|
|
RWUNLOCK(&keynode->rwlock, isc_rwlocktype_read);
|
|
|
|
|
|
|
|
if (rdataset->private2 == NULL) {
|
|
|
|
return (ISC_R_NOMORE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
keynode_next(dns_rdataset_t *rdataset) {
|
|
|
|
dns_keynode_t *keynode;
|
|
|
|
dns_rdata_t *rdata;
|
|
|
|
|
|
|
|
REQUIRE(rdataset != NULL);
|
|
|
|
REQUIRE(rdataset->methods == &methods);
|
|
|
|
|
|
|
|
rdata = rdataset->private2;
|
|
|
|
if (rdata == NULL) {
|
|
|
|
return (ISC_R_NOMORE);
|
|
|
|
}
|
|
|
|
|
|
|
|
keynode = rdataset->private1;
|
|
|
|
RWLOCK(&keynode->rwlock, isc_rwlocktype_read);
|
|
|
|
rdataset->private2 = ISC_LIST_NEXT(rdata, link);
|
|
|
|
RWUNLOCK(&keynode->rwlock, isc_rwlocktype_read);
|
|
|
|
|
|
|
|
if (rdataset->private2 == NULL) {
|
|
|
|
return (ISC_R_NOMORE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
keynode_current(dns_rdataset_t *rdataset, dns_rdata_t *rdata) {
|
|
|
|
dns_rdata_t *list_rdata;
|
|
|
|
|
|
|
|
REQUIRE(rdataset != NULL);
|
|
|
|
REQUIRE(rdataset->methods == &methods);
|
|
|
|
|
|
|
|
list_rdata = rdataset->private2;
|
|
|
|
INSIST(list_rdata != NULL);
|
|
|
|
|
|
|
|
dns_rdata_clone(list_rdata, rdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-01-05 09:12:35 +01:00
|
|
|
keynode_clone(dns_rdataset_t *source, dns_rdataset_t *target DNS__DB_FLARG) {
|
2020-06-10 17:07:52 +10:00
|
|
|
dns_keynode_t *keynode;
|
|
|
|
|
|
|
|
REQUIRE(source != NULL);
|
|
|
|
REQUIRE(target != NULL);
|
|
|
|
REQUIRE(source->methods == &methods);
|
|
|
|
|
|
|
|
keynode = source->private1;
|
|
|
|
isc_refcount_increment(&keynode->refcount);
|
|
|
|
|
|
|
|
*target = *source;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reset iterator state.
|
|
|
|
*/
|
|
|
|
target->private2 = NULL;
|
|
|
|
}
|