view.c 62.2 KB
Newer Older
Bob Halley's avatar
add    
Bob Halley committed
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4
5
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
6
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
7
8
9
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
Bob Halley's avatar
add    
Bob Halley committed
10
11
 */

12
/*! \file */
David Lawrence's avatar
David Lawrence committed
13

14
#include <inttypes.h>
15
#include <limits.h>
16
#include <stdbool.h>
17

18
#ifdef HAVE_LMDB
19
#include <lmdb.h>
20
#endif /* ifdef HAVE_LMDB */
21

22
#include <isc/atomic.h>
23
#include <isc/file.h>
24
#include <isc/hash.h>
25
#include <isc/lex.h>
26
#include <isc/print.h>
27
#include <isc/stats.h>
28
#include <isc/string.h> /* Required for HP/UX (and others?) */
29
#include <isc/task.h>
Michael Graff's avatar
Michael Graff committed
30
#include <isc/util.h>
Bob Halley's avatar
add    
Bob Halley committed
31

32
#include <dns/acl.h>
Bob Halley's avatar
add adb    
Bob Halley committed
33
#include <dns/adb.h>
Evan Hunt's avatar
Evan Hunt committed
34
#include <dns/badcache.h>
35
#include <dns/cache.h>
Bob Halley's avatar
Bob Halley committed
36
#include <dns/db.h>
37
#include <dns/dispatch.h>
38
#include <dns/dlz.h>
39
#include <dns/dns64.h>
40
#include <dns/dnssec.h>
41
#include <dns/events.h>
42
#include <dns/forward.h>
43
#include <dns/keytable.h>
44
#include <dns/keyvalues.h>
45
#include <dns/master.h>
46
#include <dns/masterdump.h>
Evan Hunt's avatar
Evan Hunt committed
47
#include <dns/nta.h>
48
#include <dns/order.h>
49
#include <dns/peer.h>
50
#include <dns/rbt.h>
Bob Halley's avatar
Bob Halley committed
51
#include <dns/rdataset.h>
Mark Andrews's avatar
Mark Andrews committed
52
#include <dns/request.h>
53
54
#include <dns/resolver.h>
#include <dns/result.h>
55
#include <dns/rpz.h>
56
#include <dns/rrl.h>
57
#include <dns/stats.h>
58
#include <dns/time.h>
Ondřej Surý's avatar
Ondřej Surý committed
59
#include <dns/transport.h>
Brian Wellington's avatar
Brian Wellington committed
60
#include <dns/tsig.h>
61
#include <dns/zone.h>
62
#include <dns/zt.h>
Bob Halley's avatar
add    
Bob Halley committed
63

64
65
66
67
68
#define CHECK(op)                            \
	do {                                 \
		result = (op);               \
		if (result != ISC_R_SUCCESS) \
			goto cleanup;        \
69
70
	} while (0)

71
72
73
74
75
76
#define RESSHUTDOWN(v) \
	((atomic_load(&(v)->attributes) & DNS_VIEWATTR_RESSHUTDOWN) != 0)
#define ADBSHUTDOWN(v) \
	((atomic_load(&(v)->attributes) & DNS_VIEWATTR_ADBSHUTDOWN) != 0)
#define REQSHUTDOWN(v) \
	((atomic_load(&(v)->attributes) & DNS_VIEWATTR_REQSHUTDOWN) != 0)
77

Evan Hunt's avatar
Evan Hunt committed
78
#define DNS_VIEW_DELONLYHASH   111
Evan Hunt's avatar
Evan Hunt committed
79
#define DNS_VIEW_FAILCACHESIZE 1021
80

Ondřej Surý's avatar
Ondřej Surý committed
81
82
83
84
85
86
static void
resolver_shutdown(isc_task_t *task, isc_event_t *event);
static void
adb_shutdown(isc_task_t *task, isc_event_t *event);
static void
req_shutdown(isc_task_t *task, isc_event_t *event);
87

