zone-update.c 12.3 KB
Newer Older
Daniel Salzman's avatar
Daniel Salzman committed
1
/*  Copyright (C) 2015 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 58 59 60 61 62 63 64
			                                 &rem_rrset.rrs,
			                                 mm);
			if (ret != KNOT_EOK) {
				return ret;
			}
		}
	}

	return KNOT_EOK;
}

static int apply_changes_to_node(zone_node_t *synth_node, const zone_node_t *add_node,
65
                                 const zone_node_t *rem_node, knot_mm_t *mm)
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
{
	// Add changes to node
	if (!node_empty(add_node)) {
		int ret = add_to_node(synth_node, add_node, mm);
		if (ret != KNOT_EOK) {
			return ret;
		}
	}

	// Remove changes from node
	if (!node_empty(rem_node)) {
		int ret = rem_from_node(synth_node, rem_node, mm);
		if (ret != KNOT_EOK) {
			return ret;
		}
	}

	return KNOT_EOK;
}

86
static int deep_copy_node_data(zone_node_t *node_copy, const zone_node_t *node,
87
                               knot_mm_t *mm)
88
{
89 90 91
	// Clear space for RRs
	node_copy->rrs = NULL;
	node_copy->rrset_count = 0;
92

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

	return KNOT_EOK;
}

104
static zone_node_t *node_deep_copy(const zone_node_t *node, knot_mm_t *mm)
105 106
{
	// Shallow copy old node
107
	zone_node_t *synth_node = node_shallow_copy(node, mm);
108 109 110 111 112
	if (synth_node == NULL) {
		return NULL;
	}

	// Deep copy data inside node copy.
113
	int ret = deep_copy_node_data(synth_node, node, mm);
114 115 116 117 118 119 120 121
	if (ret != KNOT_EOK) {
		node_free(&synth_node, mm);
		return NULL;
	}

	return synth_node;
}

122 123
static int init_incremental(zone_update_t *update, zone_t *zone)
{
124 125
	assert(zone->contents);

126 127 128 129
	int ret = changeset_init(&update->change, zone->name);
	if (ret != KNOT_EOK) {
		return ret;
	}
130 131

	apply_init_ctx(&update->a_ctx);
132

133 134 135 136 137 138
	// Copy base SOA RR.
	update->change.soa_from =
		node_create_rrset(update->zone->contents->apex, KNOT_RRTYPE_SOA);
	if (update->change.soa_from == NULL) {
		return KNOT_ENOMEM;
	}
139 140 141 142 143 144 145 146 147 148 149 150 151 152

	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;
}

153 154
/* ------------------------------- API -------------------------------------- */

155
int zone_update_init(zone_update_t *update, zone_t *zone, zone_update_flags_t flags)
156
{
157 158 159 160 161
	if (update == NULL || zone == NULL) {
		return KNOT_EINVAL;
	}

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

164
	mm_ctx_mempool(&update->mm, MM_DEFAULT_BLKSIZE);
165 166 167 168 169 170 171 172 173
	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;
	}
174 175 176 177 178 179 180 181
}

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

182
	const zone_node_t *old_node =
183
		zone_contents_find_node(update->zone->contents, dname);
184
	const zone_node_t *add_node =
185
		zone_contents_find_node(update->change.add, dname);
186
	const zone_node_t *rem_node =
187
		zone_contents_find_node(update->change.remove, dname);
188 189 190

	const bool have_change = !node_empty(add_node) || !node_empty(rem_node);
	if (!have_change) {
191
		// Nothing to apply
192 193 194
		return old_node;
	}

195
	if (!old_node) {
196
		if (add_node && node_empty(rem_node)) {
197
			// Just addition
198 199
			return add_node;
		} else {
200 201 202
			// Addition and deletion
			old_node = add_node;
			add_node = NULL;
203 204
		}
	}
