zone-update.c 23.8 KB
Newer Older
1
/*  Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

17
#include "knot/common/log.h"
18
#include "knot/dnssec/zone-events.h"
Daniel Salzman's avatar
Daniel Salzman committed
19
#include "knot/updates/zone-update.h"
20
#include "knot/zone/serial.h"
21
#include "contrib/mempattern.h"
22
#include "contrib/ucw/lists.h"
23
#include "contrib/ucw/mempool.h"
24

25 26
#include <urcu.h>

27
static int add_to_node(zone_node_t *node, const zone_node_t *add_node,
28
                       knot_mm_t *mm)
29 30 31 32
{
	for (uint16_t i = 0; i < add_node->rrset_count; ++i) {
		knot_rrset_t rr = node_rrset_at(add_node, i);
		if (!knot_rrset_empty(&rr)) {
33
			int ret = node_add_rrset(node, &rr, mm);
34 35 36 37 38 39 40 41 42
			if (ret != KNOT_EOK) {
				return ret;
			}
		}
	}

	return KNOT_EOK;
}

43
static int rem_from_node(zone_node_t *node, const zone_node_t *rem_node,
44
                         knot_mm_t *mm)
45 46
{
	for (uint16_t i = 0; i < rem_node->rrset_count; ++i) {
47
		/* Remove each found RR from 'node'. */
48
		knot_rrset_t rem_rrset = node_rrset_at(rem_node, i);
49
		knot_rdataset_t *to_change = node_rdataset(node, rem_rrset.type);
50
		if (to_change) {
51
			/* Remove data from synthesized node */
52
			int ret = knot_rdataset_subtract(to_change,
53 54 55 56 57
			                                 &rem_rrset.rrs,
			                                 mm);
			if (ret != KNOT_EOK) {
				return ret;
			}
58
			/* Remove whole rdataset if empty */
59 60 61
			if (to_change->rr_count == 0) {
				node_remove_rdataset(node, rem_rrset.type);
			}
62 63 64 65 66 67 68
		}
	}

	return KNOT_EOK;
}

static int apply_changes_to_node(zone_node_t *synth_node, const zone_node_t *add_node,
69
                                 const zone_node_t *rem_node, knot_mm_t *mm)
70
{
71
	/* Add changes to node */
72 73 74 75 76 77 78
	if (!node_empty(add_node)) {
		int ret = add_to_node(synth_node, add_node, mm);
		if (ret != KNOT_EOK) {
			return ret;
		}
	}

79
	/* Remove changes from node */
80 81 82 83 84 85 86 87 88 89
	if (!node_empty(rem_node)) {
		int ret = rem_from_node(synth_node, rem_node, mm);
		if (ret != KNOT_EOK) {
			return ret;
		}
	}

	return KNOT_EOK;
}

90
static int deep_copy_node_data(zone_node_t *node_copy, const zone_node_t *node,
91
                               knot_mm_t *mm)
92
{
93
	/* Clear space for RRs */
94 95
	node_copy->rrs = NULL;
	node_copy->rrset_count = 0;
96

97 98
	for (uint16_t i = 0; i < node->rrset_count; ++i) {
		knot_rrset_t rr = node_rrset_at(node, i);
99
		int ret = node_add_rrset(node_copy, &rr, mm);
100 101 102 103 104 105 106 107
		if (ret != KNOT_EOK) {
			return ret;
		}
	}

	return KNOT_EOK;
}

108
static zone_node_t *node_deep_copy(const zone_node_t *node, knot_mm_t *mm)
109
{
110 111 112 113 114
	if (node == NULL) {
		return NULL;
	}

	/* Shallow copy old node */
115
	zone_node_t *synth_node = node_shallow_copy(node, mm);
116 117 118 119
	if (synth_node == NULL) {
		return NULL;
	}

120
	/* Deep copy data inside node copy. */
121
	int ret = deep_copy_node_data(synth_node, node, mm);
122 123 124 125 126 127 128 129
	if (ret != KNOT_EOK) {
		node_free(&synth_node, mm);
		return NULL;
	}

	return synth_node;
}

