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