Debian Codegraph @

lib/dns/rbt-zonedb.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * SPDX-License-Identifier: MPL-2.0
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * See the COPYRIGHT file distributed with this work for additional
11 * information regarding copyright ownership.
12 */
13
14 /*! \file */
15
16 #include <inttypes.h>
17 #include <stdbool.h>
18 #include <sys/mman.h>
19
20 #include <isc/ascii.h>
21 #include <isc/async.h>
22 #include <isc/atomic.h>
23 #include <isc/crc64.h>
24 #include <isc/file.h>
25 #include <isc/hash.h>
26 #include <isc/hashmap.h>
27 #include <isc/heap.h>
28 #include <isc/hex.h>
29 #include <isc/loop.h>
30 #include <isc/mem.h>
31 #include <isc/mutex.h>
32 #include <isc/once.h>
33 #include <isc/random.h>
34 #include <isc/refcount.h>
35 #include <isc/result.h>
36 #include <isc/rwlock.h>
37 #include <isc/serial.h>
38 #include <isc/stdio.h>
39 #include <isc/string.h>
40 #include <isc/time.h>
41 #include <isc/urcu.h>
42 #include <isc/util.h>
43
44 #include <dns/callbacks.h>
45 #include <dns/db.h>
46 #include <dns/dbiterator.h>
47 #include <dns/fixedname.h>
48 #include <dns/log.h>
49 #include <dns/masterdump.h>
50 #include <dns/nsec.h>
51 #include <dns/nsec3.h>
52 #include <dns/rbt.h>
53 #include <dns/rdata.h>
54 #include <dns/rdataset.h>
55 #include <dns/rdatasetiter.h>
56 #include <dns/rdataslab.h>
57 #include <dns/rdatastruct.h>
58 #include <dns/stats.h>
59 #include <dns/time.h>
60 #include <dns/view.h>
61 #include <dns/zone.h>
62 #include <dns/zonekey.h>
63
64 #include "db_p.h"
65 #include "rbtdb_p.h"
66
67 #define CHECK(op) \
68 do { \
69 result = (op); \
70 if (result != ISC_R_SUCCESS) \
71 goto failure; \
72 } while (0)
73
74 #define EXISTS(header) \
75 ((atomic_load_acquire(&(header)->attributes) & \
76 DNS_SLABHEADERATTR_NONEXISTENT) == 0)
77 #define NONEXISTENT(header) \
78 ((atomic_load_acquire(&(header)->attributes) & \
79 DNS_SLABHEADERATTR_NONEXISTENT) != 0)
80 #define IGNORE(header) \
81 ((atomic_load_acquire(&(header)->attributes) & \
82 DNS_SLABHEADERATTR_IGNORE) != 0)
83 #define RESIGN(header) \
84 ((atomic_load_acquire(&(header)->attributes) & \
85 DNS_SLABHEADERATTR_RESIGN) != 0)
86 #define ANCIENT(header) \
87 ((atomic_load_acquire(&(header)->attributes) & \
88 DNS_SLABHEADERATTR_ANCIENT) != 0)
89
90 #define RBTDB_ATTR_LOADED 0x01
91 #define RBTDB_ATTR_LOADING 0x02
92
93 static isc_result_t
94 findnsec3node(dns_db_t *db, const dns_name_t *name, bool create,
95 dns_dbnode_t **nodep DNS__DB_FLARG) {
96 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
97
98 REQUIRE(VALID_RBTDB(rbtdb));
99
100 return dns__rbtdb_findnodeintree(rbtdb, rbtdb->nsec3, name, create,
101 nodep DNS__DB_FLARG_PASS);
102 }
103
104 static isc_result_t
105 zone_zonecut_callback(dns_rbtnode_t *node, dns_name_t *name,
106 void *arg DNS__DB_FLARG) {
107 rbtdb_search_t *search = arg;
108 dns_slabheader_t *header = NULL, *header_next = NULL;
109 dns_slabheader_t *dname_header = NULL, *sigdname_header = NULL;
110 dns_slabheader_t *ns_header = NULL;
111 dns_slabheader_t *found = NULL;
112 isc_result_t result = DNS_R_CONTINUE;
113 dns_rbtnode_t *onode = NULL;
114 isc_rwlocktype_t nlocktype = isc_rwlocktype_none;
115
116 /*
117 * We only want to remember the topmost zone cut, since it's the one
118 * that counts, so we'll just continue if we've already found a
119 * zonecut.
120 */
121 if (search->zonecut != NULL) {
122 return result;
123 }
124
125 onode = search->rbtdb->origin_node;
126
127 NODE_RDLOCK(&(search->rbtdb->node_locks[node->locknum].lock),
128 &nlocktype);
129
130 /*
131 * Look for an NS or DNAME rdataset active in our version.
132 */
133 for (header = node->data; header != NULL; header = header_next) {
134 header_next = header->next;
135 if (header->type == dns_rdatatype_ns ||
136 header->type == dns_rdatatype_dname ||
137 header->type == DNS_SIGTYPE(dns_rdatatype_dname))
138 {
139 do {
140 if (header->serial <= search->serial &&
141 !IGNORE(header))
142 {
143 /*
144 * Is this a "this rdataset doesn't
145 * exist" record?
146 */
147 if (NONEXISTENT(header)) {
148 header = NULL;
149 }
150 break;
151 } else {
152 header = header->down;
153 }
154 } while (header != NULL);
155 if (header != NULL) {
156 if (header->type == dns_rdatatype_dname) {
157 dname_header = header;
158 } else if (header->type ==
159 DNS_SIGTYPE(dns_rdatatype_dname))
160 {
161 sigdname_header = header;
162 } else if (node != onode ||
163 IS_STUB(search->rbtdb))
164 {
165 /*
166 * We've found an NS rdataset that
167 * isn't at the origin node. We check
168 * that they're not at the origin node,
169 * because otherwise we'd erroneously
170 * treat the zone top as if it were
171 * a delegation.
172 */
173 ns_header = header;
174 }
175 }
176 }
177 }
178
179 /*
180 * Did we find anything?
181 */
182 if (!IS_STUB(search->rbtdb) && ns_header != NULL) {
183 /*
184 * Note that NS has precedence over DNAME if both exist
185 * in a zone. Otherwise DNAME take precedence over NS.
186 */
187 found = ns_header;
188 search->zonecut_sigheader = NULL;
189 } else if (dname_header != NULL) {
190 found = dname_header;
191 search->zonecut_sigheader = sigdname_header;
192 } else if (ns_header != NULL) {
193 found = ns_header;
194 search->zonecut_sigheader = NULL;
195 }
196
197 if (found != NULL) {
198 /*
199 * We increment the reference count on node to ensure that
200 * search->zonecut_header will still be valid later.
201 */
202 dns__rbtdb_newref(search->rbtdb, node,
203 isc_rwlocktype_read DNS__DB_FLARG_PASS);
204 search->zonecut = node;
205 search->zonecut_header = found;
206 search->need_cleanup = true;
207 /*
208 * Since we've found a zonecut, anything beneath it is
209 * glue and is not subject to wildcard matching, so we
210 * may clear search->wild.
211 */
212 search->wild = false;
213 if ((search->options & DNS_DBFIND_GLUEOK) == 0) {
214 /*
215 * If the caller does not want to find glue, then
216 * this is the best answer and the search should
217 * stop now.
218 */
219 result = DNS_R_PARTIALMATCH;
220 } else {
221 dns_name_t *zcname = NULL;
222
223 /*
224 * The search will continue beneath the zone cut.
225 * This may or may not be the best match. In case it
226 * is, we need to remember the node name.
227 */
228 zcname = dns_fixedname_name(&search->zonecut_name);
229 dns_name_copy(name, zcname);
230 search->copy_name = true;
231 }
232 } else {
233 /*
234 * There is no zonecut at this node which is active in this
235 * version.
236 *
237 * If this is a "wild" node and the caller hasn't disabled
238 * wildcard matching, remember that we've seen a wild node
239 * in case we need to go searching for wildcard matches
240 * later on.
241 */
242 if (node->wild && (search->options & DNS_DBFIND_NOWILD) == 0) {
243 search->wild = true;
244 }
245 }
246
247 NODE_UNLOCK(&(search->rbtdb->node_locks[node->locknum].lock),
248 &nlocktype);
249
250 return result;
251 }
252
253 static isc_result_t
254 setup_delegation(rbtdb_search_t *search, dns_dbnode_t **nodep,
255 dns_name_t *foundname, dns_rdataset_t *rdataset,
256 dns_rdataset_t *sigrdataset DNS__DB_FLARG) {
257 dns_name_t *zcname = NULL;
258 dns_typepair_t type;
259 dns_rbtnode_t *node = NULL;
260
261 REQUIRE(search != NULL);
262 REQUIRE(search->zonecut != NULL);
263 REQUIRE(search->zonecut_header != NULL);
264
265 /*
266 * The caller MUST NOT be holding any node locks.
267 */
268
269 node = search->zonecut;
270 type = search->zonecut_header->type;
271
272 /*
273 * If we have to set foundname, we do it before anything else.
274 * If we were to set foundname after we had set nodep or bound the
275 * rdataset, then we'd have to undo that work if dns_name_copy()
276 * failed. By setting foundname first, there's nothing to undo if
277 * we have trouble.
278 */
279 if (foundname != NULL && search->copy_name) {
280 zcname = dns_fixedname_name(&search->zonecut_name);
281 dns_name_copy(zcname, foundname);
282 }
283 if (nodep != NULL) {
284 /*
285 * Note that we don't have to increment the node's reference
286 * count here because we're going to use the reference we
287 * already have in the search block.
288 */
289 *nodep = node;
290 search->need_cleanup = false;
291 }
292 if (rdataset != NULL) {
293 isc_rwlocktype_t nlocktype = isc_rwlocktype_none;
294 NODE_RDLOCK(&(search->rbtdb->node_locks[node->locknum].lock),
295 &nlocktype);
296 dns__rbtdb_bindrdataset(search->rbtdb, node,
297 search->zonecut_header, search->now,
298 isc_rwlocktype_read,
299 rdataset DNS__DB_FLARG_PASS);
300 if (sigrdataset != NULL && search->zonecut_sigheader != NULL) {
301 dns__rbtdb_bindrdataset(
302 search->rbtdb, node, search->zonecut_sigheader,
303 search->now, isc_rwlocktype_read,
304 sigrdataset DNS__DB_FLARG_PASS);
305 }
306 NODE_UNLOCK(&(search->rbtdb->node_locks[node->locknum].lock),
307 &nlocktype);
308 }
309
310 if (type == dns_rdatatype_dname) {
311 return DNS_R_DNAME;
312 }
313 return DNS_R_DELEGATION;
314 }
315
316 typedef enum { FORWARD, BACK } direction_t;
317
318 /*
319 * Step backwards or forwards through the database until we find a
320 * node with data in it for the desired version. If 'nextname' is not NULL,
321 * and we found a predecessor or successor, save the name we found in it.
322 * Return true if we found a predecessor or successor.
323 */
324 static bool
325 step(rbtdb_search_t *search, dns_rbtnodechain_t *chain, direction_t direction,
326 dns_name_t *nextname) {
327 dns_fixedname_t forigin;
328 dns_name_t *origin = NULL;
329 dns_name_t prefix;
330 dns_rbtdb_t *rbtdb = NULL;
331 dns_rbtnode_t *node = NULL;
332 isc_result_t result = ISC_R_SUCCESS;
333 dns_slabheader_t *header = NULL;
334
335 rbtdb = search->rbtdb;
336
337 dns_name_init(&prefix, NULL);
338 origin = dns_fixedname_initname(&forigin);
339
340 while (result == ISC_R_SUCCESS || result == DNS_R_NEWORIGIN) {
341 isc_rwlocktype_t nlocktype = isc_rwlocktype_none;
342 node = NULL;
343 result = dns_rbtnodechain_current(chain, &prefix, origin,
344 &node);
345 if (result != ISC_R_SUCCESS) {
346 break;
347 }
348 NODE_RDLOCK(&(rbtdb->node_locks[node->locknum].lock),
349 &nlocktype);
350 for (header = node->data; header != NULL; header = header->next)
351 {
352 if (header->serial <= search->serial &&
353 !IGNORE(header) && EXISTS(header))
354 {
355 break;
356 }
357 }
358 NODE_UNLOCK(&(rbtdb->node_locks[node->locknum].lock),
359 &nlocktype);
360 if (header != NULL) {
361 break;
362 }
363 if (direction == FORWARD) {
364 result = dns_rbtnodechain_next(chain, NULL, NULL);
365 } else {
366 result = dns_rbtnodechain_prev(chain, NULL, NULL);
367 }
368 };
369 if (result == ISC_R_SUCCESS) {
370 result = dns_name_concatenate(&prefix, origin, nextname, NULL);
371 }
372 if (result == ISC_R_SUCCESS) {
373 return true;
374 }
375 return false;
376 }
377
378 /*
379 * Use step() to find the successor to the current name, and then
380 * check to see whether it's a subdomain of the current name. If so,
381 * then this is an empty non-terminal in the currently active version
382 * of the database.
383 */
384 static bool
385 activeempty(rbtdb_search_t *search, dns_rbtnodechain_t *chain,
386 const dns_name_t *current) {
387 isc_result_t result;
388 dns_fixedname_t fnext;
389 dns_name_t *next = dns_fixedname_initname(&fnext);
390
391 result = dns_rbtnodechain_next(chain, NULL, NULL);
392 if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN) {
393 return false;
394 }
395 return step(search, chain, FORWARD, next) &&
396 dns_name_issubdomain(next, current);
397 }
398
399 static bool
400 wildcard_blocked(rbtdb_search_t *search, const dns_name_t *qname,
401 dns_name_t *wname) {
402 isc_result_t result;
403 dns_fixedname_t fnext;
404 dns_fixedname_t fprev;
405 dns_name_t *next = NULL, *prev = NULL;
406 dns_name_t name;
407 dns_name_t rname;
408 dns_name_t tname;
409 dns_rbtnodechain_t chain;
410 bool check_next = false;
411 bool check_prev = false;
412 unsigned int n;
413
414 dns_name_init(&name, NULL);
415 dns_name_init(&tname, NULL);
416 dns_name_init(&rname, NULL);
417 next = dns_fixedname_initname(&fnext);
418 prev = dns_fixedname_initname(&fprev);
419
420 /*
421 * The qname seems to have matched a wildcard, but we
422 * need to find out if there's an empty nonterminal node
423 * between the wildcard level and the qname.
424 *
425 * search->chain should now be pointing at the predecessor
426 * of the searched-for name. We are using a local copy of the
427 * chain so as not to change the state of search->chain.
428 * step() will walk backward until we find a predecessor with
429 * data.
430 */
431 chain = search->chain;
432 check_prev = step(search, &chain, BACK, prev);
433
434 /* Now reset the chain and look for a successor with data. */
435 chain = search->chain;
436 result = dns_rbtnodechain_next(&chain, NULL, NULL);
437 if (result == ISC_R_SUCCESS) {
438 check_next = step(search, &chain, FORWARD, next);
439 }
440
441 if (!check_prev && !check_next) {
442 /* No predecessor or successor was found at all? */
443 return false;
444 }
445
446 dns_name_clone(qname, &rname);
447
448 /*
449 * Remove the wildcard label to find the terminal name.
450 */
451 n = dns_name_countlabels(wname);
452 dns_name_getlabelsequence(wname, 1, n - 1, &tname);
453
454 do {
455 if ((check_prev && dns_name_issubdomain(prev, &rname)) ||
456 (check_next && dns_name_issubdomain(next, &rname)))
457 {
458 return true;
459 }
460
461 /*
462 * Remove the leftmost label from the qname and check again.
463 */
464 n = dns_name_countlabels(&rname);
465 dns_name_getlabelsequence(&rname, 1, n - 1, &rname);
466 } while (!dns_name_equal(&rname, &tname));
467
468 return false;
469 }
470
471 static isc_result_t
472 find_wildcard(rbtdb_search_t *search, dns_rbtnode_t **nodep,
473 const dns_name_t *qname) {
474 unsigned int i, j;
475 dns_rbtnode_t *node = NULL, *level_node = NULL, *wnode = NULL;
476 dns_slabheader_t *header = NULL;
477 isc_result_t result = ISC_R_NOTFOUND;
478 dns_name_t name;
479 dns_name_t *wname = NULL;
480 dns_fixedname_t fwname;
481 dns_rbtdb_t *rbtdb = NULL;
482 bool done, wild, active;
483 dns_rbtnodechain_t wchain;
484
485 /*
486 * Caller must be holding the tree lock and MUST NOT be holding
487 * any node locks.
488 */
489
490 /*
491 * Examine each ancestor level. If the level's wild bit
492 * is set, then construct the corresponding wildcard name and
493 * search for it. If the wildcard node exists, and is active in
494 * this version, we're done. If not, then we next check to see
495 * if the ancestor is active in this version. If so, then there
496 * can be no possible wildcard match and again we're done. If not,
497 * continue the search.
498 */
499
500 rbtdb = search->rbtdb;
501 i = search->chain.level_matches;
502 done = false;
503 node = *nodep;
504 do {
505 isc_rwlock_t *lock = &rbtdb->node_locks[node->locknum].lock;
506 isc_rwlocktype_t nlocktype = isc_rwlocktype_none;
507 NODE_RDLOCK(lock, &nlocktype);
508
509 /*
510 * First we try to figure out if this node is active in
511 * the search's version. We do this now, even though we
512 * may not need the information, because it simplifies the
513 * locking and code flow.
514 */
515 for (header = node->data; header != NULL; header = header->next)
516 {
517 if (header->serial <= search->serial &&
518 !IGNORE(header) && EXISTS(header) &&
519 !ANCIENT(header))
520 {
521 break;
522 }
523 }
524 if (header != NULL) {
525 active = true;
526 } else {
527 active = false;
528 }
529
530 if (node->wild) {
531 wild = true;
532 } else {
533 wild = false;
534 }
535
536 NODE_UNLOCK(lock, &nlocktype);
537
538 if (wild) {
539 /*
540 * Construct the wildcard name for this level.
541 */
542 dns_name_init(&name, NULL);
543 dns_rbt_namefromnode(node, &name);
544 wname = dns_fixedname_initname(&fwname);
545 result = dns_name_concatenate(dns_wildcardname, &name,
546 wname, NULL);
547 j = i;
548 while (result == ISC_R_SUCCESS && j != 0) {
549 j--;
550 level_node = search->chain.levels[j];
551 dns_name_init(&name, NULL);
552 dns_rbt_namefromnode(level_node, &name);
553 result = dns_name_concatenate(wname, &name,
554 wname, NULL);
555 }
556 if (result != ISC_R_SUCCESS) {
557 break;
558 }
559
560 wnode = NULL;
561 dns_rbtnodechain_init(&wchain);
562 result = dns_rbt_findnode(
563 rbtdb->tree, wname, NULL, &wnode, &wchain,
564 DNS_RBTFIND_EMPTYDATA, NULL, NULL);
565 if (result == ISC_R_SUCCESS) {
566 /*
567 * We have found the wildcard node. If it
568 * is active in the search's version, we're
569 * done.
570 */
571 lock = &rbtdb->node_locks[wnode->locknum].lock;
572 NODE_RDLOCK(lock, &nlocktype);
573 for (header = wnode->data; header != NULL;
574 header = header->next)
575 {
576 if (header->serial <= search->serial &&
577 !IGNORE(header) && EXISTS(header) &&
578 !ANCIENT(header))
579 {
580 break;
581 }
582 }
583 NODE_UNLOCK(lock, &nlocktype);
584 if (header != NULL ||
585 activeempty(search, &wchain, wname))
586 {
587 if (wildcard_blocked(search, qname,
588 wname))
589 {
590 return ISC_R_NOTFOUND;
591 }
592 /*
593 * The wildcard node is active!
594 *
595 * Note: result is still ISC_R_SUCCESS
596 * so we don't have to set it.
597 */
598 *nodep = wnode;
599 break;
600 }
601 } else if (result != ISC_R_NOTFOUND &&
602 result != DNS_R_PARTIALMATCH)
603 {
604 /*
605 * An error has occurred. Bail out.
606 */
607 break;
608 }
609 }
610
611 if (active) {
612 /*
613 * The level node is active. Any wildcarding
614 * present at higher levels has no
615 * effect and we're done.
616 */
617 result = ISC_R_NOTFOUND;
618 break;
619 }
620
621 if (i > 0) {
622 i--;
623 node = search->chain.levels[i];
624 } else {
625 done = true;
626 }
627 } while (!done);
628
629 return result;
630 }
631
632 static bool
633 matchparams(dns_slabheader_t *header, rbtdb_search_t *search) {
634 dns_rdata_t rdata = DNS_RDATA_INIT;
635 dns_rdata_nsec3_t nsec3;
636 unsigned char *raw = NULL;
637 unsigned int rdlen, count;
638 isc_region_t region;
639 isc_result_t result;
640
641 REQUIRE(header->type == dns_rdatatype_nsec3);
642
643 raw = (unsigned char *)header + sizeof(*header);
644 count = raw[0] * 256 + raw[1]; /* count */
645 raw += DNS_RDATASET_COUNT + DNS_RDATASET_LENGTH;
646
647 while (count-- > 0) {
648 rdlen = raw[0] * 256 + raw[1];
649 raw += DNS_RDATASET_ORDER + DNS_RDATASET_LENGTH;
650 region.base = raw;
651 region.length = rdlen;
652 dns_rdata_fromregion(&rdata, search->rbtdb->common.rdclass,
653 dns_rdatatype_nsec3, &region);
654 raw += rdlen;
655 result = dns_rdata_tostruct(&rdata, &nsec3, NULL);
656 INSIST(result == ISC_R_SUCCESS);
657 if (nsec3.hash == search->rbtversion->hash &&
658 nsec3.iterations == search->rbtversion->iterations &&
659 nsec3.salt_length == search->rbtversion->salt_length &&
660 memcmp(nsec3.salt, search->rbtversion->salt,
661 nsec3.salt_length) == 0)
662 {
663 return true;
664 }
665 dns_rdata_reset(&rdata);
666 }
667 return false;
668 }
669
670 /*
671 * Find node of the NSEC/NSEC3 record that is 'name'.
672 */
673 static isc_result_t
674 previous_closest_nsec(dns_rdatatype_t type, rbtdb_search_t *search,
675 dns_name_t *name, dns_name_t *origin,
676 dns_rbtnode_t **nodep, dns_rbtnodechain_t *nsecchain,
677 bool *firstp) {
678 dns_fixedname_t ftarget;
679 dns_name_t *target = NULL;
680 dns_rbtnode_t *nsecnode = NULL;
681 isc_result_t result;
682
683 REQUIRE(nodep != NULL && *nodep == NULL);
684 REQUIRE(type == dns_rdatatype_nsec3 || firstp != NULL);
685
686 if (type == dns_rdatatype_nsec3) {
687 result = dns_rbtnodechain_prev(&search->chain, NULL, NULL);
688 if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN) {
689 return result;
690 }
691 result = dns_rbtnodechain_current(&search->chain, name, origin,
692 nodep);
693 return result;
694 }
695
696 target = dns_fixedname_initname(&ftarget);
697
698 for (;;) {
699 if (*firstp) {
700 /*
701 * Construct the name of the second node to check.
702 * It is the first node sought in the NSEC tree.
703 */
704 *firstp = false;
705 dns_rbtnodechain_init(nsecchain);
706 result = dns_name_concatenate(name, origin, target,
707 NULL);
708 if (result != ISC_R_SUCCESS) {
709 return result;
710 }
711 nsecnode = NULL;
712 result = dns_rbt_findnode(
713 search->rbtdb->nsec, target, NULL, &nsecnode,
714 nsecchain, DNS_RBTFIND_EMPTYDATA, NULL, NULL);
715 if (result == ISC_R_SUCCESS) {
716 /*
717 * Since this was the first loop, finding the
718 * name in the NSEC tree implies that the first
719 * node checked in the main tree had an
720 * unacceptable NSEC record.
721 * Try the previous node in the NSEC tree.
722 */
723 result = dns_rbtnodechain_prev(nsecchain, name,
724 origin);
725 if (result == DNS_R_NEWORIGIN) {
726 result = ISC_R_SUCCESS;
727 }
728 } else if (result == ISC_R_NOTFOUND ||
729 result == DNS_R_PARTIALMATCH)
730 {
731 result = dns_rbtnodechain_current(
732 nsecchain, name, origin, NULL);
733 if (result == ISC_R_NOTFOUND) {
734 result = ISC_R_NOMORE;
735 }
736 }
737 } else {
738 /*
739 * This is a second or later trip through the auxiliary
740 * tree for the name of a third or earlier NSEC node in
741 * the main tree. Previous trips through the NSEC tree
742 * must have found nodes in the main tree with NSEC
743 * records. Perhaps they lacked signature records.
744 */
745 result = dns_rbtnodechain_prev(nsecchain, name, origin);
746 if (result == DNS_R_NEWORIGIN) {
747 result = ISC_R_SUCCESS;
748 }
749 }
750 if (result != ISC_R_SUCCESS) {
751 return result;
752 }
753
754 /*
755 * Construct the name to seek in the main tree.
756 */
757 result = dns_name_concatenate(name, origin, target, NULL);
758 if (result != ISC_R_SUCCESS) {
759 return result;
760 }
761
762 *nodep = NULL;
763 result = dns_rbt_findnode(search->rbtdb->tree, target, NULL,
764 nodep, &search->chain,
765 DNS_RBTFIND_EMPTYDATA, NULL, NULL);
766 if (result == ISC_R_SUCCESS) {
767 return result;
768 }
769
770 /*
771 * There should always be a node in the main tree with the
772 * same name as the node in the auxiliary NSEC tree, except for
773 * nodes in the auxiliary tree that are awaiting deletion.
774 */
775 if (result != DNS_R_PARTIALMATCH && result != ISC_R_NOTFOUND) {
776 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
777 DNS_LOGMODULE_CACHE, ISC_LOG_ERROR,
778 "previous_closest_nsec(): %s",
779 isc_result_totext(result));
780 return DNS_R_BADDB;
781 }
782 }
783 }
784
785 /*
786 * Find the NSEC/NSEC3 which is or before the current point on the
787 * search chain. For NSEC3 records only NSEC3 records that match the
788 * current NSEC3PARAM record are considered.
789 */
790 static isc_result_t
791 find_closest_nsec(rbtdb_search_t *search, dns_dbnode_t **nodep,
792 dns_name_t *foundname, dns_rdataset_t *rdataset,
793 dns_rdataset_t *sigrdataset, dns_rbt_t *tree,
794 bool secure DNS__DB_FLARG) {
795 dns_rbtnode_t *node = NULL, *prevnode = NULL;
796 dns_slabheader_t *header = NULL, *header_next = NULL;
797 dns_rbtnodechain_t nsecchain;
798 bool empty_node;
799 isc_result_t result;
800 dns_fixedname_t fname, forigin;
801 dns_name_t *name = NULL, *origin = NULL;
802 dns_rdatatype_t type;
803 dns_typepair_t sigtype;
804 bool wraps;
805 bool first = true;
806 bool need_sig = secure;
807
808 if (tree == search->rbtdb->nsec3) {
809 type = dns_rdatatype_nsec3;
810 sigtype = DNS_SIGTYPE(dns_rdatatype_nsec3);
811 wraps = true;
812 } else {
813 type = dns_rdatatype_nsec;
814 sigtype = DNS_SIGTYPE(dns_rdatatype_nsec);
815 wraps = false;
816 }
817
818 /*
819 * Use the auxiliary tree only starting with the second node in the
820 * hope that the original node will be right much of the time.
821 */
822 name = dns_fixedname_initname(&fname);
823 origin = dns_fixedname_initname(&forigin);
824 again:
825 node = NULL;
826 prevnode = NULL;
827 result = dns_rbtnodechain_current(&search->chain, name, origin, &node);
828 if (result != ISC_R_SUCCESS) {
829 return result;
830 }
831 do {
832 dns_slabheader_t *found = NULL, *foundsig = NULL;
833 isc_rwlocktype_t nlocktype = isc_rwlocktype_none;
834 NODE_RDLOCK(&(search->rbtdb->node_locks[node->locknum].lock),
835 &nlocktype);
836 empty_node = true;
837 for (header = node->data; header != NULL; header = header_next)
838 {
839 header_next = header->next;
840 /*
841 * Look for an active, extant NSEC or RRSIG NSEC.
842 */
843 do {
844 if (header->serial <= search->serial &&
845 !IGNORE(header))
846 {
847 /*
848 * Is this a "this rdataset doesn't
849 * exist" record?
850 */
851 if (NONEXISTENT(header)) {
852 header = NULL;
853 }
854 break;
855 } else {
856 header = header->down;
857 }
858 } while (header != NULL);
859 if (header != NULL) {
860 /*
861 * We now know that there is at least one
862 * active rdataset at this node.
863 */
864 empty_node = false;
865 if (header->type == type) {
866 found = header;
867 if (foundsig != NULL) {
868 break;
869 }
870 } else if (header->type == sigtype) {
871 foundsig = header;
872 if (found != NULL) {
873 break;
874 }
875 }
876 }
877 }
878 if (!empty_node) {
879 if (found != NULL && search->rbtversion->havensec3 &&
880 found->type == dns_rdatatype_nsec3 &&
881 !matchparams(found, search))
882 {
883 empty_node = true;
884 found = NULL;
885 foundsig = NULL;
886 result = previous_closest_nsec(
887 type, search, name, origin, &prevnode,
888 NULL, NULL);
889 } else if (found != NULL &&
890 (foundsig != NULL || !need_sig))
891 {
892 /*
893 * We've found the right NSEC/NSEC3 record.
894 *
895 * Note: for this to really be the right
896 * NSEC record, it's essential that the NSEC
897 * records of any nodes obscured by a zone
898 * cut have been removed; we assume this is
899 * the case.
900 */
901 result = dns_name_concatenate(name, origin,
902 foundname, NULL);
903 if (result == ISC_R_SUCCESS) {
904 if (nodep != NULL) {
905 dns__rbtdb_newref(
906 search->rbtdb, node,
907 isc_rwlocktype_read
908 DNS__DB_FLARG_PASS);
909 *nodep = node;
910 }
911 dns__rbtdb_bindrdataset(
912 search->rbtdb, node, found,
913 search->now,
914 isc_rwlocktype_read,
915 rdataset DNS__DB_FLARG_PASS);
916 if (foundsig != NULL) {
917 dns__rbtdb_bindrdataset(
918 search->rbtdb, node,
919 foundsig, search->now,
920 isc_rwlocktype_read,
921 sigrdataset
922 DNS__DB_FLARG_PASS);
923 }
924 }
925 } else if (found == NULL && foundsig == NULL) {
926 /*
927 * This node is active, but has no NSEC or
928 * RRSIG NSEC. That means it's glue or
929 * other obscured zone data that isn't
930 * relevant for our search. Treat the
931 * node as if it were empty and keep looking.
932 */
933 empty_node = true;
934 result = previous_closest_nsec(
935 type, search, name, origin, &prevnode,
936 &nsecchain, &first);
937 } else {
938 /*
939 * We found an active node, but either the
940 * NSEC or the RRSIG NSEC is missing. This
941 * shouldn't happen.
942 */
943 result = DNS_R_BADDB;
944 }
945 } else {
946 /*
947 * This node isn't active. We've got to keep
948 * looking.
949 */
950 result = previous_closest_nsec(type, search, name,
951 origin, &prevnode,
952 &nsecchain, &first);
953 }
954 NODE_UNLOCK(&(search->rbtdb->node_locks[node->locknum].lock),
955 &nlocktype);
956 node = prevnode;
957 prevnode = NULL;
958 } while (empty_node && result == ISC_R_SUCCESS);
959
960 if (!first) {
961 dns_rbtnodechain_invalidate(&nsecchain);
962 }
963
964 if (result == ISC_R_NOMORE && wraps) {
965 result = dns_rbtnodechain_last(&search->chain, tree, NULL,
966 NULL);
967 if (result == ISC_R_SUCCESS || result == DNS_R_NEWORIGIN) {
968 wraps = false;
969 goto again;
970 }
971 }
972
973 /*
974 * If the result is ISC_R_NOMORE, then we got to the beginning of
975 * the database and didn't find a NSEC record. This shouldn't
976 * happen.
977 */
978 if (result == ISC_R_NOMORE) {
979 result = DNS_R_BADDB;
980 }
981
982 return result;
983 }
984
985 static isc_result_t
986 zone_find(dns_db_t *db, const dns_name_t *name, dns_dbversion_t *version,
987 dns_rdatatype_t type, unsigned int options,
988 isc_stdtime_t now ISC_ATTR_UNUSED, dns_dbnode_t **nodep,
989 dns_name_t *foundname, dns_rdataset_t *rdataset,
990 dns_rdataset_t *sigrdataset DNS__DB_FLARG) {
991 dns_rbtnode_t *node = NULL;
992 isc_result_t result;
993 rbtdb_search_t search;
994 bool cname_ok = true;
995 bool close_version = false;
996 bool maybe_zonecut = false;
997 bool at_zonecut = false;
998 bool wild = false;
999 bool empty_node;
1000 dns_slabheader_t *header = NULL, *header_next = NULL;
1001 dns_slabheader_t *found = NULL, *nsecheader = NULL;
1002 dns_slabheader_t *foundsig = NULL, *cnamesig = NULL, *nsecsig = NULL;
1003 dns_typepair_t sigtype;
1004 bool active;
1005 isc_rwlock_t *lock = NULL;
1006 dns_rbt_t *tree = NULL;
1007 isc_rwlocktype_t nlocktype = isc_rwlocktype_none;
1008 isc_rwlocktype_t tlocktype = isc_rwlocktype_none;
1009
1010 REQUIRE(VALID_RBTDB((dns_rbtdb_t *)db));
1011 INSIST(version == NULL ||
1012 ((dns_rbtdb_version_t *)version)->rbtdb == (dns_rbtdb_t *)db);
1013
1014 /*
1015 * If the caller didn't supply a version, attach to the current
1016 * version.
1017 */
1018 if (version == NULL) {
1019 dns__rbtdb_currentversion(db, &version);
1020 close_version = true;
1021 }
1022
1023 search = (rbtdb_search_t){
1024 .rbtdb = (dns_rbtdb_t *)db,
1025 .rbtversion = version,
1026 .serial = ((dns_rbtdb_version_t *)version)->serial,
1027 .options = options,
1028 };
1029 dns_fixedname_init(&search.zonecut_name);
1030 dns_rbtnodechain_init(&search.chain);
1031
1032 TREE_RDLOCK(&search.rbtdb->tree_lock, &tlocktype);
1033
1034 /*
1035 * Search down from the root of the tree. If, while going down, we
1036 * encounter a callback node, zone_zonecut_callback() will search the
1037 * rdatasets at the zone cut for active DNAME or NS rdatasets.
1038 */
1039 tree = (options & DNS_DBFIND_FORCENSEC3) != 0 ? search.rbtdb->nsec3
1040 : search.rbtdb->tree;
1041 result = dns_rbt_findnode(tree, name, foundname, &node, &search.chain,
1042 DNS_RBTFIND_EMPTYDATA, zone_zonecut_callback,
1043 &search);
1044
1045 if (result == DNS_R_PARTIALMATCH) {
1046 partial_match:
1047 if (search.zonecut != NULL) {
1048 result = setup_delegation(
1049 &search, nodep, foundname, rdataset,
1050 sigrdataset DNS__DB_FLARG_PASS);
1051 goto tree_exit;
1052 }
1053
1054 if (search.wild) {
1055 /*
1056 * At least one of the levels in the search chain
1057 * potentially has a wildcard. For each such level,
1058 * we must see if there's a matching wildcard active
1059 * in the current version.
1060 */
1061 result = find_wildcard(&search, &node, name);
1062 if (result == ISC_R_SUCCESS) {
1063 dns_name_copy(name, foundname);
1064 wild = true;
1065 goto found;
1066 } else if (result != ISC_R_NOTFOUND) {
1067 goto tree_exit;
1068 }
1069 }
1070
1071 active = false;
1072 if ((options & DNS_DBFIND_FORCENSEC3) == 0) {
1073 /*
1074 * The NSEC3 tree won't have empty nodes,
1075 * so it isn't necessary to check for them.
1076 */
1077 dns_rbtnodechain_t chain = search.chain;
1078 active = activeempty(&search, &chain, name);
1079 }
1080
1081 /*
1082 * If we're here, then the name does not exist, is not
1083 * beneath a zonecut, and there's no matching wildcard.
1084 */
1085 if ((search.rbtversion->secure &&
1086 !search.rbtversion->havensec3) ||
1087 (search.options & DNS_DBFIND_FORCENSEC3) != 0)
1088 {
1089 result = find_closest_nsec(
1090 &search, nodep, foundname, rdataset,
1091 sigrdataset, tree,
1092 search.rbtversion->secure DNS__DB_FLARG_PASS);
1093 if (result == ISC_R_SUCCESS) {
1094 result = active ? DNS_R_EMPTYNAME
1095 : DNS_R_NXDOMAIN;
1096 }
1097 } else {
1098 result = active ? DNS_R_EMPTYNAME : DNS_R_NXDOMAIN;
1099 }
1100 goto tree_exit;
1101 } else if (result != ISC_R_SUCCESS) {
1102 goto tree_exit;
1103 }
1104
1105 found:
1106 /*
1107 * We have found a node whose name is the desired name, or we
1108 * have matched a wildcard.
1109 */
1110
1111 if (search.zonecut != NULL) {
1112 /*
1113 * If we're beneath a zone cut, we don't want to look for
1114 * CNAMEs because they're not legitimate zone glue.
1115 */
1116 cname_ok = false;
1117 } else {
1118 /*
1119 * The node may be a zone cut itself. If it might be one,
1120 * make sure we check for it later.
1121 *
1122 * DS records live above the zone cut in ordinary zone so
1123 * we want to ignore any referral.
1124 *
1125 * Stub zones don't have anything "above" the delegation so
1126 * we always return a referral.
1127 */
1128 if (node->find_callback &&
1129 ((node != search.rbtdb->origin_node &&
1130 !dns_rdatatype_atparent(type)) ||
1131 IS_STUB(search.rbtdb)))
1132 {
1133 maybe_zonecut = true;
1134 }
1135 }
1136
1137 /*
1138 * Certain DNSSEC types are not subject to CNAME matching
1139 * (RFC4035, section 2.5 and RFC3007).
1140 *
1141 * We don't check for RRSIG, because we don't store RRSIG records
1142 * directly.
1143 */
1144 if (type == dns_rdatatype_key || type == dns_rdatatype_nsec) {
1145 cname_ok = false;
1146 }
1147
1148 /*
1149 * We now go looking for rdata...
1150 */
1151
1152 lock = &search.rbtdb->node_locks[node->locknum].lock;
1153 NODE_RDLOCK(lock, &nlocktype);
1154
1155 found = NULL;
1156 foundsig = NULL;
1157 sigtype = DNS_SIGTYPE(type);
1158 nsecheader = NULL;
1159 nsecsig = NULL;
1160 cnamesig = NULL;
1161 empty_node = true;
1162 for (header = node->data; header != NULL; header = header_next) {
1163 header_next = header->next;
1164 /*
1165 * Look for an active, extant rdataset.
1166 */
1167 do {
1168 if (header->serial <= search.serial && !IGNORE(header))
1169 {
1170 /*
1171 * Is this a "this rdataset doesn't
1172 * exist" record?
1173 */
1174 if (NONEXISTENT(header)) {
1175 header = NULL;
1176 }
1177 break;
1178 } else {
1179 header = header->down;
1180 }
1181 } while (header != NULL);
1182 if (header != NULL) {
1183 /*
1184 * We now know that there is at least one active
1185 * rdataset at this node.
1186 */
1187 empty_node = false;
1188
1189 /*
1190 * Do special zone cut handling, if requested.
1191 */
1192 if (maybe_zonecut && header->type == dns_rdatatype_ns) {
1193 /*
1194 * We increment the reference count on node to
1195 * ensure that search->zonecut_header will
1196 * still be valid later.
1197 */
1198 dns__rbtdb_newref(search.rbtdb, node,
1199 nlocktype DNS__DB_FLARG_PASS);
1200 search.zonecut = node;
1201 search.zonecut_header = header;
1202 search.zonecut_sigheader = NULL;
1203 search.need_cleanup = true;
1204 maybe_zonecut = false;
1205 at_zonecut = true;
1206 /*
1207 * It is not clear if KEY should still be
1208 * allowed at the parent side of the zone
1209 * cut or not. It is needed for RFC3007
1210 * validated updates.
1211 */
1212 if ((search.options & DNS_DBFIND_GLUEOK) == 0 &&
1213 type != dns_rdatatype_nsec &&
1214 type != dns_rdatatype_key)
1215 {
1216 /*
1217 * Glue is not OK, but any answer we
1218 * could return would be glue. Return
1219 * the delegation.
1220 */
1221 found = NULL;
1222 break;
1223 }
1224 if (found != NULL && foundsig != NULL) {
1225 break;
1226 }
1227 }
1228
1229 /*
1230 * If the NSEC3 record doesn't match the chain
1231 * we are using behave as if it isn't here.
1232 */
1233 if (header->type == dns_rdatatype_nsec3 &&
1234 !matchparams(header, &search))
1235 {
1236 NODE_UNLOCK(lock, &nlocktype);
1237 goto partial_match;
1238 }
1239 /*
1240 * If we found a type we were looking for,
1241 * remember it.
1242 */
1243 if (header->type == type || type == dns_rdatatype_any ||
1244 (header->type == dns_rdatatype_cname && cname_ok))
1245 {
1246 /*
1247 * We've found the answer!
1248 */
1249 found = header;
1250 if (header->type == dns_rdatatype_cname &&
1251 cname_ok)
1252 {
1253 /*
1254 * We may be finding a CNAME instead
1255 * of the desired type.
1256 *
1257 * If we've already got the CNAME RRSIG,
1258 * use it, otherwise change sigtype
1259 * so that we find it.
1260 */
1261 if (cnamesig != NULL) {
1262 foundsig = cnamesig;
1263 } else {
1264 sigtype = DNS_SIGTYPE(
1265 dns_rdatatype_cname);
1266 }
1267 }
1268 /*
1269 * If we've got all we need, end the search.
1270 */
1271 if (!maybe_zonecut && foundsig != NULL) {
1272 break;
1273 }
1274 } else if (header->type == sigtype) {
1275 /*
1276 * We've found the RRSIG rdataset for our
1277 * target type. Remember it.
1278 */
1279 foundsig = header;
1280 /*
1281 * If we've got all we need, end the search.
1282 */
1283 if (!maybe_zonecut && found != NULL) {
1284 break;
1285 }
1286 } else if (header->type == dns_rdatatype_nsec &&
1287 !search.rbtversion->havensec3)
1288 {
1289 /*
1290 * Remember a NSEC rdataset even if we're
1291 * not specifically looking for it, because
1292 * we might need it later.
1293 */
1294 nsecheader = header;
1295 } else if (header->type ==
1296 DNS_SIGTYPE(dns_rdatatype_nsec) &&
1297 !search.rbtversion->havensec3)
1298 {
1299 /*
1300 * If we need the NSEC rdataset, we'll also
1301 * need its signature.
1302 */
1303 nsecsig = header;
1304 } else if (cname_ok &&
1305 header->type ==
1306 DNS_SIGTYPE(dns_rdatatype_cname))
1307 {
1308 /*
1309 * If we get a CNAME match, we'll also need
1310 * its signature.
1311 */
1312 cnamesig = header;
1313 }
1314 }
1315 }
1316
1317 if (empty_node) {
1318 /*
1319 * We have an exact match for the name, but there are no
1320 * active rdatasets in the desired version. That means that
1321 * this node doesn't exist in the desired version, and that
1322 * we really have a partial match.
1323 */
1324 if (!wild) {
1325 NODE_UNLOCK(lock, &nlocktype);
1326 goto partial_match;
1327 }
1328 }
1329
1330 /*
1331 * If we didn't find what we were looking for...
1332 */
1333 if (found == NULL) {
1334 if (search.zonecut != NULL) {
1335 /*
1336 * We were trying to find glue at a node beneath a
1337 * zone cut, but didn't.
1338 *
1339 * Return the delegation.
1340 */
1341 NODE_UNLOCK(lock, &nlocktype);
1342 result = setup_delegation(
1343 &search, nodep, foundname, rdataset,
1344 sigrdataset DNS__DB_FLARG_PASS);
1345 goto tree_exit;
1346 }
1347 /*
1348 * The desired type doesn't exist.
1349 */
1350 result = DNS_R_NXRRSET;
1351 if (search.rbtversion->secure &&
1352 !search.rbtversion->havensec3 &&
1353 (nsecheader == NULL || nsecsig == NULL))
1354 {
1355 /*
1356 * The zone is secure but there's no NSEC,
1357 * or the NSEC has no signature!
1358 */
1359 if (!wild) {
1360 result = DNS_R_BADDB;
1361 goto node_exit;
1362 }
1363
1364 NODE_UNLOCK(lock, &nlocktype);
1365 result = find_closest_nsec(
1366 &search, nodep, foundname, rdataset,
1367 sigrdataset, search.rbtdb->tree,
1368 search.rbtversion->secure DNS__DB_FLARG_PASS);
1369 if (result == ISC_R_SUCCESS) {
1370 result = DNS_R_EMPTYWILD;
1371 }
1372 goto tree_exit;
1373 }
1374 if (nodep != NULL) {
1375 dns__rbtdb_newref(search.rbtdb, node,
1376 nlocktype DNS__DB_FLARG_PASS);
1377 *nodep = node;
1378 }
1379 if (search.rbtversion->secure && !search.rbtversion->havensec3)
1380 {
1381 dns__rbtdb_bindrdataset(search.rbtdb, node, nsecheader,
1382 0, nlocktype,
1383 rdataset DNS__DB_FLARG_PASS);
1384 if (nsecsig != NULL) {
1385 dns__rbtdb_bindrdataset(
1386 search.rbtdb, node, nsecsig, 0,
1387 nlocktype,
1388 sigrdataset DNS__DB_FLARG_PASS);
1389 }
1390 }
1391 if (wild) {
1392 foundname->attributes.wildcard = true;
1393 }
1394 goto node_exit;
1395 }
1396
1397 /*
1398 * We found what we were looking for, or we found a CNAME.
1399 */
1400
1401 if (type != found->type && type != dns_rdatatype_any &&
1402 found->type == dns_rdatatype_cname)
1403 {
1404 /*
1405 * We weren't doing an ANY query and we found a CNAME instead
1406 * of the type we were looking for, so we need to indicate
1407 * that result to the caller.
1408 */
1409 result = DNS_R_CNAME;
1410 } else if (search.zonecut != NULL) {
1411 /*
1412 * If we're beneath a zone cut, we must indicate that the
1413 * result is glue, unless we're actually at the zone cut
1414 * and the type is NSEC or KEY.
1415 */
1416 if (search.zonecut == node) {
1417 /*
1418 * It is not clear if KEY should still be
1419 * allowed at the parent side of the zone
1420 * cut or not. It is needed for RFC3007
1421 * validated updates.
1422 */
1423 if (type == dns_rdatatype_nsec ||
1424 type == dns_rdatatype_nsec3 ||
1425 type == dns_rdatatype_key)
1426 {
1427 result = ISC_R_SUCCESS;
1428 } else if (type == dns_rdatatype_any) {
1429 result = DNS_R_ZONECUT;
1430 } else {
1431 result = DNS_R_GLUE;
1432 }
1433 } else {
1434 result = DNS_R_GLUE;
1435 }
1436 } else {
1437 /*
1438 * An ordinary successful query!
1439 */
1440 result = ISC_R_SUCCESS;
1441 }
1442
1443 if (nodep != NULL) {
1444 if (!at_zonecut) {
1445 dns__rbtdb_newref(search.rbtdb, node,
1446 nlocktype DNS__DB_FLARG_PASS);
1447 } else {
1448 search.need_cleanup = false;
1449 }
1450 *nodep = node;
1451 }
1452
1453 if (type != dns_rdatatype_any) {
1454 dns__rbtdb_bindrdataset(search.rbtdb, node, found, 0, nlocktype,
1455 rdataset DNS__DB_FLARG_PASS);
1456 if (foundsig != NULL) {
1457 dns__rbtdb_bindrdataset(search.rbtdb, node, foundsig, 0,
1458 nlocktype,
1459 sigrdataset DNS__DB_FLARG_PASS);
1460 }
1461 }
1462
1463 if (wild) {
1464 foundname->attributes.wildcard = true;
1465 }
1466
1467 node_exit:
1468 NODE_UNLOCK(lock, &nlocktype);
1469
1470 tree_exit:
1471 TREE_UNLOCK(&search.rbtdb->tree_lock, &tlocktype);
1472
1473 /*
1474 * If we found a zonecut but aren't going to use it, we have to
1475 * let go of it.
1476 */
1477 if (search.need_cleanup) {
1478 node = search.zonecut;
1479 INSIST(node != NULL);
1480 lock = &(search.rbtdb->node_locks[node->locknum].lock);
1481
1482 NODE_RDLOCK(lock, &nlocktype);
1483 dns__rbtdb_decref(search.rbtdb, node, 0, &nlocktype, &tlocktype,
1484 true, false DNS__DB_FLARG_PASS);
1485 NODE_UNLOCK(lock, &nlocktype);
1486 INSIST(tlocktype == isc_rwlocktype_none);
1487 }
1488
1489 if (close_version) {
1490 dns__rbtdb_closeversion(db, &version, false DNS__DB_FLARG_PASS);
1491 }
1492
1493 dns_rbtnodechain_reset(&search.chain);
1494
1495 return result;
1496 }
1497
1498 static isc_result_t
1499 zone_findrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
1500 dns_rdatatype_t type, dns_rdatatype_t covers,
1501 isc_stdtime_t now, dns_rdataset_t *rdataset,
1502 dns_rdataset_t *sigrdataset DNS__DB_FLARG) {
1503 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
1504 dns_rbtnode_t *rbtnode = (dns_rbtnode_t *)node;
1505 dns_slabheader_t *header = NULL, *header_next = NULL;
1506 dns_slabheader_t *found = NULL, *foundsig = NULL;
1507 uint32_t serial;
1508 dns_rbtdb_version_t *rbtversion = version;
1509 bool close_version = false;
1510 dns_typepair_t matchtype, sigmatchtype;
1511 isc_rwlocktype_t nlocktype = isc_rwlocktype_none;
1512
1513 REQUIRE(VALID_RBTDB(rbtdb));
1514 REQUIRE(type != dns_rdatatype_any);
1515 INSIST(rbtversion == NULL || rbtversion->rbtdb == rbtdb);
1516
1517 if (rbtversion == NULL) {
1518 dns__rbtdb_currentversion(
1519 db, (dns_dbversion_t **)(void *)(&rbtversion));
1520 close_version = true;
1521 }
1522 serial = rbtversion->serial;
1523 now = 0;
1524
1525 NODE_RDLOCK(&rbtdb->node_locks[rbtnode->locknum].lock, &nlocktype);
1526
1527 matchtype = DNS_TYPEPAIR_VALUE(type, covers);
1528 if (covers == 0) {
1529 sigmatchtype = DNS_SIGTYPE(type);
1530 } else {
1531 sigmatchtype = 0;
1532 }
1533
1534 for (header = rbtnode->data; header != NULL; header = header_next) {
1535 header_next = header->next;
1536 do {
1537 if (header->serial <= serial && !IGNORE(header)) {
1538 /*
1539 * Is this a "this rdataset doesn't
1540 * exist" record?
1541 */
1542 if (NONEXISTENT(header)) {
1543 header = NULL;
1544 }
1545 break;
1546 } else {
1547 header = header->down;
1548 }
1549 } while (header != NULL);
1550 if (header != NULL) {
1551 /*
1552 * We have an active, extant rdataset. If it's a
1553 * type we're looking for, remember it.
1554 */
1555 if (header->type == matchtype) {
1556 found = header;
1557 if (foundsig != NULL) {
1558 break;
1559 }
1560 } else if (header->type == sigmatchtype) {
1561 foundsig = header;
1562 if (found != NULL) {
1563 break;
1564 }
1565 }
1566 }
1567 }
1568 if (found != NULL) {
1569 dns__rbtdb_bindrdataset(rbtdb, rbtnode, found, now,
1570 isc_rwlocktype_read,
1571 rdataset DNS__DB_FLARG_PASS);
1572 if (foundsig != NULL) {
1573 dns__rbtdb_bindrdataset(rbtdb, rbtnode, foundsig, now,
1574 isc_rwlocktype_read,
1575 sigrdataset DNS__DB_FLARG_PASS);
1576 }
1577 }
1578
1579 NODE_UNLOCK(&rbtdb->node_locks[rbtnode->locknum].lock, &nlocktype);
1580
1581 if (close_version) {
1582 dns__rbtdb_closeversion(
1583 db, (dns_dbversion_t **)(void *)(&rbtversion),
1584 false DNS__DB_FLARG_PASS);
1585 }
1586
1587 if (found == NULL) {
1588 return ISC_R_NOTFOUND;
1589 }
1590
1591 return ISC_R_SUCCESS;
1592 }
1593
1594 static bool
1595 delegating_type(dns_rbtdb_t *rbtdb, dns_rbtnode_t *node, dns_typepair_t type) {
1596 if (type == dns_rdatatype_dname ||
1597 (type == dns_rdatatype_ns &&
1598 (node != rbtdb->origin_node || IS_STUB(rbtdb))))
1599 {
1600 return true;
1601 }
1602 return false;
1603 }
1604
1605 /*
1606 * load a non-NSEC3 node in the main tree and optionally to the auxiliary NSEC
1607 */
1608 static isc_result_t
1609 loadnode(dns_rbtdb_t *rbtdb, const dns_name_t *name, dns_rbtnode_t **nodep,
1610 bool hasnsec) {
1611 isc_result_t noderesult, nsecresult, tmpresult;
1612 dns_rbtnode_t *nsecnode = NULL, *node = NULL;
1613
1614 noderesult = dns_rbt_addnode(rbtdb->tree, name, &node);
1615 if (!hasnsec) {
1616 goto done;
1617 }
1618 if (noderesult == ISC_R_EXISTS) {
1619 /*
1620 * Add a node to the auxiliary NSEC tree for an old node
1621 * just now getting an NSEC record.
1622 */
1623 if (node->nsec == DNS_DB_NSEC_HAS_NSEC) {
1624 goto done;
1625 }
1626 } else if (noderesult != ISC_R_SUCCESS) {
1627 goto done;
1628 }
1629
1630 /*
1631 * Build the auxiliary tree for NSECs as we go.
1632 * This tree speeds searches for closest NSECs that would otherwise
1633 * need to examine many irrelevant nodes in large TLDs.
1634 *
1635 * Add nodes to the auxiliary tree after corresponding nodes have
1636 * been added to the main tree.
1637 */
1638 nsecresult = dns_rbt_addnode(rbtdb->nsec, name, &nsecnode);
1639 if (nsecresult == ISC_R_SUCCESS) {
1640 nsecnode->nsec = DNS_DB_NSEC_NSEC;
1641 node->nsec = DNS_DB_NSEC_HAS_NSEC;
1642 goto done;
1643 }
1644
1645 if (nsecresult == ISC_R_EXISTS) {
1646 #if 1 /* 0 */
1647 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
1648 DNS_LOGMODULE_CACHE, ISC_LOG_WARNING,
1649 "addnode: NSEC node already exists");
1650 #endif /* if 1 */
1651 node->nsec = DNS_DB_NSEC_HAS_NSEC;
1652 goto done;
1653 }
1654
1655 if (noderesult == ISC_R_SUCCESS) {
1656 /*
1657 * Remove the node we just added above.
1658 */
1659 tmpresult = dns_rbt_deletenode(rbtdb->tree, node, false);
1660 if (tmpresult != ISC_R_SUCCESS) {
1661 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
1662 DNS_LOGMODULE_CACHE, ISC_LOG_WARNING,
1663 "loading_addrdataset: "
1664 "dns_rbt_deletenode: %s after "
1665 "dns_rbt_addnode(NSEC): %s",
1666 isc_result_totext(tmpresult),
1667 isc_result_totext(noderesult));
1668 }
1669 }
1670
1671 /*
1672 * Set the error condition to be returned.
1673 */
1674 noderesult = nsecresult;
1675
1676 done:
1677 if (noderesult == ISC_R_SUCCESS || noderesult == ISC_R_EXISTS) {
1678 *nodep = node;
1679 }
1680
1681 return noderesult;
1682 }
1683
1684 static isc_result_t
1685 loading_addrdataset(void *arg, const dns_name_t *name,
1686 dns_rdataset_t *rdataset DNS__DB_FLARG) {
1687 rbtdb_load_t *loadctx = arg;
1688 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)loadctx->db;
1689 dns_rbtnode_t *node = NULL;
1690 isc_result_t result;
1691 isc_region_t region;
1692 dns_slabheader_t *newheader = NULL;
1693 isc_rwlocktype_t nlocktype = isc_rwlocktype_none;
1694
1695 REQUIRE(rdataset->rdclass == rbtdb->common.rdclass);
1696
1697 /*
1698 * SOA records are only allowed at top of zone.
1699 */
1700 if (rdataset->type == dns_rdatatype_soa &&
1701 !dns_name_equal(name, &rbtdb->common.origin))
1702 {
1703 return DNS_R_NOTZONETOP;
1704 }
1705
1706 if (rdataset->type != dns_rdatatype_nsec3 &&
1707 rdataset->covers != dns_rdatatype_nsec3)
1708 {
1709 dns__zonerbt_addwildcards(rbtdb, name, false);
1710 }
1711
1712 if (dns_name_iswildcard(name)) {
1713 /*
1714 * NS record owners cannot legally be wild cards.
1715 */
1716 if (rdataset->type == dns_rdatatype_ns) {
1717 return DNS_R_INVALIDNS;
1718 }
1719 /*
1720 * NSEC3 record owners cannot legally be wild cards.
1721 */
1722 if (rdataset->type == dns_rdatatype_nsec3) {
1723 return DNS_R_INVALIDNSEC3;
1724 }
1725 result = dns__zonerbt_wildcardmagic(rbtdb, name, false);
1726 if (result != ISC_R_SUCCESS) {
1727 return result;
1728 }
1729 }
1730
1731 if (rdataset->type == dns_rdatatype_nsec3 ||
1732 rdataset->covers == dns_rdatatype_nsec3)
1733 {
1734 result = dns_rbt_addnode(rbtdb->nsec3, name, &node);
1735 if (result == ISC_R_SUCCESS) {
1736 node->nsec = DNS_DB_NSEC_NSEC3;
1737 }
1738 } else if (rdataset->type == dns_rdatatype_nsec) {
1739 result = loadnode(rbtdb, name, &node, true);
1740 } else {
1741 result = loadnode(rbtdb, name, &node, false);
1742 }
1743 if (result != ISC_R_SUCCESS && result != ISC_R_EXISTS) {
1744 return result;
1745 }
1746 if (result == ISC_R_SUCCESS) {
1747 node->locknum = node->hashval % rbtdb->node_lock_count;
1748 }
1749
1750 result = dns_rdataslab_fromrdataset(rdataset, rbtdb->common.mctx,
1751 &region, sizeof(dns_slabheader_t),
1752 rbtdb->maxrrperset);
1753 if (result != ISC_R_SUCCESS) {
1754 if (result == DNS_R_TOOMANYRECORDS) {
1755 dns__db_logtoomanyrecords((dns_db_t *)rbtdb, name,
1756 rdataset->type, "adding",
1757 rbtdb->maxrrperset);
1758 }
1759 return result;
1760 }
1761 newheader = (dns_slabheader_t *)region.base;
1762 *newheader = (dns_slabheader_t){
1763 .type = DNS_TYPEPAIR_VALUE(rdataset->type, rdataset->covers),
1764 .ttl = rdataset->ttl + loadctx->now,
1765 .trust = rdataset->trust,
1766 .node = node,
1767 .serial = 1,
1768 .count = 1,
1769 };
1770
1771 dns_slabheader_reset(newheader, (dns_db_t *)rbtdb, node);
1772 dns_slabheader_setownercase(newheader, name);
1773
1774 if ((rdataset->attributes & DNS_RDATASETATTR_RESIGN) != 0) {
1775 DNS_SLABHEADER_SETATTR(newheader, DNS_SLABHEADERATTR_RESIGN);
1776 newheader->resign =
1777 (isc_stdtime_t)(dns_time64_from32(rdataset->resign) >>
1778 1);
1779 newheader->resign_lsb = rdataset->resign & 0x1;
1780 }
1781
1782 NODE_WRLOCK(&rbtdb->node_locks[node->locknum].lock, &nlocktype);
1783 result = dns__rbtdb_add(rbtdb, node, name, rbtdb->current_version,
1784 newheader, DNS_DBADD_MERGE, true, NULL,
1785 0 DNS__DB_FLARG_PASS);
1786 NODE_UNLOCK(&rbtdb->node_locks[node->locknum].lock, &nlocktype);
1787
1788 if (result == ISC_R_SUCCESS &&
1789 delegating_type(rbtdb, node, rdataset->type))
1790 {
1791 node->find_callback = 1;
1792 } else if (result == DNS_R_UNCHANGED) {
1793 result = ISC_R_SUCCESS;
1794 }
1795
1796 return result;
1797 }
1798
1799 static isc_result_t
1800 beginload(dns_db_t *db, dns_rdatacallbacks_t *callbacks) {
1801 rbtdb_load_t *loadctx = NULL;
1802 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
1803
1804 REQUIRE(DNS_CALLBACK_VALID(callbacks));
1805 REQUIRE(VALID_RBTDB(rbtdb));
1806
1807 loadctx = isc_mem_get(rbtdb->common.mctx, sizeof(*loadctx));
1808
1809 loadctx->db = db;
1810 loadctx->now = 0;
1811
1812 RWLOCK(&rbtdb->lock, isc_rwlocktype_write);
1813
1814 REQUIRE((rbtdb->attributes &
1815 (RBTDB_ATTR_LOADED | RBTDB_ATTR_LOADING)) == 0);
1816 rbtdb->attributes |= RBTDB_ATTR_LOADING;
1817
1818 RWUNLOCK(&rbtdb->lock, isc_rwlocktype_write);
1819
1820 callbacks->add = loading_addrdataset;
1821 callbacks->add_private = loadctx;
1822
1823 return ISC_R_SUCCESS;
1824 }
1825
1826 static isc_result_t
1827 endload(dns_db_t *db, dns_rdatacallbacks_t *callbacks) {
1828 rbtdb_load_t *loadctx = NULL;
1829 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
1830
1831 REQUIRE(VALID_RBTDB(rbtdb));
1832 REQUIRE(DNS_CALLBACK_VALID(callbacks));
1833 loadctx = callbacks->add_private;
1834 REQUIRE(loadctx != NULL);
1835 REQUIRE(loadctx->db == db);
1836
1837 RWLOCK(&rbtdb->lock, isc_rwlocktype_write);
1838
1839 REQUIRE((rbtdb->attributes & RBTDB_ATTR_LOADING) != 0);
1840 REQUIRE((rbtdb->attributes & RBTDB_ATTR_LOADED) == 0);
1841
1842 rbtdb->attributes &= ~RBTDB_ATTR_LOADING;
1843 rbtdb->attributes |= RBTDB_ATTR_LOADED;
1844
1845 /*
1846 * If there's a KEY rdataset at the zone origin containing a
1847 * zone key, we consider the zone secure.
1848 */
1849 if (rbtdb->origin_node != NULL) {
1850 dns_dbversion_t *version = rbtdb->current_version;
1851 RWUNLOCK(&rbtdb->lock, isc_rwlocktype_write);
1852 dns__rbtdb_setsecure(db, version, rbtdb->origin_node);
1853 } else {
1854 RWUNLOCK(&rbtdb->lock, isc_rwlocktype_write);
1855 }
1856
1857 callbacks->add = NULL;
1858 callbacks->add_private = NULL;
1859
1860 isc_mem_put(rbtdb->common.mctx, loadctx, sizeof(*loadctx));
1861
1862 return ISC_R_SUCCESS;
1863 }
1864
1865 static bool
1866 issecure(dns_db_t *db) {
1867 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
1868 bool secure;
1869
1870 REQUIRE(VALID_RBTDB(rbtdb));
1871
1872 RWLOCK(&rbtdb->lock, isc_rwlocktype_read);
1873 secure = rbtdb->current_version->secure;
1874 RWUNLOCK(&rbtdb->lock, isc_rwlocktype_read);
1875
1876 return secure;
1877 }
1878
1879 static isc_result_t
1880 getnsec3parameters(dns_db_t *db, dns_dbversion_t *version, dns_hash_t *hash,
1881 uint8_t *flags, uint16_t *iterations, unsigned char *salt,
1882 size_t *salt_length) {
1883 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
1884 isc_result_t result = ISC_R_NOTFOUND;
1885 dns_rbtdb_version_t *rbtversion = version;
1886
1887 REQUIRE(VALID_RBTDB(rbtdb));
1888 INSIST(rbtversion == NULL || rbtversion->rbtdb == rbtdb);
1889
1890 RWLOCK(&rbtdb->lock, isc_rwlocktype_read);
1891 if (rbtversion == NULL) {
1892 rbtversion = rbtdb->current_version;
1893 }
1894
1895 if (rbtversion->havensec3) {
1896 if (hash != NULL) {
1897 *hash = rbtversion->hash;
1898 }
1899 if (salt != NULL && salt_length != NULL) {
1900 REQUIRE(*salt_length >= rbtversion->salt_length);
1901 memmove(salt, rbtversion->salt,
1902 rbtversion->salt_length);
1903 }
1904 if (salt_length != NULL) {
1905 *salt_length = rbtversion->salt_length;
1906 }
1907 if (iterations != NULL) {
1908 *iterations = rbtversion->iterations;
1909 }
1910 if (flags != NULL) {
1911 *flags = rbtversion->flags;
1912 }
1913 result = ISC_R_SUCCESS;
1914 }
1915 RWUNLOCK(&rbtdb->lock, isc_rwlocktype_read);
1916
1917 return result;
1918 }
1919
1920 static isc_result_t
1921 getsize(dns_db_t *db, dns_dbversion_t *version, uint64_t *records,
1922 uint64_t *xfrsize) {
1923 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
1924 isc_result_t result = ISC_R_SUCCESS;
1925 dns_rbtdb_version_t *rbtversion = version;
1926
1927 REQUIRE(VALID_RBTDB(rbtdb));
1928 INSIST(rbtversion == NULL || rbtversion->rbtdb == rbtdb);
1929
1930 RWLOCK(&rbtdb->lock, isc_rwlocktype_read);
1931 if (rbtversion == NULL) {
1932 rbtversion = rbtdb->current_version;
1933 }
1934
1935 RWLOCK(&rbtversion->rwlock, isc_rwlocktype_read);
1936 SET_IF_NOT_NULL(records, rbtversion->records);
1937
1938 SET_IF_NOT_NULL(xfrsize, rbtversion->xfrsize);
1939 RWUNLOCK(&rbtversion->rwlock, isc_rwlocktype_read);
1940 RWUNLOCK(&rbtdb->lock, isc_rwlocktype_read);
1941
1942 return result;
1943 }
1944
1945 static isc_result_t
1946 setsigningtime(dns_db_t *db, dns_rdataset_t *rdataset, isc_stdtime_t resign) {
1947 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
1948 dns_slabheader_t *header, oldheader;
1949 isc_rwlocktype_t nlocktype = isc_rwlocktype_none;
1950
1951 REQUIRE(VALID_RBTDB(rbtdb));
1952 REQUIRE(!IS_CACHE(rbtdb));
1953 REQUIRE(rdataset != NULL);
1954 REQUIRE(rdataset->methods == &dns_rdataslab_rdatasetmethods);
1955
1956 header = dns_slabheader_fromrdataset(rdataset);
1957
1958 NODE_WRLOCK(&rbtdb->node_locks[RBTDB_HEADERNODE(header)->locknum].lock,
1959 &nlocktype);
1960
1961 oldheader = *header;
1962
1963 /*
1964 * Only break the heap invariant (by adjusting resign and resign_lsb)
1965 * if we are going to be restoring it by calling isc_heap_increased
1966 * or isc_heap_decreased.
1967 */
1968 if (resign != 0) {
1969 header->resign = (isc_stdtime_t)(dns_time64_from32(resign) >>
1970 1);
1971 header->resign_lsb = resign & 0x1;
1972 }
1973 if (header->heap_index != 0) {
1974 INSIST(RESIGN(header));
1975 if (resign == 0) {
1976 isc_heap_delete(
1977 rbtdb->heaps[RBTDB_HEADERNODE(header)->locknum],
1978 header->heap_index);
1979 header->heap_index = 0;
1980 header->heap = NULL;
1981 } else if (rbtdb->sooner(header, &oldheader)) {
1982 isc_heap_increased(
1983 rbtdb->heaps[RBTDB_HEADERNODE(header)->locknum],
1984 header->heap_index);
1985 } else if (rbtdb->sooner(&oldheader, header)) {
1986 isc_heap_decreased(
1987 rbtdb->heaps[RBTDB_HEADERNODE(header)->locknum],
1988 header->heap_index);
1989 }
1990 } else if (resign != 0) {
1991 DNS_SLABHEADER_SETATTR(header, DNS_SLABHEADERATTR_RESIGN);
1992 dns__zonerbt_resigninsert(
1993 rbtdb, RBTDB_HEADERNODE(header)->locknum, header);
1994 }
1995 NODE_UNLOCK(&rbtdb->node_locks[RBTDB_HEADERNODE(header)->locknum].lock,
1996 &nlocktype);
1997 return ISC_R_SUCCESS;
1998 }
1999
2000 static isc_result_t
2001 getsigningtime(dns_db_t *db, isc_stdtime_t *resign, dns_name_t *foundname,
2002 dns_typepair_t *typepair) {
2003 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
2004 dns_slabheader_t *header = NULL, *this = NULL;
2005 unsigned int i;
2006 isc_result_t result = ISC_R_NOTFOUND;
2007 unsigned int locknum = 0;
2008 isc_rwlocktype_t tlocktype = isc_rwlocktype_none;
2009 isc_rwlocktype_t nlocktype = isc_rwlocktype_none;
2010
2011 REQUIRE(VALID_RBTDB(rbtdb));
2012 REQUIRE(resign != NULL);
2013 REQUIRE(foundname != NULL);
2014 REQUIRE(typepair != NULL);
2015
2016 TREE_RDLOCK(&rbtdb->tree_lock, &tlocktype);
2017
2018 for (i = 0; i < rbtdb->node_lock_count; i++) {
2019 NODE_RDLOCK(&rbtdb->node_locks[i].lock, &nlocktype);
2020
2021 /*
2022 * Find for the earliest signing time among all of the
2023 * heaps, each of which is covered by a different bucket
2024 * lock.
2025 */
2026 this = isc_heap_element(rbtdb->heaps[i], 1);
2027 if (this == NULL) {
2028 /* Nothing found; unlock and try the next heap. */
2029 NODE_UNLOCK(&rbtdb->node_locks[i].lock, &nlocktype);
2030 continue;
2031 }
2032
2033 if (header == NULL) {
2034 /*
2035 * Found a signing time: retain the bucket lock and
2036 * preserve the lock number so we can unlock it
2037 * later.
2038 */
2039 header = this;
2040 locknum = i;
2041 nlocktype = isc_rwlocktype_none;
2042 } else if (rbtdb->sooner(this, header)) {
2043 /*
2044 * Found an earlier signing time; release the
2045 * previous bucket lock and retain this one instead.
2046 */
2047 NODE_UNLOCK(&rbtdb->node_locks[locknum].lock,
2048 &nlocktype);
2049 header = this;
2050 locknum = i;
2051 } else {
2052 /*
2053 * Earliest signing time in this heap isn't
2054 * an improvement; unlock and try the next heap.
2055 */
2056 NODE_UNLOCK(&rbtdb->node_locks[i].lock, &nlocktype);
2057 }
2058 }
2059
2060 if (header != NULL) {
2061 nlocktype = isc_rwlocktype_read;
2062 /*
2063 * Found something; pass back the answer and unlock
2064 * the bucket.
2065 */
2066 *resign = RESIGN(header)
2067 ? (header->resign << 1) | header->resign_lsb
2068 : 0;
2069 dns_rbt_fullnamefromnode(RBTDB_HEADERNODE(header), foundname);
2070 *typepair = header->type;
2071
2072 NODE_UNLOCK(&rbtdb->node_locks[locknum].lock, &nlocktype);
2073
2074 result = ISC_R_SUCCESS;
2075 }
2076
2077 TREE_UNLOCK(&rbtdb->tree_lock, &tlocktype);
2078
2079 return result;
2080 }
2081
2082 static isc_result_t
2083 setgluecachestats(dns_db_t *db, isc_stats_t *stats) {
2084 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
2085
2086 REQUIRE(VALID_RBTDB(rbtdb));
2087 REQUIRE(!IS_CACHE(rbtdb) && !IS_STUB(rbtdb));
2088 REQUIRE(stats != NULL);
2089
2090 isc_stats_attach(stats, &rbtdb->gluecachestats);
2091 return ISC_R_SUCCESS;
2092 }
2093
2094 static dns_glue_t *
2095 new_gluelist(isc_mem_t *mctx, dns_name_t *name) {
2096 dns_glue_t *glue = isc_mem_get(mctx, sizeof(*glue));
2097 *glue = (dns_glue_t){ 0 };
2098 dns_name_t *gluename = dns_fixedname_initname(&glue->fixedname);
2099
2100 dns_name_copy(name, gluename);
2101
2102 return glue;
2103 }
2104
2105 static isc_result_t
2106 glue_nsdname_cb(void *arg, const dns_name_t *name, dns_rdatatype_t qtype,
2107 dns_rdataset_t *unused DNS__DB_FLARG) {
2108 dns_glue_additionaldata_ctx_t *ctx = NULL;
2109 isc_result_t result;
2110 dns_fixedname_t fixedname_a;
2111 dns_name_t *name_a = NULL;
2112 dns_rdataset_t rdataset_a, sigrdataset_a;
2113 dns_rbtnode_t *node_a = NULL;
2114 dns_fixedname_t fixedname_aaaa;
2115 dns_name_t *name_aaaa = NULL;
2116 dns_rdataset_t rdataset_aaaa, sigrdataset_aaaa;
2117 dns_rbtnode_t *node_aaaa = NULL;
2118 dns_glue_t *glue = NULL;
2119
2120 UNUSED(unused);
2121
2122 /*
2123 * NS records want addresses in additional records.
2124 */
2125 INSIST(qtype == dns_rdatatype_a);
2126
2127 ctx = (dns_glue_additionaldata_ctx_t *)arg;
2128
2129 name_a = dns_fixedname_initname(&fixedname_a);
2130 dns_rdataset_init(&rdataset_a);
2131 dns_rdataset_init(&sigrdataset_a);
2132
2133 name_aaaa = dns_fixedname_initname(&fixedname_aaaa);
2134 dns_rdataset_init(&rdataset_aaaa);
2135 dns_rdataset_init(&sigrdataset_aaaa);
2136
2137 result = zone_find(ctx->db, name, ctx->version, dns_rdatatype_a,
2138 DNS_DBFIND_GLUEOK, 0, (dns_dbnode_t **)&node_a,
2139 name_a, &rdataset_a,
2140 &sigrdataset_a DNS__DB_FLARG_PASS);
2141 if (result == DNS_R_GLUE) {
2142 glue = new_gluelist(ctx->db->mctx, name_a);
2143
2144 dns_rdataset_init(&glue->rdataset_a);
2145 dns_rdataset_init(&glue->sigrdataset_a);
2146 dns_rdataset_init(&glue->rdataset_aaaa);
2147 dns_rdataset_init(&glue->sigrdataset_aaaa);
2148
2149 dns_rdataset_clone(&rdataset_a, &glue->rdataset_a);
2150 if (dns_rdataset_isassociated(&sigrdataset_a)) {
2151 dns_rdataset_clone(&sigrdataset_a,
2152 &glue->sigrdataset_a);
2153 }
2154 }
2155
2156 result = zone_find(ctx->db, name, ctx->version, dns_rdatatype_aaaa,
2157 DNS_DBFIND_GLUEOK, 0, (dns_dbnode_t **)&node_aaaa,
2158 name_aaaa, &rdataset_aaaa,
2159 &sigrdataset_aaaa DNS__DB_FLARG_PASS);
2160 if (result == DNS_R_GLUE) {
2161 if (glue == NULL) {
2162 glue = new_gluelist(ctx->db->mctx, name_aaaa);
2163
2164 dns_rdataset_init(&glue->rdataset_a);
2165 dns_rdataset_init(&glue->sigrdataset_a);
2166 dns_rdataset_init(&glue->rdataset_aaaa);
2167 dns_rdataset_init(&glue->sigrdataset_aaaa);
2168 } else {
2169 INSIST(node_a == node_aaaa);
2170 INSIST(dns_name_equal(name_a, name_aaaa));
2171 }
2172
2173 dns_rdataset_clone(&rdataset_aaaa, &glue->rdataset_aaaa);
2174 if (dns_rdataset_isassociated(&sigrdataset_aaaa)) {
2175 dns_rdataset_clone(&sigrdataset_aaaa,
2176 &glue->sigrdataset_aaaa);
2177 }
2178 }
2179
2180 /*
2181 * If the currently processed NS record is in-bailiwick, mark any glue
2182 * RRsets found for it with DNS_RDATASETATTR_REQUIRED. Note that for
2183 * simplicity, glue RRsets for all in-bailiwick NS records are marked
2184 * this way, even though dns_message_rendersection() only checks the
2185 * attributes for the first rdataset associated with the first name
2186 * added to the ADDITIONAL section.
2187 */
2188 if (glue != NULL && dns_name_issubdomain(name, ctx->nodename)) {
2189 if (dns_rdataset_isassociated(&glue->rdataset_a)) {
2190 glue->rdataset_a.attributes |=
2191 DNS_RDATASETATTR_REQUIRED;
2192 }
2193 if (dns_rdataset_isassociated(&glue->rdataset_aaaa)) {
2194 glue->rdataset_aaaa.attributes |=
2195 DNS_RDATASETATTR_REQUIRED;
2196 }
2197 }
2198
2199 if (glue != NULL) {
2200 glue->next = ctx->glue_list;
2201 ctx->glue_list = glue;
2202 }
2203
2204 result = ISC_R_SUCCESS;
2205
2206 if (dns_rdataset_isassociated(&rdataset_a)) {
2207 dns_rdataset_disassociate(&rdataset_a);
2208 }
2209 if (dns_rdataset_isassociated(&sigrdataset_a)) {
2210 dns_rdataset_disassociate(&sigrdataset_a);
2211 }
2212
2213 if (dns_rdataset_isassociated(&rdataset_aaaa)) {
2214 dns_rdataset_disassociate(&rdataset_aaaa);
2215 }
2216 if (dns_rdataset_isassociated(&sigrdataset_aaaa)) {
2217 dns_rdataset_disassociate(&sigrdataset_aaaa);
2218 }
2219
2220 if (node_a != NULL) {
2221 dns__db_detachnode(ctx->db,
2222 (dns_dbnode_t *)&node_a DNS__DB_FLARG_PASS);
2223 }
2224 if (node_aaaa != NULL) {
2225 dns__db_detachnode(
2226 ctx->db, (dns_dbnode_t *)&node_aaaa DNS__DB_FLARG_PASS);
2227 }
2228
2229 return result;
2230 }
2231
2232 #define IS_REQUIRED_GLUE(r) (((r)->attributes & DNS_RDATASETATTR_REQUIRED) != 0)
2233
2234 static void
2235 addglue_to_message(dns_glue_t *ge, dns_message_t *msg) {
2236 for (; ge != NULL; ge = ge->next) {
2237 dns_name_t *name = NULL;
2238 dns_rdataset_t *rdataset_a = NULL;
2239 dns_rdataset_t *sigrdataset_a = NULL;
2240 dns_rdataset_t *rdataset_aaaa = NULL;
2241 dns_rdataset_t *sigrdataset_aaaa = NULL;
2242 dns_name_t *gluename = dns_fixedname_name(&ge->fixedname);
2243 bool prepend_name = false;
2244
2245 dns_message_gettempname(msg, &name);
2246
2247 dns_name_copy(gluename, name);
2248
2249 if (dns_rdataset_isassociated(&ge->rdataset_a)) {
2250 dns_message_gettemprdataset(msg, &rdataset_a);
2251 }
2252
2253 if (dns_rdataset_isassociated(&ge->sigrdataset_a)) {
2254 dns_message_gettemprdataset(msg, &sigrdataset_a);
2255 }
2256
2257 if (dns_rdataset_isassociated(&ge->rdataset_aaaa)) {
2258 dns_message_gettemprdataset(msg, &rdataset_aaaa);
2259 }
2260
2261 if (dns_rdataset_isassociated(&ge->sigrdataset_aaaa)) {
2262 dns_message_gettemprdataset(msg, &sigrdataset_aaaa);
2263 }
2264
2265 if (rdataset_a != NULL) {
2266 dns_rdataset_clone(&ge->rdataset_a, rdataset_a);
2267 ISC_LIST_APPEND(name->list, rdataset_a, link);
2268 if (IS_REQUIRED_GLUE(rdataset_a)) {
2269 prepend_name = true;
2270 }
2271 }
2272
2273 if (sigrdataset_a != NULL) {
2274 dns_rdataset_clone(&ge->sigrdataset_a, sigrdataset_a);
2275 ISC_LIST_APPEND(name->list, sigrdataset_a, link);
2276 }
2277
2278 if (rdataset_aaaa != NULL) {
2279 dns_rdataset_clone(&ge->rdataset_aaaa, rdataset_aaaa);
2280 ISC_LIST_APPEND(name->list, rdataset_aaaa, link);
2281 if (IS_REQUIRED_GLUE(rdataset_aaaa)) {
2282 prepend_name = true;
2283 }
2284 }
2285 if (sigrdataset_aaaa != NULL) {
2286 dns_rdataset_clone(&ge->sigrdataset_aaaa,
2287 sigrdataset_aaaa);
2288 ISC_LIST_APPEND(name->list, sigrdataset_aaaa, link);
2289 }
2290
2291 dns_message_addname(msg, name, DNS_SECTION_ADDITIONAL);
2292
2293 /*
2294 * When looking for required glue, dns_message_rendersection()
2295 * only processes the first rdataset associated with the first
2296 * name added to the ADDITIONAL section. dns_message_addname()
2297 * performs an append on the list of names in a given section,
2298 * so if any glue record was marked as required, we need to
2299 * move the name it is associated with to the beginning of the
2300 * list for the ADDITIONAL section or else required glue might
2301 * not be rendered.
2302 */
2303 if (prepend_name) {
2304 ISC_LIST_UNLINK(msg->sections[DNS_SECTION_ADDITIONAL],
2305 name, link);
2306 ISC_LIST_PREPEND(msg->sections[DNS_SECTION_ADDITIONAL],
2307 name, link);
2308 }
2309 }
2310 }
2311
2312 static dns_glue_t *
2313 newglue(dns_db_t *db, dns_dbversion_t *dbversion, dns_rbtnode_t *node,
2314 dns_rdataset_t *rdataset) {
2315 dns_fixedname_t nodename;
2316 dns_glue_additionaldata_ctx_t ctx = {
2317 .db = db,
2318 .version = dbversion,
2319 .nodename = dns_fixedname_initname(&nodename),
2320 };
2321
2322 /*
2323 * Get the owner name of the NS RRset - it will be necessary for
2324 * identifying required glue in glue_nsdname_cb() (by
2325 * determining which NS records in the delegation are
2326 * in-bailiwick).
2327 */
2328 dns__rbtdb_nodefullname(db, node, ctx.nodename);
2329
2330 (void)dns_rdataset_additionaldata(rdataset, dns_rootname,
2331 glue_nsdname_cb, &ctx);
2332
2333 return ctx.glue_list;
2334 }
2335
2336 /* FIXME: Perhaps we can squash dns_gluenode_t with
2337 * dns_glue_additionaldata_ctx_t */
2338
2339 static dns_gluenode_t *
2340 new_gluenode(dns_db_t *db, dns_dbversion_t *dbversion, dns_rbtnode_t *node,
2341 dns_rdataset_t *rdataset) {
2342 dns_gluenode_t *gluenode = isc_mem_get(db->mctx, sizeof(*gluenode));
2343 *gluenode = (dns_gluenode_t){
2344 .glue = newglue(db, dbversion, node, rdataset),
2345 .db = db,
2346 };
2347
2348 isc_mem_attach(db->mctx, &gluenode->mctx);
2349 dns_db_attachnode(db, node, (dns_dbnode_t **)&gluenode->node);
2350
2351 return gluenode;
2352 }
2353
2354 static uint32_t
2355 rbtnode_hash(const dns_rbtnode_t *node) {
2356 return isc_hash32(&node, sizeof(node), true);
2357 }
2358
2359 static int
2360 rbtnode_match(struct cds_lfht_node *ht_node, const void *key) {
2361 const dns_gluenode_t *gluenode =
2362 caa_container_of(ht_node, dns_gluenode_t, ht_node);
2363 const dns_rbtnode_t *node = key;
2364
2365 return gluenode->node == node;
2366 }
2367
2368 static uint32_t
2369 gluenode_hash(const dns_gluenode_t *gluenode) {
2370 return rbtnode_hash(gluenode->node);
2371 }
2372
2373 static int
2374 gluenode_match(struct cds_lfht_node *ht_node, const void *key) {
2375 const dns_gluenode_t *gluenode = key;
2376
2377 return rbtnode_match(ht_node, gluenode->node);
2378 }
2379
2380 static void
2381 addglue(dns_db_t *db, dns_dbversion_t *dbversion, dns_rdataset_t *rdataset,
2382 dns_message_t *msg) {
2383 dns_rbtdb_t *rbtdb = (dns_rbtdb_t *)db;
2384 dns_rbtdb_version_t *version = dbversion;
2385 dns_rbtnode_t *node = (dns_rbtnode_t *)rdataset->slab.node;
2386 dns_gluenode_t *gluenode = NULL;
2387
2388 REQUIRE(rdataset->type == dns_rdatatype_ns);
2389 REQUIRE(rbtdb == (dns_rbtdb_t *)rdataset->slab.db);
2390 REQUIRE(rbtdb == version->rbtdb);
2391 REQUIRE(!IS_CACHE(rbtdb) && !IS_STUB(rbtdb));
2392
2393 /*
2394 * The glue table cache that forms a part of the DB version
2395 * structure is not explicitly bounded and there's no cache
2396 * cleaning. The zone data size itself is an implicit bound.
2397 *
2398 * The key into the glue hashtable is the node pointer. This is
2399 * because the glue hashtable is a property of the DB version,
2400 * and the glue is keyed for the ownername/NS tuple. We don't
2401 * bother with using an expensive dns_name_t comparison here as
2402 * the node pointer is a fixed value that won't change for a DB
2403 * version and can be compared directly.
2404 */
2405
2406 rcu_read_lock();
2407
2408 struct cds_lfht_iter iter;
2409 cds_lfht_lookup(version->glue_table, rbtnode_hash(node), rbtnode_match,
2410 node, &iter);
2411
2412 gluenode = cds_lfht_entry(cds_lfht_iter_get_node(&iter), dns_gluenode_t,
2413 ht_node);
2414 if (gluenode == NULL) {
2415 /* No cached glue was found in the table. Get new glue. */
2416 gluenode = new_gluenode(db, version, node, rdataset);
2417
2418 struct cds_lfht_node *ht_node = cds_lfht_add_unique(
2419 version->glue_table, gluenode_hash(gluenode),
2420 gluenode_match, gluenode, &gluenode->ht_node);
2421
2422 if (ht_node != &gluenode->ht_node) {
2423 dns__rbtdb_free_gluenode_rcu(&gluenode->rcu_head);
2424
2425 gluenode = cds_lfht_entry(ht_node, dns_gluenode_t,
2426 ht_node);
2427 }
2428 }
2429
2430 INSIST(gluenode != NULL);
2431
2432 dns_glue_t *glue = gluenode->glue;
2433 isc_statscounter_t counter = dns_gluecachestatscounter_hits_present;
2434
2435 if (glue != NULL) {
2436 /* We have a cached result. Add it to the message and return. */
2437 addglue_to_message(glue, msg);
2438 } else {
2439 counter = dns_gluecachestatscounter_hits_absent;
2440 }
2441
2442 rcu_read_unlock();
2443
2444 if (rbtdb->gluecachestats != NULL) {
2445 isc_stats_increment(rbtdb->gluecachestats, counter);
2446 }
2447 }
2448
2449 dns_dbmethods_t dns__rbtdb_zonemethods = {
2450 .destroy = dns__rbtdb_destroy,
2451 .beginload = beginload,
2452 .endload = endload,
2453 .currentversion = dns__rbtdb_currentversion,
2454 .newversion = dns__rbtdb_newversion,
2455 .attachversion = dns__rbtdb_attachversion,
2456 .closeversion = dns__rbtdb_closeversion,
2457 .findnode = dns__rbtdb_findnode,
2458 .find = zone_find,
2459 .attachnode = dns__rbtdb_attachnode,
2460 .detachnode = dns__rbtdb_detachnode,
2461 .createiterator = dns__rbtdb_createiterator,
2462 .findrdataset = zone_findrdataset,
2463 .allrdatasets = dns__rbtdb_allrdatasets,
2464 .addrdataset = dns__rbtdb_addrdataset,
2465 .subtractrdataset = dns__rbtdb_subtractrdataset,
2466 .deleterdataset = dns__rbtdb_deleterdataset,
2467 .issecure = issecure,
2468 .nodecount = dns__rbtdb_nodecount,
2469 .setloop = dns__rbtdb_setloop,
2470 .getoriginnode = dns__rbtdb_getoriginnode,
2471 .getnsec3parameters = getnsec3parameters,
2472 .findnsec3node = findnsec3node,
2473 .setsigningtime = setsigningtime,
2474 .getsigningtime = getsigningtime,
2475 .getsize = getsize,
2476 .setgluecachestats = setgluecachestats,
2477 .locknode = dns__rbtdb_locknode,
2478 .unlocknode = dns__rbtdb_unlocknode,
2479 .addglue = addglue,
2480 .deletedata = dns__rbtdb_deletedata,
2481 .nodefullname = dns__rbtdb_nodefullname,
2482 .setmaxrrperset = dns__rbtdb_setmaxrrperset,
2483 .setmaxtypepername = dns__rbtdb_setmaxtypepername,
2484 };
2485
2486 void
2487 dns__zonerbt_resigninsert(dns_rbtdb_t *rbtdb, int idx,
2488 dns_slabheader_t *newheader) {
2489 INSIST(!IS_CACHE(rbtdb));
2490 INSIST(newheader->heap_index == 0);
2491 INSIST(!ISC_LINK_LINKED(newheader, link));
2492
2493 isc_heap_insert(rbtdb->heaps[idx], newheader);
2494 newheader->heap = rbtdb->heaps[idx];
2495 }
2496
2497 void
2498 dns__zonerbt_resigndelete(dns_rbtdb_t *rbtdb, dns_rbtdb_version_t *version,
2499 dns_slabheader_t *header DNS__DB_FLARG) {
2500 /*
2501 * Remove the old header from the heap
2502 */
2503 if (header != NULL && header->heap_index != 0) {
2504 isc_heap_delete(rbtdb->heaps[RBTDB_HEADERNODE(header)->locknum],
2505 header->heap_index);
2506 header->heap_index = 0;
2507 if (version != NULL) {
2508 dns__rbtdb_newref(
2509 rbtdb, RBTDB_HEADERNODE(header),
2510 isc_rwlocktype_write DNS__DB_FLARG_PASS);
2511 ISC_LIST_APPEND(version->resigned_list, header, link);
2512 }
2513 }
2514 }
2515
2516 isc_result_t
2517 dns__zonerbt_wildcardmagic(dns_rbtdb_t *rbtdb, const dns_name_t *name,
2518 bool lock) {
2519 isc_result_t result;
2520 dns_name_t foundname;
2521 dns_offsets_t offsets;
2522 unsigned int n;
2523 dns_rbtnode_t *node = NULL;
2524 isc_rwlocktype_t nlocktype = isc_rwlocktype_none;
2525
2526 dns_name_init(&foundname, offsets);
2527 n = dns_name_countlabels(name);
2528 INSIST(n >= 2);
2529 n--;
2530 dns_name_getlabelsequence(name, 1, n, &foundname);
2531 result = dns_rbt_addnode(rbtdb->tree, &foundname, &node);
2532 if (result != ISC_R_SUCCESS && result != ISC_R_EXISTS) {
2533 return result;
2534 }
2535 if (result == ISC_R_SUCCESS) {
2536 node->nsec = DNS_DB_NSEC_NORMAL;
2537 }
2538 node->find_callback = 1;
2539 if (lock) {
2540 NODE_WRLOCK(&rbtdb->node_locks[node->locknum].lock, &nlocktype);
2541 }
2542 node->wild = 1;
2543 if (lock) {
2544 NODE_UNLOCK(&rbtdb->node_locks[node->locknum].lock, &nlocktype);
2545 }
2546 return ISC_R_SUCCESS;
2547 }
2548
2549 isc_result_t
2550 dns__zonerbt_addwildcards(dns_rbtdb_t *rbtdb, const dns_name_t *name,
2551 bool lock) {
2552 isc_result_t result;
2553 dns_name_t foundname;
2554 dns_offsets_t offsets;
2555 unsigned int n, l, i;
2556
2557 dns_name_init(&foundname, offsets);
2558 n = dns_name_countlabels(name);
2559 l = dns_name_countlabels(&rbtdb->common.origin);
2560 i = l + 1;
2561 while (i < n) {
2562 dns_rbtnode_t *node = NULL;
2563 dns_name_getlabelsequence(name, n - i, i, &foundname);
2564 if (dns_name_iswildcard(&foundname)) {
2565 result = dns__zonerbt_wildcardmagic(rbtdb, &foundname,
2566 lock);
2567 if (result != ISC_R_SUCCESS) {
2568 return result;
2569 }
2570 result = dns_rbt_addnode(rbtdb->tree, &foundname,
2571 &node);
2572 if (result != ISC_R_SUCCESS && result != ISC_R_EXISTS) {
2573 return result;
2574 }
2575 if (result == ISC_R_SUCCESS) {
2576 node->nsec = DNS_DB_NSEC_NORMAL;
2577 }
2578 }
2579 i++;
2580 }
2581 return ISC_R_SUCCESS;
2582 }