2 * Copyright (C) 2004-2009 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: view.h,v 1.111.88.4 2009/01/29 22:40:35 jinmei Exp $ */
31 * A "view" is a DNS namespace, together with an optional resolver and a
32 * forwarding policy. A "DNS namespace" is a (possibly empty) set of
33 * authoritative zones together with an optional cache and optional
34 * "hints" information.
36 * Views start out "unfrozen". In this state, core attributes like
37 * the cache, set of zones, and forwarding policy may be set. While
38 * "unfrozen", the caller (e.g. nameserver configuration loading
39 * code), must ensure exclusive access to the view. When the view is
40 * "frozen", the core attributes become immutable, and the view module
41 * will ensure synchronization. Freezing allows the view's core attributes
42 * to be accessed without locking.
45 *\li Before the view is frozen, the caller must ensure synchronization.
47 *\li After the view is frozen, the module guarantees appropriate
48 * synchronization of any data structures it creates and manipulates.
51 *\li No anticipated impact.
57 *\li No anticipated impact.
66 #include <isc/magic.h>
67 #include <isc/event.h>
68 #include <isc/mutex.h>
70 #include <isc/refcount.h>
71 #include <isc/rwlock.h>
72 #include <isc/stdtime.h>
75 #include <dns/fixedname.h>
76 #include <dns/types.h>
84 dns_rdataclass_t rdclass;
87 dns_dlzdb_t * dlzdatabase;
88 dns_resolver_t * resolver;
90 dns_requestmgr_t * requestmgr;
91 dns_acache_t * acache;
95 dns_keytable_t * secroots;
96 dns_keytable_t * trustedkeys;
100 isc_event_t resevent;
101 isc_event_t adbevent;
102 isc_event_t reqevent;
103 isc_stats_t * resstats;
104 dns_stats_t * resquerystats;
106 /* Configurable data. */
107 dns_tsig_keyring_t * statickeys;
108 dns_tsig_keyring_t * dynamickeys;
109 dns_peerlist_t * peers;
111 dns_fwdtable_t * fwdtable;
112 isc_boolean_t recursion;
113 isc_boolean_t auth_nxdomain;
114 isc_boolean_t additionalfromcache;
115 isc_boolean_t additionalfromauth;
116 isc_boolean_t minimalresponses;
117 isc_boolean_t enablednssec;
118 isc_boolean_t enablevalidation;
119 isc_boolean_t acceptexpired;
120 dns_transfer_format_t transfer_format;
121 dns_acl_t * queryacl;
122 dns_acl_t * queryonacl;
123 dns_acl_t * recursionacl;
124 dns_acl_t * recursiononacl;
125 dns_acl_t * sortlist;
126 dns_acl_t * notifyacl;
127 dns_acl_t * transferacl;
128 dns_acl_t * updateacl;
129 dns_acl_t * upfwdacl;
130 isc_boolean_t requestixfr;
131 isc_boolean_t provideixfr;
132 isc_boolean_t requestnsid;
133 dns_ttl_t maxcachettl;
134 dns_ttl_t maxncachettl;
137 dns_rdatatype_t preferred_glue;
139 dns_namelist_t * delonly;
140 isc_boolean_t rootdelonly;
141 dns_namelist_t * rootexclude;
142 isc_boolean_t checknames;
144 dns_fixedname_t dlv_fixed;
148 * Configurable data for server use only,
149 * locked by server configuration lock.
151 dns_acl_t * matchclients;
152 dns_acl_t * matchdestinations;
153 isc_boolean_t matchrecursiveonly;
155 /* Locked by themselves. */
156 isc_refcount_t references;
158 /* Locked by lock. */
159 unsigned int weakrefs;
160 unsigned int attributes;
161 /* Under owner's locking control. */
162 ISC_LINK(struct dns_view) link;
165 #define DNS_VIEW_MAGIC ISC_MAGIC('V','i','e','w')
166 #define DNS_VIEW_VALID(view) ISC_MAGIC_VALID(view, DNS_VIEW_MAGIC)
168 #define DNS_VIEWATTR_RESSHUTDOWN 0x01
169 #define DNS_VIEWATTR_ADBSHUTDOWN 0x02
170 #define DNS_VIEWATTR_REQSHUTDOWN 0x04
173 dns_view_create(isc_mem_t *mctx, dns_rdataclass_t rdclass,
174 const char *name, dns_view_t **viewp);
180 *\li The newly created view has no cache, no resolver, and an empty
181 * zone table. The view is not frozen.
185 *\li 'mctx' is a valid memory context.
187 *\li 'rdclass' is a valid class.
189 *\li 'name' is a valid C string.
191 *\li viewp != NULL && *viewp == NULL
198 *\li Other errors are possible.
202 dns_view_attach(dns_view_t *source, dns_view_t **targetp);
204 * Attach '*targetp' to 'source'.
208 *\li 'source' is a valid, frozen view.
210 *\li 'targetp' points to a NULL dns_view_t *.
214 *\li *targetp is attached to source.
216 *\li While *targetp is attached, the view will not shut down.
220 dns_view_detach(dns_view_t **viewp);
222 * Detach '*viewp' from its view.
226 *\li 'viewp' points to a valid dns_view_t *
234 dns_view_flushanddetach(dns_view_t **viewp);
236 * Detach '*viewp' from its view. If this was the last reference
237 * uncommitted changed in zones will be flushed to disk.
241 *\li 'viewp' points to a valid dns_view_t *
249 dns_view_weakattach(dns_view_t *source, dns_view_t **targetp);
251 * Weakly attach '*targetp' to 'source'.
255 *\li 'source' is a valid, frozen view.
257 *\li 'targetp' points to a NULL dns_view_t *.
261 *\li *targetp is attached to source.
263 * \li While *targetp is attached, the view will not be freed.
267 dns_view_weakdetach(dns_view_t **targetp);
269 * Detach '*viewp' from its view.
273 *\li 'viewp' points to a valid dns_view_t *.
281 dns_view_createresolver(dns_view_t *view,
282 isc_taskmgr_t *taskmgr, unsigned int ntasks,
283 isc_socketmgr_t *socketmgr,
284 isc_timermgr_t *timermgr,
285 unsigned int options,
286 dns_dispatchmgr_t *dispatchmgr,
287 dns_dispatch_t *dispatchv4,
288 dns_dispatch_t *dispatchv6);
290 * Create a resolver and address database for the view.
294 *\li 'view' is a valid, unfrozen view.
296 *\li 'view' does not have a resolver already.
298 *\li The requirements of dns_resolver_create() apply to 'taskmgr',
299 * 'ntasks', 'socketmgr', 'timermgr', 'options', 'dispatchv4', and
306 *\li Any error that dns_resolver_create() can return.
310 dns_view_setcache(dns_view_t *view, dns_cache_t *cache);
312 * Set the view's cache database.
316 *\li 'view' is a valid, unfrozen view.
318 *\li 'cache' is a valid cache.
322 * \li The cache of 'view' is 'cached.
324 *\li If this is not the first call to dns_view_setcache() for this
325 * view, then previously set cache is detached.
329 dns_view_sethints(dns_view_t *view, dns_db_t *hints);
331 * Set the view's hints database.
335 *\li 'view' is a valid, unfrozen view, whose hints database has not been
338 *\li 'hints' is a valid zone database.
342 * \li The hints database of 'view' is 'hints'.
346 dns_view_setkeyring(dns_view_t *view, dns_tsig_keyring_t *ring);
348 * Set the view's static TSIG keys
352 * \li 'view' is a valid, unfrozen view, whose static TSIG keyring has not
355 *\li 'ring' is a valid TSIG keyring
359 *\li The static TSIG keyring of 'view' is 'ring'.
363 dns_view_setdstport(dns_view_t *view, in_port_t dstport);
365 * Set the view's destination port. This is the port to
366 * which outgoing queries are sent. The default is 53,
367 * the standard DNS port.
371 *\li 'view' is a valid view.
373 *\li 'dstport' is a valid TCP/UDP port number.
376 *\li External name servers will be assumed to be listening
377 * on 'dstport'. For servers whose address has already
378 * obtained obtained at the time of the call, the view may
379 * continue to use the previously set port until the address
380 * times out from the view's address database.
385 dns_view_addzone(dns_view_t *view, dns_zone_t *zone);
387 * Add zone 'zone' to 'view'.
391 *\li 'view' is a valid, unfrozen view.
393 *\li 'zone' is a valid zone.
397 dns_view_freeze(dns_view_t *view);
403 *\li 'view' is a valid, unfrozen view.
407 *\li 'view' is frozen.
411 dns_view_find(dns_view_t *view, dns_name_t *name, dns_rdatatype_t type,
412 isc_stdtime_t now, unsigned int options, isc_boolean_t use_hints,
413 dns_db_t **dbp, dns_dbnode_t **nodep, dns_name_t *foundname,
414 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset);
416 * Find an rdataset whose owner name is 'name', and whose type is
421 *\li See the description of dns_db_find() for information about 'options'.
422 * If the caller sets #DNS_DBFIND_GLUEOK, it must ensure that 'name'
423 * and 'type' are appropriate for glue retrieval.
425 *\li If 'now' is zero, then the current time will be used.
427 *\li If 'use_hints' is ISC_TRUE, and the view has a hints database, then
428 * it will be searched last. If the answer is found in the hints
429 * database, the result code will be DNS_R_HINT. If the name is found
430 * in the hints database but not the type, the result code will be
431 * #DNS_R_HINTNXRRSET.
433 *\li 'foundname' must meet the requirements of dns_db_find().
435 *\li If 'sigrdataset' is not NULL, and there is a SIG rdataset which
436 * covers 'type', then 'sigrdataset' will be bound to it.
440 *\li 'view' is a valid, frozen view.
442 *\li 'name' is valid name.
444 *\li 'type' is a valid dns_rdatatype_t, and is not a meta query type
445 * except dns_rdatatype_any.
447 *\li dbp == NULL || *dbp == NULL
449 *\li nodep == NULL || *nodep == NULL. If nodep != NULL, dbp != NULL.
451 *\li 'foundname' is a valid name with a dedicated buffer or NULL.
453 *\li 'rdataset' is a valid, disassociated rdataset.
455 *\li 'sigrdataset' is NULL, or is a valid, disassociated rdataset.
459 *\li In successful cases, 'rdataset', and possibly 'sigrdataset', are
460 * bound to the found data.
462 *\li If dbp != NULL, it points to the database containing the data.
464 *\li If nodep != NULL, it points to the database node containing the data.
466 *\li If foundname != NULL, it contains the full name of the found data.
470 *\li Any result that dns_db_find() can return, with the exception of
475 dns_view_simplefind(dns_view_t *view, dns_name_t *name, dns_rdatatype_t type,
476 isc_stdtime_t now, unsigned int options,
477 isc_boolean_t use_hints,
478 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset);
480 * Find an rdataset whose owner name is 'name', and whose type is
485 *\li This routine is appropriate for simple, exact-match queries of the
486 * view. 'name' must be a canonical name; there is no DNAME or CNAME
489 *\li See the description of dns_db_find() for information about 'options'.
490 * If the caller sets DNS_DBFIND_GLUEOK, it must ensure that 'name'
491 * and 'type' are appropriate for glue retrieval.
493 *\li If 'now' is zero, then the current time will be used.
495 *\li If 'use_hints' is ISC_TRUE, and the view has a hints database, then
496 * it will be searched last. If the answer is found in the hints
497 * database, the result code will be DNS_R_HINT. If the name is found
498 * in the hints database but not the type, the result code will be
501 *\li If 'sigrdataset' is not NULL, and there is a SIG rdataset which
502 * covers 'type', then 'sigrdataset' will be bound to it.
506 *\li 'view' is a valid, frozen view.
508 *\li 'name' is valid name.
510 *\li 'type' is a valid dns_rdatatype_t, and is not a meta query type
511 * (e.g. dns_rdatatype_any), or dns_rdatatype_rrsig.
513 *\li 'rdataset' is a valid, disassociated rdataset.
515 *\li 'sigrdataset' is NULL, or is a valid, disassociated rdataset.
519 *\li In successful cases, 'rdataset', and possibly 'sigrdataset', are
520 * bound to the found data.
524 *\li #ISC_R_SUCCESS Success; result is desired type.
525 *\li DNS_R_GLUE Success; result is glue.
526 *\li DNS_R_HINT Success; result is a hint.
527 *\li DNS_R_NCACHENXDOMAIN Success; result is a ncache entry.
528 *\li DNS_R_NCACHENXRRSET Success; result is a ncache entry.
529 *\li DNS_R_NXDOMAIN The name does not exist.
530 *\li DNS_R_NXRRSET The rrset does not exist.
531 *\li #ISC_R_NOTFOUND No matching data found,
532 * or an error occurred.
535 /*% See dns_view_findzonecut2() */
537 dns_view_findzonecut(dns_view_t *view, dns_name_t *name, dns_name_t *fname,
538 isc_stdtime_t now, unsigned int options,
539 isc_boolean_t use_hints,
540 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset);
543 dns_view_findzonecut2(dns_view_t *view, dns_name_t *name, dns_name_t *fname,
544 isc_stdtime_t now, unsigned int options,
545 isc_boolean_t use_hints, isc_boolean_t use_cache,
546 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset);
548 * Find the best known zonecut containing 'name'.
550 * This uses local authority, cache, and optionally hints data.
551 * No external queries are performed.
555 *\li If 'now' is zero, then the current time will be used.
557 *\li If 'use_hints' is ISC_TRUE, and the view has a hints database, then
558 * it will be searched last.
560 *\li If 'use_cache' is ISC_TRUE, and the view has a cache, then it will be
563 *\li If 'sigrdataset' is not NULL, and there is a SIG rdataset which
564 * covers 'type', then 'sigrdataset' will be bound to it.
566 *\li If the DNS_DBFIND_NOEXACT option is set, then the zonecut returned
567 * (if any) will be the deepest known ancestor of 'name'.
571 *\li 'view' is a valid, frozen view.
573 *\li 'name' is valid name.
575 *\li 'rdataset' is a valid, disassociated rdataset.
577 *\li 'sigrdataset' is NULL, or is a valid, disassociated rdataset.
581 *\li #ISC_R_SUCCESS Success.
583 *\li Many other results are possible.
587 dns_viewlist_find(dns_viewlist_t *list, const char *name,
588 dns_rdataclass_t rdclass, dns_view_t **viewp);
590 * Search for a view with name 'name' and class 'rdclass' in 'list'.
591 * If found, '*viewp' is (strongly) attached to it.
595 *\li 'viewp' points to a NULL dns_view_t *.
599 *\li #ISC_R_SUCCESS A matching view was found.
600 *\li #ISC_R_NOTFOUND No matching view was found.
604 dns_viewlist_findzone(dns_viewlist_t *list, dns_name_t *name, isc_boolean_t allclasses,
605 dns_rdataclass_t rdclass, dns_zone_t **zonep);
608 * Search zone with 'name' in view with 'rdclass' in viewlist 'list'
609 * If found, zone is returned in *zonep. If allclasses is set rdclass is ignored
612 *\li #ISC_R_SUCCESS A matching zone was found.
613 *\li #ISC_R_NOTFOUND No matching zone was found.
617 dns_view_findzone(dns_view_t *view, dns_name_t *name, dns_zone_t **zonep);
619 * Search for the zone 'name' in the zone table of 'view'.
620 * If found, 'zonep' is (strongly) attached to it. There
621 * are no partial matches.
625 *\li 'zonep' points to a NULL dns_zone_t *.
628 *\li #ISC_R_SUCCESS A matching zone was found.
629 *\li #ISC_R_NOTFOUND No matching zone was found.
630 *\li others An error occurred.
634 dns_view_load(dns_view_t *view, isc_boolean_t stop);
637 dns_view_loadnew(dns_view_t *view, isc_boolean_t stop);
639 * Load zones attached to this view. dns_view_load() loads
640 * all zones whose master file has changed since the last
641 * load; dns_view_loadnew() loads only zones that have never
644 * If 'stop' is ISC_TRUE, stop on the first error and return it.
645 * If 'stop' is ISC_FALSE, ignore errors.
649 *\li 'view' is valid.
653 dns_view_gettsig(dns_view_t *view, dns_name_t *keyname,
654 dns_tsigkey_t **keyp);
656 * Find the TSIG key configured in 'view' with name 'keyname',
660 *\li keyp points to a NULL dns_tsigkey_t *.
663 *\li #ISC_R_SUCCESS A key was found and '*keyp' now points to it.
664 *\li #ISC_R_NOTFOUND No key was found.
665 *\li others An error occurred.
669 dns_view_getpeertsig(dns_view_t *view, isc_netaddr_t *peeraddr,
670 dns_tsigkey_t **keyp);
672 * Find the TSIG key configured in 'view' for the server whose
673 * address is 'peeraddr', if any.
676 * keyp points to a NULL dns_tsigkey_t *.
679 *\li #ISC_R_SUCCESS A key was found and '*keyp' now points to it.
680 *\li #ISC_R_NOTFOUND No key was found.
681 *\li others An error occurred.
685 dns_view_checksig(dns_view_t *view, isc_buffer_t *source, dns_message_t *msg);
687 * Verifies the signature of a message.
691 *\li 'view' is a valid view.
692 *\li 'source' is a valid buffer containing the message
693 *\li 'msg' is a valid message
696 *\li see dns_tsig_verify()
700 dns_view_dialup(dns_view_t *view);
702 * Perform dialup-time maintenance on the zones of 'view'.
706 dns_view_dumpdbtostream(dns_view_t *view, FILE *fp);
708 * Dump the current state of the view 'view' to the stream 'fp'
709 * for purposes of analysis or debugging.
711 * Currently the dumped state includes the view's cache; in the future
712 * it may also include other state such as the address database.
713 * It will not not include authoritative data since it is voluminous and
714 * easily obtainable by other means.
718 *\li 'view' is valid.
720 *\li 'fp' refers to a file open for writing.
723 * \li ISC_R_SUCCESS The cache was successfully dumped.
724 * \li others An error occurred (see dns_master_dump)
728 dns_view_flushcache(dns_view_t *view);
730 * Flush the view's cache (and ADB).
735 * No other tasks are executing.
743 dns_view_flushname(dns_view_t *view, dns_name_t *);
745 * Flush the given name from the view's cache (and ADB).
748 *\li 'view' is valid.
749 *\li 'name' is valid.
753 * other returns are failures.
757 dns_view_adddelegationonly(dns_view_t *view, dns_name_t *name);
759 * Add the given name to the delegation only table.
763 *\li 'view' is valid.
764 *\li 'name' is valid.
772 dns_view_excludedelegationonly(dns_view_t *view, dns_name_t *name);
774 * Add the given name to be excluded from the root-delegation-only.
778 *\li 'view' is valid.
779 *\li 'name' is valid.
787 dns_view_isdelegationonly(dns_view_t *view, dns_name_t *name);
789 * Check if 'name' is in the delegation only table or if
790 * rootdelonly is set that name is not being excluded.
793 *\li 'view' is valid.
794 *\li 'name' is valid.
797 *\li #ISC_TRUE if the name is the table.
798 *\li #ISC_FALSE otherwise.
802 dns_view_setrootdelonly(dns_view_t *view, isc_boolean_t value);
804 * Set the root delegation only flag.
807 *\li 'view' is valid.
811 dns_view_getrootdelonly(dns_view_t *view);
813 * Get the root delegation only flag.
816 *\li 'view' is valid.
820 dns_view_freezezones(dns_view_t *view, isc_boolean_t freeze);
822 * Freeze/thaw updates to master zones.
825 * \li 'view' is valid.
829 dns_view_setresstats(dns_view_t *view, isc_stats_t *stats);
831 * Set a general resolver statistics counter set 'stats' for 'view'.
834 * \li 'view' is valid and is not frozen.
836 *\li stats is a valid statistics supporting resolver statistics counters
841 dns_view_getresstats(dns_view_t *view, isc_stats_t **statsp);
843 * Get the general statistics counter set for 'view'. If a statistics set is
844 * set '*statsp' will be attached to the set; otherwise, '*statsp' will be
848 * \li 'view' is valid and is not frozen.
850 *\li 'statsp' != NULL && '*statsp' != NULL
854 dns_view_setresquerystats(dns_view_t *view, dns_stats_t *stats);
856 * Set a statistics counter set of rdata type, 'stats', for 'view'. Once the
857 * statistic set is installed, view's resolver will count outgoing queries
861 * \li 'view' is valid and is not frozen.
863 *\li stats is a valid statistics created by dns_rdatatypestats_create().
867 dns_view_getresquerystats(dns_view_t *view, dns_stats_t **statsp);
869 * Get the rdatatype statistics counter set for 'view'. If a statistics set is
870 * set '*statsp' will be attached to the set; otherwise, '*statsp' will be
874 * \li 'view' is valid and is not frozen.
876 *\li 'statsp' != NULL && '*statsp' != NULL
879 #endif /* DNS_VIEW_H */