Bob Halley's avatar
add    
Bob Halley committed
88
isc_result_t
89
dns_view_create(isc_mem_t *mctx, dns_rdataclass_t rdclass, const char *name,
Evan Hunt's avatar
Evan Hunt committed
90
91
		dns_view_t **viewp) {
	dns_view_t *view;
Bob Halley's avatar
add    
Bob Halley committed
92
	isc_result_t result;
Evan Hunt's avatar
Evan Hunt committed
93
	char buffer[1024];
Bob Halley's avatar
add    
Bob Halley committed
94
95
96
97
98
99
100
101

	/*
	 * Create a view.
	 */

	REQUIRE(name != NULL);
	REQUIRE(viewp != NULL && *viewp == NULL);

Andreas Gustafsson's avatar
Andreas Gustafsson committed
102
	view = isc_mem_get(mctx, sizeof(*view));
103

104
	view->nta_file = NULL;
105
106
	view->mctx = NULL;
	isc_mem_attach(mctx, &view->mctx);
Bob Halley's avatar
add    
Bob Halley committed
107
	view->name = isc_mem_strdup(mctx, name);
108

109
110
	result = isc_file_sanitize(NULL, view->name, "nta", buffer,
				   sizeof(buffer));
111
	if (result != ISC_R_SUCCESS) {
112
		goto cleanup_name;
113
	}
114
115
	view->nta_file = isc_mem_strdup(mctx, buffer);

Ondřej Surý's avatar
Ondřej Surý committed
116
	isc_mutex_init(&view->lock);
117

118
	view->zonetable = NULL;
Evan Hunt's avatar
Evan Hunt committed
119
120
121
122
123
124
125
	result = dns_zt_create(mctx, rdclass, &view->zonetable);
	if (result != ISC_R_SUCCESS) {
		UNEXPECTED_ERROR(__FILE__, __LINE__,
				 "dns_zt_create() failed: %s",
				 isc_result_totext(result));
		result = ISC_R_UNEXPECTED;
		goto cleanup_mutex;
Bob Halley's avatar
add    
Bob Halley committed
126
	}
Evan Hunt's avatar
Evan Hunt committed
127

128
	view->secroots_priv = NULL;
Evan Hunt's avatar
Evan Hunt committed
129
	view->ntatable_priv = NULL;
130
131
132
133
134
135
136
	view->fwdtable = NULL;
	result = dns_fwdtable_create(mctx, &view->fwdtable);
	if (result != ISC_R_SUCCESS) {
		UNEXPECTED_ERROR(__FILE__, __LINE__,
				 "dns_fwdtable_create() failed: %s",
				 isc_result_totext(result));
		result = ISC_R_UNEXPECTED;
137
		goto cleanup_zt;
138
	}
139

140
	view->cache = NULL;
Bob Halley's avatar
Bob Halley committed
141
	view->cachedb = NULL;
Evan Hunt's avatar
Evan Hunt committed
142
143
	ISC_LIST_INIT(view->dlz_searched);
	ISC_LIST_INIT(view->dlz_unsearched);
Bob Halley's avatar
Bob Halley committed
144
	view->hints = NULL;
Bob Halley's avatar
add    
Bob Halley committed
145
	view->resolver = NULL;
Bob Halley's avatar
add adb    
Bob Halley committed
146
	view->adb = NULL;
Mark Andrews's avatar
Mark Andrews committed
147
	view->requestmgr = NULL;
Bob Halley's avatar
add    
Bob Halley committed
148
	view->rdclass = rdclass;
149
	view->frozen = false;
150
	view->task = NULL;
151
	isc_refcount_init(&view->references, 1);
152
	isc_refcount_init(&view->weakrefs, 1);
153
154
155
	atomic_init(&view->attributes,
		    (DNS_VIEWATTR_RESSHUTDOWN | DNS_VIEWATTR_ADBSHUTDOWN |
		     DNS_VIEWATTR_REQSHUTDOWN));
Ondřej Surý's avatar
Ondřej Surý committed
156
	view->transports = NULL;
Brian Wellington's avatar
Brian Wellington committed
157
158
	view->statickeys = NULL;
	view->dynamickeys = NULL;
159
	view->matchclients = NULL;
160
	view->matchdestinations = NULL;
161
	view->matchrecursiveonly = false;
162
	result = dns_tsigkeyring_create(view->mctx, &view->dynamickeys);
163
	if (result != ISC_R_SUCCESS) {
164
		goto cleanup_weakrefs;
165
	}
166
	view->peers = NULL;
167
	view->order = NULL;
168
	view->delonly = NULL;
169
	view->rootdelonly = false;
Mark Andrews's avatar
Mark Andrews committed
170
	view->rootexclude = NULL;
171
	view->adbstats = NULL;
172
173
	view->resstats = NULL;
	view->resquerystats = NULL;
174
	view->cacheshared = false;
175
176
	ISC_LIST_INIT(view->dns64);
	view->dns64cnt = 0;
177

178
179
	/*
	 * Initialize configuration data with default values.
180
	 */
181
182
183
184
185
186
187
188
	view->recursion = true;
	view->qminimization = false;
	view->qmin_strict = false;
	view->auth_nxdomain = false; /* Was true in BIND 8 */
	view->enablevalidation = true;
	view->acceptexpired = false;
	view->use_glue_cache = false;
	view->minimal_any = false;
189
	view->minimalresponses = dns_minimal_no;
190
	view->transfer_format = dns_one_answer;
191
192
	view->cacheacl = NULL;
	view->cacheonacl = NULL;
193
	view->checknames = false;
194
	view->queryacl = NULL;
195
	view->queryonacl = NULL;
196
	view->recursionacl = NULL;
197
	view->recursiononacl = NULL;
198
	view->sortlist = NULL;
199
200
201
202
	view->transferacl = NULL;
	view->notifyacl = NULL;
	view->updateacl = NULL;
	view->upfwdacl = NULL;
203
	view->denyansweracl = NULL;
Evan Hunt's avatar
Evan Hunt committed
204
	view->nocasecompress = NULL;
205
	view->msgcompression = true;
206
207
208
	view->answeracl_exclude = NULL;
	view->denyanswernames = NULL;
	view->answernames_exclude = NULL;
Evan Hunt's avatar
Evan Hunt committed
209
	view->rrl = NULL;
210
	view->provideixfr = true;
211
212
	view->maxcachettl = 7 * 24 * 3600;
	view->maxncachettl = 3 * 3600;
213
214
	view->mincachettl = 0;
	view->minncachettl = 0;
Evan Hunt's avatar
Evan Hunt committed
215
216
	view->nta_lifetime = 0;
	view->nta_recheck = 0;
Evan Hunt's avatar
Evan Hunt committed
217
218
	view->prefetch_eligible = 0;
	view->prefetch_trigger = 0;
219
	view->dstport = 53;
220
	view->preferred_glue = 0;
221
	view->flush = false;
222
	view->maxudp = 0;
223
224
	view->staleanswerttl = 1;
	view->staleanswersok = dns_stale_answer_conf;
225
	view->staleanswersenable = false;
226
	view->nocookieudp = 0;
227
228
	view->padding = 0;
	view->pad_acl = NULL;
229
	view->maxbits = 0;
230
	view->rpzs = NULL;
231
	view->catzs = NULL;
232
	view->managed_keys = NULL;
233
	view->redirect = NULL;
234
235
	view->redirectzone = NULL;
	dns_fixedname_init(&view->redirectfixed);
236
237
238
239
240
241
	view->requestnsid = false;
	view->sendcookie = true;
	view->requireservercookie = false;
	view->synthfromdnssec = true;
	view->trust_anchor_telemetry = true;
	view->root_key_sentinel = true;
242
	view->new_zone_dir = NULL;
243
	view->new_zone_file = NULL;
244
245
	view->new_zone_db = NULL;
	view->new_zone_dbenv = NULL;
246
	view->new_zone_mapsize = 0ULL;
247
248
	view->new_zone_config = NULL;
	view->cfg_destroy = NULL;
Evan Hunt's avatar
Evan Hunt committed
249
250
	view->fail_ttl = 0;
	view->failcache = NULL;
251
	result = dns_badcache_init(view->mctx, DNS_VIEW_FAILCACHESIZE,
252
				   &view->failcache);
253
254
255
	if (result != ISC_R_SUCCESS) {
		goto cleanup_dynkeys;
	}
256
	view->v6bias = 0;
Evan Hunt's avatar
Evan Hunt committed
257
258
	view->dtenv = NULL;
	view->dttypes = 0;
259

260
261
	view->plugins = NULL;
	view->plugins_free = NULL;
262
263
264
	view->hooktable = NULL;
	view->hooktable_free = NULL;

Ondřej Surý's avatar
Ondřej Surý committed
265
	isc_mutex_init(&view->new_zone_lock);
266

Evan Hunt's avatar
Evan Hunt committed
267
268
269
	result = dns_order_create(view->mctx, &view->order);
	if (result != ISC_R_SUCCESS) {
		goto cleanup_new_zone_lock;
270
	}
271

272
	result = dns_peerlist_new(view->mctx, &view->peers);
273
	if (result != ISC_R_SUCCESS) {
274
		goto cleanup_order;
275
	}
276

277
	result = dns_aclenv_init(view->mctx, &view->aclenv);
278
	if (result != ISC_R_SUCCESS) {
279
		goto cleanup_peerlist;
280
	}
281

Michael Graff's avatar
Michael Graff committed
282
	ISC_LINK_INIT(view, link);
Andreas Gustafsson's avatar
Andreas Gustafsson committed
283
	ISC_EVENT_INIT(&view->resevent, sizeof(view->resevent), 0, NULL,
284
285
		       DNS_EVENT_VIEWRESSHUTDOWN, resolver_shutdown, view, NULL,
		       NULL, NULL);
Andreas Gustafsson's avatar
Andreas Gustafsson committed
286
	ISC_EVENT_INIT(&view->adbevent, sizeof(view->adbevent), 0, NULL,
287
288
		       DNS_EVENT_VIEWADBSHUTDOWN, adb_shutdown, view, NULL,
		       NULL, NULL);
Andreas Gustafsson's avatar
Andreas Gustafsson committed
289
	ISC_EVENT_INIT(&view->reqevent, sizeof(view->reqevent), 0, NULL,
290
291
		       DNS_EVENT_VIEWREQSHUTDOWN, req_shutdown, view, NULL,
		       NULL, NULL);
292
	view->viewlist = NULL;
Bob Halley's avatar
add    
Bob Halley committed
293
	view->magic = DNS_VIEW_MAGIC;
294

Bob Halley's avatar
add    
Bob Halley committed
295
296
297
298
	*viewp = view;

	return (ISC_R_SUCCESS);

299
cleanup_peerlist:
300
	if (view->peers != NULL) {
301
		dns_peerlist_detach(&view->peers);
302
	}
303

304
cleanup_order:
305
	if (view->order != NULL) {
306
		dns_order_detach(&view->order);
307
	}
308

309
cleanup_new_zone_lock:
310
	isc_mutex_destroy(&view->new_zone_lock);
311

312
313
	dns_badcache_destroy(&view->failcache);

314
cleanup_dynkeys:
315
	if (view->dynamickeys != NULL) {
316
		dns_tsigkeyring_detach(&view->dynamickeys);
317
	}
318

319
cleanup_weakrefs:
320
	isc_refcount_decrementz(&view->weakrefs);
321
322
	isc_refcount_destroy(&view->weakrefs);

323
	isc_refcount_decrementz(&view->references);
324
325
	isc_refcount_destroy(&view->references);

326
	if (view->fwdtable != NULL) {
327
		dns_fwdtable_destroy(&view->fwdtable);
328
	}
329

330
cleanup_zt:
331
	if (view->zonetable != NULL) {
332
		dns_zt_detach(&view->zonetable);
333
	}
Bob Halley's avatar
Bob Halley committed
334

335
cleanup_mutex:
336
	isc_mutex_destroy(&view->lock);
Bob Halley's avatar
add    
Bob Halley committed
337

338
	if (view->nta_file != NULL) {
339
		isc_mem_free(mctx, view->nta_file);
340
	}
341

342
cleanup_name:
Bob Halley's avatar
add    
Bob Halley committed
343
	isc_mem_free(mctx, view->name);
344
	isc_mem_putanddetach(&view->mctx, view, sizeof(*view));
Bob Halley's avatar
add    
Bob Halley committed
345
346
347
348
349

	return (result);
}