205 206 207 208 209 210 211 212 213 214 215

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

	// Apply changes to node.
	int ret = apply_changes_to_node(synth_node, add_node, rem_node,
	                                &update->mm);
	if (ret != KNOT_EOK) {
216
		node_free_rrsets(synth_node, &update->mm);
Jan Kadlec's avatar
Jan Kadlec committed
217
		node_free(&synth_node, &update->mm);
218 219 220 221
		return NULL;
	}

	return synth_node;
222 223
}

224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
const zone_node_t *zone_update_get_apex(zone_update_t *update)
{
	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)
{
	const zone_node_t *apex = update->zone->contents->apex;
	return node_rdataset(apex, KNOT_RRTYPE_SOA);
}

const knot_rdataset_t *zone_update_to(zone_update_t *update)
{
	assert(update);

	if (update->change.soa_to == NULL) {
		return NULL;
	}

	return &update->change.soa_to->rrs;
}

256 257
void zone_update_clear(zone_update_t *update)
{
258
	if (update) {
259 260 261 262 263
		if (update->flags & UPDATE_INCREMENTAL) {
			/* Revert any changes on error, do nothing on success. */
			update_rollback(&update->a_ctx);
			changeset_clear(&update->change);
		}
264
		mp_delete(update->mm.ctx);
Jan Kadlec's avatar
Jan Kadlec committed
265
		memset(update, 0, sizeof(*update));
266
	}
267
}
268

269 270
int zone_update_add(zone_update_t *update, const knot_rrset_t *rrset)
{
271
	if (update->flags & UPDATE_INCREMENTAL) {
272
		return changeset_add_rrset(&update->change, rrset, CHANGESET_CHECK);
273 274 275 276 277 278 279 280 281 282
	} 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)
{
283
	if (update->flags & UPDATE_INCREMENTAL) {
284
		return changeset_rem_rrset(&update->change, rrset, CHANGESET_CHECK);
285 286 287 288 289
	} else {
		return KNOT_ENOTSUP;
	}
}

290 291 292 293 294 295 296 297 298 299
static bool apex_rr_changed(const zone_node_t *old_apex,
                            const zone_node_t *new_apex,
                            uint16_t type)
{
	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);
}

300
static bool apex_dnssec_changed(zone_update_t *update)
301 302 303 304 305 306 307 308 309 310
{
	assert(update->zone->contents);
	const zone_node_t *new_apex = zone_update_get_apex(update);
	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));
}

static int sign_update(zone_update_t *update,
311
                       zone_contents_t *new_contents)
312 313 314 315 316 317 318 319 320
{
	assert(update != NULL);

	/*
	 * Check if the UPDATE changed DNSKEYs or NSEC3PARAM.
	 * If so, we have to sign the whole zone.
	 */
	int ret = KNOT_EOK;
	uint32_t refresh_at = 0;
321 322 323 324 325 326
	changeset_t sec_ch;
	ret = changeset_init(&sec_ch, update->zone->name);
	if (ret != KNOT_EOK) {
		return ret;
	}

327
	const bool full_sign = changeset_empty(&update->change) ||
328
	                       apex_dnssec_changed(update);
329
	if (full_sign) {
330
		ret = knot_dnssec_zone_sign(new_contents, &sec_ch,
331 332 333 334 335
		                            ZONE_SIGN_KEEP_SOA_SERIAL,
		                            &refresh_at);
	} else {
		// Sign the created changeset
		ret = knot_dnssec_sign_changeset(new_contents, &update->change,
336
		                                 &sec_ch, &refresh_at);
337 338
	}
	if (ret != KNOT_EOK) {
339
		changeset_clear(&sec_ch);
340 341 342 343
		return ret;
	}

	// Apply DNSSEC changeset
344
	ret = apply_changeset_directly(&update->a_ctx, new_contents, &sec_ch);
345
	if (ret != KNOT_EOK) {
346
		changeset_clear(&sec_ch);
347 348 349 350
		return ret;
	}

	// Merge changesets
351
	ret = changeset_merge(&update->change, &sec_ch);
352
	if (ret != KNOT_EOK) {
353
		update_rollback(&update->a_ctx);
354
		changeset_clear(&sec_ch);
355 356 357 358 359 360 361 362 363
		return ret;
	}

	// Plan next zone resign.
	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);
	}

