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