static inline void
Evan Hunt's avatar
Evan Hunt committed
350
destroy(dns_view_t *view) {
351
	dns_dns64_t *dns64;
Evan Hunt's avatar
Evan Hunt committed
352
	dns_dlzdb_t *dlzdb;
353

Bob Halley's avatar
add    
Bob Halley committed
354
	REQUIRE(!ISC_LINK_LINKED(view, link));
355
356
	REQUIRE(RESSHUTDOWN(view));
	REQUIRE(ADBSHUTDOWN(view));
Mark Andrews's avatar
Mark Andrews committed
357
	REQUIRE(REQSHUTDOWN(view));
Bob Halley's avatar
add    
Bob Halley committed
358

359
360
361
	isc_refcount_destroy(&view->references);
	isc_refcount_destroy(&view->weakrefs);

362
	if (view->order != NULL) {
363
		dns_order_detach(&view->order);
364
365
	}
	if (view->peers != NULL) {
366
		dns_peerlist_detach(&view->peers);
367
	}
368
369
370

	if (view->dynamickeys != NULL) {
		isc_result_t result;
371
		char template[PATH_MAX];
Evan Hunt's avatar
Evan Hunt committed
372
		char keyfile[PATH_MAX];
373
		FILE *fp = NULL;
374
375
376
377

		result = isc_file_mktemplate(NULL, template, sizeof(template));
		if (result == ISC_R_SUCCESS) {
			(void)isc_file_openuniqueprivate(template, &fp);
378
		}
379
		if (fp == NULL) {
380
			dns_tsigkeyring_detach(&view->dynamickeys);
381
		} else {
382
383
			result = dns_tsigkeyring_dumpanddetach(
				&view->dynamickeys, fp);
384
			if (result == ISC_R_SUCCESS) {
385
				if (fclose(fp) == 0) {
386
387
388
					result = isc_file_sanitize(
						NULL, view->name, "tsigkeys",
						keyfile, sizeof(keyfile));
389
					if (result == ISC_R_SUCCESS) {
390
391
						result = isc_file_rename(
							template, keyfile);
392
					}
393
				}
394
				if (result != ISC_R_SUCCESS) {
395
					(void)remove(template);
396
				}
397
398
399
400
401
402
			} else {
				(void)fclose(fp);
				(void)remove(template);
			}
		}
	}
Ondřej Surý's avatar
Ondřej Surý committed
403
404
405
	if (view->transports != NULL) {
		dns_transport_list_detach(&view->transports);
	}
406
	if (view->statickeys != NULL) {
407
		dns_tsigkeyring_detach(&view->statickeys);
408
409
	}
	if (view->adb != NULL) {
Bob Halley's avatar
add adb    
Bob Halley committed
410
		dns_adb_detach(&view->adb);
411
412
	}
	if (view->resolver != NULL) {
Bob Halley's avatar
add    
Bob Halley committed
413
		dns_resolver_detach(&view->resolver);
414
	}
Evan Hunt's avatar
Evan Hunt committed
415
	dns_rrl_view_destroy(view);
416
	if (view->rpzs != NULL) {
417
		dns_rpz_detach_rpzs(&view->rpzs);
418
419
	}
	if (view->catzs != NULL) {
420
		dns_catz_catzs_detach(&view->catzs);
421
	}
422
	for (dlzdb = ISC_LIST_HEAD(view->dlz_searched); dlzdb != NULL;
Evan Hunt's avatar
Evan Hunt committed
423
424
	     dlzdb = ISC_LIST_HEAD(view->dlz_searched))
	{
Evan Hunt's avatar
Evan Hunt committed
425
426
427
		ISC_LIST_UNLINK(view->dlz_searched, dlzdb, link);
		dns_dlzdestroy(&dlzdb);
	}
428
	for (dlzdb = ISC_LIST_HEAD(view->dlz_unsearched); dlzdb != NULL;
Evan Hunt's avatar
Evan Hunt committed
429
430
	     dlzdb = ISC_LIST_HEAD(view->dlz_unsearched))
	{
Evan Hunt's avatar
Evan Hunt committed
431
432
433
		ISC_LIST_UNLINK(view->dlz_unsearched, dlzdb, link);
		dns_dlzdestroy(&dlzdb);
	}
434
	if (view->requestmgr != NULL) {
Mark Andrews's avatar
Mark Andrews committed
435
		dns_requestmgr_detach(&view->requestmgr);
436
437
	}
	if (view->task != NULL) {
438
		isc_task_detach(&view->task);
439
440
	}
	if (view->hints != NULL) {
Bob Halley's avatar
Bob Halley committed
441
		dns_db_detach(&view->hints);
442
443
	}
	if (view->cachedb != NULL) {
Bob Halley's avatar
Bob Halley committed
444
		dns_db_detach(&view->cachedb);
445
446
	}
	if (view->cache != NULL) {
447
		dns_cache_detach(&view->cache);
448
449
	}
	if (view->nocasecompress != NULL) {
Evan Hunt's avatar
Evan Hunt committed
450
		dns_acl_detach(&view->nocasecompress);
451
452
	}
	if (view->matchclients != NULL) {
453
		dns_acl_detach(&view->matchclients);
454
455
	}
	if (view->matchdestinations != NULL) {
456
		dns_acl_detach(&view->matchdestinations);
457
458
	}
	if (view->cacheacl != NULL) {
459
		dns_acl_detach(&view->cacheacl);
460
461
	}
	if (view->cacheonacl != NULL) {
462
		dns_acl_detach(&view->cacheonacl);
463
464
	}
	if (view->queryacl != NULL) {
465
		dns_acl_detach(&view->queryacl);
466
467
	}
	if (view->queryonacl != NULL) {
468
		dns_acl_detach(&view->queryonacl);
469
470
	}
	if (view->recursionacl != NULL) {
471
		dns_acl_detach(&view->recursionacl);
472
473
	}
	if (view->recursiononacl != NULL) {
474
		dns_acl_detach(&view->recursiononacl);
475
476
	}
	if (view->sortlist != NULL) {
477
		dns_acl_detach(&view->sortlist);
478
479
	}
	if (view->transferacl != NULL) {
480
		dns_acl_detach(&view->transferacl);
481
482
	}
	if (view->notifyacl != NULL) {
483
		dns_acl_detach(&view->notifyacl);
484
485
	}
	if (view->updateacl != NULL) {
486
		dns_acl_detach(&view->updateacl);
487
488
	}
	if (view->upfwdacl != NULL) {
489
		dns_acl_detach(&view->upfwdacl);
490
491
	}
	if (view->denyansweracl != NULL) {
492
		dns_acl_detach(&view->denyansweracl);
493
494
	}
	if (view->pad_acl != NULL) {
495
		dns_acl_detach(&view->pad_acl);
496
497
	}
	if (view->answeracl_exclude != NULL) {
498
		dns_rbt_destroy(&view->answeracl_exclude);
499
500
	}
	if (view->denyanswernames != NULL) {
501
		dns_rbt_destroy(&view->denyanswernames);
502
503
	}
	if (view->answernames_exclude != NULL) {
504
		dns_rbt_destroy(&view->answernames_exclude);
505
	}
506
507
	if (view->delonly != NULL) {
		dns_name_t *name;
Evan Hunt's avatar
Evan Hunt committed
508
		int i;
509
510
511
512
513
514
515
516
517
518

		for (i = 0; i < DNS_VIEW_DELONLYHASH; i++) {
			name = ISC_LIST_HEAD(view->delonly[i]);
			while (name != NULL) {
				ISC_LIST_UNLINK(view->delonly[i], name, link);
				dns_name_free(name, view->mctx);
				isc_mem_put(view->mctx, name, sizeof(*name));
				name = ISC_LIST_HEAD(view->delonly[i]);
			}
		}
519
520
		isc_mem_put(view->mctx, view->delonly,
			    sizeof(dns_namelist_t) * DNS_VIEW_DELONLYHASH);
521
522
		view->delonly = NULL;
	}
Mark Andrews's avatar
Mark Andrews committed
523
524
	if (view->rootexclude != NULL) {
		dns_name_t *name;
Evan Hunt's avatar
Evan Hunt committed
525
		int i;
Mark Andrews's avatar
Mark Andrews committed
526
527
528
529

		for (i = 0; i < DNS_VIEW_DELONLYHASH; i++) {
			name = ISC_LIST_HEAD(view->rootexclude[i]);
			while (name != NULL) {
530
531
				ISC_LIST_UNLINK(view->rootexclude[i], name,
						link);
Mark Andrews's avatar
Mark Andrews committed
532
533
534
535
536
537
538
539
540
				dns_name_free(name, view->mctx);
				isc_mem_put(view->mctx, name, sizeof(*name));
				name = ISC_LIST_HEAD(view->rootexclude[i]);
			}
		}
		isc_mem_put(view->mctx, view->rootexclude,
			    sizeof(dns_namelist_t) * DNS_VIEW_DELONLYHASH);
		view->rootexclude = NULL;
	}
541
	if (view->adbstats != NULL) {
542
		isc_stats_detach(&view->adbstats);
543
544
	}
	if (view->resstats != NULL) {
545
		isc_stats_detach(&view->resstats);
546
547
	}
	if (view->resquerystats != NULL) {
548
		dns_stats_detach(&view->resquerystats);
549
550
	}
	if (view->secroots_priv != NULL) {
551
		dns_keytable_detach(&view->secroots_priv);
552
553
	}
	if (view->ntatable_priv != NULL) {
Evan Hunt's avatar
Evan Hunt committed
554
		dns_ntatable_detach(&view->ntatable_priv);
555
	}
556
	for (dns64 = ISC_LIST_HEAD(view->dns64); dns64 != NULL;
Evan Hunt's avatar
Evan Hunt committed
557
558
	     dns64 = ISC_LIST_HEAD(view->dns64))
	{
559
560
561
		dns_dns64_unlink(&view->dns64, dns64);
		dns_dns64_destroy(&dns64);
	}
562
	if (view->managed_keys != NULL) {
563
		dns_zone_detach(&view->managed_keys);
564
565
	}
	if (view->redirect != NULL) {
566
		dns_zone_detach(&view->redirect);
567
	}
Evan Hunt's avatar
Evan Hunt committed
568
#ifdef HAVE_DNSTAP
569
	if (view->dtenv != NULL) {
Evan Hunt's avatar
Evan Hunt committed
570
		dns_dt_detach(&view->dtenv);
571
	}
Evan Hunt's avatar
Evan Hunt committed
572
#endif /* HAVE_DNSTAP */
573
	dns_view_setnewzones(view, false, NULL, NULL, 0ULL);
574
575
576
577
	if (view->new_zone_file != NULL) {
		isc_mem_free(view->mctx, view->new_zone_file);
		view->new_zone_file = NULL;
	}
578
579
580
581
	if (view->new_zone_dir != NULL) {
		isc_mem_free(view->mctx, view->new_zone_dir);
		view->new_zone_dir = NULL;
	}
582
#ifdef HAVE_LMDB
583
	if (view->new_zone_dbenv != NULL) {
584
		mdb_env_close((MDB_env *)view->new_zone_dbenv);
585
586
		view->new_zone_dbenv = NULL;
	}
587
588
589
590
591
	if (view->new_zone_db != NULL) {
		isc_mem_free(view->mctx, view->new_zone_db);
		view->new_zone_db = NULL;
	}
#endif /* HAVE_LMDB */
592
	dns_fwdtable_destroy(&view->fwdtable);
593
	dns_aclenv_destroy(&view->aclenv);
594
	if (view->failcache != NULL) {
595
		dns_badcache_destroy(&view->failcache);
596
	}
597
598
	isc_mutex_destroy(&view->new_zone_lock);
	isc_mutex_destroy(&view->lock);
599
600
	isc_refcount_destroy(&view->references);
	isc_refcount_destroy(&view->weakrefs);
601
	isc_mem_free(view->mctx, view->nta_file);
Bob Halley's avatar
add    
Bob Halley committed
602
	isc_mem_free(view->mctx, view->name);
603
604
605
	if (view->hooktable != NULL && view->hooktable_free != NULL) {
		view->hooktable_free(view->mctx, &view->hooktable);
	}
606
607
	if (view->plugins != NULL && view->plugins_free != NULL) {
		view->plugins_free(view->mctx, &view->plugins);
608
	}
609
	isc_mem_putanddetach(&view->mctx, view, sizeof(*view));
Bob Halley's avatar
add    
Bob Halley committed
610
611
}