364 365 366 367 368 369
	/*
	 * We are not calling update_cleanup, as the rollback data are merged
	 * into the main changeset and will get cleaned up with that.
	 */
	changeset_clear(&sec_ch);

370 371 372
	return KNOT_EOK;
}

373
static int set_new_soa(zone_update_t *update, unsigned serial_policy)
374
{
375 376
	assert(update);

377 378 379 380 381 382
	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);
383
	uint32_t new_serial = serial_next(old_serial, serial_policy);
384 385 386 387 388 389 390 391 392 393 394 395
	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;
}

396
static int commit_incremental(conf_t *conf, zone_update_t *update, zone_contents_t **contents_out)
397
{
398 399
	assert(update);

400 401 402 403 404
	if (changeset_empty(&update->change)) {
		changeset_clear(&update->change);
		return KNOT_EOK;
	}

405
	int ret = KNOT_EOK;
406 407
	if (zone_update_to(update) == NULL) {
		// No SOA in the update, create one according to the current policy
408 409
		conf_val_t val = conf_zone_get(conf, C_SERIAL_POLICY, update->zone->name);
		ret = set_new_soa(update, conf_opt(&val));
410 411 412 413 414 415
		if (ret != KNOT_EOK) {
			return ret;
		}
	}

	// Apply changes.
416
	zone_contents_t *new_contents = NULL;
417
	ret = apply_changeset(&update->a_ctx, update->zone, &update->change, &new_contents);
418 419 420 421
	if (ret != KNOT_EOK) {
		changeset_clear(&update->change);
		return ret;
	}
422

423
	assert(new_contents);
424

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

428 429
	// Sign the update.
	if (dnssec_enable) {
430
		ret = sign_update(update, new_contents);
431
		if (ret != KNOT_EOK) {
432
			update_rollback(&update->a_ctx);
433
			update_free_zone(&new_contents);
434
			changeset_clear(&update->change);
435 436
			return ret;
		}
437
	}
438

439
	// Write changes to journal if all went well. (DNSSEC merged)
440
	ret = zone_change_store(conf, update->zone, &update->change);
441
	if (ret != KNOT_EOK) {
442
		update_rollback(&update->a_ctx);
443 444 445
		update_free_zone(&new_contents);
		return ret;
	}
446

447 448
	*contents_out = new_contents;

449 450 451
	return KNOT_EOK;
}

452
int zone_update_commit(conf_t *conf, zone_update_t *update)
453
{
454 455 456 457 458 459
	if (!conf || !update) {
		return KNOT_EINVAL;
	}

	int ret = KNOT_EOK;
	zone_contents_t *new_contents = NULL;
460
	if (update->flags & UPDATE_INCREMENTAL) {
461 462 463 464 465 466
		ret = commit_incremental(conf, update, &new_contents);
		if (ret != KNOT_EOK) {
			return ret;
		}
	} else {
		return KNOT_ENOTSUP;
467 468
	}

469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
	/* If there is anything to change */
	if (new_contents) {
		/* Switch zone contents. */
		zone_contents_t *old_contents = zone_switch_contents(update->zone, new_contents);

		/* Sync RCU. */
		synchronize_rcu();

		if (update->flags & UPDATE_INCREMENTAL) {
			update_cleanup(&update->a_ctx);
			update_free_zone(&old_contents);
			changeset_clear(&update->change);
		}
	}

	return KNOT_EOK;
485 486
}

487 488 489 490
bool zone_update_no_change(zone_update_t *up)
{
	return changeset_empty(&up->change);
}