2000-08-24 22:15:40 +00:00
|
|
|
/*
|
2009-09-02 23:48:03 +00:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2000-08-24 22:15:40 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
2021-06-03 08:37:05 +02:00
|
|
|
*
|
2000-08-24 22:15:40 +00: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
|
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
2000-08-24 22:15:40 +00:00
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
|
|
|
*/
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*! \file */
|
2000-08-24 22:15:40 +00:00
|
|
|
|
|
|
|
#include <isc/magic.h>
|
|
|
|
#include <isc/mem.h>
|
2021-10-04 17:14:53 +02:00
|
|
|
#include <isc/result.h>
|
2000-08-24 22:15:40 +00:00
|
|
|
#include <isc/util.h>
|
|
|
|
|
2023-04-05 23:08:12 -07:00
|
|
|
#include <dns/fixedname.h>
|
2000-08-24 22:15:40 +00:00
|
|
|
#include <dns/forward.h>
|
2022-12-08 14:17:24 +00:00
|
|
|
#include <dns/name.h>
|
2023-04-05 23:08:12 -07:00
|
|
|
#include <dns/qp.h>
|
2000-08-24 22:15:40 +00:00
|
|
|
#include <dns/types.h>
|
2023-04-05 23:08:12 -07:00
|
|
|
#include <dns/view.h>
|
2000-08-24 22:15:40 +00:00
|
|
|
|
|
|
|
struct dns_fwdtable {
|
|
|
|
/* Unlocked. */
|
2000-12-11 19:24:30 +00:00
|
|
|
unsigned int magic;
|
2000-08-24 22:15:40 +00:00
|
|
|
isc_mem_t *mctx;
|
2023-04-05 23:08:12 -07:00
|
|
|
dns_qpmulti_t *table;
|
2000-08-24 22:15:40 +00:00
|
|
|
};
|
|
|
|
|
2001-06-04 19:33:39 +00:00
|
|
|
#define FWDTABLEMAGIC ISC_MAGIC('F', 'w', 'd', 'T')
|
2000-08-24 22:15:40 +00:00
|
|
|
#define VALID_FWDTABLE(ft) ISC_MAGIC_VALID(ft, FWDTABLEMAGIC)
|
|
|
|
|
|
|
|
static void
|
2023-04-05 23:08:12 -07:00
|
|
|
qp_attach(void *uctx, void *pval, uint32_t ival);
|
|
|
|
static void
|
|
|
|
qp_detach(void *uctx, void *pval, uint32_t ival);
|
|
|
|
static size_t
|
|
|
|
qp_makekey(dns_qpkey_t key, void *uctx, void *pval, uint32_t ival);
|
|
|
|
static void
|
|
|
|
qp_triename(void *uctx, char *buf, size_t size);
|
2000-08-24 22:15:40 +00:00
|
|
|
|
2023-04-05 23:08:12 -07:00
|
|
|
static dns_qpmethods_t qpmethods = {
|
|
|
|
qp_attach,
|
|
|
|
qp_detach,
|
|
|
|
qp_makekey,
|
|
|
|
qp_triename,
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
dns_fwdtable_create(isc_mem_t *mctx, dns_view_t *view,
|
|
|
|
dns_fwdtable_t **fwdtablep) {
|
|
|
|
dns_fwdtable_t *fwdtable = NULL;
|
2000-08-24 22:15:40 +00:00
|
|
|
|
|
|
|
REQUIRE(fwdtablep != NULL && *fwdtablep == NULL);
|
|
|
|
|
2022-12-12 09:20:48 +00:00
|
|
|
fwdtable = isc_mem_get(mctx, sizeof(*fwdtable));
|
2023-04-05 23:08:12 -07:00
|
|
|
*fwdtable = (dns_fwdtable_t){ .magic = FWDTABLEMAGIC };
|
2000-08-24 22:15:40 +00:00
|
|
|
|
2023-04-05 23:08:12 -07:00
|
|
|
dns_qpmulti_create(mctx, &qpmethods, view, &fwdtable->table);
|
2000-08-24 22:15:40 +00:00
|
|
|
|
|
|
|
isc_mem_attach(mctx, &fwdtable->mctx);
|
|
|
|
*fwdtablep = fwdtable;
|
2023-04-05 23:08:12 -07:00
|
|
|
}
|
2000-08-24 22:15:40 +00:00
|
|
|
|
2023-04-05 23:08:12 -07:00
|
|
|
static dns_forwarders_t *
|
|
|
|
new_forwarders(isc_mem_t *mctx, const dns_name_t *name,
|
|
|
|
dns_fwdpolicy_t fwdpolicy) {
|
|
|
|
dns_forwarders_t *forwarders = NULL;
|
2000-08-24 22:15:40 +00:00
|
|
|
|
2023-04-05 23:08:12 -07:00
|
|
|
forwarders = isc_mem_get(mctx, sizeof(*forwarders));
|
|
|
|
*forwarders = (dns_forwarders_t){
|
|
|
|
.fwdpolicy = fwdpolicy,
|
2024-03-13 13:42:57 -07:00
|
|
|
.name = DNS_NAME_INITEMPTY,
|
2023-04-05 23:08:12 -07:00
|
|
|
.fwdrs = ISC_LIST_INITIALIZER,
|
|
|
|
};
|
|
|
|
isc_mem_attach(mctx, &forwarders->mctx);
|
|
|
|
isc_refcount_init(&forwarders->references, 1);
|
2000-08-24 22:15:40 +00:00
|
|
|
|
2025-02-21 12:09:28 +01:00
|
|
|
dns_name_dup(name, mctx, &forwarders->name);
|
2023-04-05 23:08:12 -07:00
|
|
|
|
|
|
|
return forwarders;
|
2000-08-24 22:15:40 +00:00
|
|
|
}
|
|
|
|
|
2013-03-22 12:27:54 -07:00
|
|
|
isc_result_t
|
2016-12-30 15:45:08 +11:00
|
|
|
dns_fwdtable_addfwd(dns_fwdtable_t *fwdtable, const dns_name_t *name,
|
2013-03-22 12:27:54 -07:00
|
|
|
dns_forwarderlist_t *fwdrs, dns_fwdpolicy_t fwdpolicy) {
|
|
|
|
isc_result_t result;
|
2023-04-05 23:08:12 -07:00
|
|
|
dns_forwarders_t *forwarders = NULL;
|
|
|
|
dns_qp_t *qp = NULL;
|
2013-03-22 12:27:54 -07:00
|
|
|
|
|
|
|
REQUIRE(VALID_FWDTABLE(fwdtable));
|
|
|
|
|
2023-04-05 23:08:12 -07:00
|
|
|
forwarders = new_forwarders(fwdtable->mctx, name, fwdpolicy);
|
2013-03-22 12:27:54 -07:00
|
|
|
|
2025-03-20 22:25:56 -07:00
|
|
|
ISC_LIST_FOREACH (*fwdrs, fwd, link) {
|
|
|
|
dns_forwarder_t *nfwd = isc_mem_get(fwdtable->mctx,
|
|
|
|
sizeof(*nfwd));
|
2013-03-22 12:27:54 -07:00
|
|
|
*nfwd = *fwd;
|
2022-12-08 14:17:24 +00:00
|
|
|
|
|
|
|
if (fwd->tlsname != NULL) {
|
|
|
|
nfwd->tlsname = isc_mem_get(fwdtable->mctx,
|
|
|
|
sizeof(*nfwd->tlsname));
|
2025-02-21 12:09:39 +01:00
|
|
|
dns_name_init(nfwd->tlsname);
|
2022-12-08 14:17:24 +00:00
|
|
|
dns_name_dup(fwd->tlsname, fwdtable->mctx,
|
|
|
|
nfwd->tlsname);
|
|
|
|
}
|
|
|
|
|
2013-03-22 12:27:54 -07:00
|
|
|
ISC_LINK_INIT(nfwd, link);
|
|
|
|
ISC_LIST_APPEND(forwarders->fwdrs, nfwd, link);
|
|
|
|
}
|
|
|
|
|
2023-04-05 23:08:12 -07:00
|
|
|
dns_qpmulti_write(fwdtable->table, &qp);
|
|
|
|
result = dns_qp_insert(qp, forwarders, 0);
|
|
|
|
dns_qp_compact(qp, DNS_QPGC_MAYBE);
|
|
|
|
dns_qpmulti_commit(fwdtable->table, &qp);
|
2013-03-22 12:27:54 -07:00
|
|
|
|
2023-08-17 12:39:10 +02:00
|
|
|
dns_forwarders_detach(&forwarders);
|
2013-03-22 12:27:54 -07:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2000-08-24 22:15:40 +00:00
|
|
|
isc_result_t
|
2016-12-30 15:45:08 +11:00
|
|
|
dns_fwdtable_add(dns_fwdtable_t *fwdtable, const dns_name_t *name,
|
2000-08-24 22:15:40 +00:00
|
|
|
isc_sockaddrlist_t *addrs, dns_fwdpolicy_t fwdpolicy) {
|
|
|
|
isc_result_t result;
|
2023-04-05 23:08:12 -07:00
|
|
|
dns_forwarders_t *forwarders = NULL;
|
|
|
|
dns_qp_t *qp = NULL;
|
2000-08-24 22:15:40 +00:00
|
|
|
|
|
|
|
REQUIRE(VALID_FWDTABLE(fwdtable));
|
|
|
|
|
2023-04-05 23:08:12 -07:00
|
|
|
forwarders = new_forwarders(fwdtable->mctx, name, fwdpolicy);
|
2000-08-24 22:15:40 +00:00
|
|
|
|
2025-03-20 22:25:56 -07:00
|
|
|
ISC_LIST_FOREACH (*addrs, sa, link) {
|
|
|
|
dns_forwarder_t *fwd = isc_mem_get(fwdtable->mctx,
|
|
|
|
sizeof(*fwd));
|
2022-12-08 14:17:24 +00:00
|
|
|
*fwd = (dns_forwarder_t){ .addr = *sa,
|
|
|
|
.link = ISC_LINK_INITIALIZER };
|
2013-03-22 12:27:54 -07:00
|
|
|
ISC_LIST_APPEND(forwarders->fwdrs, fwd, link);
|
2000-08-24 22:15:40 +00:00
|
|
|
}
|
|
|
|
|
2023-04-05 23:08:12 -07:00
|
|
|
dns_qpmulti_write(fwdtable->table, &qp);
|
|
|
|
result = dns_qp_insert(qp, forwarders, 0);
|
|
|
|
dns_qp_compact(qp, DNS_QPGC_MAYBE);
|
|
|
|
dns_qpmulti_commit(fwdtable->table, &qp);
|
2000-08-24 22:15:40 +00:00
|
|
|
|
2023-08-17 12:39:10 +02:00
|
|
|
dns_forwarders_detach(&forwarders);
|
2000-08-24 22:15:40 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2016-12-30 15:45:08 +11:00
|
|
|
dns_fwdtable_find(dns_fwdtable_t *fwdtable, const dns_name_t *name,
|
2023-04-05 23:08:12 -07:00
|
|
|
dns_forwarders_t **forwardersp) {
|
2000-08-24 22:15:40 +00:00
|
|
|
isc_result_t result;
|
2023-04-05 23:08:12 -07:00
|
|
|
dns_qpread_t qpr;
|
|
|
|
void *pval = NULL;
|
2000-08-24 22:15:40 +00:00
|
|
|
|
|
|
|
REQUIRE(VALID_FWDTABLE(fwdtable));
|
|
|
|
|
2023-04-05 23:08:12 -07:00
|
|
|
dns_qpmulti_query(fwdtable->table, &qpr);
|
2025-05-27 10:57:11 +02:00
|
|
|
result = dns_qp_lookup(&qpr, name, 0, NULL, NULL, NULL, &pval, NULL);
|
2023-04-05 23:08:12 -07:00
|
|
|
if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
|
|
|
|
dns_forwarders_t *fwdrs = pval;
|
|
|
|
*forwardersp = fwdrs;
|
|
|
|
dns_forwarders_ref(fwdrs);
|
|
|
|
}
|
|
|
|
dns_qpread_destroy(fwdtable->table, &qpr);
|
2000-08-24 22:15:40 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dns_fwdtable_destroy(dns_fwdtable_t **fwdtablep) {
|
2023-04-05 23:08:12 -07:00
|
|
|
dns_fwdtable_t *fwdtable = NULL;
|
2000-08-24 22:15:40 +00:00
|
|
|
|
|
|
|
REQUIRE(fwdtablep != NULL && VALID_FWDTABLE(*fwdtablep));
|
|
|
|
|
|
|
|
fwdtable = *fwdtablep;
|
2020-02-08 04:37:54 -08:00
|
|
|
*fwdtablep = NULL;
|
2000-08-24 22:15:40 +00:00
|
|
|
|
2023-04-05 23:08:12 -07:00
|
|
|
dns_qpmulti_destroy(&fwdtable->table);
|
2000-08-24 22:15:40 +00:00
|
|
|
fwdtable->magic = 0;
|
2019-07-23 17:16:57 -04:00
|
|
|
|
2022-12-12 09:20:48 +00:00
|
|
|
isc_mem_putanddetach(&fwdtable->mctx, fwdtable, sizeof(*fwdtable));
|
2000-08-24 22:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
*** Private
|
|
|
|
***/
|
|
|
|
|
|
|
|
static void
|
2023-04-05 23:08:12 -07:00
|
|
|
destroy_forwarders(dns_forwarders_t *forwarders) {
|
2025-05-23 13:02:22 -07:00
|
|
|
ISC_LIST_FOREACH (forwarders->fwdrs, fwd, link) {
|
2013-03-22 12:27:54 -07:00
|
|
|
ISC_LIST_UNLINK(forwarders->fwdrs, fwd, link);
|
2022-12-08 14:17:24 +00:00
|
|
|
if (fwd->tlsname != NULL) {
|
2023-04-05 23:08:12 -07:00
|
|
|
dns_name_free(fwd->tlsname, forwarders->mctx);
|
|
|
|
isc_mem_put(forwarders->mctx, fwd->tlsname,
|
2022-12-08 14:17:24 +00:00
|
|
|
sizeof(*fwd->tlsname));
|
|
|
|
}
|
2023-04-05 23:08:12 -07:00
|
|
|
isc_mem_put(forwarders->mctx, fwd, sizeof(*fwd));
|
2000-08-24 22:15:40 +00:00
|
|
|
}
|
2024-03-13 13:42:57 -07:00
|
|
|
dns_name_free(&forwarders->name, forwarders->mctx);
|
2023-04-05 23:08:12 -07:00
|
|
|
isc_mem_putanddetach(&forwarders->mctx, forwarders,
|
|
|
|
sizeof(*forwarders));
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DNS_FORWARD_TRACE
|
|
|
|
ISC_REFCOUNT_TRACE_IMPL(dns_forwarders, destroy_forwarders);
|
|
|
|
#else
|
|
|
|
ISC_REFCOUNT_IMPL(dns_forwarders, destroy_forwarders);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void
|
|
|
|
qp_attach(void *uctx ISC_ATTR_UNUSED, void *pval,
|
|
|
|
uint32_t ival ISC_ATTR_UNUSED) {
|
|
|
|
dns_forwarders_t *forwarders = pval;
|
|
|
|
dns_forwarders_ref(forwarders);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
qp_detach(void *uctx ISC_ATTR_UNUSED, void *pval,
|
|
|
|
uint32_t ival ISC_ATTR_UNUSED) {
|
|
|
|
dns_forwarders_t *forwarders = pval;
|
|
|
|
dns_forwarders_detach(&forwarders);
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
qp_makekey(dns_qpkey_t key, void *uctx ISC_ATTR_UNUSED, void *pval,
|
|
|
|
uint32_t ival ISC_ATTR_UNUSED) {
|
|
|
|
dns_forwarders_t *fwd = pval;
|
Prepend qpkey with denial byte
In preparation to merge the three qp tries (tree, nsec, nsec3) into
one, add the piece of information into the qpkey. This is the most
significant bit of information, so prepend the denial type to the qpkey.
This means we need to pass on the denial type when constructing the
qpkey from a name, or doing a lookup.
Reuse the the DNS_DB_NSEC_* values. Most qp tries in the code we just
pass on 0 (nta, rpz, zt, etc.), because there is no need for denial of
existence, but for qpzone and qpcache we must pass the right value.
Change the code, so that node->nsec no longer can have the value
DNS_DB_NSEC_HAS_NSEC, instead track this in a new attribute 'havensec'.
Since we use node->nsec to convert names to keys, the value MUST be set
before inserting the node into the qp-trie.
Update the fuzzing and unit tests accordingly. This only adds a few
extra test cases, more are needed.
In the qp_test.c we can remove test code for empty keys as this is
no longer possible.
2025-04-25 17:21:16 +02:00
|
|
|
return dns_qpkey_fromname(key, &fwd->name, 0);
|
2023-04-05 23:08:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
qp_triename(void *uctx, char *buf, size_t size) {
|
|
|
|
dns_view_t *view = uctx;
|
|
|
|
snprintf(buf, size, "view %s forwarder table", view->name);
|
2000-08-24 22:15:40 +00:00
|
|
|
}
|