1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
|
/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* SPDX-License-Identifier: MPL-2.0
*
* 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/.
*
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
*/
/*! \file */
#include <inttypes.h>
#include <stdbool.h>
#include <isc/async.h>
#include <isc/buffer.h>
#include <isc/log.h>
#include <isc/loop.h>
#include <isc/mem.h>
#include <isc/result.h>
#include <isc/rwlock.h>
#include <isc/string.h>
#include <isc/time.h>
#include <isc/timer.h>
#include <isc/util.h>
#include <dns/db.h>
#include <dns/fixedname.h>
#include <dns/log.h>
#include <dns/name.h>
#include <dns/nta.h>
#include <dns/qp.h>
#include <dns/rdataset.h>
#include <dns/resolver.h>
#include <dns/time.h>
struct dns_ntatable {
unsigned int magic;
isc_mem_t *mctx;
dns_view_t *view;
isc_rwlock_t rwlock;
isc_loopmgr_t *loopmgr;
isc_refcount_t references;
dns_qpmulti_t *table;
atomic_bool shuttingdown;
};
struct dns__nta {
unsigned int magic;
isc_mem_t *mctx;
isc_loop_t *loop;
isc_refcount_t references;
dns_ntatable_t *ntatable;
bool forced;
isc_timer_t *timer;
dns_fetch_t *fetch;
dns_rdataset_t rdataset;
dns_rdataset_t sigrdataset;
dns_name_t name;
isc_stdtime_t expiry;
bool shuttingdown;
};
#define NTA_MAGIC ISC_MAGIC('N', 'T', 'A', 'n')
#define VALID_NTA(nn) ISC_MAGIC_VALID(nn, NTA_MAGIC)
static void
dns__nta_shutdown(dns__nta_t *nta);
static void
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);
static dns_qpmethods_t qpmethods = {
qp_attach,
qp_detach,
qp_makekey,
qp_triename,
};
static void
dns__nta_destroy(dns__nta_t *nta) {
REQUIRE(nta->timer == NULL);
nta->magic = 0;
if (dns_rdataset_isassociated(&nta->rdataset)) {
dns_rdataset_disassociate(&nta->rdataset);
}
if (dns_rdataset_isassociated(&nta->sigrdataset)) {
dns_rdataset_disassociate(&nta->sigrdataset);
}
if (nta->fetch != NULL) {
dns_resolver_cancelfetch(nta->fetch);
dns_resolver_destroyfetch(&nta->fetch);
}
isc_loop_detach(&nta->loop);
dns_name_free(&nta->name, nta->mctx);
isc_mem_putanddetach(&nta->mctx, nta, sizeof(*nta));
}
#if DNS_NTA_TRACE
ISC_REFCOUNT_TRACE_IMPL(dns__nta, dns__nta_destroy);
#else
ISC_REFCOUNT_IMPL(dns__nta, dns__nta_destroy);
#endif
void
dns_ntatable_create(dns_view_t *view, isc_loopmgr_t *loopmgr,
dns_ntatable_t **ntatablep) {
dns_ntatable_t *ntatable = NULL;
REQUIRE(ntatablep != NULL && *ntatablep == NULL);
ntatable = isc_mem_get(view->mctx, sizeof(*ntatable));
*ntatable = (dns_ntatable_t){
.loopmgr = loopmgr,
};
isc_mem_attach(view->mctx, &ntatable->mctx);
dns_view_weakattach(view, &ntatable->view);
isc_rwlock_init(&ntatable->rwlock);
dns_qpmulti_create(view->mctx, &qpmethods, view, &ntatable->table);
isc_refcount_init(&ntatable->references, 1);
ntatable->magic = NTATABLE_MAGIC;
*ntatablep = ntatable;
}
static void
dns__ntatable_destroy(dns_ntatable_t *ntatable) {
ntatable->magic = 0;
isc_rwlock_destroy(&ntatable->rwlock);
dns_qpmulti_destroy(&ntatable->table);
INSIST(ntatable->view == NULL);
isc_mem_putanddetach(&ntatable->mctx, ntatable, sizeof(*ntatable));
}
#if DNS_NTA_TRACE
ISC_REFCOUNT_TRACE_IMPL(dns_ntatable, dns__ntatable_destroy);
#else
ISC_REFCOUNT_IMPL(dns_ntatable, dns__ntatable_destroy);
#endif
static void
fetch_done(void *arg) {
dns_fetchresponse_t *resp = (dns_fetchresponse_t *)arg;
dns__nta_t *nta = resp->arg;
isc_result_t eresult = resp->result;
dns_ntatable_t *ntatable = nta->ntatable;
dns_view_t *view = ntatable->view;
isc_stdtime_t now = isc_stdtime_now();
if (dns_rdataset_isassociated(&nta->rdataset)) {
dns_rdataset_disassociate(&nta->rdataset);
}
if (dns_rdataset_isassociated(&nta->sigrdataset)) {
dns_rdataset_disassociate(&nta->sigrdataset);
}
if (nta->fetch == resp->fetch) {
nta->fetch = NULL;
}
dns_resolver_destroyfetch(&resp->fetch);
if (resp->node != NULL) {
dns_db_detachnode(resp->db, &resp->node);
}
if (resp->db != NULL) {
dns_db_detach(&resp->db);
}
isc_mem_putanddetach(&resp->mctx, resp, sizeof(*resp));
switch (eresult) {
case ISC_R_SUCCESS:
case DNS_R_NCACHENXDOMAIN:
case DNS_R_NXDOMAIN:
case DNS_R_NCACHENXRRSET:
case DNS_R_NXRRSET:
RWLOCK(&ntatable->rwlock, isc_rwlocktype_write);
if (nta->expiry > now) {
nta->expiry = now;
}
RWUNLOCK(&ntatable->rwlock, isc_rwlocktype_write);
break;
default:
break;
}
/*
* If we're expiring before the next recheck, we might
* as well stop the timer now.
*/
RWLOCK(&ntatable->rwlock, isc_rwlocktype_read);
if (nta->timer != NULL && nta->expiry - now < view->nta_recheck) {
isc_timer_stop(nta->timer);
}
RWUNLOCK(&ntatable->rwlock, isc_rwlocktype_read);
dns__nta_detach(&nta); /* for dns_resolver_createfetch() */
}
static void
checkbogus(void *arg) {
dns__nta_t *nta = arg;
dns_ntatable_t *ntatable = nta->ntatable;
dns_resolver_t *resolver = NULL;
isc_result_t result;
if (nta->fetch != NULL) {
dns_resolver_cancelfetch(nta->fetch);
nta->fetch = NULL;
}
if (dns_rdataset_isassociated(&nta->rdataset)) {
dns_rdataset_disassociate(&nta->rdataset);
}
if (dns_rdataset_isassociated(&nta->sigrdataset)) {
dns_rdataset_disassociate(&nta->sigrdataset);
}
if (atomic_load(&ntatable->shuttingdown)) {
isc_timer_stop(nta->timer);
return;
}
result = dns_view_getresolver(ntatable->view, &resolver);
if (result != ISC_R_SUCCESS) {
return;
}
dns__nta_ref(nta); /* for dns_resolver_createfetch */
result = dns_resolver_createfetch(
resolver, &nta->name, dns_rdatatype_nsec, NULL, NULL, NULL,
NULL, 0, DNS_FETCHOPT_NONTA, 0, NULL, nta->loop, fetch_done,
nta, &nta->rdataset, &nta->sigrdataset, &nta->fetch);
if (result != ISC_R_SUCCESS) {
dns__nta_detach(&nta); /* for dns_resolver_createfetch() */
}
dns_resolver_detach(&resolver);
}
static void
settimer(dns_ntatable_t *ntatable, dns__nta_t *nta, uint32_t lifetime) {
dns_view_t *view = NULL;
isc_interval_t interval;
REQUIRE(VALID_NTATABLE(ntatable));
REQUIRE(VALID_NTA(nta));
view = ntatable->view;
if (view->nta_recheck == 0 || lifetime <= view->nta_recheck) {
return;
}
isc_timer_create(nta->loop, checkbogus, nta, &nta->timer);
isc_interval_set(&interval, view->nta_recheck, 0);
isc_timer_start(nta->timer, isc_timertype_ticker, &interval);
}
static void
nta_create(dns_ntatable_t *ntatable, const dns_name_t *name,
dns__nta_t **target) {
dns__nta_t *nta = NULL;
REQUIRE(VALID_NTATABLE(ntatable));
REQUIRE(target != NULL && *target == NULL);
nta = isc_mem_get(ntatable->mctx, sizeof(dns__nta_t));
*nta = (dns__nta_t){
.ntatable = ntatable,
.name = DNS_NAME_INITEMPTY,
.magic = NTA_MAGIC,
};
isc_mem_attach(ntatable->mctx, &nta->mctx);
isc_loop_attach(isc_loop(), &nta->loop);
dns_rdataset_init(&nta->rdataset);
dns_rdataset_init(&nta->sigrdataset);
isc_refcount_init(&nta->references, 1);
dns_name_dupwithoffsets(name, nta->mctx, &nta->name);
*target = nta;
}
isc_result_t
dns_ntatable_add(dns_ntatable_t *ntatable, const dns_name_t *name, bool force,
isc_stdtime_t now, uint32_t lifetime) {
isc_result_t result = ISC_R_SUCCESS;
dns__nta_t *nta = NULL;
dns_qp_t *qp = NULL;
void *pval = NULL;
REQUIRE(VALID_NTATABLE(ntatable));
if (atomic_load(&ntatable->shuttingdown)) {
return ISC_R_SUCCESS;
}
RWLOCK(&ntatable->rwlock, isc_rwlocktype_write);
dns_qpmulti_write(ntatable->table, &qp);
nta_create(ntatable, name, &nta);
nta->forced = force;
result = dns_qp_insert(qp, nta, 0);
switch (result) {
case ISC_R_EXISTS:
result = dns_qp_getname(qp, &nta->name, &pval, NULL);
if (result == ISC_R_SUCCESS) {
/*
* an NTA already existed: throw away the
* new one and update the old one.
*/
dns__nta_detach(&nta); /* for nta_create */
nta = pval;
break;
}
/* update the NTA's timer as if it were new */
FALLTHROUGH;
case ISC_R_SUCCESS:
nta->expiry = now + lifetime;
if (!force) {
settimer(ntatable, nta, lifetime);
}
break;
default:
break;
}
dns_qp_compact(qp, DNS_QPGC_MAYBE);
dns_qpmulti_commit(ntatable->table, &qp);
RWUNLOCK(&ntatable->rwlock, isc_rwlocktype_write);
return result;
}
isc_result_t
dns_ntatable_delete(dns_ntatable_t *ntatable, const dns_name_t *name) {
isc_result_t result;
dns_qp_t *qp = NULL;
void *pval = NULL;
REQUIRE(VALID_NTATABLE(ntatable));
REQUIRE(name != NULL);
dns_qpmulti_write(ntatable->table, &qp);
result = dns_qp_deletename(qp, name, &pval, NULL);
if (result == ISC_R_SUCCESS) {
dns__nta_t *n = pval;
dns__nta_shutdown(n);
dns__nta_detach(&n);
}
dns_qp_compact(qp, DNS_QPGC_MAYBE);
dns_qpmulti_commit(ntatable->table, &qp);
return result;
}
static void
delete_expired(void *arg) {
dns__nta_t *nta = arg;
dns_ntatable_t *ntatable = nta->ntatable;
isc_result_t result;
dns_qp_t *qp = NULL;
void *pval = NULL;
REQUIRE(VALID_NTATABLE(ntatable));
RWLOCK(&ntatable->rwlock, isc_rwlocktype_write);
dns_qpmulti_write(ntatable->table, &qp);
result = dns_qp_getname(qp, &nta->name, &pval, NULL);
if (result == ISC_R_SUCCESS &&
((dns__nta_t *)pval)->expiry == nta->expiry && !nta->shuttingdown)
{
char nb[DNS_NAME_FORMATSIZE];
dns_name_format(&nta->name, nb, sizeof(nb));
isc_log_write(dns_lctx, DNS_LOGCATEGORY_DNSSEC,
DNS_LOGMODULE_NTA, ISC_LOG_INFO,
"deleting expired NTA at %s", nb);
dns_qp_deletename(qp, &nta->name, NULL, NULL);
dns__nta_shutdown(nta);
dns__nta_unref(nta);
}
dns_qp_compact(qp, DNS_QPGC_MAYBE);
dns_qpmulti_commit(ntatable->table, &qp);
RWUNLOCK(&ntatable->rwlock, isc_rwlocktype_write);
dns__nta_detach(&nta);
dns_ntatable_detach(&ntatable);
}
bool
dns_ntatable_covered(dns_ntatable_t *ntatable, isc_stdtime_t now,
const dns_name_t *name, const dns_name_t *anchor) {
isc_result_t result;
dns__nta_t *nta = NULL;
bool answer = false;
dns_qpread_t qpr;
void *pval = NULL;
REQUIRE(VALID_NTATABLE(ntatable));
REQUIRE(dns_name_isabsolute(name));
RWLOCK(&ntatable->rwlock, isc_rwlocktype_read);
dns_qpmulti_query(ntatable->table, &qpr);
result = dns_qp_lookup(&qpr, name, NULL, NULL, NULL, &pval, NULL);
nta = pval;
switch (result) {
case ISC_R_SUCCESS:
/* Exact match */
break;
case DNS_R_PARTIALMATCH:
/*
* Found a NTA that's an ancestor of 'name'; we
* now have to make sure 'anchor' isn't below it.
*/
if (!dns_name_issubdomain(&nta->name, anchor)) {
goto done;
}
/* Ancestor match */
break;
default:
/* Found nothing */
goto done;
}
if (nta->expiry <= now) {
/* NTA is expired */
dns__nta_ref(nta);
dns_ntatable_ref(nta->ntatable);
isc_async_current(delete_expired, nta);
goto done;
}
answer = true;
done:
RWUNLOCK(&ntatable->rwlock, isc_rwlocktype_read);
dns_qpread_destroy(ntatable->table, &qpr);
return answer;
}
static isc_result_t
putstr(isc_buffer_t **b, const char *str) {
isc_result_t result;
result = isc_buffer_reserve(*b, strlen(str));
if (result != ISC_R_SUCCESS) {
return result;
}
isc_buffer_putstr(*b, str);
return ISC_R_SUCCESS;
}
isc_result_t
dns_ntatable_totext(dns_ntatable_t *ntatable, const char *view,
isc_buffer_t **buf) {
isc_result_t result = ISC_R_SUCCESS;
isc_stdtime_t now = isc_stdtime_now();
dns_qpread_t qpr;
dns_qpiter_t iter;
bool first = true;
void *pval = NULL;
REQUIRE(VALID_NTATABLE(ntatable));
RWLOCK(&ntatable->rwlock, isc_rwlocktype_read);
dns_qpmulti_query(ntatable->table, &qpr);
dns_qpiter_init(&qpr, &iter);
while (dns_qpiter_next(&iter, NULL, &pval, NULL) == ISC_R_SUCCESS) {
dns__nta_t *n = pval;
char nbuf[DNS_NAME_FORMATSIZE];
char tbuf[ISC_FORMATHTTPTIMESTAMP_SIZE];
char obuf[DNS_NAME_FORMATSIZE + ISC_FORMATHTTPTIMESTAMP_SIZE +
sizeof("expired: \n")];
isc_time_t t;
dns_name_format(&n->name, nbuf, sizeof(nbuf));
if (n->expiry != 0xffffffffU) {
/* Normal NTA entries */
isc_time_set(&t, n->expiry, 0);
isc_time_formattimestamp(&t, tbuf, sizeof(tbuf));
snprintf(obuf, sizeof(obuf), "%s%s%s%s: %s %s",
first ? "" : "\n", nbuf,
view != NULL ? "/" : "",
view != NULL ? view : "",
n->expiry <= now ? "expired" : "expiry", tbuf);
} else {
/* "validate-except" entries */
snprintf(obuf, sizeof(obuf), "%s%s%s%s: %s",
first ? "" : "\n", nbuf,
view != NULL ? "/" : "",
view != NULL ? view : "", "permanent");
}
first = false;
result = putstr(buf, obuf);
if (result != ISC_R_SUCCESS) {
goto cleanup;
}
}
cleanup:
dns_qpread_destroy(ntatable->table, &qpr);
RWUNLOCK(&ntatable->rwlock, isc_rwlocktype_read);
return result;
}
isc_result_t
dns_ntatable_save(dns_ntatable_t *ntatable, FILE *fp) {
isc_result_t result = ISC_R_SUCCESS;
isc_stdtime_t now = isc_stdtime_now();
dns_qpread_t qpr;
dns_qpiter_t iter;
bool written = false;
void *pval = NULL;
REQUIRE(VALID_NTATABLE(ntatable));
RWLOCK(&ntatable->rwlock, isc_rwlocktype_read);
dns_qpmulti_query(ntatable->table, &qpr);
dns_qpiter_init(&qpr, &iter);
while (dns_qpiter_next(&iter, NULL, &pval, NULL) == ISC_R_SUCCESS) {
dns__nta_t *n = pval;
isc_buffer_t b;
char nbuf[DNS_NAME_FORMATSIZE + 1], tbuf[80];
/*
* Skip this node if the expiry is already in the
* past, or if this is a "validate-except" entry.
*/
if (n->expiry <= now || n->expiry == 0xffffffffU) {
continue;
}
isc_buffer_init(&b, nbuf, sizeof(nbuf));
result = dns_name_totext(&n->name, 0, &b);
if (result != ISC_R_SUCCESS) {
continue;
}
/* Zero terminate */
isc_buffer_putuint8(&b, 0);
isc_buffer_init(&b, tbuf, sizeof(tbuf));
dns_time32_totext(n->expiry, &b);
/* Zero terminate */
isc_buffer_putuint8(&b, 0);
fprintf(fp, "%s %s %s\n", nbuf,
n->forced ? "forced" : "regular", tbuf);
written = true;
}
dns_qpread_destroy(ntatable->table, &qpr);
RWUNLOCK(&ntatable->rwlock, isc_rwlocktype_read);
if (result == ISC_R_SUCCESS && !written) {
result = ISC_R_NOTFOUND;
}
return result;
}
static void
dns__nta_shutdown_cb(void *arg) {
dns__nta_t *nta = arg;
REQUIRE(VALID_NTA(nta));
if (isc_log_wouldlog(dns_lctx, ISC_LOG_DEBUG(3))) {
char nb[DNS_NAME_FORMATSIZE];
dns_name_format(&nta->name, nb, sizeof(nb));
isc_log_write(dns_lctx, DNS_LOGCATEGORY_DNSSEC,
DNS_LOGMODULE_NTA, ISC_LOG_DEBUG(3),
"shutting down NTA %p at %s", nta, nb);
}
if (nta->timer) {
isc_timer_stop(nta->timer);
isc_timer_destroy(&nta->timer);
}
dns__nta_detach(&nta);
}
static void
dns__nta_shutdown(dns__nta_t *nta) {
REQUIRE(VALID_NTA(nta));
dns__nta_ref(nta);
isc_async_run(nta->loop, dns__nta_shutdown_cb, nta);
nta->shuttingdown = true;
}
void
dns_ntatable_shutdown(dns_ntatable_t *ntatable) {
dns_qpread_t qpr;
dns_qpiter_t iter;
void *pval = NULL;
REQUIRE(VALID_NTATABLE(ntatable));
RWLOCK(&ntatable->rwlock, isc_rwlocktype_write);
dns_qpmulti_query(ntatable->table, &qpr);
ntatable->shuttingdown = true;
dns_qpiter_init(&qpr, &iter);
while (dns_qpiter_next(&iter, NULL, &pval, NULL) == ISC_R_SUCCESS) {
dns__nta_t *n = pval;
dns__nta_shutdown(n);
dns__nta_detach(&n);
}
dns_qpread_destroy(ntatable->table, &qpr);
dns_view_weakdetach(&ntatable->view);
RWUNLOCK(&ntatable->rwlock, isc_rwlocktype_write);
}
static void
qp_attach(void *uctx ISC_ATTR_UNUSED, void *pval,
uint32_t ival ISC_ATTR_UNUSED) {
dns__nta_t *nta = pval;
dns__nta_ref(nta);
}
static void
qp_detach(void *uctx ISC_ATTR_UNUSED, void *pval,
uint32_t ival ISC_ATTR_UNUSED) {
dns__nta_t *nta = pval;
dns__nta_detach(&nta);
}
static size_t
qp_makekey(dns_qpkey_t key, void *uctx ISC_ATTR_UNUSED, void *pval,
uint32_t ival ISC_ATTR_UNUSED) {
dns__nta_t *nta = pval;
return dns_qpkey_fromname(key, &nta->name);
}
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);
}
|