612
void
Evan Hunt's avatar
Evan Hunt committed
613
dns_view_attach(dns_view_t *source, dns_view_t **targetp) {
614
615
616
	REQUIRE(DNS_VIEW_VALID(source));
	REQUIRE(targetp != NULL && *targetp == NULL);

617
	isc_refcount_increment(&source->references);
618
619
620
621

	*targetp = source;
}

622
static void
Evan Hunt's avatar
Evan Hunt committed
623
view_flushanddetach(dns_view_t **viewp, bool flush) {
624
625
626
	REQUIRE(viewp != NULL && DNS_VIEW_VALID(*viewp));
	dns_view_t *view = *viewp;
	*viewp = NULL;
Bob Halley's avatar
add    
Bob Halley committed
627

628
629
630
	if (flush) {
		view->flush = flush;
	}
Bob Halley's avatar
add    
Bob Halley committed
631

632
	if (isc_refcount_decrement(&view->references) == 1) {
Evan Hunt's avatar
Evan Hunt committed
633
634
		dns_zone_t *mkzone = NULL, *rdzone = NULL;

635
636
		isc_refcount_destroy(&view->references);
		if (!RESSHUTDOWN(view)) {
637
			dns_resolver_shutdown(view->resolver);
638
639
		}
		if (!ADBSHUTDOWN(view)) {
640
			dns_adb_shutdown(view->adb);
641
642
		}
		if (!REQSHUTDOWN(view)) {
Mark Andrews's avatar
Mark Andrews committed
643
			dns_requestmgr_shutdown(view->requestmgr);
644
		}
Mark Andrews's avatar
Mark Andrews committed
645
		LOCK(&view->lock);
646
		if (view->zonetable != NULL) {
647
			if (view->flush) {
648
				dns_zt_flushanddetach(&view->zonetable);
649
			} else {
650
				dns_zt_detach(&view->zonetable);
651
			}
652
		}
653
		if (view->managed_keys != NULL) {
Evan Hunt's avatar
Evan Hunt committed
654
655
			mkzone = view->managed_keys;
			view->managed_keys = NULL;
656
			if (view->flush) {
Evan Hunt's avatar
Evan Hunt committed
657
				dns_zone_flush(mkzone);
658
			}
659
		}
660
		if (view->redirect != NULL) {
Evan Hunt's avatar
Evan Hunt committed
661
662
			rdzone = view->redirect;
			view->redirect = NULL;
663
			if (view->flush) {
Evan Hunt's avatar
Evan Hunt committed
664
				dns_zone_flush(rdzone);
665
			}
666
		}
667
668
669
		if (view->catzs != NULL) {
			dns_catz_catzs_detach(&view->catzs);
		}
670
671
672
		if (view->ntatable_priv != NULL) {
			dns_ntatable_shutdown(view->ntatable_priv);
		}
673
		UNLOCK(&view->lock);
Evan Hunt's avatar
Evan Hunt committed
674
675

		/* Need to detach zones outside view lock */
676
		if (mkzone != NULL) {
Evan Hunt's avatar
Evan Hunt committed
677
			dns_zone_detach(&mkzone);
678
		}
Evan Hunt's avatar
Evan Hunt committed
679

680
		if (rdzone != NULL) {
Evan Hunt's avatar
Evan Hunt committed
681
			dns_zone_detach(&rdzone);
682
		}
Bob Halley's avatar
add    
Bob Halley committed
683

684
		dns_view_weakdetach(&view);
685
	}
686
687
}

