Home | History | Annotate | Line # | Download | only in dns
      1  1.15  christos /*	$NetBSD: update.c,v 1.16 2026/01/29 18:37:50 christos Exp $	*/
      2   1.1  christos 
      3   1.1  christos /*
      4   1.1  christos  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
      5   1.1  christos  *
      6  1.11  christos  * SPDX-License-Identifier: MPL-2.0
      7  1.11  christos  *
      8   1.1  christos  * This Source Code Form is subject to the terms of the Mozilla Public
      9   1.1  christos  * License, v. 2.0. If a copy of the MPL was not distributed with this
     10   1.8  christos  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
     11   1.1  christos  *
     12   1.1  christos  * See the COPYRIGHT file distributed with this work for additional
     13   1.1  christos  * information regarding copyright ownership.
     14   1.1  christos  */
     15   1.1  christos 
     16   1.3  christos #include <inttypes.h>
     17   1.3  christos #include <stdbool.h>
     18   1.1  christos #include <time.h>
     19   1.1  christos 
     20   1.1  christos #include <isc/log.h>
     21   1.1  christos #include <isc/magic.h>
     22   1.1  christos #include <isc/mem.h>
     23   1.1  christos #include <isc/netaddr.h>
     24   1.5  christos #include <isc/random.h>
     25  1.13  christos #include <isc/result.h>
     26   1.1  christos #include <isc/serial.h>
     27   1.1  christos #include <isc/stats.h>
     28   1.1  christos #include <isc/stdtime.h>
     29   1.1  christos #include <isc/string.h>
     30   1.1  christos #include <isc/time.h>
     31   1.1  christos #include <isc/util.h>
     32   1.1  christos 
     33   1.1  christos #include <dns/db.h>
     34   1.1  christos #include <dns/dbiterator.h>
     35   1.1  christos #include <dns/diff.h>
     36   1.1  christos #include <dns/dnssec.h>
     37   1.1  christos #include <dns/fixedname.h>
     38   1.1  christos #include <dns/journal.h>
     39   1.6  christos #include <dns/kasp.h>
     40   1.1  christos #include <dns/keyvalues.h>
     41   1.1  christos #include <dns/log.h>
     42   1.1  christos #include <dns/message.h>
     43   1.1  christos #include <dns/nsec.h>
     44   1.1  christos #include <dns/nsec3.h>
     45   1.1  christos #include <dns/private.h>
     46   1.1  christos #include <dns/rdataclass.h>
     47   1.1  christos #include <dns/rdataset.h>
     48   1.1  christos #include <dns/rdatasetiter.h>
     49   1.1  christos #include <dns/rdatastruct.h>
     50   1.1  christos #include <dns/rdatatype.h>
     51  1.15  christos #include <dns/skr.h>
     52   1.1  christos #include <dns/soa.h>
     53   1.1  christos #include <dns/ssu.h>
     54   1.4  christos #include <dns/stats.h>
     55   1.1  christos #include <dns/tsig.h>
     56   1.1  christos #include <dns/update.h>
     57   1.1  christos #include <dns/view.h>
     58   1.1  christos #include <dns/zone.h>
     59   1.1  christos #include <dns/zt.h>
     60   1.1  christos 
     61   1.1  christos /**************************************************************************/
     62   1.1  christos 
     63   1.6  christos #define STATE_MAGIC	       ISC_MAGIC('S', 'T', 'T', 'E')
     64   1.6  christos #define DNS_STATE_VALID(state) ISC_MAGIC_VALID(state, STATE_MAGIC)
     65   1.1  christos 
     66   1.1  christos /*%
     67   1.1  christos  * Log level for tracing dynamic update protocol requests.
     68   1.1  christos  */
     69   1.6  christos #define LOGLEVEL_PROTOCOL ISC_LOG_INFO
     70   1.1  christos 
     71   1.1  christos /*%
     72   1.1  christos  * Log level for low-level debug tracing.
     73   1.1  christos  */
     74   1.6  christos #define LOGLEVEL_DEBUG ISC_LOG_DEBUG(8)
     75   1.1  christos 
     76   1.1  christos /**************************************************************************/
     77   1.1  christos 
     78   1.1  christos typedef struct rr rr_t;
     79   1.1  christos 
     80   1.1  christos struct rr {
     81   1.1  christos 	/* dns_name_t name; */
     82   1.6  christos 	uint32_t ttl;
     83   1.6  christos 	dns_rdata_t rdata;
     84   1.1  christos };
     85   1.1  christos 
     86   1.1  christos typedef struct update_event update_event_t;
     87   1.1  christos 
     88   1.1  christos /**************************************************************************/
     89   1.1  christos 
     90   1.1  christos static void
     91   1.6  christos update_log(dns_update_log_t *callback, dns_zone_t *zone, int level,
     92   1.6  christos 	   const char *fmt, ...) ISC_FORMAT_PRINTF(4, 5);
     93   1.1  christos 
     94   1.1  christos static void
     95   1.6  christos update_log(dns_update_log_t *callback, dns_zone_t *zone, int level,
     96   1.6  christos 	   const char *fmt, ...) {
     97   1.1  christos 	va_list ap;
     98   1.1  christos 	char message[4096];
     99   1.1  christos 
    100   1.6  christos 	if (callback == NULL) {
    101   1.1  christos 		return;
    102   1.6  christos 	}
    103   1.1  christos 
    104   1.7  christos 	if (!isc_log_wouldlog(dns_lctx, level)) {
    105   1.1  christos 		return;
    106   1.6  christos 	}
    107   1.1  christos 
    108   1.1  christos 	va_start(ap, fmt);
    109   1.1  christos 	vsnprintf(message, sizeof(message), fmt, ap);
    110   1.1  christos 	va_end(ap);
    111   1.1  christos 
    112   1.1  christos 	(callback->func)(callback->arg, zone, level, message);
    113   1.1  christos }
    114   1.1  christos 
    115   1.1  christos /*%
    116   1.1  christos  * Update a single RR in version 'ver' of 'db' and log the
    117   1.1  christos  * update in 'diff'.
    118   1.1  christos  *
    119   1.1  christos  * Ensures:
    120   1.1  christos  * \li	'*tuple' == NULL.  Either the tuple is freed, or its
    121   1.1  christos  *	ownership has been transferred to the diff.
    122   1.1  christos  */
    123   1.1  christos static isc_result_t
    124   1.1  christos do_one_tuple(dns_difftuple_t **tuple, dns_db_t *db, dns_dbversion_t *ver,
    125   1.6  christos 	     dns_diff_t *diff) {
    126   1.1  christos 	dns_diff_t temp_diff;
    127   1.1  christos 	isc_result_t result;
    128   1.1  christos 
    129   1.1  christos 	/*
    130   1.1  christos 	 * Create a singleton diff.
    131   1.1  christos 	 */
    132   1.1  christos 	dns_diff_init(diff->mctx, &temp_diff);
    133   1.1  christos 	ISC_LIST_APPEND(temp_diff.tuples, *tuple, link);
    134   1.1  christos 
    135   1.1  christos 	/*
    136   1.1  christos 	 * Apply it to the database.
    137   1.1  christos 	 */
    138   1.1  christos 	result = dns_diff_apply(&temp_diff, db, ver);
    139   1.1  christos 	ISC_LIST_UNLINK(temp_diff.tuples, *tuple, link);
    140   1.1  christos 	if (result != ISC_R_SUCCESS) {
    141   1.1  christos 		dns_difftuple_free(tuple);
    142  1.15  christos 		return result;
    143   1.1  christos 	}
    144   1.1  christos 
    145   1.1  christos 	/*
    146   1.1  christos 	 * Merge it into the current pending journal entry.
    147   1.1  christos 	 */
    148   1.1  christos 	dns_diff_appendminimal(diff, tuple);
    149   1.1  christos 
    150   1.1  christos 	/*
    151   1.1  christos 	 * Do not clear temp_diff.
    152   1.1  christos 	 */
    153  1.15  christos 	return ISC_R_SUCCESS;
    154   1.1  christos }
    155   1.1  christos 
    156   1.1  christos static isc_result_t
    157   1.1  christos update_one_rr(dns_db_t *db, dns_dbversion_t *ver, dns_diff_t *diff,
    158   1.1  christos 	      dns_diffop_t op, dns_name_t *name, dns_ttl_t ttl,
    159   1.6  christos 	      dns_rdata_t *rdata) {
    160   1.1  christos 	dns_difftuple_t *tuple = NULL;
    161   1.1  christos 	isc_result_t result;
    162   1.6  christos 	result = dns_difftuple_create(diff->mctx, op, name, ttl, rdata, &tuple);
    163   1.6  christos 	if (result != ISC_R_SUCCESS) {
    164  1.15  christos 		return result;
    165   1.6  christos 	}
    166  1.15  christos 	return do_one_tuple(&tuple, db, ver, diff);
    167   1.1  christos }
    168   1.1  christos 
    169   1.1  christos /**************************************************************************/
    170   1.1  christos /*
    171   1.1  christos  * Callback-style iteration over rdatasets and rdatas.
    172   1.1  christos  *
    173   1.1  christos  * foreach_rrset() can be used to iterate over the RRsets
    174   1.1  christos  * of a name and call a callback function with each
    175   1.1  christos  * one.  Similarly, foreach_rr() can be used to iterate
    176   1.1  christos  * over the individual RRs at name, optionally restricted
    177   1.1  christos  * to RRs of a given type.
    178   1.1  christos  *
    179   1.1  christos  * The callback functions are called "actions" and take
    180   1.1  christos  * two arguments: a void pointer for passing arbitrary
    181   1.1  christos  * context information, and a pointer to the current RRset
    182   1.1  christos  * or RR.  By convention, their names end in "_action".
    183   1.1  christos  */
    184   1.1  christos 
    185   1.1  christos /*
    186   1.1  christos  * XXXRTH  We might want to make this public somewhere in libdns.
    187   1.1  christos  */
    188   1.1  christos 
    189   1.1  christos /*%
    190   1.1  christos  * Function type for foreach_rrset() iterator actions.
    191   1.1  christos  */
    192   1.6  christos typedef isc_result_t
    193   1.6  christos rrset_func(void *data, dns_rdataset_t *rrset);
    194   1.1  christos 
    195   1.1  christos /*%
    196   1.1  christos  * Function type for foreach_rr() iterator actions.
    197   1.1  christos  */
    198   1.6  christos typedef isc_result_t
    199   1.6  christos rr_func(void *data, rr_t *rr);
    200   1.1  christos 
    201   1.1  christos /*%
    202   1.1  christos  * Internal context struct for foreach_node_rr().
    203   1.1  christos  */
    204   1.1  christos typedef struct {
    205   1.6  christos 	rr_func *rr_action;
    206   1.6  christos 	void *rr_action_data;
    207   1.1  christos } foreach_node_rr_ctx_t;
    208   1.1  christos 
    209   1.1  christos /*%
    210   1.1  christos  * Internal helper function for foreach_node_rr().
    211   1.1  christos  */
    212   1.1  christos static isc_result_t
    213   1.1  christos foreach_node_rr_action(void *data, dns_rdataset_t *rdataset) {
    214   1.1  christos 	isc_result_t result;
    215   1.1  christos 	foreach_node_rr_ctx_t *ctx = data;
    216   1.6  christos 	for (result = dns_rdataset_first(rdataset); result == ISC_R_SUCCESS;
    217   1.1  christos 	     result = dns_rdataset_next(rdataset))
    218   1.1  christos 	{
    219   1.1  christos 		rr_t rr = { 0, DNS_RDATA_INIT };
    220   1.1  christos 
    221   1.1  christos 		dns_rdataset_current(rdataset, &rr.rdata);
    222   1.1  christos 		rr.ttl = rdataset->ttl;
    223   1.1  christos 		result = (*ctx->rr_action)(ctx->rr_action_data, &rr);
    224   1.6  christos 		if (result != ISC_R_SUCCESS) {
    225  1.15  christos 			return result;
    226   1.6  christos 		}
    227   1.1  christos 	}
    228   1.6  christos 	if (result != ISC_R_NOMORE) {
    229  1.15  christos 		return result;
    230   1.6  christos 	}
    231  1.15  christos 	return ISC_R_SUCCESS;
    232   1.1  christos }
    233   1.1  christos 
    234   1.1  christos /*%
    235   1.1  christos  * For each rdataset of 'name' in 'ver' of 'db', call 'action'
    236   1.1  christos  * with the rdataset and 'action_data' as arguments.  If the name
    237   1.1  christos  * does not exist, do nothing.
    238   1.1  christos  *
    239   1.1  christos  * If 'action' returns an error, abort iteration and return the error.
    240   1.1  christos  */
    241   1.1  christos static isc_result_t
    242   1.1  christos foreach_rrset(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
    243   1.6  christos 	      rrset_func *action, void *action_data) {
    244   1.1  christos 	isc_result_t result;
    245   1.1  christos 	dns_dbnode_t *node;
    246   1.1  christos 	dns_rdatasetiter_t *iter;
    247   1.1  christos 
    248   1.1  christos 	node = NULL;
    249   1.3  christos 	result = dns_db_findnode(db, name, false, &node);
    250   1.6  christos 	if (result == ISC_R_NOTFOUND) {
    251  1.15  christos 		return ISC_R_SUCCESS;
    252   1.6  christos 	}
    253   1.6  christos 	if (result != ISC_R_SUCCESS) {
    254  1.15  christos 		return result;
    255   1.6  christos 	}
    256   1.1  christos 
    257   1.1  christos 	iter = NULL;
    258  1.12  christos 	result = dns_db_allrdatasets(db, node, ver, 0, (isc_stdtime_t)0, &iter);
    259   1.6  christos 	if (result != ISC_R_SUCCESS) {
    260   1.1  christos 		goto cleanup_node;
    261   1.6  christos 	}
    262   1.1  christos 
    263   1.6  christos 	for (result = dns_rdatasetiter_first(iter); result == ISC_R_SUCCESS;
    264   1.1  christos 	     result = dns_rdatasetiter_next(iter))
    265   1.1  christos 	{
    266   1.1  christos 		dns_rdataset_t rdataset;
    267   1.1  christos 
    268   1.1  christos 		dns_rdataset_init(&rdataset);
    269   1.1  christos 		dns_rdatasetiter_current(iter, &rdataset);
    270   1.1  christos 
    271   1.1  christos 		result = (*action)(action_data, &rdataset);
    272   1.1  christos 
    273   1.1  christos 		dns_rdataset_disassociate(&rdataset);
    274   1.6  christos 		if (result != ISC_R_SUCCESS) {
    275   1.1  christos 			goto cleanup_iterator;
    276   1.6  christos 		}
    277   1.1  christos 	}
    278   1.6  christos 	if (result == ISC_R_NOMORE) {
    279   1.1  christos 		result = ISC_R_SUCCESS;
    280   1.6  christos 	}
    281   1.1  christos 
    282   1.6  christos cleanup_iterator:
    283   1.1  christos 	dns_rdatasetiter_destroy(&iter);
    284   1.1  christos 
    285   1.6  christos cleanup_node:
    286   1.1  christos 	dns_db_detachnode(db, &node);
    287   1.1  christos 
    288  1.15  christos 	return result;
    289   1.1  christos }
    290   1.1  christos 
    291   1.1  christos /*%
    292   1.1  christos  * For each RR of 'name' in 'ver' of 'db', call 'action'
    293   1.1  christos  * with the RR and 'action_data' as arguments.  If the name
    294   1.1  christos  * does not exist, do nothing.
    295   1.1  christos  *
    296   1.1  christos  * If 'action' returns an error, abort iteration
    297   1.1  christos  * and return the error.
    298   1.1  christos  */
    299   1.1  christos static isc_result_t
    300   1.1  christos foreach_node_rr(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
    301   1.6  christos 		rr_func *rr_action, void *rr_action_data) {
    302   1.1  christos 	foreach_node_rr_ctx_t ctx;
    303   1.1  christos 	ctx.rr_action = rr_action;
    304   1.1  christos 	ctx.rr_action_data = rr_action_data;
    305  1.15  christos 	return foreach_rrset(db, ver, name, foreach_node_rr_action, &ctx);
    306   1.1  christos }
    307   1.1  christos 
    308   1.1  christos /*%
    309   1.1  christos  * For each of the RRs specified by 'db', 'ver', 'name', 'type',
    310   1.1  christos  * (which can be dns_rdatatype_any to match any type), and 'covers', call
    311   1.1  christos  * 'action' with the RR and 'action_data' as arguments. If the name
    312   1.1  christos  * does not exist, or if no RRset of the given type exists at the name,
    313   1.1  christos  * do nothing.
    314   1.1  christos  *
    315   1.1  christos  * If 'action' returns an error, abort iteration and return the error.
    316   1.1  christos  */
    317   1.1  christos static isc_result_t
    318   1.1  christos foreach_rr(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
    319   1.1  christos 	   dns_rdatatype_t type, dns_rdatatype_t covers, rr_func *rr_action,
    320   1.6  christos 	   void *rr_action_data) {
    321   1.1  christos 	isc_result_t result;
    322   1.1  christos 	dns_dbnode_t *node;
    323   1.1  christos 	dns_rdataset_t rdataset;
    324   1.1  christos 
    325   1.6  christos 	if (type == dns_rdatatype_any) {
    326  1.15  christos 		return foreach_node_rr(db, ver, name, rr_action,
    327  1.15  christos 				       rr_action_data);
    328   1.6  christos 	}
    329   1.1  christos 
    330   1.1  christos 	node = NULL;
    331   1.1  christos 	if (type == dns_rdatatype_nsec3 ||
    332   1.1  christos 	    (type == dns_rdatatype_rrsig && covers == dns_rdatatype_nsec3))
    333   1.6  christos 	{
    334   1.3  christos 		result = dns_db_findnsec3node(db, name, false, &node);
    335   1.6  christos 	} else {
    336   1.3  christos 		result = dns_db_findnode(db, name, false, &node);
    337   1.6  christos 	}
    338   1.6  christos 	if (result == ISC_R_NOTFOUND) {
    339  1.15  christos 		return ISC_R_SUCCESS;
    340   1.6  christos 	}
    341   1.6  christos 	if (result != ISC_R_SUCCESS) {
    342  1.15  christos 		return result;
    343   1.6  christos 	}
    344   1.1  christos 
    345   1.1  christos 	dns_rdataset_init(&rdataset);
    346   1.1  christos 	result = dns_db_findrdataset(db, node, ver, type, covers,
    347   1.6  christos 				     (isc_stdtime_t)0, &rdataset, NULL);
    348   1.1  christos 	if (result == ISC_R_NOTFOUND) {
    349   1.1  christos 		result = ISC_R_SUCCESS;
    350   1.1  christos 		goto cleanup_node;
    351   1.1  christos 	}
    352   1.6  christos 	if (result != ISC_R_SUCCESS) {
    353   1.1  christos 		goto cleanup_node;
    354   1.6  christos 	}
    355   1.1  christos 
    356   1.6  christos 	for (result = dns_rdataset_first(&rdataset); result == ISC_R_SUCCESS;
    357   1.1  christos 	     result = dns_rdataset_next(&rdataset))
    358   1.1  christos 	{
    359   1.1  christos 		rr_t rr = { 0, DNS_RDATA_INIT };
    360   1.1  christos 		dns_rdataset_current(&rdataset, &rr.rdata);
    361   1.1  christos 		rr.ttl = rdataset.ttl;
    362   1.1  christos 		result = (*rr_action)(rr_action_data, &rr);
    363   1.6  christos 		if (result != ISC_R_SUCCESS) {
    364   1.1  christos 			goto cleanup_rdataset;
    365   1.6  christos 		}
    366   1.1  christos 	}
    367   1.6  christos 	if (result != ISC_R_NOMORE) {
    368   1.1  christos 		goto cleanup_rdataset;
    369   1.6  christos 	}
    370   1.1  christos 	result = ISC_R_SUCCESS;
    371   1.1  christos 
    372   1.6  christos cleanup_rdataset:
    373   1.1  christos 	dns_rdataset_disassociate(&rdataset);
    374   1.6  christos cleanup_node:
    375   1.1  christos 	dns_db_detachnode(db, &node);
    376   1.1  christos 
    377  1.15  christos 	return result;
    378   1.1  christos }
    379   1.1  christos 
    380   1.1  christos /**************************************************************************/
    381   1.1  christos /*
    382   1.1  christos  * Various tests on the database contents (for prerequisites, etc).
    383   1.1  christos  */
    384   1.1  christos 
    385   1.1  christos /*%
    386   1.1  christos  * Function type for predicate functions that compare a database RR 'db_rr'
    387   1.1  christos  * against an update RR 'update_rr'.
    388   1.1  christos  */
    389   1.6  christos typedef bool
    390   1.6  christos rr_predicate(dns_rdata_t *update_rr, dns_rdata_t *db_rr);
    391   1.1  christos 
    392   1.1  christos /*%
    393   1.1  christos  * Helper function for rrset_exists().
    394   1.1  christos  */
    395   1.1  christos static isc_result_t
    396   1.1  christos rrset_exists_action(void *data, rr_t *rr) {
    397   1.1  christos 	UNUSED(data);
    398   1.1  christos 	UNUSED(rr);
    399  1.15  christos 	return ISC_R_EXISTS;
    400   1.1  christos }
    401   1.1  christos 
    402   1.1  christos /*%
    403   1.1  christos  * Utility macro for RR existence checking functions.
    404   1.1  christos  *
    405   1.1  christos  * If the variable 'result' has the value ISC_R_EXISTS or
    406   1.3  christos  * ISC_R_SUCCESS, set *exists to true or false,
    407   1.1  christos  * respectively, and return success.
    408   1.1  christos  *
    409   1.1  christos  * If 'result' has any other value, there was a failure.
    410   1.1  christos  * Return the failure result code and do not set *exists.
    411   1.1  christos  *
    412   1.1  christos  * This would be more readable as "do { if ... } while(0)",
    413   1.1  christos  * but that form generates tons of warnings on Solaris 2.6.
    414   1.1  christos  */
    415   1.6  christos #define RETURN_EXISTENCE_FLAG                                         \
    416   1.6  christos 	return ((result == ISC_R_EXISTS)                              \
    417   1.6  christos 			? (*exists = true, ISC_R_SUCCESS)             \
    418   1.6  christos 			: ((result == ISC_R_SUCCESS)                  \
    419   1.6  christos 				   ? (*exists = false, ISC_R_SUCCESS) \
    420   1.6  christos 				   : result))
    421   1.1  christos 
    422   1.1  christos /*%
    423   1.1  christos  * Set '*exists' to true iff an rrset of the given type exists,
    424   1.1  christos  * to false otherwise.
    425   1.1  christos  */
    426   1.1  christos static isc_result_t
    427   1.1  christos rrset_exists(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
    428   1.6  christos 	     dns_rdatatype_t type, dns_rdatatype_t covers, bool *exists) {
    429   1.1  christos 	isc_result_t result;
    430   1.6  christos 	result = foreach_rr(db, ver, name, type, covers, rrset_exists_action,
    431   1.6  christos 			    NULL);
    432   1.1  christos 	RETURN_EXISTENCE_FLAG;
    433   1.1  christos }
    434   1.1  christos 
    435   1.1  christos /*%
    436   1.1  christos  * Set '*visible' to true if the RRset exists and is part of the
    437   1.1  christos  * visible zone.  Otherwise '*visible' is set to false unless a
    438   1.1  christos  * error occurs.
    439   1.1  christos  */
    440   1.1  christos static isc_result_t
    441   1.1  christos rrset_visible(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
    442   1.6  christos 	      dns_rdatatype_t type, bool *visible) {
    443   1.1  christos 	isc_result_t result;
    444   1.1  christos 	dns_fixedname_t fixed;
    445   1.1  christos 
    446   1.1  christos 	dns_fixedname_init(&fixed);
    447   1.1  christos 	result = dns_db_find(db, name, ver, type, DNS_DBFIND_NOWILD,
    448   1.6  christos 			     (isc_stdtime_t)0, NULL, dns_fixedname_name(&fixed),
    449   1.6  christos 			     NULL, NULL);
    450   1.1  christos 	switch (result) {
    451   1.1  christos 	case ISC_R_SUCCESS:
    452   1.3  christos 		*visible = true;
    453   1.1  christos 		break;
    454   1.1  christos 	/*
    455   1.1  christos 	 * Glue, obscured, deleted or replaced records.
    456   1.1  christos 	 */
    457   1.1  christos 	case DNS_R_DELEGATION:
    458   1.1  christos 	case DNS_R_DNAME:
    459   1.1  christos 	case DNS_R_CNAME:
    460   1.1  christos 	case DNS_R_NXDOMAIN:
    461   1.1  christos 	case DNS_R_NXRRSET:
    462   1.1  christos 	case DNS_R_EMPTYNAME:
    463   1.1  christos 	case DNS_R_COVERINGNSEC:
    464   1.3  christos 		*visible = false;
    465   1.1  christos 		result = ISC_R_SUCCESS;
    466   1.1  christos 		break;
    467   1.1  christos 	default:
    468   1.6  christos 		*visible = false; /* silence false compiler warning */
    469   1.1  christos 		break;
    470   1.1  christos 	}
    471  1.15  christos 	return result;
    472   1.1  christos }
    473   1.1  christos 
    474   1.1  christos /*%
    475   1.1  christos  * Context struct and helper function for name_exists().
    476   1.1  christos  */
    477   1.1  christos 
    478   1.1  christos static isc_result_t
    479   1.1  christos name_exists_action(void *data, dns_rdataset_t *rrset) {
    480   1.1  christos 	UNUSED(data);
    481   1.1  christos 	UNUSED(rrset);
    482  1.15  christos 	return ISC_R_EXISTS;
    483   1.1  christos }
    484   1.1  christos 
    485   1.1  christos /*%
    486   1.1  christos  * Set '*exists' to true iff the given name exists, to false otherwise.
    487   1.1  christos  */
    488   1.1  christos static isc_result_t
    489   1.1  christos name_exists(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
    490   1.6  christos 	    bool *exists) {
    491   1.1  christos 	isc_result_t result;
    492   1.6  christos 	result = foreach_rrset(db, ver, name, name_exists_action, NULL);
    493   1.1  christos 	RETURN_EXISTENCE_FLAG;
    494   1.1  christos }
    495   1.1  christos 
    496   1.1  christos /**************************************************************************/
    497   1.1  christos /*
    498   1.1  christos  * Checking of "RRset exists (value dependent)" prerequisites.
    499   1.1  christos  *
    500   1.1  christos  * In the RFC2136 section 3.2.5, this is the pseudocode involving
    501   1.1  christos  * a variable called "temp", a mapping of <name, type> tuples to rrsets.
    502   1.1  christos  *
    503   1.1  christos  * Here, we represent the "temp" data structure as (non-minimal) "dns_diff_t"
    504   1.1  christos  * where each tuple has op==DNS_DIFFOP_EXISTS.
    505   1.1  christos  */
    506   1.1  christos 
    507   1.1  christos /*%
    508   1.1  christos  * A comparison function defining the sorting order for the entries
    509   1.1  christos  * in the "temp" data structure.  The major sort key is the owner name,
    510   1.1  christos  * followed by the type and rdata.
    511   1.1  christos  */
    512   1.1  christos static int
    513   1.1  christos temp_order(const void *av, const void *bv) {
    514   1.6  christos 	dns_difftuple_t const *const *ap = av;
    515   1.6  christos 	dns_difftuple_t const *const *bp = bv;
    516   1.1  christos 	dns_difftuple_t const *a = *ap;
    517   1.1  christos 	dns_difftuple_t const *b = *bp;
    518   1.1  christos 	int r;
    519   1.1  christos 	r = dns_name_compare(&a->name, &b->name);
    520   1.6  christos 	if (r != 0) {
    521  1.15  christos 		return r;
    522   1.6  christos 	}
    523   1.1  christos 	r = (b->rdata.type - a->rdata.type);
    524   1.6  christos 	if (r != 0) {
    525  1.15  christos 		return r;
    526   1.6  christos 	}
    527   1.1  christos 	r = dns_rdata_casecompare(&a->rdata, &b->rdata);
    528  1.15  christos 	return r;
    529   1.1  christos }
    530   1.1  christos 
    531   1.1  christos /**************************************************************************/
    532   1.1  christos /*
    533   1.1  christos  * Conditional deletion of RRs.
    534   1.1  christos  */
    535   1.1  christos 
    536   1.1  christos /*%
    537   1.1  christos  * Context structure for delete_if().
    538   1.1  christos  */
    539   1.1  christos 
    540   1.1  christos typedef struct {
    541   1.1  christos 	rr_predicate *predicate;
    542   1.1  christos 	dns_db_t *db;
    543   1.1  christos 	dns_dbversion_t *ver;
    544   1.1  christos 	dns_diff_t *diff;
    545   1.1  christos 	dns_name_t *name;
    546   1.1  christos 	dns_rdata_t *update_rr;
    547   1.1  christos } conditional_delete_ctx_t;
    548   1.1  christos 
    549   1.1  christos /*%
    550   1.1  christos  * Predicate functions for delete_if().
    551   1.1  christos  */
    552   1.1  christos 
    553   1.1  christos /*%
    554   1.1  christos  * Return true always.
    555   1.1  christos  */
    556   1.3  christos static bool
    557   1.1  christos true_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
    558   1.1  christos 	UNUSED(update_rr);
    559   1.1  christos 	UNUSED(db_rr);
    560  1.15  christos 	return true;
    561   1.1  christos }
    562   1.1  christos 
    563   1.1  christos /*%
    564   1.1  christos  * Return true if the record is a RRSIG.
    565   1.1  christos  */
    566   1.3  christos static bool
    567   1.1  christos rrsig_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
    568   1.1  christos 	UNUSED(update_rr);
    569  1.15  christos 	return (db_rr->type == dns_rdatatype_rrsig) ? true : false;
    570   1.1  christos }
    571   1.1  christos 
    572   1.1  christos /*%
    573   1.1  christos  * Internal helper function for delete_if().
    574   1.1  christos  */
    575   1.1  christos static isc_result_t
    576   1.1  christos delete_if_action(void *data, rr_t *rr) {
    577   1.1  christos 	conditional_delete_ctx_t *ctx = data;
    578   1.1  christos 	if ((*ctx->predicate)(ctx->update_rr, &rr->rdata)) {
    579   1.1  christos 		isc_result_t result;
    580   1.1  christos 		result = update_one_rr(ctx->db, ctx->ver, ctx->diff,
    581   1.6  christos 				       DNS_DIFFOP_DEL, ctx->name, rr->ttl,
    582   1.6  christos 				       &rr->rdata);
    583  1.15  christos 		return result;
    584   1.1  christos 	} else {
    585  1.15  christos 		return ISC_R_SUCCESS;
    586   1.1  christos 	}
    587   1.1  christos }
    588   1.1  christos 
    589   1.1  christos /*%
    590   1.1  christos  * Conditionally delete RRs.  Apply 'predicate' to the RRs
    591   1.1  christos  * specified by 'db', 'ver', 'name', and 'type' (which can
    592   1.1  christos  * be dns_rdatatype_any to match any type).  Delete those
    593   1.1  christos  * RRs for which the predicate returns true, and log the
    594   1.1  christos  * deletions in 'diff'.
    595   1.1  christos  */
    596   1.1  christos static isc_result_t
    597   1.1  christos delete_if(rr_predicate *predicate, dns_db_t *db, dns_dbversion_t *ver,
    598   1.1  christos 	  dns_name_t *name, dns_rdatatype_t type, dns_rdatatype_t covers,
    599   1.6  christos 	  dns_rdata_t *update_rr, dns_diff_t *diff) {
    600   1.1  christos 	conditional_delete_ctx_t ctx;
    601   1.1  christos 	ctx.predicate = predicate;
    602   1.1  christos 	ctx.db = db;
    603   1.1  christos 	ctx.ver = ver;
    604   1.1  christos 	ctx.diff = diff;
    605   1.1  christos 	ctx.name = name;
    606   1.1  christos 	ctx.update_rr = update_rr;
    607  1.15  christos 	return foreach_rr(db, ver, name, type, covers, delete_if_action, &ctx);
    608   1.1  christos }
    609   1.1  christos 
    610   1.1  christos /**************************************************************************/
    611   1.1  christos /*
    612   1.1  christos  * Incremental updating of NSECs and RRSIGs.
    613   1.1  christos  */
    614   1.1  christos 
    615   1.1  christos /*%
    616   1.1  christos  * We abuse the dns_diff_t type to represent a set of domain names
    617   1.1  christos  * affected by the update.
    618   1.1  christos  */
    619   1.1  christos static isc_result_t
    620   1.1  christos namelist_append_name(dns_diff_t *list, dns_name_t *name) {
    621   1.1  christos 	isc_result_t result;
    622   1.1  christos 	dns_difftuple_t *tuple = NULL;
    623   1.1  christos 	static dns_rdata_t dummy_rdata = DNS_RDATA_INIT;
    624   1.1  christos 
    625   1.1  christos 	CHECK(dns_difftuple_create(list->mctx, DNS_DIFFOP_EXISTS, name, 0,
    626   1.1  christos 				   &dummy_rdata, &tuple));
    627   1.1  christos 	dns_diff_append(list, &tuple);
    628  1.16  christos cleanup:
    629  1.15  christos 	return result;
    630   1.1  christos }
    631   1.1  christos 
    632   1.1  christos static isc_result_t
    633   1.6  christos namelist_append_subdomain(dns_db_t *db, dns_name_t *name,
    634   1.6  christos 			  dns_diff_t *affected) {
    635   1.1  christos 	isc_result_t result;
    636   1.1  christos 	dns_fixedname_t fixedname;
    637   1.1  christos 	dns_name_t *child;
    638   1.1  christos 	dns_dbiterator_t *dbit = NULL;
    639   1.1  christos 
    640   1.1  christos 	child = dns_fixedname_initname(&fixedname);
    641   1.1  christos 
    642   1.1  christos 	CHECK(dns_db_createiterator(db, DNS_DB_NONSEC3, &dbit));
    643   1.1  christos 
    644   1.6  christos 	for (result = dns_dbiterator_seek(dbit, name); result == ISC_R_SUCCESS;
    645   1.1  christos 	     result = dns_dbiterator_next(dbit))
    646   1.1  christos 	{
    647   1.1  christos 		dns_dbnode_t *node = NULL;
    648   1.1  christos 		CHECK(dns_dbiterator_current(dbit, &node, child));
    649   1.1  christos 		dns_db_detachnode(db, &node);
    650   1.6  christos 		if (!dns_name_issubdomain(child, name)) {
    651   1.1  christos 			break;
    652   1.6  christos 		}
    653   1.1  christos 		CHECK(namelist_append_name(affected, child));
    654   1.1  christos 	}
    655   1.6  christos 	if (result == ISC_R_NOMORE) {
    656   1.1  christos 		result = ISC_R_SUCCESS;
    657   1.6  christos 	}
    658  1.16  christos cleanup:
    659   1.6  christos 	if (dbit != NULL) {
    660   1.1  christos 		dns_dbiterator_destroy(&dbit);
    661   1.6  christos 	}
    662  1.15  christos 	return result;
    663   1.1  christos }
    664   1.1  christos 
    665   1.1  christos /*%
    666   1.1  christos  * Helper function for non_nsec_rrset_exists().
    667   1.1  christos  */
    668   1.1  christos static isc_result_t
    669   1.1  christos is_non_nsec_action(void *data, dns_rdataset_t *rrset) {
    670   1.1  christos 	UNUSED(data);
    671   1.1  christos 	if (!(rrset->type == dns_rdatatype_nsec ||
    672   1.1  christos 	      rrset->type == dns_rdatatype_nsec3 ||
    673   1.1  christos 	      (rrset->type == dns_rdatatype_rrsig &&
    674   1.1  christos 	       (rrset->covers == dns_rdatatype_nsec ||
    675   1.1  christos 		rrset->covers == dns_rdatatype_nsec3))))
    676   1.6  christos 	{
    677  1.15  christos 		return ISC_R_EXISTS;
    678   1.6  christos 	}
    679  1.15  christos 	return ISC_R_SUCCESS;
    680   1.1  christos }
    681   1.1  christos 
    682   1.1  christos /*%
    683   1.1  christos  * Check whether there is an rrset other than a NSEC or RRSIG NSEC,
    684   1.1  christos  * i.e., anything that justifies the continued existence of a name
    685   1.1  christos  * after a secure update.
    686   1.1  christos  *
    687   1.3  christos  * If such an rrset exists, set '*exists' to true.
    688   1.3  christos  * Otherwise, set it to false.
    689   1.1  christos  */
    690   1.1  christos static isc_result_t
    691   1.6  christos non_nsec_rrset_exists(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
    692   1.6  christos 		      bool *exists) {
    693   1.1  christos 	isc_result_t result;
    694   1.1  christos 	result = foreach_rrset(db, ver, name, is_non_nsec_action, NULL);
    695   1.1  christos 	RETURN_EXISTENCE_FLAG;
    696   1.1  christos }
    697   1.1  christos 
    698   1.1  christos /*%
    699   1.1  christos  * A comparison function for sorting dns_diff_t:s by name.
    700   1.1  christos  */
    701   1.1  christos static int
    702   1.1  christos name_order(const void *av, const void *bv) {
    703   1.6  christos 	dns_difftuple_t const *const *ap = av;
    704   1.6  christos 	dns_difftuple_t const *const *bp = bv;
    705   1.1  christos 	dns_difftuple_t const *a = *ap;
    706   1.1  christos 	dns_difftuple_t const *b = *bp;
    707  1.15  christos 	return dns_name_compare(&a->name, &b->name);
    708   1.1  christos }
    709   1.1  christos 
    710   1.1  christos static isc_result_t
    711   1.1  christos uniqify_name_list(dns_diff_t *list) {
    712   1.1  christos 	isc_result_t result;
    713   1.1  christos 	dns_difftuple_t *p, *q;
    714   1.1  christos 
    715   1.1  christos 	CHECK(dns_diff_sort(list, name_order));
    716   1.1  christos 
    717   1.1  christos 	p = ISC_LIST_HEAD(list->tuples);
    718   1.1  christos 	while (p != NULL) {
    719   1.1  christos 		do {
    720   1.1  christos 			q = ISC_LIST_NEXT(p, link);
    721   1.6  christos 			if (q == NULL || !dns_name_equal(&p->name, &q->name)) {
    722   1.1  christos 				break;
    723   1.6  christos 			}
    724   1.1  christos 			ISC_LIST_UNLINK(list->tuples, q, link);
    725   1.1  christos 			dns_difftuple_free(&q);
    726   1.1  christos 		} while (1);
    727   1.1  christos 		p = ISC_LIST_NEXT(p, link);
    728   1.1  christos 	}
    729  1.16  christos cleanup:
    730  1.15  christos 	return result;
    731   1.1  christos }
    732   1.1  christos 
    733   1.1  christos static isc_result_t
    734   1.6  christos is_active(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name, bool *flag,
    735   1.6  christos 	  bool *cut, bool *unsecure) {
    736   1.1  christos 	isc_result_t result;
    737   1.1  christos 	dns_fixedname_t foundname;
    738   1.1  christos 	dns_fixedname_init(&foundname);
    739   1.1  christos 	result = dns_db_find(db, name, ver, dns_rdatatype_any,
    740   1.1  christos 			     DNS_DBFIND_GLUEOK | DNS_DBFIND_NOWILD,
    741   1.6  christos 			     (isc_stdtime_t)0, NULL,
    742   1.6  christos 			     dns_fixedname_name(&foundname), NULL, NULL);
    743   1.1  christos 	if (result == ISC_R_SUCCESS || result == DNS_R_EMPTYNAME) {
    744   1.3  christos 		*flag = true;
    745   1.3  christos 		*cut = false;
    746  1.15  christos 		SET_IF_NOT_NULL(unsecure, false);
    747  1.15  christos 		return ISC_R_SUCCESS;
    748   1.1  christos 	} else if (result == DNS_R_ZONECUT) {
    749   1.3  christos 		*flag = true;
    750   1.3  christos 		*cut = true;
    751   1.1  christos 		if (unsecure != NULL) {
    752   1.1  christos 			/*
    753   1.1  christos 			 * We are at the zonecut.  Check to see if there
    754   1.1  christos 			 * is a DS RRset.
    755   1.1  christos 			 */
    756   1.1  christos 			if (dns_db_find(db, name, ver, dns_rdatatype_ds, 0,
    757   1.6  christos 					(isc_stdtime_t)0, NULL,
    758   1.6  christos 					dns_fixedname_name(&foundname), NULL,
    759   1.6  christos 					NULL) == DNS_R_NXRRSET)
    760   1.6  christos 			{
    761   1.3  christos 				*unsecure = true;
    762   1.6  christos 			} else {
    763   1.3  christos 				*unsecure = false;
    764   1.6  christos 			}
    765   1.1  christos 		}
    766  1.15  christos 		return ISC_R_SUCCESS;
    767   1.1  christos 	} else if (result == DNS_R_GLUE || result == DNS_R_DNAME ||
    768   1.6  christos 		   result == DNS_R_DELEGATION || result == DNS_R_NXDOMAIN)
    769   1.6  christos 	{
    770   1.3  christos 		*flag = false;
    771   1.3  christos 		*cut = false;
    772  1.15  christos 		SET_IF_NOT_NULL(unsecure, false);
    773  1.15  christos 		return ISC_R_SUCCESS;
    774   1.1  christos 	} else {
    775   1.1  christos 		/*
    776   1.1  christos 		 * Silence compiler.
    777   1.1  christos 		 */
    778   1.3  christos 		*flag = false;
    779   1.3  christos 		*cut = false;
    780  1.15  christos 		SET_IF_NOT_NULL(unsecure, false);
    781  1.15  christos 		return result;
    782   1.1  christos 	}
    783   1.1  christos }
    784   1.1  christos 
    785   1.1  christos /*%
    786   1.1  christos  * Find the next/previous name that has a NSEC record.
    787   1.1  christos  * In other words, skip empty database nodes and names that
    788   1.1  christos  * have had their NSECs removed because they are obscured by
    789   1.1  christos  * a zone cut.
    790   1.1  christos  */
    791   1.1  christos static isc_result_t
    792   1.1  christos next_active(dns_update_log_t *log, dns_zone_t *zone, dns_db_t *db,
    793   1.1  christos 	    dns_dbversion_t *ver, dns_name_t *oldname, dns_name_t *newname,
    794   1.6  christos 	    bool forward) {
    795   1.1  christos 	isc_result_t result;
    796   1.1  christos 	dns_dbiterator_t *dbit = NULL;
    797   1.3  christos 	bool has_nsec = false;
    798   1.1  christos 	unsigned int wraps = 0;
    799   1.3  christos 	bool secure = dns_db_issecure(db);
    800   1.1  christos 
    801   1.1  christos 	CHECK(dns_db_createiterator(db, 0, &dbit));
    802   1.1  christos 
    803   1.1  christos 	CHECK(dns_dbiterator_seek(dbit, oldname));
    804   1.1  christos 	do {
    805   1.1  christos 		dns_dbnode_t *node = NULL;
    806   1.1  christos 
    807   1.6  christos 		if (forward) {
    808   1.1  christos 			result = dns_dbiterator_next(dbit);
    809   1.6  christos 		} else {
    810   1.1  christos 			result = dns_dbiterator_prev(dbit);
    811   1.6  christos 		}
    812   1.1  christos 		if (result == ISC_R_NOMORE) {
    813   1.1  christos 			/*
    814   1.1  christos 			 * Wrap around.
    815   1.1  christos 			 */
    816   1.6  christos 			if (forward) {
    817   1.1  christos 				CHECK(dns_dbiterator_first(dbit));
    818   1.6  christos 			} else {
    819   1.1  christos 				CHECK(dns_dbiterator_last(dbit));
    820   1.6  christos 			}
    821   1.1  christos 			wraps++;
    822   1.1  christos 			if (wraps == 2) {
    823   1.1  christos 				update_log(log, zone, ISC_LOG_ERROR,
    824   1.1  christos 					   "secure zone with no NSECs");
    825  1.16  christos 				CHECK(DNS_R_BADZONE);
    826   1.1  christos 			}
    827   1.1  christos 		}
    828   1.1  christos 		CHECK(dns_dbiterator_current(dbit, &node, newname));
    829   1.1  christos 		dns_db_detachnode(db, &node);
    830   1.1  christos 
    831   1.1  christos 		/*
    832   1.1  christos 		 * The iterator may hold the tree lock, and
    833   1.1  christos 		 * rrset_exists() calls dns_db_findnode() which
    834   1.1  christos 		 * may try to reacquire it.  To avoid deadlock
    835   1.1  christos 		 * we must pause the iterator first.
    836   1.1  christos 		 */
    837   1.1  christos 		CHECK(dns_dbiterator_pause(dbit));
    838   1.1  christos 		if (secure) {
    839   1.6  christos 			CHECK(rrset_exists(db, ver, newname, dns_rdatatype_nsec,
    840   1.6  christos 					   0, &has_nsec));
    841   1.1  christos 		} else {
    842   1.1  christos 			dns_fixedname_t ffound;
    843   1.1  christos 			dns_name_t *found;
    844   1.1  christos 			found = dns_fixedname_initname(&ffound);
    845   1.6  christos 			result = dns_db_find(
    846   1.6  christos 				db, newname, ver, dns_rdatatype_soa,
    847   1.6  christos 				DNS_DBFIND_NOWILD, 0, NULL, found, NULL, NULL);
    848   1.1  christos 			if (result == ISC_R_SUCCESS ||
    849   1.1  christos 			    result == DNS_R_EMPTYNAME ||
    850   1.6  christos 			    result == DNS_R_NXRRSET || result == DNS_R_CNAME ||
    851   1.1  christos 			    (result == DNS_R_DELEGATION &&
    852   1.6  christos 			     dns_name_equal(newname, found)))
    853   1.6  christos 			{
    854   1.3  christos 				has_nsec = true;
    855   1.1  christos 				result = ISC_R_SUCCESS;
    856   1.6  christos 			} else if (result != DNS_R_NXDOMAIN) {
    857   1.1  christos 				break;
    858   1.6  christos 			}
    859   1.1  christos 		}
    860   1.6  christos 	} while (!has_nsec);
    861  1.16  christos cleanup:
    862   1.6  christos 	if (dbit != NULL) {
    863   1.1  christos 		dns_dbiterator_destroy(&dbit);
    864   1.6  christos 	}
    865   1.1  christos 
    866  1.15  christos 	return result;
    867   1.1  christos }
    868   1.1  christos 
    869   1.1  christos /*%
    870   1.1  christos  * Add a NSEC record for "name", recording the change in "diff".
    871   1.1  christos  * The existing NSEC is removed.
    872   1.1  christos  */
    873   1.1  christos static isc_result_t
    874   1.1  christos add_nsec(dns_update_log_t *log, dns_zone_t *zone, dns_db_t *db,
    875   1.1  christos 	 dns_dbversion_t *ver, dns_name_t *name, dns_ttl_t nsecttl,
    876   1.6  christos 	 dns_diff_t *diff) {
    877   1.1  christos 	isc_result_t result;
    878   1.1  christos 	dns_dbnode_t *node = NULL;
    879   1.1  christos 	unsigned char buffer[DNS_NSEC_BUFFERSIZE];
    880   1.1  christos 	dns_rdata_t rdata = DNS_RDATA_INIT;
    881   1.1  christos 	dns_difftuple_t *tuple = NULL;
    882   1.1  christos 	dns_fixedname_t fixedname;
    883   1.1  christos 	dns_name_t *target;
    884   1.1  christos 
    885   1.1  christos 	target = dns_fixedname_initname(&fixedname);
    886   1.1  christos 
    887   1.1  christos 	/*
    888   1.1  christos 	 * Find the successor name, aka NSEC target.
    889   1.1  christos 	 */
    890   1.3  christos 	CHECK(next_active(log, zone, db, ver, name, target, true));
    891   1.1  christos 
    892   1.1  christos 	/*
    893   1.1  christos 	 * Create the NSEC RDATA.
    894   1.1  christos 	 */
    895   1.3  christos 	CHECK(dns_db_findnode(db, name, false, &node));
    896   1.1  christos 	dns_rdata_init(&rdata);
    897   1.1  christos 	CHECK(dns_nsec_buildrdata(db, ver, node, target, buffer, &rdata));
    898   1.1  christos 	dns_db_detachnode(db, &node);
    899   1.1  christos 
    900   1.1  christos 	/*
    901   1.1  christos 	 * Delete the old NSEC and record the change.
    902   1.1  christos 	 */
    903   1.6  christos 	CHECK(delete_if(true_p, db, ver, name, dns_rdatatype_nsec, 0, NULL,
    904   1.6  christos 			diff));
    905   1.1  christos 	/*
    906   1.1  christos 	 * Add the new NSEC and record the change.
    907   1.1  christos 	 */
    908   1.6  christos 	CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_ADD, name, nsecttl,
    909   1.6  christos 				   &rdata, &tuple));
    910   1.1  christos 	CHECK(do_one_tuple(&tuple, db, ver, diff));
    911   1.1  christos 	INSIST(tuple == NULL);
    912   1.1  christos 
    913  1.16  christos cleanup:
    914   1.6  christos 	if (node != NULL) {
    915   1.1  christos 		dns_db_detachnode(db, &node);
    916   1.6  christos 	}
    917  1.15  christos 	return result;
    918   1.1  christos }
    919   1.1  christos 
    920   1.1  christos /*%
    921   1.1  christos  * Add a placeholder NSEC record for "name", recording the change in "diff".
    922   1.1  christos  */
    923   1.1  christos static isc_result_t
    924   1.1  christos add_placeholder_nsec(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
    925   1.6  christos 		     dns_diff_t *diff) {
    926   1.1  christos 	isc_result_t result;
    927   1.1  christos 	dns_difftuple_t *tuple = NULL;
    928   1.1  christos 	isc_region_t r;
    929   1.1  christos 	unsigned char data[1] = { 0 }; /* The root domain, no bits. */
    930   1.1  christos 	dns_rdata_t rdata = DNS_RDATA_INIT;
    931   1.1  christos 
    932   1.1  christos 	r.base = data;
    933   1.1  christos 	r.length = sizeof(data);
    934   1.1  christos 	dns_rdata_fromregion(&rdata, dns_db_class(db), dns_rdatatype_nsec, &r);
    935   1.6  christos 	CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_ADD, name, 0, &rdata,
    936   1.6  christos 				   &tuple));
    937   1.1  christos 	CHECK(do_one_tuple(&tuple, db, ver, diff));
    938  1.16  christos cleanup:
    939  1.15  christos 	return result;
    940   1.1  christos }
    941   1.1  christos 
    942   1.1  christos static isc_result_t
    943  1.15  christos find_zone_keys(dns_zone_t *zone, isc_mem_t *mctx, unsigned int maxkeys,
    944  1.15  christos 	       dst_key_t **keys, unsigned int *nkeys) {
    945  1.15  christos 	dns_dnsseckeylist_t keylist;
    946  1.15  christos 	dns_dnsseckey_t *k = NULL;
    947  1.15  christos 	unsigned int count = 0;
    948   1.1  christos 	isc_result_t result;
    949  1.15  christos 	isc_stdtime_t now = isc_stdtime_now();
    950  1.15  christos 	dns_kasp_t *kasp;
    951  1.15  christos 	dns_keystorelist_t *keystores;
    952  1.15  christos 	const char *keydir;
    953  1.15  christos 
    954  1.15  christos 	ISC_LIST_INIT(keylist);
    955  1.15  christos 
    956  1.15  christos 	kasp = dns_zone_getkasp(zone);
    957  1.15  christos 	keydir = dns_zone_getkeydirectory(zone);
    958  1.15  christos 	keystores = dns_zone_getkeystores(zone);
    959  1.10  christos 
    960  1.10  christos 	dns_zone_lock_keyfiles(zone);
    961  1.15  christos 	result = dns_dnssec_findmatchingkeys(dns_zone_getorigin(zone), kasp,
    962  1.16  christos 					     keydir, keystores, now, false,
    963  1.16  christos 					     mctx, &keylist);
    964  1.10  christos 	dns_zone_unlock_keyfiles(zone);
    965  1.10  christos 
    966  1.15  christos 	if (result != ISC_R_SUCCESS) {
    967  1.15  christos 		*nkeys = 0;
    968  1.15  christos 		return result;
    969  1.15  christos 	}
    970  1.15  christos 
    971  1.15  christos 	/* Add new 'dnskeys' to 'keys' */
    972  1.15  christos 	while ((k = ISC_LIST_HEAD(keylist)) != NULL) {
    973  1.15  christos 		if (count >= maxkeys) {
    974  1.15  christos 			result = ISC_R_NOSPACE;
    975  1.15  christos 			goto next;
    976  1.15  christos 		}
    977  1.15  christos 
    978  1.15  christos 		/* Detect inactive keys */
    979  1.15  christos 		if (!dns_dnssec_keyactive(k->key, now)) {
    980  1.15  christos 			dst_key_setinactive(k->key, true);
    981  1.15  christos 		}
    982  1.15  christos 
    983  1.15  christos 		keys[count] = k->key;
    984  1.15  christos 		k->key = NULL;
    985  1.15  christos 		count++;
    986  1.15  christos 
    987  1.15  christos 	next:
    988  1.15  christos 		ISC_LIST_UNLINK(keylist, k, link);
    989  1.15  christos 		dns_dnsseckey_destroy(mctx, &k);
    990   1.6  christos 	}
    991  1.15  christos 
    992  1.15  christos 	*nkeys = count;
    993  1.15  christos 	return result;
    994   1.1  christos }
    995   1.1  christos 
    996   1.1  christos /*%
    997   1.1  christos  * Add RRSIG records for an RRset, recording the change in "diff".
    998   1.1  christos  */
    999   1.1  christos static isc_result_t
   1000   1.1  christos add_sigs(dns_update_log_t *log, dns_zone_t *zone, dns_db_t *db,
   1001   1.1  christos 	 dns_dbversion_t *ver, dns_name_t *name, dns_rdatatype_t type,
   1002   1.1  christos 	 dns_diff_t *diff, dst_key_t **keys, unsigned int nkeys,
   1003  1.15  christos 	 isc_stdtime_t now, isc_stdtime_t inception, isc_stdtime_t expire) {
   1004   1.1  christos 	isc_result_t result;
   1005   1.1  christos 	dns_dbnode_t *node = NULL;
   1006  1.10  christos 	dns_kasp_t *kasp = dns_zone_getkasp(zone);
   1007   1.1  christos 	dns_rdataset_t rdataset;
   1008   1.1  christos 	dns_rdata_t sig_rdata = DNS_RDATA_INIT;
   1009   1.6  christos 	dns_stats_t *dnssecsignstats = dns_zone_getdnssecsignstats(zone);
   1010   1.1  christos 	isc_buffer_t buffer;
   1011   1.1  christos 	unsigned char data[1024]; /* XXX */
   1012  1.15  christos 	unsigned int i;
   1013   1.3  christos 	bool added_sig = false;
   1014   1.8  christos 	bool use_kasp = false;
   1015  1.15  christos 	bool offlineksk = false;
   1016   1.1  christos 	isc_mem_t *mctx = diff->mctx;
   1017   1.1  christos 
   1018  1.10  christos 	if (kasp != NULL) {
   1019   1.8  christos 		use_kasp = true;
   1020  1.15  christos 		offlineksk = dns_kasp_offlineksk(kasp);
   1021   1.6  christos 	}
   1022   1.6  christos 
   1023   1.1  christos 	dns_rdataset_init(&rdataset);
   1024   1.1  christos 	isc_buffer_init(&buffer, data, sizeof(data));
   1025   1.1  christos 
   1026   1.1  christos 	/* Get the rdataset to sign. */
   1027   1.6  christos 	if (type == dns_rdatatype_nsec3) {
   1028   1.3  christos 		CHECK(dns_db_findnsec3node(db, name, false, &node));
   1029   1.6  christos 	} else {
   1030   1.3  christos 		CHECK(dns_db_findnode(db, name, false, &node));
   1031   1.6  christos 	}
   1032   1.6  christos 	CHECK(dns_db_findrdataset(db, node, ver, type, 0, (isc_stdtime_t)0,
   1033   1.6  christos 				  &rdataset, NULL));
   1034   1.1  christos 	dns_db_detachnode(db, &node);
   1035   1.1  christos 
   1036   1.1  christos #define REVOKE(x) ((dst_key_flags(x) & DNS_KEYFLAG_REVOKE) != 0)
   1037   1.6  christos #define KSK(x)	  ((dst_key_flags(x) & DNS_KEYFLAG_KSK) != 0)
   1038   1.6  christos #define ID(x)	  dst_key_id(x)
   1039   1.6  christos #define ALG(x)	  dst_key_alg(x)
   1040   1.1  christos 
   1041   1.1  christos 	/*
   1042   1.1  christos 	 * If we are honoring KSK flags then we need to check that we
   1043   1.1  christos 	 * have both KSK and non-KSK keys that are not revoked per
   1044   1.1  christos 	 * algorithm.
   1045   1.1  christos 	 */
   1046   1.1  christos 	for (i = 0; i < nkeys; i++) {
   1047   1.3  christos 		bool both = false;
   1048   1.1  christos 
   1049   1.4  christos 		/* Don't add signatures for offline or inactive keys */
   1050  1.15  christos 		if (!dst_key_isprivate(keys[i]) && !offlineksk) {
   1051   1.1  christos 			continue;
   1052   1.4  christos 		}
   1053  1.15  christos 		if (dst_key_inactive(keys[i]) && !offlineksk) {
   1054   1.1  christos 			continue;
   1055   1.4  christos 		}
   1056   1.1  christos 
   1057   1.8  christos 		if (use_kasp) {
   1058   1.6  christos 			/*
   1059   1.6  christos 			 * A dnssec-policy is found. Check what RRsets this
   1060   1.6  christos 			 * key should sign.
   1061   1.6  christos 			 */
   1062   1.6  christos 			isc_stdtime_t when;
   1063   1.6  christos 			isc_result_t kresult;
   1064   1.6  christos 			bool ksk = false;
   1065   1.6  christos 			bool zsk = false;
   1066   1.6  christos 
   1067   1.6  christos 			kresult = dst_key_getbool(keys[i], DST_BOOL_KSK, &ksk);
   1068   1.6  christos 			if (kresult != ISC_R_SUCCESS) {
   1069   1.6  christos 				if (KSK(keys[i])) {
   1070   1.6  christos 					ksk = true;
   1071   1.6  christos 				}
   1072   1.6  christos 			}
   1073   1.6  christos 			kresult = dst_key_getbool(keys[i], DST_BOOL_ZSK, &zsk);
   1074   1.6  christos 			if (kresult != ISC_R_SUCCESS) {
   1075   1.6  christos 				if (!KSK(keys[i])) {
   1076   1.6  christos 					zsk = true;
   1077   1.6  christos 				}
   1078   1.6  christos 			}
   1079   1.6  christos 
   1080  1.15  christos 			if (!dst_key_isprivate(keys[i]) && offlineksk && zsk) {
   1081  1.15  christos 				continue;
   1082  1.15  christos 			}
   1083  1.15  christos 			if (dst_key_inactive(keys[i]) && offlineksk && zsk) {
   1084  1.15  christos 				continue;
   1085  1.15  christos 			}
   1086  1.15  christos 
   1087  1.14  christos 			if (dns_rdatatype_iskeymaterial(type)) {
   1088   1.6  christos 				/*
   1089   1.6  christos 				 * DNSKEY RRset is signed with KSK.
   1090   1.6  christos 				 * CDS and CDNSKEY RRsets too (RFC 7344, 4.1).
   1091   1.6  christos 				 */
   1092   1.6  christos 				if (!ksk) {
   1093   1.6  christos 					continue;
   1094   1.6  christos 				}
   1095   1.6  christos 			} else if (!zsk) {
   1096   1.6  christos 				/*
   1097   1.6  christos 				 * Other RRsets are signed with ZSK.
   1098   1.6  christos 				 */
   1099   1.6  christos 				continue;
   1100   1.6  christos 			} else if (zsk &&
   1101   1.6  christos 				   !dst_key_is_signing(keys[i], DST_BOOL_ZSK,
   1102  1.13  christos 						       now, &when))
   1103  1.12  christos 			{
   1104   1.6  christos 				/*
   1105   1.6  christos 				 * This key is not active for zone-signing.
   1106   1.6  christos 				 */
   1107   1.6  christos 				continue;
   1108   1.6  christos 			}
   1109  1.15  christos 		} else if (!REVOKE(keys[i])) {
   1110   1.1  christos 			/*
   1111  1.15  christos 			 * Don't consider inactive keys, however the KSK may be
   1112  1.15  christos 			 * temporary offline, so do consider KSKs which private
   1113  1.15  christos 			 * key files are unavailable.
   1114   1.1  christos 			 */
   1115  1.15  christos 			both = dst_key_have_ksk_and_zsk(
   1116  1.15  christos 				keys, nkeys, i, false, KSK(keys[i]),
   1117  1.15  christos 				!KSK(keys[i]), NULL, NULL);
   1118  1.15  christos 			if (both) {
   1119  1.15  christos 				/*
   1120  1.15  christos 				 * CDS and CDNSKEY are signed with KSK (RFC
   1121  1.15  christos 				 * 7344, 4.1).
   1122  1.15  christos 				 */
   1123  1.15  christos 				if (dns_rdatatype_iskeymaterial(type)) {
   1124  1.15  christos 					if (!KSK(keys[i])) {
   1125  1.15  christos 						continue;
   1126  1.15  christos 					}
   1127  1.15  christos 				} else if (KSK(keys[i])) {
   1128   1.1  christos 					continue;
   1129   1.6  christos 				}
   1130   1.1  christos 			}
   1131  1.15  christos 		}
   1132  1.15  christos 
   1133  1.15  christos 		/*
   1134  1.15  christos 		 * If this key is revoked, it may only sign the DNSKEY RRset.
   1135  1.15  christos 		 */
   1136  1.15  christos 		if (REVOKE(keys[i]) && type != dns_rdatatype_dnskey) {
   1137   1.1  christos 			continue;
   1138   1.1  christos 		}
   1139   1.1  christos 
   1140   1.1  christos 		/* Calculate the signature, creating a RRSIG RDATA. */
   1141  1.15  christos 		if (offlineksk && dns_rdatatype_iskeymaterial(type)) {
   1142  1.15  christos 			/* Look up the signature in the SKR bundle */
   1143  1.15  christos 			dns_skrbundle_t *bundle = dns_zone_getskrbundle(zone);
   1144  1.15  christos 			if (bundle == NULL) {
   1145  1.15  christos 				CHECK(DNS_R_NOSKRBUNDLE);
   1146  1.15  christos 			}
   1147  1.15  christos 			CHECK(dns_skrbundle_getsig(bundle, keys[i], type,
   1148  1.15  christos 						   &sig_rdata));
   1149  1.15  christos 		} else {
   1150  1.15  christos 			CHECK(dns_dnssec_sign(name, &rdataset, keys[i],
   1151  1.15  christos 					      &inception, &expire, mctx,
   1152  1.15  christos 					      &buffer, &sig_rdata));
   1153  1.15  christos 		}
   1154   1.1  christos 
   1155   1.1  christos 		/* Update the database and journal with the RRSIG. */
   1156   1.1  christos 		/* XXX inefficient - will cause dataset merging */
   1157   1.1  christos 		CHECK(update_one_rr(db, ver, diff, DNS_DIFFOP_ADDRESIGN, name,
   1158   1.1  christos 				    rdataset.ttl, &sig_rdata));
   1159   1.1  christos 		dns_rdata_reset(&sig_rdata);
   1160   1.1  christos 		isc_buffer_init(&buffer, data, sizeof(data));
   1161   1.3  christos 		added_sig = true;
   1162   1.4  christos 		/* Update DNSSEC sign statistics. */
   1163   1.4  christos 		if (dnssecsignstats != NULL) {
   1164   1.4  christos 			dns_dnssecsignstats_increment(dnssecsignstats,
   1165   1.6  christos 						      ID(keys[i]),
   1166   1.6  christos 						      (uint8_t)ALG(keys[i]),
   1167   1.6  christos 						      dns_dnssecsignstats_sign);
   1168   1.4  christos 		}
   1169   1.1  christos 	}
   1170   1.1  christos 	if (!added_sig) {
   1171   1.1  christos 		update_log(log, zone, ISC_LOG_ERROR,
   1172   1.1  christos 			   "found no active private keys, "
   1173   1.1  christos 			   "unable to generate any signatures");
   1174   1.1  christos 		result = ISC_R_NOTFOUND;
   1175   1.1  christos 	}
   1176   1.1  christos 
   1177  1.16  christos cleanup:
   1178   1.6  christos 	if (dns_rdataset_isassociated(&rdataset)) {
   1179   1.1  christos 		dns_rdataset_disassociate(&rdataset);
   1180   1.6  christos 	}
   1181   1.6  christos 	if (node != NULL) {
   1182   1.1  christos 		dns_db_detachnode(db, &node);
   1183   1.6  christos 	}
   1184  1.15  christos 	return result;
   1185   1.1  christos }
   1186   1.1  christos 
   1187   1.1  christos /*
   1188   1.1  christos  * Delete expired RRsigs and any RRsigs we are about to re-sign.
   1189   1.1  christos  * See also zone.c:del_sigs().
   1190   1.1  christos  */
   1191   1.1  christos static isc_result_t
   1192   1.1  christos del_keysigs(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
   1193   1.6  christos 	    dns_diff_t *diff, dst_key_t **keys, unsigned int nkeys) {
   1194   1.1  christos 	isc_result_t result;
   1195   1.1  christos 	dns_dbnode_t *node = NULL;
   1196   1.1  christos 	dns_rdataset_t rdataset;
   1197   1.1  christos 	dns_rdata_t rdata = DNS_RDATA_INIT;
   1198   1.1  christos 	unsigned int i;
   1199   1.1  christos 	dns_rdata_rrsig_t rrsig;
   1200   1.3  christos 	bool found;
   1201   1.1  christos 
   1202   1.1  christos 	dns_rdataset_init(&rdataset);
   1203   1.1  christos 
   1204   1.3  christos 	result = dns_db_findnode(db, name, false, &node);
   1205   1.6  christos 	if (result == ISC_R_NOTFOUND) {
   1206  1.15  christos 		return ISC_R_SUCCESS;
   1207   1.6  christos 	}
   1208  1.16  christos 	CHECK(result);
   1209  1.16  christos 
   1210   1.1  christos 	result = dns_db_findrdataset(db, node, ver, dns_rdatatype_rrsig,
   1211   1.6  christos 				     dns_rdatatype_dnskey, (isc_stdtime_t)0,
   1212   1.1  christos 				     &rdataset, NULL);
   1213   1.1  christos 	dns_db_detachnode(db, &node);
   1214   1.1  christos 
   1215   1.6  christos 	if (result == ISC_R_NOTFOUND) {
   1216  1.15  christos 		return ISC_R_SUCCESS;
   1217   1.6  christos 	}
   1218  1.16  christos 	CHECK(result);
   1219   1.1  christos 
   1220   1.6  christos 	for (result = dns_rdataset_first(&rdataset); result == ISC_R_SUCCESS;
   1221   1.6  christos 	     result = dns_rdataset_next(&rdataset))
   1222   1.6  christos 	{
   1223   1.1  christos 		dns_rdataset_current(&rdataset, &rdata);
   1224   1.1  christos 		result = dns_rdata_tostruct(&rdata, &rrsig, NULL);
   1225   1.1  christos 		RUNTIME_CHECK(result == ISC_R_SUCCESS);
   1226   1.3  christos 		found = false;
   1227   1.1  christos 		for (i = 0; i < nkeys; i++) {
   1228   1.1  christos 			if (rrsig.keyid == dst_key_id(keys[i])) {
   1229   1.3  christos 				found = true;
   1230   1.1  christos 				if (!dst_key_isprivate(keys[i]) &&
   1231  1.12  christos 				    !dst_key_inactive(keys[i]))
   1232  1.12  christos 				{
   1233   1.1  christos 					/*
   1234   1.1  christos 					 * The re-signing code in zone.c
   1235   1.1  christos 					 * will mark this as offline.
   1236   1.1  christos 					 * Just skip the record for now.
   1237   1.1  christos 					 */
   1238   1.1  christos 					break;
   1239   1.1  christos 				}
   1240   1.1  christos 				result = update_one_rr(db, ver, diff,
   1241   1.1  christos 						       DNS_DIFFOP_DEL, name,
   1242   1.1  christos 						       rdataset.ttl, &rdata);
   1243   1.1  christos 				break;
   1244   1.1  christos 			}
   1245   1.1  christos 		}
   1246   1.1  christos 		/*
   1247   1.1  christos 		 * If there is not a matching DNSKEY then delete the RRSIG.
   1248   1.1  christos 		 */
   1249   1.6  christos 		if (!found) {
   1250   1.1  christos 			result = update_one_rr(db, ver, diff, DNS_DIFFOP_DEL,
   1251   1.1  christos 					       name, rdataset.ttl, &rdata);
   1252   1.6  christos 		}
   1253   1.1  christos 		dns_rdata_reset(&rdata);
   1254   1.6  christos 		if (result != ISC_R_SUCCESS) {
   1255   1.1  christos 			break;
   1256   1.6  christos 		}
   1257   1.1  christos 	}
   1258   1.1  christos 	dns_rdataset_disassociate(&rdataset);
   1259   1.6  christos 	if (result == ISC_R_NOMORE) {
   1260   1.1  christos 		result = ISC_R_SUCCESS;
   1261   1.6  christos 	}
   1262  1.16  christos 
   1263  1.16  christos cleanup:
   1264   1.6  christos 	if (node != NULL) {
   1265   1.1  christos 		dns_db_detachnode(db, &node);
   1266   1.6  christos 	}
   1267  1.15  christos 	return result;
   1268   1.1  christos }
   1269   1.1  christos 
   1270   1.1  christos static isc_result_t
   1271   1.1  christos add_exposed_sigs(dns_update_log_t *log, dns_zone_t *zone, dns_db_t *db,
   1272   1.3  christos 		 dns_dbversion_t *ver, dns_name_t *name, bool cut,
   1273   1.1  christos 		 dns_diff_t *diff, dst_key_t **keys, unsigned int nkeys,
   1274  1.13  christos 		 isc_stdtime_t now, isc_stdtime_t inception,
   1275  1.15  christos 		 isc_stdtime_t expire, unsigned int *sigs) {
   1276   1.1  christos 	isc_result_t result;
   1277   1.1  christos 	dns_dbnode_t *node;
   1278   1.1  christos 	dns_rdatasetiter_t *iter;
   1279   1.1  christos 
   1280   1.1  christos 	node = NULL;
   1281   1.3  christos 	result = dns_db_findnode(db, name, false, &node);
   1282   1.6  christos 	if (result == ISC_R_NOTFOUND) {
   1283  1.15  christos 		return ISC_R_SUCCESS;
   1284   1.6  christos 	}
   1285   1.6  christos 	if (result != ISC_R_SUCCESS) {
   1286  1.15  christos 		return result;
   1287   1.6  christos 	}
   1288   1.1  christos 
   1289   1.1  christos 	iter = NULL;
   1290  1.12  christos 	result = dns_db_allrdatasets(db, node, ver, 0, (isc_stdtime_t)0, &iter);
   1291   1.6  christos 	if (result != ISC_R_SUCCESS) {
   1292   1.1  christos 		goto cleanup_node;
   1293   1.6  christos 	}
   1294   1.1  christos 
   1295   1.6  christos 	for (result = dns_rdatasetiter_first(iter); result == ISC_R_SUCCESS;
   1296   1.1  christos 	     result = dns_rdatasetiter_next(iter))
   1297   1.1  christos 	{
   1298   1.1  christos 		dns_rdataset_t rdataset;
   1299   1.1  christos 		dns_rdatatype_t type;
   1300   1.3  christos 		bool flag;
   1301   1.1  christos 
   1302   1.1  christos 		dns_rdataset_init(&rdataset);
   1303   1.1  christos 		dns_rdatasetiter_current(iter, &rdataset);
   1304   1.1  christos 		type = rdataset.type;
   1305   1.1  christos 		dns_rdataset_disassociate(&rdataset);
   1306   1.1  christos 
   1307   1.1  christos 		/*
   1308   1.1  christos 		 * We don't need to sign unsigned NSEC records at the cut
   1309   1.1  christos 		 * as they are handled elsewhere.
   1310   1.1  christos 		 */
   1311   1.1  christos 		if ((type == dns_rdatatype_rrsig) ||
   1312  1.12  christos 		    (cut && type != dns_rdatatype_ds))
   1313  1.12  christos 		{
   1314   1.1  christos 			continue;
   1315   1.6  christos 		}
   1316   1.6  christos 		result = rrset_exists(db, ver, name, dns_rdatatype_rrsig, type,
   1317   1.6  christos 				      &flag);
   1318   1.6  christos 		if (result != ISC_R_SUCCESS) {
   1319   1.1  christos 			goto cleanup_iterator;
   1320   1.6  christos 		}
   1321   1.6  christos 		if (flag) {
   1322   1.6  christos 			continue;
   1323   1.6  christos 		}
   1324   1.6  christos 		result = add_sigs(log, zone, db, ver, name, type, diff, keys,
   1325  1.15  christos 				  nkeys, now, inception, expire);
   1326   1.6  christos 		if (result != ISC_R_SUCCESS) {
   1327   1.1  christos 			goto cleanup_iterator;
   1328   1.6  christos 		}
   1329   1.1  christos 		(*sigs)++;
   1330   1.1  christos 	}
   1331   1.6  christos 	if (result == ISC_R_NOMORE) {
   1332   1.1  christos 		result = ISC_R_SUCCESS;
   1333   1.6  christos 	}
   1334   1.1  christos 
   1335   1.6  christos cleanup_iterator:
   1336   1.1  christos 	dns_rdatasetiter_destroy(&iter);
   1337   1.1  christos 
   1338   1.6  christos cleanup_node:
   1339   1.1  christos 	dns_db_detachnode(db, &node);
   1340   1.1  christos 
   1341  1.15  christos 	return result;
   1342   1.1  christos }
   1343   1.1  christos 
   1344   1.1  christos /*%
   1345   1.1  christos  * Update RRSIG, NSEC and NSEC3 records affected by an update.  The original
   1346   1.1  christos  * update, including the SOA serial update but excluding the RRSIG & NSEC
   1347   1.1  christos  * changes, is in "diff" and has already been applied to "newver" of "db".
   1348   1.1  christos  * The database version prior to the update is "oldver".
   1349   1.1  christos  *
   1350   1.1  christos  * The necessary RRSIG, NSEC and NSEC3 changes will be applied to "newver"
   1351   1.1  christos  * and added (as a minimal diff) to "diff".
   1352   1.1  christos  *
   1353   1.1  christos  * The RRSIGs generated will be valid for 'sigvalidityinterval' seconds.
   1354   1.1  christos  */
   1355   1.1  christos isc_result_t
   1356   1.1  christos dns_update_signatures(dns_update_log_t *log, dns_zone_t *zone, dns_db_t *db,
   1357   1.1  christos 		      dns_dbversion_t *oldver, dns_dbversion_t *newver,
   1358   1.6  christos 		      dns_diff_t *diff, uint32_t sigvalidityinterval) {
   1359  1.15  christos 	return dns_update_signaturesinc(log, zone, db, oldver, newver, diff,
   1360  1.15  christos 					sigvalidityinterval, NULL);
   1361   1.1  christos }
   1362   1.1  christos 
   1363   1.1  christos struct dns_update_state {
   1364   1.1  christos 	unsigned int magic;
   1365   1.1  christos 	dns_diff_t diffnames;
   1366   1.1  christos 	dns_diff_t affected;
   1367   1.1  christos 	dns_diff_t sig_diff;
   1368   1.1  christos 	dns_diff_t nsec_diff;
   1369   1.1  christos 	dns_diff_t nsec_mindiff;
   1370   1.1  christos 	dns_diff_t work;
   1371   1.1  christos 	dst_key_t *zone_keys[DNS_MAXZONEKEYS];
   1372   1.1  christos 	unsigned int nkeys;
   1373  1.13  christos 	isc_stdtime_t now, inception, expire, soaexpire, keyexpire;
   1374   1.1  christos 	dns_ttl_t nsecttl;
   1375  1.15  christos 	bool build_nsec3;
   1376   1.8  christos 	enum {
   1377   1.8  christos 		sign_updates,
   1378   1.8  christos 		remove_orphaned,
   1379   1.8  christos 		build_chain,
   1380   1.8  christos 		process_nsec,
   1381   1.8  christos 		sign_nsec,
   1382   1.8  christos 		update_nsec3,
   1383   1.8  christos 		process_nsec3,
   1384   1.8  christos 		sign_nsec3
   1385   1.8  christos 	} state;
   1386   1.1  christos };
   1387   1.1  christos 
   1388   1.5  christos static uint32_t
   1389  1.14  christos dns__jitter_expire(dns_zone_t *zone) {
   1390   1.5  christos 	/* Spread out signatures over time */
   1391  1.14  christos 	isc_stdtime_t jitter = DEFAULT_JITTER;
   1392  1.14  christos 	isc_stdtime_t sigvalidity = dns_zone_getsigvalidityinterval(zone);
   1393  1.14  christos 	dns_kasp_t *kasp = dns_zone_getkasp(zone);
   1394  1.14  christos 
   1395  1.14  christos 	if (kasp != NULL) {
   1396  1.14  christos 		jitter = dns_kasp_sigjitter(kasp);
   1397  1.14  christos 		sigvalidity = dns_kasp_sigvalidity(kasp);
   1398  1.14  christos 		INSIST(jitter <= sigvalidity);
   1399  1.14  christos 	}
   1400  1.14  christos 
   1401  1.14  christos 	if (jitter > sigvalidity) {
   1402  1.14  christos 		jitter = sigvalidity;
   1403  1.14  christos 	}
   1404  1.14  christos 
   1405  1.14  christos 	if (sigvalidity >= 3600U) {
   1406  1.14  christos 		if (sigvalidity > 7200U) {
   1407  1.14  christos 			sigvalidity -= isc_random_uniform(jitter);
   1408   1.5  christos 		} else {
   1409  1.14  christos 			sigvalidity -= isc_random_uniform(1200);
   1410   1.5  christos 		}
   1411   1.5  christos 	}
   1412  1.15  christos 	return sigvalidity;
   1413   1.5  christos }
   1414   1.5  christos 
   1415   1.1  christos isc_result_t
   1416   1.1  christos dns_update_signaturesinc(dns_update_log_t *log, dns_zone_t *zone, dns_db_t *db,
   1417   1.1  christos 			 dns_dbversion_t *oldver, dns_dbversion_t *newver,
   1418   1.3  christos 			 dns_diff_t *diff, uint32_t sigvalidityinterval,
   1419   1.6  christos 			 dns_update_state_t **statep) {
   1420   1.1  christos 	isc_result_t result = ISC_R_SUCCESS;
   1421   1.1  christos 	dns_update_state_t mystate, *state;
   1422   1.1  christos 
   1423   1.1  christos 	dns_difftuple_t *t, *next;
   1424   1.3  christos 	bool flag, build_nsec;
   1425   1.1  christos 	unsigned int i;
   1426   1.1  christos 	dns_rdata_soa_t soa;
   1427   1.1  christos 	dns_rdata_t rdata = DNS_RDATA_INIT;
   1428   1.1  christos 	dns_rdataset_t rdataset;
   1429   1.1  christos 	dns_dbnode_t *node = NULL;
   1430   1.3  christos 	bool unsecure;
   1431   1.3  christos 	bool cut;
   1432   1.1  christos 	dns_rdatatype_t privatetype = dns_zone_getprivatetype(zone);
   1433   1.1  christos 	unsigned int sigs = 0;
   1434   1.1  christos 	unsigned int maxsigs = dns_zone_getsignatures(zone);
   1435   1.1  christos 
   1436   1.1  christos 	if (statep == NULL || *statep == NULL) {
   1437   1.1  christos 		if (statep == NULL) {
   1438   1.1  christos 			state = &mystate;
   1439   1.1  christos 		} else {
   1440   1.1  christos 			state = isc_mem_get(diff->mctx, sizeof(*state));
   1441   1.1  christos 		}
   1442   1.1  christos 
   1443   1.1  christos 		dns_diff_init(diff->mctx, &state->diffnames);
   1444   1.1  christos 		dns_diff_init(diff->mctx, &state->affected);
   1445   1.1  christos 		dns_diff_init(diff->mctx, &state->sig_diff);
   1446   1.1  christos 		dns_diff_init(diff->mctx, &state->nsec_diff);
   1447   1.1  christos 		dns_diff_init(diff->mctx, &state->nsec_mindiff);
   1448   1.1  christos 		dns_diff_init(diff->mctx, &state->work);
   1449   1.1  christos 		state->nkeys = 0;
   1450   1.3  christos 		state->build_nsec3 = false;
   1451   1.1  christos 
   1452  1.15  christos 		result = find_zone_keys(zone, diff->mctx, DNS_MAXZONEKEYS,
   1453  1.15  christos 					state->zone_keys, &state->nkeys);
   1454  1.15  christos 		if (result == ISC_R_NOSPACE) {
   1455  1.15  christos 			update_log(log, zone, ISC_LOG_ERROR,
   1456  1.15  christos 				   "too many zone keys for secure "
   1457  1.15  christos 				   "dynamic update");
   1458  1.15  christos 		} else if (result != ISC_R_SUCCESS) {
   1459   1.1  christos 			update_log(log, zone, ISC_LOG_ERROR,
   1460   1.1  christos 				   "could not get zone keys for secure "
   1461   1.1  christos 				   "dynamic update");
   1462  1.16  christos 			goto cleanup;
   1463   1.1  christos 		}
   1464   1.1  christos 
   1465  1.15  christos 		state->now = isc_stdtime_now();
   1466  1.13  christos 		state->inception = state->now - 3600; /* Allow for some clock
   1467  1.13  christos 							 skew. */
   1468  1.14  christos 		state->expire = state->now + dns__jitter_expire(zone);
   1469  1.13  christos 		state->soaexpire = state->now + sigvalidityinterval;
   1470   1.3  christos 		state->keyexpire = dns_zone_getkeyvalidityinterval(zone);
   1471   1.3  christos 		if (state->keyexpire == 0) {
   1472   1.3  christos 			state->keyexpire = state->expire;
   1473   1.3  christos 		} else {
   1474  1.13  christos 			state->keyexpire += state->now;
   1475   1.3  christos 		}
   1476   1.1  christos 
   1477   1.1  christos 		/*
   1478  1.10  christos 		 * Calculate the NSEC/NSEC3 TTL as a minimum of the SOA TTL and
   1479  1.10  christos 		 * MINIMUM field.
   1480   1.1  christos 		 */
   1481   1.3  christos 		CHECK(dns_db_findnode(db, dns_db_origin(db), false, &node));
   1482   1.1  christos 		dns_rdataset_init(&rdataset);
   1483   1.1  christos 		CHECK(dns_db_findrdataset(db, node, newver, dns_rdatatype_soa,
   1484   1.6  christos 					  0, (isc_stdtime_t)0, &rdataset,
   1485   1.1  christos 					  NULL));
   1486   1.1  christos 		CHECK(dns_rdataset_first(&rdataset));
   1487   1.1  christos 		dns_rdataset_current(&rdataset, &rdata);
   1488   1.1  christos 		CHECK(dns_rdata_tostruct(&rdata, &soa, NULL));
   1489  1.10  christos 		state->nsecttl = ISC_MIN(rdataset.ttl, soa.minimum);
   1490   1.1  christos 		dns_rdataset_disassociate(&rdataset);
   1491   1.1  christos 		dns_db_detachnode(db, &node);
   1492   1.1  christos 
   1493   1.1  christos 		/*
   1494   1.1  christos 		 * Find all RRsets directly affected by the update, and
   1495   1.1  christos 		 * update their RRSIGs.  Also build a list of names affected
   1496   1.1  christos 		 * by the update in "diffnames".
   1497   1.1  christos 		 */
   1498   1.1  christos 		CHECK(dns_diff_sort(diff, temp_order));
   1499   1.1  christos 		state->state = sign_updates;
   1500   1.1  christos 		state->magic = STATE_MAGIC;
   1501   1.6  christos 		if (statep != NULL) {
   1502   1.1  christos 			*statep = state;
   1503   1.6  christos 		}
   1504   1.1  christos 	} else {
   1505   1.1  christos 		REQUIRE(DNS_STATE_VALID(*statep));
   1506   1.1  christos 		state = *statep;
   1507   1.1  christos 	}
   1508   1.1  christos 
   1509   1.6  christos next_state:
   1510   1.1  christos 	switch (state->state) {
   1511   1.1  christos 	case sign_updates:
   1512   1.1  christos 		t = ISC_LIST_HEAD(diff->tuples);
   1513   1.1  christos 		while (t != NULL) {
   1514   1.1  christos 			dns_name_t *name = &t->name;
   1515   1.1  christos 			/*
   1516   1.1  christos 			 * Now "name" is a new, unique name affected by the
   1517   1.1  christos 			 * update.
   1518   1.1  christos 			 */
   1519   1.1  christos 
   1520   1.1  christos 			CHECK(namelist_append_name(&state->diffnames, name));
   1521   1.1  christos 
   1522   1.1  christos 			while (t != NULL && dns_name_equal(&t->name, name)) {
   1523   1.1  christos 				dns_rdatatype_t type;
   1524   1.1  christos 				type = t->rdata.type;
   1525   1.1  christos 
   1526   1.1  christos 				/*
   1527   1.1  christos 				 * Now "name" and "type" denote a new unique
   1528   1.1  christos 				 * RRset affected by the update.
   1529   1.1  christos 				 */
   1530   1.1  christos 
   1531   1.1  christos 				/* Don't sign RRSIGs. */
   1532   1.6  christos 				if (type == dns_rdatatype_rrsig) {
   1533   1.1  christos 					goto skip;
   1534   1.6  christos 				}
   1535   1.1  christos 
   1536   1.1  christos 				/*
   1537   1.1  christos 				 * Delete all old RRSIGs covering this type,
   1538   1.1  christos 				 * since they are all invalid when the signed
   1539   1.1  christos 				 * RRset has changed.  We may not be able to
   1540   1.1  christos 				 * recreate all of them - tough.
   1541   1.1  christos 				 * Special case changes to the zone's DNSKEY
   1542   1.1  christos 				 * records to support offline KSKs.
   1543   1.1  christos 				 */
   1544   1.6  christos 				if (type == dns_rdatatype_dnskey) {
   1545   1.1  christos 					del_keysigs(db, newver, name,
   1546   1.1  christos 						    &state->sig_diff,
   1547   1.1  christos 						    state->zone_keys,
   1548   1.1  christos 						    state->nkeys);
   1549   1.6  christos 				} else {
   1550   1.6  christos 					CHECK(delete_if(
   1551   1.6  christos 						true_p, db, newver, name,
   1552   1.6  christos 						dns_rdatatype_rrsig, type, NULL,
   1553   1.6  christos 						&state->sig_diff));
   1554   1.6  christos 				}
   1555   1.1  christos 
   1556   1.1  christos 				/*
   1557   1.1  christos 				 * If this RRset is still visible after the
   1558   1.1  christos 				 * update, add a new signature for it.
   1559   1.1  christos 				 */
   1560   1.1  christos 				CHECK(rrset_visible(db, newver, name, type,
   1561   1.6  christos 						    &flag));
   1562   1.1  christos 				if (flag) {
   1563   1.3  christos 					isc_stdtime_t exp;
   1564  1.14  christos 					if (dns_rdatatype_iskeymaterial(type)) {
   1565   1.3  christos 						exp = state->keyexpire;
   1566   1.6  christos 					} else if (type == dns_rdatatype_soa) {
   1567   1.6  christos 						exp = state->soaexpire;
   1568   1.3  christos 					} else {
   1569   1.3  christos 						exp = state->expire;
   1570   1.3  christos 					}
   1571   1.3  christos 
   1572  1.15  christos 					CHECK(add_sigs(log, zone, db, newver,
   1573  1.15  christos 						       name, type,
   1574  1.15  christos 						       &state->sig_diff,
   1575  1.15  christos 						       state->zone_keys,
   1576  1.15  christos 						       state->nkeys, state->now,
   1577  1.15  christos 						       state->inception, exp));
   1578   1.1  christos 					sigs++;
   1579   1.1  christos 				}
   1580   1.1  christos 			skip:
   1581   1.1  christos 				/* Skip any other updates to the same RRset. */
   1582   1.1  christos 				while (t != NULL &&
   1583   1.1  christos 				       dns_name_equal(&t->name, name) &&
   1584  1.12  christos 				       t->rdata.type == type)
   1585  1.12  christos 				{
   1586   1.1  christos 					next = ISC_LIST_NEXT(t, link);
   1587   1.1  christos 					ISC_LIST_UNLINK(diff->tuples, t, link);
   1588   1.1  christos 					ISC_LIST_APPEND(state->work.tuples, t,
   1589   1.1  christos 							link);
   1590   1.1  christos 					t = next;
   1591   1.1  christos 				}
   1592   1.1  christos 			}
   1593   1.6  christos 			if (state != &mystate && sigs > maxsigs) {
   1594  1.15  christos 				return DNS_R_CONTINUE;
   1595   1.6  christos 			}
   1596   1.1  christos 		}
   1597   1.1  christos 		ISC_LIST_APPENDLIST(diff->tuples, state->work.tuples, link);
   1598   1.1  christos 
   1599   1.1  christos 		update_log(log, zone, ISC_LOG_DEBUG(3),
   1600   1.1  christos 			   "updated data signatures");
   1601  1.11  christos 		FALLTHROUGH;
   1602   1.1  christos 	case remove_orphaned:
   1603   1.1  christos 		state->state = remove_orphaned;
   1604   1.1  christos 
   1605   1.1  christos 		/* Remove orphaned NSECs and RRSIG NSECs. */
   1606   1.6  christos 		for (t = ISC_LIST_HEAD(state->diffnames.tuples); t != NULL;
   1607   1.1  christos 		     t = ISC_LIST_NEXT(t, link))
   1608   1.1  christos 		{
   1609   1.6  christos 			CHECK(non_nsec_rrset_exists(db, newver, &t->name,
   1610   1.6  christos 						    &flag));
   1611   1.1  christos 			if (!flag) {
   1612   1.1  christos 				CHECK(delete_if(true_p, db, newver, &t->name,
   1613   1.6  christos 						dns_rdatatype_any, 0, NULL,
   1614   1.6  christos 						&state->sig_diff));
   1615   1.1  christos 			}
   1616   1.1  christos 		}
   1617   1.1  christos 		update_log(log, zone, ISC_LOG_DEBUG(3),
   1618   1.1  christos 			   "removed any orphaned NSEC records");
   1619   1.1  christos 
   1620   1.1  christos 		/*
   1621   1.1  christos 		 * See if we need to build NSEC or NSEC3 chains.
   1622   1.1  christos 		 */
   1623   1.1  christos 		CHECK(dns_private_chains(db, newver, privatetype, &build_nsec,
   1624   1.1  christos 					 &state->build_nsec3));
   1625   1.1  christos 		if (!build_nsec) {
   1626   1.1  christos 			state->state = update_nsec3;
   1627   1.1  christos 			goto next_state;
   1628   1.1  christos 		}
   1629   1.1  christos 
   1630   1.1  christos 		update_log(log, zone, ISC_LOG_DEBUG(3),
   1631   1.1  christos 			   "rebuilding NSEC chain");
   1632   1.1  christos 
   1633  1.11  christos 		FALLTHROUGH;
   1634   1.1  christos 	case build_chain:
   1635   1.1  christos 		state->state = build_chain;
   1636   1.1  christos 		/*
   1637   1.1  christos 		 * When a name is created or deleted, its predecessor needs to
   1638   1.1  christos 		 * have its NSEC updated.
   1639   1.1  christos 		 */
   1640   1.6  christos 		for (t = ISC_LIST_HEAD(state->diffnames.tuples); t != NULL;
   1641   1.1  christos 		     t = ISC_LIST_NEXT(t, link))
   1642   1.1  christos 		{
   1643   1.3  christos 			bool existed, exists;
   1644   1.1  christos 			dns_fixedname_t fixedname;
   1645   1.1  christos 			dns_name_t *prevname;
   1646   1.1  christos 
   1647   1.1  christos 			prevname = dns_fixedname_initname(&fixedname);
   1648   1.1  christos 
   1649   1.6  christos 			if (oldver != NULL) {
   1650   1.1  christos 				CHECK(name_exists(db, oldver, &t->name,
   1651   1.1  christos 						  &existed));
   1652   1.6  christos 			} else {
   1653   1.3  christos 				existed = false;
   1654   1.6  christos 			}
   1655   1.1  christos 			CHECK(name_exists(db, newver, &t->name, &exists));
   1656   1.6  christos 			if (exists == existed) {
   1657   1.1  christos 				continue;
   1658   1.6  christos 			}
   1659   1.1  christos 
   1660   1.1  christos 			/*
   1661   1.1  christos 			 * Find the predecessor.
   1662   1.1  christos 			 * When names become obscured or unobscured in this
   1663   1.1  christos 			 * update transaction, we may find the wrong
   1664   1.1  christos 			 * predecessor because the NSECs have not yet been
   1665   1.1  christos 			 * updated to reflect the delegation change.  This
   1666   1.1  christos 			 * should not matter because in this case, the correct
   1667   1.1  christos 			 * predecessor is either the delegation node or a
   1668   1.1  christos 			 * newly unobscured node, and those nodes are on the
   1669   1.1  christos 			 * "affected" list in any case.
   1670   1.1  christos 			 */
   1671   1.6  christos 			CHECK(next_active(log, zone, db, newver, &t->name,
   1672   1.6  christos 					  prevname, false));
   1673   1.1  christos 			CHECK(namelist_append_name(&state->affected, prevname));
   1674   1.1  christos 		}
   1675   1.1  christos 
   1676   1.1  christos 		/*
   1677   1.1  christos 		 * Find names potentially affected by delegation changes
   1678   1.1  christos 		 * (obscured by adding an NS or DNAME, or unobscured by
   1679   1.1  christos 		 * removing one).
   1680   1.1  christos 		 */
   1681   1.6  christos 		for (t = ISC_LIST_HEAD(state->diffnames.tuples); t != NULL;
   1682   1.1  christos 		     t = ISC_LIST_NEXT(t, link))
   1683   1.1  christos 		{
   1684   1.3  christos 			bool ns_existed, dname_existed;
   1685   1.3  christos 			bool ns_exists, dname_exists;
   1686   1.1  christos 
   1687   1.6  christos 			if (oldver != NULL) {
   1688   1.1  christos 				CHECK(rrset_exists(db, oldver, &t->name,
   1689   1.6  christos 						   dns_rdatatype_ns, 0,
   1690   1.6  christos 						   &ns_existed));
   1691   1.6  christos 			} else {
   1692   1.3  christos 				ns_existed = false;
   1693   1.6  christos 			}
   1694   1.6  christos 			if (oldver != NULL) {
   1695   1.1  christos 				CHECK(rrset_exists(db, oldver, &t->name,
   1696   1.1  christos 						   dns_rdatatype_dname, 0,
   1697   1.1  christos 						   &dname_existed));
   1698   1.6  christos 			} else {
   1699   1.3  christos 				dname_existed = false;
   1700   1.6  christos 			}
   1701   1.1  christos 			CHECK(rrset_exists(db, newver, &t->name,
   1702   1.1  christos 					   dns_rdatatype_ns, 0, &ns_exists));
   1703   1.1  christos 			CHECK(rrset_exists(db, newver, &t->name,
   1704   1.1  christos 					   dns_rdatatype_dname, 0,
   1705   1.1  christos 					   &dname_exists));
   1706   1.1  christos 			if ((ns_exists || dname_exists) ==
   1707  1.12  christos 			    (ns_existed || dname_existed))
   1708  1.12  christos 			{
   1709   1.1  christos 				continue;
   1710   1.6  christos 			}
   1711   1.1  christos 			/*
   1712   1.1  christos 			 * There was a delegation change.  Mark all subdomains
   1713   1.1  christos 			 * of t->name as potentially needing a NSEC update.
   1714   1.1  christos 			 */
   1715   1.1  christos 			CHECK(namelist_append_subdomain(db, &t->name,
   1716   1.1  christos 							&state->affected));
   1717   1.1  christos 		}
   1718   1.1  christos 		ISC_LIST_APPENDLIST(state->affected.tuples,
   1719   1.1  christos 				    state->diffnames.tuples, link);
   1720   1.1  christos 		INSIST(ISC_LIST_EMPTY(state->diffnames.tuples));
   1721   1.1  christos 
   1722   1.1  christos 		CHECK(uniqify_name_list(&state->affected));
   1723   1.1  christos 
   1724  1.11  christos 		FALLTHROUGH;
   1725   1.1  christos 	case process_nsec:
   1726   1.1  christos 		state->state = process_nsec;
   1727   1.1  christos 
   1728   1.1  christos 		/*
   1729   1.1  christos 		 * Determine which names should have NSECs, and delete/create
   1730   1.1  christos 		 * NSECs to make it so.  We don't know the final NSEC targets
   1731   1.1  christos 		 * yet, so we just create placeholder NSECs with arbitrary
   1732   1.1  christos 		 * contents to indicate that their respective owner names
   1733   1.1  christos 		 * should be part of the NSEC chain.
   1734   1.1  christos 		 */
   1735   1.1  christos 		while ((t = ISC_LIST_HEAD(state->affected.tuples)) != NULL) {
   1736   1.3  christos 			bool exists;
   1737   1.1  christos 			dns_name_t *name = &t->name;
   1738   1.1  christos 
   1739   1.1  christos 			CHECK(name_exists(db, newver, name, &exists));
   1740   1.6  christos 			if (!exists) {
   1741   1.1  christos 				goto unlink;
   1742   1.6  christos 			}
   1743   1.1  christos 			CHECK(is_active(db, newver, name, &flag, &cut, NULL));
   1744   1.1  christos 			if (!flag) {
   1745   1.1  christos 				/*
   1746   1.1  christos 				 * This name is obscured.  Delete any
   1747   1.1  christos 				 * existing NSEC record.
   1748   1.1  christos 				 */
   1749   1.1  christos 				CHECK(delete_if(true_p, db, newver, name,
   1750   1.6  christos 						dns_rdatatype_nsec, 0, NULL,
   1751   1.6  christos 						&state->nsec_diff));
   1752   1.1  christos 				CHECK(delete_if(rrsig_p, db, newver, name,
   1753   1.1  christos 						dns_rdatatype_any, 0, NULL,
   1754   1.1  christos 						diff));
   1755   1.1  christos 			} else {
   1756   1.1  christos 				/*
   1757   1.1  christos 				 * This name is not obscured.  It needs to have
   1758   1.1  christos 				 * a NSEC unless it is the at the origin, in
   1759   1.1  christos 				 * which case it should already exist if there
   1760   1.1  christos 				 * is a complete NSEC chain and if there isn't
   1761   1.1  christos 				 * a complete NSEC chain we don't want to add
   1762   1.1  christos 				 * one as that would signal that there is a
   1763   1.1  christos 				 * complete NSEC chain.
   1764   1.1  christos 				 */
   1765   1.1  christos 				if (!dns_name_equal(name, dns_db_origin(db))) {
   1766   1.1  christos 					CHECK(rrset_exists(db, newver, name,
   1767   1.1  christos 							   dns_rdatatype_nsec,
   1768   1.1  christos 							   0, &flag));
   1769   1.6  christos 					if (!flag) {
   1770   1.6  christos 						CHECK(add_placeholder_nsec(
   1771   1.6  christos 							db, newver, name,
   1772   1.6  christos 							diff));
   1773   1.6  christos 					}
   1774   1.1  christos 				}
   1775   1.6  christos 				CHECK(add_exposed_sigs(
   1776   1.6  christos 					log, zone, db, newver, name, cut,
   1777   1.6  christos 					&state->sig_diff, state->zone_keys,
   1778  1.13  christos 					state->nkeys, state->now,
   1779  1.13  christos 					state->inception, state->expire,
   1780  1.13  christos 					&sigs));
   1781   1.1  christos 			}
   1782   1.6  christos 		unlink:
   1783   1.1  christos 			ISC_LIST_UNLINK(state->affected.tuples, t, link);
   1784   1.1  christos 			ISC_LIST_APPEND(state->work.tuples, t, link);
   1785   1.6  christos 			if (state != &mystate && sigs > maxsigs) {
   1786  1.15  christos 				return DNS_R_CONTINUE;
   1787   1.6  christos 			}
   1788   1.1  christos 		}
   1789   1.6  christos 		ISC_LIST_APPENDLIST(state->affected.tuples, state->work.tuples,
   1790   1.6  christos 				    link);
   1791   1.1  christos 
   1792   1.1  christos 		/*
   1793   1.1  christos 		 * Now we know which names are part of the NSEC chain.
   1794   1.1  christos 		 * Make them all point at their correct targets.
   1795   1.1  christos 		 */
   1796   1.6  christos 		for (t = ISC_LIST_HEAD(state->affected.tuples); t != NULL;
   1797   1.1  christos 		     t = ISC_LIST_NEXT(t, link))
   1798   1.1  christos 		{
   1799   1.1  christos 			CHECK(rrset_exists(db, newver, &t->name,
   1800   1.1  christos 					   dns_rdatatype_nsec, 0, &flag));
   1801   1.1  christos 			if (flag) {
   1802   1.1  christos 				/*
   1803   1.1  christos 				 * There is a NSEC, but we don't know if it
   1804   1.1  christos 				 * is correct. Delete it and create a correct
   1805   1.1  christos 				 * one to be sure. If the update was
   1806   1.1  christos 				 * unnecessary, the diff minimization
   1807   1.1  christos 				 * will take care of eliminating it from the
   1808   1.1  christos 				 * journal, IXFRs, etc.
   1809   1.1  christos 				 *
   1810   1.1  christos 				 * The RRSIG bit should always be set in the
   1811   1.1  christos 				 * NSECs we generate, because they will all
   1812   1.1  christos 				 * get RRSIG NSECs.
   1813   1.1  christos 				 * (XXX what if the zone keys are missing?).
   1814   1.1  christos 				 * Because the RRSIG NSECs have not necessarily
   1815   1.1  christos 				 * been created yet, the correctness of the
   1816   1.1  christos 				 * bit mask relies on the assumption that NSECs
   1817   1.1  christos 				 * are only created if there is other data, and
   1818   1.1  christos 				 * if there is other data, there are other
   1819   1.1  christos 				 * RRSIGs.
   1820   1.1  christos 				 */
   1821   1.1  christos 				CHECK(add_nsec(log, zone, db, newver, &t->name,
   1822   1.1  christos 					       state->nsecttl,
   1823   1.1  christos 					       &state->nsec_diff));
   1824   1.1  christos 			}
   1825   1.1  christos 		}
   1826   1.1  christos 
   1827   1.1  christos 		/*
   1828   1.1  christos 		 * Minimize the set of NSEC updates so that we don't
   1829   1.1  christos 		 * have to regenerate the RRSIG NSECs for NSECs that were
   1830   1.1  christos 		 * replaced with identical ones.
   1831   1.1  christos 		 */
   1832   1.1  christos 		while ((t = ISC_LIST_HEAD(state->nsec_diff.tuples)) != NULL) {
   1833   1.1  christos 			ISC_LIST_UNLINK(state->nsec_diff.tuples, t, link);
   1834   1.1  christos 			dns_diff_appendminimal(&state->nsec_mindiff, &t);
   1835   1.1  christos 		}
   1836   1.1  christos 
   1837   1.1  christos 		update_log(log, zone, ISC_LOG_DEBUG(3),
   1838   1.1  christos 			   "signing rebuilt NSEC chain");
   1839   1.1  christos 
   1840  1.11  christos 		FALLTHROUGH;
   1841   1.1  christos 	case sign_nsec:
   1842   1.1  christos 		state->state = sign_nsec;
   1843   1.1  christos 		/* Update RRSIG NSECs. */
   1844   1.1  christos 		while ((t = ISC_LIST_HEAD(state->nsec_mindiff.tuples)) != NULL)
   1845   1.1  christos 		{
   1846   1.1  christos 			if (t->op == DNS_DIFFOP_DEL) {
   1847   1.1  christos 				CHECK(delete_if(true_p, db, newver, &t->name,
   1848   1.1  christos 						dns_rdatatype_rrsig,
   1849   1.6  christos 						dns_rdatatype_nsec, NULL,
   1850   1.6  christos 						&state->sig_diff));
   1851   1.1  christos 			} else if (t->op == DNS_DIFFOP_ADD) {
   1852   1.1  christos 				CHECK(add_sigs(log, zone, db, newver, &t->name,
   1853   1.1  christos 					       dns_rdatatype_nsec,
   1854   1.1  christos 					       &state->sig_diff,
   1855   1.1  christos 					       state->zone_keys, state->nkeys,
   1856  1.13  christos 					       state->now, state->inception,
   1857  1.15  christos 					       state->expire));
   1858   1.1  christos 				sigs++;
   1859   1.1  christos 			} else {
   1860  1.11  christos 				UNREACHABLE();
   1861   1.1  christos 			}
   1862   1.1  christos 			ISC_LIST_UNLINK(state->nsec_mindiff.tuples, t, link);
   1863   1.1  christos 			ISC_LIST_APPEND(state->work.tuples, t, link);
   1864   1.6  christos 			if (state != &mystate && sigs > maxsigs) {
   1865  1.15  christos 				return DNS_R_CONTINUE;
   1866   1.6  christos 			}
   1867   1.1  christos 		}
   1868   1.1  christos 		ISC_LIST_APPENDLIST(state->nsec_mindiff.tuples,
   1869   1.1  christos 				    state->work.tuples, link);
   1870  1.11  christos 		FALLTHROUGH;
   1871   1.1  christos 	case update_nsec3:
   1872   1.1  christos 		state->state = update_nsec3;
   1873   1.1  christos 
   1874   1.1  christos 		/* Record our changes for the journal. */
   1875   1.1  christos 		while ((t = ISC_LIST_HEAD(state->sig_diff.tuples)) != NULL) {
   1876   1.1  christos 			ISC_LIST_UNLINK(state->sig_diff.tuples, t, link);
   1877   1.1  christos 			dns_diff_appendminimal(diff, &t);
   1878   1.1  christos 		}
   1879   1.1  christos 		while ((t = ISC_LIST_HEAD(state->nsec_mindiff.tuples)) != NULL)
   1880   1.1  christos 		{
   1881   1.1  christos 			ISC_LIST_UNLINK(state->nsec_mindiff.tuples, t, link);
   1882   1.1  christos 			dns_diff_appendminimal(diff, &t);
   1883   1.1  christos 		}
   1884   1.1  christos 
   1885   1.1  christos 		INSIST(ISC_LIST_EMPTY(state->sig_diff.tuples));
   1886   1.1  christos 		INSIST(ISC_LIST_EMPTY(state->nsec_diff.tuples));
   1887   1.1  christos 		INSIST(ISC_LIST_EMPTY(state->nsec_mindiff.tuples));
   1888   1.1  christos 
   1889   1.1  christos 		if (!state->build_nsec3) {
   1890   1.1  christos 			update_log(log, zone, ISC_LOG_DEBUG(3),
   1891   1.1  christos 				   "no NSEC3 chains to rebuild");
   1892  1.16  christos 			goto cleanup;
   1893   1.1  christos 		}
   1894   1.1  christos 
   1895   1.1  christos 		update_log(log, zone, ISC_LOG_DEBUG(3),
   1896   1.1  christos 			   "rebuilding NSEC3 chains");
   1897   1.1  christos 
   1898   1.1  christos 		dns_diff_clear(&state->diffnames);
   1899   1.1  christos 		dns_diff_clear(&state->affected);
   1900   1.1  christos 
   1901   1.1  christos 		CHECK(dns_diff_sort(diff, temp_order));
   1902   1.1  christos 
   1903   1.1  christos 		/*
   1904   1.1  christos 		 * Find names potentially affected by delegation changes
   1905   1.1  christos 		 * (obscured by adding an NS or DNAME, or unobscured by
   1906   1.1  christos 		 * removing one).
   1907   1.1  christos 		 */
   1908   1.1  christos 		t = ISC_LIST_HEAD(diff->tuples);
   1909   1.1  christos 		while (t != NULL) {
   1910   1.1  christos 			dns_name_t *name = &t->name;
   1911   1.1  christos 
   1912   1.3  christos 			bool ns_existed, dname_existed;
   1913   1.3  christos 			bool ns_exists, dname_exists;
   1914   1.3  christos 			bool exists, existed;
   1915   1.1  christos 
   1916   1.1  christos 			if (t->rdata.type == dns_rdatatype_nsec ||
   1917  1.12  christos 			    t->rdata.type == dns_rdatatype_rrsig)
   1918  1.12  christos 			{
   1919   1.1  christos 				t = ISC_LIST_NEXT(t, link);
   1920   1.1  christos 				continue;
   1921   1.1  christos 			}
   1922   1.1  christos 
   1923   1.1  christos 			CHECK(namelist_append_name(&state->affected, name));
   1924   1.1  christos 
   1925   1.6  christos 			if (oldver != NULL) {
   1926   1.1  christos 				CHECK(rrset_exists(db, oldver, name,
   1927   1.6  christos 						   dns_rdatatype_ns, 0,
   1928   1.6  christos 						   &ns_existed));
   1929   1.6  christos 			} else {
   1930   1.3  christos 				ns_existed = false;
   1931   1.6  christos 			}
   1932   1.6  christos 			if (oldver != NULL) {
   1933   1.1  christos 				CHECK(rrset_exists(db, oldver, name,
   1934   1.1  christos 						   dns_rdatatype_dname, 0,
   1935   1.1  christos 						   &dname_existed));
   1936   1.6  christos 			} else {
   1937   1.3  christos 				dname_existed = false;
   1938   1.6  christos 			}
   1939   1.1  christos 			CHECK(rrset_exists(db, newver, name, dns_rdatatype_ns,
   1940   1.1  christos 					   0, &ns_exists));
   1941   1.1  christos 			CHECK(rrset_exists(db, newver, name,
   1942   1.1  christos 					   dns_rdatatype_dname, 0,
   1943   1.1  christos 					   &dname_exists));
   1944   1.1  christos 
   1945   1.1  christos 			exists = ns_exists || dname_exists;
   1946   1.1  christos 			existed = ns_existed || dname_existed;
   1947   1.6  christos 			if (exists == existed) {
   1948   1.1  christos 				goto nextname;
   1949   1.6  christos 			}
   1950   1.1  christos 			/*
   1951   1.1  christos 			 * There was a delegation change.  Mark all subdomains
   1952   1.1  christos 			 * of t->name as potentially needing a NSEC3 update.
   1953   1.1  christos 			 */
   1954   1.1  christos 			CHECK(namelist_append_subdomain(db, name,
   1955   1.1  christos 							&state->affected));
   1956   1.1  christos 
   1957   1.6  christos 		nextname:
   1958   1.6  christos 			while (t != NULL && dns_name_equal(&t->name, name)) {
   1959   1.1  christos 				t = ISC_LIST_NEXT(t, link);
   1960   1.6  christos 			}
   1961   1.1  christos 		}
   1962   1.1  christos 
   1963  1.11  christos 		FALLTHROUGH;
   1964   1.1  christos 	case process_nsec3:
   1965   1.1  christos 		state->state = process_nsec3;
   1966   1.1  christos 		while ((t = ISC_LIST_HEAD(state->affected.tuples)) != NULL) {
   1967   1.1  christos 			dns_name_t *name = &t->name;
   1968   1.1  christos 
   1969   1.6  christos 			unsecure = false; /* Silence compiler warning. */
   1970   1.1  christos 			CHECK(is_active(db, newver, name, &flag, &cut,
   1971   1.1  christos 					&unsecure));
   1972   1.1  christos 
   1973   1.1  christos 			if (!flag) {
   1974   1.1  christos 				CHECK(delete_if(rrsig_p, db, newver, name,
   1975   1.1  christos 						dns_rdatatype_any, 0, NULL,
   1976   1.1  christos 						diff));
   1977   1.1  christos 				CHECK(dns_nsec3_delnsec3sx(db, newver, name,
   1978   1.1  christos 							   privatetype,
   1979   1.1  christos 							   &state->nsec_diff));
   1980   1.1  christos 			} else {
   1981   1.6  christos 				CHECK(add_exposed_sigs(
   1982   1.6  christos 					log, zone, db, newver, name, cut,
   1983   1.6  christos 					&state->sig_diff, state->zone_keys,
   1984  1.13  christos 					state->nkeys, state->now,
   1985  1.13  christos 					state->inception, state->expire,
   1986  1.13  christos 					&sigs));
   1987   1.6  christos 				CHECK(dns_nsec3_addnsec3sx(
   1988   1.6  christos 					db, newver, name, state->nsecttl,
   1989   1.6  christos 					unsecure, privatetype,
   1990   1.6  christos 					&state->nsec_diff));
   1991   1.1  christos 			}
   1992   1.1  christos 			ISC_LIST_UNLINK(state->affected.tuples, t, link);
   1993   1.1  christos 			ISC_LIST_APPEND(state->work.tuples, t, link);
   1994   1.6  christos 			if (state != &mystate && sigs > maxsigs) {
   1995  1.15  christos 				return DNS_R_CONTINUE;
   1996   1.6  christos 			}
   1997   1.1  christos 		}
   1998   1.6  christos 		ISC_LIST_APPENDLIST(state->affected.tuples, state->work.tuples,
   1999   1.6  christos 				    link);
   2000   1.1  christos 
   2001   1.1  christos 		/*
   2002   1.1  christos 		 * Minimize the set of NSEC3 updates so that we don't
   2003   1.1  christos 		 * have to regenerate the RRSIG NSEC3s for NSEC3s that were
   2004   1.1  christos 		 * replaced with identical ones.
   2005   1.1  christos 		 */
   2006   1.1  christos 		while ((t = ISC_LIST_HEAD(state->nsec_diff.tuples)) != NULL) {
   2007   1.1  christos 			ISC_LIST_UNLINK(state->nsec_diff.tuples, t, link);
   2008   1.1  christos 			dns_diff_appendminimal(&state->nsec_mindiff, &t);
   2009   1.1  christos 		}
   2010   1.1  christos 
   2011   1.1  christos 		update_log(log, zone, ISC_LOG_DEBUG(3),
   2012   1.1  christos 			   "signing rebuilt NSEC3 chain");
   2013   1.1  christos 
   2014  1.11  christos 		FALLTHROUGH;
   2015   1.1  christos 	case sign_nsec3:
   2016   1.1  christos 		state->state = sign_nsec3;
   2017   1.1  christos 		/* Update RRSIG NSEC3s. */
   2018   1.1  christos 		while ((t = ISC_LIST_HEAD(state->nsec_mindiff.tuples)) != NULL)
   2019   1.1  christos 		{
   2020   1.1  christos 			if (t->op == DNS_DIFFOP_DEL) {
   2021   1.1  christos 				CHECK(delete_if(true_p, db, newver, &t->name,
   2022   1.1  christos 						dns_rdatatype_rrsig,
   2023   1.6  christos 						dns_rdatatype_nsec3, NULL,
   2024   1.6  christos 						&state->sig_diff));
   2025   1.1  christos 			} else if (t->op == DNS_DIFFOP_ADD) {
   2026   1.1  christos 				CHECK(add_sigs(log, zone, db, newver, &t->name,
   2027   1.1  christos 					       dns_rdatatype_nsec3,
   2028   1.1  christos 					       &state->sig_diff,
   2029   1.6  christos 					       state->zone_keys, state->nkeys,
   2030  1.13  christos 					       state->now, state->inception,
   2031  1.15  christos 					       state->expire));
   2032   1.1  christos 				sigs++;
   2033   1.1  christos 			} else {
   2034  1.11  christos 				UNREACHABLE();
   2035   1.1  christos 			}
   2036   1.1  christos 			ISC_LIST_UNLINK(state->nsec_mindiff.tuples, t, link);
   2037   1.1  christos 			ISC_LIST_APPEND(state->work.tuples, t, link);
   2038   1.6  christos 			if (state != &mystate && sigs > maxsigs) {
   2039  1.15  christos 				return DNS_R_CONTINUE;
   2040   1.6  christos 			}
   2041   1.1  christos 		}
   2042   1.1  christos 		ISC_LIST_APPENDLIST(state->nsec_mindiff.tuples,
   2043   1.1  christos 				    state->work.tuples, link);
   2044   1.1  christos 
   2045   1.1  christos 		/* Record our changes for the journal. */
   2046   1.1  christos 		while ((t = ISC_LIST_HEAD(state->sig_diff.tuples)) != NULL) {
   2047   1.1  christos 			ISC_LIST_UNLINK(state->sig_diff.tuples, t, link);
   2048   1.1  christos 			dns_diff_appendminimal(diff, &t);
   2049   1.1  christos 		}
   2050   1.1  christos 		while ((t = ISC_LIST_HEAD(state->nsec_mindiff.tuples)) != NULL)
   2051   1.1  christos 		{
   2052   1.1  christos 			ISC_LIST_UNLINK(state->nsec_mindiff.tuples, t, link);
   2053   1.1  christos 			dns_diff_appendminimal(diff, &t);
   2054   1.1  christos 		}
   2055   1.1  christos 
   2056   1.1  christos 		INSIST(ISC_LIST_EMPTY(state->sig_diff.tuples));
   2057   1.1  christos 		INSIST(ISC_LIST_EMPTY(state->nsec_diff.tuples));
   2058   1.1  christos 		INSIST(ISC_LIST_EMPTY(state->nsec_mindiff.tuples));
   2059   1.1  christos 		break;
   2060   1.1  christos 	default:
   2061  1.11  christos 		UNREACHABLE();
   2062   1.1  christos 	}
   2063   1.1  christos 
   2064  1.16  christos cleanup:
   2065   1.1  christos 	if (node != NULL) {
   2066   1.1  christos 		dns_db_detachnode(db, &node);
   2067   1.1  christos 	}
   2068   1.1  christos 
   2069   1.1  christos 	dns_diff_clear(&state->sig_diff);
   2070   1.1  christos 	dns_diff_clear(&state->nsec_diff);
   2071   1.1  christos 	dns_diff_clear(&state->nsec_mindiff);
   2072   1.1  christos 
   2073   1.1  christos 	dns_diff_clear(&state->affected);
   2074   1.1  christos 	dns_diff_clear(&state->diffnames);
   2075   1.1  christos 	dns_diff_clear(&state->work);
   2076   1.1  christos 
   2077   1.6  christos 	for (i = 0; i < state->nkeys; i++) {
   2078   1.1  christos 		dst_key_free(&state->zone_keys[i]);
   2079   1.6  christos 	}
   2080   1.1  christos 
   2081   1.8  christos 	if (state != &mystate) {
   2082   1.1  christos 		*statep = NULL;
   2083   1.1  christos 		state->magic = 0;
   2084   1.1  christos 		isc_mem_put(diff->mctx, state, sizeof(*state));
   2085   1.1  christos 	}
   2086   1.1  christos 
   2087  1.15  christos 	return result;
   2088   1.1  christos }
   2089   1.1  christos 
   2090   1.1  christos static isc_stdtime_t
   2091   1.1  christos epoch_to_yyyymmdd(time_t when) {
   2092   1.6  christos 	struct tm t, *tm = localtime_r(&when, &t);
   2093   1.6  christos 	if (tm == NULL) {
   2094  1.15  christos 		return 0;
   2095   1.6  christos 	}
   2096  1.15  christos 	return ((tm->tm_year + 1900) * 10000) + ((tm->tm_mon + 1) * 100) +
   2097  1.15  christos 	       tm->tm_mday;
   2098   1.1  christos }
   2099   1.1  christos 
   2100   1.8  christos static uint32_t
   2101   1.8  christos dns__update_soaserial(uint32_t serial, dns_updatemethod_t method) {
   2102   1.1  christos 	isc_stdtime_t now;
   2103   1.1  christos 
   2104   1.1  christos 	switch (method) {
   2105   1.1  christos 	case dns_updatemethod_none:
   2106  1.15  christos 		return serial;
   2107   1.1  christos 	case dns_updatemethod_unixtime:
   2108  1.15  christos 		now = isc_stdtime_now();
   2109  1.15  christos 		return now;
   2110   1.8  christos 	case dns_updatemethod_date:
   2111  1.15  christos 		now = isc_stdtime_now();
   2112  1.15  christos 		return epoch_to_yyyymmdd((time_t)now) * 100;
   2113   1.8  christos 	case dns_updatemethod_increment:
   2114   1.8  christos 		/* RFC1982 */
   2115   1.8  christos 		serial = (serial + 1) & 0xFFFFFFFF;
   2116   1.8  christos 		if (serial == 0) {
   2117  1.15  christos 			return 1;
   2118   1.6  christos 		}
   2119  1.15  christos 		return serial;
   2120   1.8  christos 	default:
   2121  1.11  christos 		UNREACHABLE();
   2122   1.8  christos 	}
   2123   1.8  christos }
   2124   1.8  christos 
   2125   1.8  christos uint32_t
   2126   1.8  christos dns_update_soaserial(uint32_t serial, dns_updatemethod_t method,
   2127   1.8  christos 		     dns_updatemethod_t *used) {
   2128   1.8  christos 	uint32_t new_serial = dns__update_soaserial(serial, method);
   2129   1.8  christos 	switch (method) {
   2130   1.8  christos 	case dns_updatemethod_none:
   2131   1.8  christos 	case dns_updatemethod_increment:
   2132   1.1  christos 		break;
   2133   1.8  christos 	case dns_updatemethod_unixtime:
   2134   1.1  christos 	case dns_updatemethod_date:
   2135   1.8  christos 		if (!(new_serial != 0 && isc_serial_gt(new_serial, serial))) {
   2136  1.10  christos 			/*
   2137  1.10  christos 			 * If the new date serial following YYYYMMDD00 is equal
   2138  1.10  christos 			 * to or smaller than the current serial, but YYYYMMDD99
   2139  1.10  christos 			 * would be larger, pretend we have used the
   2140  1.10  christos 			 * "dns_updatemethod_date" method.
   2141  1.10  christos 			 */
   2142  1.10  christos 			if (method == dns_updatemethod_unixtime ||
   2143  1.10  christos 			    !isc_serial_gt(new_serial + 99, serial))
   2144  1.10  christos 			{
   2145  1.10  christos 				method = dns_updatemethod_increment;
   2146  1.10  christos 			}
   2147  1.10  christos 			new_serial = dns__update_soaserial(
   2148  1.10  christos 				serial, dns_updatemethod_increment);
   2149   1.6  christos 		}
   2150   1.1  christos 		break;
   2151   1.8  christos 	default:
   2152  1.11  christos 		UNREACHABLE();
   2153   1.1  christos 	}
   2154   1.1  christos 
   2155  1.15  christos 	SET_IF_NOT_NULL(used, method);
   2156   1.1  christos 
   2157  1.15  christos 	return new_serial;
   2158   1.1  christos }
   2159