130 131
static int init_incremental(zone_update_t *update, zone_t *zone)
{
132 133 134
	if (zone->contents == NULL) {
		return KNOT_EINVAL;
	}
135

136 137 138 139
	int ret = changeset_init(&update->change, zone->name);
	if (ret != KNOT_EOK) {
		return ret;
	}
140

141
	/* Copy base SOA RR. */
142 143 144 145 146
	update->change.soa_from =
		node_create_rrset(update->zone->contents->apex, KNOT_RRTYPE_SOA);
	if (update->change.soa_from == NULL) {
		return KNOT_ENOMEM;
	}
147 148 149 150 151 152 153 154 155 156 157 158 159 160

	return KNOT_EOK;
}

static int init_full(zone_update_t *update, zone_t *zone)
{
	update->new_cont = zone_contents_new(zone->name);
	if (update->new_cont == NULL) {
		return KNOT_ENOMEM;
	}

	return KNOT_EOK;
}

161
static const zone_node_t *get_synth_node(zone_update_t *update, const knot_dname_t *dname)
162
{
163
	const zone_node_t *old_node =
164
		zone_contents_find_node(update->zone->contents, dname);
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

	if (old_node == update->zone->contents->apex && update->change.soa_to != NULL) {
		/* We have an apex and a SOA change, make a copy and apply the change. */
		zone_node_t *synth_node = node_deep_copy(old_node, &update->mm);
		if (synth_node == NULL) {
			return NULL;
		}

		/* Remove the old SOA */
		knot_rdataset_t *from = node_rdataset(synth_node, KNOT_RRTYPE_SOA);
		knot_rdataset_t *what = node_rdataset(old_node, KNOT_RRTYPE_SOA);
		int ret = knot_rdataset_subtract(from, what, &update->mm);
		if (ret != KNOT_EOK) {
			node_free_rrsets(synth_node, &update->mm);
			node_free(&synth_node, &update->mm);
			return NULL;
		}

		/* Add the new SOA */
		ret = node_add_rrset(synth_node, update->change.soa_to, &update->mm);
		if (ret != KNOT_EOK) {
			node_free_rrsets(synth_node, &update->mm);
			node_free(&synth_node, &update->mm);
			return NULL;
		}

		old_node = synth_node;
	}

194
	const zone_node_t *add_node =
195
		zone_contents_find_node(update->change.add, dname);
196
	const zone_node_t *rem_node =
197
		zone_contents_find_node(update->change.remove, dname);
198 199 200

	const bool have_change = !node_empty(add_node) || !node_empty(rem_node);
	if (!have_change) {
201
		/* Nothing to apply */
202 203 204
		return old_node;
	}

205
	if (old_node == NULL) {
206
		if (add_node && node_empty(rem_node)) {
207
			/* Just addition */
208 209
			return add_node;
		} else {
210
			/* Addition and deletion */
211 212
			old_node = add_node;
			add_node = NULL;
213 214
		}
	}
215

216
	/* We have to apply changes to node. */
217 218 219 220 221
	zone_node_t *synth_node = node_deep_copy(old_node, &update->mm);
	if (synth_node == NULL) {
		return NULL;
	}

222
	/* Apply changes to node. */
223 224 225
	int ret = apply_changes_to_node(synth_node, add_node, rem_node,
	                                &update->mm);
	if (ret != KNOT_EOK) {
226
		node_free_rrsets(synth_node, &update->mm);
Jan Kadlec's avatar
Jan Kadlec committed
227
		node_free(&synth_node, &update->mm);
228 229 230 231
		return NULL;
	}

	return synth_node;
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
/* ------------------------------- API -------------------------------------- */

int zone_update_init(zone_update_t *update, zone_t *zone, zone_update_flags_t flags)
{
	if (update == NULL || zone == NULL) {
		return KNOT_EINVAL;
	}

	memset(update, 0, sizeof(*update));
	update->zone = zone;

	apply_init_ctx(&update->a_ctx);

	mm_ctx_mempool(&update->mm, MM_DEFAULT_BLKSIZE);
	update->flags = flags;

	if (flags & UPDATE_INCREMENTAL) {
		return init_incremental(update, zone);
	} else if (flags & UPDATE_FULL) {
		return init_full(update, zone);
	} else {
		return KNOT_EINVAL;
	}
}

const zone_node_t *zone_update_get_node(zone_update_t *update, const knot_dname_t *dname)
{
	if (update == NULL || dname == NULL) {
		return NULL;
	}

	if (update->flags & UPDATE_FULL) {
		return zone_contents_find_node(update->new_cont, dname);
	} else {
		return get_synth_node(update, dname);
	}
}

272 273
const zone_node_t *zone_update_get_apex(zone_update_t *update)
{
274 275 276 277
	if (update == NULL) {
		return NULL;
	}

278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
	return zone_update_get_node(update, update->zone->name);
}

uint32_t zone_update_current_serial(zone_update_t *update)
{
	const zone_node_t *apex = zone_update_get_apex(update);
	if (apex) {
		return knot_soa_serial(node_rdataset(apex, KNOT_RRTYPE_SOA));
	} else {
		return 0;
	}
}

const knot_rdataset_t *zone_update_from(zone_update_t *update)
{
293 294 295 296
	if (update == NULL) {
		return NULL;
	}

297 298 299 300 301 302
	if (update->flags & UPDATE_INCREMENTAL) {
		const zone_node_t *apex = update->zone->contents->apex;
		return node_rdataset(apex, KNOT_RRTYPE_SOA);
	}

	return NULL;
303 304 305 306
}

const knot_rdataset_t *zone_update_to(zone_update_t *update)
{
307 308 309
	if (update == NULL) {
		return NULL;
	}
310

311 312 313 314 315 316 317 318
	if (update->flags & UPDATE_FULL) {
		const zone_node_t *apex = update->new_cont->apex;
		return node_rdataset(apex, KNOT_RRTYPE_SOA);
	} else if (update->flags & UPDATE_INCREMENTAL) {
		if (update->change.soa_to == NULL) {
			return NULL;
		}
		return &update->change.soa_to->rrs;
319 320
	}

321
	return NULL;
322 323
}

324 325
void zone_update_clear(zone_update_t *update)
{
326 327
	if (update == NULL) {
		return;
328
	}
329 330 331 332 333 334 335 336 337 338

	if (update->flags & UPDATE_INCREMENTAL) {
		/* Revert any changes on error, do nothing on success. */
		update_rollback(&update->a_ctx);
		changeset_clear(&update->change);
	} else if (update->flags & UPDATE_FULL) {
		zone_contents_deep_free(&update->new_cont);
	}
	mp_delete(update->mm.ctx);
	memset(update, 0, sizeof(*update));
339
}
340

341 342
int zone_update_add(zone_update_t *update, const knot_rrset_t *rrset)
{
343 344 345 346
	if (update == NULL) {
		return KNOT_EINVAL;
	}

347
	if (update->flags & UPDATE_INCREMENTAL) {
348
		return changeset_add_rrset(&update->change, rrset, CHANGESET_CHECK);
349 350 351 352 353 354 355 356 357 358
	} else if (update->flags & UPDATE_FULL) {
		zone_node_t *n = NULL;
		return zone_contents_add_rr(update->new_cont, rrset, &n);
	} else {
		return KNOT_EINVAL;
	}
}

int zone_update_remove(zone_update_t *update, const knot_rrset_t *rrset)
{
359 360 361 362
	if (update == NULL) {
		return KNOT_EINVAL;
	}

363
	if (update->flags & UPDATE_INCREMENTAL) {
364
		return changeset_rem_rrset(&update->change, rrset, CHANGESET_CHECK);
365 366
	} else if (update->flags & UPDATE_FULL) {
		zone_node_t *n = NULL;
367 368 369 370
		knot_rrset_t *rrs_copy = knot_rrset_copy(rrset, &update->mm);
		int ret = zone_contents_remove_rr(update->new_cont, rrs_copy, &n);
		knot_rrset_free(&rrs_copy, &update->mm);
		return ret;
371
	} else {
372
		return KNOT_EINVAL;
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
int zone_update_remove_rrset(zone_update_t *update, knot_dname_t *owner, uint16_t type)
{
	if (update == NULL || owner == NULL) {
		return KNOT_EINVAL;
	}

	if (update->flags & UPDATE_INCREMENTAL) {
		/* Remove the RRSet from the original node */
		const zone_node_t *node = zone_contents_find_node(update->zone->contents, owner);
		if (node != NULL) {
			knot_rrset_t rrset = node_rrset(node, type);
			int ret = changeset_rem_rrset(&update->change, &rrset, CHANGESET_CHECK);
			if (ret != KNOT_EOK) {
				return ret;
			}
		}

		/* Remove the RRSet from the additions in the changeset */
		const zone_node_t *additions = zone_contents_find_node(update->change.add, owner);
		if (additions != NULL) {
			knot_rrset_t rrset = node_rrset(additions, type);
			int ret = changeset_rem_rrset(&update->change, &rrset, CHANGESET_CHECK);
			if (ret != KNOT_EOK) {
				return ret;
			}
		}

		if (node == NULL && additions == NULL) {
			return KNOT_ENONODE;
		}
	} else if (update->flags & UPDATE_FULL) {
		/* Remove the RRSet from the non-synthesized new node */
		const zone_node_t *node = zone_contents_find_node(update->new_cont, owner);
		if (node == NULL) {
			return KNOT_ENONODE;
		}

		knot_rrset_t rrset = node_rrset(node, type);
		int ret = zone_update_remove(update, &rrset);
		if (ret != KNOT_EOK) {
			return ret;
		}
	}

	return KNOT_EOK;
}

int zone_update_remove_node(zone_update_t *update, const knot_dname_t *owner)
{
	if (update == NULL || owner == NULL) {
		return KNOT_EINVAL;
	}

	if (update->flags & UPDATE_INCREMENTAL) {
		/* Remove all RRSets from the original node */
		const zone_node_t *node = zone_contents_find_node(update->zone->contents, owner);
		if (node != NULL) {
			size_t rrset_count = node->rrset_count;
			for (int i = 0; i < rrset_count; ++i) {
				knot_rrset_t rrset = node_rrset_at(node, rrset_count - 1 - i);
				int ret = changeset_rem_rrset(&update->change, &rrset, CHANGESET_CHECK);
				if (ret != KNOT_EOK) {
					return ret;
				}
			}
		}

		/* Remove all RRSets from the additions in the changeset */
		const zone_node_t *additions = zone_contents_find_node(update->change.add, owner);
		if (additions != NULL) {
			size_t rrset_count = additions->rrset_count;
			for (int i = 0; i < rrset_count; ++i) {
				knot_rrset_t rrset = node_rrset_at(additions, rrset_count - 1 - i);
				int ret = changeset_rem_rrset(&update->change, &rrset, CHANGESET_CHECK);
				if (ret != KNOT_EOK) {
					return ret;
				}
			}
		}

		if (node == NULL && additions == NULL) {
			return KNOT_ENONODE;
		}
	} else if (update->flags & UPDATE_FULL) {
		/* Remove all RRSets from the non-synthesized new node */
		const zone_node_t *node = zone_contents_find_node(update->new_cont, owner);
		if (node == NULL) {
			return KNOT_ENONODE;
		}

		size_t rrset_count = node->rrset_count;
		for (int i = 0; i < rrset_count; ++i) {
			knot_rrset_t rrset = node_rrset_at(node, rrset_count - 1 - i);
			int ret = zone_update_remove(update, &rrset);
			if (ret != KNOT_EOK) {
				return ret;
			}
		}
	}

	return KNOT_EOK;
}

479 480 481 482
static bool apex_rr_changed(const zone_node_t *old_apex,
                            const zone_node_t *new_apex,
                            uint16_t type)
{
483 484
	assert(old_apex);
	assert(new_apex);
485 486 487 488 489 490
	knot_rrset_t old_rr = node_rrset(old_apex, type);
	knot_rrset_t new_rr = node_rrset(new_apex, type);

	return !knot_rrset_equal(&old_rr, &new_rr, KNOT_RRSET_COMPARE_WHOLE);
}

491
static bool apex_dnssec_changed(zone_update_t *update)
492 493
{
	const zone_node_t *new_apex = zone_update_get_apex(update);
494 495 496 497 498 499 500 501 502 503
	if (update->zone->contents != NULL) {
		const zone_node_t *old_apex = update->zone->contents->apex;
		return !changeset_empty(&update->change) &&
		       (apex_rr_changed(new_apex, old_apex, KNOT_RRTYPE_DNSKEY) ||
		        apex_rr_changed(new_apex, old_apex, KNOT_RRTYPE_NSEC3PARAM));
	} else if (new_apex != NULL) {
		return true;
	}

	return false;
504 505 506
}

static int sign_update(zone_update_t *update,
507
                       zone_contents_t *new_contents)
508
{
509 510
	assert(update);
	assert(new_contents);
511

512 513
	/* Check if the UPDATE changed DNSKEYs or NSEC3PARAM.
	 * If so, we have to sign the whole zone. */
514 515
	int ret = KNOT_EOK;
	uint32_t refresh_at = 0;
516 517 518 519 520 521
	changeset_t sec_ch;
	ret = changeset_init(&sec_ch, update->zone->name);
	if (ret != KNOT_EOK) {
		return ret;
	}

522
	const bool full_sign = changeset_empty(&update->change) ||
523
	                       apex_dnssec_changed(update);
524
	if (full_sign) {
525
		ret = knot_dnssec_zone_sign(new_contents, &sec_ch,
526 527 528
		                            ZONE_SIGN_KEEP_SOA_SERIAL,
		                            &refresh_at);
	} else {
529
		/* Sign the created changeset */
530
		ret = knot_dnssec_sign_changeset(new_contents, &update->change,
531
		                                 &sec_ch, &refresh_at);
532 533
	}
	if (ret != KNOT_EOK) {
534
		changeset_clear(&sec_ch);
535 536 537
		return ret;
	}

538
	/* Apply DNSSEC changeset */
539
	ret = apply_changeset_directly(&update->a_ctx, new_contents, &sec_ch);
540
	if (ret != KNOT_EOK) {
541
		changeset_clear(&sec_ch);
542 543 544
		return ret;
	}

545
	if (!full_sign) {
546
		/* Merge changesets */
547 548 549 550 551 552
		ret = changeset_merge(&update->change, &sec_ch);
		if (ret != KNOT_EOK) {
			update_rollback(&update->a_ctx);
			changeset_clear(&sec_ch);
			return ret;
		}
553 554
	}

555
	/* Plan next zone resign. */
556 557 558 559 560
	const time_t resign_time = zone_events_get_time(update->zone, ZONE_EVENT_DNSSEC);
	if (refresh_at < resign_time) {
		zone_events_schedule_at(update->zone, ZONE_EVENT_DNSSEC, refresh_at);
	}

561 562
	/* We are not calling update_cleanup, as the rollback data are merged
	 * into the main changeset and will get cleaned up with that. */
563 564
	changeset_clear(&sec_ch);

565 566 567
	return KNOT_EOK;
}

568
static int set_new_soa(zone_update_t *update, unsigned serial_policy)
569
{
570 571
	assert(update);

572 573 574 575 576 577
	knot_rrset_t *soa_cpy = node_create_rrset(zone_update_get_apex(update), KNOT_RRTYPE_SOA);
	if (soa_cpy == NULL) {
		return KNOT_ENOMEM;
	}

	uint32_t old_serial = knot_soa_serial(&soa_cpy->rrs);
578
	uint32_t new_serial = serial_next(old_serial, serial_policy);
579 580 581 582 583 584 585 586 587 588 589 590
	if (serial_compare(old_serial, new_serial) >= 0) {
		log_zone_warning(update->zone->name, "updated serial is lower "
		                 "than current, serial %u -> %u",
		                  old_serial, new_serial);
	}

	knot_soa_serial_set(&soa_cpy->rrs, new_serial);
	update->change.soa_to = soa_cpy;

	return KNOT_EOK;
}

591
static int commit_incremental(conf_t *conf, zone_update_t *update, zone_contents_t **contents_out)
592
{
593
	assert(update);
594
	assert(contents_out);
595

596 597 598 599 600
	if (changeset_empty(&update->change)) {
		changeset_clear(&update->change);
		return KNOT_EOK;
	}

601
	int ret = KNOT_EOK;
602
	if (zone_update_to(update) == NULL) {
603
		/* No SOA in the update, create one according to the current policy */
604 605
		conf_val_t val = conf_zone_get(conf, C_SERIAL_POLICY, update->zone->name);
		ret = set_new_soa(update, conf_opt(&val));
606 607 608 609 610
		if (ret != KNOT_EOK) {
			return ret;
		}
	}

611
	/* Apply changes. */
612
	zone_contents_t *new_contents = NULL;
613
	ret = apply_changeset(&update->a_ctx, update->zone, &update->change, &new_contents);
614 615 616 617
	if (ret != KNOT_EOK) {
		changeset_clear(&update->change);
		return ret;
	}
618

619
	assert(new_contents);
620

621
	conf_val_t val = conf_zone_get(conf, C_DNSSEC_SIGNING, update->zone->name);
622
	bool dnssec_enable = (update->flags & UPDATE_SIGN) && conf_bool(&val);
623

624
	/* Sign the update. */
625
	if (dnssec_enable) {
626
		ret = sign_update(update, new_contents);
627
		if (ret != KNOT_EOK) {
628
			update_rollback(&update->a_ctx);
629
			update_free_zone(&new_contents);
630
			changeset_clear(&update->change);
631 632
			return ret;
		}
633
	}
634

635
	/* Write changes to journal if all went well. (DNSSEC merged) */
636
	ret = zone_change_store(conf, update->zone, &update->change);
637
	if (ret != KNOT_EOK) {
638
		update_rollback(&update->a_ctx);
639 640 641
		update_free_zone(&new_contents);
		return ret;
	}
642

643 644
	*contents_out = new_contents;

645 646 647
	return KNOT_EOK;
}

648 649
static int commit_full(conf_t *conf, zone_update_t *update, zone_contents_t **contents_out)
{
650 651 652 653 654 655 656 657 658
	assert(update);
	assert(contents_out);

	/* Check if we have SOA. We might consider adding full semantic check here.
	 * But if we wanted full sem-check I'd consider being it controlled by a flag
	 * - to enable/disable it on demand. */
	if (!node_rrtype_exists(update->new_cont->apex, KNOT_RRTYPE_SOA)) {
		return KNOT_ESEMCHECK;
	}
659 660 661 662

	zone_contents_adjust_full(update->new_cont);

	conf_val_t val = conf_zone_get(conf, C_DNSSEC_SIGNING, update->zone->name);
663
	bool dnssec_enable = (update->flags & UPDATE_SIGN) && conf_bool(&val);
664

665
	/* Sign the update. */
666
	if (dnssec_enable) {
667
		int ret = sign_update(update, update->new_cont);
668 669 670 671 672 673 674 675 676 677 678
		if (ret != KNOT_EOK) {
			update_rollback(&update->a_ctx);
			return ret;
		}
	}

	update_cleanup(&update->a_ctx);
	*contents_out = update->new_cont;
	return KNOT_EOK;
}

679
int zone_update_commit(conf_t *conf, zone_update_t *update)
680
{
681
	if (conf == NULL || update == NULL) {
682 683 684 685 686
		return KNOT_EINVAL;
	}

	int ret = KNOT_EOK;
	zone_contents_t *new_contents = NULL;
687
	if (update->flags & UPDATE_INCREMENTAL) {
688 689 690 691 692
		ret = commit_incremental(conf, update, &new_contents);
		if (ret != KNOT_EOK) {
			return ret;
		}
	} else {
693 694 695 696
		ret = commit_full(conf, update, &new_contents);
		if (ret != KNOT_EOK) {
			return ret;
		}
697 698
	}

699
	/* If there is anything to change */
700
	if (new_contents != NULL) {
701 702 703 704 705
		/* Switch zone contents. */
		zone_contents_t *old_contents = zone_switch_contents(update->zone, new_contents);

		/* Sync RCU. */
		synchronize_rcu();
706 707 708
		if (update->flags & UPDATE_FULL) {
			update->new_cont = NULL;
		} else if (update->flags & UPDATE_INCREMENTAL) {
709 710 711 712 713 714 715
			update_cleanup(&update->a_ctx);
			update_free_zone(&old_contents);
			changeset_clear(&update->change);
		}
	}

	return KNOT_EOK;
716 717
}

718 719 720
static void select_next_node(zone_update_iter_t *it)
{
	int compare = 0;
721 722
	if (it->base_node != NULL) {
		if (it->add_node != NULL) {
723
			/* Both original and new node exists. Choose the 'smaller' node to return. */
724
			compare = knot_dname_cmp(it->base_node->owner, it->add_node->owner);
725
			if (compare <= 0) {
726 727 728
				/* Return the original node. */
				it->next_node = it->base_node;
				it->base_node = NULL;
729 730 731 732
				if (compare == 0) {
					it->add_node = NULL;
				}
			} else {
733 734
				/* Return the new node. */
				it->next_node = it->add_node;
735 736 737
				it->add_node = NULL;
			}
		} else {
738 739 740
			/* Return the original node. */
			it->next_node = it->base_node;
			it->base_node = NULL;
741 742
		}
	} else {
743 744 745
		if (it->add_node != NULL) {
			/* Return the new node. */
			it->next_node = it->add_node;
746 747
			it->add_node = NULL;
		} else {
748 749
			/* Iteration done. */
			it->next_node = NULL;
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
		}
	}
}

static int iter_init_tree_iters(zone_update_iter_t *it, zone_update_t *update,
                                bool nsec3)
{
	zone_tree_t *tree;

	/* Set zone iterator. */
	zone_contents_t *_contents = NULL;
	if (update->flags & UPDATE_FULL) {
		_contents = update->new_cont;
	} else if (update->flags & UPDATE_INCREMENTAL) {
		_contents = update->zone->contents;
	} else {
		return KNOT_EINVAL;
	}

	/* Begin iteration. We can safely assume _contents is a valid pointer. */
	tree = nsec3 ? _contents->nsec3_nodes : _contents->nodes;
	hattrie_build_index(tree);
772 773
	it->base_it = hattrie_iter_begin(nsec3 ? _contents->nsec3_nodes : _contents->nodes, true);
	if (it->base_it == NULL) {
774 775 776 777
		return KNOT_ENOMEM;
	}

	/* Set changeset iterator. */
778
	if ((update->flags & UPDATE_INCREMENTAL) && !changeset_empty(&update->change)) {
779 780 781 782 783 784 785 786
		tree = nsec3 ? update->change.add->nsec3_nodes :
		               update->change.add->nodes;
		if (tree == NULL) {
			it->add_it = NULL;
		} else {
			hattrie_build_index(tree);
			it->add_it = hattrie_iter_begin(tree, true);
			if (it->add_it == NULL) {
787
				hattrie_iter_free(it->base_it);
788 789 790 791 792 793
				return KNOT_ENOMEM;
			}
		}
	} else {
		it->add_it = NULL;
	}
794

795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
	return KNOT_EOK;
}

static int iter_get_added_node(zone_update_iter_t *it)
{
	hattrie_iter_next(it->add_it);
	if (hattrie_iter_finished(it->add_it)) {
		hattrie_iter_free(it->add_it);
		it->add_it = NULL;
		return KNOT_ENOENT;
	}

	it->add_node = (zone_node_t *)(*hattrie_iter_val(it->add_it));

	return KNOT_EOK;
}

static int iter_get_synth_node(zone_update_iter_t *it)
{
814 815 816 817
	hattrie_iter_next(it->base_it);
	if (hattrie_iter_finished(it->base_it)) {
		hattrie_iter_free(it->base_it);
		it->base_it = NULL;
818 819 820
		return KNOT_ENOENT;
	}

821
	const zone_node_t *n = (zone_node_t *)(*hattrie_iter_val(it->base_it));
822
	if (it->update->flags & UPDATE_FULL) {
823
		it->base_node = n;
824
	} else {
825 826
		it->base_node = zone_update_get_node(it->update, n->owner);
		if (it->base_node == NULL) {
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
			return KNOT_ENOMEM;
		}
	}

	return KNOT_EOK;
}

static int iter_init(zone_update_iter_t *it, zone_update_t *update, const bool nsec3)
{
	memset(it, 0, sizeof(*it));

	it->update = update;
	it->nsec3 = nsec3;
	int ret = iter_init_tree_iters(it, update, nsec3);
	if (ret != KNOT_EOK) {
		return ret;
	}

845
	if (it->add_it != NULL) {
846 847 848
		it->add_node = (zone_node_t *)(*hattrie_iter_val(it->add_it));
		assert(it->add_node);
	}
849 850 851
	if (it->base_it != NULL) {
		it->base_node = (zone_node_t *)(*hattrie_iter_val(it->base_it));
		assert(it->base_node);
852
		if (it->update->flags & UPDATE_INCREMENTAL) {
853 854
			it->base_node = zone_update_get_node(it->update, it->base_node->owner);
			if (it->base_node == NULL) {
855 856 857 858 859 860 861 862 863 864 865
				return KNOT_ENOMEM;
			}
		}
	}

	select_next_node(it);
	return KNOT_EOK;
}

int zone_update_iter(zone_update_iter_t *it, zone_update_t *update)
{
866 867 868 869
	if (it == NULL || update == NULL) {
		return KNOT_EINVAL;
	}

870 871 872 873 874
	return iter_init(it, update, false);
}

int zone_update_iter_nsec3(zone_update_iter_t *it, zone_update_t *update)
{
875 876 877 878
	if (it == NULL || update == NULL) {
		return KNOT_EINVAL;
	}

879 880
	if (update->flags & UPDATE_FULL) {
		if (update->new_cont->nsec3_nodes == NULL) {
881
			/* No NSEC3 tree. */
882 883 884 885 886
			return KNOT_ENOENT;
		}
	} else {
		if (update->change.add->nsec3_nodes == NULL &&
		    update->change.remove->nsec3_nodes == NULL) {
887
			/* No NSEC3 changes. */
888 889 890 891 892 893 894 895 896 897 898 899 900
			return KNOT_ENOENT;
		}
	}

	return iter_init(it, update, true);
}

int zone_update_iter_next(zone_update_iter_t *it)
{
	if (it == NULL) {
		return KNOT_EINVAL;
	}

901 902
	/* Get nodes from both iterators if needed. */
	if (it->base_it != NULL && it->base_node == NULL) {
903 904 905 906 907 908
		int ret = iter_get_synth_node(it);
		if (ret != KNOT_EOK && ret != KNOT_ENOENT) {
			return ret;
		}
	}

909
	if (it->add_it != NULL && it->add_node == NULL) {
910 911 912 913 914
		int ret = iter_get_added_node(it);
		if (ret != KNOT_EOK && ret != KNOT_ENOENT) {
			return ret;
		}
	}
915

916 917 918 919 920 921
	select_next_node(it);
	return KNOT_EOK;
}

const zone_node_t *zone_update_iter_val(zone_update_iter_t *it)
{
922 923
	if (it != NULL) {
		return it->next_node;
924 925 926 927 928
	} else {
		return NULL;
	}
}

929
void zone_update_iter_finish(zone_update_iter_t *it)
930
{
931
	if (it == NULL) {
932
		return;
933 934 935
	}

	hattrie_iter_free(it->base_it);
936 937
}

938
bool zone_update_no_change(zone_update_t *update)
939
{
940 941 942 943
	if (update == NULL) {
		return true;
	}

944 945 946
	if (update->flags & UPDATE_INCREMENTAL) {
		return changeset_empty(&update->change);
	} else {
947 948 949
		/* This branch does not make much sense and FULL update will most likely
		 * be a change every time anyway, just return false. */
		return false;
950
	}
951
}
952