Home | History | Annotate | Line # | Download | only in dns
xfrin.c revision 1.14.2.1
      1 /*	$NetBSD: xfrin.c,v 1.14.2.1 2025/08/02 05:53:32 perseant Exp $	*/
      2 
      3 /*
      4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
      5  *
      6  * SPDX-License-Identifier: MPL-2.0
      7  *
      8  * This Source Code Form is subject to the terms of the Mozilla Public
      9  * License, v. 2.0. If a copy of the MPL was not distributed with this
     10  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
     11  *
     12  * See the COPYRIGHT file distributed with this work for additional
     13  * information regarding copyright ownership.
     14  */
     15 
     16 /*! \file */
     17 
     18 #include <inttypes.h>
     19 #include <stdbool.h>
     20 
     21 #include <isc/async.h>
     22 #include <isc/atomic.h>
     23 #include <isc/mem.h>
     24 #include <isc/random.h>
     25 #include <isc/result.h>
     26 #include <isc/string.h>
     27 #include <isc/util.h>
     28 #include <isc/work.h>
     29 
     30 #include <dns/callbacks.h>
     31 #include <dns/catz.h>
     32 #include <dns/db.h>
     33 #include <dns/diff.h>
     34 #include <dns/dispatch.h>
     35 #include <dns/journal.h>
     36 #include <dns/log.h>
     37 #include <dns/message.h>
     38 #include <dns/peer.h>
     39 #include <dns/rdataclass.h>
     40 #include <dns/rdatalist.h>
     41 #include <dns/rdataset.h>
     42 #include <dns/result.h>
     43 #include <dns/soa.h>
     44 #include <dns/trace.h>
     45 #include <dns/transport.h>
     46 #include <dns/tsig.h>
     47 #include <dns/view.h>
     48 #include <dns/xfrin.h>
     49 #include <dns/zone.h>
     50 
     51 #include <dst/dst.h>
     52 
     53 #include "probes.h"
     54 
     55 /*
     56  * Incoming AXFR and IXFR.
     57  */
     58 
     59 #define CHECK(op)                              \
     60 	{                                      \
     61 		result = (op);                 \
     62 		if (result != ISC_R_SUCCESS) { \
     63 			goto failure;          \
     64 		}                              \
     65 	}
     66 
     67 /*%
     68  * The states of the *XFR state machine.  We handle both IXFR and AXFR
     69  * with a single integrated state machine because they cannot be distinguished
     70  * immediately - an AXFR response to an IXFR request can only be detected
     71  * when the first two (2) response RRs have already been received.
     72  */
     73 typedef enum {
     74 	XFRST_SOAQUERY,
     75 	XFRST_GOTSOA,
     76 	XFRST_ZONEXFRREQUEST,
     77 	XFRST_FIRSTDATA,
     78 	XFRST_IXFR_DELSOA,
     79 	XFRST_IXFR_DEL,
     80 	XFRST_IXFR_ADDSOA,
     81 	XFRST_IXFR_ADD,
     82 	XFRST_IXFR_END,
     83 	XFRST_AXFR,
     84 	XFRST_AXFR_END
     85 } xfrin_state_t;
     86 
     87 #ifdef _LP64
     88 #define ISC_XFRIN_LOAD(a, t)	atomic_load_relaxed(a)
     89 #define ISC_XFRIN_STORE(a, b)	atomic_store_relaxed(a, b)
     90 #define ISC_XFRIN_ADD(a, b) 	atomic_fetch_add_relaxed(a, b)
     91 #else
     92 static isc_mutex_t xfrin_lock = PTHREAD_MUTEX_INITIALIZER;
     93 #define ISC_XFRIN_LOAD(a, t) \
     94 	({ \
     95 		isc_mutex_lock(&xfrin_lock); \
     96 		t x = *(a); \
     97 		isc_mutex_unlock(&xfrin_lock); \
     98 		x; \
     99 	})
    100 #define ISC_XFRIN_STORE(a, b) \
    101 	({ \
    102 		isc_mutex_lock(&xfrin_lock); \
    103 		 *(a) = (b); \
    104 		isc_mutex_unlock(&xfrin_lock); \
    105 	})
    106 #define ISC_XFRIN_ADD(a, b) \
    107 	({ \
    108 		isc_mutex_lock(&xfrin_lock); \
    109 		 *(a) += (b); \
    110 		isc_mutex_unlock(&xfrin_lock); \
    111 	})
    112 #endif
    113 
    114 
    115 /*%
    116  * Incoming zone transfer context.
    117  */
    118 
    119 struct dns_xfrin {
    120 	unsigned int magic;
    121 	isc_mem_t *mctx;
    122 	dns_zone_t *zone;
    123 	dns_view_t *view;
    124 
    125 	isc_refcount_t references;
    126 
    127 	atomic_bool shuttingdown;
    128 
    129 	isc_result_t shutdown_result;
    130 
    131 	dns_name_t name; /*%< Name of zone to transfer */
    132 	dns_rdataclass_t rdclass;
    133 
    134 	dns_messageid_t id;
    135 
    136 	/*%
    137 	 * Requested transfer type (dns_rdatatype_axfr or
    138 	 * dns_rdatatype_ixfr).  The actual transfer type
    139 	 * may differ due to IXFR->AXFR fallback.
    140 	 */
    141 	dns_rdatatype_t reqtype;
    142 
    143 	isc_sockaddr_t primaryaddr;
    144 	isc_sockaddr_t sourceaddr;
    145 
    146 	dns_dispatch_t *disp;
    147 	dns_dispentry_t *dispentry;
    148 
    149 	/*% Buffer for IXFR/AXFR request message */
    150 	isc_buffer_t qbuffer;
    151 	unsigned char qbuffer_data[512];
    152 
    153 	/*%
    154 	 * Whether the zone originally had a database attached at the time this
    155 	 * transfer context was created.  Used by xfrin_destroy() when making
    156 	 * logging decisions.
    157 	 */
    158 	bool zone_had_db;
    159 
    160 	dns_db_t *db;
    161 	dns_dbversion_t *ver;
    162 	dns_diff_t diff; /*%< Pending database changes */
    163 
    164 	/* Diff queue */
    165 	bool diff_running;
    166 	struct __cds_wfcq_head diff_head;
    167 	struct cds_wfcq_tail diff_tail;
    168 
    169 	_Atomic xfrin_state_t state;
    170 	uint32_t expireopt;
    171 	bool edns, expireoptset;
    172 	atomic_bool is_ixfr;
    173 
    174 	/*
    175 	 * Following variable were made atomic only for loading the values for
    176 	 * the statistics channel, thus all accesses can be **relaxed** because
    177 	 * all store and load operations that affect XFR are done on the same
    178 	 * thread and only the statistics channel thread could perform a load
    179 	 * operation from a different thread and it's ok to not be precise in
    180 	 * the statistics.
    181 	 */
    182 	atomic_uint nmsg;	     /*%< Number of messages recvd */
    183 	atomic_uint nrecs;	     /*%< Number of records recvd */
    184 #ifdef _LP64
    185 	atomic_uint_fast64_t nbytes; /*%< Number of bytes received */
    186 	_Atomic(isc_time_t) start;   /*%< Start time of the transfer */
    187 	atomic_uint_fast64_t rate_bytes_per_second;
    188 #else
    189 	atomic_uint_fast32_t nbytes; /*%< Number of bytes received */
    190 	isc_time_t start;	     /*%< Start time of the transfer */
    191 	atomic_uint_fast32_t rate_bytes_per_second;
    192 #endif
    193 	_Atomic(dns_transport_type_t) soa_transport_type;
    194 	atomic_uint_fast32_t end_serial;
    195 
    196 	unsigned int maxrecords; /*%< The maximum number of
    197 				  *   records set for the zone */
    198 	uint64_t nbytes_saved;	 /*%< For enforcing the minimum transfer rate */
    199 
    200 	dns_tsigkey_t *tsigkey; /*%< Key used to create TSIG */
    201 	isc_buffer_t *lasttsig; /*%< The last TSIG */
    202 	dst_context_t *tsigctx; /*%< TSIG verification context */
    203 	unsigned int sincetsig; /*%< recvd since the last TSIG */
    204 
    205 	dns_transport_t *transport;
    206 
    207 	dns_xfrindone_t done;
    208 
    209 	/*%
    210 	 * AXFR- and IXFR-specific data.  Only one is used at a time
    211 	 * according to the is_ixfr flag, so this could be a union,
    212 	 * but keeping them separate makes it a bit simpler to clean
    213 	 * things up when destroying the context.
    214 	 */
    215 	dns_rdatacallbacks_t axfr;
    216 
    217 	struct {
    218 		uint32_t request_serial;
    219 		uint32_t current_serial;
    220 		dns_journal_t *journal;
    221 	} ixfr;
    222 
    223 	dns_rdata_t firstsoa;
    224 	unsigned char *firstsoa_data;
    225 
    226 	isc_tlsctx_cache_t *tlsctx_cache;
    227 
    228 	isc_loop_t *loop;
    229 
    230 	isc_timer_t *min_rate_timer;
    231 	isc_timer_t *max_time_timer;
    232 	isc_timer_t *max_idle_timer;
    233 
    234 	char info[DNS_NAME_MAXTEXT + 32];
    235 };
    236 
    237 #define XFRIN_MAGIC    ISC_MAGIC('X', 'f', 'r', 'I')
    238 #define VALID_XFRIN(x) ISC_MAGIC_VALID(x, XFRIN_MAGIC)
    239 
    240 #define XFRIN_WORK_MAGIC    ISC_MAGIC('X', 'f', 'r', 'W')
    241 #define VALID_XFRIN_WORK(x) ISC_MAGIC_VALID(x, XFRIN_WORK_MAGIC)
    242 
    243 typedef struct xfrin_work {
    244 	unsigned int magic;
    245 	isc_result_t result;
    246 	dns_xfrin_t *xfr;
    247 } xfrin_work_t;
    248 
    249 /**************************************************************************/
    250 /*
    251  * Forward declarations.
    252  */
    253 
    254 static void
    255 xfrin_create(isc_mem_t *mctx, dns_zone_t *zone, dns_db_t *db, isc_loop_t *loop,
    256 	     dns_name_t *zonename, dns_rdataclass_t rdclass,
    257 	     dns_rdatatype_t reqtype, const isc_sockaddr_t *primaryaddr,
    258 	     const isc_sockaddr_t *sourceaddr, dns_tsigkey_t *tsigkey,
    259 	     dns_transport_type_t soa_transport_type,
    260 	     dns_transport_t *transport, isc_tlsctx_cache_t *tlsctx_cache,
    261 	     dns_xfrin_t **xfrp);
    262 
    263 static isc_result_t
    264 axfr_init(dns_xfrin_t *xfr);
    265 static isc_result_t
    266 axfr_putdata(dns_xfrin_t *xfr, dns_diffop_t op, dns_name_t *name, dns_ttl_t ttl,
    267 	     dns_rdata_t *rdata);
    268 static void
    269 axfr_commit(dns_xfrin_t *xfr);
    270 static isc_result_t
    271 axfr_finalize(dns_xfrin_t *xfr);
    272 
    273 static isc_result_t
    274 ixfr_init(dns_xfrin_t *xfr);
    275 static isc_result_t
    276 ixfr_putdata(dns_xfrin_t *xfr, dns_diffop_t op, dns_name_t *name, dns_ttl_t ttl,
    277 	     dns_rdata_t *rdata);
    278 static isc_result_t
    279 ixfr_commit(dns_xfrin_t *xfr);
    280 
    281 static isc_result_t
    282 xfr_rr(dns_xfrin_t *xfr, dns_name_t *name, uint32_t ttl, dns_rdata_t *rdata);
    283 
    284 static isc_result_t
    285 xfrin_start(dns_xfrin_t *xfr);
    286 
    287 static void
    288 xfrin_connect_done(isc_result_t result, isc_region_t *region, void *arg);
    289 static isc_result_t
    290 xfrin_send_request(dns_xfrin_t *xfr);
    291 static void
    292 xfrin_send_done(isc_result_t eresult, isc_region_t *region, void *arg);
    293 static void
    294 xfrin_recv_done(isc_result_t result, isc_region_t *region, void *arg);
    295 
    296 static void
    297 xfrin_end(dns_xfrin_t *xfr, isc_result_t result);
    298 
    299 static void
    300 xfrin_destroy(dns_xfrin_t *xfr);
    301 
    302 static void
    303 xfrin_timedout(void *);
    304 static void
    305 xfrin_idledout(void *);
    306 static void
    307 xfrin_minratecheck(void *);
    308 static void
    309 xfrin_fail(dns_xfrin_t *xfr, isc_result_t result, const char *msg);
    310 static isc_result_t
    311 render(dns_message_t *msg, isc_mem_t *mctx, isc_buffer_t *buf);
    312 
    313 static void
    314 xfrin_log(dns_xfrin_t *xfr, int level, const char *fmt, ...)
    315 	ISC_FORMAT_PRINTF(3, 4);
    316 
    317 /**************************************************************************/
    318 /*
    319  * AXFR handling
    320  */
    321 
    322 static isc_result_t
    323 axfr_init(dns_xfrin_t *xfr) {
    324 	isc_result_t result;
    325 
    326 	atomic_store(&xfr->is_ixfr, false);
    327 
    328 	if (xfr->db != NULL) {
    329 		dns_db_detach(&xfr->db);
    330 	}
    331 
    332 	CHECK(dns_zone_makedb(xfr->zone, &xfr->db));
    333 
    334 	dns_zone_rpz_enable_db(xfr->zone, xfr->db);
    335 	dns_zone_catz_enable_db(xfr->zone, xfr->db);
    336 
    337 	dns_rdatacallbacks_init(&xfr->axfr);
    338 	CHECK(dns_db_beginload(xfr->db, &xfr->axfr));
    339 	result = ISC_R_SUCCESS;
    340 failure:
    341 	return result;
    342 }
    343 
    344 static void
    345 axfr_apply(void *arg);
    346 
    347 static isc_result_t
    348 axfr_putdata(dns_xfrin_t *xfr, dns_diffop_t op, dns_name_t *name, dns_ttl_t ttl,
    349 	     dns_rdata_t *rdata) {
    350 	isc_result_t result;
    351 
    352 	dns_difftuple_t *tuple = NULL;
    353 
    354 	if (rdata->rdclass != xfr->rdclass) {
    355 		return DNS_R_BADCLASS;
    356 	}
    357 
    358 	CHECK(dns_zone_checknames(xfr->zone, name, rdata));
    359 
    360 	if (dns_diff_size(&xfr->diff) > 128 &&
    361 	    dns_diff_is_boundary(&xfr->diff, name))
    362 	{
    363 		xfrin_work_t work = (xfrin_work_t){
    364 			.magic = XFRIN_WORK_MAGIC,
    365 			.result = ISC_R_UNSET,
    366 			.xfr = xfr,
    367 		};
    368 		axfr_apply((void *)&work);
    369 		CHECK(work.result);
    370 	}
    371 
    372 	CHECK(dns_difftuple_create(xfr->diff.mctx, op, name, ttl, rdata,
    373 				   &tuple));
    374 	dns_diff_append(&xfr->diff, &tuple);
    375 
    376 	result = ISC_R_SUCCESS;
    377 failure:
    378 	return result;
    379 }
    380 
    381 /*
    382  * Store a set of AXFR RRs in the database.
    383  */
    384 static void
    385 axfr_apply(void *arg) {
    386 	xfrin_work_t *work = arg;
    387 	REQUIRE(VALID_XFRIN_WORK(work));
    388 
    389 	dns_xfrin_t *xfr = work->xfr;
    390 	REQUIRE(VALID_XFRIN(xfr));
    391 
    392 	isc_result_t result = ISC_R_SUCCESS;
    393 	uint64_t records;
    394 
    395 	if (atomic_load(&xfr->shuttingdown)) {
    396 		result = ISC_R_SHUTTINGDOWN;
    397 		goto failure;
    398 	}
    399 
    400 	CHECK(dns_diff_load(&xfr->diff, &xfr->axfr));
    401 	if (xfr->maxrecords != 0U) {
    402 		result = dns_db_getsize(xfr->db, xfr->ver, &records, NULL);
    403 		if (result == ISC_R_SUCCESS && records > xfr->maxrecords) {
    404 			result = DNS_R_TOOMANYRECORDS;
    405 			goto failure;
    406 		}
    407 	}
    408 
    409 failure:
    410 	dns_diff_clear(&xfr->diff);
    411 	work->result = result;
    412 }
    413 
    414 static void
    415 axfr_apply_done(void *arg) {
    416 	xfrin_work_t *work = arg;
    417 	REQUIRE(VALID_XFRIN_WORK(work));
    418 
    419 	dns_xfrin_t *xfr = work->xfr;
    420 	isc_result_t result = work->result;
    421 
    422 	REQUIRE(VALID_XFRIN(xfr));
    423 
    424 	if (atomic_load(&xfr->shuttingdown)) {
    425 		result = ISC_R_SHUTTINGDOWN;
    426 	}
    427 
    428 	if (result == ISC_R_SUCCESS) {
    429 		CHECK(dns_db_endload(xfr->db, &xfr->axfr));
    430 		CHECK(dns_zone_verifydb(xfr->zone, xfr->db, NULL));
    431 		CHECK(axfr_finalize(xfr));
    432 	} else {
    433 		(void)dns_db_endload(xfr->db, &xfr->axfr);
    434 	}
    435 
    436 failure:
    437 	xfr->diff_running = false;
    438 
    439 	isc_mem_put(xfr->mctx, work, sizeof(*work));
    440 
    441 	if (result == ISC_R_SUCCESS) {
    442 		if (atomic_load(&xfr->state) == XFRST_AXFR_END) {
    443 			xfrin_end(xfr, result);
    444 		}
    445 	} else {
    446 		xfrin_fail(xfr, result, "failed while processing responses");
    447 	}
    448 
    449 	dns_xfrin_detach(&xfr);
    450 }
    451 
    452 static void
    453 axfr_commit(dns_xfrin_t *xfr) {
    454 	REQUIRE(!xfr->diff_running);
    455 
    456 	xfrin_work_t *work = isc_mem_get(xfr->mctx, sizeof(*work));
    457 	*work = (xfrin_work_t){
    458 		.magic = XFRIN_WORK_MAGIC,
    459 		.result = ISC_R_UNSET,
    460 		.xfr = dns_xfrin_ref(xfr),
    461 	};
    462 	xfr->diff_running = true;
    463 	isc_work_enqueue(xfr->loop, axfr_apply, axfr_apply_done, work);
    464 }
    465 
    466 static isc_result_t
    467 axfr_finalize(dns_xfrin_t *xfr) {
    468 	isc_result_t result;
    469 
    470 	LIBDNS_XFRIN_AXFR_FINALIZE_BEGIN(xfr, xfr->info);
    471 	result = dns_zone_replacedb(xfr->zone, xfr->db, true);
    472 	LIBDNS_XFRIN_AXFR_FINALIZE_END(xfr, xfr->info, result);
    473 
    474 	return result;
    475 }
    476 
    477 /**************************************************************************/
    478 /*
    479  * IXFR handling
    480  */
    481 
    482 typedef struct ixfr_apply_data {
    483 	dns_diff_t diff; /*%< Pending database changes */
    484 	struct cds_wfcq_node wfcq_node;
    485 } ixfr_apply_data_t;
    486 
    487 static isc_result_t
    488 ixfr_init(dns_xfrin_t *xfr) {
    489 	isc_result_t result;
    490 	char *journalfile = NULL;
    491 
    492 	if (xfr->reqtype != dns_rdatatype_ixfr) {
    493 		xfrin_log(xfr, ISC_LOG_NOTICE,
    494 			  "got incremental response to AXFR request");
    495 		return DNS_R_FORMERR;
    496 	}
    497 
    498 	atomic_store(&xfr->is_ixfr, true);
    499 	INSIST(xfr->db != NULL);
    500 
    501 	journalfile = dns_zone_getjournal(xfr->zone);
    502 	if (journalfile != NULL) {
    503 		CHECK(dns_journal_open(xfr->mctx, journalfile,
    504 				       DNS_JOURNAL_CREATE, &xfr->ixfr.journal));
    505 	}
    506 
    507 	result = ISC_R_SUCCESS;
    508 failure:
    509 	return result;
    510 }
    511 
    512 static isc_result_t
    513 ixfr_putdata(dns_xfrin_t *xfr, dns_diffop_t op, dns_name_t *name, dns_ttl_t ttl,
    514 	     dns_rdata_t *rdata) {
    515 	isc_result_t result;
    516 	dns_difftuple_t *tuple = NULL;
    517 
    518 	if (rdata->rdclass != xfr->rdclass) {
    519 		return DNS_R_BADCLASS;
    520 	}
    521 
    522 	if (op == DNS_DIFFOP_ADD) {
    523 		CHECK(dns_zone_checknames(xfr->zone, name, rdata));
    524 	}
    525 	CHECK(dns_difftuple_create(xfr->diff.mctx, op, name, ttl, rdata,
    526 				   &tuple));
    527 	dns_diff_append(&xfr->diff, &tuple);
    528 	result = ISC_R_SUCCESS;
    529 failure:
    530 	return result;
    531 }
    532 
    533 static isc_result_t
    534 ixfr_begin_transaction(dns_xfrin_t *xfr) {
    535 	isc_result_t result = ISC_R_SUCCESS;
    536 
    537 	if (xfr->ixfr.journal != NULL) {
    538 		CHECK(dns_journal_begin_transaction(xfr->ixfr.journal));
    539 	}
    540 failure:
    541 	return result;
    542 }
    543 
    544 static isc_result_t
    545 ixfr_end_transaction(dns_xfrin_t *xfr) {
    546 	isc_result_t result = ISC_R_SUCCESS;
    547 
    548 	CHECK(dns_zone_verifydb(xfr->zone, xfr->db, xfr->ver));
    549 	/* XXX enter ready-to-commit state here */
    550 	if (xfr->ixfr.journal != NULL) {
    551 		CHECK(dns_journal_commit(xfr->ixfr.journal));
    552 	}
    553 failure:
    554 	return result;
    555 }
    556 
    557 static isc_result_t
    558 ixfr_apply_one(dns_xfrin_t *xfr, ixfr_apply_data_t *data) {
    559 	isc_result_t result = ISC_R_SUCCESS;
    560 	uint64_t records;
    561 
    562 	CHECK(ixfr_begin_transaction(xfr));
    563 
    564 	CHECK(dns_diff_apply(&data->diff, xfr->db, xfr->ver));
    565 	if (xfr->maxrecords != 0U) {
    566 		result = dns_db_getsize(xfr->db, xfr->ver, &records, NULL);
    567 		if (result == ISC_R_SUCCESS && records > xfr->maxrecords) {
    568 			result = DNS_R_TOOMANYRECORDS;
    569 			goto failure;
    570 		}
    571 	}
    572 	if (xfr->ixfr.journal != NULL) {
    573 		CHECK(dns_journal_writediff(xfr->ixfr.journal, &data->diff));
    574 	}
    575 
    576 	result = ixfr_end_transaction(xfr);
    577 
    578 	return result;
    579 failure:
    580 	/* We need to end the transaction, but keep the previous error */
    581 	(void)ixfr_end_transaction(xfr);
    582 
    583 	return result;
    584 }
    585 
    586 static void
    587 ixfr_apply(void *arg) {
    588 	xfrin_work_t *work = arg;
    589 	dns_xfrin_t *xfr = work->xfr;
    590 	isc_result_t result = ISC_R_SUCCESS;
    591 
    592 	REQUIRE(VALID_XFRIN(xfr));
    593 	REQUIRE(VALID_XFRIN_WORK(work));
    594 
    595 	struct __cds_wfcq_head diff_head;
    596 	struct cds_wfcq_tail diff_tail;
    597 
    598 	/* Initialize local wfcqueue */
    599 	__cds_wfcq_init(&diff_head, &diff_tail);
    600 
    601 	enum cds_wfcq_ret ret = __cds_wfcq_splice_blocking(
    602 		&diff_head, &diff_tail, &xfr->diff_head, &xfr->diff_tail);
    603 	INSIST(ret == CDS_WFCQ_RET_DEST_EMPTY);
    604 
    605 	struct cds_wfcq_node *node, *next;
    606 	__cds_wfcq_for_each_blocking_safe(&diff_head, &diff_tail, node, next) {
    607 		ixfr_apply_data_t *data =
    608 			caa_container_of(node, ixfr_apply_data_t, wfcq_node);
    609 
    610 		if (atomic_load(&xfr->shuttingdown)) {
    611 			result = ISC_R_SHUTTINGDOWN;
    612 		}
    613 
    614 		/* Apply only until first failure */
    615 		if (result == ISC_R_SUCCESS) {
    616 			/* This also checks for shuttingdown condition */
    617 			result = ixfr_apply_one(xfr, data);
    618 		}
    619 
    620 		/* We need to clear and free all data chunks */
    621 		dns_diff_clear(&data->diff);
    622 		isc_mem_put(xfr->mctx, data, sizeof(*data));
    623 	}
    624 
    625 	work->result = result;
    626 }
    627 
    628 static void
    629 ixfr_apply_done(void *arg) {
    630 	xfrin_work_t *work = arg;
    631 	REQUIRE(VALID_XFRIN_WORK(work));
    632 
    633 	dns_xfrin_t *xfr = work->xfr;
    634 	REQUIRE(VALID_XFRIN(xfr));
    635 
    636 	isc_result_t result = work->result;
    637 
    638 	if (atomic_load(&xfr->shuttingdown)) {
    639 		result = ISC_R_SHUTTINGDOWN;
    640 	}
    641 
    642 	if (result != ISC_R_SUCCESS) {
    643 		goto failure;
    644 	}
    645 
    646 	/* Reschedule */
    647 	if (!cds_wfcq_empty(&xfr->diff_head, &xfr->diff_tail)) {
    648 		isc_work_enqueue(xfr->loop, ixfr_apply, ixfr_apply_done, work);
    649 		return;
    650 	}
    651 
    652 failure:
    653 	xfr->diff_running = false;
    654 
    655 	isc_mem_put(xfr->mctx, work, sizeof(*work));
    656 
    657 	if (result == ISC_R_SUCCESS) {
    658 		dns_db_closeversion(xfr->db, &xfr->ver, true);
    659 		dns_zone_markdirty(xfr->zone);
    660 
    661 		if (atomic_load(&xfr->state) == XFRST_IXFR_END) {
    662 			xfrin_end(xfr, result);
    663 		}
    664 	} else {
    665 		dns_db_closeversion(xfr->db, &xfr->ver, false);
    666 
    667 		xfrin_fail(xfr, result, "failed while processing responses");
    668 	}
    669 
    670 	dns_xfrin_detach(&xfr);
    671 }
    672 
    673 /*
    674  * Apply a set of IXFR changes to the database.
    675  */
    676 static isc_result_t
    677 ixfr_commit(dns_xfrin_t *xfr) {
    678 	isc_result_t result = ISC_R_SUCCESS;
    679 	ixfr_apply_data_t *data = isc_mem_get(xfr->mctx, sizeof(*data));
    680 
    681 	*data = (ixfr_apply_data_t){ 0 };
    682 	cds_wfcq_node_init(&data->wfcq_node);
    683 
    684 	if (xfr->ver == NULL) {
    685 		CHECK(dns_db_newversion(xfr->db, &xfr->ver));
    686 	}
    687 
    688 	dns_diff_init(xfr->mctx, &data->diff);
    689 	/* FIXME: Should we add dns_diff_move() */
    690 	ISC_LIST_MOVE(data->diff.tuples, xfr->diff.tuples);
    691 
    692 	(void)cds_wfcq_enqueue(&xfr->diff_head, &xfr->diff_tail,
    693 			       &data->wfcq_node);
    694 
    695 	if (!xfr->diff_running) {
    696 		xfrin_work_t *work = isc_mem_get(xfr->mctx, sizeof(*work));
    697 		*work = (xfrin_work_t){
    698 			.magic = XFRIN_WORK_MAGIC,
    699 			.result = ISC_R_UNSET,
    700 			.xfr = dns_xfrin_ref(xfr),
    701 		};
    702 		xfr->diff_running = true;
    703 		isc_work_enqueue(xfr->loop, ixfr_apply, ixfr_apply_done, work);
    704 	}
    705 
    706 failure:
    707 	return result;
    708 }
    709 
    710 /**************************************************************************/
    711 /*
    712  * Common AXFR/IXFR protocol code
    713  */
    714 
    715 /*
    716  * Handle a single incoming resource record according to the current
    717  * state.
    718  */
    719 static isc_result_t
    720 xfr_rr(dns_xfrin_t *xfr, dns_name_t *name, uint32_t ttl, dns_rdata_t *rdata) {
    721 	isc_result_t result;
    722 	uint_fast32_t end_serial;
    723 
    724 	atomic_fetch_add_relaxed(&xfr->nrecs, 1);
    725 
    726 	if (rdata->type == dns_rdatatype_none ||
    727 	    dns_rdatatype_ismeta(rdata->type))
    728 	{
    729 		char buf[64];
    730 		dns_rdatatype_format(rdata->type, buf, sizeof(buf));
    731 		xfrin_log(xfr, ISC_LOG_NOTICE,
    732 			  "Unexpected %s record in zone transfer", buf);
    733 		result = DNS_R_FORMERR;
    734 		goto failure;
    735 	}
    736 
    737 	/*
    738 	 * Immediately reject the entire transfer if the RR that is currently
    739 	 * being processed is an SOA record that is not placed at the zone
    740 	 * apex.
    741 	 */
    742 	if (rdata->type == dns_rdatatype_soa &&
    743 	    !dns_name_equal(&xfr->name, name))
    744 	{
    745 		char namebuf[DNS_NAME_FORMATSIZE];
    746 		dns_name_format(name, namebuf, sizeof(namebuf));
    747 		xfrin_log(xfr, ISC_LOG_DEBUG(3), "SOA name mismatch: '%s'",
    748 			  namebuf);
    749 		result = DNS_R_NOTZONETOP;
    750 		goto failure;
    751 	}
    752 
    753 redo:
    754 	switch (atomic_load(&xfr->state)) {
    755 	case XFRST_SOAQUERY:
    756 		if (rdata->type != dns_rdatatype_soa) {
    757 			xfrin_log(xfr, ISC_LOG_NOTICE,
    758 				  "non-SOA response to SOA query");
    759 			result = DNS_R_FORMERR;
    760 			goto failure;
    761 		}
    762 		end_serial = dns_soa_getserial(rdata);
    763 		atomic_store_relaxed(&xfr->end_serial, end_serial);
    764 		if (!DNS_SERIAL_GT(end_serial, xfr->ixfr.request_serial) &&
    765 		    !dns_zone_isforced(xfr->zone))
    766 		{
    767 			xfrin_log(xfr, ISC_LOG_DEBUG(3),
    768 				  "requested serial %u, "
    769 				  "primary has %" PRIuFAST32 ", not updating",
    770 				  xfr->ixfr.request_serial, end_serial);
    771 			result = DNS_R_UPTODATE;
    772 			goto failure;
    773 		}
    774 		atomic_store(&xfr->state, XFRST_GOTSOA);
    775 		break;
    776 
    777 	case XFRST_GOTSOA:
    778 		/*
    779 		 * Skip other records in the answer section.
    780 		 */
    781 		break;
    782 
    783 	case XFRST_ZONEXFRREQUEST:
    784 		if (rdata->type != dns_rdatatype_soa) {
    785 			xfrin_log(xfr, ISC_LOG_NOTICE,
    786 				  "first RR in zone transfer must be SOA");
    787 			result = DNS_R_FORMERR;
    788 			goto failure;
    789 		}
    790 		/*
    791 		 * Remember the serial number in the initial SOA.
    792 		 * We need it to recognize the end of an IXFR.
    793 		 */
    794 		end_serial = dns_soa_getserial(rdata);
    795 		atomic_store_relaxed(&xfr->end_serial, end_serial);
    796 		if (xfr->reqtype == dns_rdatatype_ixfr &&
    797 		    !DNS_SERIAL_GT(end_serial, xfr->ixfr.request_serial) &&
    798 		    !dns_zone_isforced(xfr->zone))
    799 		{
    800 			/*
    801 			 * This must be the single SOA record that is
    802 			 * sent when the current version on the primary
    803 			 * is not newer than the version in the request.
    804 			 */
    805 			xfrin_log(xfr, ISC_LOG_DEBUG(3),
    806 				  "requested serial %u, "
    807 				  "primary has %" PRIuFAST32 ", not updating",
    808 				  xfr->ixfr.request_serial, end_serial);
    809 			result = DNS_R_UPTODATE;
    810 			goto failure;
    811 		}
    812 		xfr->firstsoa = *rdata;
    813 		if (xfr->firstsoa_data != NULL) {
    814 			isc_mem_free(xfr->mctx, xfr->firstsoa_data);
    815 		}
    816 		xfr->firstsoa_data = isc_mem_allocate(xfr->mctx, rdata->length);
    817 		memcpy(xfr->firstsoa_data, rdata->data, rdata->length);
    818 		xfr->firstsoa.data = xfr->firstsoa_data;
    819 		atomic_store(&xfr->state, XFRST_FIRSTDATA);
    820 		break;
    821 
    822 	case XFRST_FIRSTDATA:
    823 		/*
    824 		 * If the transfer begins with one SOA record, it is an AXFR,
    825 		 * if it begins with two SOAs, it is an IXFR.
    826 		 */
    827 		if (xfr->reqtype == dns_rdatatype_ixfr &&
    828 		    rdata->type == dns_rdatatype_soa &&
    829 		    xfr->ixfr.request_serial == dns_soa_getserial(rdata))
    830 		{
    831 			xfrin_log(xfr, ISC_LOG_DEBUG(3),
    832 				  "got incremental response");
    833 			CHECK(ixfr_init(xfr));
    834 			atomic_store(&xfr->state, XFRST_IXFR_DELSOA);
    835 		} else {
    836 			xfrin_log(xfr, ISC_LOG_DEBUG(3),
    837 				  "got nonincremental response");
    838 			CHECK(axfr_init(xfr));
    839 			atomic_store(&xfr->state, XFRST_AXFR);
    840 		}
    841 		goto redo;
    842 
    843 	case XFRST_IXFR_DELSOA:
    844 		INSIST(rdata->type == dns_rdatatype_soa);
    845 		CHECK(ixfr_putdata(xfr, DNS_DIFFOP_DEL, name, ttl, rdata));
    846 		atomic_store(&xfr->state, XFRST_IXFR_DEL);
    847 		break;
    848 
    849 	case XFRST_IXFR_DEL:
    850 		if (rdata->type == dns_rdatatype_soa) {
    851 			uint32_t soa_serial = dns_soa_getserial(rdata);
    852 			atomic_store(&xfr->state, XFRST_IXFR_ADDSOA);
    853 			xfr->ixfr.current_serial = soa_serial;
    854 			goto redo;
    855 		}
    856 		CHECK(ixfr_putdata(xfr, DNS_DIFFOP_DEL, name, ttl, rdata));
    857 		break;
    858 
    859 	case XFRST_IXFR_ADDSOA:
    860 		INSIST(rdata->type == dns_rdatatype_soa);
    861 		CHECK(ixfr_putdata(xfr, DNS_DIFFOP_ADD, name, ttl, rdata));
    862 		atomic_store(&xfr->state, XFRST_IXFR_ADD);
    863 		break;
    864 
    865 	case XFRST_IXFR_ADD:
    866 		if (rdata->type == dns_rdatatype_soa) {
    867 			uint32_t soa_serial = dns_soa_getserial(rdata);
    868 			if (soa_serial == atomic_load_relaxed(&xfr->end_serial))
    869 			{
    870 				CHECK(ixfr_commit(xfr));
    871 				atomic_store(&xfr->state, XFRST_IXFR_END);
    872 				break;
    873 			} else if (soa_serial != xfr->ixfr.current_serial) {
    874 				xfrin_log(xfr, ISC_LOG_NOTICE,
    875 					  "IXFR out of sync: "
    876 					  "expected serial %u, got %u",
    877 					  xfr->ixfr.current_serial, soa_serial);
    878 				result = DNS_R_FORMERR;
    879 				goto failure;
    880 			} else {
    881 				CHECK(ixfr_commit(xfr));
    882 				atomic_store(&xfr->state, XFRST_IXFR_DELSOA);
    883 				goto redo;
    884 			}
    885 		}
    886 		if (rdata->type == dns_rdatatype_ns &&
    887 		    dns_name_iswildcard(name))
    888 		{
    889 			result = DNS_R_INVALIDNS;
    890 			goto failure;
    891 		}
    892 		CHECK(ixfr_putdata(xfr, DNS_DIFFOP_ADD, name, ttl, rdata));
    893 		break;
    894 
    895 	case XFRST_AXFR:
    896 		/*
    897 		 * Old BINDs sent cross class A records for non IN classes.
    898 		 */
    899 		if (rdata->type == dns_rdatatype_a &&
    900 		    rdata->rdclass != xfr->rdclass &&
    901 		    xfr->rdclass != dns_rdataclass_in)
    902 		{
    903 			break;
    904 		}
    905 		CHECK(axfr_putdata(xfr, DNS_DIFFOP_ADD, name, ttl, rdata));
    906 		if (rdata->type == dns_rdatatype_soa) {
    907 			/*
    908 			 * Use dns_rdata_compare instead of memcmp to
    909 			 * allow for case differences.
    910 			 */
    911 			if (dns_rdata_compare(rdata, &xfr->firstsoa) != 0) {
    912 				xfrin_log(xfr, ISC_LOG_NOTICE,
    913 					  "start and ending SOA records "
    914 					  "mismatch");
    915 				result = DNS_R_FORMERR;
    916 				goto failure;
    917 			}
    918 			axfr_commit(xfr);
    919 			atomic_store(&xfr->state, XFRST_AXFR_END);
    920 			break;
    921 		}
    922 		break;
    923 	case XFRST_AXFR_END:
    924 	case XFRST_IXFR_END:
    925 		result = DNS_R_EXTRADATA;
    926 		goto failure;
    927 	default:
    928 		UNREACHABLE();
    929 	}
    930 	result = ISC_R_SUCCESS;
    931 failure:
    932 	return result;
    933 }
    934 
    935 void
    936 dns_xfrin_create(dns_zone_t *zone, dns_rdatatype_t xfrtype,
    937 		 const isc_sockaddr_t *primaryaddr,
    938 		 const isc_sockaddr_t *sourceaddr, dns_tsigkey_t *tsigkey,
    939 		 dns_transport_type_t soa_transport_type,
    940 		 dns_transport_t *transport, isc_tlsctx_cache_t *tlsctx_cache,
    941 		 isc_mem_t *mctx, dns_xfrin_t **xfrp) {
    942 	dns_name_t *zonename = dns_zone_getorigin(zone);
    943 	dns_xfrin_t *xfr = NULL;
    944 	dns_db_t *db = NULL;
    945 	isc_loop_t *loop = NULL;
    946 
    947 	REQUIRE(xfrp != NULL && *xfrp == NULL);
    948 	REQUIRE(isc_sockaddr_getport(primaryaddr) != 0);
    949 	REQUIRE(zone != NULL);
    950 	REQUIRE(dns_zone_getview(zone) != NULL);
    951 
    952 	loop = dns_zone_getloop(zone);
    953 
    954 	(void)dns_zone_getdb(zone, &db);
    955 
    956 	if (xfrtype == dns_rdatatype_soa || xfrtype == dns_rdatatype_ixfr) {
    957 		REQUIRE(db != NULL);
    958 	}
    959 
    960 	xfrin_create(mctx, zone, db, loop, zonename, dns_zone_getclass(zone),
    961 		     xfrtype, primaryaddr, sourceaddr, tsigkey,
    962 		     soa_transport_type, transport, tlsctx_cache, &xfr);
    963 
    964 	if (db != NULL) {
    965 		xfr->zone_had_db = true;
    966 		dns_db_detach(&db);
    967 	}
    968 
    969 	*xfrp = xfr;
    970 }
    971 
    972 isc_result_t
    973 dns_xfrin_start(dns_xfrin_t *xfr, dns_xfrindone_t done) {
    974 	isc_result_t result;
    975 
    976 	REQUIRE(xfr != NULL);
    977 	REQUIRE(xfr->zone != NULL);
    978 	REQUIRE(done != NULL);
    979 
    980 	xfr->done = done;
    981 
    982 	result = xfrin_start(xfr);
    983 	if (result != ISC_R_SUCCESS) {
    984 		xfr->done = NULL;
    985 		xfrin_fail(xfr, result, "zone transfer start failed");
    986 	}
    987 
    988 	return result;
    989 }
    990 
    991 static void
    992 xfrin_timedout(void *xfr) {
    993 	REQUIRE(VALID_XFRIN(xfr));
    994 
    995 	xfrin_fail(xfr, ISC_R_TIMEDOUT, "maximum transfer time exceeded");
    996 }
    997 
    998 static void
    999 xfrin_idledout(void *xfr) {
   1000 	REQUIRE(VALID_XFRIN(xfr));
   1001 
   1002 	xfrin_fail(xfr, ISC_R_TIMEDOUT, "maximum idle time exceeded");
   1003 }
   1004 
   1005 static void
   1006 xfrin_minratecheck(void *arg) {
   1007 	dns_xfrin_t *xfr = arg;
   1008 
   1009 	REQUIRE(VALID_XFRIN(xfr));
   1010 
   1011 	const uint64_t nbytes = ISC_XFRIN_LOAD(&xfr->nbytes, uint64_t);
   1012 	const uint64_t min = dns_zone_getminxfrratebytesin(xfr->zone);
   1013 	uint64_t rate = nbytes - xfr->nbytes_saved;
   1014 
   1015 	if (rate < min) {
   1016 		isc_timer_stop(xfr->min_rate_timer);
   1017 		xfrin_fail(xfr, ISC_R_TIMEDOUT,
   1018 			   "minimum transfer rate reached");
   1019 	} else {
   1020 		xfr->nbytes_saved = nbytes;
   1021 
   1022 		/*
   1023 		 * Calculate and store for the statistics channel the transfer
   1024 		 * rate in bytes-per-second for the latest interval.
   1025 		 */
   1026 		rate /= dns_zone_getminxfrratesecondsin(xfr->zone);
   1027 		ISC_XFRIN_STORE(&xfr->rate_bytes_per_second, rate);
   1028 	}
   1029 }
   1030 
   1031 isc_time_t
   1032 dns_xfrin_getstarttime(dns_xfrin_t *xfr) {
   1033 	REQUIRE(VALID_XFRIN(xfr));
   1034 
   1035 	return ISC_XFRIN_LOAD(&xfr->start, isc_time_t);
   1036 }
   1037 
   1038 void
   1039 dns_xfrin_getstate(const dns_xfrin_t *xfr, const char **statestr,
   1040 		   bool *is_first_data_received, bool *is_ixfr) {
   1041 	xfrin_state_t state;
   1042 
   1043 	REQUIRE(VALID_XFRIN(xfr));
   1044 	REQUIRE(statestr != NULL && *statestr == NULL);
   1045 	REQUIRE(is_ixfr != NULL);
   1046 
   1047 	state = atomic_load(&xfr->state);
   1048 	*statestr = "";
   1049 	*is_first_data_received = (state > XFRST_FIRSTDATA);
   1050 	*is_ixfr = atomic_load(&xfr->is_ixfr);
   1051 
   1052 	switch (state) {
   1053 	case XFRST_SOAQUERY:
   1054 		*statestr = "SOA Query";
   1055 		break;
   1056 	case XFRST_GOTSOA:
   1057 		*statestr = "Got SOA";
   1058 		break;
   1059 	case XFRST_ZONEXFRREQUEST:
   1060 		*statestr = "Zone Transfer Request";
   1061 		break;
   1062 	case XFRST_FIRSTDATA:
   1063 		*statestr = "First Data";
   1064 		break;
   1065 	case XFRST_IXFR_DELSOA:
   1066 	case XFRST_IXFR_DEL:
   1067 	case XFRST_IXFR_ADDSOA:
   1068 	case XFRST_IXFR_ADD:
   1069 		*statestr = "Receiving IXFR Data";
   1070 		break;
   1071 	case XFRST_IXFR_END:
   1072 		*statestr = "Finalizing IXFR";
   1073 		break;
   1074 	case XFRST_AXFR:
   1075 		*statestr = "Receiving AXFR Data";
   1076 		break;
   1077 	case XFRST_AXFR_END:
   1078 		*statestr = "Finalizing AXFR";
   1079 		break;
   1080 	}
   1081 }
   1082 
   1083 uint32_t
   1084 dns_xfrin_getendserial(dns_xfrin_t *xfr) {
   1085 	REQUIRE(VALID_XFRIN(xfr));
   1086 
   1087 	return atomic_load_relaxed(&xfr->end_serial);
   1088 }
   1089 
   1090 void
   1091 dns_xfrin_getstats(dns_xfrin_t *xfr, unsigned int *nmsgp, unsigned int *nrecsp,
   1092 		   uint64_t *nbytesp, uint64_t *ratep) {
   1093 	REQUIRE(VALID_XFRIN(xfr));
   1094 	REQUIRE(nmsgp != NULL && nrecsp != NULL && nbytesp != NULL);
   1095 
   1096 	uint64_t rate = ISC_XFRIN_LOAD(&xfr->rate_bytes_per_second, uint64_t);
   1097 	if (rate == 0) {
   1098 		/*
   1099 		 * Likely the first 'min-transfer-rate-in <bytes> <minutes>'
   1100 		 * minutes interval hasn't passed yet. Calculate the overall
   1101 		 * average transfer rate instead.
   1102 		 */
   1103 		isc_time_t now = isc_time_now();
   1104 		isc_time_t start = ISC_XFRIN_LOAD(&xfr->start, isc_time_t);
   1105 		uint64_t sec = isc_time_microdiff(&now, &start) / US_PER_SEC;
   1106 		if (sec > 0) {
   1107 			rate = ISC_XFRIN_LOAD(&xfr->nbytes, uint64_t) / sec;
   1108 		}
   1109 	}
   1110 
   1111 	SET_IF_NOT_NULL(nmsgp, atomic_load_relaxed(&xfr->nmsg));
   1112 	SET_IF_NOT_NULL(nrecsp, atomic_load_relaxed(&xfr->nrecs));
   1113 	SET_IF_NOT_NULL(nbytesp, ISC_XFRIN_LOAD(&xfr->nbytes, uint64_t));
   1114 	SET_IF_NOT_NULL(ratep, rate);
   1115 }
   1116 
   1117 const isc_sockaddr_t *
   1118 dns_xfrin_getsourceaddr(const dns_xfrin_t *xfr) {
   1119 	REQUIRE(VALID_XFRIN(xfr));
   1120 
   1121 	return &xfr->sourceaddr;
   1122 }
   1123 
   1124 const isc_sockaddr_t *
   1125 dns_xfrin_getprimaryaddr(const dns_xfrin_t *xfr) {
   1126 	REQUIRE(VALID_XFRIN(xfr));
   1127 
   1128 	return &xfr->primaryaddr;
   1129 }
   1130 
   1131 dns_transport_type_t
   1132 dns_xfrin_gettransporttype(const dns_xfrin_t *xfr) {
   1133 	REQUIRE(VALID_XFRIN(xfr));
   1134 
   1135 	if (xfr->transport != NULL) {
   1136 		return dns_transport_get_type(xfr->transport);
   1137 	}
   1138 
   1139 	return DNS_TRANSPORT_TCP;
   1140 }
   1141 
   1142 dns_transport_type_t
   1143 dns_xfrin_getsoatransporttype(dns_xfrin_t *xfr) {
   1144 	REQUIRE(VALID_XFRIN(xfr));
   1145 
   1146 	return atomic_load_relaxed(&xfr->soa_transport_type);
   1147 }
   1148 
   1149 const dns_name_t *
   1150 dns_xfrin_gettsigkeyname(const dns_xfrin_t *xfr) {
   1151 	REQUIRE(VALID_XFRIN(xfr));
   1152 
   1153 	if (xfr->tsigkey == NULL || xfr->tsigkey->key == NULL) {
   1154 		return NULL;
   1155 	}
   1156 
   1157 	return dst_key_name(xfr->tsigkey->key);
   1158 }
   1159 
   1160 static void
   1161 xfrin_shutdown(void *arg) {
   1162 	dns_xfrin_t *xfr = arg;
   1163 
   1164 	REQUIRE(VALID_XFRIN(xfr));
   1165 
   1166 	xfrin_fail(xfr, ISC_R_SHUTTINGDOWN, "shut down");
   1167 	dns_xfrin_detach(&xfr);
   1168 }
   1169 
   1170 void
   1171 dns_xfrin_shutdown(dns_xfrin_t *xfr) {
   1172 	REQUIRE(VALID_XFRIN(xfr));
   1173 
   1174 	if (xfr->loop != isc_loop()) {
   1175 		dns_xfrin_ref(xfr);
   1176 		isc_async_run(xfr->loop, xfrin_shutdown, xfr);
   1177 	} else {
   1178 		xfrin_fail(xfr, ISC_R_SHUTTINGDOWN, "shut down");
   1179 	}
   1180 }
   1181 
   1182 #if DNS_XFRIN_TRACE
   1183 ISC_REFCOUNT_TRACE_IMPL(dns_xfrin, xfrin_destroy);
   1184 #else
   1185 ISC_REFCOUNT_IMPL(dns_xfrin, xfrin_destroy);
   1186 #endif
   1187 
   1188 static void
   1189 xfrin_cancelio(dns_xfrin_t *xfr) {
   1190 	if (xfr->dispentry != NULL) {
   1191 		dns_dispatch_done(&xfr->dispentry);
   1192 	}
   1193 	if (xfr->disp != NULL) {
   1194 		dns_dispatch_detach(&xfr->disp);
   1195 	}
   1196 }
   1197 
   1198 static void
   1199 xfrin_reset(dns_xfrin_t *xfr) {
   1200 	REQUIRE(VALID_XFRIN(xfr));
   1201 
   1202 	xfrin_log(xfr, ISC_LOG_INFO, "resetting");
   1203 
   1204 	if (xfr->lasttsig != NULL) {
   1205 		isc_buffer_free(&xfr->lasttsig);
   1206 	}
   1207 
   1208 	dns_diff_clear(&xfr->diff);
   1209 
   1210 	if (xfr->ixfr.journal != NULL) {
   1211 		dns_journal_destroy(&xfr->ixfr.journal);
   1212 	}
   1213 
   1214 	if (xfr->axfr.add_private != NULL) {
   1215 		(void)dns_db_endload(xfr->db, &xfr->axfr);
   1216 	}
   1217 
   1218 	if (xfr->ver != NULL) {
   1219 		dns_db_closeversion(xfr->db, &xfr->ver, false);
   1220 	}
   1221 }
   1222 
   1223 static void
   1224 xfrin_fail(dns_xfrin_t *xfr, isc_result_t result, const char *msg) {
   1225 	REQUIRE(VALID_XFRIN(xfr));
   1226 
   1227 	dns_xfrin_ref(xfr);
   1228 
   1229 	/* Make sure only the first xfrin_fail() trumps */
   1230 	if (atomic_compare_exchange_strong(&xfr->shuttingdown, &(bool){ false },
   1231 					   true))
   1232 	{
   1233 		if (result != DNS_R_UPTODATE) {
   1234 			xfrin_log(xfr, ISC_LOG_ERROR, "%s: %s", msg,
   1235 				  isc_result_totext(result));
   1236 			if (atomic_load(&xfr->is_ixfr) &&
   1237 			    result != ISC_R_CANCELED &&
   1238 			    result != ISC_R_SHUTTINGDOWN)
   1239 			{
   1240 				/*
   1241 				 * Pass special result code to force AXFR retry
   1242 				 */
   1243 				result = DNS_R_BADIXFR;
   1244 			}
   1245 		}
   1246 
   1247 		xfrin_cancelio(xfr);
   1248 
   1249 		xfrin_end(xfr, result);
   1250 	}
   1251 
   1252 	dns_xfrin_detach(&xfr);
   1253 }
   1254 
   1255 static void
   1256 xfrin_create(isc_mem_t *mctx, dns_zone_t *zone, dns_db_t *db, isc_loop_t *loop,
   1257 	     dns_name_t *zonename, dns_rdataclass_t rdclass,
   1258 	     dns_rdatatype_t reqtype, const isc_sockaddr_t *primaryaddr,
   1259 	     const isc_sockaddr_t *sourceaddr, dns_tsigkey_t *tsigkey,
   1260 	     dns_transport_type_t soa_transport_type,
   1261 	     dns_transport_t *transport, isc_tlsctx_cache_t *tlsctx_cache,
   1262 	     dns_xfrin_t **xfrp) {
   1263 	dns_xfrin_t *xfr = NULL;
   1264 
   1265 	xfr = isc_mem_get(mctx, sizeof(*xfr));
   1266 	*xfr = (dns_xfrin_t){
   1267 		.shutdown_result = ISC_R_UNSET,
   1268 		.rdclass = rdclass,
   1269 		.reqtype = reqtype,
   1270 		.maxrecords = dns_zone_getmaxrecords(zone),
   1271 		.primaryaddr = *primaryaddr,
   1272 		.sourceaddr = *sourceaddr,
   1273 		.soa_transport_type = soa_transport_type,
   1274 		.firstsoa = DNS_RDATA_INIT,
   1275 		.edns = true,
   1276 		.references = 1,
   1277 		.magic = XFRIN_MAGIC,
   1278 	};
   1279 
   1280 	isc_loop_attach(loop, &xfr->loop);
   1281 	isc_mem_attach(mctx, &xfr->mctx);
   1282 	dns_zone_iattach(zone, &xfr->zone);
   1283 	dns_view_weakattach(dns_zone_getview(zone), &xfr->view);
   1284 	dns_name_init(&xfr->name, NULL);
   1285 
   1286 	__cds_wfcq_init(&xfr->diff_head, &xfr->diff_tail);
   1287 
   1288 	atomic_init(&xfr->is_ixfr, false);
   1289 
   1290 	if (db != NULL) {
   1291 		dns_db_attach(db, &xfr->db);
   1292 	}
   1293 
   1294 	dns_diff_init(xfr->mctx, &xfr->diff);
   1295 
   1296 	if (reqtype == dns_rdatatype_soa) {
   1297 		atomic_init(&xfr->state, XFRST_SOAQUERY);
   1298 	} else {
   1299 		atomic_init(&xfr->state, XFRST_ZONEXFRREQUEST);
   1300 	}
   1301 
   1302 	ISC_XFRIN_STORE(&xfr->start, isc_time_now());
   1303 
   1304 	if (tsigkey != NULL) {
   1305 		dns_tsigkey_attach(tsigkey, &xfr->tsigkey);
   1306 	}
   1307 
   1308 	if (transport != NULL) {
   1309 		dns_transport_attach(transport, &xfr->transport);
   1310 	}
   1311 
   1312 	dns_name_dup(zonename, mctx, &xfr->name);
   1313 
   1314 	INSIST(isc_sockaddr_pf(primaryaddr) == isc_sockaddr_pf(sourceaddr));
   1315 	isc_sockaddr_setport(&xfr->sourceaddr, 0);
   1316 
   1317 	/*
   1318 	 * Reserve 2 bytes for TCP length at the beginning of the buffer.
   1319 	 */
   1320 	isc_buffer_init(&xfr->qbuffer, &xfr->qbuffer_data[2],
   1321 			sizeof(xfr->qbuffer_data) - 2);
   1322 
   1323 	isc_tlsctx_cache_attach(tlsctx_cache, &xfr->tlsctx_cache);
   1324 
   1325 	dns_zone_name(xfr->zone, xfr->info, sizeof(xfr->info));
   1326 
   1327 	*xfrp = xfr;
   1328 }
   1329 
   1330 static isc_result_t
   1331 xfrin_start(dns_xfrin_t *xfr) {
   1332 	isc_result_t result = ISC_R_FAILURE;
   1333 	isc_interval_t interval;
   1334 
   1335 	dns_xfrin_ref(xfr);
   1336 
   1337 	/* If this is a retry, we need to cancel the previous dispentry */
   1338 	xfrin_cancelio(xfr);
   1339 
   1340 	dns_dispatchmgr_t *dispmgr = dns_view_getdispatchmgr(xfr->view);
   1341 	if (dispmgr == NULL) {
   1342 		result = ISC_R_SHUTTINGDOWN;
   1343 		goto failure;
   1344 	} else {
   1345 		result = dns_dispatch_createtcp(
   1346 			dispmgr, &xfr->sourceaddr, &xfr->primaryaddr,
   1347 			xfr->transport, DNS_DISPATCHOPT_UNSHARED, &xfr->disp);
   1348 		dns_dispatchmgr_detach(&dispmgr);
   1349 		if (result != ISC_R_SUCCESS) {
   1350 			goto failure;
   1351 		}
   1352 	}
   1353 
   1354 	LIBDNS_XFRIN_START(xfr, xfr->info);
   1355 
   1356 	/*
   1357 	 * If the transfer is started when the 'state' is XFRST_SOAQUERY, it
   1358 	 * means the SOA query will be performed by xfrin. A transfer could also
   1359 	 * be initiated starting from the XFRST_ZONEXFRREQUEST state, which
   1360 	 * means that the SOA query was already performed by other means (e.g.
   1361 	 * by zone.c:soa_query()), or that it's a transfer without a preceding
   1362 	 * SOA request, and 'soa_transport_type' is already correctly
   1363 	 * set by the creator of the xfrin.
   1364 	 */
   1365 	if (atomic_load(&xfr->state) == XFRST_SOAQUERY) {
   1366 		/*
   1367 		 * The "SOA before" mode is used, where the SOA request is
   1368 		 * using the same transport as the XFR.
   1369 		 */
   1370 		atomic_store_relaxed(&xfr->soa_transport_type,
   1371 				     dns_xfrin_gettransporttype(xfr));
   1372 	}
   1373 
   1374 	CHECK(dns_dispatch_add(
   1375 		xfr->disp, xfr->loop, 0, 0, &xfr->primaryaddr, xfr->transport,
   1376 		xfr->tlsctx_cache, xfrin_connect_done, xfrin_send_done,
   1377 		xfrin_recv_done, xfr, &xfr->id, &xfr->dispentry));
   1378 
   1379 	/* Set the maximum timer */
   1380 	if (xfr->max_time_timer == NULL) {
   1381 		isc_timer_create(dns_zone_getloop(xfr->zone), xfrin_timedout,
   1382 				 xfr, &xfr->max_time_timer);
   1383 	}
   1384 	isc_interval_set(&interval, dns_zone_getmaxxfrin(xfr->zone), 0);
   1385 	isc_timer_start(xfr->max_time_timer, isc_timertype_once, &interval);
   1386 
   1387 	/* Set the idle timer */
   1388 	if (xfr->max_idle_timer == NULL) {
   1389 		isc_timer_create(dns_zone_getloop(xfr->zone), xfrin_idledout,
   1390 				 xfr, &xfr->max_idle_timer);
   1391 	}
   1392 	isc_interval_set(&interval, dns_zone_getidlein(xfr->zone), 0);
   1393 	isc_timer_start(xfr->max_idle_timer, isc_timertype_once, &interval);
   1394 
   1395 	/* Set the minimum transfer rate checking timer */
   1396 	if (xfr->min_rate_timer == NULL) {
   1397 		isc_timer_create(dns_zone_getloop(xfr->zone),
   1398 				 xfrin_minratecheck, xfr, &xfr->min_rate_timer);
   1399 	}
   1400 	isc_interval_set(&interval, dns_zone_getminxfrratesecondsin(xfr->zone),
   1401 			 0);
   1402 	isc_timer_start(xfr->min_rate_timer, isc_timertype_ticker, &interval);
   1403 
   1404 	/*
   1405 	 * The connect has to be the last thing that is called before returning,
   1406 	 * as it can end synchronously and destroy the xfr object.
   1407 	 */
   1408 	CHECK(dns_dispatch_connect(xfr->dispentry));
   1409 
   1410 	return ISC_R_SUCCESS;
   1411 
   1412 failure:
   1413 	xfrin_cancelio(xfr);
   1414 	dns_xfrin_detach(&xfr);
   1415 
   1416 	return result;
   1417 }
   1418 
   1419 /* XXX the resolver could use this, too */
   1420 
   1421 static isc_result_t
   1422 render(dns_message_t *msg, isc_mem_t *mctx, isc_buffer_t *buf) {
   1423 	dns_compress_t cctx;
   1424 	isc_result_t result;
   1425 
   1426 	dns_compress_init(&cctx, mctx, 0);
   1427 	CHECK(dns_message_renderbegin(msg, &cctx, buf));
   1428 	CHECK(dns_message_rendersection(msg, DNS_SECTION_QUESTION, 0));
   1429 	CHECK(dns_message_rendersection(msg, DNS_SECTION_ANSWER, 0));
   1430 	CHECK(dns_message_rendersection(msg, DNS_SECTION_AUTHORITY, 0));
   1431 	CHECK(dns_message_rendersection(msg, DNS_SECTION_ADDITIONAL, 0));
   1432 	CHECK(dns_message_renderend(msg));
   1433 	result = ISC_R_SUCCESS;
   1434 failure:
   1435 	dns_compress_invalidate(&cctx);
   1436 	return result;
   1437 }
   1438 
   1439 /*
   1440  * A connection has been established.
   1441  */
   1442 static void
   1443 xfrin_connect_done(isc_result_t result, isc_region_t *region ISC_ATTR_UNUSED,
   1444 		   void *arg) {
   1445 	dns_xfrin_t *xfr = (dns_xfrin_t *)arg;
   1446 	char addrtext[ISC_SOCKADDR_FORMATSIZE];
   1447 	char signerbuf[DNS_NAME_FORMATSIZE];
   1448 	const char *signer = "", *sep = "";
   1449 	dns_zonemgr_t *zmgr = NULL;
   1450 
   1451 	REQUIRE(VALID_XFRIN(xfr));
   1452 
   1453 	if (atomic_load(&xfr->shuttingdown)) {
   1454 		result = ISC_R_SHUTTINGDOWN;
   1455 	}
   1456 
   1457 	LIBDNS_XFRIN_CONNECTED(xfr, xfr->info, result);
   1458 
   1459 	if (result != ISC_R_SUCCESS) {
   1460 		xfrin_fail(xfr, result, "failed to connect");
   1461 		goto failure;
   1462 	}
   1463 
   1464 	result = dns_dispatch_checkperm(xfr->disp);
   1465 	if (result != ISC_R_SUCCESS) {
   1466 		xfrin_fail(xfr, result, "connected but unable to transfer");
   1467 		goto failure;
   1468 	}
   1469 
   1470 	zmgr = dns_zone_getmgr(xfr->zone);
   1471 	if (zmgr != NULL) {
   1472 		dns_zonemgr_unreachabledel(zmgr, &xfr->primaryaddr,
   1473 					   &xfr->sourceaddr);
   1474 	}
   1475 
   1476 	if (xfr->tsigkey != NULL && xfr->tsigkey->key != NULL) {
   1477 		dns_name_format(dst_key_name(xfr->tsigkey->key), signerbuf,
   1478 				sizeof(signerbuf));
   1479 		sep = " TSIG ";
   1480 		signer = signerbuf;
   1481 	}
   1482 
   1483 	isc_sockaddr_format(&xfr->primaryaddr, addrtext, sizeof(addrtext));
   1484 	xfrin_log(xfr, ISC_LOG_INFO, "connected using %s%s%s", addrtext, sep,
   1485 		  signer);
   1486 
   1487 	result = xfrin_send_request(xfr);
   1488 	if (result != ISC_R_SUCCESS) {
   1489 		xfrin_fail(xfr, result, "connected but unable to send");
   1490 		goto detach;
   1491 	}
   1492 
   1493 	return;
   1494 
   1495 failure:
   1496 	switch (result) {
   1497 	case ISC_R_NETDOWN:
   1498 	case ISC_R_HOSTDOWN:
   1499 	case ISC_R_NETUNREACH:
   1500 	case ISC_R_HOSTUNREACH:
   1501 	case ISC_R_CONNREFUSED:
   1502 	case ISC_R_TIMEDOUT:
   1503 		/*
   1504 		 * Add the server to unreachable primaries table if
   1505 		 * the server has a permanent networking error or
   1506 		 * the connection attempt as timed out.
   1507 		 */
   1508 		zmgr = dns_zone_getmgr(xfr->zone);
   1509 		if (zmgr != NULL) {
   1510 			isc_time_t now = isc_time_now();
   1511 
   1512 			dns_zonemgr_unreachableadd(zmgr, &xfr->primaryaddr,
   1513 						   &xfr->sourceaddr, &now);
   1514 		}
   1515 		break;
   1516 	default:
   1517 		/* Retry sooner than in 10 minutes */
   1518 		break;
   1519 	}
   1520 
   1521 detach:
   1522 	dns_xfrin_detach(&xfr);
   1523 }
   1524 
   1525 /*
   1526  * Convert a tuple into a dns_name_t suitable for inserting
   1527  * into the given dns_message_t.
   1528  */
   1529 static void
   1530 tuple2msgname(dns_difftuple_t *tuple, dns_message_t *msg, dns_name_t **target) {
   1531 	dns_rdata_t *rdata = NULL;
   1532 	dns_rdatalist_t *rdl = NULL;
   1533 	dns_rdataset_t *rds = NULL;
   1534 	dns_name_t *name = NULL;
   1535 
   1536 	REQUIRE(target != NULL && *target == NULL);
   1537 
   1538 	dns_message_gettemprdata(msg, &rdata);
   1539 	dns_rdata_init(rdata);
   1540 	dns_rdata_clone(&tuple->rdata, rdata);
   1541 
   1542 	dns_message_gettemprdatalist(msg, &rdl);
   1543 	dns_rdatalist_init(rdl);
   1544 	rdl->type = tuple->rdata.type;
   1545 	rdl->rdclass = tuple->rdata.rdclass;
   1546 	rdl->ttl = tuple->ttl;
   1547 	ISC_LIST_APPEND(rdl->rdata, rdata, link);
   1548 
   1549 	dns_message_gettemprdataset(msg, &rds);
   1550 	dns_rdatalist_tordataset(rdl, rds);
   1551 
   1552 	dns_message_gettempname(msg, &name);
   1553 	dns_name_clone(&tuple->name, name);
   1554 	ISC_LIST_APPEND(name->list, rds, link);
   1555 
   1556 	*target = name;
   1557 }
   1558 
   1559 static const char *
   1560 request_type(dns_xfrin_t *xfr) {
   1561 	switch (xfr->reqtype) {
   1562 	case dns_rdatatype_soa:
   1563 		return "SOA";
   1564 	case dns_rdatatype_axfr:
   1565 		return "AXFR";
   1566 	case dns_rdatatype_ixfr:
   1567 		return "IXFR";
   1568 	default:
   1569 		ISC_UNREACHABLE();
   1570 	}
   1571 }
   1572 
   1573 static isc_result_t
   1574 add_opt(dns_message_t *message, uint16_t udpsize, bool reqnsid,
   1575 	bool reqexpire) {
   1576 	isc_result_t result;
   1577 	dns_rdataset_t *rdataset = NULL;
   1578 	dns_ednsopt_t ednsopts[DNS_EDNSOPTIONS];
   1579 	int count = 0;
   1580 
   1581 	/* Set EDNS options if applicable. */
   1582 	if (reqnsid) {
   1583 		INSIST(count < DNS_EDNSOPTIONS);
   1584 		ednsopts[count].code = DNS_OPT_NSID;
   1585 		ednsopts[count].length = 0;
   1586 		ednsopts[count].value = NULL;
   1587 		count++;
   1588 	}
   1589 	if (reqexpire) {
   1590 		INSIST(count < DNS_EDNSOPTIONS);
   1591 		ednsopts[count].code = DNS_OPT_EXPIRE;
   1592 		ednsopts[count].length = 0;
   1593 		ednsopts[count].value = NULL;
   1594 		count++;
   1595 	}
   1596 	result = dns_message_buildopt(message, &rdataset, 0, udpsize, 0,
   1597 				      ednsopts, count);
   1598 	if (result != ISC_R_SUCCESS) {
   1599 		return result;
   1600 	}
   1601 
   1602 	return dns_message_setopt(message, rdataset);
   1603 }
   1604 
   1605 /*
   1606  * Build an *XFR request and send its length prefix.
   1607  */
   1608 static isc_result_t
   1609 xfrin_send_request(dns_xfrin_t *xfr) {
   1610 	isc_result_t result;
   1611 	isc_region_t region;
   1612 	dns_rdataset_t *qrdataset = NULL;
   1613 	dns_message_t *msg = NULL;
   1614 	dns_difftuple_t *soatuple = NULL;
   1615 	dns_name_t *qname = NULL;
   1616 	dns_dbversion_t *ver = NULL;
   1617 	dns_name_t *msgsoaname = NULL;
   1618 	bool edns = xfr->edns;
   1619 	bool reqnsid = xfr->view->requestnsid;
   1620 	bool reqexpire = dns_zone_getrequestexpire(xfr->zone);
   1621 	uint16_t udpsize = dns_view_getudpsize(xfr->view);
   1622 
   1623 	LIBDNS_XFRIN_RECV_SEND_REQUEST(xfr, xfr->info);
   1624 
   1625 	/* Create the request message */
   1626 	dns_message_create(xfr->mctx, NULL, NULL, DNS_MESSAGE_INTENTRENDER,
   1627 			   &msg);
   1628 	CHECK(dns_message_settsigkey(msg, xfr->tsigkey));
   1629 
   1630 	/* Create a name for the question section. */
   1631 	dns_message_gettempname(msg, &qname);
   1632 	dns_name_clone(&xfr->name, qname);
   1633 
   1634 	/* Formulate the question and attach it to the question name. */
   1635 	dns_message_gettemprdataset(msg, &qrdataset);
   1636 	dns_rdataset_makequestion(qrdataset, xfr->rdclass, xfr->reqtype);
   1637 	ISC_LIST_APPEND(qname->list, qrdataset, link);
   1638 	qrdataset = NULL;
   1639 
   1640 	dns_message_addname(msg, qname, DNS_SECTION_QUESTION);
   1641 	qname = NULL;
   1642 
   1643 	if (xfr->reqtype == dns_rdatatype_ixfr) {
   1644 		/* Get the SOA and add it to the authority section. */
   1645 		dns_db_currentversion(xfr->db, &ver);
   1646 		CHECK(dns_db_createsoatuple(xfr->db, ver, xfr->mctx,
   1647 					    DNS_DIFFOP_EXISTS, &soatuple));
   1648 		xfr->ixfr.request_serial = dns_soa_getserial(&soatuple->rdata);
   1649 		xfr->ixfr.current_serial = xfr->ixfr.request_serial;
   1650 		xfrin_log(xfr, ISC_LOG_DEBUG(3),
   1651 			  "requesting IXFR for serial %u",
   1652 			  xfr->ixfr.request_serial);
   1653 
   1654 		tuple2msgname(soatuple, msg, &msgsoaname);
   1655 		dns_message_addname(msg, msgsoaname, DNS_SECTION_AUTHORITY);
   1656 	} else if (xfr->reqtype == dns_rdatatype_soa) {
   1657 		CHECK(dns_db_getsoaserial(xfr->db, NULL,
   1658 					  &xfr->ixfr.request_serial));
   1659 	}
   1660 
   1661 	if (edns && xfr->view->peers != NULL) {
   1662 		dns_peer_t *peer = NULL;
   1663 		isc_netaddr_t primaryip;
   1664 		isc_netaddr_fromsockaddr(&primaryip, &xfr->primaryaddr);
   1665 		result = dns_peerlist_peerbyaddr(xfr->view->peers, &primaryip,
   1666 						 &peer);
   1667 		if (result == ISC_R_SUCCESS) {
   1668 			(void)dns_peer_getsupportedns(peer, &edns);
   1669 			(void)dns_peer_getudpsize(peer, &udpsize);
   1670 			(void)dns_peer_getrequestnsid(peer, &reqnsid);
   1671 			(void)dns_peer_getrequestexpire(peer, &reqexpire);
   1672 		}
   1673 	}
   1674 
   1675 	if (edns) {
   1676 		CHECK(add_opt(msg, udpsize, reqnsid, reqexpire));
   1677 	}
   1678 
   1679 	atomic_store_relaxed(&xfr->nmsg, 0);
   1680 	atomic_store_relaxed(&xfr->nrecs, 0);
   1681 	ISC_XFRIN_STORE(&xfr->nbytes, 0);
   1682 	ISC_XFRIN_STORE(&xfr->start, isc_time_now());
   1683 
   1684 	xfr->nbytes_saved = 0;
   1685 
   1686 	msg->id = xfr->id;
   1687 	if (xfr->tsigctx != NULL) {
   1688 		dst_context_destroy(&xfr->tsigctx);
   1689 	}
   1690 
   1691 	CHECK(render(msg, xfr->mctx, &xfr->qbuffer));
   1692 
   1693 	/*
   1694 	 * Free the last tsig, if there is one.
   1695 	 */
   1696 	if (xfr->lasttsig != NULL) {
   1697 		isc_buffer_free(&xfr->lasttsig);
   1698 	}
   1699 
   1700 	/*
   1701 	 * Save the query TSIG and don't let message_destroy free it.
   1702 	 */
   1703 	CHECK(dns_message_getquerytsig(msg, xfr->mctx, &xfr->lasttsig));
   1704 
   1705 	isc_buffer_usedregion(&xfr->qbuffer, &region);
   1706 	INSIST(region.length <= 65535);
   1707 
   1708 	dns_xfrin_ref(xfr);
   1709 	dns_dispatch_send(xfr->dispentry, &region);
   1710 	xfrin_log(xfr, ISC_LOG_DEBUG(3), "sending %s request, QID %d",
   1711 		  request_type(xfr), xfr->id);
   1712 
   1713 failure:
   1714 	dns_message_detach(&msg);
   1715 	if (soatuple != NULL) {
   1716 		dns_difftuple_free(&soatuple);
   1717 	}
   1718 	if (ver != NULL) {
   1719 		dns_db_closeversion(xfr->db, &ver, false);
   1720 	}
   1721 
   1722 	return result;
   1723 }
   1724 
   1725 static void
   1726 xfrin_send_done(isc_result_t result, isc_region_t *region, void *arg) {
   1727 	dns_xfrin_t *xfr = (dns_xfrin_t *)arg;
   1728 
   1729 	UNUSED(region);
   1730 
   1731 	REQUIRE(VALID_XFRIN(xfr));
   1732 
   1733 	if (atomic_load(&xfr->shuttingdown)) {
   1734 		result = ISC_R_SHUTTINGDOWN;
   1735 	}
   1736 
   1737 	LIBDNS_XFRIN_SENT(xfr, xfr->info, result);
   1738 
   1739 	CHECK(result);
   1740 
   1741 	xfrin_log(xfr, ISC_LOG_DEBUG(3), "sent request data");
   1742 
   1743 failure:
   1744 	if (result != ISC_R_SUCCESS) {
   1745 		xfrin_fail(xfr, result, "failed sending request data");
   1746 	}
   1747 
   1748 	dns_xfrin_detach(&xfr);
   1749 }
   1750 
   1751 static void
   1752 get_edns_expire(dns_xfrin_t *xfr, dns_message_t *msg) {
   1753 	isc_result_t result;
   1754 	dns_rdata_t rdata = DNS_RDATA_INIT;
   1755 	isc_buffer_t optbuf;
   1756 	uint16_t optcode;
   1757 	uint16_t optlen;
   1758 
   1759 	result = dns_rdataset_first(msg->opt);
   1760 	if (result == ISC_R_SUCCESS) {
   1761 		dns_rdataset_current(msg->opt, &rdata);
   1762 		isc_buffer_init(&optbuf, rdata.data, rdata.length);
   1763 		isc_buffer_add(&optbuf, rdata.length);
   1764 		while (isc_buffer_remaininglength(&optbuf) >= 4) {
   1765 			optcode = isc_buffer_getuint16(&optbuf);
   1766 			optlen = isc_buffer_getuint16(&optbuf);
   1767 			/*
   1768 			 * A EDNS EXPIRE response has a length of 4.
   1769 			 */
   1770 			if (optcode != DNS_OPT_EXPIRE || optlen != 4) {
   1771 				isc_buffer_forward(&optbuf, optlen);
   1772 				continue;
   1773 			}
   1774 			xfr->expireopt = isc_buffer_getuint32(&optbuf);
   1775 			xfr->expireoptset = true;
   1776 			dns_zone_log(xfr->zone, ISC_LOG_DEBUG(1),
   1777 				     "got EDNS EXPIRE of %u", xfr->expireopt);
   1778 			break;
   1779 		}
   1780 	}
   1781 }
   1782 
   1783 static void
   1784 xfrin_end(dns_xfrin_t *xfr, isc_result_t result) {
   1785 	/* Inform the caller. */
   1786 	if (xfr->done != NULL) {
   1787 		LIBDNS_XFRIN_DONE_CALLBACK_BEGIN(xfr, xfr->info, result);
   1788 		(xfr->done)(xfr->zone,
   1789 			    xfr->expireoptset ? &xfr->expireopt : NULL, result);
   1790 		xfr->done = NULL;
   1791 		LIBDNS_XFRIN_DONE_CALLBACK_END(xfr, xfr->info, result);
   1792 	}
   1793 
   1794 	atomic_store(&xfr->shuttingdown, true);
   1795 
   1796 	if (xfr->max_time_timer != NULL) {
   1797 		isc_timer_stop(xfr->max_time_timer);
   1798 		isc_timer_destroy(&xfr->max_time_timer);
   1799 	}
   1800 	if (xfr->max_idle_timer != NULL) {
   1801 		isc_timer_stop(xfr->max_idle_timer);
   1802 		isc_timer_destroy(&xfr->max_idle_timer);
   1803 	}
   1804 	if (xfr->min_rate_timer != NULL) {
   1805 		isc_timer_stop(xfr->min_rate_timer);
   1806 		isc_timer_destroy(&xfr->min_rate_timer);
   1807 	}
   1808 
   1809 	if (xfr->shutdown_result == ISC_R_UNSET) {
   1810 		xfr->shutdown_result = result;
   1811 	}
   1812 }
   1813 
   1814 static void
   1815 xfrin_recv_done(isc_result_t result, isc_region_t *region, void *arg) {
   1816 	dns_xfrin_t *xfr = (dns_xfrin_t *)arg;
   1817 	dns_message_t *msg = NULL;
   1818 	dns_name_t *name = NULL;
   1819 	const dns_name_t *tsigowner = NULL;
   1820 	isc_buffer_t buffer;
   1821 
   1822 	REQUIRE(VALID_XFRIN(xfr));
   1823 
   1824 	if (atomic_load(&xfr->shuttingdown)) {
   1825 		result = ISC_R_SHUTTINGDOWN;
   1826 	}
   1827 
   1828 	/* Stop the idle timer */
   1829 	isc_timer_stop(xfr->max_idle_timer);
   1830 
   1831 	LIBDNS_XFRIN_RECV_START(xfr, xfr->info, result);
   1832 
   1833 	CHECK(result);
   1834 
   1835 	xfrin_log(xfr, ISC_LOG_DEBUG(7), "received %u bytes", region->length);
   1836 
   1837 	dns_message_create(xfr->mctx, NULL, NULL, DNS_MESSAGE_INTENTPARSE,
   1838 			   &msg);
   1839 
   1840 	CHECK(dns_message_settsigkey(msg, xfr->tsigkey));
   1841 	dns_message_setquerytsig(msg, xfr->lasttsig);
   1842 
   1843 	msg->tsigctx = xfr->tsigctx;
   1844 	xfr->tsigctx = NULL;
   1845 
   1846 	dns_message_setclass(msg, xfr->rdclass);
   1847 
   1848 	msg->tcp_continuation = (atomic_load_relaxed(&xfr->nmsg) > 0) ? 1 : 0;
   1849 
   1850 	isc_buffer_init(&buffer, region->base, region->length);
   1851 	isc_buffer_add(&buffer, region->length);
   1852 
   1853 	result = dns_message_parse(msg, &buffer,
   1854 				   DNS_MESSAGEPARSE_PRESERVEORDER);
   1855 	if (result == ISC_R_SUCCESS) {
   1856 		dns_message_logpacket(
   1857 			msg, "received message from", &xfr->primaryaddr,
   1858 			DNS_LOGCATEGORY_XFER_IN, DNS_LOGMODULE_XFER_IN,
   1859 			ISC_LOG_DEBUG(10), xfr->mctx);
   1860 	} else {
   1861 		xfrin_log(xfr, ISC_LOG_DEBUG(10), "dns_message_parse: %s",
   1862 			  isc_result_totext(result));
   1863 	}
   1864 
   1865 	LIBDNS_XFRIN_RECV_PARSED(xfr, xfr->info, result);
   1866 
   1867 	if (result != ISC_R_SUCCESS || msg->rcode != dns_rcode_noerror ||
   1868 	    msg->opcode != dns_opcode_query || msg->rdclass != xfr->rdclass)
   1869 	{
   1870 		if (result == ISC_R_SUCCESS &&
   1871 		    msg->rcode == dns_rcode_formerr && xfr->edns &&
   1872 		    (atomic_load(&xfr->state) == XFRST_SOAQUERY ||
   1873 		     atomic_load(&xfr->state) == XFRST_ZONEXFRREQUEST))
   1874 		{
   1875 			xfr->edns = false;
   1876 			dns_message_detach(&msg);
   1877 			xfrin_reset(xfr);
   1878 			goto try_again;
   1879 		} else if (result == ISC_R_SUCCESS &&
   1880 			   msg->rcode != dns_rcode_noerror)
   1881 		{
   1882 			result = dns_result_fromrcode(msg->rcode);
   1883 		} else if (result == ISC_R_SUCCESS &&
   1884 			   msg->opcode != dns_opcode_query)
   1885 		{
   1886 			result = DNS_R_UNEXPECTEDOPCODE;
   1887 		} else if (result == ISC_R_SUCCESS &&
   1888 			   msg->rdclass != xfr->rdclass)
   1889 		{
   1890 			result = DNS_R_BADCLASS;
   1891 		} else if (result == ISC_R_SUCCESS || result == DNS_R_NOERROR) {
   1892 			result = DNS_R_UNEXPECTEDID;
   1893 		}
   1894 
   1895 		if (xfr->reqtype == dns_rdatatype_axfr ||
   1896 		    xfr->reqtype == dns_rdatatype_soa)
   1897 		{
   1898 			goto failure;
   1899 		}
   1900 
   1901 		xfrin_log(xfr, ISC_LOG_DEBUG(3), "got %s, retrying with AXFR",
   1902 			  isc_result_totext(result));
   1903 	try_axfr:
   1904 		LIBDNS_XFRIN_RECV_TRY_AXFR(xfr, xfr->info, result);
   1905 		dns_message_detach(&msg);
   1906 		xfrin_reset(xfr);
   1907 		xfr->reqtype = dns_rdatatype_soa;
   1908 		atomic_store(&xfr->state, XFRST_SOAQUERY);
   1909 	try_again:
   1910 		result = xfrin_start(xfr);
   1911 		if (result != ISC_R_SUCCESS) {
   1912 			xfrin_fail(xfr, result, "failed setting up socket");
   1913 		}
   1914 		dns_xfrin_detach(&xfr);
   1915 		return;
   1916 	}
   1917 
   1918 	/*
   1919 	 * The question section should exist for SOA and in the first
   1920 	 * message of a AXFR or IXFR response.  The question section
   1921 	 * may exist in the 2nd and subsequent messages in a AXFR or
   1922 	 * IXFR response.  If the question section exists it should
   1923 	 * match the question that was sent.
   1924 	 */
   1925 	if (msg->counts[DNS_SECTION_QUESTION] > 1) {
   1926 		xfrin_log(xfr, ISC_LOG_NOTICE, "too many questions (%u)",
   1927 			  msg->counts[DNS_SECTION_QUESTION]);
   1928 		result = DNS_R_FORMERR;
   1929 		goto failure;
   1930 	}
   1931 
   1932 	if ((atomic_load(&xfr->state) == XFRST_SOAQUERY ||
   1933 	     atomic_load(&xfr->state) == XFRST_ZONEXFRREQUEST) &&
   1934 	    msg->counts[DNS_SECTION_QUESTION] != 1)
   1935 	{
   1936 		xfrin_log(xfr, ISC_LOG_NOTICE, "missing question section");
   1937 		result = DNS_R_FORMERR;
   1938 		goto failure;
   1939 	}
   1940 
   1941 	for (result = dns_message_firstname(msg, DNS_SECTION_QUESTION);
   1942 	     result == ISC_R_SUCCESS;
   1943 	     result = dns_message_nextname(msg, DNS_SECTION_QUESTION))
   1944 	{
   1945 		dns_rdataset_t *rds = NULL;
   1946 
   1947 		LIBDNS_XFRIN_RECV_QUESTION(xfr, xfr->info, msg);
   1948 
   1949 		name = NULL;
   1950 		dns_message_currentname(msg, DNS_SECTION_QUESTION, &name);
   1951 		if (!dns_name_equal(name, &xfr->name)) {
   1952 			xfrin_log(xfr, ISC_LOG_NOTICE,
   1953 				  "question name mismatch");
   1954 			result = DNS_R_FORMERR;
   1955 			goto failure;
   1956 		}
   1957 		rds = ISC_LIST_HEAD(name->list);
   1958 		INSIST(rds != NULL);
   1959 		if (rds->type != xfr->reqtype) {
   1960 			xfrin_log(xfr, ISC_LOG_NOTICE,
   1961 				  "question type mismatch");
   1962 			result = DNS_R_FORMERR;
   1963 			goto failure;
   1964 		}
   1965 		if (rds->rdclass != xfr->rdclass) {
   1966 			xfrin_log(xfr, ISC_LOG_NOTICE,
   1967 				  "question class mismatch");
   1968 			result = DNS_R_FORMERR;
   1969 			goto failure;
   1970 		}
   1971 	}
   1972 	if (result != ISC_R_NOMORE) {
   1973 		goto failure;
   1974 	}
   1975 
   1976 	/*
   1977 	 * Does the server know about IXFR?  If it doesn't we will get
   1978 	 * a message with a empty answer section or a potentially a CNAME /
   1979 	 * DNAME, the later is handled by xfr_rr() which will return FORMERR
   1980 	 * if the first RR in the answer section is not a SOA record.
   1981 	 */
   1982 	if (xfr->reqtype == dns_rdatatype_ixfr &&
   1983 	    atomic_load(&xfr->state) == XFRST_ZONEXFRREQUEST &&
   1984 	    msg->counts[DNS_SECTION_ANSWER] == 0)
   1985 	{
   1986 		xfrin_log(xfr, ISC_LOG_DEBUG(3),
   1987 			  "empty answer section, retrying with AXFR");
   1988 		goto try_axfr;
   1989 	}
   1990 
   1991 	if (xfr->reqtype == dns_rdatatype_soa &&
   1992 	    (msg->flags & DNS_MESSAGEFLAG_AA) == 0)
   1993 	{
   1994 		result = DNS_R_NOTAUTHORITATIVE;
   1995 		goto failure;
   1996 	}
   1997 
   1998 	result = dns_message_checksig(msg, xfr->view);
   1999 	if (result != ISC_R_SUCCESS) {
   2000 		xfrin_log(xfr, ISC_LOG_DEBUG(3), "TSIG check failed: %s",
   2001 			  isc_result_totext(result));
   2002 		goto failure;
   2003 	}
   2004 
   2005 	for (result = dns_message_firstname(msg, DNS_SECTION_ANSWER);
   2006 	     result == ISC_R_SUCCESS;
   2007 	     result = dns_message_nextname(msg, DNS_SECTION_ANSWER))
   2008 	{
   2009 		dns_rdataset_t *rds = NULL;
   2010 
   2011 		LIBDNS_XFRIN_RECV_ANSWER(xfr, xfr->info, msg);
   2012 
   2013 		name = NULL;
   2014 		dns_message_currentname(msg, DNS_SECTION_ANSWER, &name);
   2015 		for (rds = ISC_LIST_HEAD(name->list); rds != NULL;
   2016 		     rds = ISC_LIST_NEXT(rds, link))
   2017 		{
   2018 			for (result = dns_rdataset_first(rds);
   2019 			     result == ISC_R_SUCCESS;
   2020 			     result = dns_rdataset_next(rds))
   2021 			{
   2022 				dns_rdata_t rdata = DNS_RDATA_INIT;
   2023 				dns_rdataset_current(rds, &rdata);
   2024 				CHECK(xfr_rr(xfr, name, rds->ttl, &rdata));
   2025 			}
   2026 		}
   2027 	}
   2028 	if (result == ISC_R_NOMORE) {
   2029 		result = ISC_R_SUCCESS;
   2030 	}
   2031 	CHECK(result);
   2032 
   2033 	if (dns_message_gettsig(msg, &tsigowner) != NULL) {
   2034 		/*
   2035 		 * Reset the counter.
   2036 		 */
   2037 		xfr->sincetsig = 0;
   2038 
   2039 		/*
   2040 		 * Free the last tsig, if there is one.
   2041 		 */
   2042 		if (xfr->lasttsig != NULL) {
   2043 			isc_buffer_free(&xfr->lasttsig);
   2044 		}
   2045 
   2046 		/*
   2047 		 * Update the last tsig pointer.
   2048 		 */
   2049 		CHECK(dns_message_getquerytsig(msg, xfr->mctx, &xfr->lasttsig));
   2050 	} else if (dns_message_gettsigkey(msg) != NULL) {
   2051 		xfr->sincetsig++;
   2052 		if (xfr->sincetsig > 100 ||
   2053 		    atomic_load_relaxed(&xfr->nmsg) == 0 ||
   2054 		    atomic_load(&xfr->state) == XFRST_AXFR_END ||
   2055 		    atomic_load(&xfr->state) == XFRST_IXFR_END)
   2056 		{
   2057 			result = DNS_R_EXPECTEDTSIG;
   2058 			goto failure;
   2059 		}
   2060 	}
   2061 
   2062 	/*
   2063 	 * Update the number of messages and bytes received.
   2064 	 */
   2065 	atomic_fetch_add_relaxed(&xfr->nmsg, 1);
   2066 	ISC_XFRIN_ADD(&xfr->nbytes, buffer.used);
   2067 
   2068 	/*
   2069 	 * Take the context back.
   2070 	 */
   2071 	INSIST(xfr->tsigctx == NULL);
   2072 	xfr->tsigctx = msg->tsigctx;
   2073 	msg->tsigctx = NULL;
   2074 
   2075 	if (!xfr->expireoptset && msg->opt != NULL) {
   2076 		get_edns_expire(xfr, msg);
   2077 	}
   2078 
   2079 	switch (atomic_load(&xfr->state)) {
   2080 	case XFRST_GOTSOA:
   2081 		xfr->reqtype = dns_rdatatype_axfr;
   2082 		atomic_store(&xfr->state, XFRST_ZONEXFRREQUEST);
   2083 		CHECK(xfrin_start(xfr));
   2084 		break;
   2085 	case XFRST_AXFR_END:
   2086 	case XFRST_IXFR_END:
   2087 		/* We are at the end, cancel the timers and IO */
   2088 		isc_timer_stop(xfr->min_rate_timer);
   2089 		isc_timer_stop(xfr->max_idle_timer);
   2090 		isc_timer_stop(xfr->max_time_timer);
   2091 		xfrin_cancelio(xfr);
   2092 		break;
   2093 	default:
   2094 		/*
   2095 		 * Read the next message.
   2096 		 */
   2097 		dns_message_detach(&msg);
   2098 		result = dns_dispatch_getnext(xfr->dispentry);
   2099 		if (result != ISC_R_SUCCESS) {
   2100 			goto failure;
   2101 		}
   2102 
   2103 		isc_interval_t interval;
   2104 		isc_interval_set(&interval, dns_zone_getidlein(xfr->zone), 0);
   2105 		isc_timer_start(xfr->max_idle_timer, isc_timertype_once,
   2106 				&interval);
   2107 
   2108 		LIBDNS_XFRIN_READ(xfr, xfr->info, result);
   2109 		return;
   2110 	}
   2111 
   2112 failure:
   2113 	if (result != ISC_R_SUCCESS) {
   2114 		xfrin_fail(xfr, result, "failed while receiving responses");
   2115 	}
   2116 
   2117 	if (msg != NULL) {
   2118 		dns_message_detach(&msg);
   2119 	}
   2120 	dns_xfrin_detach(&xfr);
   2121 	LIBDNS_XFRIN_RECV_DONE(xfr, xfr->info, result);
   2122 }
   2123 
   2124 static void
   2125 xfrin_destroy(dns_xfrin_t *xfr) {
   2126 	uint64_t msecs, persec;
   2127 	isc_time_t now = isc_time_now();
   2128 	char expireopt[sizeof("4000000000")] = { 0 };
   2129 	const char *sep = "";
   2130 
   2131 	REQUIRE(VALID_XFRIN(xfr));
   2132 
   2133 	/* Safe-guards */
   2134 	REQUIRE(atomic_load(&xfr->shuttingdown));
   2135 
   2136 	INSIST(xfr->shutdown_result != ISC_R_UNSET);
   2137 
   2138 	/*
   2139 	 * If we're called through dns_xfrin_detach() and are not
   2140 	 * shutting down, we can't know what the transfer status is as
   2141 	 * we are only called when the last reference is lost.
   2142 	 */
   2143 	xfrin_log(xfr, ISC_LOG_INFO, "Transfer status: %s",
   2144 		  isc_result_totext(xfr->shutdown_result));
   2145 
   2146 	/*
   2147 	 * Calculate the length of time the transfer took,
   2148 	 * and print a log message with the bytes and rate.
   2149 	 */
   2150 	isc_time_t start = ISC_XFRIN_LOAD(&xfr->start, isc_time_t);
   2151 	msecs = isc_time_microdiff(&now, &start) / 1000;
   2152 	if (msecs == 0) {
   2153 		msecs = 1;
   2154 	}
   2155 	persec = (ISC_XFRIN_LOAD(&xfr->nbytes, uint64_t) * 1000) / msecs;
   2156 
   2157 	if (xfr->expireoptset) {
   2158 		sep = ", expire option ";
   2159 		snprintf(expireopt, sizeof(expireopt), "%u", xfr->expireopt);
   2160 	}
   2161 
   2162 	xfrin_log(xfr, ISC_LOG_INFO,
   2163 		  "Transfer completed: %d messages, %d records, "
   2164 		  "%" PRIu64 " bytes, "
   2165 		  "%u.%03u secs (%u bytes/sec) (serial %" PRIuFAST32 "%s%s)",
   2166 		  atomic_load_relaxed(&xfr->nmsg),
   2167 		  atomic_load_relaxed(&xfr->nrecs),
   2168 		  ISC_XFRIN_LOAD(&xfr->nbytes, uint64_t),
   2169 		  (unsigned int)(msecs / 1000), (unsigned int)(msecs % 1000),
   2170 		  (unsigned int)persec, atomic_load_relaxed(&xfr->end_serial),
   2171 		  sep, expireopt);
   2172 
   2173 	/* Cleanup unprocessed IXFR data */
   2174 	struct cds_wfcq_node *node, *next;
   2175 	__cds_wfcq_for_each_blocking_safe(&xfr->diff_head, &xfr->diff_tail,
   2176 					  node, next) {
   2177 		ixfr_apply_data_t *data =
   2178 			caa_container_of(node, ixfr_apply_data_t, wfcq_node);
   2179 		/* We need to clear and free all data chunks */
   2180 		dns_diff_clear(&data->diff);
   2181 		isc_mem_put(xfr->mctx, data, sizeof(*data));
   2182 	}
   2183 
   2184 	/* Cleanup unprocessed AXFR data */
   2185 	dns_diff_clear(&xfr->diff);
   2186 
   2187 	xfrin_cancelio(xfr);
   2188 
   2189 	if (xfr->transport != NULL) {
   2190 		dns_transport_detach(&xfr->transport);
   2191 	}
   2192 
   2193 	if (xfr->tsigkey != NULL) {
   2194 		dns_tsigkey_detach(&xfr->tsigkey);
   2195 	}
   2196 
   2197 	if (xfr->lasttsig != NULL) {
   2198 		isc_buffer_free(&xfr->lasttsig);
   2199 	}
   2200 
   2201 	if (xfr->ixfr.journal != NULL) {
   2202 		dns_journal_destroy(&xfr->ixfr.journal);
   2203 	}
   2204 
   2205 	if (xfr->axfr.add_private != NULL) {
   2206 		(void)dns_db_endload(xfr->db, &xfr->axfr);
   2207 	}
   2208 
   2209 	if (xfr->tsigctx != NULL) {
   2210 		dst_context_destroy(&xfr->tsigctx);
   2211 	}
   2212 
   2213 	if (xfr->name.attributes.dynamic) {
   2214 		dns_name_free(&xfr->name, xfr->mctx);
   2215 	}
   2216 
   2217 	if (xfr->ver != NULL) {
   2218 		dns_db_closeversion(xfr->db, &xfr->ver, false);
   2219 	}
   2220 
   2221 	if (xfr->db != NULL) {
   2222 		dns_db_detach(&xfr->db);
   2223 	}
   2224 
   2225 	if (xfr->zone != NULL) {
   2226 		if (!xfr->zone_had_db &&
   2227 		    xfr->shutdown_result == ISC_R_SUCCESS &&
   2228 		    dns_zone_gettype(xfr->zone) == dns_zone_mirror)
   2229 		{
   2230 			dns_zone_log(xfr->zone, ISC_LOG_INFO,
   2231 				     "mirror zone is now in use");
   2232 		}
   2233 		xfrin_log(xfr, ISC_LOG_DEBUG(99), "freeing transfer context");
   2234 		/*
   2235 		 * xfr->zone must not be detached before xfrin_log() is called.
   2236 		 */
   2237 		dns_zone_idetach(&xfr->zone);
   2238 	}
   2239 
   2240 	if (xfr->view != NULL) {
   2241 		dns_view_weakdetach(&xfr->view);
   2242 	}
   2243 
   2244 	if (xfr->firstsoa_data != NULL) {
   2245 		isc_mem_free(xfr->mctx, xfr->firstsoa_data);
   2246 	}
   2247 
   2248 	if (xfr->tlsctx_cache != NULL) {
   2249 		isc_tlsctx_cache_detach(&xfr->tlsctx_cache);
   2250 	}
   2251 
   2252 	INSIST(xfr->max_time_timer == NULL);
   2253 	INSIST(xfr->max_idle_timer == NULL);
   2254 	INSIST(xfr->min_rate_timer == NULL);
   2255 
   2256 	isc_loop_detach(&xfr->loop);
   2257 
   2258 	isc_mem_putanddetach(&xfr->mctx, xfr, sizeof(*xfr));
   2259 }
   2260 
   2261 /*
   2262  * Log incoming zone transfer messages in a format like
   2263  * transfer of <zone> from <address>: <message>
   2264  */
   2265 
   2266 static void
   2267 xfrin_log(dns_xfrin_t *xfr, int level, const char *fmt, ...) {
   2268 	va_list ap;
   2269 	char primarytext[ISC_SOCKADDR_FORMATSIZE];
   2270 	char msgtext[2048];
   2271 
   2272 	if (!isc_log_wouldlog(dns_lctx, level)) {
   2273 		return;
   2274 	}
   2275 
   2276 	isc_sockaddr_format(&xfr->primaryaddr, primarytext,
   2277 			    sizeof(primarytext));
   2278 	va_start(ap, fmt);
   2279 	vsnprintf(msgtext, sizeof(msgtext), fmt, ap);
   2280 	va_end(ap);
   2281 
   2282 	isc_log_write(dns_lctx, DNS_LOGCATEGORY_XFER_IN, DNS_LOGMODULE_XFER_IN,
   2283 		      level, "%p: transfer of '%s' from %s: %s", xfr, xfr->info,
   2284 		      primarytext, msgtext);
   2285 }
   2286