688
void
Evan Hunt's avatar
Evan Hunt committed
689
dns_view_flushanddetach(dns_view_t **viewp) {
690
	view_flushanddetach(viewp, true);
691
692
693
}

void
Evan Hunt's avatar
Evan Hunt committed
694
dns_view_detach(dns_view_t **viewp) {
695
	view_flushanddetach(viewp, false);
696
697
}

Mark Andrews's avatar
Mark Andrews committed
698
static isc_result_t
Evan Hunt's avatar
Evan Hunt committed
699
dialup(dns_zone_t *zone, void *dummy) {
700
	UNUSED(dummy);
Mark Andrews's avatar
Mark Andrews committed
701
702
703
704
705
	dns_zone_dialup(zone);
	return (ISC_R_SUCCESS);
}

void
Evan Hunt's avatar
Evan Hunt committed
706
dns_view_dialup(dns_view_t *view) {
Mark Andrews's avatar
Mark Andrews committed
707
	REQUIRE(DNS_VIEW_VALID(view));
708
709
	REQUIRE(view->zonetable != NULL);

710
	(void)dns_zt_apply(view->zonetable, false, NULL, dialup, NULL);
Mark Andrews's avatar
Mark Andrews committed
711
712
}

713
void
Evan Hunt's avatar
Evan Hunt committed
714
dns_view_weakattach(dns_view_t *source, dns_view_t **targetp) {
715
716
717
	REQUIRE(DNS_VIEW_VALID(source));
	REQUIRE(targetp != NULL && *targetp == NULL);

718
	isc_refcount_increment(&source->weakrefs);
719
720
721
722
723

	*targetp = source;
}

void
Evan Hunt's avatar
Evan Hunt committed
724
dns_view_weakdetach(dns_view_t **viewp) {
725
726
727
728
729
	dns_view_t *view;

	REQUIRE(viewp != NULL);
	view = *viewp;
	*viewp = NULL;
730
	REQUIRE(DNS_VIEW_VALID(view));
731

732
	if (isc_refcount_decrement(&view->weakrefs) == 1) {
733
		destroy(view);
734
	}
735
736
}

737
static void
Evan Hunt's avatar
Evan Hunt committed
738
resolver_shutdown(isc_task_t *task, isc_event_t *event) {
739
	dns_view_t *view = event->ev_arg;
740

741
	REQUIRE(event->ev_type == DNS_EVENT_VIEWRESSHUTDOWN);
742
743
744
	REQUIRE(DNS_VIEW_VALID(view));
	REQUIRE(view->task == task);

745
	UNUSED(task);
746

747
748
	isc_event_free(&event);

749
	atomic_fetch_or(&view->attributes, DNS_VIEWATTR_RESSHUTDOWN);
750
	dns_view_weakdetach(&view);
751
752
753
}

static void
Evan Hunt's avatar
Evan Hunt committed
754
adb_shutdown(isc_task_t *task, isc_event_t *event) {
755
	dns_view_t *view = event->ev_arg;
756