]> CyberLeo.Net >> Repos - FreeBSD/releng/9.3.git/blob - contrib/bind9/lib/dns/resolver.c
Copy stable/9 to releng/9.3 as part of the 9.3-RELEASE cycle.
[FreeBSD/releng/9.3.git] / contrib / bind9 / lib / dns / resolver.c
1 /*
2  * Copyright (C) 2004-2014  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2003  Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17
18 /* $Id$ */
19
20 /*! \file */
21
22 #include <config.h>
23
24 #include <isc/log.h>
25 #include <isc/platform.h>
26 #include <isc/print.h>
27 #include <isc/string.h>
28 #include <isc/random.h>
29 #include <isc/socket.h>
30 #include <isc/stats.h>
31 #include <isc/task.h>
32 #include <isc/timer.h>
33 #include <isc/util.h>
34
35 #include <dns/acl.h>
36 #include <dns/adb.h>
37 #include <dns/cache.h>
38 #include <dns/db.h>
39 #include <dns/dispatch.h>
40 #include <dns/ds.h>
41 #include <dns/events.h>
42 #include <dns/forward.h>
43 #include <dns/keytable.h>
44 #include <dns/log.h>
45 #include <dns/message.h>
46 #include <dns/ncache.h>
47 #include <dns/nsec.h>
48 #include <dns/nsec3.h>
49 #include <dns/opcode.h>
50 #include <dns/peer.h>
51 #include <dns/rbt.h>
52 #include <dns/rcode.h>
53 #include <dns/rdata.h>
54 #include <dns/rdataclass.h>
55 #include <dns/rdatalist.h>
56 #include <dns/rdataset.h>
57 #include <dns/rdatastruct.h>
58 #include <dns/rdatatype.h>
59 #include <dns/resolver.h>
60 #include <dns/result.h>
61 #include <dns/rootns.h>
62 #include <dns/stats.h>
63 #include <dns/tsig.h>
64 #include <dns/validator.h>
65
66 #define DNS_RESOLVER_TRACE
67 #ifdef DNS_RESOLVER_TRACE
68 #define RTRACE(m)       isc_log_write(dns_lctx, \
69                                       DNS_LOGCATEGORY_RESOLVER, \
70                                       DNS_LOGMODULE_RESOLVER, \
71                                       ISC_LOG_DEBUG(3), \
72                                       "res %p: %s", res, (m))
73 #define RRTRACE(r, m)   isc_log_write(dns_lctx, \
74                                       DNS_LOGCATEGORY_RESOLVER, \
75                                       DNS_LOGMODULE_RESOLVER, \
76                                       ISC_LOG_DEBUG(3), \
77                                       "res %p: %s", (r), (m))
78 #define FCTXTRACE(m)    isc_log_write(dns_lctx, \
79                                       DNS_LOGCATEGORY_RESOLVER, \
80                                       DNS_LOGMODULE_RESOLVER, \
81                                       ISC_LOG_DEBUG(3), \
82                                       "fctx %p(%s): %s", fctx, fctx->info, (m))
83 #define FCTXTRACE2(m1, m2) \
84                         isc_log_write(dns_lctx, \
85                                       DNS_LOGCATEGORY_RESOLVER, \
86                                       DNS_LOGMODULE_RESOLVER, \
87                                       ISC_LOG_DEBUG(3), \
88                                       "fctx %p(%s): %s %s", \
89                                       fctx, fctx->info, (m1), (m2))
90 #define FTRACE(m)       isc_log_write(dns_lctx, \
91                                       DNS_LOGCATEGORY_RESOLVER, \
92                                       DNS_LOGMODULE_RESOLVER, \
93                                       ISC_LOG_DEBUG(3), \
94                                       "fetch %p (fctx %p(%s)): %s", \
95                                       fetch, fetch->private, \
96                                       fetch->private->info, (m))
97 #define QTRACE(m)       isc_log_write(dns_lctx, \
98                                       DNS_LOGCATEGORY_RESOLVER, \
99                                       DNS_LOGMODULE_RESOLVER, \
100                                       ISC_LOG_DEBUG(3), \
101                                       "resquery %p (fctx %p(%s)): %s", \
102                                       query, query->fctx, \
103                                       query->fctx->info, (m))
104 #else
105 #define RTRACE(m)
106 #define RRTRACE(r, m)
107 #define FCTXTRACE(m)
108 #define FCTXTRACE2(m1, m2)
109 #define FTRACE(m)
110 #define QTRACE(m)
111 #endif
112
113 #define US_PER_SEC 1000000U
114 /*
115  * The maximum time we will wait for a single query.
116  */
117 #define MAX_SINGLE_QUERY_TIMEOUT 9U
118 #define MAX_SINGLE_QUERY_TIMEOUT_US (MAX_SINGLE_QUERY_TIMEOUT*US_PER_SEC)
119
120 /*
121  * We need to allow a individual query time to complete / timeout.
122  */
123 #define MINIMUM_QUERY_TIMEOUT (MAX_SINGLE_QUERY_TIMEOUT + 1U)
124
125 /* The default time in seconds for the whole query to live. */
126 #ifndef DEFAULT_QUERY_TIMEOUT
127 #define DEFAULT_QUERY_TIMEOUT MINIMUM_QUERY_TIMEOUT
128 #endif
129
130 #ifndef MAXIMUM_QUERY_TIMEOUT
131 #define MAXIMUM_QUERY_TIMEOUT 30 /* The maximum time in seconds for the whole query to live. */
132 #endif
133
134 /*%
135  * Maximum EDNS0 input packet size.
136  */
137 #define RECV_BUFFER_SIZE                4096            /* XXXRTH  Constant. */
138 #define EDNSOPTS                        2
139
140 /*%
141  * This defines the maximum number of timeouts we will permit before we
142  * disable EDNS0 on the query.
143  */
144 #define MAX_EDNS0_TIMEOUTS      3
145
146 typedef struct fetchctx fetchctx_t;
147
148 typedef struct query {
149         /* Locked by task event serialization. */
150         unsigned int                    magic;
151         fetchctx_t *                    fctx;
152         isc_mem_t *                     mctx;
153         dns_dispatchmgr_t *             dispatchmgr;
154         dns_dispatch_t *                dispatch;
155         isc_boolean_t                   exclusivesocket;
156         dns_adbaddrinfo_t *             addrinfo;
157         isc_socket_t *                  tcpsocket;
158         isc_time_t                      start;
159         dns_messageid_t                 id;
160         dns_dispentry_t *               dispentry;
161         ISC_LINK(struct query)          link;
162         isc_buffer_t                    buffer;
163         isc_buffer_t                    *tsig;
164         dns_tsigkey_t                   *tsigkey;
165         isc_socketevent_t               sendevent;
166         unsigned int                    options;
167         unsigned int                    attributes;
168         unsigned int                    sends;
169         unsigned int                    connects;
170         unsigned char                   data[512];
171 } resquery_t;
172
173 #define QUERY_MAGIC                     ISC_MAGIC('Q', '!', '!', '!')
174 #define VALID_QUERY(query)              ISC_MAGIC_VALID(query, QUERY_MAGIC)
175
176 #define RESQUERY_ATTR_CANCELED          0x02
177
178 #define RESQUERY_CONNECTING(q)          ((q)->connects > 0)
179 #define RESQUERY_CANCELED(q)            (((q)->attributes & \
180                                           RESQUERY_ATTR_CANCELED) != 0)
181 #define RESQUERY_SENDING(q)             ((q)->sends > 0)
182
183 typedef enum {
184         fetchstate_init = 0,            /*%< Start event has not run yet. */
185         fetchstate_active,
186         fetchstate_done                 /*%< FETCHDONE events posted. */
187 } fetchstate;
188
189 typedef enum {
190         badns_unreachable = 0,
191         badns_response,
192         badns_validation
193 } badnstype_t;
194
195 struct fetchctx {
196         /*% Not locked. */
197         unsigned int                    magic;
198         dns_resolver_t *                res;
199         dns_name_t                      name;
200         dns_rdatatype_t                 type;
201         unsigned int                    options;
202         unsigned int                    bucketnum;
203         char *                          info;
204         isc_mem_t *                     mctx;
205
206         /*% Locked by appropriate bucket lock. */
207         fetchstate                      state;
208         isc_boolean_t                   want_shutdown;
209         isc_boolean_t                   cloned;
210         isc_boolean_t                   spilled;
211         unsigned int                    references;
212         isc_event_t                     control_event;
213         ISC_LINK(struct fetchctx)       link;
214         ISC_LIST(dns_fetchevent_t)      events;
215         /*% Locked by task event serialization. */
216         dns_name_t                      domain;
217         dns_rdataset_t                  nameservers;
218         unsigned int                    attributes;
219         isc_timer_t *                   timer;
220         isc_time_t                      expires;
221         isc_interval_t                  interval;
222         dns_message_t *                 qmessage;
223         dns_message_t *                 rmessage;
224         ISC_LIST(resquery_t)            queries;
225         dns_adbfindlist_t               finds;
226         dns_adbfind_t *                 find;
227         dns_adbfindlist_t               altfinds;
228         dns_adbfind_t *                 altfind;
229         dns_adbaddrinfolist_t           forwaddrs;
230         dns_adbaddrinfolist_t           altaddrs;
231         isc_sockaddrlist_t              forwarders;
232         dns_fwdpolicy_t                 fwdpolicy;
233         isc_sockaddrlist_t              bad;
234         isc_sockaddrlist_t              edns;
235         isc_sockaddrlist_t              edns512;
236         isc_sockaddrlist_t              bad_edns;
237         dns_validator_t                 *validator;
238         ISC_LIST(dns_validator_t)       validators;
239         dns_db_t *                      cache;
240         dns_adb_t *                     adb;
241         isc_boolean_t                   ns_ttl_ok;
242         isc_uint32_t                    ns_ttl;
243
244         /*%
245          * The number of events we're waiting for.
246          */
247         unsigned int                    pending;
248
249         /*%
250          * The number of times we've "restarted" the current
251          * nameserver set.  This acts as a failsafe to prevent
252          * us from pounding constantly on a particular set of
253          * servers that, for whatever reason, are not giving
254          * us useful responses, but are responding in such a
255          * way that they are not marked "bad".
256          */
257         unsigned int                    restarts;
258
259         /*%
260          * The number of timeouts that have occurred since we
261          * last successfully received a response packet.  This
262          * is used for EDNS0 black hole detection.
263          */
264         unsigned int                    timeouts;
265
266         /*%
267          * Look aside state for DS lookups.
268          */
269         dns_name_t                      nsname;
270         dns_fetch_t *                   nsfetch;
271         dns_rdataset_t                  nsrrset;
272
273         /*%
274          * Number of queries that reference this context.
275          */
276         unsigned int                    nqueries;
277
278         /*%
279          * The reason to print when logging a successful
280          * response to a query.
281          */
282         const char *                    reason;
283
284         /*%
285          * Random numbers to use for mixing up server addresses.
286          */
287         isc_uint32_t                    rand_buf;
288         isc_uint32_t                    rand_bits;
289
290         /*%
291          * Fetch-local statistics for detailed logging.
292          */
293         isc_result_t                    result; /*%< fetch result  */
294         isc_result_t                    vresult; /*%< validation result  */
295         int                             exitline;
296         isc_time_t                      start;
297         isc_uint64_t                    duration;
298         isc_boolean_t                   logged;
299         unsigned int                    querysent;
300         unsigned int                    referrals;
301         unsigned int                    lamecount;
302         unsigned int                    neterr;
303         unsigned int                    badresp;
304         unsigned int                    adberr;
305         unsigned int                    findfail;
306         unsigned int                    valfail;
307         isc_boolean_t                   timeout;
308         dns_adbaddrinfo_t               *addrinfo;
309         isc_sockaddr_t                  *client;
310 };
311
312 #define FCTX_MAGIC                      ISC_MAGIC('F', '!', '!', '!')
313 #define VALID_FCTX(fctx)                ISC_MAGIC_VALID(fctx, FCTX_MAGIC)
314
315 #define FCTX_ATTR_HAVEANSWER            0x0001
316 #define FCTX_ATTR_GLUING                0x0002
317 #define FCTX_ATTR_ADDRWAIT              0x0004
318 #define FCTX_ATTR_SHUTTINGDOWN          0x0008
319 #define FCTX_ATTR_WANTCACHE             0x0010
320 #define FCTX_ATTR_WANTNCACHE            0x0020
321 #define FCTX_ATTR_NEEDEDNS0             0x0040
322 #define FCTX_ATTR_TRIEDFIND             0x0080
323 #define FCTX_ATTR_TRIEDALT              0x0100
324
325 #define HAVE_ANSWER(f)          (((f)->attributes & FCTX_ATTR_HAVEANSWER) != \
326                                  0)
327 #define GLUING(f)               (((f)->attributes & FCTX_ATTR_GLUING) != \
328                                  0)
329 #define ADDRWAIT(f)             (((f)->attributes & FCTX_ATTR_ADDRWAIT) != \
330                                  0)
331 #define SHUTTINGDOWN(f)         (((f)->attributes & FCTX_ATTR_SHUTTINGDOWN) \
332                                  != 0)
333 #define WANTCACHE(f)            (((f)->attributes & FCTX_ATTR_WANTCACHE) != 0)
334 #define WANTNCACHE(f)           (((f)->attributes & FCTX_ATTR_WANTNCACHE) != 0)
335 #define NEEDEDNS0(f)            (((f)->attributes & FCTX_ATTR_NEEDEDNS0) != 0)
336 #define TRIEDFIND(f)            (((f)->attributes & FCTX_ATTR_TRIEDFIND) != 0)
337 #define TRIEDALT(f)             (((f)->attributes & FCTX_ATTR_TRIEDALT) != 0)
338
339 typedef struct {
340         dns_adbaddrinfo_t *             addrinfo;
341         fetchctx_t *                    fctx;
342 } dns_valarg_t;
343
344 struct dns_fetch {
345         unsigned int                    magic;
346         fetchctx_t *                    private;
347 };
348
349 #define DNS_FETCH_MAGIC                 ISC_MAGIC('F', 't', 'c', 'h')
350 #define DNS_FETCH_VALID(fetch)          ISC_MAGIC_VALID(fetch, DNS_FETCH_MAGIC)
351
352 typedef struct fctxbucket {
353         isc_task_t *                    task;
354         isc_mutex_t                     lock;
355         ISC_LIST(fetchctx_t)            fctxs;
356         isc_boolean_t                   exiting;
357         isc_mem_t *                     mctx;
358 } fctxbucket_t;
359
360 typedef struct alternate {
361         isc_boolean_t                   isaddress;
362         union   {
363                 isc_sockaddr_t          addr;
364                 struct {
365                         dns_name_t      name;
366                         in_port_t       port;
367                 } _n;
368         } _u;
369         ISC_LINK(struct alternate)      link;
370 } alternate_t;
371
372 typedef struct dns_badcache dns_badcache_t;
373 struct dns_badcache {
374         dns_badcache_t *        next;
375         dns_rdatatype_t         type;
376         isc_time_t              expire;
377         unsigned int            hashval;
378         dns_name_t              name;
379 };
380 #define DNS_BADCACHE_SIZE 1021
381 #define DNS_BADCACHE_TTL(fctx) \
382         (((fctx)->res->lame_ttl > 30 ) ? (fctx)->res->lame_ttl : 30)
383
384 struct dns_resolver {
385         /* Unlocked. */
386         unsigned int                    magic;
387         isc_mem_t *                     mctx;
388         isc_mutex_t                     lock;
389         isc_mutex_t                     nlock;
390         isc_mutex_t                     primelock;
391         dns_rdataclass_t                rdclass;
392         isc_socketmgr_t *               socketmgr;
393         isc_timermgr_t *                timermgr;
394         isc_taskmgr_t *                 taskmgr;
395         dns_view_t *                    view;
396         isc_boolean_t                   frozen;
397         unsigned int                    options;
398         dns_dispatchmgr_t *             dispatchmgr;
399         dns_dispatchset_t *             dispatches4;
400         isc_boolean_t                   exclusivev4;
401         dns_dispatchset_t *             dispatches6;
402         isc_boolean_t                   exclusivev6;
403         unsigned int                    nbuckets;
404         fctxbucket_t *                  buckets;
405         isc_uint32_t                    lame_ttl;
406         ISC_LIST(alternate_t)           alternates;
407         isc_uint16_t                    udpsize;
408 #if USE_ALGLOCK
409         isc_rwlock_t                    alglock;
410 #endif
411         dns_rbt_t *                     algorithms;
412 #if USE_MBSLOCK
413         isc_rwlock_t                    mbslock;
414 #endif
415         dns_rbt_t *                     mustbesecure;
416         unsigned int                    spillatmax;
417         unsigned int                    spillatmin;
418         isc_timer_t *                   spillattimer;
419         isc_boolean_t                   zero_no_soa_ttl;
420         unsigned int                    query_timeout;
421
422         /* Locked by lock. */
423         unsigned int                    references;
424         isc_boolean_t                   exiting;
425         isc_eventlist_t                 whenshutdown;
426         unsigned int                    activebuckets;
427         isc_boolean_t                   priming;
428         unsigned int                    spillat;        /* clients-per-query */
429
430         /* Bad cache. */
431         dns_badcache_t  **              badcache;
432         unsigned int                    badcount;
433         unsigned int                    badhash;
434         unsigned int                    badsweep;
435
436         /* Locked by primelock. */
437         dns_fetch_t *                   primefetch;
438         /* Locked by nlock. */
439         unsigned int                    nfctx;
440 };
441
442 #define RES_MAGIC                       ISC_MAGIC('R', 'e', 's', '!')
443 #define VALID_RESOLVER(res)             ISC_MAGIC_VALID(res, RES_MAGIC)
444
445 /*%
446  * Private addrinfo flags.  These must not conflict with DNS_FETCHOPT_NOEDNS0,
447  * which we also use as an addrinfo flag.
448  */
449 #define FCTX_ADDRINFO_MARK              0x0001
450 #define FCTX_ADDRINFO_FORWARDER         0x1000
451 #define FCTX_ADDRINFO_TRIED             0x2000
452 #define UNMARKED(a)                     (((a)->flags & FCTX_ADDRINFO_MARK) \
453                                          == 0)
454 #define ISFORWARDER(a)                  (((a)->flags & \
455                                          FCTX_ADDRINFO_FORWARDER) != 0)
456 #define TRIED(a)                        (((a)->flags & \
457                                          FCTX_ADDRINFO_TRIED) != 0)
458
459 #define NXDOMAIN(r) (((r)->attributes & DNS_RDATASETATTR_NXDOMAIN) != 0)
460 #define NEGATIVE(r) (((r)->attributes & DNS_RDATASETATTR_NEGATIVE) != 0)
461
462 static void destroy(dns_resolver_t *res);
463 static void empty_bucket(dns_resolver_t *res);
464 static isc_result_t resquery_send(resquery_t *query);
465 static void resquery_response(isc_task_t *task, isc_event_t *event);
466 static void resquery_connected(isc_task_t *task, isc_event_t *event);
467 static void fctx_try(fetchctx_t *fctx, isc_boolean_t retrying,
468                      isc_boolean_t badcache);
469 static void fctx_destroy(fetchctx_t *fctx);
470 static isc_boolean_t fctx_unlink(fetchctx_t *fctx);
471 static isc_result_t ncache_adderesult(dns_message_t *message,
472                                       dns_db_t *cache, dns_dbnode_t *node,
473                                       dns_rdatatype_t covers,
474                                       isc_stdtime_t now, dns_ttl_t maxttl,
475                                       isc_boolean_t optout,
476                                       isc_boolean_t secure,
477                                       dns_rdataset_t *ardataset,
478                                       isc_result_t *eresultp);
479 static void validated(isc_task_t *task, isc_event_t *event);
480 static isc_boolean_t maybe_destroy(fetchctx_t *fctx, isc_boolean_t locked);
481 static void add_bad(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
482                     isc_result_t reason, badnstype_t badtype);
483 static inline isc_result_t findnoqname(fetchctx_t *fctx, dns_name_t *name,
484                                        dns_rdatatype_t type,
485                                        dns_name_t **noqname);
486
487 /*%
488  * Increment resolver-related statistics counters.
489  */
490 static inline void
491 inc_stats(dns_resolver_t *res, isc_statscounter_t counter) {
492         if (res->view->resstats != NULL)
493                 isc_stats_increment(res->view->resstats, counter);
494 }
495
496 static isc_result_t
497 valcreate(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, dns_name_t *name,
498           dns_rdatatype_t type, dns_rdataset_t *rdataset,
499           dns_rdataset_t *sigrdataset, unsigned int valoptions,
500           isc_task_t *task)
501 {
502         dns_validator_t *validator = NULL;
503         dns_valarg_t *valarg;
504         isc_result_t result;
505
506         valarg = isc_mem_get(fctx->mctx, sizeof(*valarg));
507         if (valarg == NULL)
508                 return (ISC_R_NOMEMORY);
509
510         valarg->fctx = fctx;
511         valarg->addrinfo = addrinfo;
512
513         if (!ISC_LIST_EMPTY(fctx->validators))
514                 INSIST((valoptions & DNS_VALIDATOR_DEFER) != 0);
515
516         result = dns_validator_create(fctx->res->view, name, type, rdataset,
517                                       sigrdataset, fctx->rmessage,
518                                       valoptions, task, validated, valarg,
519                                       &validator);
520         if (result == ISC_R_SUCCESS) {
521                 inc_stats(fctx->res, dns_resstatscounter_val);
522                 if ((valoptions & DNS_VALIDATOR_DEFER) == 0) {
523                         INSIST(fctx->validator == NULL);
524                         fctx->validator = validator;
525                 }
526                 ISC_LIST_APPEND(fctx->validators, validator, link);
527         } else
528                 isc_mem_put(fctx->mctx, valarg, sizeof(*valarg));
529         return (result);
530 }
531
532 static isc_boolean_t
533 rrsig_fromchildzone(fetchctx_t *fctx, dns_rdataset_t *rdataset) {
534         dns_namereln_t namereln;
535         dns_rdata_rrsig_t rrsig;
536         dns_rdata_t rdata = DNS_RDATA_INIT;
537         int order;
538         isc_result_t result;
539         unsigned int labels;
540
541         for (result = dns_rdataset_first(rdataset);
542              result == ISC_R_SUCCESS;
543              result = dns_rdataset_next(rdataset)) {
544                 dns_rdataset_current(rdataset, &rdata);
545                 result = dns_rdata_tostruct(&rdata, &rrsig, NULL);
546                 RUNTIME_CHECK(result == ISC_R_SUCCESS);
547                 namereln = dns_name_fullcompare(&rrsig.signer, &fctx->domain,
548                                                 &order, &labels);
549                 if (namereln == dns_namereln_subdomain)
550                         return (ISC_TRUE);
551                 dns_rdata_reset(&rdata);
552         }
553         return (ISC_FALSE);
554 }
555
556 static isc_boolean_t
557 fix_mustbedelegationornxdomain(dns_message_t *message, fetchctx_t *fctx) {
558         dns_name_t *name;
559         dns_name_t *domain = &fctx->domain;
560         dns_rdataset_t *rdataset;
561         dns_rdatatype_t type;
562         isc_result_t result;
563         isc_boolean_t keep_auth = ISC_FALSE;
564
565         if (message->rcode == dns_rcode_nxdomain)
566                 return (ISC_FALSE);
567
568         /*
569          * A DS RRset can appear anywhere in a zone, even for a delegation-only
570          * zone.  So a response to an explicit query for this type should be
571          * excluded from delegation-only fixup.
572          *
573          * SOA, NS, and DNSKEY can only exist at a zone apex, so a postive
574          * response to a query for these types can never violate the
575          * delegation-only assumption: if the query name is below a
576          * zone cut, the response should normally be a referral, which should
577          * be accepted; if the query name is below a zone cut but the server
578          * happens to have authority for the zone of the query name, the
579          * response is a (non-referral) answer.  But this does not violate
580          * delegation-only because the query name must be in a different zone
581          * due to the "apex-only" nature of these types.  Note that if the
582          * remote server happens to have authority for a child zone of a
583          * delegation-only zone, we may still incorrectly "fix" the response
584          * with NXDOMAIN for queries for other types.  Unfortunately it's
585          * generally impossible to differentiate this case from violation of
586          * the delegation-only assumption.  Once the resolver learns the
587          * correct zone cut, possibly via a separate query for an "apex-only"
588          * type, queries for other types will be resolved correctly.
589          *
590          * A query for type ANY will be accepted if it hits an exceptional
591          * type above in the answer section as it should be from a child
592          * zone.
593          *
594          * Also accept answers with RRSIG records from the child zone.
595          * Direct queries for RRSIG records should not be answered from
596          * the parent zone.
597          */
598
599         if (message->counts[DNS_SECTION_ANSWER] != 0 &&
600             (fctx->type == dns_rdatatype_ns ||
601              fctx->type == dns_rdatatype_ds ||
602              fctx->type == dns_rdatatype_soa ||
603              fctx->type == dns_rdatatype_any ||
604              fctx->type == dns_rdatatype_rrsig ||
605              fctx->type == dns_rdatatype_dnskey)) {
606                 result = dns_message_firstname(message, DNS_SECTION_ANSWER);
607                 while (result == ISC_R_SUCCESS) {
608                         name = NULL;
609                         dns_message_currentname(message, DNS_SECTION_ANSWER,
610                                                 &name);
611                         for (rdataset = ISC_LIST_HEAD(name->list);
612                              rdataset != NULL;
613                              rdataset = ISC_LIST_NEXT(rdataset, link)) {
614                                 if (!dns_name_equal(name, &fctx->name))
615                                         continue;
616                                 type = rdataset->type;
617                                 /*
618                                  * RRsig from child?
619                                  */
620                                 if (type == dns_rdatatype_rrsig &&
621                                     rrsig_fromchildzone(fctx, rdataset))
622                                         return (ISC_FALSE);
623                                 /*
624                                  * Direct query for apex records or DS.
625                                  */
626                                 if (fctx->type == type &&
627                                     (type == dns_rdatatype_ds ||
628                                      type == dns_rdatatype_ns ||
629                                      type == dns_rdatatype_soa ||
630                                      type == dns_rdatatype_dnskey))
631                                         return (ISC_FALSE);
632                                 /*
633                                  * Indirect query for apex records or DS.
634                                  */
635                                 if (fctx->type == dns_rdatatype_any &&
636                                     (type == dns_rdatatype_ns ||
637                                      type == dns_rdatatype_ds ||
638                                      type == dns_rdatatype_soa ||
639                                      type == dns_rdatatype_dnskey))
640                                         return (ISC_FALSE);
641                         }
642                         result = dns_message_nextname(message,
643                                                       DNS_SECTION_ANSWER);
644                 }
645         }
646
647         /*
648          * A NODATA response to a DS query?
649          */
650         if (fctx->type == dns_rdatatype_ds &&
651             message->counts[DNS_SECTION_ANSWER] == 0)
652                 return (ISC_FALSE);
653
654         /* Look for referral or indication of answer from child zone? */
655         if (message->counts[DNS_SECTION_AUTHORITY] == 0)
656                 goto munge;
657
658         result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
659         while (result == ISC_R_SUCCESS) {
660                 name = NULL;
661                 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
662                 for (rdataset = ISC_LIST_HEAD(name->list);
663                      rdataset != NULL;
664                      rdataset = ISC_LIST_NEXT(rdataset, link)) {
665                         type = rdataset->type;
666                         if (type == dns_rdatatype_soa &&
667                             dns_name_equal(name, domain))
668                                 keep_auth = ISC_TRUE;
669
670                         if (type != dns_rdatatype_ns &&
671                             type != dns_rdatatype_soa &&
672                             type != dns_rdatatype_rrsig)
673                                 continue;
674
675                         if (type == dns_rdatatype_rrsig) {
676                                 if (rrsig_fromchildzone(fctx, rdataset))
677                                         return (ISC_FALSE);
678                                 else
679                                         continue;
680                         }
681
682                         /* NS or SOA records. */
683                         if (dns_name_equal(name, domain)) {
684                                 /*
685                                  * If a query for ANY causes a negative
686                                  * response, we can be sure that this is
687                                  * an empty node.  For other type of queries
688                                  * we cannot differentiate an empty node
689                                  * from a node that just doesn't have that
690                                  * type of record.  We only accept the former
691                                  * case.
692                                  */
693                                 if (message->counts[DNS_SECTION_ANSWER] == 0 &&
694                                     fctx->type == dns_rdatatype_any)
695                                         return (ISC_FALSE);
696                         } else if (dns_name_issubdomain(name, domain)) {
697                                 /* Referral or answer from child zone. */
698                                 return (ISC_FALSE);
699                         }
700                 }
701                 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
702         }
703
704  munge:
705         message->rcode = dns_rcode_nxdomain;
706         message->counts[DNS_SECTION_ANSWER] = 0;
707         if (!keep_auth)
708                 message->counts[DNS_SECTION_AUTHORITY] = 0;
709         message->counts[DNS_SECTION_ADDITIONAL] = 0;
710         return (ISC_TRUE);
711 }
712
713 static inline isc_result_t
714 fctx_starttimer(fetchctx_t *fctx) {
715         /*
716          * Start the lifetime timer for fctx.
717          *
718          * This is also used for stopping the idle timer; in that
719          * case we must purge events already posted to ensure that
720          * no further idle events are delivered.
721          */
722         return (isc_timer_reset(fctx->timer, isc_timertype_once,
723                                 &fctx->expires, NULL, ISC_TRUE));
724 }
725
726 static inline void
727 fctx_stoptimer(fetchctx_t *fctx) {
728         isc_result_t result;
729
730         /*
731          * We don't return a result if resetting the timer to inactive fails
732          * since there's nothing to be done about it.  Resetting to inactive
733          * should never fail anyway, since the code as currently written
734          * cannot fail in that case.
735          */
736         result = isc_timer_reset(fctx->timer, isc_timertype_inactive,
737                                   NULL, NULL, ISC_TRUE);
738         if (result != ISC_R_SUCCESS) {
739                 UNEXPECTED_ERROR(__FILE__, __LINE__,
740                                  "isc_timer_reset(): %s",
741                                  isc_result_totext(result));
742         }
743 }
744
745
746 static inline isc_result_t
747 fctx_startidletimer(fetchctx_t *fctx, isc_interval_t *interval) {
748         /*
749          * Start the idle timer for fctx.  The lifetime timer continues
750          * to be in effect.
751          */
752         return (isc_timer_reset(fctx->timer, isc_timertype_once,
753                                 &fctx->expires, interval, ISC_FALSE));
754 }
755
756 /*
757  * Stopping the idle timer is equivalent to calling fctx_starttimer(), but
758  * we use fctx_stopidletimer for readability in the code below.
759  */
760 #define fctx_stopidletimer      fctx_starttimer
761
762
763 static inline void
764 resquery_destroy(resquery_t **queryp) {
765         resquery_t *query;
766
767         REQUIRE(queryp != NULL);
768         query = *queryp;
769         REQUIRE(!ISC_LINK_LINKED(query, link));
770
771         INSIST(query->tcpsocket == NULL);
772
773         query->fctx->nqueries--;
774         if (SHUTTINGDOWN(query->fctx)) {
775                 dns_resolver_t *res = query->fctx->res;
776                 if (maybe_destroy(query->fctx, ISC_FALSE))
777                         empty_bucket(res);
778         }
779         query->magic = 0;
780         isc_mem_put(query->mctx, query, sizeof(*query));
781         *queryp = NULL;
782 }
783
784 static void
785 fctx_cancelquery(resquery_t **queryp, dns_dispatchevent_t **deventp,
786                  isc_time_t *finish, isc_boolean_t no_response)
787 {
788         fetchctx_t *fctx;
789         resquery_t *query;
790         unsigned int rtt, rttms;
791         unsigned int factor;
792         dns_adbfind_t *find;
793         dns_adbaddrinfo_t *addrinfo;
794         isc_socket_t *socket;
795
796         query = *queryp;
797         fctx = query->fctx;
798
799         FCTXTRACE("cancelquery");
800
801         REQUIRE(!RESQUERY_CANCELED(query));
802
803         query->attributes |= RESQUERY_ATTR_CANCELED;
804
805         /*
806          * Should we update the RTT?
807          */
808         if (finish != NULL || no_response) {
809                 if (finish != NULL) {
810                         /*
811                          * We have both the start and finish times for this
812                          * packet, so we can compute a real RTT.
813                          */
814                         rtt = (unsigned int)isc_time_microdiff(finish,
815                                                                &query->start);
816                         factor = DNS_ADB_RTTADJDEFAULT;
817
818                         rttms = rtt / 1000;
819                         if (rttms < DNS_RESOLVER_QRYRTTCLASS0) {
820                                 inc_stats(fctx->res,
821                                           dns_resstatscounter_queryrtt0);
822                         } else if (rttms < DNS_RESOLVER_QRYRTTCLASS1) {
823                                 inc_stats(fctx->res,
824                                           dns_resstatscounter_queryrtt1);
825                         } else if (rttms < DNS_RESOLVER_QRYRTTCLASS2) {
826                                 inc_stats(fctx->res,
827                                           dns_resstatscounter_queryrtt2);
828                         } else if (rttms < DNS_RESOLVER_QRYRTTCLASS3) {
829                                 inc_stats(fctx->res,
830                                           dns_resstatscounter_queryrtt3);
831                         } else if (rttms < DNS_RESOLVER_QRYRTTCLASS4) {
832                                 inc_stats(fctx->res,
833                                           dns_resstatscounter_queryrtt4);
834                         } else {
835                                 inc_stats(fctx->res,
836                                           dns_resstatscounter_queryrtt5);
837                         }
838                 } else {
839                         /*
840                          * We don't have an RTT for this query.  Maybe the
841                          * packet was lost, or maybe this server is very
842                          * slow.  We don't know.  Increase the RTT.
843                          */
844                         INSIST(no_response);
845                         rtt = query->addrinfo->srtt + 200000;
846                         if (rtt > MAX_SINGLE_QUERY_TIMEOUT_US)
847                                 rtt = MAX_SINGLE_QUERY_TIMEOUT_US;
848                         /*
849                          * Replace the current RTT with our value.
850                          */
851                         factor = DNS_ADB_RTTADJREPLACE;
852                 }
853                 dns_adb_adjustsrtt(fctx->adb, query->addrinfo, rtt, factor);
854         }
855
856         /* Remember that the server has been tried. */
857         if (!TRIED(query->addrinfo)) {
858                 dns_adb_changeflags(fctx->adb, query->addrinfo,
859                                     FCTX_ADDRINFO_TRIED, FCTX_ADDRINFO_TRIED);
860         }
861
862         /*
863          * Age RTTs of servers not tried.
864          */
865         factor = DNS_ADB_RTTADJAGE;
866         if (finish != NULL)
867                 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
868                      addrinfo != NULL;
869                      addrinfo = ISC_LIST_NEXT(addrinfo, publink))
870                         if (UNMARKED(addrinfo))
871                                 dns_adb_adjustsrtt(fctx->adb, addrinfo,
872                                                    0, factor);
873
874         if (finish != NULL && TRIEDFIND(fctx))
875                 for (find = ISC_LIST_HEAD(fctx->finds);
876                      find != NULL;
877                      find = ISC_LIST_NEXT(find, publink))
878                         for (addrinfo = ISC_LIST_HEAD(find->list);
879                              addrinfo != NULL;
880                              addrinfo = ISC_LIST_NEXT(addrinfo, publink))
881                                 if (UNMARKED(addrinfo))
882                                         dns_adb_adjustsrtt(fctx->adb, addrinfo,
883                                                            0, factor);
884
885         if (finish != NULL && TRIEDALT(fctx)) {
886                 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
887                      addrinfo != NULL;
888                      addrinfo = ISC_LIST_NEXT(addrinfo, publink))
889                         if (UNMARKED(addrinfo))
890                                 dns_adb_adjustsrtt(fctx->adb, addrinfo,
891                                                    0, factor);
892                 for (find = ISC_LIST_HEAD(fctx->altfinds);
893                      find != NULL;
894                      find = ISC_LIST_NEXT(find, publink))
895                         for (addrinfo = ISC_LIST_HEAD(find->list);
896                              addrinfo != NULL;
897                              addrinfo = ISC_LIST_NEXT(addrinfo, publink))
898                                 if (UNMARKED(addrinfo))
899                                         dns_adb_adjustsrtt(fctx->adb, addrinfo,
900                                                            0, factor);
901         }
902
903         /*
904          * Check for any outstanding socket events.  If they exist, cancel
905          * them and let the event handlers finish the cleanup.  The resolver
906          * only needs to worry about managing the connect and send events;
907          * the dispatcher manages the recv events.
908          */
909         if (RESQUERY_CONNECTING(query)) {
910                 /*
911                  * Cancel the connect.
912                  */
913                 if (query->tcpsocket != NULL) {
914                         isc_socket_cancel(query->tcpsocket, NULL,
915                                           ISC_SOCKCANCEL_CONNECT);
916                 } else if (query->dispentry != NULL) {
917                         INSIST(query->exclusivesocket);
918                         socket = dns_dispatch_getentrysocket(query->dispentry);
919                         if (socket != NULL)
920                                 isc_socket_cancel(socket, NULL,
921                                                   ISC_SOCKCANCEL_CONNECT);
922                 }
923         } else if (RESQUERY_SENDING(query)) {
924                 /*
925                  * Cancel the pending send.
926                  */
927                 if (query->exclusivesocket && query->dispentry != NULL)
928                         socket = dns_dispatch_getentrysocket(query->dispentry);
929                 else
930                         socket = dns_dispatch_getsocket(query->dispatch);
931                 if (socket != NULL)
932                         isc_socket_cancel(socket, NULL, ISC_SOCKCANCEL_SEND);
933         }
934
935         if (query->dispentry != NULL)
936                 dns_dispatch_removeresponse(&query->dispentry, deventp);
937
938         ISC_LIST_UNLINK(fctx->queries, query, link);
939
940         if (query->tsig != NULL)
941                 isc_buffer_free(&query->tsig);
942
943         if (query->tsigkey != NULL)
944                 dns_tsigkey_detach(&query->tsigkey);
945
946         if (query->dispatch != NULL)
947                 dns_dispatch_detach(&query->dispatch);
948
949         if (! (RESQUERY_CONNECTING(query) || RESQUERY_SENDING(query)))
950                 /*
951                  * It's safe to destroy the query now.
952                  */
953                 resquery_destroy(&query);
954 }
955
956 static void
957 fctx_cancelqueries(fetchctx_t *fctx, isc_boolean_t no_response) {
958         resquery_t *query, *next_query;
959
960         FCTXTRACE("cancelqueries");
961
962         for (query = ISC_LIST_HEAD(fctx->queries);
963              query != NULL;
964              query = next_query) {
965                 next_query = ISC_LIST_NEXT(query, link);
966                 fctx_cancelquery(&query, NULL, NULL, no_response);
967         }
968 }
969
970 static void
971 fctx_cleanupfinds(fetchctx_t *fctx) {
972         dns_adbfind_t *find, *next_find;
973
974         REQUIRE(ISC_LIST_EMPTY(fctx->queries));
975
976         for (find = ISC_LIST_HEAD(fctx->finds);
977              find != NULL;
978              find = next_find) {
979                 next_find = ISC_LIST_NEXT(find, publink);
980                 ISC_LIST_UNLINK(fctx->finds, find, publink);
981                 dns_adb_destroyfind(&find);
982         }
983         fctx->find = NULL;
984 }
985
986 static void
987 fctx_cleanupaltfinds(fetchctx_t *fctx) {
988         dns_adbfind_t *find, *next_find;
989
990         REQUIRE(ISC_LIST_EMPTY(fctx->queries));
991
992         for (find = ISC_LIST_HEAD(fctx->altfinds);
993              find != NULL;
994              find = next_find) {
995                 next_find = ISC_LIST_NEXT(find, publink);
996                 ISC_LIST_UNLINK(fctx->altfinds, find, publink);
997                 dns_adb_destroyfind(&find);
998         }
999         fctx->altfind = NULL;
1000 }
1001
1002 static void
1003 fctx_cleanupforwaddrs(fetchctx_t *fctx) {
1004         dns_adbaddrinfo_t *addr, *next_addr;
1005
1006         REQUIRE(ISC_LIST_EMPTY(fctx->queries));
1007
1008         for (addr = ISC_LIST_HEAD(fctx->forwaddrs);
1009              addr != NULL;
1010              addr = next_addr) {
1011                 next_addr = ISC_LIST_NEXT(addr, publink);
1012                 ISC_LIST_UNLINK(fctx->forwaddrs, addr, publink);
1013                 dns_adb_freeaddrinfo(fctx->adb, &addr);
1014         }
1015 }
1016
1017 static void
1018 fctx_cleanupaltaddrs(fetchctx_t *fctx) {
1019         dns_adbaddrinfo_t *addr, *next_addr;
1020
1021         REQUIRE(ISC_LIST_EMPTY(fctx->queries));
1022
1023         for (addr = ISC_LIST_HEAD(fctx->altaddrs);
1024              addr != NULL;
1025              addr = next_addr) {
1026                 next_addr = ISC_LIST_NEXT(addr, publink);
1027                 ISC_LIST_UNLINK(fctx->altaddrs, addr, publink);
1028                 dns_adb_freeaddrinfo(fctx->adb, &addr);
1029         }
1030 }
1031
1032 static inline void
1033 fctx_stopeverything(fetchctx_t *fctx, isc_boolean_t no_response) {
1034         FCTXTRACE("stopeverything");
1035         fctx_cancelqueries(fctx, no_response);
1036         fctx_cleanupfinds(fctx);
1037         fctx_cleanupaltfinds(fctx);
1038         fctx_cleanupforwaddrs(fctx);
1039         fctx_cleanupaltaddrs(fctx);
1040         fctx_stoptimer(fctx);
1041 }
1042
1043 static inline void
1044 fctx_sendevents(fetchctx_t *fctx, isc_result_t result, int line) {
1045         dns_fetchevent_t *event, *next_event;
1046         isc_task_t *task;
1047         unsigned int count = 0;
1048         isc_interval_t i;
1049         isc_boolean_t logit = ISC_FALSE;
1050         isc_time_t now;
1051         unsigned int old_spillat;
1052         unsigned int new_spillat = 0;   /* initialized to silence
1053                                            compiler warnings */
1054
1055         /*
1056          * Caller must be holding the appropriate bucket lock.
1057          */
1058         REQUIRE(fctx->state == fetchstate_done);
1059
1060         FCTXTRACE("sendevents");
1061
1062         /*
1063          * Keep some record of fetch result for logging later (if required).
1064          */
1065         fctx->result = result;
1066         fctx->exitline = line;
1067         TIME_NOW(&now);
1068         fctx->duration = isc_time_microdiff(&now, &fctx->start);
1069
1070         for (event = ISC_LIST_HEAD(fctx->events);
1071              event != NULL;
1072              event = next_event) {
1073                 next_event = ISC_LIST_NEXT(event, ev_link);
1074                 ISC_LIST_UNLINK(fctx->events, event, ev_link);
1075                 task = event->ev_sender;
1076                 event->ev_sender = fctx;
1077                 event->vresult = fctx->vresult;
1078                 if (!HAVE_ANSWER(fctx))
1079                         event->result = result;
1080
1081                 INSIST(result != ISC_R_SUCCESS ||
1082                        dns_rdataset_isassociated(event->rdataset) ||
1083                        fctx->type == dns_rdatatype_any ||
1084                        fctx->type == dns_rdatatype_rrsig ||
1085                        fctx->type == dns_rdatatype_sig);
1086
1087                 /*
1088                  * Negative results must be indicated in event->result.
1089                  */
1090                 if (dns_rdataset_isassociated(event->rdataset) &&
1091                     NEGATIVE(event->rdataset)) {
1092                         INSIST(event->result == DNS_R_NCACHENXDOMAIN ||
1093                                event->result == DNS_R_NCACHENXRRSET);
1094                 }
1095
1096                 isc_task_sendanddetach(&task, ISC_EVENT_PTR(&event));
1097                 count++;
1098         }
1099
1100         if ((fctx->attributes & FCTX_ATTR_HAVEANSWER) != 0 &&
1101             fctx->spilled &&
1102             (count < fctx->res->spillatmax || fctx->res->spillatmax == 0)) {
1103                 LOCK(&fctx->res->lock);
1104                 if (count == fctx->res->spillat && !fctx->res->exiting) {
1105                         old_spillat = fctx->res->spillat;
1106                         fctx->res->spillat += 5;
1107                         if (fctx->res->spillat > fctx->res->spillatmax &&
1108                             fctx->res->spillatmax != 0)
1109                                 fctx->res->spillat = fctx->res->spillatmax;
1110                         new_spillat = fctx->res->spillat;
1111                         if (new_spillat != old_spillat) {
1112                                 logit = ISC_TRUE;
1113                         }
1114                         isc_interval_set(&i, 20 * 60, 0);
1115                         result = isc_timer_reset(fctx->res->spillattimer,
1116                                                  isc_timertype_ticker, NULL,
1117                                                  &i, ISC_TRUE);
1118                         RUNTIME_CHECK(result == ISC_R_SUCCESS);
1119                 }
1120                 UNLOCK(&fctx->res->lock);
1121                 if (logit)
1122                         isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
1123                                       DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
1124                                       "clients-per-query increased to %u",
1125                                       new_spillat);
1126         }
1127 }
1128
1129 static inline void
1130 log_edns(fetchctx_t *fctx) {
1131         char domainbuf[DNS_NAME_FORMATSIZE];
1132
1133         if (fctx->reason == NULL)
1134                 return;
1135
1136         /*
1137          * We do not know if fctx->domain is the actual domain the record
1138          * lives in or a parent domain so we have a '?' after it.
1139          */
1140         dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
1141         isc_log_write(dns_lctx, DNS_LOGCATEGORY_EDNS_DISABLED,
1142                       DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
1143                       "success resolving '%s' (in '%s'?) after %s",
1144                       fctx->info, domainbuf, fctx->reason);
1145
1146         fctx->reason = NULL;
1147 }
1148
1149 static void
1150 fctx_done(fetchctx_t *fctx, isc_result_t result, int line) {
1151         dns_resolver_t *res;
1152         isc_boolean_t no_response;
1153
1154         REQUIRE(line >= 0);
1155
1156         FCTXTRACE("done");
1157
1158         res = fctx->res;
1159
1160         if (result == ISC_R_SUCCESS) {
1161                 /*%
1162                  * Log any deferred EDNS timeout messages.
1163                  */
1164                 log_edns(fctx);
1165                 no_response = ISC_TRUE;
1166          } else
1167                 no_response = ISC_FALSE;
1168
1169         fctx->reason = NULL;
1170         fctx_stopeverything(fctx, no_response);
1171
1172         LOCK(&res->buckets[fctx->bucketnum].lock);
1173
1174         fctx->state = fetchstate_done;
1175         fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
1176         fctx_sendevents(fctx, result, line);
1177
1178         UNLOCK(&res->buckets[fctx->bucketnum].lock);
1179 }
1180
1181 static void
1182 process_sendevent(resquery_t *query, isc_event_t *event) {
1183         isc_socketevent_t *sevent = (isc_socketevent_t *)event;
1184         isc_boolean_t retry = ISC_FALSE;
1185         isc_result_t result;
1186         fetchctx_t *fctx;
1187
1188         fctx = query->fctx;
1189
1190         if (RESQUERY_CANCELED(query)) {
1191                 if (query->sends == 0 && query->connects == 0) {
1192                         /*
1193                          * This query was canceled while the
1194                          * isc_socket_sendto/connect() was in progress.
1195                          */
1196                         if (query->tcpsocket != NULL)
1197                                 isc_socket_detach(&query->tcpsocket);
1198                         resquery_destroy(&query);
1199                 }
1200         } else {
1201                 switch (sevent->result) {
1202                 case ISC_R_SUCCESS:
1203                         break;
1204
1205                 case ISC_R_HOSTUNREACH:
1206                 case ISC_R_NETUNREACH:
1207                 case ISC_R_NOPERM:
1208                 case ISC_R_ADDRNOTAVAIL:
1209                 case ISC_R_CONNREFUSED:
1210
1211                         /*
1212                          * No route to remote.
1213                          */
1214                         add_bad(fctx, query->addrinfo, sevent->result,
1215                                 badns_unreachable);
1216                         fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
1217                         retry = ISC_TRUE;
1218                         break;
1219
1220                 default:
1221                         fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
1222                         break;
1223                 }
1224         }
1225
1226         if (event->ev_type == ISC_SOCKEVENT_CONNECT)
1227                 isc_event_free(&event);
1228
1229         if (retry) {
1230                 /*
1231                  * Behave as if the idle timer has expired.  For TCP
1232                  * this may not actually reflect the latest timer.
1233                  */
1234                 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
1235                 result = fctx_stopidletimer(fctx);
1236                 if (result != ISC_R_SUCCESS)
1237                         fctx_done(fctx, result, __LINE__);
1238                 else
1239                         fctx_try(fctx, ISC_TRUE, ISC_FALSE);
1240         }
1241 }
1242
1243 static void
1244 resquery_udpconnected(isc_task_t *task, isc_event_t *event) {
1245         resquery_t *query = event->ev_arg;
1246
1247         REQUIRE(event->ev_type == ISC_SOCKEVENT_CONNECT);
1248
1249         QTRACE("udpconnected");
1250
1251         UNUSED(task);
1252
1253         INSIST(RESQUERY_CONNECTING(query));
1254
1255         query->connects--;
1256
1257         process_sendevent(query, event);
1258 }
1259
1260 static void
1261 resquery_senddone(isc_task_t *task, isc_event_t *event) {
1262         resquery_t *query = event->ev_arg;
1263
1264         REQUIRE(event->ev_type == ISC_SOCKEVENT_SENDDONE);
1265
1266         QTRACE("senddone");
1267
1268         /*
1269          * XXXRTH
1270          *
1271          * Currently we don't wait for the senddone event before retrying
1272          * a query.  This means that if we get really behind, we may end
1273          * up doing extra work!
1274          */
1275
1276         UNUSED(task);
1277
1278         INSIST(RESQUERY_SENDING(query));
1279
1280         query->sends--;
1281
1282         process_sendevent(query, event);
1283 }
1284
1285 static inline isc_result_t
1286 fctx_addopt(dns_message_t *message, unsigned int version,
1287             isc_uint16_t udpsize, dns_ednsopt_t *ednsopts, size_t count)
1288 {
1289         dns_rdataset_t *rdataset = NULL;
1290         isc_result_t result;
1291
1292         result = dns_message_buildopt(message, &rdataset, version, udpsize,
1293                                       DNS_MESSAGEEXTFLAG_DO, ednsopts, count);
1294         if (result != ISC_R_SUCCESS)
1295                 return (result);
1296         return (dns_message_setopt(message, rdataset));
1297 }
1298
1299 static inline void
1300 fctx_setretryinterval(fetchctx_t *fctx, unsigned int rtt) {
1301         unsigned int seconds;
1302         unsigned int us;
1303
1304         /*
1305          * We retry every .8 seconds the first two times through the address
1306          * list, and then we do exponential back-off.
1307          */
1308         if (fctx->restarts < 3)
1309                 us = 800000;
1310         else
1311                 us = (800000 << (fctx->restarts - 2));
1312
1313         /*
1314          * Add a fudge factor to the expected rtt based on the current
1315          * estimate.
1316          */
1317         if (rtt < 50000)
1318                 rtt += 50000;
1319         else if (rtt < 100000)
1320                 rtt += 100000;
1321         else
1322                 rtt += 200000;
1323
1324         /*
1325          * Always wait for at least the expected rtt.
1326          */
1327         if (us < rtt)
1328                 us = rtt;
1329
1330         /*
1331          * But don't ever wait for more than 10 seconds.
1332          */
1333         if (us > MAX_SINGLE_QUERY_TIMEOUT_US)
1334                 us = MAX_SINGLE_QUERY_TIMEOUT_US;
1335
1336         seconds = us / US_PER_SEC;
1337         us -= seconds * US_PER_SEC;
1338         isc_interval_set(&fctx->interval, seconds, us * 1000);
1339 }
1340
1341 static isc_result_t
1342 fctx_query(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
1343            unsigned int options)
1344 {
1345         dns_resolver_t *res;
1346         isc_task_t *task;
1347         isc_result_t result;
1348         resquery_t *query;
1349         isc_sockaddr_t addr;
1350         isc_boolean_t have_addr = ISC_FALSE;
1351         unsigned int srtt;
1352
1353         FCTXTRACE("query");
1354
1355         res = fctx->res;
1356         task = res->buckets[fctx->bucketnum].task;
1357
1358         srtt = addrinfo->srtt;
1359
1360         /*
1361          * A forwarder needs to make multiple queries. Give it at least
1362          * a second to do these in.
1363          */
1364         if (ISFORWARDER(addrinfo) && srtt < 1000000)
1365                 srtt = 1000000;
1366
1367         fctx_setretryinterval(fctx, srtt);
1368         result = fctx_startidletimer(fctx, &fctx->interval);
1369         if (result != ISC_R_SUCCESS)
1370                 return (result);
1371
1372         INSIST(ISC_LIST_EMPTY(fctx->validators));
1373
1374         dns_message_reset(fctx->rmessage, DNS_MESSAGE_INTENTPARSE);
1375
1376         query = isc_mem_get(fctx->mctx, sizeof(*query));
1377         if (query == NULL) {
1378                 result = ISC_R_NOMEMORY;
1379                 goto stop_idle_timer;
1380         }
1381         query->mctx = fctx->mctx;
1382         query->options = options;
1383         query->attributes = 0;
1384         query->sends = 0;
1385         query->connects = 0;
1386         /*
1387          * Note that the caller MUST guarantee that 'addrinfo' will remain
1388          * valid until this query is canceled.
1389          */
1390         query->addrinfo = addrinfo;
1391         TIME_NOW(&query->start);
1392
1393         /*
1394          * If this is a TCP query, then we need to make a socket and
1395          * a dispatch for it here.  Otherwise we use the resolver's
1396          * shared dispatch.
1397          */
1398         query->dispatchmgr = res->dispatchmgr;
1399         query->dispatch = NULL;
1400         query->exclusivesocket = ISC_FALSE;
1401         query->tcpsocket = NULL;
1402         if (res->view->peers != NULL) {
1403                 dns_peer_t *peer = NULL;
1404                 isc_netaddr_t dstip;
1405                 isc_netaddr_fromsockaddr(&dstip, &addrinfo->sockaddr);
1406                 result = dns_peerlist_peerbyaddr(res->view->peers,
1407                                                  &dstip, &peer);
1408                 if (result == ISC_R_SUCCESS) {
1409                         result = dns_peer_getquerysource(peer, &addr);
1410                         if (result == ISC_R_SUCCESS)
1411                                 have_addr = ISC_TRUE;
1412                 }
1413         }
1414
1415         if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1416                 int pf;
1417
1418                 pf = isc_sockaddr_pf(&addrinfo->sockaddr);
1419                 if (!have_addr) {
1420                         switch (pf) {
1421                         case PF_INET:
1422                                 result = dns_dispatch_getlocaladdress(
1423                                               res->dispatches4->dispatches[0],
1424                                               &addr);
1425                                 break;
1426                         case PF_INET6:
1427                                 result = dns_dispatch_getlocaladdress(
1428                                               res->dispatches6->dispatches[0],
1429                                               &addr);
1430                                 break;
1431                         default:
1432                                 result = ISC_R_NOTIMPLEMENTED;
1433                                 break;
1434                         }
1435                         if (result != ISC_R_SUCCESS)
1436                                 goto cleanup_query;
1437                 }
1438                 isc_sockaddr_setport(&addr, 0);
1439
1440                 result = isc_socket_create(res->socketmgr, pf,
1441                                            isc_sockettype_tcp,
1442                                            &query->tcpsocket);
1443                 if (result != ISC_R_SUCCESS)
1444                         goto cleanup_query;
1445
1446 #ifndef BROKEN_TCP_BIND_BEFORE_CONNECT
1447                 result = isc_socket_bind(query->tcpsocket, &addr, 0);
1448                 if (result != ISC_R_SUCCESS)
1449                         goto cleanup_socket;
1450 #endif
1451
1452                 /*
1453                  * A dispatch will be created once the connect succeeds.
1454                  */
1455         } else {
1456                 if (have_addr) {
1457                         unsigned int attrs, attrmask;
1458                         attrs = DNS_DISPATCHATTR_UDP;
1459                         switch (isc_sockaddr_pf(&addr)) {
1460                         case AF_INET:
1461                                 attrs |= DNS_DISPATCHATTR_IPV4;
1462                                 break;
1463                         case AF_INET6:
1464                                 attrs |= DNS_DISPATCHATTR_IPV6;
1465                                 break;
1466                         default:
1467                                 result = ISC_R_NOTIMPLEMENTED;
1468                                 goto cleanup_query;
1469                         }
1470                         attrmask = DNS_DISPATCHATTR_UDP;
1471                         attrmask |= DNS_DISPATCHATTR_TCP;
1472                         attrmask |= DNS_DISPATCHATTR_IPV4;
1473                         attrmask |= DNS_DISPATCHATTR_IPV6;
1474                         result = dns_dispatch_getudp(res->dispatchmgr,
1475                                                      res->socketmgr,
1476                                                      res->taskmgr, &addr,
1477                                                      4096, 1000, 32768, 16411,
1478                                                      16433, attrs, attrmask,
1479                                                      &query->dispatch);
1480                         if (result != ISC_R_SUCCESS)
1481                                 goto cleanup_query;
1482                 } else {
1483                         switch (isc_sockaddr_pf(&addrinfo->sockaddr)) {
1484                         case PF_INET:
1485                                 dns_dispatch_attach(
1486                                     dns_resolver_dispatchv4(res),
1487                                     &query->dispatch);
1488                                 query->exclusivesocket = res->exclusivev4;
1489                                 break;
1490                         case PF_INET6:
1491                                 dns_dispatch_attach(
1492                                     dns_resolver_dispatchv6(res),
1493                                     &query->dispatch);
1494                                 query->exclusivesocket = res->exclusivev6;
1495                                 break;
1496                         default:
1497                                 result = ISC_R_NOTIMPLEMENTED;
1498                                 goto cleanup_query;
1499                         }
1500                 }
1501                 /*
1502                  * We should always have a valid dispatcher here.  If we
1503                  * don't support a protocol family, then its dispatcher
1504                  * will be NULL, but we shouldn't be finding addresses for
1505                  * protocol types we don't support, so the dispatcher
1506                  * we found should never be NULL.
1507                  */
1508                 INSIST(query->dispatch != NULL);
1509         }
1510
1511         query->dispentry = NULL;
1512         query->fctx = fctx;
1513         query->tsig = NULL;
1514         query->tsigkey = NULL;
1515         ISC_LINK_INIT(query, link);
1516         query->magic = QUERY_MAGIC;
1517
1518         if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1519                 /*
1520                  * Connect to the remote server.
1521                  *
1522                  * XXXRTH  Should we attach to the socket?
1523                  */
1524                 result = isc_socket_connect(query->tcpsocket,
1525                                             &addrinfo->sockaddr, task,
1526                                             resquery_connected, query);
1527                 if (result != ISC_R_SUCCESS)
1528                         goto cleanup_socket;
1529                 query->connects++;
1530                 QTRACE("connecting via TCP");
1531         } else {
1532                 result = resquery_send(query);
1533                 if (result != ISC_R_SUCCESS)
1534                         goto cleanup_dispatch;
1535         }
1536         fctx->querysent++;
1537
1538         ISC_LIST_APPEND(fctx->queries, query, link);
1539         query->fctx->nqueries++;
1540         if (isc_sockaddr_pf(&addrinfo->sockaddr) == PF_INET)
1541                 inc_stats(res, dns_resstatscounter_queryv4);
1542         else
1543                 inc_stats(res, dns_resstatscounter_queryv6);
1544         if (res->view->resquerystats != NULL)
1545                 dns_rdatatypestats_increment(res->view->resquerystats,
1546                                              fctx->type);
1547
1548         return (ISC_R_SUCCESS);
1549
1550  cleanup_socket:
1551         isc_socket_detach(&query->tcpsocket);
1552
1553  cleanup_dispatch:
1554         if (query->dispatch != NULL)
1555                 dns_dispatch_detach(&query->dispatch);
1556
1557  cleanup_query:
1558         if (query->connects == 0) {
1559                 query->magic = 0;
1560                 isc_mem_put(fctx->mctx, query, sizeof(*query));
1561         }
1562
1563  stop_idle_timer:
1564         RUNTIME_CHECK(fctx_stopidletimer(fctx) == ISC_R_SUCCESS);
1565
1566         return (result);
1567 }
1568
1569 static isc_boolean_t
1570 bad_edns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1571         isc_sockaddr_t *sa;
1572
1573         for (sa = ISC_LIST_HEAD(fctx->bad_edns);
1574              sa != NULL;
1575              sa = ISC_LIST_NEXT(sa, link)) {
1576                 if (isc_sockaddr_equal(sa, address))
1577                         return (ISC_TRUE);
1578         }
1579
1580         return (ISC_FALSE);
1581 }
1582
1583 static void
1584 add_bad_edns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1585         isc_sockaddr_t *sa;
1586
1587         if (bad_edns(fctx, address))
1588                 return;
1589
1590         sa = isc_mem_get(fctx->mctx, sizeof(*sa));
1591         if (sa == NULL)
1592                 return;
1593
1594         *sa = *address;
1595         ISC_LIST_INITANDAPPEND(fctx->bad_edns, sa, link);
1596 }
1597
1598 static isc_boolean_t
1599 triededns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1600         isc_sockaddr_t *sa;
1601
1602         for (sa = ISC_LIST_HEAD(fctx->edns);
1603              sa != NULL;
1604              sa = ISC_LIST_NEXT(sa, link)) {
1605                 if (isc_sockaddr_equal(sa, address))
1606                         return (ISC_TRUE);
1607         }
1608
1609         return (ISC_FALSE);
1610 }
1611
1612 static void
1613 add_triededns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1614         isc_sockaddr_t *sa;
1615
1616         if (triededns(fctx, address))
1617                 return;
1618
1619         sa = isc_mem_get(fctx->mctx, sizeof(*sa));
1620         if (sa == NULL)
1621                 return;
1622
1623         *sa = *address;
1624         ISC_LIST_INITANDAPPEND(fctx->edns, sa, link);
1625 }
1626
1627 static isc_boolean_t
1628 triededns512(fetchctx_t *fctx, isc_sockaddr_t *address) {
1629         isc_sockaddr_t *sa;
1630
1631         for (sa = ISC_LIST_HEAD(fctx->edns512);
1632              sa != NULL;
1633              sa = ISC_LIST_NEXT(sa, link)) {
1634                 if (isc_sockaddr_equal(sa, address))
1635                         return (ISC_TRUE);
1636         }
1637
1638         return (ISC_FALSE);
1639 }
1640
1641 static void
1642 add_triededns512(fetchctx_t *fctx, isc_sockaddr_t *address) {
1643         isc_sockaddr_t *sa;
1644
1645         if (triededns512(fctx, address))
1646                 return;
1647
1648         sa = isc_mem_get(fctx->mctx, sizeof(*sa));
1649         if (sa == NULL)
1650                 return;
1651
1652         *sa = *address;
1653         ISC_LIST_INITANDAPPEND(fctx->edns512, sa, link);
1654 }
1655
1656 static isc_result_t
1657 resquery_send(resquery_t *query) {
1658         fetchctx_t *fctx;
1659         isc_result_t result;
1660         dns_name_t *qname = NULL;
1661         dns_rdataset_t *qrdataset = NULL;
1662         isc_region_t r;
1663         dns_resolver_t *res;
1664         isc_task_t *task;
1665         isc_socket_t *socket;
1666         isc_buffer_t tcpbuffer;
1667         isc_sockaddr_t *address;
1668         isc_buffer_t *buffer;
1669         isc_netaddr_t ipaddr;
1670         dns_tsigkey_t *tsigkey = NULL;
1671         dns_peer_t *peer = NULL;
1672         isc_boolean_t useedns;
1673         dns_compress_t cctx;
1674         isc_boolean_t cleanup_cctx = ISC_FALSE;
1675         isc_boolean_t secure_domain;
1676         isc_boolean_t connecting = ISC_FALSE;
1677         dns_ednsopt_t ednsopts[EDNSOPTS];
1678         unsigned ednsopt = 0;
1679
1680         fctx = query->fctx;
1681         QTRACE("send");
1682
1683         res = fctx->res;
1684         task = res->buckets[fctx->bucketnum].task;
1685         address = NULL;
1686
1687         if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1688                 /*
1689                  * Reserve space for the TCP message length.
1690                  */
1691                 isc_buffer_init(&tcpbuffer, query->data, sizeof(query->data));
1692                 isc_buffer_init(&query->buffer, query->data + 2,
1693                                 sizeof(query->data) - 2);
1694                 buffer = &tcpbuffer;
1695         } else {
1696                 isc_buffer_init(&query->buffer, query->data,
1697                                 sizeof(query->data));
1698                 buffer = &query->buffer;
1699         }
1700
1701         result = dns_message_gettempname(fctx->qmessage, &qname);
1702         if (result != ISC_R_SUCCESS)
1703                 goto cleanup_temps;
1704         result = dns_message_gettemprdataset(fctx->qmessage, &qrdataset);
1705         if (result != ISC_R_SUCCESS)
1706                 goto cleanup_temps;
1707
1708         /*
1709          * Get a query id from the dispatch.
1710          */
1711         result = dns_dispatch_addresponse2(query->dispatch,
1712                                            &query->addrinfo->sockaddr,
1713                                            task,
1714                                            resquery_response,
1715                                            query,
1716                                            &query->id,
1717                                            &query->dispentry,
1718                                            res->socketmgr);
1719         if (result != ISC_R_SUCCESS)
1720                 goto cleanup_temps;
1721
1722         fctx->qmessage->opcode = dns_opcode_query;
1723
1724         /*
1725          * Set up question.
1726          */
1727         dns_name_init(qname, NULL);
1728         dns_name_clone(&fctx->name, qname);
1729         dns_rdataset_init(qrdataset);
1730         dns_rdataset_makequestion(qrdataset, res->rdclass, fctx->type);
1731         ISC_LIST_APPEND(qname->list, qrdataset, link);
1732         dns_message_addname(fctx->qmessage, qname, DNS_SECTION_QUESTION);
1733         qname = NULL;
1734         qrdataset = NULL;
1735
1736         /*
1737          * Set RD if the client has requested that we do a recursive query,
1738          * or if we're sending to a forwarder.
1739          */
1740         if ((query->options & DNS_FETCHOPT_RECURSIVE) != 0 ||
1741             ISFORWARDER(query->addrinfo))
1742                 fctx->qmessage->flags |= DNS_MESSAGEFLAG_RD;
1743
1744         /*
1745          * Set CD if the client says don't validate or the question is
1746          * under a secure entry point.
1747          */
1748         if ((query->options & DNS_FETCHOPT_NOVALIDATE) != 0) {
1749                 fctx->qmessage->flags |= DNS_MESSAGEFLAG_CD;
1750         } else if (res->view->enablevalidation) {
1751                 result = dns_view_issecuredomain(res->view, &fctx->name,
1752                                                  &secure_domain);
1753                 if (result != ISC_R_SUCCESS)
1754                         secure_domain = ISC_FALSE;
1755                 if (res->view->dlv != NULL)
1756                         secure_domain = ISC_TRUE;
1757                 if (secure_domain)
1758                         fctx->qmessage->flags |= DNS_MESSAGEFLAG_CD;
1759         }
1760
1761         /*
1762          * We don't have to set opcode because it defaults to query.
1763          */
1764         fctx->qmessage->id = query->id;
1765
1766         /*
1767          * Convert the question to wire format.
1768          */
1769         result = dns_compress_init(&cctx, -1, fctx->res->mctx);
1770         if (result != ISC_R_SUCCESS)
1771                 goto cleanup_message;
1772         cleanup_cctx = ISC_TRUE;
1773
1774         result = dns_message_renderbegin(fctx->qmessage, &cctx,
1775                                          &query->buffer);
1776         if (result != ISC_R_SUCCESS)
1777                 goto cleanup_message;
1778
1779         result = dns_message_rendersection(fctx->qmessage,
1780                                            DNS_SECTION_QUESTION, 0);
1781         if (result != ISC_R_SUCCESS)
1782                 goto cleanup_message;
1783
1784         peer = NULL;
1785         isc_netaddr_fromsockaddr(&ipaddr, &query->addrinfo->sockaddr);
1786         (void) dns_peerlist_peerbyaddr(fctx->res->view->peers, &ipaddr, &peer);
1787
1788         /*
1789          * The ADB does not know about servers with "edns no".  Check this,
1790          * and then inform the ADB for future use.
1791          */
1792         if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) == 0 &&
1793             peer != NULL &&
1794             dns_peer_getsupportedns(peer, &useedns) == ISC_R_SUCCESS &&
1795             !useedns)
1796         {
1797                 query->options |= DNS_FETCHOPT_NOEDNS0;
1798                 dns_adb_changeflags(fctx->adb, query->addrinfo,
1799                                     DNS_FETCHOPT_NOEDNS0,
1800                                     DNS_FETCHOPT_NOEDNS0);
1801         }
1802
1803         /* Sync NOEDNS0 flag in addrinfo->flags and options now. */
1804         if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) != 0)
1805                 query->options |= DNS_FETCHOPT_NOEDNS0;
1806
1807         /*
1808          * Handle timeouts by reducing the UDP response size to 512 bytes
1809          * then if that doesn't work disabling EDNS (includes DO) and CD.
1810          *
1811          * These timeout can be due to:
1812          *      * broken nameservers that don't respond to EDNS queries.
1813          *      * broken/misconfigured firewalls and NAT implementations
1814          *        that don't handle IP fragmentation.
1815          *      * broken/misconfigured firewalls that don't handle responses
1816          *        greater than 512 bytes.
1817          *      * broken/misconfigured firewalls that don't handle EDNS, DO
1818          *        or CD.
1819          *      * packet loss / link outage.
1820          */
1821         if (fctx->timeout) {
1822                 if ((triededns512(fctx, &query->addrinfo->sockaddr) ||
1823                      fctx->timeouts >= (MAX_EDNS0_TIMEOUTS * 2)) &&
1824                     (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
1825                         query->options |= DNS_FETCHOPT_NOEDNS0;
1826                         fctx->reason = "disabling EDNS";
1827                 } else if ((triededns(fctx, &query->addrinfo->sockaddr) ||
1828                             fctx->timeouts >= MAX_EDNS0_TIMEOUTS) &&
1829                            (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
1830                         query->options |= DNS_FETCHOPT_EDNS512;
1831                         fctx->reason = "reducing the advertised EDNS UDP "
1832                                        "packet size to 512 octets";
1833                 }
1834                 fctx->timeout = ISC_FALSE;
1835         }
1836
1837         /*
1838          * Use EDNS0, unless the caller doesn't want it, or we know that
1839          * the remote server doesn't like it.
1840          */
1841         if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
1842                 if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) == 0) {
1843                         unsigned int version = 0;       /* Default version. */
1844                         unsigned int flags;
1845                         isc_uint16_t udpsize = res->udpsize;
1846                         isc_boolean_t reqnsid = res->view->requestnsid;
1847
1848                         flags = query->addrinfo->flags;
1849                         if ((flags & DNS_FETCHOPT_EDNSVERSIONSET) != 0) {
1850                                 version = flags & DNS_FETCHOPT_EDNSVERSIONMASK;
1851                                 version >>= DNS_FETCHOPT_EDNSVERSIONSHIFT;
1852                         }
1853                         if ((query->options & DNS_FETCHOPT_EDNS512) != 0)
1854                                 udpsize = 512;
1855                         else if (peer != NULL)
1856                                 (void)dns_peer_getudpsize(peer, &udpsize);
1857
1858                         /* request NSID for current view or peer? */
1859                         if (peer != NULL)
1860                                 (void) dns_peer_getrequestnsid(peer, &reqnsid);
1861                         if (reqnsid) {
1862                                 INSIST(ednsopt < EDNSOPTS);
1863                                 ednsopts[ednsopt].code = DNS_OPT_NSID;
1864                                 ednsopts[ednsopt].length = 0;
1865                                 ednsopts[ednsopt].value = NULL;
1866                                 ednsopt++;
1867                         }
1868                         result = fctx_addopt(fctx->qmessage, version,
1869                                              udpsize, ednsopts, ednsopt);
1870                         if (reqnsid && result == ISC_R_SUCCESS) {
1871                                 query->options |= DNS_FETCHOPT_WANTNSID;
1872                         } else if (result != ISC_R_SUCCESS) {
1873                                 /*
1874                                  * We couldn't add the OPT, but we'll press on.
1875                                  * We're not using EDNS0, so set the NOEDNS0
1876                                  * bit.
1877                                  */
1878                                 query->options |= DNS_FETCHOPT_NOEDNS0;
1879                         }
1880                 } else {
1881                         /*
1882                          * We know this server doesn't like EDNS0, so we
1883                          * won't use it.  Set the NOEDNS0 bit since we're
1884                          * not using EDNS0.
1885                          */
1886                         query->options |= DNS_FETCHOPT_NOEDNS0;
1887                 }
1888         }
1889
1890         /*
1891          * If we need EDNS0 to do this query and aren't using it, we lose.
1892          */
1893         if (NEEDEDNS0(fctx) && (query->options & DNS_FETCHOPT_NOEDNS0) != 0) {
1894                 result = DNS_R_SERVFAIL;
1895                 goto cleanup_message;
1896         }
1897
1898         if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0)
1899                 add_triededns(fctx, &query->addrinfo->sockaddr);
1900
1901         if ((query->options & DNS_FETCHOPT_EDNS512) != 0)
1902                 add_triededns512(fctx, &query->addrinfo->sockaddr);
1903
1904         /*
1905          * Clear CD if EDNS is not in use.
1906          */
1907         if ((query->options & DNS_FETCHOPT_NOEDNS0) != 0)
1908                 fctx->qmessage->flags &= ~DNS_MESSAGEFLAG_CD;
1909
1910         /*
1911          * Add TSIG record tailored to the current recipient.
1912          */
1913         result = dns_view_getpeertsig(fctx->res->view, &ipaddr, &tsigkey);
1914         if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND)
1915                 goto cleanup_message;
1916
1917         if (tsigkey != NULL) {
1918                 result = dns_message_settsigkey(fctx->qmessage, tsigkey);
1919                 dns_tsigkey_detach(&tsigkey);
1920                 if (result != ISC_R_SUCCESS)
1921                         goto cleanup_message;
1922         }
1923
1924         result = dns_message_rendersection(fctx->qmessage,
1925                                            DNS_SECTION_ADDITIONAL, 0);
1926         if (result != ISC_R_SUCCESS)
1927                 goto cleanup_message;
1928
1929         result = dns_message_renderend(fctx->qmessage);
1930         if (result != ISC_R_SUCCESS)
1931                 goto cleanup_message;
1932
1933         dns_compress_invalidate(&cctx);
1934         cleanup_cctx = ISC_FALSE;
1935
1936         if (dns_message_gettsigkey(fctx->qmessage) != NULL) {
1937                 dns_tsigkey_attach(dns_message_gettsigkey(fctx->qmessage),
1938                                    &query->tsigkey);
1939                 result = dns_message_getquerytsig(fctx->qmessage,
1940                                                   fctx->res->mctx,
1941                                                   &query->tsig);
1942                 if (result != ISC_R_SUCCESS)
1943                         goto cleanup_message;
1944         }
1945
1946         /*
1947          * If using TCP, write the length of the message at the beginning
1948          * of the buffer.
1949          */
1950         if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1951                 isc_buffer_usedregion(&query->buffer, &r);
1952                 isc_buffer_putuint16(&tcpbuffer, (isc_uint16_t)r.length);
1953                 isc_buffer_add(&tcpbuffer, r.length);
1954         }
1955
1956         /*
1957          * We're now done with the query message.
1958          */
1959         dns_message_reset(fctx->qmessage, DNS_MESSAGE_INTENTRENDER);
1960
1961         if (query->exclusivesocket)
1962                 socket = dns_dispatch_getentrysocket(query->dispentry);
1963         else
1964                 socket = dns_dispatch_getsocket(query->dispatch);
1965         /*
1966          * Send the query!
1967          */
1968         if ((query->options & DNS_FETCHOPT_TCP) == 0) {
1969                 address = &query->addrinfo->sockaddr;
1970                 if (query->exclusivesocket) {
1971                         result = isc_socket_connect(socket, address, task,
1972                                                     resquery_udpconnected,
1973                                                     query);
1974                         if (result != ISC_R_SUCCESS)
1975                                 goto cleanup_message;
1976                         connecting = ISC_TRUE;
1977                         query->connects++;
1978                 }
1979         }
1980         isc_buffer_usedregion(buffer, &r);
1981
1982         /*
1983          * XXXRTH  Make sure we don't send to ourselves!  We should probably
1984          *              prune out these addresses when we get them from the ADB.
1985          */
1986         ISC_EVENT_INIT(&query->sendevent, sizeof(query->sendevent), 0, NULL,
1987                        ISC_SOCKEVENT_SENDDONE, resquery_senddone, query,
1988                        NULL, NULL, NULL);
1989         result = isc_socket_sendto2(socket, &r, task, address, NULL,
1990                                     &query->sendevent, 0);
1991         if (result != ISC_R_SUCCESS) {
1992                 if (connecting) {
1993                         /*
1994                          * This query is still connecting.
1995                          * Mark it as canceled so that it will just be
1996                          * cleaned up when the connected event is received.
1997                          * Keep fctx around until the event is processed.
1998                          */
1999                         query->fctx->nqueries++;
2000                         query->attributes |= RESQUERY_ATTR_CANCELED;
2001                 }
2002                 goto cleanup_message;
2003         }
2004
2005         query->sends++;
2006
2007         QTRACE("sent");
2008
2009         return (ISC_R_SUCCESS);
2010
2011  cleanup_message:
2012         if (cleanup_cctx)
2013                 dns_compress_invalidate(&cctx);
2014
2015         dns_message_reset(fctx->qmessage, DNS_MESSAGE_INTENTRENDER);
2016
2017         /*
2018          * Stop the dispatcher from listening.
2019          */
2020         dns_dispatch_removeresponse(&query->dispentry, NULL);
2021
2022  cleanup_temps:
2023         if (qname != NULL)
2024                 dns_message_puttempname(fctx->qmessage, &qname);
2025         if (qrdataset != NULL)
2026                 dns_message_puttemprdataset(fctx->qmessage, &qrdataset);
2027
2028         return (result);
2029 }
2030
2031 static void
2032 resquery_connected(isc_task_t *task, isc_event_t *event) {
2033         isc_socketevent_t *sevent = (isc_socketevent_t *)event;
2034         resquery_t *query = event->ev_arg;
2035         isc_boolean_t retry = ISC_FALSE;
2036         isc_interval_t interval;
2037         isc_result_t result;
2038         unsigned int attrs;
2039         fetchctx_t *fctx;
2040
2041         REQUIRE(event->ev_type == ISC_SOCKEVENT_CONNECT);
2042         REQUIRE(VALID_QUERY(query));
2043
2044         QTRACE("connected");
2045
2046         UNUSED(task);
2047
2048         /*
2049          * XXXRTH
2050          *
2051          * Currently we don't wait for the connect event before retrying
2052          * a query.  This means that if we get really behind, we may end
2053          * up doing extra work!
2054          */
2055
2056         query->connects--;
2057         fctx = query->fctx;
2058
2059         if (RESQUERY_CANCELED(query)) {
2060                 /*
2061                  * This query was canceled while the connect() was in
2062                  * progress.
2063                  */
2064                 isc_socket_detach(&query->tcpsocket);
2065                 resquery_destroy(&query);
2066         } else {
2067                 switch (sevent->result) {
2068                 case ISC_R_SUCCESS:
2069
2070                         /*
2071                          * Extend the idle timer for TCP.  20 seconds
2072                          * should be long enough for a TCP connection to be
2073                          * established, a single DNS request to be sent,
2074                          * and the response received.
2075                          */
2076                         isc_interval_set(&interval, 20, 0);
2077                         result = fctx_startidletimer(query->fctx, &interval);
2078                         if (result != ISC_R_SUCCESS) {
2079                                 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
2080                                 fctx_done(fctx, result, __LINE__);
2081                                 break;
2082                         }
2083                         /*
2084                          * We are connected.  Create a dispatcher and
2085                          * send the query.
2086                          */
2087                         attrs = 0;
2088                         attrs |= DNS_DISPATCHATTR_TCP;
2089                         attrs |= DNS_DISPATCHATTR_PRIVATE;
2090                         attrs |= DNS_DISPATCHATTR_CONNECTED;
2091                         if (isc_sockaddr_pf(&query->addrinfo->sockaddr) ==
2092                             AF_INET)
2093                                 attrs |= DNS_DISPATCHATTR_IPV4;
2094                         else
2095                                 attrs |= DNS_DISPATCHATTR_IPV6;
2096                         attrs |= DNS_DISPATCHATTR_MAKEQUERY;
2097
2098                         result = dns_dispatch_createtcp(query->dispatchmgr,
2099                                                      query->tcpsocket,
2100                                                      query->fctx->res->taskmgr,
2101                                                      4096, 2, 1, 1, 3, attrs,
2102                                                      &query->dispatch);
2103
2104                         /*
2105                          * Regardless of whether dns_dispatch_create()
2106                          * succeeded or not, we don't need our reference
2107                          * to the socket anymore.
2108                          */
2109                         isc_socket_detach(&query->tcpsocket);
2110
2111                         if (result == ISC_R_SUCCESS)
2112                                 result = resquery_send(query);
2113
2114                         if (result != ISC_R_SUCCESS) {
2115                                 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
2116                                 fctx_done(fctx, result, __LINE__);
2117                         }
2118                         break;
2119
2120                 case ISC_R_NETUNREACH:
2121                 case ISC_R_HOSTUNREACH:
2122                 case ISC_R_CONNREFUSED:
2123                 case ISC_R_NOPERM:
2124                 case ISC_R_ADDRNOTAVAIL:
2125                 case ISC_R_CONNECTIONRESET:
2126                         /*
2127                          * No route to remote.
2128                          */
2129                         isc_socket_detach(&query->tcpsocket);
2130                         fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
2131                         retry = ISC_TRUE;
2132                         break;
2133
2134                 default:
2135                         isc_socket_detach(&query->tcpsocket);
2136                         fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
2137                         break;
2138                 }
2139         }
2140
2141         isc_event_free(&event);
2142
2143         if (retry) {
2144                 /*
2145                  * Behave as if the idle timer has expired.  For TCP
2146                  * connections this may not actually reflect the latest timer.
2147                  */
2148                 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
2149                 result = fctx_stopidletimer(fctx);
2150                 if (result != ISC_R_SUCCESS)
2151                         fctx_done(fctx, result, __LINE__);
2152                 else
2153                         fctx_try(fctx, ISC_TRUE, ISC_FALSE);
2154         }
2155 }
2156
2157 static void
2158 fctx_finddone(isc_task_t *task, isc_event_t *event) {
2159         fetchctx_t *fctx;
2160         dns_adbfind_t *find;
2161         dns_resolver_t *res;
2162         isc_boolean_t want_try = ISC_FALSE;
2163         isc_boolean_t want_done = ISC_FALSE;
2164         isc_boolean_t bucket_empty = ISC_FALSE;
2165         unsigned int bucketnum;
2166         isc_boolean_t destroy = ISC_FALSE;
2167
2168         find = event->ev_sender;
2169         fctx = event->ev_arg;
2170         REQUIRE(VALID_FCTX(fctx));
2171         res = fctx->res;
2172
2173         UNUSED(task);
2174
2175         FCTXTRACE("finddone");
2176
2177         bucketnum = fctx->bucketnum;
2178         LOCK(&res->buckets[bucketnum].lock);
2179
2180         INSIST(fctx->pending > 0);
2181         fctx->pending--;
2182
2183         if (ADDRWAIT(fctx)) {
2184                 /*
2185                  * The fetch is waiting for a name to be found.
2186                  */
2187                 INSIST(!SHUTTINGDOWN(fctx));
2188                 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
2189                 if (event->ev_type == DNS_EVENT_ADBMOREADDRESSES)
2190                         want_try = ISC_TRUE;
2191                 else {
2192                         fctx->findfail++;
2193                         if (fctx->pending == 0) {
2194                                 /*
2195                                  * We've got nothing else to wait for and don't
2196                                  * know the answer.  There's nothing to do but
2197                                  * fail the fctx.
2198                                  */
2199                                 want_done = ISC_TRUE;
2200                         }
2201                 }
2202         } else if (SHUTTINGDOWN(fctx) && fctx->pending == 0 &&
2203                    fctx->nqueries == 0 && ISC_LIST_EMPTY(fctx->validators)) {
2204
2205                 if (fctx->references == 0) {
2206                         bucket_empty = fctx_unlink(fctx);
2207                         destroy = ISC_TRUE;
2208                 }
2209         }
2210         UNLOCK(&res->buckets[bucketnum].lock);
2211
2212         isc_event_free(&event);
2213         dns_adb_destroyfind(&find);
2214
2215         if (want_try)
2216                 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
2217         else if (want_done)
2218                 fctx_done(fctx, ISC_R_FAILURE, __LINE__);
2219         else if (destroy) {
2220                         fctx_destroy(fctx);
2221                 if (bucket_empty)
2222                         empty_bucket(res);
2223         }
2224 }
2225
2226
2227 static inline isc_boolean_t
2228 bad_server(fetchctx_t *fctx, isc_sockaddr_t *address) {
2229         isc_sockaddr_t *sa;
2230
2231         for (sa = ISC_LIST_HEAD(fctx->bad);
2232              sa != NULL;
2233              sa = ISC_LIST_NEXT(sa, link)) {
2234                 if (isc_sockaddr_equal(sa, address))
2235                         return (ISC_TRUE);
2236         }
2237
2238         return (ISC_FALSE);
2239 }
2240
2241 static inline isc_boolean_t
2242 mark_bad(fetchctx_t *fctx) {
2243         dns_adbfind_t *curr;
2244         dns_adbaddrinfo_t *addrinfo;
2245         isc_boolean_t all_bad = ISC_TRUE;
2246
2247         /*
2248          * Mark all known bad servers, so we don't try to talk to them
2249          * again.
2250          */
2251
2252         /*
2253          * Mark any bad nameservers.
2254          */
2255         for (curr = ISC_LIST_HEAD(fctx->finds);
2256              curr != NULL;
2257              curr = ISC_LIST_NEXT(curr, publink)) {
2258                 for (addrinfo = ISC_LIST_HEAD(curr->list);
2259                      addrinfo != NULL;
2260                      addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2261                         if (bad_server(fctx, &addrinfo->sockaddr))
2262                                 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2263                         else
2264                                 all_bad = ISC_FALSE;
2265                 }
2266         }
2267
2268         /*
2269          * Mark any bad forwarders.
2270          */
2271         for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
2272              addrinfo != NULL;
2273              addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2274                 if (bad_server(fctx, &addrinfo->sockaddr))
2275                         addrinfo->flags |= FCTX_ADDRINFO_MARK;
2276                 else
2277                         all_bad = ISC_FALSE;
2278         }
2279
2280         /*
2281          * Mark any bad alternates.
2282          */
2283         for (curr = ISC_LIST_HEAD(fctx->altfinds);
2284              curr != NULL;
2285              curr = ISC_LIST_NEXT(curr, publink)) {
2286                 for (addrinfo = ISC_LIST_HEAD(curr->list);
2287                      addrinfo != NULL;
2288                      addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2289                         if (bad_server(fctx, &addrinfo->sockaddr))
2290                                 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2291                         else
2292                                 all_bad = ISC_FALSE;
2293                 }
2294         }
2295
2296         for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
2297              addrinfo != NULL;
2298              addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2299                 if (bad_server(fctx, &addrinfo->sockaddr))
2300                         addrinfo->flags |= FCTX_ADDRINFO_MARK;
2301                 else
2302                         all_bad = ISC_FALSE;
2303         }
2304
2305         return (all_bad);
2306 }
2307
2308 static void
2309 add_bad(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, isc_result_t reason,
2310         badnstype_t badtype)
2311 {
2312         char namebuf[DNS_NAME_FORMATSIZE];
2313         char addrbuf[ISC_SOCKADDR_FORMATSIZE];
2314         char classbuf[64];
2315         char typebuf[64];
2316         char code[64];
2317         isc_buffer_t b;
2318         isc_sockaddr_t *sa;
2319         const char *spc = "";
2320         isc_sockaddr_t *address = &addrinfo->sockaddr;
2321
2322         if (reason == DNS_R_LAME)
2323                 fctx->lamecount++;
2324         else {
2325                 switch (badtype) {
2326                 case badns_unreachable:
2327                         fctx->neterr++;
2328                         break;
2329                 case badns_response:
2330                         fctx->badresp++;
2331                         break;
2332                 case badns_validation:
2333                         break;  /* counted as 'valfail' */
2334                 }
2335         }
2336
2337         if (bad_server(fctx, address)) {
2338                 /*
2339                  * We already know this server is bad.
2340                  */
2341                 return;
2342         }
2343
2344         FCTXTRACE("add_bad");
2345
2346         sa = isc_mem_get(fctx->mctx, sizeof(*sa));
2347         if (sa == NULL)
2348                 return;
2349         *sa = *address;
2350         ISC_LIST_INITANDAPPEND(fctx->bad, sa, link);
2351
2352         if (reason == DNS_R_LAME)       /* already logged */
2353                 return;
2354
2355         if (reason == DNS_R_UNEXPECTEDRCODE &&
2356             fctx->rmessage->rcode == dns_rcode_servfail &&
2357             ISFORWARDER(addrinfo))
2358                 return;
2359
2360         if (reason == DNS_R_UNEXPECTEDRCODE) {
2361                 isc_buffer_init(&b, code, sizeof(code) - 1);
2362                 dns_rcode_totext(fctx->rmessage->rcode, &b);
2363                 code[isc_buffer_usedlength(&b)] = '\0';
2364                 spc = " ";
2365         } else if (reason == DNS_R_UNEXPECTEDOPCODE) {
2366                 isc_buffer_init(&b, code, sizeof(code) - 1);
2367                 dns_opcode_totext((dns_opcode_t)fctx->rmessage->opcode, &b);
2368                 code[isc_buffer_usedlength(&b)] = '\0';
2369                 spc = " ";
2370         } else {
2371                 code[0] = '\0';
2372         }
2373         dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
2374         dns_rdatatype_format(fctx->type, typebuf, sizeof(typebuf));
2375         dns_rdataclass_format(fctx->res->rdclass, classbuf, sizeof(classbuf));
2376         isc_sockaddr_format(address, addrbuf, sizeof(addrbuf));
2377         isc_log_write(dns_lctx, DNS_LOGCATEGORY_LAME_SERVERS,
2378                       DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
2379                       "error (%s%s%s) resolving '%s/%s/%s': %s",
2380                       dns_result_totext(reason), spc, code,
2381                       namebuf, typebuf, classbuf, addrbuf);
2382 }
2383
2384 /*
2385  * Sort addrinfo list by RTT.
2386  */
2387 static void
2388 sort_adbfind(dns_adbfind_t *find) {
2389         dns_adbaddrinfo_t *best, *curr;
2390         dns_adbaddrinfolist_t sorted;
2391
2392         /* Lame N^2 bubble sort. */
2393         ISC_LIST_INIT(sorted);
2394         while (!ISC_LIST_EMPTY(find->list)) {
2395                 best = ISC_LIST_HEAD(find->list);
2396                 curr = ISC_LIST_NEXT(best, publink);
2397                 while (curr != NULL) {
2398                         if (curr->srtt < best->srtt)
2399                                 best = curr;
2400                         curr = ISC_LIST_NEXT(curr, publink);
2401                 }
2402                 ISC_LIST_UNLINK(find->list, best, publink);
2403                 ISC_LIST_APPEND(sorted, best, publink);
2404         }
2405         find->list = sorted;
2406 }
2407
2408 /*
2409  * Sort a list of finds by server RTT.
2410  */
2411 static void
2412 sort_finds(dns_adbfindlist_t *findlist) {
2413         dns_adbfind_t *best, *curr;
2414         dns_adbfindlist_t sorted;
2415         dns_adbaddrinfo_t *addrinfo, *bestaddrinfo;
2416
2417         /* Sort each find's addrinfo list by SRTT. */
2418         for (curr = ISC_LIST_HEAD(*findlist);
2419              curr != NULL;
2420              curr = ISC_LIST_NEXT(curr, publink))
2421                 sort_adbfind(curr);
2422
2423         /* Lame N^2 bubble sort. */
2424         ISC_LIST_INIT(sorted);
2425         while (!ISC_LIST_EMPTY(*findlist)) {
2426                 best = ISC_LIST_HEAD(*findlist);
2427                 bestaddrinfo = ISC_LIST_HEAD(best->list);
2428                 INSIST(bestaddrinfo != NULL);
2429                 curr = ISC_LIST_NEXT(best, publink);
2430                 while (curr != NULL) {
2431                         addrinfo = ISC_LIST_HEAD(curr->list);
2432                         INSIST(addrinfo != NULL);
2433                         if (addrinfo->srtt < bestaddrinfo->srtt) {
2434                                 best = curr;
2435                                 bestaddrinfo = addrinfo;
2436                         }
2437                         curr = ISC_LIST_NEXT(curr, publink);
2438                 }
2439                 ISC_LIST_UNLINK(*findlist, best, publink);
2440                 ISC_LIST_APPEND(sorted, best, publink);
2441         }
2442         *findlist = sorted;
2443 }
2444
2445 static void
2446 findname(fetchctx_t *fctx, dns_name_t *name, in_port_t port,
2447          unsigned int options, unsigned int flags, isc_stdtime_t now,
2448          isc_boolean_t *need_alternate)
2449 {
2450         dns_adbaddrinfo_t *ai;
2451         dns_adbfind_t *find;
2452         dns_resolver_t *res;
2453         isc_boolean_t unshared;
2454         isc_result_t result;
2455
2456         res = fctx->res;
2457         unshared = ISC_TF((fctx->options & DNS_FETCHOPT_UNSHARED) != 0);
2458         /*
2459          * If this name is a subdomain of the query domain, tell
2460          * the ADB to start looking using zone/hint data. This keeps us
2461          * from getting stuck if the nameserver is beneath the zone cut
2462          * and we don't know its address (e.g. because the A record has
2463          * expired).
2464          */
2465         if (dns_name_issubdomain(name, &fctx->domain))
2466                 options |= DNS_ADBFIND_STARTATZONE;
2467         options |= DNS_ADBFIND_GLUEOK;
2468         options |= DNS_ADBFIND_HINTOK;
2469
2470         /*
2471          * See what we know about this address.
2472          */
2473         find = NULL;
2474         result = dns_adb_createfind(fctx->adb,
2475                                     res->buckets[fctx->bucketnum].task,
2476                                     fctx_finddone, fctx, name,
2477                                     &fctx->name, fctx->type,
2478                                     options, now, NULL,
2479                                     res->view->dstport, &find);
2480         if (result != ISC_R_SUCCESS) {
2481                 if (result == DNS_R_ALIAS) {
2482                         /*
2483                          * XXXRTH  Follow the CNAME/DNAME chain?
2484                          */
2485                         dns_adb_destroyfind(&find);
2486                         fctx->adberr++;
2487                 }
2488         } else if (!ISC_LIST_EMPTY(find->list)) {
2489                 /*
2490                  * We have at least some of the addresses for the
2491                  * name.
2492                  */
2493                 INSIST((find->options & DNS_ADBFIND_WANTEVENT) == 0);
2494                 if (flags != 0 || port != 0) {
2495                         for (ai = ISC_LIST_HEAD(find->list);
2496                              ai != NULL;
2497                              ai = ISC_LIST_NEXT(ai, publink)) {
2498                                 ai->flags |= flags;
2499                                 if (port != 0)
2500                                         isc_sockaddr_setport(&ai->sockaddr,
2501                                                              port);
2502                         }
2503                 }
2504                 if ((flags & FCTX_ADDRINFO_FORWARDER) != 0)
2505                         ISC_LIST_APPEND(fctx->altfinds, find, publink);
2506                 else
2507                         ISC_LIST_APPEND(fctx->finds, find, publink);
2508         } else {
2509                 /*
2510                  * We don't know any of the addresses for this
2511                  * name.
2512                  */
2513                 if ((find->options & DNS_ADBFIND_WANTEVENT) != 0) {
2514                         /*
2515                          * We're looking for them and will get an
2516                          * event about it later.
2517                          */
2518                         fctx->pending++;
2519                         /*
2520                          * Bootstrap.
2521                          */
2522                         if (need_alternate != NULL &&
2523                             !*need_alternate && unshared &&
2524                             ((res->dispatches4 == NULL &&
2525                               find->result_v6 != DNS_R_NXDOMAIN) ||
2526                              (res->dispatches6 == NULL &&
2527                               find->result_v4 != DNS_R_NXDOMAIN)))
2528                                 *need_alternate = ISC_TRUE;
2529                 } else {
2530                         if ((find->options & DNS_ADBFIND_LAMEPRUNED) != 0)
2531                                 fctx->lamecount++; /* cached lame server */
2532                         else
2533                                 fctx->adberr++; /* unreachable server, etc. */
2534
2535                         /*
2536                          * If we know there are no addresses for
2537                          * the family we are using then try to add
2538                          * an alternative server.
2539                          */
2540                         if (need_alternate != NULL && !*need_alternate &&
2541                             ((res->dispatches4 == NULL &&
2542                               find->result_v6 == DNS_R_NXRRSET) ||
2543                              (res->dispatches6 == NULL &&
2544                               find->result_v4 == DNS_R_NXRRSET)))
2545                                 *need_alternate = ISC_TRUE;
2546                         dns_adb_destroyfind(&find);
2547                 }
2548         }
2549 }
2550
2551 static isc_boolean_t
2552 isstrictsubdomain(dns_name_t *name1, dns_name_t *name2) {
2553         int order;
2554         unsigned int nlabels;
2555         dns_namereln_t namereln;
2556
2557         namereln = dns_name_fullcompare(name1, name2, &order, &nlabels);
2558         return (ISC_TF(namereln == dns_namereln_subdomain));
2559 }
2560
2561 static isc_result_t
2562 fctx_getaddresses(fetchctx_t *fctx, isc_boolean_t badcache) {
2563         dns_rdata_t rdata = DNS_RDATA_INIT;
2564         isc_result_t result;
2565         dns_resolver_t *res;
2566         isc_stdtime_t now;
2567         unsigned int stdoptions = 0;
2568         isc_sockaddr_t *sa;
2569         dns_adbaddrinfo_t *ai;
2570         isc_boolean_t all_bad;
2571         dns_rdata_ns_t ns;
2572         isc_boolean_t need_alternate = ISC_FALSE;
2573
2574         FCTXTRACE("getaddresses");
2575
2576         /*
2577          * Don't pound on remote servers.  (Failsafe!)
2578          */
2579         fctx->restarts++;
2580         if (fctx->restarts > 10) {
2581                 FCTXTRACE("too many restarts");
2582                 return (DNS_R_SERVFAIL);
2583         }
2584
2585         res = fctx->res;
2586
2587         /*
2588          * Forwarders.
2589          */
2590
2591         INSIST(ISC_LIST_EMPTY(fctx->forwaddrs));
2592         INSIST(ISC_LIST_EMPTY(fctx->altaddrs));
2593
2594         /*
2595          * If this fctx has forwarders, use them; otherwise use any
2596          * selective forwarders specified in the view; otherwise use the
2597          * resolver's forwarders (if any).
2598          */
2599         sa = ISC_LIST_HEAD(fctx->forwarders);
2600         if (sa == NULL) {
2601                 dns_forwarders_t *forwarders = NULL;
2602                 dns_name_t *name = &fctx->name;
2603                 dns_name_t suffix;
2604                 unsigned int labels;
2605                 dns_fixedname_t fixed;
2606                 dns_name_t *domain;
2607
2608                 /*
2609                  * DS records are found in the parent server.
2610                  * Strip label to get the correct forwarder (if any).
2611                  */
2612                 if (dns_rdatatype_atparent(fctx->type) &&
2613                     dns_name_countlabels(name) > 1) {
2614                         dns_name_init(&suffix, NULL);
2615                         labels = dns_name_countlabels(name);
2616                         dns_name_getlabelsequence(name, 1, labels - 1, &suffix);
2617                         name = &suffix;
2618                 }
2619
2620                 dns_fixedname_init(&fixed);
2621                 domain = dns_fixedname_name(&fixed);
2622                 result = dns_fwdtable_find2(fctx->res->view->fwdtable, name,
2623                                             domain, &forwarders);
2624                 if (result == ISC_R_SUCCESS) {
2625                         sa = ISC_LIST_HEAD(forwarders->addrs);
2626                         fctx->fwdpolicy = forwarders->fwdpolicy;
2627                         if (fctx->fwdpolicy == dns_fwdpolicy_only &&
2628                             isstrictsubdomain(domain, &fctx->domain)) {
2629                                 dns_name_free(&fctx->domain, fctx->mctx);
2630                                 dns_name_init(&fctx->domain, NULL);
2631                                 result = dns_name_dup(domain, fctx->mctx,
2632                                                       &fctx->domain);
2633                                 if (result != ISC_R_SUCCESS)
2634                                         return (result);
2635                         }
2636                 }
2637         }
2638
2639         while (sa != NULL) {
2640                 if ((isc_sockaddr_pf(sa) == AF_INET &&
2641                          fctx->res->dispatches4 == NULL) ||
2642                     (isc_sockaddr_pf(sa) == AF_INET6 &&
2643                         fctx->res->dispatches6 == NULL)) {
2644                                 sa = ISC_LIST_NEXT(sa, link);
2645                                 continue;
2646                 }
2647                 ai = NULL;
2648                 result = dns_adb_findaddrinfo(fctx->adb,
2649                                               sa, &ai, 0);  /* XXXMLG */
2650                 if (result == ISC_R_SUCCESS) {
2651                         dns_adbaddrinfo_t *cur;
2652                         ai->flags |= FCTX_ADDRINFO_FORWARDER;
2653                         cur = ISC_LIST_HEAD(fctx->forwaddrs);
2654                         while (cur != NULL && cur->srtt < ai->srtt)
2655                                 cur = ISC_LIST_NEXT(cur, publink);
2656                         if (cur != NULL)
2657                                 ISC_LIST_INSERTBEFORE(fctx->forwaddrs, cur,
2658                                                       ai, publink);
2659                         else
2660                                 ISC_LIST_APPEND(fctx->forwaddrs, ai, publink);
2661                 }
2662                 sa = ISC_LIST_NEXT(sa, link);
2663         }
2664
2665         /*
2666          * If the forwarding policy is "only", we don't need the addresses
2667          * of the nameservers.
2668          */
2669         if (fctx->fwdpolicy == dns_fwdpolicy_only)
2670                 goto out;
2671
2672         /*
2673          * Normal nameservers.
2674          */
2675
2676         stdoptions = DNS_ADBFIND_WANTEVENT | DNS_ADBFIND_EMPTYEVENT;
2677         if (fctx->restarts == 1) {
2678                 /*
2679                  * To avoid sending out a flood of queries likely to
2680                  * result in NXRRSET, we suppress fetches for address
2681                  * families we don't have the first time through,
2682                  * provided that we have addresses in some family we
2683                  * can use.
2684                  *
2685                  * We don't want to set this option all the time, since
2686                  * if fctx->restarts > 1, we've clearly been having trouble
2687                  * with the addresses we had, so getting more could help.
2688                  */
2689                 stdoptions |= DNS_ADBFIND_AVOIDFETCHES;
2690         }
2691         if (res->dispatches4 != NULL)
2692                 stdoptions |= DNS_ADBFIND_INET;
2693         if (res->dispatches6 != NULL)
2694                 stdoptions |= DNS_ADBFIND_INET6;
2695         isc_stdtime_get(&now);
2696
2697         INSIST(ISC_LIST_EMPTY(fctx->finds));
2698         INSIST(ISC_LIST_EMPTY(fctx->altfinds));
2699
2700         for (result = dns_rdataset_first(&fctx->nameservers);
2701              result == ISC_R_SUCCESS;
2702              result = dns_rdataset_next(&fctx->nameservers))
2703         {
2704                 dns_rdataset_current(&fctx->nameservers, &rdata);
2705                 /*
2706                  * Extract the name from the NS record.
2707                  */
2708                 result = dns_rdata_tostruct(&rdata, &ns, NULL);
2709                 if (result != ISC_R_SUCCESS)
2710                         continue;
2711
2712                 findname(fctx, &ns.name, 0, stdoptions, 0, now,
2713                          &need_alternate);
2714                 dns_rdata_reset(&rdata);
2715                 dns_rdata_freestruct(&ns);
2716         }
2717         if (result != ISC_R_NOMORE)
2718                 return (result);
2719
2720         /*
2721          * Do we need to use 6 to 4?
2722          */
2723         if (need_alternate) {
2724                 int family;
2725                 alternate_t *a;
2726                 family = (res->dispatches6 != NULL) ? AF_INET6 : AF_INET;
2727                 for (a = ISC_LIST_HEAD(fctx->res->alternates);
2728                      a != NULL;
2729                      a = ISC_LIST_NEXT(a, link)) {
2730                         if (!a->isaddress) {
2731                                 findname(fctx, &a->_u._n.name, a->_u._n.port,
2732                                          stdoptions, FCTX_ADDRINFO_FORWARDER,
2733                                          now, NULL);
2734                                 continue;
2735                         }
2736                         if (isc_sockaddr_pf(&a->_u.addr) != family)
2737                                 continue;
2738                         ai = NULL;
2739                         result = dns_adb_findaddrinfo(fctx->adb, &a->_u.addr,
2740                                                       &ai, 0);
2741                         if (result == ISC_R_SUCCESS) {
2742                                 dns_adbaddrinfo_t *cur;
2743                                 ai->flags |= FCTX_ADDRINFO_FORWARDER;
2744                                 cur = ISC_LIST_HEAD(fctx->altaddrs);
2745                                 while (cur != NULL && cur->srtt < ai->srtt)
2746                                         cur = ISC_LIST_NEXT(cur, publink);
2747                                 if (cur != NULL)
2748                                         ISC_LIST_INSERTBEFORE(fctx->altaddrs,
2749                                                               cur, ai, publink);
2750                                 else
2751                                         ISC_LIST_APPEND(fctx->altaddrs, ai,
2752                                                         publink);
2753                         }
2754                 }
2755         }
2756
2757  out:
2758         /*
2759          * Mark all known bad servers.
2760          */
2761         all_bad = mark_bad(fctx);
2762
2763         /*
2764          * How are we doing?
2765          */
2766         if (all_bad) {
2767                 /*
2768                  * We've got no addresses.
2769                  */
2770                 if (fctx->pending > 0) {
2771                         /*
2772                          * We're fetching the addresses, but don't have any
2773                          * yet.   Tell the caller to wait for an answer.
2774                          */
2775                         result = DNS_R_WAIT;
2776                 } else {
2777                         isc_time_t expire;
2778                         isc_interval_t i;
2779                         /*
2780                          * We've lost completely.  We don't know any
2781                          * addresses, and the ADB has told us it can't get
2782                          * them.
2783                          */
2784                         FCTXTRACE("no addresses");
2785                         isc_interval_set(&i, DNS_BADCACHE_TTL(fctx), 0);
2786                         result = isc_time_nowplusinterval(&expire, &i);
2787                         if (badcache &&
2788                             (fctx->type == dns_rdatatype_dnskey ||
2789                              fctx->type == dns_rdatatype_dlv ||
2790                              fctx->type == dns_rdatatype_ds) &&
2791                              result == ISC_R_SUCCESS)
2792                                 dns_resolver_addbadcache(fctx->res,
2793                                                          &fctx->name,
2794                                                          fctx->type, &expire);
2795                         result = ISC_R_FAILURE;
2796                 }
2797         } else {
2798                 /*
2799                  * We've found some addresses.  We might still be looking
2800                  * for more addresses.
2801                  */
2802                 sort_finds(&fctx->finds);
2803                 sort_finds(&fctx->altfinds);
2804                 result = ISC_R_SUCCESS;
2805         }
2806
2807         return (result);
2808 }
2809
2810 static inline void
2811 possibly_mark(fetchctx_t *fctx, dns_adbaddrinfo_t *addr)
2812 {
2813         isc_netaddr_t na;
2814         char buf[ISC_NETADDR_FORMATSIZE];
2815         isc_sockaddr_t *sa;
2816         isc_boolean_t aborted = ISC_FALSE;
2817         isc_boolean_t bogus;
2818         dns_acl_t *blackhole;
2819         isc_netaddr_t ipaddr;
2820         dns_peer_t *peer = NULL;
2821         dns_resolver_t *res;
2822         const char *msg = NULL;
2823
2824         sa = &addr->sockaddr;
2825
2826         res = fctx->res;
2827         isc_netaddr_fromsockaddr(&ipaddr, sa);
2828         blackhole = dns_dispatchmgr_getblackhole(res->dispatchmgr);
2829         (void) dns_peerlist_peerbyaddr(res->view->peers, &ipaddr, &peer);
2830
2831         if (blackhole != NULL) {
2832                 int match;
2833
2834                 if (dns_acl_match(&ipaddr, NULL, blackhole,
2835                                   &res->view->aclenv,
2836                                   &match, NULL) == ISC_R_SUCCESS &&
2837                     match > 0)
2838                         aborted = ISC_TRUE;
2839         }
2840
2841         if (peer != NULL &&
2842             dns_peer_getbogus(peer, &bogus) == ISC_R_SUCCESS &&
2843             bogus)
2844                 aborted = ISC_TRUE;
2845
2846         if (aborted) {
2847                 addr->flags |= FCTX_ADDRINFO_MARK;
2848                 msg = "ignoring blackholed / bogus server: ";
2849         } else if (isc_sockaddr_ismulticast(sa)) {
2850                 addr->flags |= FCTX_ADDRINFO_MARK;
2851                 msg = "ignoring multicast address: ";
2852         } else if (isc_sockaddr_isexperimental(sa)) {
2853                 addr->flags |= FCTX_ADDRINFO_MARK;
2854                 msg = "ignoring experimental address: ";
2855         } else if (sa->type.sa.sa_family != AF_INET6) {
2856                 return;
2857         } else if (IN6_IS_ADDR_V4MAPPED(&sa->type.sin6.sin6_addr)) {
2858                 addr->flags |= FCTX_ADDRINFO_MARK;
2859                 msg = "ignoring IPv6 mapped IPV4 address: ";
2860         } else if (IN6_IS_ADDR_V4COMPAT(&sa->type.sin6.sin6_addr)) {
2861                 addr->flags |= FCTX_ADDRINFO_MARK;
2862                 msg = "ignoring IPv6 compatibility IPV4 address: ";
2863         } else
2864                 return;
2865
2866         if (!isc_log_wouldlog(dns_lctx, ISC_LOG_DEBUG(3)))
2867                 return;
2868
2869         isc_netaddr_fromsockaddr(&na, sa);
2870         isc_netaddr_format(&na, buf, sizeof(buf));
2871         FCTXTRACE2(msg, buf);
2872 }
2873
2874 static inline dns_adbaddrinfo_t *
2875 fctx_nextaddress(fetchctx_t *fctx) {
2876         dns_adbfind_t *find, *start;
2877         dns_adbaddrinfo_t *addrinfo;
2878         dns_adbaddrinfo_t *faddrinfo;
2879
2880         /*
2881          * Return the next untried address, if any.
2882          */
2883
2884         /*
2885          * Find the first unmarked forwarder (if any).
2886          */
2887         for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
2888              addrinfo != NULL;
2889              addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2890                 if (!UNMARKED(addrinfo))
2891                         continue;
2892                 possibly_mark(fctx, addrinfo);
2893                 if (UNMARKED(addrinfo)) {
2894                         addrinfo->flags |= FCTX_ADDRINFO_MARK;
2895                         fctx->find = NULL;
2896                         return (addrinfo);
2897                 }
2898         }
2899
2900         /*
2901          * No forwarders.  Move to the next find.
2902          */
2903
2904         fctx->attributes |= FCTX_ATTR_TRIEDFIND;
2905
2906         find = fctx->find;
2907         if (find == NULL)
2908                 find = ISC_LIST_HEAD(fctx->finds);
2909         else {
2910                 find = ISC_LIST_NEXT(find, publink);
2911                 if (find == NULL)
2912                         find = ISC_LIST_HEAD(fctx->finds);
2913         }
2914
2915         /*
2916          * Find the first unmarked addrinfo.
2917          */
2918         addrinfo = NULL;
2919         if (find != NULL) {
2920                 start = find;
2921                 do {
2922                         for (addrinfo = ISC_LIST_HEAD(find->list);
2923                              addrinfo != NULL;
2924                              addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2925                                 if (!UNMARKED(addrinfo))
2926                                         continue;
2927                                 possibly_mark(fctx, addrinfo);
2928                                 if (UNMARKED(addrinfo)) {
2929                                         addrinfo->flags |= FCTX_ADDRINFO_MARK;
2930                                         break;
2931                                 }
2932                         }
2933                         if (addrinfo != NULL)
2934                                 break;
2935                         find = ISC_LIST_NEXT(find, publink);
2936                         if (find == NULL)
2937                                 find = ISC_LIST_HEAD(fctx->finds);
2938                 } while (find != start);
2939         }
2940
2941         fctx->find = find;
2942         if (addrinfo != NULL)
2943                 return (addrinfo);
2944
2945         /*
2946          * No nameservers left.  Try alternates.
2947          */
2948
2949         fctx->attributes |= FCTX_ATTR_TRIEDALT;
2950
2951         find = fctx->altfind;
2952         if (find == NULL)
2953                 find = ISC_LIST_HEAD(fctx->altfinds);
2954         else {
2955                 find = ISC_LIST_NEXT(find, publink);
2956                 if (find == NULL)
2957                         find = ISC_LIST_HEAD(fctx->altfinds);
2958         }
2959
2960         /*
2961          * Find the first unmarked addrinfo.
2962          */
2963         addrinfo = NULL;
2964         if (find != NULL) {
2965                 start = find;
2966                 do {
2967                         for (addrinfo = ISC_LIST_HEAD(find->list);
2968                              addrinfo != NULL;
2969                              addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2970                                 if (!UNMARKED(addrinfo))
2971                                         continue;
2972                                 possibly_mark(fctx, addrinfo);
2973                                 if (UNMARKED(addrinfo)) {
2974                                         addrinfo->flags |= FCTX_ADDRINFO_MARK;
2975                                         break;
2976                                 }
2977                         }
2978                         if (addrinfo != NULL)
2979                                 break;
2980                         find = ISC_LIST_NEXT(find, publink);
2981                         if (find == NULL)
2982                                 find = ISC_LIST_HEAD(fctx->altfinds);
2983                 } while (find != start);
2984         }
2985
2986         faddrinfo = addrinfo;
2987
2988         /*
2989          * See if we have a better alternate server by address.
2990          */
2991
2992         for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
2993              addrinfo != NULL;
2994              addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2995                 if (!UNMARKED(addrinfo))
2996                         continue;
2997                 possibly_mark(fctx, addrinfo);
2998                 if (UNMARKED(addrinfo) &&
2999                     (faddrinfo == NULL ||
3000                      addrinfo->srtt < faddrinfo->srtt)) {
3001                         if (faddrinfo != NULL)
3002                                 faddrinfo->flags &= ~FCTX_ADDRINFO_MARK;
3003                         addrinfo->flags |= FCTX_ADDRINFO_MARK;
3004                         break;
3005                 }
3006         }
3007
3008         if (addrinfo == NULL) {
3009                 addrinfo = faddrinfo;
3010                 fctx->altfind = find;
3011         }
3012
3013         return (addrinfo);
3014 }
3015
3016 static void
3017 fctx_try(fetchctx_t *fctx, isc_boolean_t retrying, isc_boolean_t badcache) {
3018         isc_result_t result;
3019         dns_adbaddrinfo_t *addrinfo;
3020
3021         FCTXTRACE("try");
3022
3023         REQUIRE(!ADDRWAIT(fctx));
3024
3025         addrinfo = fctx_nextaddress(fctx);
3026         if (addrinfo == NULL) {
3027                 /*
3028                  * We have no more addresses.  Start over.
3029                  */
3030                 fctx_cancelqueries(fctx, ISC_TRUE);
3031                 fctx_cleanupfinds(fctx);
3032                 fctx_cleanupaltfinds(fctx);
3033                 fctx_cleanupforwaddrs(fctx);
3034                 fctx_cleanupaltaddrs(fctx);
3035                 result = fctx_getaddresses(fctx, badcache);
3036                 if (result == DNS_R_WAIT) {
3037                         /*
3038                          * Sleep waiting for addresses.
3039                          */
3040                         FCTXTRACE("addrwait");
3041                         fctx->attributes |= FCTX_ATTR_ADDRWAIT;
3042                         return;
3043                 } else if (result != ISC_R_SUCCESS) {
3044                         /*
3045                          * Something bad happened.
3046                          */
3047                         fctx_done(fctx, result, __LINE__);
3048                         return;
3049                 }
3050
3051                 addrinfo = fctx_nextaddress(fctx);
3052                 /*
3053                  * While we may have addresses from the ADB, they
3054                  * might be bad ones.  In this case, return SERVFAIL.
3055                  */
3056                 if (addrinfo == NULL) {
3057                         fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
3058                         return;
3059                 }
3060         }
3061
3062         result = fctx_query(fctx, addrinfo, fctx->options);
3063         if (result != ISC_R_SUCCESS)
3064                 fctx_done(fctx, result, __LINE__);
3065         else if (retrying)
3066                 inc_stats(fctx->res, dns_resstatscounter_retry);
3067 }
3068
3069 static isc_boolean_t
3070 fctx_unlink(fetchctx_t *fctx) {
3071         dns_resolver_t *res;
3072         unsigned int bucketnum;
3073
3074         /*
3075          * Caller must be holding the bucket lock.
3076          */
3077
3078         REQUIRE(VALID_FCTX(fctx));
3079         REQUIRE(fctx->state == fetchstate_done ||
3080                 fctx->state == fetchstate_init);
3081         REQUIRE(ISC_LIST_EMPTY(fctx->events));
3082         REQUIRE(ISC_LIST_EMPTY(fctx->queries));
3083         REQUIRE(ISC_LIST_EMPTY(fctx->finds));
3084         REQUIRE(ISC_LIST_EMPTY(fctx->altfinds));
3085         REQUIRE(fctx->pending == 0);
3086         REQUIRE(fctx->references == 0);
3087         REQUIRE(ISC_LIST_EMPTY(fctx->validators));
3088
3089         FCTXTRACE("unlink");
3090
3091         res = fctx->res;
3092         bucketnum = fctx->bucketnum;
3093
3094         ISC_LIST_UNLINK(res->buckets[bucketnum].fctxs, fctx, link);
3095
3096         LOCK(&res->nlock);
3097         res->nfctx--;
3098         UNLOCK(&res->nlock);
3099
3100         if (res->buckets[bucketnum].exiting &&
3101             ISC_LIST_EMPTY(res->buckets[bucketnum].fctxs))
3102                 return (ISC_TRUE);
3103
3104         return (ISC_FALSE);
3105 }
3106
3107 static void
3108 fctx_destroy(fetchctx_t *fctx) {
3109         isc_sockaddr_t *sa, *next_sa;
3110
3111         REQUIRE(VALID_FCTX(fctx));
3112         REQUIRE(fctx->state == fetchstate_done ||
3113                 fctx->state == fetchstate_init);
3114         REQUIRE(ISC_LIST_EMPTY(fctx->events));
3115         REQUIRE(ISC_LIST_EMPTY(fctx->queries));
3116         REQUIRE(ISC_LIST_EMPTY(fctx->finds));
3117         REQUIRE(ISC_LIST_EMPTY(fctx->altfinds));
3118         REQUIRE(fctx->pending == 0);
3119         REQUIRE(fctx->references == 0);
3120         REQUIRE(ISC_LIST_EMPTY(fctx->validators));
3121         REQUIRE(!ISC_LINK_LINKED(fctx, link));
3122
3123         FCTXTRACE("destroy");
3124
3125         /*
3126          * Free bad.
3127          */
3128         for (sa = ISC_LIST_HEAD(fctx->bad);
3129              sa != NULL;
3130              sa = next_sa) {
3131                 next_sa = ISC_LIST_NEXT(sa, link);
3132                 ISC_LIST_UNLINK(fctx->bad, sa, link);
3133                 isc_mem_put(fctx->mctx, sa, sizeof(*sa));
3134         }
3135
3136         for (sa = ISC_LIST_HEAD(fctx->edns);
3137              sa != NULL;
3138              sa = next_sa) {
3139                 next_sa = ISC_LIST_NEXT(sa, link);
3140                 ISC_LIST_UNLINK(fctx->edns, sa, link);
3141                 isc_mem_put(fctx->mctx, sa, sizeof(*sa));
3142         }
3143
3144         for (sa = ISC_LIST_HEAD(fctx->edns512);
3145              sa != NULL;
3146              sa = next_sa) {
3147                 next_sa = ISC_LIST_NEXT(sa, link);
3148                 ISC_LIST_UNLINK(fctx->edns512, sa, link);
3149                 isc_mem_put(fctx->mctx, sa, sizeof(*sa));
3150         }
3151
3152         for (sa = ISC_LIST_HEAD(fctx->bad_edns);
3153              sa != NULL;
3154              sa = next_sa) {
3155                 next_sa = ISC_LIST_NEXT(sa, link);
3156                 ISC_LIST_UNLINK(fctx->bad_edns, sa, link);
3157                 isc_mem_put(fctx->mctx, sa, sizeof(*sa));
3158         }
3159
3160         isc_timer_detach(&fctx->timer);
3161         dns_message_destroy(&fctx->rmessage);
3162         dns_message_destroy(&fctx->qmessage);
3163         if (dns_name_countlabels(&fctx->domain) > 0)
3164                 dns_name_free(&fctx->domain, fctx->mctx);
3165         if (dns_rdataset_isassociated(&fctx->nameservers))
3166                 dns_rdataset_disassociate(&fctx->nameservers);
3167         dns_name_free(&fctx->name, fctx->mctx);
3168         dns_db_detach(&fctx->cache);
3169         dns_adb_detach(&fctx->adb);
3170         isc_mem_free(fctx->mctx, fctx->info);
3171         isc_mem_putanddetach(&fctx->mctx, fctx, sizeof(*fctx));
3172 }
3173
3174 /*
3175  * Fetch event handlers.
3176  */
3177
3178 static void
3179 fctx_timeout(isc_task_t *task, isc_event_t *event) {
3180         fetchctx_t *fctx = event->ev_arg;
3181         isc_timerevent_t *tevent = (isc_timerevent_t *)event;
3182         resquery_t *query;
3183
3184         REQUIRE(VALID_FCTX(fctx));
3185
3186         UNUSED(task);
3187
3188         FCTXTRACE("timeout");
3189
3190         inc_stats(fctx->res, dns_resstatscounter_querytimeout);
3191
3192         if (event->ev_type == ISC_TIMEREVENT_LIFE) {
3193                 fctx->reason = NULL;
3194                 fctx_done(fctx, ISC_R_TIMEDOUT, __LINE__);
3195         } else {
3196                 isc_result_t result;
3197
3198                 fctx->timeouts++;
3199                 fctx->timeout = ISC_TRUE;
3200                 /*
3201                  * We could cancel the running queries here, or we could let
3202                  * them keep going.  Since we normally use separate sockets for
3203                  * different queries, we adopt the former approach to reduce
3204                  * the number of open sockets: cancel the oldest query if it
3205                  * expired after the query had started (this is usually the
3206                  * case but is not always so, depending on the task schedule
3207                  * timing).
3208                  */
3209                 query = ISC_LIST_HEAD(fctx->queries);
3210                 if (query != NULL &&
3211                     isc_time_compare(&tevent->due, &query->start) >= 0) {
3212                         fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
3213                 }
3214                 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
3215                 /*
3216                  * Our timer has triggered.  Reestablish the fctx lifetime
3217                  * timer.
3218                  */
3219                 result = fctx_starttimer(fctx);
3220                 if (result != ISC_R_SUCCESS)
3221                         fctx_done(fctx, result, __LINE__);
3222                 else
3223                         /*
3224                          * Keep trying.
3225                          */
3226                         fctx_try(fctx, ISC_TRUE, ISC_FALSE);
3227         }
3228
3229         isc_event_free(&event);
3230 }
3231
3232 static void
3233 fctx_shutdown(fetchctx_t *fctx) {
3234         isc_event_t *cevent;
3235
3236         /*
3237          * Start the shutdown process for fctx, if it isn't already underway.
3238          */
3239
3240         FCTXTRACE("shutdown");
3241
3242         /*
3243          * The caller must be holding the appropriate bucket lock.
3244          */
3245
3246         if (fctx->want_shutdown)
3247                 return;
3248
3249         fctx->want_shutdown = ISC_TRUE;
3250
3251         /*
3252          * Unless we're still initializing (in which case the
3253          * control event is still outstanding), we need to post
3254          * the control event to tell the fetch we want it to
3255          * exit.
3256          */
3257         if (fctx->state != fetchstate_init) {
3258                 cevent = &fctx->control_event;
3259                 isc_task_send(fctx->res->buckets[fctx->bucketnum].task,
3260                               &cevent);
3261         }
3262 }
3263
3264 static void
3265 fctx_doshutdown(isc_task_t *task, isc_event_t *event) {
3266         fetchctx_t *fctx = event->ev_arg;
3267         isc_boolean_t bucket_empty = ISC_FALSE;
3268         dns_resolver_t *res;
3269         unsigned int bucketnum;
3270         dns_validator_t *validator;
3271         isc_boolean_t destroy = ISC_FALSE;
3272
3273         REQUIRE(VALID_FCTX(fctx));
3274
3275         UNUSED(task);
3276
3277         res = fctx->res;
3278         bucketnum = fctx->bucketnum;
3279
3280         FCTXTRACE("doshutdown");
3281
3282         /*
3283          * An fctx that is shutting down is no longer in ADDRWAIT mode.
3284          */
3285         fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
3286
3287         /*
3288          * Cancel all pending validators.  Note that this must be done
3289          * without the bucket lock held, since that could cause deadlock.
3290          */
3291         validator = ISC_LIST_HEAD(fctx->validators);
3292         while (validator != NULL) {
3293                 dns_validator_cancel(validator);
3294                 validator = ISC_LIST_NEXT(validator, link);
3295         }
3296
3297         if (fctx->nsfetch != NULL)
3298                 dns_resolver_cancelfetch(fctx->nsfetch);
3299
3300         /*
3301          * Shut down anything that is still running on behalf of this
3302          * fetch.  To avoid deadlock with the ADB, we must do this
3303          * before we lock the bucket lock.
3304          */
3305         fctx_stopeverything(fctx, ISC_FALSE);
3306
3307         LOCK(&res->buckets[bucketnum].lock);
3308
3309         fctx->attributes |= FCTX_ATTR_SHUTTINGDOWN;
3310
3311         INSIST(fctx->state == fetchstate_active ||
3312                fctx->state == fetchstate_done);
3313         INSIST(fctx->want_shutdown);
3314
3315         if (fctx->state != fetchstate_done) {
3316                 fctx->state = fetchstate_done;
3317                 fctx_sendevents(fctx, ISC_R_CANCELED, __LINE__);
3318         }
3319
3320         if (fctx->references == 0 && fctx->pending == 0 &&
3321             fctx->nqueries == 0 && ISC_LIST_EMPTY(fctx->validators)) {
3322                 bucket_empty = fctx_unlink(fctx);
3323                 destroy = ISC_TRUE;
3324         }
3325
3326         UNLOCK(&res->buckets[bucketnum].lock);
3327
3328         if (destroy) {
3329                 fctx_destroy(fctx);
3330                 if (bucket_empty)
3331                         empty_bucket(res);
3332         }
3333 }
3334
3335 static void
3336 fctx_start(isc_task_t *task, isc_event_t *event) {
3337         fetchctx_t *fctx = event->ev_arg;
3338         isc_boolean_t done = ISC_FALSE, bucket_empty = ISC_FALSE;
3339         dns_resolver_t *res;
3340         unsigned int bucketnum;
3341         isc_boolean_t destroy = ISC_FALSE;
3342
3343         REQUIRE(VALID_FCTX(fctx));
3344
3345         UNUSED(task);
3346
3347         res = fctx->res;
3348         bucketnum = fctx->bucketnum;
3349
3350         FCTXTRACE("start");
3351
3352         LOCK(&res->buckets[bucketnum].lock);
3353
3354         INSIST(fctx->state == fetchstate_init);
3355         if (fctx->want_shutdown) {
3356                 /*
3357                  * We haven't started this fctx yet, and we've been requested
3358                  * to shut it down.
3359                  */
3360                 fctx->attributes |= FCTX_ATTR_SHUTTINGDOWN;
3361                 fctx->state = fetchstate_done;
3362                 fctx_sendevents(fctx, ISC_R_CANCELED, __LINE__);
3363                 /*
3364                  * Since we haven't started, we INSIST that we have no
3365                  * pending ADB finds and no pending validations.
3366                  */
3367                 INSIST(fctx->pending == 0);
3368                 INSIST(fctx->nqueries == 0);
3369                 INSIST(ISC_LIST_EMPTY(fctx->validators));
3370                 if (fctx->references == 0) {
3371                         /*
3372                          * It's now safe to destroy this fctx.
3373                          */
3374                         bucket_empty = fctx_unlink(fctx);
3375                         destroy = ISC_TRUE;
3376                 }
3377                 done = ISC_TRUE;
3378         } else {
3379                 /*
3380                  * Normal fctx startup.
3381                  */
3382                 fctx->state = fetchstate_active;
3383                 /*
3384                  * Reset the control event for later use in shutting down
3385                  * the fctx.
3386                  */
3387                 ISC_EVENT_INIT(event, sizeof(*event), 0, NULL,
3388                                DNS_EVENT_FETCHCONTROL, fctx_doshutdown, fctx,
3389                                NULL, NULL, NULL);
3390         }
3391
3392         UNLOCK(&res->buckets[bucketnum].lock);
3393
3394         if (!done) {
3395                 isc_result_t result;
3396
3397                 INSIST(!destroy);
3398
3399                 /*
3400                  * All is well.  Start working on the fetch.
3401                  */
3402                 result = fctx_starttimer(fctx);
3403                 if (result != ISC_R_SUCCESS)
3404                         fctx_done(fctx, result, __LINE__);
3405                 else
3406                         fctx_try(fctx, ISC_FALSE, ISC_FALSE);
3407         } else if (destroy) {
3408                         fctx_destroy(fctx);
3409                 if (bucket_empty)
3410                         empty_bucket(res);
3411         }
3412 }
3413
3414 /*
3415  * Fetch Creation, Joining, and Cancelation.
3416  */
3417
3418 static inline isc_result_t
3419 fctx_join(fetchctx_t *fctx, isc_task_t *task, isc_sockaddr_t *client,
3420           dns_messageid_t id, isc_taskaction_t action, void *arg,
3421           dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset,
3422           dns_fetch_t *fetch)
3423 {
3424         isc_task_t *clone;
3425         dns_fetchevent_t *event;
3426
3427         FCTXTRACE("join");
3428
3429         /*
3430          * We store the task we're going to send this event to in the
3431          * sender field.  We'll make the fetch the sender when we actually
3432          * send the event.
3433          */
3434         clone = NULL;
3435         isc_task_attach(task, &clone);
3436         event = (dns_fetchevent_t *)
3437                 isc_event_allocate(fctx->res->mctx, clone, DNS_EVENT_FETCHDONE,
3438                                    action, arg, sizeof(*event));
3439         if (event == NULL) {
3440                 isc_task_detach(&clone);
3441                 return (ISC_R_NOMEMORY);
3442         }
3443         event->result = DNS_R_SERVFAIL;
3444         event->qtype = fctx->type;
3445         event->db = NULL;
3446         event->node = NULL;
3447         event->rdataset = rdataset;
3448         event->sigrdataset = sigrdataset;
3449         event->fetch = fetch;
3450         event->client = client;
3451         event->id = id;
3452         dns_fixedname_init(&event->foundname);
3453
3454         /*
3455          * Make sure that we can store the sigrdataset in the
3456          * first event if it is needed by any of the events.
3457          */
3458         if (event->sigrdataset != NULL)
3459                 ISC_LIST_PREPEND(fctx->events, event, ev_link);
3460         else
3461                 ISC_LIST_APPEND(fctx->events, event, ev_link);
3462         fctx->references++;
3463         fctx->client = client;
3464
3465         fetch->magic = DNS_FETCH_MAGIC;
3466         fetch->private = fctx;
3467
3468         return (ISC_R_SUCCESS);
3469 }
3470
3471 static inline void
3472 log_ns_ttl(fetchctx_t *fctx, const char *where) {
3473         char namebuf[DNS_NAME_FORMATSIZE];
3474         char domainbuf[DNS_NAME_FORMATSIZE];
3475
3476         dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
3477         dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
3478         isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
3479                       DNS_LOGMODULE_RESOLVER, ISC_LOG_DEBUG(10),
3480                       "log_ns_ttl: fctx %p: %s: %s (in '%s'?): %u %u",
3481                       fctx, where, namebuf, domainbuf,
3482                       fctx->ns_ttl_ok, fctx->ns_ttl);
3483 }
3484
3485 static isc_result_t
3486 fctx_create(dns_resolver_t *res, dns_name_t *name, dns_rdatatype_t type,
3487             dns_name_t *domain, dns_rdataset_t *nameservers,
3488             unsigned int options, unsigned int bucketnum, fetchctx_t **fctxp)
3489 {
3490         fetchctx_t *fctx;
3491         isc_result_t result;
3492         isc_result_t iresult;
3493         isc_interval_t interval;
3494         dns_fixedname_t fixed;
3495         unsigned int findoptions = 0;
3496         char buf[DNS_NAME_FORMATSIZE + DNS_RDATATYPE_FORMATSIZE];
3497         char typebuf[DNS_RDATATYPE_FORMATSIZE];
3498         dns_name_t suffix;
3499         isc_mem_t *mctx;
3500
3501         /*
3502          * Caller must be holding the lock for bucket number 'bucketnum'.
3503          */
3504         REQUIRE(fctxp != NULL && *fctxp == NULL);
3505
3506         mctx = res->buckets[bucketnum].mctx;
3507         fctx = isc_mem_get(mctx, sizeof(*fctx));
3508         if (fctx == NULL)
3509                 return (ISC_R_NOMEMORY);
3510         dns_name_format(name, buf, sizeof(buf));
3511         dns_rdatatype_format(type, typebuf, sizeof(typebuf));
3512         strcat(buf, "/");       /* checked */
3513         strcat(buf, typebuf);   /* checked */
3514         fctx->info = isc_mem_strdup(mctx, buf);
3515         if (fctx->info == NULL) {
3516                 result = ISC_R_NOMEMORY;
3517                 goto cleanup_fetch;
3518         }
3519         FCTXTRACE("create");
3520         dns_name_init(&fctx->name, NULL);
3521         result = dns_name_dup(name, mctx, &fctx->name);
3522         if (result != ISC_R_SUCCESS)
3523                 goto cleanup_info;
3524         dns_name_init(&fctx->domain, NULL);
3525         dns_rdataset_init(&fctx->nameservers);
3526
3527         fctx->type = type;
3528         fctx->options = options;
3529         /*
3530          * Note!  We do not attach to the task.  We are relying on the
3531          * resolver to ensure that this task doesn't go away while we are
3532          * using it.
3533          */
3534         fctx->res = res;
3535         fctx->references = 0;
3536         fctx->bucketnum = bucketnum;
3537         fctx->state = fetchstate_init;
3538         fctx->want_shutdown = ISC_FALSE;
3539         fctx->cloned = ISC_FALSE;
3540         ISC_LIST_INIT(fctx->queries);
3541         ISC_LIST_INIT(fctx->finds);
3542         ISC_LIST_INIT(fctx->altfinds);
3543         ISC_LIST_INIT(fctx->forwaddrs);
3544         ISC_LIST_INIT(fctx->altaddrs);
3545         ISC_LIST_INIT(fctx->forwarders);
3546         fctx->fwdpolicy = dns_fwdpolicy_none;
3547         ISC_LIST_INIT(fctx->bad);
3548         ISC_LIST_INIT(fctx->edns);
3549         ISC_LIST_INIT(fctx->edns512);
3550         ISC_LIST_INIT(fctx->bad_edns);
3551         ISC_LIST_INIT(fctx->validators);
3552         fctx->validator = NULL;
3553         fctx->find = NULL;
3554         fctx->altfind = NULL;
3555         fctx->pending = 0;
3556         fctx->restarts = 0;
3557         fctx->querysent = 0;
3558         fctx->referrals = 0;
3559         TIME_NOW(&fctx->start);
3560         fctx->timeouts = 0;
3561         fctx->lamecount = 0;
3562         fctx->adberr = 0;
3563         fctx->neterr = 0;
3564         fctx->badresp = 0;
3565         fctx->findfail = 0;
3566         fctx->valfail = 0;
3567         fctx->result = ISC_R_FAILURE;
3568         fctx->vresult = ISC_R_SUCCESS;
3569         fctx->exitline = -1;    /* sentinel */
3570         fctx->logged = ISC_FALSE;
3571         fctx->attributes = 0;
3572         fctx->spilled = ISC_FALSE;
3573         fctx->nqueries = 0;
3574         fctx->reason = NULL;
3575         fctx->rand_buf = 0;
3576         fctx->rand_bits = 0;
3577         fctx->timeout = ISC_FALSE;
3578         fctx->addrinfo = NULL;
3579         fctx->client = NULL;
3580         fctx->ns_ttl = 0;
3581         fctx->ns_ttl_ok = ISC_FALSE;
3582
3583         dns_name_init(&fctx->nsname, NULL);
3584         fctx->nsfetch = NULL;
3585         dns_rdataset_init(&fctx->nsrrset);
3586
3587         if (domain == NULL) {
3588                 dns_forwarders_t *forwarders = NULL;
3589                 unsigned int labels;
3590                 dns_name_t *fwdname = name;
3591
3592                 /*
3593                  * DS records are found in the parent server.
3594                  * Strip label to get the correct forwarder (if any).
3595                  */
3596                 if (dns_rdatatype_atparent(fctx->type) &&
3597                     dns_name_countlabels(name) > 1) {
3598                         dns_name_init(&suffix, NULL);
3599                         labels = dns_name_countlabels(name);
3600                         dns_name_getlabelsequence(name, 1, labels - 1, &suffix);
3601                         fwdname = &suffix;
3602                 }
3603                 dns_fixedname_init(&fixed);
3604                 domain = dns_fixedname_name(&fixed);
3605                 result = dns_fwdtable_find2(fctx->res->view->fwdtable, fwdname,
3606                                             domain, &forwarders);
3607                 if (result == ISC_R_SUCCESS)
3608                         fctx->fwdpolicy = forwarders->fwdpolicy;
3609
3610                 if (fctx->fwdpolicy != dns_fwdpolicy_only) {
3611                         /*
3612                          * The caller didn't supply a query domain and
3613                          * nameservers, and we're not in forward-only mode,
3614                          * so find the best nameservers to use.
3615                          */
3616                         if (dns_rdatatype_atparent(fctx->type))
3617                                 findoptions |= DNS_DBFIND_NOEXACT;
3618                         result = dns_view_findzonecut(res->view, fwdname,
3619                                                       domain, 0, findoptions,
3620                                                       ISC_TRUE,
3621                                                       &fctx->nameservers,
3622                                                       NULL);
3623                         if (result != ISC_R_SUCCESS)
3624                                 goto cleanup_name;
3625
3626                         result = dns_name_dup(domain, mctx, &fctx->domain);
3627                         if (result != ISC_R_SUCCESS) {
3628                                 dns_rdataset_disassociate(&fctx->nameservers);
3629                                 goto cleanup_name;
3630                         }
3631                         fctx->ns_ttl = fctx->nameservers.ttl;
3632                         fctx->ns_ttl_ok = ISC_TRUE;
3633                 } else {
3634                         /*
3635                          * We're in forward-only mode.  Set the query domain.
3636                          */
3637                         result = dns_name_dup(domain, mctx, &fctx->domain);
3638                         if (result != ISC_R_SUCCESS)
3639                                 goto cleanup_name;
3640                 }
3641         } else {
3642                 result = dns_name_dup(domain, mctx, &fctx->domain);
3643                 if (result != ISC_R_SUCCESS)
3644                         goto cleanup_name;
3645                 dns_rdataset_clone(nameservers, &fctx->nameservers);
3646                 fctx->ns_ttl = fctx->nameservers.ttl;
3647                 fctx->ns_ttl_ok = ISC_TRUE;
3648         }
3649
3650         log_ns_ttl(fctx, "fctx_create");
3651
3652         INSIST(dns_name_issubdomain(&fctx->name, &fctx->domain));
3653
3654         fctx->qmessage = NULL;
3655         result = dns_message_create(mctx, DNS_MESSAGE_INTENTRENDER,
3656                                     &fctx->qmessage);
3657
3658         if (result != ISC_R_SUCCESS)
3659                 goto cleanup_domain;
3660
3661         fctx->rmessage = NULL;
3662         result = dns_message_create(mctx, DNS_MESSAGE_INTENTPARSE,
3663                                     &fctx->rmessage);
3664
3665         if (result != ISC_R_SUCCESS)
3666                 goto cleanup_qmessage;
3667
3668         /*
3669          * Compute an expiration time for the entire fetch.
3670          */
3671         isc_interval_set(&interval, res->query_timeout, 0);
3672         iresult = isc_time_nowplusinterval(&fctx->expires, &interval);
3673         if (iresult != ISC_R_SUCCESS) {
3674                 UNEXPECTED_ERROR(__FILE__, __LINE__,
3675                                  "isc_time_nowplusinterval: %s",
3676                                  isc_result_totext(iresult));
3677                 result = ISC_R_UNEXPECTED;
3678                 goto cleanup_rmessage;
3679         }
3680
3681         /*
3682          * Default retry interval initialization.  We set the interval now
3683          * mostly so it won't be uninitialized.  It will be set to the
3684          * correct value before a query is issued.
3685          */
3686         isc_interval_set(&fctx->interval, 2, 0);
3687
3688         /*
3689          * Create an inactive timer.  It will be made active when the fetch
3690          * is actually started.
3691          */
3692         fctx->timer = NULL;
3693         iresult = isc_timer_create(res->timermgr, isc_timertype_inactive,
3694                                    NULL, NULL,
3695                                    res->buckets[bucketnum].task, fctx_timeout,
3696                                    fctx, &fctx->timer);
3697         if (iresult != ISC_R_SUCCESS) {
3698                 UNEXPECTED_ERROR(__FILE__, __LINE__,
3699                                  "isc_timer_create: %s",
3700                                  isc_result_totext(iresult));
3701                 result = ISC_R_UNEXPECTED;
3702                 goto cleanup_rmessage;
3703         }
3704
3705         /*
3706          * Attach to the view's cache and adb.
3707          */
3708         fctx->cache = NULL;
3709         dns_db_attach(res->view->cachedb, &fctx->cache);
3710         fctx->adb = NULL;
3711         dns_adb_attach(res->view->adb, &fctx->adb);
3712         fctx->mctx = NULL;
3713         isc_mem_attach(mctx, &fctx->mctx);
3714
3715         ISC_LIST_INIT(fctx->events);
3716         ISC_LINK_INIT(fctx, link);
3717         fctx->magic = FCTX_MAGIC;
3718
3719         ISC_LIST_APPEND(res->buckets[bucketnum].fctxs, fctx, link);
3720
3721         LOCK(&res->nlock);
3722         res->nfctx++;
3723         UNLOCK(&res->nlock);
3724
3725         *fctxp = fctx;
3726
3727         return (ISC_R_SUCCESS);
3728
3729  cleanup_rmessage:
3730         dns_message_destroy(&fctx->rmessage);
3731
3732  cleanup_qmessage:
3733         dns_message_destroy(&fctx->qmessage);
3734
3735  cleanup_domain:
3736         if (dns_name_countlabels(&fctx->domain) > 0)
3737                 dns_name_free(&fctx->domain, mctx);
3738         if (dns_rdataset_isassociated(&fctx->nameservers))
3739                 dns_rdataset_disassociate(&fctx->nameservers);
3740
3741  cleanup_name:
3742         dns_name_free(&fctx->name, mctx);
3743
3744  cleanup_info:
3745         isc_mem_free(mctx, fctx->info);
3746
3747  cleanup_fetch:
3748         isc_mem_put(mctx, fctx, sizeof(*fctx));
3749
3750         return (result);
3751 }
3752
3753 /*
3754  * Handle Responses
3755  */
3756 static inline isc_boolean_t
3757 is_lame(fetchctx_t *fctx) {
3758         dns_message_t *message = fctx->rmessage;
3759         dns_name_t *name;
3760         dns_rdataset_t *rdataset;
3761         isc_result_t result;
3762
3763         if (message->rcode != dns_rcode_noerror &&
3764             message->rcode != dns_rcode_nxdomain)
3765                 return (ISC_FALSE);
3766
3767         if (message->counts[DNS_SECTION_ANSWER] != 0)
3768                 return (ISC_FALSE);
3769
3770         if (message->counts[DNS_SECTION_AUTHORITY] == 0)
3771                 return (ISC_FALSE);
3772
3773         result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
3774         while (result == ISC_R_SUCCESS) {
3775                 name = NULL;
3776                 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
3777                 for (rdataset = ISC_LIST_HEAD(name->list);
3778                      rdataset != NULL;
3779                      rdataset = ISC_LIST_NEXT(rdataset, link)) {
3780                         dns_namereln_t namereln;
3781                         int order;
3782                         unsigned int labels;
3783                         if (rdataset->type != dns_rdatatype_ns)
3784                                 continue;
3785                         namereln = dns_name_fullcompare(name, &fctx->domain,
3786                                                         &order, &labels);
3787                         if (namereln == dns_namereln_equal &&
3788                             (message->flags & DNS_MESSAGEFLAG_AA) != 0)
3789                                 return (ISC_FALSE);
3790                         if (namereln == dns_namereln_subdomain)
3791                                 return (ISC_FALSE);
3792                         return (ISC_TRUE);
3793                 }
3794                 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
3795         }
3796
3797         return (ISC_FALSE);
3798 }
3799
3800 static inline void
3801 log_lame(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo) {
3802         char namebuf[DNS_NAME_FORMATSIZE];
3803         char domainbuf[DNS_NAME_FORMATSIZE];
3804         char addrbuf[ISC_SOCKADDR_FORMATSIZE];
3805
3806         dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
3807         dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
3808         isc_sockaddr_format(&addrinfo->sockaddr, addrbuf, sizeof(addrbuf));
3809         isc_log_write(dns_lctx, DNS_LOGCATEGORY_LAME_SERVERS,
3810                       DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
3811                       "lame server resolving '%s' (in '%s'?): %s",
3812                       namebuf, domainbuf, addrbuf);
3813 }
3814
3815 static inline void
3816 log_formerr(fetchctx_t *fctx, const char *format, ...) {
3817         char nsbuf[ISC_SOCKADDR_FORMATSIZE];
3818         char clbuf[ISC_SOCKADDR_FORMATSIZE];
3819         const char *clmsg = "";
3820         char msgbuf[2048];
3821         va_list args;
3822
3823         va_start(args, format);
3824         vsnprintf(msgbuf, sizeof(msgbuf), format, args);
3825         va_end(args);
3826
3827         isc_sockaddr_format(&fctx->addrinfo->sockaddr, nsbuf, sizeof(nsbuf));
3828
3829         if (fctx->client != NULL) {
3830                 clmsg = " for client ";
3831                 isc_sockaddr_format(fctx->client, clbuf, sizeof(clbuf));
3832         } else {
3833                 clbuf[0] = '\0';
3834         }
3835
3836         isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
3837                       DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
3838                       "DNS format error from %s resolving %s%s%s: %s",
3839                       nsbuf, fctx->info, clmsg, clbuf, msgbuf);
3840 }
3841
3842 static inline isc_result_t
3843 same_question(fetchctx_t *fctx) {
3844         isc_result_t result;
3845         dns_message_t *message = fctx->rmessage;
3846         dns_name_t *name;
3847         dns_rdataset_t *rdataset;
3848
3849         /*
3850          * Caller must be holding the fctx lock.
3851          */
3852
3853         /*
3854          * XXXRTH  Currently we support only one question.
3855          */
3856         if (message->counts[DNS_SECTION_QUESTION] != 1) {
3857                 log_formerr(fctx, "too many questions");
3858                 return (DNS_R_FORMERR);
3859         }
3860
3861         result = dns_message_firstname(message, DNS_SECTION_QUESTION);
3862         if (result != ISC_R_SUCCESS)
3863                 return (result);
3864         name = NULL;
3865         dns_message_currentname(message, DNS_SECTION_QUESTION, &name);
3866         rdataset = ISC_LIST_HEAD(name->list);
3867         INSIST(rdataset != NULL);
3868         INSIST(ISC_LIST_NEXT(rdataset, link) == NULL);
3869
3870         if (fctx->type != rdataset->type ||
3871             fctx->res->rdclass != rdataset->rdclass ||
3872             !dns_name_equal(&fctx->name, name)) {
3873                 char namebuf[DNS_NAME_FORMATSIZE];
3874                 char class[DNS_RDATACLASS_FORMATSIZE];
3875                 char type[DNS_RDATATYPE_FORMATSIZE];
3876
3877                 dns_name_format(name, namebuf, sizeof(namebuf));
3878                 dns_rdataclass_format(rdataset->rdclass, class, sizeof(class));
3879                 dns_rdatatype_format(rdataset->type, type, sizeof(type));
3880                 log_formerr(fctx, "question section mismatch: got %s/%s/%s",
3881                             namebuf, class, type);
3882                 return (DNS_R_FORMERR);
3883         }
3884
3885         return (ISC_R_SUCCESS);
3886 }
3887
3888 static void
3889 clone_results(fetchctx_t *fctx) {
3890         dns_fetchevent_t *event, *hevent;
3891         isc_result_t result;
3892         dns_name_t *name, *hname;
3893
3894         FCTXTRACE("clone_results");
3895
3896         /*
3897          * Set up any other events to have the same data as the first
3898          * event.
3899          *
3900          * Caller must be holding the appropriate lock.
3901          */
3902
3903         fctx->cloned = ISC_TRUE;
3904         hevent = ISC_LIST_HEAD(fctx->events);
3905         if (hevent == NULL)
3906                 return;
3907         hname = dns_fixedname_name(&hevent->foundname);
3908         for (event = ISC_LIST_NEXT(hevent, ev_link);
3909              event != NULL;
3910              event = ISC_LIST_NEXT(event, ev_link)) {
3911                 name = dns_fixedname_name(&event->foundname);
3912                 result = dns_name_copy(hname, name, NULL);
3913                 if (result != ISC_R_SUCCESS)
3914                         event->result = result;
3915                 else
3916                         event->result = hevent->result;
3917                 dns_db_attach(hevent->db, &event->db);
3918                 dns_db_attachnode(hevent->db, hevent->node, &event->node);
3919                 INSIST(hevent->rdataset != NULL);
3920                 INSIST(event->rdataset != NULL);
3921                 if (dns_rdataset_isassociated(hevent->rdataset))
3922                         dns_rdataset_clone(hevent->rdataset, event->rdataset);
3923                 INSIST(! (hevent->sigrdataset == NULL &&
3924                           event->sigrdataset != NULL));
3925                 if (hevent->sigrdataset != NULL &&
3926                     dns_rdataset_isassociated(hevent->sigrdataset) &&
3927                     event->sigrdataset != NULL)
3928                         dns_rdataset_clone(hevent->sigrdataset,
3929                                            event->sigrdataset);
3930         }
3931 }
3932
3933 #define CACHE(r)        (((r)->attributes & DNS_RDATASETATTR_CACHE) != 0)
3934 #define ANSWER(r)       (((r)->attributes & DNS_RDATASETATTR_ANSWER) != 0)
3935 #define ANSWERSIG(r)    (((r)->attributes & DNS_RDATASETATTR_ANSWERSIG) != 0)
3936 #define EXTERNAL(r)     (((r)->attributes & DNS_RDATASETATTR_EXTERNAL) != 0)
3937 #define CHAINING(r)     (((r)->attributes & DNS_RDATASETATTR_CHAINING) != 0)
3938 #define CHASE(r)        (((r)->attributes & DNS_RDATASETATTR_CHASE) != 0)
3939 #define CHECKNAMES(r)   (((r)->attributes & DNS_RDATASETATTR_CHECKNAMES) != 0)
3940
3941
3942 /*
3943  * Destroy '*fctx' if it is ready to be destroyed (i.e., if it has
3944  * no references and is no longer waiting for any events).
3945  *
3946  * Requires:
3947  *      '*fctx' is shutting down.
3948  *
3949  * Returns:
3950  *      true if the resolver is exiting and this is the last fctx in the bucket.
3951  */
3952 static isc_boolean_t
3953 maybe_destroy(fetchctx_t *fctx, isc_boolean_t locked) {
3954         unsigned int bucketnum;
3955         isc_boolean_t bucket_empty = ISC_FALSE;
3956         dns_resolver_t *res = fctx->res;
3957         dns_validator_t *validator, *next_validator;
3958         isc_boolean_t destroy = ISC_FALSE;
3959
3960         REQUIRE(SHUTTINGDOWN(fctx));
3961
3962         bucketnum = fctx->bucketnum;
3963         if (!locked)
3964                 LOCK(&res->buckets[bucketnum].lock);
3965         if (fctx->pending != 0 || fctx->nqueries != 0)
3966                 goto unlock;
3967
3968         for (validator = ISC_LIST_HEAD(fctx->validators);
3969              validator != NULL; validator = next_validator) {
3970                 next_validator = ISC_LIST_NEXT(validator, link);
3971                 dns_validator_cancel(validator);
3972         }
3973
3974         if (fctx->references == 0 && ISC_LIST_EMPTY(fctx->validators)) {
3975                 bucket_empty = fctx_unlink(fctx);
3976                 destroy = ISC_TRUE;
3977         }
3978  unlock:
3979         if (!locked)
3980                 UNLOCK(&res->buckets[bucketnum].lock);
3981         if (destroy)
3982                 fctx_destroy(fctx);
3983         return (bucket_empty);
3984 }
3985
3986 /*
3987  * The validator has finished.
3988  */
3989 static void
3990 validated(isc_task_t *task, isc_event_t *event) {
3991         dns_adbaddrinfo_t *addrinfo;
3992         dns_dbnode_t *node = NULL;
3993         dns_dbnode_t *nsnode = NULL;
3994         dns_fetchevent_t *hevent;
3995         dns_name_t *name;
3996         dns_rdataset_t *ardataset = NULL;
3997         dns_rdataset_t *asigrdataset = NULL;
3998         dns_rdataset_t *rdataset;
3999         dns_rdataset_t *sigrdataset;
4000         dns_resolver_t *res;
4001         dns_valarg_t *valarg;
4002         dns_validatorevent_t *vevent;
4003         fetchctx_t *fctx;
4004         isc_boolean_t chaining;
4005         isc_boolean_t negative;
4006         isc_boolean_t sentresponse;
4007         isc_result_t eresult = ISC_R_SUCCESS;
4008         isc_result_t result = ISC_R_SUCCESS;
4009         isc_stdtime_t now;
4010         isc_uint32_t ttl;
4011
4012         UNUSED(task); /* for now */
4013
4014         REQUIRE(event->ev_type == DNS_EVENT_VALIDATORDONE);
4015         valarg = event->ev_arg;
4016         fctx = valarg->fctx;
4017         res = fctx->res;
4018         addrinfo = valarg->addrinfo;
4019         REQUIRE(VALID_FCTX(fctx));
4020         REQUIRE(!ISC_LIST_EMPTY(fctx->validators));
4021
4022         vevent = (dns_validatorevent_t *)event;
4023         fctx->vresult = vevent->result;
4024
4025         FCTXTRACE("received validation completion event");
4026
4027         LOCK(&res->buckets[fctx->bucketnum].lock);
4028
4029         ISC_LIST_UNLINK(fctx->validators, vevent->validator, link);
4030         fctx->validator = NULL;
4031
4032         /*
4033          * Destroy the validator early so that we can
4034          * destroy the fctx if necessary.
4035          */
4036         dns_validator_destroy(&vevent->validator);
4037         isc_mem_put(fctx->mctx, valarg, sizeof(*valarg));
4038
4039         negative = ISC_TF(vevent->rdataset == NULL);
4040
4041         sentresponse = ISC_TF((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0);
4042
4043         /*
4044          * If shutting down, ignore the results.  Check to see if we're
4045          * done waiting for validator completions and ADB pending events; if
4046          * so, destroy the fctx.
4047          */
4048         if (SHUTTINGDOWN(fctx) && !sentresponse) {
4049                 isc_uint32_t bucketnum = fctx->bucketnum;
4050                 isc_boolean_t bucket_empty;
4051                 bucket_empty = maybe_destroy(fctx, ISC_TRUE);
4052                 UNLOCK(&res->buckets[bucketnum].lock);
4053                 if (bucket_empty)
4054                         empty_bucket(res);
4055                 goto cleanup_event;
4056         }
4057
4058         isc_stdtime_get(&now);
4059
4060         /*
4061          * If chaining, we need to make sure that the right result code is
4062          * returned, and that the rdatasets are bound.
4063          */
4064         if (vevent->result == ISC_R_SUCCESS &&
4065             !negative &&
4066             vevent->rdataset != NULL &&
4067             CHAINING(vevent->rdataset))
4068         {
4069                 if (vevent->rdataset->type == dns_rdatatype_cname)
4070                         eresult = DNS_R_CNAME;
4071                 else {
4072                         INSIST(vevent->rdataset->type == dns_rdatatype_dname);
4073                         eresult = DNS_R_DNAME;
4074                 }
4075                 chaining = ISC_TRUE;
4076         } else
4077                 chaining = ISC_FALSE;
4078
4079         /*
4080          * Either we're not shutting down, or we are shutting down but want
4081          * to cache the result anyway (if this was a validation started by
4082          * a query with cd set)
4083          */
4084
4085         hevent = ISC_LIST_HEAD(fctx->events);
4086         if (hevent != NULL) {
4087                 if (!negative && !chaining &&
4088                     (fctx->type == dns_rdatatype_any ||
4089                      fctx->type == dns_rdatatype_rrsig ||
4090                      fctx->type == dns_rdatatype_sig)) {
4091                         /*
4092                          * Don't bind rdatasets; the caller
4093                          * will iterate the node.
4094                          */
4095                 } else {
4096                         ardataset = hevent->rdataset;
4097                         asigrdataset = hevent->sigrdataset;
4098                 }
4099         }
4100
4101         if (vevent->result != ISC_R_SUCCESS) {
4102                 FCTXTRACE("validation failed");
4103                 inc_stats(res, dns_resstatscounter_valfail);
4104                 fctx->valfail++;
4105                 fctx->vresult = vevent->result;
4106                 if (fctx->vresult != DNS_R_BROKENCHAIN) {
4107                         result = ISC_R_NOTFOUND;
4108                         if (vevent->rdataset != NULL)
4109                                 result = dns_db_findnode(fctx->cache,
4110                                                          vevent->name,
4111                                                          ISC_TRUE, &node);
4112                         if (result == ISC_R_SUCCESS)
4113                                 (void)dns_db_deleterdataset(fctx->cache, node,
4114                                                              NULL,
4115                                                             vevent->type, 0);
4116                         if (result == ISC_R_SUCCESS &&
4117                              vevent->sigrdataset != NULL)
4118                                 (void)dns_db_deleterdataset(fctx->cache, node,
4119                                                             NULL,
4120                                                             dns_rdatatype_rrsig,
4121                                                             vevent->type);
4122                         if (result == ISC_R_SUCCESS)
4123                                 dns_db_detachnode(fctx->cache, &node);
4124                 }
4125                 if (fctx->vresult == DNS_R_BROKENCHAIN && !negative) {
4126                         /*
4127                          * Cache the data as pending for later validation.
4128                          */
4129                         result = ISC_R_NOTFOUND;
4130                         if (vevent->rdataset != NULL)
4131                                 result = dns_db_findnode(fctx->cache,
4132                                                          vevent->name,
4133                                                          ISC_TRUE, &node);
4134                         if (result == ISC_R_SUCCESS) {
4135                                 (void)dns_db_addrdataset(fctx->cache, node,
4136                                                          NULL, now,
4137                                                          vevent->rdataset, 0,
4138                                                          NULL);
4139                         }
4140                         if (result == ISC_R_SUCCESS &&
4141                             vevent->sigrdataset != NULL)
4142                                 (void)dns_db_addrdataset(fctx->cache, node,
4143                                                          NULL, now,
4144                                                          vevent->sigrdataset,
4145                                                          0, NULL);
4146                         if (result == ISC_R_SUCCESS)
4147                                 dns_db_detachnode(fctx->cache, &node);
4148                 }
4149                 result = fctx->vresult;
4150                 add_bad(fctx, addrinfo, result, badns_validation);
4151                 isc_event_free(&event);
4152                 UNLOCK(&res->buckets[fctx->bucketnum].lock);
4153                 INSIST(fctx->validator == NULL);
4154                 fctx->validator = ISC_LIST_HEAD(fctx->validators);
4155                 if (fctx->validator != NULL)
4156                         dns_validator_send(fctx->validator);
4157                 else if (sentresponse)
4158                         fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4159                 else if (result == DNS_R_BROKENCHAIN) {
4160                         isc_result_t tresult;
4161                         isc_time_t expire;
4162                         isc_interval_t i;
4163
4164                         isc_interval_set(&i, DNS_BADCACHE_TTL(fctx), 0);
4165                         tresult = isc_time_nowplusinterval(&expire, &i);
4166                         if (negative &&
4167                             (fctx->type == dns_rdatatype_dnskey ||
4168                              fctx->type == dns_rdatatype_dlv ||
4169                              fctx->type == dns_rdatatype_ds) &&
4170                              tresult == ISC_R_SUCCESS)
4171                                 dns_resolver_addbadcache(res, &fctx->name,
4172                                                          fctx->type, &expire);
4173                         fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4174                 } else
4175                         fctx_try(fctx, ISC_TRUE, ISC_TRUE); /* Locks bucket. */
4176                 return;
4177         }
4178
4179
4180         if (negative) {
4181                 dns_rdatatype_t covers;
4182                 FCTXTRACE("nonexistence validation OK");
4183
4184                 inc_stats(res, dns_resstatscounter_valnegsuccess);
4185
4186                 if (fctx->rmessage->rcode == dns_rcode_nxdomain)
4187                         covers = dns_rdatatype_any;
4188                 else
4189                         covers = fctx->type;
4190
4191                 result = dns_db_findnode(fctx->cache, vevent->name, ISC_TRUE,
4192                                          &node);
4193                 if (result != ISC_R_SUCCESS)
4194                         goto noanswer_response;
4195
4196                 /*
4197                  * If we are asking for a SOA record set the cache time
4198                  * to zero to facilitate locating the containing zone of
4199                  * a arbitrary zone.
4200                  */
4201                 ttl = res->view->maxncachettl;
4202                 if (fctx->type == dns_rdatatype_soa &&
4203                     covers == dns_rdatatype_any && res->zero_no_soa_ttl)
4204                         ttl = 0;
4205
4206                 result = ncache_adderesult(fctx->rmessage, fctx->cache, node,
4207                                            covers, now, ttl, vevent->optout,
4208                                            vevent->secure, ardataset, &eresult);
4209                 if (result != ISC_R_SUCCESS)
4210                         goto noanswer_response;
4211                 goto answer_response;
4212         } else
4213                 inc_stats(res, dns_resstatscounter_valsuccess);
4214
4215         FCTXTRACE("validation OK");
4216
4217         if (vevent->proofs[DNS_VALIDATOR_NOQNAMEPROOF] != NULL) {
4218                 result = dns_rdataset_addnoqname(vevent->rdataset,
4219                                    vevent->proofs[DNS_VALIDATOR_NOQNAMEPROOF]);
4220                 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4221                 INSIST(vevent->sigrdataset != NULL);
4222                 vevent->sigrdataset->ttl = vevent->rdataset->ttl;
4223                 if (vevent->proofs[DNS_VALIDATOR_CLOSESTENCLOSER] != NULL) {
4224                         result = dns_rdataset_addclosest(vevent->rdataset,
4225                                  vevent->proofs[DNS_VALIDATOR_CLOSESTENCLOSER]);
4226                         RUNTIME_CHECK(result == ISC_R_SUCCESS);
4227                 }
4228         } else if (vevent->rdataset->trust == dns_trust_answer &&
4229                    vevent->rdataset->type != dns_rdatatype_rrsig)
4230         {
4231                 isc_result_t tresult;
4232                 dns_name_t *noqname = NULL;
4233                 tresult = findnoqname(fctx, vevent->name,
4234                                       vevent->rdataset->type, &noqname);
4235                 if (tresult == ISC_R_SUCCESS && noqname != NULL) {
4236                         tresult = dns_rdataset_addnoqname(vevent->rdataset,
4237                                                           noqname);
4238                         RUNTIME_CHECK(tresult == ISC_R_SUCCESS);
4239                 }
4240         }
4241
4242         /*
4243          * The data was already cached as pending data.
4244          * Re-cache it as secure and bind the cached
4245          * rdatasets to the first event on the fetch
4246          * event list.
4247          */
4248         result = dns_db_findnode(fctx->cache, vevent->name, ISC_TRUE, &node);
4249         if (result != ISC_R_SUCCESS)
4250                 goto noanswer_response;
4251
4252         result = dns_db_addrdataset(fctx->cache, node, NULL, now,
4253                                     vevent->rdataset, 0, ardataset);
4254         if (result != ISC_R_SUCCESS &&
4255             result != DNS_R_UNCHANGED)
4256                 goto noanswer_response;
4257         if (ardataset != NULL && NEGATIVE(ardataset)) {
4258                 if (NXDOMAIN(ardataset))
4259                         eresult = DNS_R_NCACHENXDOMAIN;
4260                 else
4261                         eresult = DNS_R_NCACHENXRRSET;
4262         } else if (vevent->sigrdataset != NULL) {
4263                 result = dns_db_addrdataset(fctx->cache, node, NULL, now,
4264                                             vevent->sigrdataset, 0,
4265                                             asigrdataset);
4266                 if (result != ISC_R_SUCCESS &&
4267                     result != DNS_R_UNCHANGED)
4268                         goto noanswer_response;
4269         }
4270
4271         if (sentresponse) {
4272                 isc_boolean_t bucket_empty = ISC_FALSE;
4273                 /*
4274                  * If we only deferred the destroy because we wanted to cache
4275                  * the data, destroy now.
4276                  */
4277                 dns_db_detachnode(fctx->cache, &node);
4278                 if (SHUTTINGDOWN(fctx))
4279                         bucket_empty = maybe_destroy(fctx, ISC_TRUE);
4280                 UNLOCK(&res->buckets[fctx->bucketnum].lock);
4281                 if (bucket_empty)
4282                         empty_bucket(res);
4283                 goto cleanup_event;
4284         }
4285
4286         if (!ISC_LIST_EMPTY(fctx->validators)) {
4287                 INSIST(!negative);
4288                 INSIST(fctx->type == dns_rdatatype_any ||
4289                        fctx->type == dns_rdatatype_rrsig ||
4290                        fctx->type == dns_rdatatype_sig);
4291                 /*
4292                  * Don't send a response yet - we have
4293                  * more rdatasets that still need to
4294                  * be validated.
4295                  */
4296                 dns_db_detachnode(fctx->cache, &node);
4297                 UNLOCK(&res->buckets[fctx->bucketnum].lock);
4298                 dns_validator_send(ISC_LIST_HEAD(fctx->validators));
4299                 goto cleanup_event;
4300         }
4301
4302  answer_response:
4303         /*
4304          * Cache any NS/NSEC records that happened to be validated.
4305          */
4306         result = dns_message_firstname(fctx->rmessage, DNS_SECTION_AUTHORITY);
4307         while (result == ISC_R_SUCCESS) {
4308                 name = NULL;
4309                 dns_message_currentname(fctx->rmessage, DNS_SECTION_AUTHORITY,
4310                                         &name);
4311                 for (rdataset = ISC_LIST_HEAD(name->list);
4312                      rdataset != NULL;
4313                      rdataset = ISC_LIST_NEXT(rdataset, link)) {
4314                         if ((rdataset->type != dns_rdatatype_ns &&
4315                              rdataset->type != dns_rdatatype_nsec) ||
4316                             rdataset->trust != dns_trust_secure)
4317                                 continue;
4318                         for (sigrdataset = ISC_LIST_HEAD(name->list);
4319                              sigrdataset != NULL;
4320                              sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4321                                 if (sigrdataset->type != dns_rdatatype_rrsig ||
4322                                     sigrdataset->covers != rdataset->type)
4323                                         continue;
4324                                 break;
4325                         }
4326                         if (sigrdataset == NULL ||
4327                             sigrdataset->trust != dns_trust_secure)
4328                                 continue;
4329                         result = dns_db_findnode(fctx->cache, name, ISC_TRUE,
4330                                                  &nsnode);
4331                         if (result != ISC_R_SUCCESS)
4332                                 continue;
4333
4334                         result = dns_db_addrdataset(fctx->cache, nsnode, NULL,
4335                                                     now, rdataset, 0, NULL);
4336                         if (result == ISC_R_SUCCESS)
4337                                 result = dns_db_addrdataset(fctx->cache, nsnode,
4338                                                             NULL, now,
4339                                                             sigrdataset, 0,
4340                                                             NULL);
4341                         dns_db_detachnode(fctx->cache, &nsnode);
4342                         if (result != ISC_R_SUCCESS)
4343                                 continue;
4344                 }
4345                 result = dns_message_nextname(fctx->rmessage,
4346                                               DNS_SECTION_AUTHORITY);
4347         }
4348
4349         result = ISC_R_SUCCESS;
4350
4351         /*
4352          * Respond with an answer, positive or negative,
4353          * as opposed to an error.  'node' must be non-NULL.
4354          */
4355
4356         fctx->attributes |= FCTX_ATTR_HAVEANSWER;
4357
4358         if (hevent != NULL) {
4359                 /*
4360                  * Negative results must be indicated in event->result.
4361                  */
4362                 if (dns_rdataset_isassociated(hevent->rdataset) &&
4363                     NEGATIVE(hevent->rdataset)) {
4364                         INSIST(eresult == DNS_R_NCACHENXDOMAIN ||
4365                                eresult == DNS_R_NCACHENXRRSET);
4366                 }
4367                 hevent->result = eresult;
4368                 RUNTIME_CHECK(dns_name_copy(vevent->name,
4369                               dns_fixedname_name(&hevent->foundname), NULL)
4370                               == ISC_R_SUCCESS);
4371                 dns_db_attach(fctx->cache, &hevent->db);
4372                 dns_db_transfernode(fctx->cache, &node, &hevent->node);
4373                 clone_results(fctx);
4374         }
4375
4376  noanswer_response:
4377         if (node != NULL)
4378                 dns_db_detachnode(fctx->cache, &node);
4379
4380         UNLOCK(&res->buckets[fctx->bucketnum].lock);
4381         fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4382
4383  cleanup_event:
4384         INSIST(node == NULL);
4385         isc_event_free(&event);
4386 }
4387
4388 static void
4389 fctx_log(void *arg, int level, const char *fmt, ...) {
4390         char msgbuf[2048];
4391         va_list args;
4392         fetchctx_t *fctx = arg;
4393
4394         va_start(args, fmt);
4395         vsnprintf(msgbuf, sizeof(msgbuf), fmt, args);
4396         va_end(args);
4397
4398         isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
4399                       DNS_LOGMODULE_RESOLVER, level,
4400                       "fctx %p(%s): %s", fctx, fctx->info, msgbuf);
4401 }
4402
4403 static inline isc_result_t
4404 findnoqname(fetchctx_t *fctx, dns_name_t *name, dns_rdatatype_t type,
4405             dns_name_t **noqnamep)
4406 {
4407         dns_rdataset_t *nrdataset, *next, *sigrdataset;
4408         dns_rdata_rrsig_t rrsig;
4409         isc_result_t result;
4410         unsigned int labels;
4411         dns_section_t section;
4412         dns_name_t *zonename;
4413         dns_fixedname_t fzonename;
4414         dns_name_t *closest;
4415         dns_fixedname_t fclosest;
4416         dns_name_t *nearest;
4417         dns_fixedname_t fnearest;
4418         dns_rdatatype_t found = dns_rdatatype_none;
4419         dns_name_t *noqname = NULL;
4420
4421         FCTXTRACE("findnoqname");
4422
4423         REQUIRE(noqnamep != NULL && *noqnamep == NULL);
4424
4425         /*
4426          * Find the SIG for this rdataset, if we have it.
4427          */
4428         for (sigrdataset = ISC_LIST_HEAD(name->list);
4429              sigrdataset != NULL;
4430              sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4431                 if (sigrdataset->type == dns_rdatatype_rrsig &&
4432                     sigrdataset->covers == type)
4433                         break;
4434         }
4435
4436         if (sigrdataset == NULL)
4437                 return (ISC_R_NOTFOUND);
4438
4439         labels = dns_name_countlabels(name);
4440
4441         for (result = dns_rdataset_first(sigrdataset);
4442              result == ISC_R_SUCCESS;
4443              result = dns_rdataset_next(sigrdataset)) {
4444                 dns_rdata_t rdata = DNS_RDATA_INIT;
4445                 dns_rdataset_current(sigrdataset, &rdata);
4446                 result = dns_rdata_tostruct(&rdata, &rrsig, NULL);
4447                 RUNTIME_CHECK(result == ISC_R_SUCCESS);
4448                 /* Wildcard has rrsig.labels < labels - 1. */
4449                 if (rrsig.labels + 1U >= labels)
4450                         continue;
4451                 break;
4452         }
4453
4454         if (result == ISC_R_NOMORE)
4455                 return (ISC_R_NOTFOUND);
4456         if (result != ISC_R_SUCCESS)
4457                 return (result);
4458
4459         dns_fixedname_init(&fzonename);
4460         zonename = dns_fixedname_name(&fzonename);
4461         dns_fixedname_init(&fclosest);
4462         closest = dns_fixedname_name(&fclosest);
4463         dns_fixedname_init(&fnearest);
4464         nearest = dns_fixedname_name(&fnearest);
4465
4466 #define NXND(x) ((x) == ISC_R_SUCCESS)
4467
4468         section = DNS_SECTION_AUTHORITY;
4469         for (result = dns_message_firstname(fctx->rmessage, section);
4470              result == ISC_R_SUCCESS;
4471              result = dns_message_nextname(fctx->rmessage, section)) {
4472                 dns_name_t *nsec = NULL;
4473                 dns_message_currentname(fctx->rmessage, section, &nsec);
4474                 for (nrdataset = ISC_LIST_HEAD(nsec->list);
4475                       nrdataset != NULL; nrdataset = next) {
4476                         isc_boolean_t data = ISC_FALSE, exists = ISC_FALSE;
4477                         isc_boolean_t optout = ISC_FALSE, unknown = ISC_FALSE;
4478                         isc_boolean_t setclosest = ISC_FALSE;
4479                         isc_boolean_t setnearest = ISC_FALSE;
4480
4481                         next = ISC_LIST_NEXT(nrdataset, link);
4482                         if (nrdataset->type != dns_rdatatype_nsec &&
4483                             nrdataset->type != dns_rdatatype_nsec3)
4484                                 continue;
4485
4486                         if (nrdataset->type == dns_rdatatype_nsec &&
4487                             NXND(dns_nsec_noexistnodata(type, name, nsec,
4488                                                         nrdataset, &exists,
4489                                                         &data, NULL, fctx_log,
4490                                                         fctx)))
4491                         {
4492                                 if (!exists) {
4493                                         noqname = nsec;
4494                                         found = dns_rdatatype_nsec;
4495                                 }
4496                         }
4497
4498                         if (nrdataset->type == dns_rdatatype_nsec3 &&
4499                             NXND(dns_nsec3_noexistnodata(type, name, nsec,
4500                                                          nrdataset, zonename,
4501                                                          &exists, &data,
4502                                                          &optout, &unknown,
4503                                                          &setclosest,
4504                                                          &setnearest,
4505                                                          closest, nearest,
4506                                                          fctx_log, fctx)))
4507                         {
4508                                 if (!exists && setnearest) {
4509                                         noqname = nsec;
4510                                         found = dns_rdatatype_nsec3;
4511                                 }
4512                         }
4513                 }
4514         }
4515         if (result == ISC_R_NOMORE)
4516                 result = ISC_R_SUCCESS;
4517         if (noqname != NULL) {
4518                 for (sigrdataset = ISC_LIST_HEAD(noqname->list);
4519                      sigrdataset != NULL;
4520                      sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4521                         if (sigrdataset->type == dns_rdatatype_rrsig &&
4522                             sigrdataset->covers == found)
4523                                 break;
4524                 }
4525                 if (sigrdataset != NULL)
4526                         *noqnamep = noqname;
4527         }
4528         return (result);
4529 }
4530
4531 static inline isc_result_t
4532 cache_name(fetchctx_t *fctx, dns_name_t *name, dns_adbaddrinfo_t *addrinfo,
4533            isc_stdtime_t now)
4534 {
4535         dns_rdataset_t *rdataset, *sigrdataset;
4536         dns_rdataset_t *addedrdataset, *ardataset, *asigrdataset;
4537         dns_rdataset_t *valrdataset = NULL, *valsigrdataset = NULL;
4538         dns_dbnode_t *node, **anodep;
4539         dns_db_t **adbp;
4540         dns_name_t *aname;
4541         dns_resolver_t *res;
4542         isc_boolean_t need_validation, secure_domain, have_answer;
4543         isc_result_t result, eresult;
4544         dns_fetchevent_t *event;
4545         unsigned int options;
4546         isc_task_t *task;
4547         isc_boolean_t fail;
4548         unsigned int valoptions = 0;
4549
4550         /*
4551          * The appropriate bucket lock must be held.
4552          */
4553
4554         res = fctx->res;
4555         need_validation = ISC_FALSE;
4556         POST(need_validation);
4557         secure_domain = ISC_FALSE;
4558         have_answer = ISC_FALSE;
4559         eresult = ISC_R_SUCCESS;
4560         task = res->buckets[fctx->bucketnum].task;
4561
4562         /*
4563          * Is DNSSEC validation required for this name?
4564          */
4565         if (res->view->enablevalidation) {
4566                 result = dns_view_issecuredomain(res->view, name,
4567                                                  &secure_domain);
4568                 if (result != ISC_R_SUCCESS)
4569                         return (result);
4570
4571                 if (!secure_domain && res->view->dlv != NULL) {
4572                         valoptions = DNS_VALIDATOR_DLV;
4573                         secure_domain = ISC_TRUE;
4574                 }
4575         }
4576
4577         if ((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0)
4578                 need_validation = ISC_FALSE;
4579         else
4580                 need_validation = secure_domain;
4581
4582         adbp = NULL;
4583         aname = NULL;
4584         anodep = NULL;
4585         ardataset = NULL;
4586         asigrdataset = NULL;
4587         event = NULL;
4588         if ((name->attributes & DNS_NAMEATTR_ANSWER) != 0 &&
4589             !need_validation) {
4590                 have_answer = ISC_TRUE;
4591                 event = ISC_LIST_HEAD(fctx->events);
4592                 if (event != NULL) {
4593                         adbp = &event->db;
4594                         aname = dns_fixedname_name(&event->foundname);
4595                         result = dns_name_copy(name, aname, NULL);
4596                         if (result != ISC_R_SUCCESS)
4597                                 return (result);
4598                         anodep = &event->node;
4599                         /*
4600                          * If this is an ANY, SIG or RRSIG query, we're not
4601                          * going to return any rdatasets, unless we encountered
4602                          * a CNAME or DNAME as "the answer".  In this case,
4603                          * we're going to return DNS_R_CNAME or DNS_R_DNAME
4604                          * and we must set up the rdatasets.
4605                          */
4606                         if ((fctx->type != dns_rdatatype_any &&
4607                              fctx->type != dns_rdatatype_rrsig &&
4608                              fctx->type != dns_rdatatype_sig) ||
4609                             (name->attributes & DNS_NAMEATTR_CHAINING) != 0) {
4610                                 ardataset = event->rdataset;
4611                                 asigrdataset = event->sigrdataset;
4612                         }
4613                 }
4614         }
4615
4616         /*
4617          * Find or create the cache node.
4618          */
4619         node = NULL;
4620         result = dns_db_findnode(fctx->cache, name, ISC_TRUE, &node);
4621         if (result != ISC_R_SUCCESS)
4622                 return (result);
4623
4624         /*
4625          * Cache or validate each cacheable rdataset.
4626          */
4627         fail = ISC_TF((fctx->res->options & DNS_RESOLVER_CHECKNAMESFAIL) != 0);
4628         for (rdataset = ISC_LIST_HEAD(name->list);
4629              rdataset != NULL;
4630              rdataset = ISC_LIST_NEXT(rdataset, link)) {
4631                 if (!CACHE(rdataset))
4632                         continue;
4633                 if (CHECKNAMES(rdataset)) {
4634                         char namebuf[DNS_NAME_FORMATSIZE];
4635                         char typebuf[DNS_RDATATYPE_FORMATSIZE];
4636                         char classbuf[DNS_RDATATYPE_FORMATSIZE];
4637
4638                         dns_name_format(name, namebuf, sizeof(namebuf));
4639                         dns_rdatatype_format(rdataset->type, typebuf,
4640                                              sizeof(typebuf));
4641                         dns_rdataclass_format(rdataset->rdclass, classbuf,
4642                                               sizeof(classbuf));
4643                         isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
4644                                       DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
4645                                       "check-names %s %s/%s/%s",
4646                                       fail ? "failure" : "warning",
4647                                       namebuf, typebuf, classbuf);
4648                         if (fail) {
4649                                 if (ANSWER(rdataset)) {
4650                                         dns_db_detachnode(fctx->cache, &node);
4651                                         return (DNS_R_BADNAME);
4652                                 }
4653                                 continue;
4654                         }
4655                 }
4656
4657                 /*
4658                  * Enforce the configure maximum cache TTL.
4659                  */
4660                 if (rdataset->ttl > res->view->maxcachettl)
4661                         rdataset->ttl = res->view->maxcachettl;
4662
4663                 /*
4664                  * Find the SIG for this rdataset, if we have it.
4665                  */
4666                 for (sigrdataset = ISC_LIST_HEAD(name->list);
4667                      sigrdataset != NULL;
4668                      sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4669                         if (sigrdataset->type == dns_rdatatype_rrsig &&
4670                             sigrdataset->covers == rdataset->type)
4671                                 break;
4672                 }
4673
4674                 /*
4675                  * If this RRset is in a secure domain, is in bailiwick,
4676                  * and is not glue, attempt DNSSEC validation.  (We do not
4677                  * attempt to validate glue or out-of-bailiwick data--even
4678                  * though there might be some performance benefit to doing
4679                  * so--because it makes it simpler and safer to ensure that
4680                  * records from a secure domain are only cached if validated
4681                  * within the context of a query to the domain that owns
4682                  * them.)
4683                  */
4684                 if (secure_domain && rdataset->trust != dns_trust_glue &&
4685                     !EXTERNAL(rdataset)) {
4686                         dns_trust_t trust;
4687
4688                         /*
4689                          * RRSIGs are validated as part of validating the
4690                          * type they cover.
4691                          */
4692                         if (rdataset->type == dns_rdatatype_rrsig)
4693                                 continue;
4694
4695                         if (sigrdataset == NULL) {
4696                                 if (!ANSWER(rdataset) && need_validation) {
4697                                         /*
4698                                          * Ignore non-answer rdatasets that
4699                                          * are missing signatures.
4700                                          */
4701                                         continue;
4702                                 }
4703                         }
4704
4705                         /*
4706                          * Normalize the rdataset and sigrdataset TTLs.
4707                          */
4708                         if (sigrdataset != NULL) {
4709                                 rdataset->ttl = ISC_MIN(rdataset->ttl,
4710                                                         sigrdataset->ttl);
4711                                 sigrdataset->ttl = rdataset->ttl;
4712                         }
4713
4714                         /*
4715                          * Cache this rdataset/sigrdataset pair as
4716                          * pending data.  Track whether it was additional
4717                          * or not.
4718                          */
4719                         if (rdataset->trust == dns_trust_additional)
4720                                 trust = dns_trust_pending_additional;
4721                         else
4722                                 trust = dns_trust_pending_answer;
4723
4724                         rdataset->trust = trust;
4725                         if (sigrdataset != NULL)
4726                                 sigrdataset->trust = trust;
4727                         if (!need_validation || !ANSWER(rdataset)) {
4728                                 if (ANSWER(rdataset) &&
4729                                    rdataset->type != dns_rdatatype_rrsig) {
4730                                         isc_result_t tresult;
4731                                         dns_name_t *noqname = NULL;
4732                                         tresult = findnoqname(fctx, name,
4733                                                               rdataset->type,
4734                                                               &noqname);
4735                                         if (tresult == ISC_R_SUCCESS &&
4736                                             noqname != NULL) {
4737                                                 tresult =
4738                                                      dns_rdataset_addnoqname(
4739                                                             rdataset, noqname);
4740                                                 RUNTIME_CHECK(tresult ==
4741                                                               ISC_R_SUCCESS);
4742                                         }
4743                                 }
4744                                 addedrdataset = ardataset;
4745                                 result = dns_db_addrdataset(fctx->cache, node,
4746                                                             NULL, now, rdataset,
4747                                                             0, addedrdataset);
4748                                 if (result == DNS_R_UNCHANGED) {
4749                                         result = ISC_R_SUCCESS;
4750                                         if (!need_validation &&
4751                                             ardataset != NULL &&
4752                                             NEGATIVE(ardataset)) {
4753                                                 /*
4754                                                  * The answer in the cache is
4755                                                  * better than the answer we
4756                                                  * found, and is a negative
4757                                                  * cache entry, so we must set
4758                                                  * eresult appropriately.
4759                                                  */
4760                                                 if (NXDOMAIN(ardataset))
4761                                                         eresult =
4762                                                            DNS_R_NCACHENXDOMAIN;
4763                                                 else
4764                                                         eresult =
4765                                                            DNS_R_NCACHENXRRSET;
4766                                                 /*
4767                                                  * We have a negative response
4768                                                  * from the cache so don't
4769                                                  * attempt to add the RRSIG
4770                                                  * rrset.
4771                                                  */
4772                                                 continue;
4773                                         }
4774                                 }
4775                                 if (result != ISC_R_SUCCESS)
4776                                         break;
4777                                 if (sigrdataset != NULL) {
4778                                         addedrdataset = asigrdataset;
4779                                         result = dns_db_addrdataset(fctx->cache,
4780                                                                 node, NULL, now,
4781                                                                 sigrdataset, 0,
4782                                                                 addedrdataset);
4783                                         if (result == DNS_R_UNCHANGED)
4784                                                 result = ISC_R_SUCCESS;
4785                                         if (result != ISC_R_SUCCESS)
4786                                                 break;
4787                                 } else if (!ANSWER(rdataset))
4788                                         continue;
4789                         }
4790
4791                         if (ANSWER(rdataset) && need_validation) {
4792                                 if (fctx->type != dns_rdatatype_any &&
4793                                     fctx->type != dns_rdatatype_rrsig &&
4794                                     fctx->type != dns_rdatatype_sig) {
4795                                         /*
4796                                          * This is The Answer.  We will
4797                                          * validate it, but first we cache
4798                                          * the rest of the response - it may
4799                                          * contain useful keys.
4800                                          */
4801                                         INSIST(valrdataset == NULL &&
4802                                                valsigrdataset == NULL);
4803                                         valrdataset = rdataset;
4804                                         valsigrdataset = sigrdataset;
4805                                 } else {
4806                                         /*
4807                                          * This is one of (potentially)
4808                                          * multiple answers to an ANY
4809                                          * or SIG query.  To keep things
4810                                          * simple, we just start the
4811                                          * validator right away rather
4812                                          * than caching first and
4813                                          * having to remember which
4814                                          * rdatasets needed validation.
4815                                          */
4816                                         result = valcreate(fctx, addrinfo,
4817                                                            name, rdataset->type,
4818                                                            rdataset,
4819                                                            sigrdataset,
4820                                                            valoptions, task);
4821                                         /*
4822                                          * Defer any further validations.
4823                                          * This prevents multiple validators
4824                                          * from manipulating fctx->rmessage
4825                                          * simultaneously.
4826                                          */
4827                                         valoptions |= DNS_VALIDATOR_DEFER;
4828                                 }
4829                         } else if (CHAINING(rdataset)) {
4830                                 if (rdataset->type == dns_rdatatype_cname)
4831                                         eresult = DNS_R_CNAME;
4832                                 else {
4833                                         INSIST(rdataset->type ==
4834                                                dns_rdatatype_dname);
4835                                         eresult = DNS_R_DNAME;
4836                                 }
4837                         }
4838                 } else if (!EXTERNAL(rdataset)) {
4839                         /*
4840                          * It's OK to cache this rdataset now.
4841                          */
4842                         if (ANSWER(rdataset))
4843                                 addedrdataset = ardataset;
4844                         else if (ANSWERSIG(rdataset))
4845                                 addedrdataset = asigrdataset;
4846                         else
4847                                 addedrdataset = NULL;
4848                         if (CHAINING(rdataset)) {
4849                                 if (rdataset->type == dns_rdatatype_cname)
4850                                         eresult = DNS_R_CNAME;
4851                                 else {
4852                                         INSIST(rdataset->type ==
4853                                                dns_rdatatype_dname);
4854                                         eresult = DNS_R_DNAME;
4855                                 }
4856                         }
4857                         if (rdataset->trust == dns_trust_glue &&
4858                             (rdataset->type == dns_rdatatype_ns ||
4859                              (rdataset->type == dns_rdatatype_rrsig &&
4860                               rdataset->covers == dns_rdatatype_ns))) {
4861                                 /*
4862                                  * If the trust level is 'dns_trust_glue'
4863                                  * then we are adding data from a referral
4864                                  * we got while executing the search algorithm.
4865                                  * New referral data always takes precedence
4866                                  * over the existing cache contents.
4867                                  */
4868                                 options = DNS_DBADD_FORCE;
4869                         } else
4870                                 options = 0;
4871
4872                         if (ANSWER(rdataset) &&
4873                            rdataset->type != dns_rdatatype_rrsig) {
4874                                 isc_result_t tresult;
4875                                 dns_name_t *noqname = NULL;
4876                                 tresult = findnoqname(fctx, name,
4877                                                       rdataset->type, &noqname);
4878                                 if (tresult == ISC_R_SUCCESS &&
4879                                     noqname != NULL) {
4880                                         tresult = dns_rdataset_addnoqname(
4881                                                             rdataset, noqname);
4882                                         RUNTIME_CHECK(tresult == ISC_R_SUCCESS);
4883                                 }
4884                         }
4885
4886                         /*
4887                          * Now we can add the rdataset.
4888                          */
4889                         result = dns_db_addrdataset(fctx->cache,
4890                                                     node, NULL, now,
4891                                                     rdataset,
4892                                                     options,
4893                                                     addedrdataset);
4894
4895                         if (result == DNS_R_UNCHANGED) {
4896                                 if (ANSWER(rdataset) &&
4897                                     ardataset != NULL &&
4898                                     NEGATIVE(ardataset)) {
4899                                         /*
4900                                          * The answer in the cache is better
4901                                          * than the answer we found, and is
4902                                          * a negative cache entry, so we
4903                                          * must set eresult appropriately.
4904                                          */
4905                                         if (NXDOMAIN(ardataset))
4906                                                 eresult = DNS_R_NCACHENXDOMAIN;
4907                                         else
4908                                                 eresult = DNS_R_NCACHENXRRSET;
4909                                 }
4910                                 result = ISC_R_SUCCESS;
4911                         } else if (result != ISC_R_SUCCESS)
4912                                 break;
4913                 }
4914         }
4915
4916         if (valrdataset != NULL)
4917                 result = valcreate(fctx, addrinfo, name, fctx->type,
4918                                    valrdataset, valsigrdataset, valoptions,
4919                                    task);
4920
4921         if (result == ISC_R_SUCCESS && have_answer) {
4922                 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
4923                 if (event != NULL) {
4924                         /*
4925                          * Negative results must be indicated in event->result.
4926                          */
4927                         if (dns_rdataset_isassociated(event->rdataset) &&
4928                             NEGATIVE(event->rdataset)) {
4929                                 INSIST(eresult == DNS_R_NCACHENXDOMAIN ||
4930                                        eresult == DNS_R_NCACHENXRRSET);
4931                         }
4932                         event->result = eresult;
4933                         dns_db_attach(fctx->cache, adbp);
4934                         dns_db_transfernode(fctx->cache, &node, anodep);
4935                         clone_results(fctx);
4936                 }
4937         }
4938
4939         if (node != NULL)
4940                 dns_db_detachnode(fctx->cache, &node);
4941
4942         return (result);
4943 }
4944
4945 static inline isc_result_t
4946 cache_message(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, isc_stdtime_t now)
4947 {
4948         isc_result_t result;
4949         dns_section_t section;
4950         dns_name_t *name;
4951
4952         FCTXTRACE("cache_message");
4953
4954         fctx->attributes &= ~FCTX_ATTR_WANTCACHE;
4955
4956         LOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4957
4958         for (section = DNS_SECTION_ANSWER;
4959              section <= DNS_SECTION_ADDITIONAL;
4960              section++) {
4961                 result = dns_message_firstname(fctx->rmessage, section);
4962                 while (result == ISC_R_SUCCESS) {
4963                         name = NULL;
4964                         dns_message_currentname(fctx->rmessage, section,
4965                                                 &name);
4966                         if ((name->attributes & DNS_NAMEATTR_CACHE) != 0) {
4967                                 result = cache_name(fctx, name, addrinfo, now);
4968                                 if (result != ISC_R_SUCCESS)
4969                                         break;
4970                         }
4971                         result = dns_message_nextname(fctx->rmessage, section);
4972                 }
4973                 if (result != ISC_R_NOMORE)
4974                         break;
4975         }
4976         if (result == ISC_R_NOMORE)
4977                 result = ISC_R_SUCCESS;
4978
4979         UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4980
4981         return (result);
4982 }
4983
4984 /*
4985  * Do what dns_ncache_addoptout() does, and then compute an appropriate eresult.
4986  */
4987 static isc_result_t
4988 ncache_adderesult(dns_message_t *message, dns_db_t *cache, dns_dbnode_t *node,
4989                   dns_rdatatype_t covers, isc_stdtime_t now, dns_ttl_t maxttl,
4990                   isc_boolean_t optout, isc_boolean_t secure,
4991                   dns_rdataset_t *ardataset, isc_result_t *eresultp)
4992 {
4993         isc_result_t result;
4994         dns_rdataset_t rdataset;
4995
4996         if (ardataset == NULL) {
4997                 dns_rdataset_init(&rdataset);
4998                 ardataset = &rdataset;
4999         }
5000         if (secure)
5001                 result = dns_ncache_addoptout(message, cache, node, covers,
5002                                               now, maxttl, optout, ardataset);
5003         else
5004                 result = dns_ncache_add(message, cache, node, covers, now,
5005                                         maxttl, ardataset);
5006         if (result == DNS_R_UNCHANGED || result == ISC_R_SUCCESS) {
5007                 /*
5008                  * If the cache now contains a negative entry and we
5009                  * care about whether it is DNS_R_NCACHENXDOMAIN or
5010                  * DNS_R_NCACHENXRRSET then extract it.
5011                  */
5012                 if (NEGATIVE(ardataset)) {
5013                         /*
5014                          * The cache data is a negative cache entry.
5015                          */
5016                         if (NXDOMAIN(ardataset))
5017                                 *eresultp = DNS_R_NCACHENXDOMAIN;
5018                         else
5019                                 *eresultp = DNS_R_NCACHENXRRSET;
5020                 } else {
5021                         /*
5022                          * Either we don't care about the nature of the
5023                          * cache rdataset (because no fetch is interested
5024                          * in the outcome), or the cache rdataset is not
5025                          * a negative cache entry.  Whichever case it is,
5026                          * we can return success.
5027                          *
5028                          * XXXRTH  There's a CNAME/DNAME problem here.
5029                          */
5030                         *eresultp = ISC_R_SUCCESS;
5031                 }
5032                 result = ISC_R_SUCCESS;
5033         }
5034         if (ardataset == &rdataset && dns_rdataset_isassociated(ardataset))
5035                 dns_rdataset_disassociate(ardataset);
5036
5037         return (result);
5038 }
5039
5040 static inline isc_result_t
5041 ncache_message(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
5042                dns_rdatatype_t covers, isc_stdtime_t now)
5043 {
5044         isc_result_t result, eresult;
5045         dns_name_t *name;
5046         dns_resolver_t *res;
5047         dns_db_t **adbp;
5048         dns_dbnode_t *node, **anodep;
5049         dns_rdataset_t *ardataset;
5050         isc_boolean_t need_validation, secure_domain;
5051         dns_name_t *aname;
5052         dns_fetchevent_t *event;
5053         isc_uint32_t ttl;
5054         unsigned int valoptions = 0;
5055
5056         FCTXTRACE("ncache_message");
5057
5058         fctx->attributes &= ~FCTX_ATTR_WANTNCACHE;
5059
5060         res = fctx->res;
5061         need_validation = ISC_FALSE;
5062         POST(need_validation);
5063         secure_domain = ISC_FALSE;
5064         eresult = ISC_R_SUCCESS;
5065         name = &fctx->name;
5066         node = NULL;
5067
5068         /*
5069          * XXXMPA remove when we follow cnames and adjust the setting
5070          * of FCTX_ATTR_WANTNCACHE in noanswer_response().
5071          */
5072         INSIST(fctx->rmessage->counts[DNS_SECTION_ANSWER] == 0);
5073
5074         /*
5075          * Is DNSSEC validation required for this name?
5076          */
5077         if (fctx->res->view->enablevalidation) {
5078                 result = dns_view_issecuredomain(res->view, name,
5079                                                  &secure_domain);
5080                 if (result != ISC_R_SUCCESS)
5081                         return (result);
5082
5083                 if (!secure_domain && res->view->dlv != NULL) {
5084                         valoptions = DNS_VALIDATOR_DLV;
5085                         secure_domain = ISC_TRUE;
5086                 }
5087         }
5088
5089         if ((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0)
5090                 need_validation = ISC_FALSE;
5091         else
5092                 need_validation = secure_domain;
5093
5094         if (secure_domain) {
5095                 /*
5096                  * Mark all rdatasets as pending.
5097                  */
5098                 dns_rdataset_t *trdataset;
5099                 dns_name_t *tname;
5100
5101                 result = dns_message_firstname(fctx->rmessage,
5102                                                DNS_SECTION_AUTHORITY);
5103                 while (result == ISC_R_SUCCESS) {
5104                         tname = NULL;
5105                         dns_message_currentname(fctx->rmessage,
5106                                                 DNS_SECTION_AUTHORITY,
5107                                                 &tname);
5108                         for (trdataset = ISC_LIST_HEAD(tname->list);
5109                              trdataset != NULL;
5110                              trdataset = ISC_LIST_NEXT(trdataset, link))
5111                                 trdataset->trust = dns_trust_pending_answer;
5112                         result = dns_message_nextname(fctx->rmessage,
5113                                                       DNS_SECTION_AUTHORITY);
5114                 }
5115                 if (result != ISC_R_NOMORE)
5116                         return (result);
5117
5118         }
5119
5120         if (need_validation) {
5121                 /*
5122                  * Do negative response validation.
5123                  */
5124                 result = valcreate(fctx, addrinfo, name, fctx->type,
5125                                    NULL, NULL, valoptions,
5126                                    res->buckets[fctx->bucketnum].task);
5127                 /*
5128                  * If validation is necessary, return now.  Otherwise continue
5129                  * to process the message, letting the validation complete
5130                  * in its own good time.
5131                  */
5132                 return (result);
5133         }
5134
5135         LOCK(&res->buckets[fctx->bucketnum].lock);
5136
5137         adbp = NULL;
5138         aname = NULL;
5139         anodep = NULL;
5140         ardataset = NULL;
5141         if (!HAVE_ANSWER(fctx)) {
5142                 event = ISC_LIST_HEAD(fctx->events);
5143                 if (event != NULL) {
5144                         adbp = &event->db;
5145                         aname = dns_fixedname_name(&event->foundname);
5146                         result = dns_name_copy(name, aname, NULL);
5147                         if (result != ISC_R_SUCCESS)
5148                                 goto unlock;
5149                         anodep = &event->node;
5150                         ardataset = event->rdataset;
5151                 }
5152         } else
5153                 event = NULL;
5154
5155         result = dns_db_findnode(fctx->cache, name, ISC_TRUE, &node);
5156         if (result != ISC_R_SUCCESS)
5157                 goto unlock;
5158
5159         /*
5160          * If we are asking for a SOA record set the cache time
5161          * to zero to facilitate locating the containing zone of
5162          * a arbitrary zone.
5163          */
5164         ttl = fctx->res->view->maxncachettl;
5165         if (fctx->type == dns_rdatatype_soa &&
5166             covers == dns_rdatatype_any &&
5167             fctx->res->zero_no_soa_ttl)
5168                 ttl = 0;
5169
5170         result = ncache_adderesult(fctx->rmessage, fctx->cache, node,
5171                                    covers, now, ttl, ISC_FALSE,
5172                                    ISC_FALSE, ardataset, &eresult);
5173         if (result != ISC_R_SUCCESS)
5174                 goto unlock;
5175
5176         if (!HAVE_ANSWER(fctx)) {
5177                 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
5178                 if (event != NULL) {
5179                         event->result = eresult;
5180                         dns_db_attach(fctx->cache, adbp);
5181                         dns_db_transfernode(fctx->cache, &node, anodep);
5182                         clone_results(fctx);
5183                 }
5184         }
5185
5186  unlock:
5187         UNLOCK(&res->buckets[fctx->bucketnum].lock);
5188
5189         if (node != NULL)
5190                 dns_db_detachnode(fctx->cache, &node);
5191
5192         return (result);
5193 }
5194
5195 static inline void
5196 mark_related(dns_name_t *name, dns_rdataset_t *rdataset,
5197              isc_boolean_t external, isc_boolean_t gluing)
5198 {
5199         name->attributes |= DNS_NAMEATTR_CACHE;
5200         if (gluing) {
5201                 rdataset->trust = dns_trust_glue;
5202                 /*
5203                  * Glue with 0 TTL causes problems.  We force the TTL to
5204                  * 1 second to prevent this.
5205                  */
5206                 if (rdataset->ttl == 0)
5207                         rdataset->ttl = 1;
5208         } else
5209                 rdataset->trust = dns_trust_additional;
5210         /*
5211          * Avoid infinite loops by only marking new rdatasets.
5212          */
5213         if (!CACHE(rdataset)) {
5214                 name->attributes |= DNS_NAMEATTR_CHASE;
5215                 rdataset->attributes |= DNS_RDATASETATTR_CHASE;
5216         }
5217         rdataset->attributes |= DNS_RDATASETATTR_CACHE;
5218         if (external)
5219                 rdataset->attributes |= DNS_RDATASETATTR_EXTERNAL;
5220 }
5221
5222 static isc_result_t
5223 check_section(void *arg, dns_name_t *addname, dns_rdatatype_t type,
5224               dns_section_t section)
5225 {
5226         fetchctx_t *fctx = arg;
5227         isc_result_t result;
5228         dns_name_t *name;
5229         dns_rdataset_t *rdataset;
5230         isc_boolean_t external;
5231         dns_rdatatype_t rtype;
5232         isc_boolean_t gluing;
5233
5234         REQUIRE(VALID_FCTX(fctx));
5235
5236 #if CHECK_FOR_GLUE_IN_ANSWER
5237         if (section == DNS_SECTION_ANSWER && type != dns_rdatatype_a)
5238                 return (ISC_R_SUCCESS);
5239 #endif
5240
5241         if (GLUING(fctx))
5242                 gluing = ISC_TRUE;
5243         else
5244                 gluing = ISC_FALSE;
5245         name = NULL;
5246         rdataset = NULL;
5247         result = dns_message_findname(fctx->rmessage, section, addname,
5248                                       dns_rdatatype_any, 0, &name, NULL);
5249         if (result == ISC_R_SUCCESS) {
5250                 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
5251                 if (type == dns_rdatatype_a) {
5252                         for (rdataset = ISC_LIST_HEAD(name->list);
5253                              rdataset != NULL;
5254                              rdataset = ISC_LIST_NEXT(rdataset, link)) {
5255                                 if (rdataset->type == dns_rdatatype_rrsig)
5256                                         rtype = rdataset->covers;
5257                                 else
5258                                         rtype = rdataset->type;
5259                                 if (rtype == dns_rdatatype_a ||
5260                                     rtype == dns_rdatatype_aaaa)
5261                                         mark_related(name, rdataset, external,
5262                                                      gluing);
5263                         }
5264                 } else {
5265                         result = dns_message_findtype(name, type, 0,
5266                                                       &rdataset);
5267                         if (result == ISC_R_SUCCESS) {
5268                                 mark_related(name, rdataset, external, gluing);
5269                                 /*
5270                                  * Do we have its SIG too?
5271                                  */
5272                                 rdataset = NULL;
5273                                 result = dns_message_findtype(name,
5274                                                       dns_rdatatype_rrsig,
5275                                                       type, &rdataset);
5276                                 if (result == ISC_R_SUCCESS)
5277                                         mark_related(name, rdataset, external,
5278                                                      gluing);
5279                         }
5280                 }
5281         }
5282
5283         return (ISC_R_SUCCESS);
5284 }
5285
5286 static isc_result_t
5287 check_related(void *arg, dns_name_t *addname, dns_rdatatype_t type) {
5288         return (check_section(arg, addname, type, DNS_SECTION_ADDITIONAL));
5289 }
5290
5291 #ifndef CHECK_FOR_GLUE_IN_ANSWER
5292 #define CHECK_FOR_GLUE_IN_ANSWER 0
5293 #endif
5294 #if CHECK_FOR_GLUE_IN_ANSWER
5295 static isc_result_t
5296 check_answer(void *arg, dns_name_t *addname, dns_rdatatype_t type) {
5297         return (check_section(arg, addname, type, DNS_SECTION_ANSWER));
5298 }
5299 #endif
5300
5301 static void
5302 chase_additional(fetchctx_t *fctx) {
5303         isc_boolean_t rescan;
5304         dns_section_t section = DNS_SECTION_ADDITIONAL;
5305         isc_result_t result;
5306
5307  again:
5308         rescan = ISC_FALSE;
5309
5310         for (result = dns_message_firstname(fctx->rmessage, section);
5311              result == ISC_R_SUCCESS;
5312              result = dns_message_nextname(fctx->rmessage, section)) {
5313                 dns_name_t *name = NULL;
5314                 dns_rdataset_t *rdataset;
5315                 dns_message_currentname(fctx->rmessage, DNS_SECTION_ADDITIONAL,
5316                                         &name);
5317                 if ((name->attributes & DNS_NAMEATTR_CHASE) == 0)
5318                         continue;
5319                 name->attributes &= ~DNS_NAMEATTR_CHASE;
5320                 for (rdataset = ISC_LIST_HEAD(name->list);
5321                      rdataset != NULL;
5322                      rdataset = ISC_LIST_NEXT(rdataset, link)) {
5323                         if (CHASE(rdataset)) {
5324                                 rdataset->attributes &= ~DNS_RDATASETATTR_CHASE;
5325                                 (void)dns_rdataset_additionaldata(rdataset,
5326                                                                   check_related,
5327                                                                   fctx);
5328                                 rescan = ISC_TRUE;
5329                         }
5330                 }
5331         }
5332         if (rescan)
5333                 goto again;
5334 }
5335
5336 static inline isc_result_t
5337 cname_target(dns_rdataset_t *rdataset, dns_name_t *tname) {
5338         isc_result_t result;
5339         dns_rdata_t rdata = DNS_RDATA_INIT;
5340         dns_rdata_cname_t cname;
5341
5342         result = dns_rdataset_first(rdataset);
5343         if (result != ISC_R_SUCCESS)
5344                 return (result);
5345         dns_rdataset_current(rdataset, &rdata);
5346         result = dns_rdata_tostruct(&rdata, &cname, NULL);
5347         if (result != ISC_R_SUCCESS)
5348                 return (result);
5349         dns_name_init(tname, NULL);
5350         dns_name_clone(&cname.cname, tname);
5351         dns_rdata_freestruct(&cname);
5352
5353         return (ISC_R_SUCCESS);
5354 }
5355
5356 static inline isc_result_t
5357 dname_target(fetchctx_t *fctx, dns_rdataset_t *rdataset, dns_name_t *qname,
5358              dns_name_t *oname, dns_fixedname_t *fixeddname)
5359 {
5360         isc_result_t result;
5361         dns_rdata_t rdata = DNS_RDATA_INIT;
5362         unsigned int nlabels;
5363         int order;
5364         dns_namereln_t namereln;
5365         dns_rdata_dname_t dname;
5366         dns_fixedname_t prefix;
5367
5368         /*
5369          * Get the target name of the DNAME.
5370          */
5371         result = dns_rdataset_first(rdataset);
5372         if (result != ISC_R_SUCCESS)
5373                 return (result);
5374         dns_rdataset_current(rdataset, &rdata);
5375         result = dns_rdata_tostruct(&rdata, &dname, NULL);
5376         if (result != ISC_R_SUCCESS)
5377                 return (result);
5378
5379         /*
5380          * Get the prefix of qname.
5381          */
5382         namereln = dns_name_fullcompare(qname, oname, &order, &nlabels);
5383         if (namereln != dns_namereln_subdomain) {
5384                 char qbuf[DNS_NAME_FORMATSIZE];
5385                 char obuf[DNS_NAME_FORMATSIZE];
5386
5387                 dns_rdata_freestruct(&dname);
5388                 dns_name_format(qname, qbuf, sizeof(qbuf));
5389                 dns_name_format(oname, obuf, sizeof(obuf));
5390                 log_formerr(fctx, "unrelated DNAME in answer: "
5391                                    "%s is not in %s", qbuf, obuf);
5392                 return (DNS_R_FORMERR);
5393         }
5394         dns_fixedname_init(&prefix);
5395         dns_name_split(qname, nlabels, dns_fixedname_name(&prefix), NULL);
5396         dns_fixedname_init(fixeddname);
5397         result = dns_name_concatenate(dns_fixedname_name(&prefix),
5398                                       &dname.dname,
5399                                       dns_fixedname_name(fixeddname), NULL);
5400         dns_rdata_freestruct(&dname);
5401         return (result);
5402 }
5403
5404 static isc_boolean_t
5405 is_answeraddress_allowed(dns_view_t *view, dns_name_t *name,
5406                          dns_rdataset_t *rdataset)
5407 {
5408         isc_result_t result;
5409         dns_rdata_t rdata = DNS_RDATA_INIT;
5410         struct in_addr ina;
5411         struct in6_addr in6a;
5412         isc_netaddr_t netaddr;
5413         char addrbuf[ISC_NETADDR_FORMATSIZE];
5414         char namebuf[DNS_NAME_FORMATSIZE];
5415         char classbuf[64];
5416         char typebuf[64];
5417         int match;
5418
5419         /* By default, we allow any addresses. */
5420         if (view->denyansweracl == NULL)
5421                 return (ISC_TRUE);
5422
5423         /*
5424          * If the owner name matches one in the exclusion list, either exactly
5425          * or partially, allow it.
5426          */
5427         if (view->answeracl_exclude != NULL) {
5428                 dns_rbtnode_t *node = NULL;
5429
5430                 result = dns_rbt_findnode(view->answeracl_exclude, name, NULL,
5431                                           &node, NULL, 0, NULL, NULL);
5432
5433                 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
5434                         return (ISC_TRUE);
5435         }
5436
5437         /*
5438          * Otherwise, search the filter list for a match for each address
5439          * record.  If a match is found, the address should be filtered,
5440          * so should the entire answer.
5441          */
5442         for (result = dns_rdataset_first(rdataset);
5443              result == ISC_R_SUCCESS;
5444              result = dns_rdataset_next(rdataset)) {
5445                 dns_rdata_reset(&rdata);
5446                 dns_rdataset_current(rdataset, &rdata);
5447                 if (rdataset->type == dns_rdatatype_a) {
5448                         INSIST(rdata.length == sizeof(ina.s_addr));
5449                         memmove(&ina.s_addr, rdata.data, sizeof(ina.s_addr));
5450                         isc_netaddr_fromin(&netaddr, &ina);
5451                 } else {
5452                         INSIST(rdata.length == sizeof(in6a.s6_addr));
5453                         memmove(in6a.s6_addr, rdata.data, sizeof(in6a.s6_addr));
5454                         isc_netaddr_fromin6(&netaddr, &in6a);
5455                 }
5456
5457                 result = dns_acl_match(&netaddr, NULL, view->denyansweracl,
5458                                        &view->aclenv, &match, NULL);
5459
5460                 if (result == ISC_R_SUCCESS && match > 0) {
5461                         isc_netaddr_format(&netaddr, addrbuf, sizeof(addrbuf));
5462                         dns_name_format(name, namebuf, sizeof(namebuf));
5463                         dns_rdatatype_format(rdataset->type, typebuf,
5464                                              sizeof(typebuf));
5465                         dns_rdataclass_format(rdataset->rdclass, classbuf,
5466                                               sizeof(classbuf));
5467                         isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
5468                                       DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
5469                                       "answer address %s denied for %s/%s/%s",
5470                                       addrbuf, namebuf, typebuf, classbuf);
5471                         return (ISC_FALSE);
5472                 }
5473         }
5474
5475         return (ISC_TRUE);
5476 }
5477
5478 static isc_boolean_t
5479 is_answertarget_allowed(dns_view_t *view, dns_name_t *name,
5480                         dns_rdatatype_t type, dns_name_t *tname,
5481                         dns_name_t *domain)
5482 {
5483         isc_result_t result;
5484         dns_rbtnode_t *node = NULL;
5485         char qnamebuf[DNS_NAME_FORMATSIZE];
5486         char tnamebuf[DNS_NAME_FORMATSIZE];
5487         char classbuf[64];
5488         char typebuf[64];
5489
5490         /* By default, we allow any target name. */
5491         if (view->denyanswernames == NULL)
5492                 return (ISC_TRUE);
5493
5494         /*
5495          * If the owner name matches one in the exclusion list, either exactly
5496          * or partially, allow it.
5497          */
5498         if (view->answernames_exclude != NULL) {
5499                 result = dns_rbt_findnode(view->answernames_exclude, name, NULL,
5500                                           &node, NULL, 0, NULL, NULL);
5501                 if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
5502                         return (ISC_TRUE);
5503         }
5504
5505         /*
5506          * If the target name is a subdomain of the search domain, allow it.
5507          */
5508         if (dns_name_issubdomain(tname, domain))
5509                 return (ISC_TRUE);
5510
5511         /*
5512          * Otherwise, apply filters.
5513          */
5514         result = dns_rbt_findnode(view->denyanswernames, tname, NULL, &node,
5515                                   NULL, 0, NULL, NULL);
5516         if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
5517                 dns_name_format(name, qnamebuf, sizeof(qnamebuf));
5518                 dns_name_format(tname, tnamebuf, sizeof(tnamebuf));
5519                 dns_rdatatype_format(type, typebuf, sizeof(typebuf));
5520                 dns_rdataclass_format(view->rdclass, classbuf,
5521                                       sizeof(classbuf));
5522                 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
5523                               DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
5524                               "%s target %s denied for %s/%s",
5525                               typebuf, tnamebuf, qnamebuf, classbuf);
5526                 return (ISC_FALSE);
5527         }
5528
5529         return (ISC_TRUE);
5530 }
5531
5532 static void
5533 trim_ns_ttl(fetchctx_t *fctx, dns_name_t *name, dns_rdataset_t *rdataset) {
5534         char ns_namebuf[DNS_NAME_FORMATSIZE];
5535         char namebuf[DNS_NAME_FORMATSIZE];
5536         char tbuf[DNS_RDATATYPE_FORMATSIZE];
5537
5538         if (fctx->ns_ttl_ok && rdataset->ttl > fctx->ns_ttl) {
5539                 dns_name_format(name, ns_namebuf, sizeof(ns_namebuf));
5540                 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
5541                 dns_rdatatype_format(fctx->type, tbuf, sizeof(tbuf));
5542
5543                 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
5544                               DNS_LOGMODULE_RESOLVER, ISC_LOG_DEBUG(10),
5545                               "fctx %p: trimming ttl of %s/NS for %s/%s: "
5546                               "%u -> %u", fctx, ns_namebuf, namebuf, tbuf,
5547                               rdataset->ttl, fctx->ns_ttl);
5548                 rdataset->ttl = fctx->ns_ttl;
5549         }
5550 }
5551
5552 /*
5553  * Handle a no-answer response (NXDOMAIN, NXRRSET, or referral).
5554  * If look_in_options has LOOK_FOR_NS_IN_ANSWER then we look in the answer
5555  * section for the NS RRset if the query type is NS; if it has
5556  * LOOK_FOR_GLUE_IN_ANSWER we look for glue incorrectly returned in the answer
5557  * section for A and AAAA queries.
5558  */
5559 #define LOOK_FOR_NS_IN_ANSWER 0x1
5560 #define LOOK_FOR_GLUE_IN_ANSWER 0x2
5561
5562 static isc_result_t
5563 noanswer_response(fetchctx_t *fctx, dns_name_t *oqname,
5564                   unsigned int look_in_options)
5565 {
5566         isc_result_t result;
5567         dns_message_t *message;
5568         dns_name_t *name, *qname, *ns_name, *soa_name, *ds_name, *save_name;
5569         dns_rdataset_t *rdataset, *ns_rdataset;
5570         isc_boolean_t aa, negative_response;
5571         dns_rdatatype_t type, save_type;
5572         dns_section_t section;
5573
5574         FCTXTRACE("noanswer_response");
5575
5576         if ((look_in_options & LOOK_FOR_NS_IN_ANSWER) != 0) {
5577                 INSIST(fctx->type == dns_rdatatype_ns);
5578                 section = DNS_SECTION_ANSWER;
5579         } else
5580                 section = DNS_SECTION_AUTHORITY;
5581
5582         message = fctx->rmessage;
5583
5584         /*
5585          * Setup qname.
5586          */
5587         if (oqname == NULL) {
5588                 /*
5589                  * We have a normal, non-chained negative response or
5590                  * referral.
5591                  */
5592                 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0)
5593                         aa = ISC_TRUE;
5594                 else
5595                         aa = ISC_FALSE;
5596                 qname = &fctx->name;
5597         } else {
5598                 /*
5599                  * We're being invoked by answer_response() after it has
5600                  * followed a CNAME/DNAME chain.
5601                  */
5602                 qname = oqname;
5603                 aa = ISC_FALSE;
5604                 /*
5605                  * If the current qname is not a subdomain of the query
5606                  * domain, there's no point in looking at the authority
5607                  * section without doing DNSSEC validation.
5608                  *
5609                  * Until we do that validation, we'll just return success
5610                  * in this case.
5611                  */
5612                 if (!dns_name_issubdomain(qname, &fctx->domain))
5613                         return (ISC_R_SUCCESS);
5614         }
5615
5616         /*
5617          * We have to figure out if this is a negative response, or a
5618          * referral.
5619          */
5620
5621         /*
5622          * Sometimes we can tell if its a negative response by looking at
5623          * the message header.
5624          */
5625         negative_response = ISC_FALSE;
5626         if (message->rcode == dns_rcode_nxdomain ||
5627             (message->counts[DNS_SECTION_ANSWER] == 0 &&
5628              message->counts[DNS_SECTION_AUTHORITY] == 0))
5629                 negative_response = ISC_TRUE;
5630
5631         /*
5632          * Process the authority section.
5633          */
5634         ns_name = NULL;
5635         ns_rdataset = NULL;
5636         soa_name = NULL;
5637         ds_name = NULL;
5638         save_name = NULL;
5639         save_type = dns_rdatatype_none;
5640         result = dns_message_firstname(message, section);
5641         while (result == ISC_R_SUCCESS) {
5642                 name = NULL;
5643                 dns_message_currentname(message, section, &name);
5644                 if (dns_name_issubdomain(name, &fctx->domain)) {
5645                         /*
5646                          * Look for NS/SOA RRsets first.
5647                          */
5648                         for (rdataset = ISC_LIST_HEAD(name->list);
5649                              rdataset != NULL;
5650                              rdataset = ISC_LIST_NEXT(rdataset, link)) {
5651                                 type = rdataset->type;
5652                                 if (type == dns_rdatatype_rrsig)
5653                                         type = rdataset->covers;
5654                                 if (((type == dns_rdatatype_ns ||
5655                                       type == dns_rdatatype_soa) &&
5656                                      !dns_name_issubdomain(qname, name))) {
5657                                         char qbuf[DNS_NAME_FORMATSIZE];
5658                                         char nbuf[DNS_NAME_FORMATSIZE];
5659                                         char tbuf[DNS_RDATATYPE_FORMATSIZE];
5660                                         dns_rdatatype_format(fctx->type, tbuf,
5661                                                              sizeof(tbuf));
5662                                         dns_name_format(name, nbuf,
5663                                                              sizeof(nbuf));
5664                                         dns_name_format(qname, qbuf,
5665                                                              sizeof(qbuf));
5666                                         log_formerr(fctx,
5667                                                     "unrelated %s %s in "
5668                                                     "%s authority section",
5669                                                     tbuf, qbuf, nbuf);
5670                                         return (DNS_R_FORMERR);
5671                                 }
5672                                 if (type == dns_rdatatype_ns) {
5673                                         /*
5674                                          * NS or RRSIG NS.
5675                                          *
5676                                          * Only one set of NS RRs is allowed.
5677                                          */
5678                                         if (rdataset->type ==
5679                                             dns_rdatatype_ns) {
5680                                                 if (ns_name != NULL &&
5681                                                     name != ns_name) {
5682                                                         log_formerr(fctx,
5683                                                                 "multiple NS "
5684                                                                 "RRsets in "
5685                                                                 "authority "
5686                                                                 "section");
5687                                                         return (DNS_R_FORMERR);
5688                                                 }
5689                                                 ns_name = name;
5690                                                 ns_rdataset = rdataset;
5691                                         }
5692                                         name->attributes |=
5693                                                 DNS_NAMEATTR_CACHE;
5694                                         rdataset->attributes |=
5695                                                 DNS_RDATASETATTR_CACHE;
5696                                         rdataset->trust = dns_trust_glue;
5697                                 }
5698                                 if (type == dns_rdatatype_soa) {
5699                                         /*
5700                                          * SOA, or RRSIG SOA.
5701                                          *
5702                                          * Only one SOA is allowed.
5703                                          */
5704                                         if (rdataset->type ==
5705                                             dns_rdatatype_soa) {
5706                                                 if (soa_name != NULL &&
5707                                                     name != soa_name) {
5708                                                         log_formerr(fctx,
5709                                                                 "multiple SOA "
5710                                                                 "RRs in "
5711                                                                 "authority "
5712                                                                 "section");
5713                                                         return (DNS_R_FORMERR);
5714                                                 }
5715                                                 soa_name = name;
5716                                         }
5717                                         name->attributes |=
5718                                                 DNS_NAMEATTR_NCACHE;
5719                                         rdataset->attributes |=
5720                                                 DNS_RDATASETATTR_NCACHE;
5721                                         if (aa)
5722                                                 rdataset->trust =
5723                                                     dns_trust_authauthority;
5724                                         else if (ISFORWARDER(fctx->addrinfo))
5725                                                 rdataset->trust =
5726                                                         dns_trust_answer;
5727                                         else
5728                                                 rdataset->trust =
5729                                                         dns_trust_additional;
5730                                 }
5731                         }
5732                 }
5733                 result = dns_message_nextname(message, section);
5734                 if (result == ISC_R_NOMORE)
5735                         break;
5736                 else if (result != ISC_R_SUCCESS)
5737                         return (result);
5738         }
5739
5740         log_ns_ttl(fctx, "noanswer_response");
5741
5742         if (ns_rdataset != NULL && dns_name_equal(&fctx->domain, ns_name) &&
5743             !dns_name_equal(ns_name, dns_rootname))
5744                 trim_ns_ttl(fctx, ns_name, ns_rdataset);
5745
5746         /*
5747          * A negative response has a SOA record (Type 2)
5748          * and a optional NS RRset (Type 1) or it has neither
5749          * a SOA or a NS RRset (Type 3, handled above) or
5750          * rcode is NXDOMAIN (handled above) in which case
5751          * the NS RRset is allowed (Type 4).
5752          */
5753         if (soa_name != NULL)
5754                 negative_response = ISC_TRUE;
5755
5756         result = dns_message_firstname(message, section);
5757         while (result == ISC_R_SUCCESS) {
5758                 name = NULL;
5759                 dns_message_currentname(message, section, &name);
5760                 if (dns_name_issubdomain(name, &fctx->domain)) {
5761                         for (rdataset = ISC_LIST_HEAD(name->list);
5762                              rdataset != NULL;
5763                              rdataset = ISC_LIST_NEXT(rdataset, link)) {
5764                                 type = rdataset->type;
5765                                 if (type == dns_rdatatype_rrsig)
5766                                         type = rdataset->covers;
5767                                 if (type == dns_rdatatype_nsec ||
5768                                     type == dns_rdatatype_nsec3) {
5769                                         /*
5770                                          * NSEC or RRSIG NSEC.
5771                                          */
5772                                         if (negative_response) {
5773                                                 name->attributes |=
5774                                                         DNS_NAMEATTR_NCACHE;
5775                                                 rdataset->attributes |=
5776                                                         DNS_RDATASETATTR_NCACHE;
5777                                         } else if (type == dns_rdatatype_nsec) {
5778                                                 name->attributes |=
5779                                                         DNS_NAMEATTR_CACHE;
5780                                                 rdataset->attributes |=
5781                                                         DNS_RDATASETATTR_CACHE;
5782                                         }
5783                                         if (aa)
5784                                                 rdataset->trust =
5785                                                     dns_trust_authauthority;
5786                                         else if (ISFORWARDER(fctx->addrinfo))
5787                                                 rdataset->trust =
5788                                                         dns_trust_answer;
5789                                         else
5790                                                 rdataset->trust =
5791                                                         dns_trust_additional;
5792                                         /*
5793                                          * No additional data needs to be
5794                                          * marked.
5795                                          */
5796                                 } else if (type == dns_rdatatype_ds) {
5797                                         /*
5798                                          * DS or SIG DS.
5799                                          *
5800                                          * These should only be here if
5801                                          * this is a referral, and there
5802                                          * should only be one DS RRset.
5803                                          */
5804                                         if (ns_name == NULL) {
5805                                                 log_formerr(fctx,
5806                                                             "DS with no "
5807                                                             "referral");
5808                                                 return (DNS_R_FORMERR);
5809                                         }
5810                                         if (rdataset->type ==
5811                                             dns_rdatatype_ds) {
5812                                                 if (ds_name != NULL &&
5813                                                     name != ds_name) {
5814                                                         log_formerr(fctx,
5815                                                                 "DS doesn't "
5816                                                                 "match "
5817                                                                 "referral "
5818                                                                 "(NS)");
5819                                                         return (DNS_R_FORMERR);
5820                                                 }
5821                                                 ds_name = name;
5822                                         }
5823                                         name->attributes |=
5824                                                 DNS_NAMEATTR_CACHE;
5825                                         rdataset->attributes |=
5826                                                 DNS_RDATASETATTR_CACHE;
5827                                         if (aa)
5828                                                 rdataset->trust =
5829                                                     dns_trust_authauthority;
5830                                         else if (ISFORWARDER(fctx->addrinfo))
5831                                                 rdataset->trust =
5832                                                         dns_trust_answer;
5833                                         else
5834                                                 rdataset->trust =
5835                                                         dns_trust_additional;
5836                                 }
5837                         }
5838                 } else {
5839                         save_name = name;
5840                         save_type = ISC_LIST_HEAD(name->list)->type;
5841                 }
5842                 result = dns_message_nextname(message, section);
5843                 if (result == ISC_R_NOMORE)
5844                         break;
5845                 else if (result != ISC_R_SUCCESS)
5846                         return (result);
5847         }
5848
5849         /*
5850          * Trigger lookups for DNS nameservers.
5851          */
5852         if (negative_response && message->rcode == dns_rcode_noerror &&
5853             fctx->type == dns_rdatatype_ds && soa_name != NULL &&
5854             dns_name_equal(soa_name, qname) &&
5855             !dns_name_equal(qname, dns_rootname))
5856                 return (DNS_R_CHASEDSSERVERS);
5857
5858         /*
5859          * Did we find anything?
5860          */
5861         if (!negative_response && ns_name == NULL) {
5862                 /*
5863                  * Nope.
5864                  */
5865                 if (oqname != NULL) {
5866                         /*
5867                          * We've already got a partial CNAME/DNAME chain,
5868                          * and haven't found else anything useful here, but
5869                          * no error has occurred since we have an answer.
5870                          */
5871                         return (ISC_R_SUCCESS);
5872                 } else {
5873                         /*
5874                          * The responder is insane.
5875                          */
5876                         if (save_name == NULL) {
5877                                 log_formerr(fctx, "invalid response");
5878                                 return (DNS_R_FORMERR);
5879                         }
5880                         if (!dns_name_issubdomain(save_name, &fctx->domain)) {
5881                                 char nbuf[DNS_NAME_FORMATSIZE];
5882                                 char dbuf[DNS_NAME_FORMATSIZE];
5883                                 char tbuf[DNS_RDATATYPE_FORMATSIZE];
5884
5885                                 dns_rdatatype_format(save_type, tbuf,
5886                                         sizeof(tbuf));
5887                                 dns_name_format(save_name, nbuf, sizeof(nbuf));
5888                                 dns_name_format(&fctx->domain, dbuf,
5889                                         sizeof(dbuf));
5890
5891                                 log_formerr(fctx, "Name %s (%s) not subdomain"
5892                                         " of zone %s -- invalid response",
5893                                         nbuf, tbuf, dbuf);
5894                         } else {
5895                                 log_formerr(fctx, "invalid response");
5896                         }
5897                         return (DNS_R_FORMERR);
5898                 }
5899         }
5900
5901         /*
5902          * If we found both NS and SOA, they should be the same name.
5903          */
5904         if (ns_name != NULL && soa_name != NULL && ns_name != soa_name) {
5905                 log_formerr(fctx, "NS/SOA mismatch");
5906                 return (DNS_R_FORMERR);
5907         }
5908
5909         /*
5910          * Do we have a referral?  (We only want to follow a referral if
5911          * we're not following a chain.)
5912          */
5913         if (!negative_response && ns_name != NULL && oqname == NULL) {
5914                 /*
5915                  * We already know ns_name is a subdomain of fctx->domain.
5916                  * If ns_name is equal to fctx->domain, we're not making
5917                  * progress.  We return DNS_R_FORMERR so that we'll keep
5918                  * trying other servers.
5919                  */
5920                 if (dns_name_equal(ns_name, &fctx->domain)) {
5921                         log_formerr(fctx, "non-improving referral");
5922                         return (DNS_R_FORMERR);
5923                 }
5924
5925                 /*
5926                  * If the referral name is not a parent of the query
5927                  * name, consider the responder insane.
5928                  */
5929                 if (! dns_name_issubdomain(&fctx->name, ns_name)) {
5930                         /* Logged twice */
5931                         log_formerr(fctx, "referral to non-parent");
5932                         FCTXTRACE("referral to non-parent");
5933                         return (DNS_R_FORMERR);
5934                 }
5935
5936                 /*
5937                  * Mark any additional data related to this rdataset.
5938                  * It's important that we do this before we change the
5939                  * query domain.
5940                  */
5941                 INSIST(ns_rdataset != NULL);
5942                 fctx->attributes |= FCTX_ATTR_GLUING;
5943                 (void)dns_rdataset_additionaldata(ns_rdataset, check_related,
5944                                                   fctx);
5945 #if CHECK_FOR_GLUE_IN_ANSWER
5946                 /*
5947                  * Look in the answer section for "glue" that is incorrectly
5948                  * returned as a answer.  This is needed if the server also
5949                  * minimizes the response size by not adding records to the
5950                  * additional section that are in the answer section or if
5951                  * the record gets dropped due to message size constraints.
5952                  */
5953                 if ((look_in_options & LOOK_FOR_GLUE_IN_ANSWER) != 0 &&
5954                     (fctx->type == dns_rdatatype_aaaa ||
5955                      fctx->type == dns_rdatatype_a))
5956                         (void)dns_rdataset_additionaldata(ns_rdataset,
5957                                                           check_answer, fctx);
5958 #endif
5959                 fctx->attributes &= ~FCTX_ATTR_GLUING;
5960                 /*
5961                  * NS rdatasets with 0 TTL cause problems.
5962                  * dns_view_findzonecut() will not find them when we
5963                  * try to follow the referral, and we'll SERVFAIL
5964                  * because the best nameservers are now above QDOMAIN.
5965                  * We force the TTL to 1 second to prevent this.
5966                  */
5967                 if (ns_rdataset->ttl == 0)
5968                         ns_rdataset->ttl = 1;
5969                 /*
5970                  * Set the current query domain to the referral name.
5971                  *
5972                  * XXXRTH  We should check if we're in forward-only mode, and
5973                  *              if so we should bail out.
5974                  */
5975                 INSIST(dns_name_countlabels(&fctx->domain) > 0);
5976                 dns_name_free(&fctx->domain, fctx->mctx);
5977                 if (dns_rdataset_isassociated(&fctx->nameservers))
5978                         dns_rdataset_disassociate(&fctx->nameservers);
5979                 dns_name_init(&fctx->domain, NULL);
5980                 result = dns_name_dup(ns_name, fctx->mctx, &fctx->domain);
5981                 if (result != ISC_R_SUCCESS)
5982                         return (result);
5983                 fctx->attributes |= FCTX_ATTR_WANTCACHE;
5984                 fctx->ns_ttl_ok = ISC_FALSE;
5985                 log_ns_ttl(fctx, "DELEGATION");
5986                 return (DNS_R_DELEGATION);
5987         }
5988
5989         /*
5990          * Since we're not doing a referral, we don't want to cache any
5991          * NS RRs we may have found.
5992          */
5993         if (ns_name != NULL)
5994                 ns_name->attributes &= ~DNS_NAMEATTR_CACHE;
5995
5996         if (negative_response && oqname == NULL)
5997                 fctx->attributes |= FCTX_ATTR_WANTNCACHE;
5998
5999         return (ISC_R_SUCCESS);
6000 }
6001
6002 static isc_result_t
6003 answer_response(fetchctx_t *fctx) {
6004         isc_result_t result;
6005         dns_message_t *message;
6006         dns_name_t *name, *qname, tname, *ns_name;
6007         dns_rdataset_t *rdataset, *ns_rdataset;
6008         isc_boolean_t done, external, chaining, aa, found, want_chaining;
6009         isc_boolean_t have_answer, found_cname, found_type, wanted_chaining;
6010         unsigned int aflag;
6011         dns_rdatatype_t type;
6012         dns_fixedname_t dname, fqname;
6013         dns_view_t *view;
6014
6015         FCTXTRACE("answer_response");
6016
6017         message = fctx->rmessage;
6018
6019         /*
6020          * Examine the answer section, marking those rdatasets which are
6021          * part of the answer and should be cached.
6022          */
6023
6024         done = ISC_FALSE;
6025         found_cname = ISC_FALSE;
6026         found_type = ISC_FALSE;
6027         chaining = ISC_FALSE;
6028         have_answer = ISC_FALSE;
6029         want_chaining = ISC_FALSE;
6030         POST(want_chaining);
6031         if ((message->flags & DNS_MESSAGEFLAG_AA) != 0)
6032                 aa = ISC_TRUE;
6033         else
6034                 aa = ISC_FALSE;
6035         qname = &fctx->name;
6036         type = fctx->type;
6037         view = fctx->res->view;
6038         result = dns_message_firstname(message, DNS_SECTION_ANSWER);
6039         while (!done && result == ISC_R_SUCCESS) {
6040                 name = NULL;
6041                 dns_message_currentname(message, DNS_SECTION_ANSWER, &name);
6042                 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
6043                 if (dns_name_equal(name, qname)) {
6044                         wanted_chaining = ISC_FALSE;
6045                         for (rdataset = ISC_LIST_HEAD(name->list);
6046                              rdataset != NULL;
6047                              rdataset = ISC_LIST_NEXT(rdataset, link)) {
6048                                 found = ISC_FALSE;
6049                                 want_chaining = ISC_FALSE;
6050                                 aflag = 0;
6051                                 if (rdataset->type == dns_rdatatype_nsec3) {
6052                                         /*
6053                                          * NSEC3 records are not allowed to
6054                                          * appear in the answer section.
6055                                          */
6056                                         log_formerr(fctx, "NSEC3 in answer");
6057                                         return (DNS_R_FORMERR);
6058                                 }
6059
6060                                 /*
6061                                  * Apply filters, if given, on answers to reject
6062                                  * a malicious attempt of rebinding.
6063                                  */
6064                                 if ((rdataset->type == dns_rdatatype_a ||
6065                                      rdataset->type == dns_rdatatype_aaaa) &&
6066                                     !is_answeraddress_allowed(view, name,
6067                                                               rdataset)) {
6068                                         return (DNS_R_SERVFAIL);
6069                                 }
6070
6071                                 if (rdataset->type == type && !found_cname) {
6072                                         /*
6073                                          * We've found an ordinary answer.
6074                                          */
6075                                         found = ISC_TRUE;
6076                                         found_type = ISC_TRUE;
6077                                         done = ISC_TRUE;
6078                                         aflag = DNS_RDATASETATTR_ANSWER;
6079                                 } else if (type == dns_rdatatype_any) {
6080                                         /*
6081                                          * We've found an answer matching
6082                                          * an ANY query.  There may be
6083                                          * more.
6084                                          */
6085                                         found = ISC_TRUE;
6086                                         aflag = DNS_RDATASETATTR_ANSWER;
6087                                 } else if (rdataset->type == dns_rdatatype_rrsig
6088                                            && rdataset->covers == type
6089                                            && !found_cname) {
6090                                         /*
6091                                          * We've found a signature that
6092                                          * covers the type we're looking for.
6093                                          */
6094                                         found = ISC_TRUE;
6095                                         found_type = ISC_TRUE;
6096                                         aflag = DNS_RDATASETATTR_ANSWERSIG;
6097                                 } else if (rdataset->type ==
6098                                            dns_rdatatype_cname
6099                                            && !found_type) {
6100                                         /*
6101                                          * We're looking for something else,
6102                                          * but we found a CNAME.
6103                                          *
6104                                          * Getting a CNAME response for some
6105                                          * query types is an error, see
6106                                          * RFC 4035, Section 2.5.
6107                                          */
6108                                         if (type == dns_rdatatype_rrsig ||
6109                                             type == dns_rdatatype_key ||
6110                                             type == dns_rdatatype_nsec) {
6111                                                 char buf[DNS_RDATATYPE_FORMATSIZE];
6112                                                 dns_rdatatype_format(fctx->type,
6113                                                               buf, sizeof(buf));
6114                                                 log_formerr(fctx,
6115                                                             "CNAME response "
6116                                                             "for %s RR", buf);
6117                                                 return (DNS_R_FORMERR);
6118                                         }
6119                                         found = ISC_TRUE;
6120                                         found_cname = ISC_TRUE;
6121                                         want_chaining = ISC_TRUE;
6122                                         aflag = DNS_RDATASETATTR_ANSWER;
6123                                         result = cname_target(rdataset,
6124                                                               &tname);
6125                                         if (result != ISC_R_SUCCESS)
6126                                                 return (result);
6127                                         /* Apply filters on the target name. */
6128                                         if (!is_answertarget_allowed(view,
6129                                                         name,
6130                                                         rdataset->type,
6131                                                         &tname,
6132                                                         &fctx->domain)) {
6133                                                 return (DNS_R_SERVFAIL);
6134                                         }
6135                                 } else if (rdataset->type == dns_rdatatype_rrsig
6136                                            && rdataset->covers ==
6137                                            dns_rdatatype_cname
6138                                            && !found_type) {
6139                                         /*
6140                                          * We're looking for something else,
6141                                          * but we found a SIG CNAME.
6142                                          */
6143                                         found = ISC_TRUE;
6144                                         found_cname = ISC_TRUE;
6145                                         aflag = DNS_RDATASETATTR_ANSWERSIG;
6146                                 }
6147
6148                                 if (found) {
6149                                         /*
6150                                          * We've found an answer to our
6151                                          * question.
6152                                          */
6153                                         name->attributes |=
6154                                                 DNS_NAMEATTR_CACHE;
6155                                         rdataset->attributes |=
6156                                                 DNS_RDATASETATTR_CACHE;
6157                                         rdataset->trust = dns_trust_answer;
6158                                         if (!chaining) {
6159                                                 /*
6160                                                  * This data is "the" answer
6161                                                  * to our question only if
6162                                                  * we're not chaining (i.e.
6163                                                  * if we haven't followed
6164                                                  * a CNAME or DNAME).
6165                                                  */
6166                                                 INSIST(!external);
6167                                                 if (aflag ==
6168                                                     DNS_RDATASETATTR_ANSWER)
6169                                                         have_answer = ISC_TRUE;
6170                                                 name->attributes |=
6171                                                         DNS_NAMEATTR_ANSWER;
6172                                                 rdataset->attributes |= aflag;
6173                                                 if (aa)
6174                                                         rdataset->trust =
6175                                                           dns_trust_authanswer;
6176                                         } else if (external) {
6177                                                 /*
6178                                                  * This data is outside of
6179                                                  * our query domain, and
6180                                                  * may not be cached.
6181                                                  */
6182                                                 rdataset->attributes |=
6183                                                     DNS_RDATASETATTR_EXTERNAL;
6184                                         }
6185
6186                                         /*
6187                                          * Mark any additional data related
6188                                          * to this rdataset.
6189                                          */
6190                                         (void)dns_rdataset_additionaldata(
6191                                                         rdataset,
6192                                                         check_related,
6193                                                         fctx);
6194
6195                                         /*
6196                                          * CNAME chaining.
6197                                          */
6198                                         if (want_chaining) {
6199                                                 wanted_chaining = ISC_TRUE;
6200                                                 name->attributes |=
6201                                                         DNS_NAMEATTR_CHAINING;
6202                                                 rdataset->attributes |=
6203                                                     DNS_RDATASETATTR_CHAINING;
6204                                                 qname = &tname;
6205                                         }
6206                                 }
6207                                 /*
6208                                  * We could add an "else" clause here and
6209                                  * log that we're ignoring this rdataset.
6210                                  */
6211                         }
6212                         /*
6213                          * If wanted_chaining is true, we've done
6214                          * some chaining as the result of processing
6215                          * this node, and thus we need to set
6216                          * chaining to true.
6217                          *
6218                          * We don't set chaining inside of the
6219                          * rdataset loop because doing that would
6220                          * cause us to ignore the signatures of
6221                          * CNAMEs.
6222                          */
6223                         if (wanted_chaining)
6224                                 chaining = ISC_TRUE;
6225                 } else {
6226                         /*
6227                          * Look for a DNAME (or its SIG).  Anything else is
6228                          * ignored.
6229                          */
6230                         wanted_chaining = ISC_FALSE;
6231                         for (rdataset = ISC_LIST_HEAD(name->list);
6232                              rdataset != NULL;
6233                              rdataset = ISC_LIST_NEXT(rdataset, link)) {
6234                                 isc_boolean_t found_dname = ISC_FALSE;
6235                                 dns_name_t *dname_name;
6236
6237                                 found = ISC_FALSE;
6238                                 aflag = 0;
6239                                 if (rdataset->type == dns_rdatatype_dname) {
6240                                         /*
6241                                          * We're looking for something else,
6242                                          * but we found a DNAME.
6243                                          *
6244                                          * If we're not chaining, then the
6245                                          * DNAME should not be external.
6246                                          */
6247                                         if (!chaining && external) {
6248                                                 log_formerr(fctx,
6249                                                             "external DNAME");
6250                                                 return (DNS_R_FORMERR);
6251                                         }
6252                                         found = ISC_TRUE;
6253                                         want_chaining = ISC_TRUE;
6254                                         POST(want_chaining);
6255                                         aflag = DNS_RDATASETATTR_ANSWER;
6256                                         result = dname_target(fctx, rdataset,
6257                                                               qname, name,
6258                                                               &dname);
6259                                         if (result == ISC_R_NOSPACE) {
6260                                                 /*
6261                                                  * We can't construct the
6262                                                  * DNAME target.  Do not
6263                                                  * try to continue.
6264                                                  */
6265                                                 want_chaining = ISC_FALSE;
6266                                                 POST(want_chaining);
6267                                         } else if (result != ISC_R_SUCCESS)
6268                                                 return (result);
6269                                         else
6270                                                 found_dname = ISC_TRUE;
6271
6272                                         dname_name = dns_fixedname_name(&dname);
6273                                         if (!is_answertarget_allowed(view,
6274                                                         qname,
6275                                                         rdataset->type,
6276                                                         dname_name,
6277                                                         &fctx->domain)) {
6278                                                 return (DNS_R_SERVFAIL);
6279                                         }
6280                                 } else if (rdataset->type == dns_rdatatype_rrsig
6281                                            && rdataset->covers ==
6282                                            dns_rdatatype_dname) {
6283                                         /*
6284                                          * We've found a signature that
6285                                          * covers the DNAME.
6286                                          */
6287                                         found = ISC_TRUE;
6288                                         aflag = DNS_RDATASETATTR_ANSWERSIG;
6289                                 }
6290
6291                                 if (found) {
6292                                         /*
6293                                          * We've found an answer to our
6294                                          * question.
6295                                          */
6296                                         name->attributes |=
6297                                                 DNS_NAMEATTR_CACHE;
6298                                         rdataset->attributes |=
6299                                                 DNS_RDATASETATTR_CACHE;
6300                                         rdataset->trust = dns_trust_answer;
6301                                         if (!chaining) {
6302                                                 /*
6303                                                  * This data is "the" answer
6304                                                  * to our question only if
6305                                                  * we're not chaining.
6306                                                  */
6307                                                 INSIST(!external);
6308                                                 if (aflag ==
6309                                                     DNS_RDATASETATTR_ANSWER)
6310                                                         have_answer = ISC_TRUE;
6311                                                 name->attributes |=
6312                                                         DNS_NAMEATTR_ANSWER;
6313                                                 rdataset->attributes |= aflag;
6314                                                 if (aa)
6315                                                         rdataset->trust =
6316                                                           dns_trust_authanswer;
6317                                         } else if (external) {
6318                                                 rdataset->attributes |=
6319                                                     DNS_RDATASETATTR_EXTERNAL;
6320                                         }
6321
6322                                         /*
6323                                          * DNAME chaining.
6324                                          */
6325                                         if (found_dname) {
6326                                                 /*
6327                                                  * Copy the dname into the
6328                                                  * qname fixed name.
6329                                                  *
6330                                                  * Although we check for
6331                                                  * failure of the copy
6332                                                  * operation, in practice it
6333                                                  * should never fail since
6334                                                  * we already know that the
6335                                                  * result fits in a fixedname.
6336                                                  */
6337                                                 dns_fixedname_init(&fqname);
6338                                                 result = dns_name_copy(
6339                                                   dns_fixedname_name(&dname),
6340                                                   dns_fixedname_name(&fqname),
6341                                                   NULL);
6342                                                 if (result != ISC_R_SUCCESS)
6343                                                         return (result);
6344                                                 wanted_chaining = ISC_TRUE;
6345                                                 name->attributes |=
6346                                                         DNS_NAMEATTR_CHAINING;
6347                                                 rdataset->attributes |=
6348                                                     DNS_RDATASETATTR_CHAINING;
6349                                                 qname = dns_fixedname_name(
6350                                                                    &fqname);
6351                                         }
6352                                 }
6353                         }
6354                         if (wanted_chaining)
6355                                 chaining = ISC_TRUE;
6356                 }
6357                 result = dns_message_nextname(message, DNS_SECTION_ANSWER);
6358         }
6359         if (result == ISC_R_NOMORE)
6360                 result = ISC_R_SUCCESS;
6361         if (result != ISC_R_SUCCESS)
6362                 return (result);
6363
6364         /*
6365          * We should have found an answer.
6366          */
6367         if (!have_answer) {
6368                 log_formerr(fctx, "reply has no answer");
6369                 return (DNS_R_FORMERR);
6370         }
6371
6372         /*
6373          * This response is now potentially cacheable.
6374          */
6375         fctx->attributes |= FCTX_ATTR_WANTCACHE;
6376
6377         /*
6378          * Did chaining end before we got the final answer?
6379          */
6380         if (chaining) {
6381                 /*
6382                  * Yes.  This may be a negative reply, so hand off
6383                  * authority section processing to the noanswer code.
6384                  * If it isn't a noanswer response, no harm will be
6385                  * done.
6386                  */
6387                 return (noanswer_response(fctx, qname, 0));
6388         }
6389
6390         /*
6391          * We didn't end with an incomplete chain, so the rcode should be
6392          * "no error".
6393          */
6394         if (message->rcode != dns_rcode_noerror) {
6395                 log_formerr(fctx, "CNAME/DNAME chain complete, but RCODE "
6396                                   "indicates error");
6397                 return (DNS_R_FORMERR);
6398         }
6399
6400         /*
6401          * Examine the authority section (if there is one).
6402          *
6403          * We expect there to be only one owner name for all the rdatasets
6404          * in this section, and we expect that it is not external.
6405          */
6406         done = ISC_FALSE;
6407         ns_name = NULL;
6408         ns_rdataset = NULL;
6409         result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
6410         while (!done && result == ISC_R_SUCCESS) {
6411                 name = NULL;
6412                 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
6413                 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
6414                 if (!external) {
6415                         /*
6416                          * We expect to find NS or SIG NS rdatasets, and
6417                          * nothing else.
6418                          */
6419                         for (rdataset = ISC_LIST_HEAD(name->list);
6420                              rdataset != NULL;
6421                              rdataset = ISC_LIST_NEXT(rdataset, link)) {
6422                                 if (rdataset->type == dns_rdatatype_ns ||
6423                                     (rdataset->type == dns_rdatatype_rrsig &&
6424                                      rdataset->covers == dns_rdatatype_ns)) {
6425                                         name->attributes |=
6426                                                 DNS_NAMEATTR_CACHE;
6427                                         rdataset->attributes |=
6428                                                 DNS_RDATASETATTR_CACHE;
6429                                         if (aa && !chaining)
6430                                                 rdataset->trust =
6431                                                     dns_trust_authauthority;
6432                                         else
6433                                                 rdataset->trust =
6434                                                     dns_trust_additional;
6435
6436                                         if (rdataset->type == dns_rdatatype_ns) {
6437                                                 ns_name = name;
6438                                                 ns_rdataset = rdataset;
6439                                         }
6440                                         /*
6441                                          * Mark any additional data related
6442                                          * to this rdataset.
6443                                          */
6444                                         (void)dns_rdataset_additionaldata(
6445                                                         rdataset,
6446                                                         check_related,
6447                                                         fctx);
6448                                         done = ISC_TRUE;
6449                                 }
6450                         }
6451                 }
6452                 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
6453         }
6454         if (result == ISC_R_NOMORE)
6455                 result = ISC_R_SUCCESS;
6456
6457         log_ns_ttl(fctx, "answer_response");
6458
6459         if (ns_rdataset != NULL && dns_name_equal(&fctx->domain, ns_name) &&
6460             !dns_name_equal(ns_name, dns_rootname))
6461                 trim_ns_ttl(fctx, ns_name, ns_rdataset);
6462
6463         return (result);
6464 }
6465
6466 static isc_boolean_t
6467 fctx_decreference(fetchctx_t *fctx) {
6468         isc_boolean_t bucket_empty = ISC_FALSE;
6469
6470         INSIST(fctx->references > 0);
6471         fctx->references--;
6472         if (fctx->references == 0) {
6473                 /*
6474                  * No one cares about the result of this fetch anymore.
6475                  */
6476                 if (fctx->pending == 0 && fctx->nqueries == 0 &&
6477                     ISC_LIST_EMPTY(fctx->validators) && SHUTTINGDOWN(fctx)) {
6478                         /*
6479                          * This fctx is already shutdown; we were just
6480                          * waiting for the last reference to go away.
6481                          */
6482                         bucket_empty = fctx_unlink(fctx);
6483                         fctx_destroy(fctx);
6484                 } else {
6485                         /*
6486                          * Initiate shutdown.
6487                          */
6488                         fctx_shutdown(fctx);
6489                 }
6490         }
6491         return (bucket_empty);
6492 }
6493
6494 static void
6495 resume_dslookup(isc_task_t *task, isc_event_t *event) {
6496         dns_fetchevent_t *fevent;
6497         dns_resolver_t *res;
6498         fetchctx_t *fctx;
6499         isc_result_t result;
6500         isc_boolean_t bucket_empty;
6501         isc_boolean_t locked = ISC_FALSE;
6502         unsigned int bucketnum;
6503         dns_rdataset_t nameservers;
6504         dns_fixedname_t fixed;
6505         dns_name_t *domain;
6506
6507         REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
6508         fevent = (dns_fetchevent_t *)event;
6509         fctx = event->ev_arg;
6510         REQUIRE(VALID_FCTX(fctx));
6511         res = fctx->res;
6512
6513         UNUSED(task);
6514         FCTXTRACE("resume_dslookup");
6515
6516         if (fevent->node != NULL)
6517                 dns_db_detachnode(fevent->db, &fevent->node);
6518         if (fevent->db != NULL)
6519                 dns_db_detach(&fevent->db);
6520
6521         dns_rdataset_init(&nameservers);
6522
6523         bucketnum = fctx->bucketnum;
6524         if (fevent->result == ISC_R_CANCELED) {
6525                 dns_resolver_destroyfetch(&fctx->nsfetch);
6526                 fctx_done(fctx, ISC_R_CANCELED, __LINE__);
6527         } else if (fevent->result == ISC_R_SUCCESS) {
6528
6529                 FCTXTRACE("resuming DS lookup");
6530
6531                 dns_resolver_destroyfetch(&fctx->nsfetch);
6532                 if (dns_rdataset_isassociated(&fctx->nameservers))
6533                         dns_rdataset_disassociate(&fctx->nameservers);
6534                 dns_rdataset_clone(fevent->rdataset, &fctx->nameservers);
6535                 fctx->ns_ttl = fctx->nameservers.ttl;
6536                 fctx->ns_ttl_ok = ISC_TRUE;
6537                 log_ns_ttl(fctx, "resume_dslookup");
6538                 dns_name_free(&fctx->domain, fctx->mctx);
6539                 dns_name_init(&fctx->domain, NULL);
6540                 result = dns_name_dup(&fctx->nsname, fctx->mctx, &fctx->domain);
6541                 if (result != ISC_R_SUCCESS) {
6542                         fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
6543                         goto cleanup;
6544                 }
6545                 /*
6546                  * Try again.
6547                  */
6548                 fctx_try(fctx, ISC_TRUE, ISC_FALSE);
6549         } else {
6550                 unsigned int n;
6551                 dns_rdataset_t *nsrdataset = NULL;
6552
6553                 /*
6554                  * Retrieve state from fctx->nsfetch before we destroy it.
6555                  */
6556                 dns_fixedname_init(&fixed);
6557                 domain = dns_fixedname_name(&fixed);
6558                 dns_name_copy(&fctx->nsfetch->private->domain, domain, NULL);
6559                 if (dns_name_equal(&fctx->nsname, domain)) {
6560                         fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
6561                         dns_resolver_destroyfetch(&fctx->nsfetch);
6562                         goto cleanup;
6563                 }
6564                 if (dns_rdataset_isassociated(
6565                     &fctx->nsfetch->private->nameservers)) {
6566                         dns_rdataset_clone(
6567                             &fctx->nsfetch->private->nameservers,
6568                             &nameservers);
6569                         nsrdataset = &nameservers;
6570                 } else
6571                         domain = NULL;
6572                 dns_resolver_destroyfetch(&fctx->nsfetch);
6573                 n = dns_name_countlabels(&fctx->nsname);
6574                 dns_name_getlabelsequence(&fctx->nsname, 1, n - 1,
6575                                           &fctx->nsname);
6576
6577                 if (dns_rdataset_isassociated(fevent->rdataset))
6578                         dns_rdataset_disassociate(fevent->rdataset);
6579                 FCTXTRACE("continuing to look for parent's NS records");
6580                 result = dns_resolver_createfetch(fctx->res, &fctx->nsname,
6581                                                   dns_rdatatype_ns, domain,
6582                                                   nsrdataset, NULL, 0, task,
6583                                                   resume_dslookup, fctx,
6584                                                   &fctx->nsrrset, NULL,
6585                                                   &fctx->nsfetch);
6586                 if (result != ISC_R_SUCCESS)
6587                         fctx_done(fctx, result, __LINE__);
6588                 else {
6589                         LOCK(&res->buckets[bucketnum].lock);
6590                         locked = ISC_TRUE;
6591                         fctx->references++;
6592                 }
6593         }
6594
6595  cleanup:
6596         if (dns_rdataset_isassociated(&nameservers))
6597                 dns_rdataset_disassociate(&nameservers);
6598         if (dns_rdataset_isassociated(fevent->rdataset))
6599                 dns_rdataset_disassociate(fevent->rdataset);
6600         INSIST(fevent->sigrdataset == NULL);
6601         isc_event_free(&event);
6602         if (!locked)
6603                 LOCK(&res->buckets[bucketnum].lock);
6604         bucket_empty = fctx_decreference(fctx);
6605         UNLOCK(&res->buckets[bucketnum].lock);
6606         if (bucket_empty)
6607                 empty_bucket(res);
6608 }
6609
6610 static inline void
6611 checknamessection(dns_message_t *message, dns_section_t section) {
6612         isc_result_t result;
6613         dns_name_t *name;
6614         dns_rdata_t rdata = DNS_RDATA_INIT;
6615         dns_rdataset_t *rdataset;
6616
6617         for (result = dns_message_firstname(message, section);
6618              result == ISC_R_SUCCESS;
6619              result = dns_message_nextname(message, section))
6620         {
6621                 name = NULL;
6622                 dns_message_currentname(message, section, &name);
6623                 for (rdataset = ISC_LIST_HEAD(name->list);
6624                      rdataset != NULL;
6625                      rdataset = ISC_LIST_NEXT(rdataset, link)) {
6626                         for (result = dns_rdataset_first(rdataset);
6627                              result == ISC_R_SUCCESS;
6628                              result = dns_rdataset_next(rdataset)) {
6629                                 dns_rdataset_current(rdataset, &rdata);
6630                                 if (!dns_rdata_checkowner(name, rdata.rdclass,
6631                                                           rdata.type,
6632                                                           ISC_FALSE) ||
6633                                     !dns_rdata_checknames(&rdata, name, NULL))
6634                                 {
6635                                         rdataset->attributes |=
6636                                                 DNS_RDATASETATTR_CHECKNAMES;
6637                                 }
6638                                 dns_rdata_reset(&rdata);
6639                         }
6640                 }
6641         }
6642 }
6643
6644 static void
6645 checknames(dns_message_t *message) {
6646
6647         checknamessection(message, DNS_SECTION_ANSWER);
6648         checknamessection(message, DNS_SECTION_AUTHORITY);
6649         checknamessection(message, DNS_SECTION_ADDITIONAL);
6650 }
6651
6652 /*
6653  * Log server NSID at log level 'level'
6654  */
6655 static void
6656 log_nsid(isc_buffer_t *opt, size_t nsid_len, resquery_t *query,
6657          int level, isc_mem_t *mctx)
6658 {
6659         static const char hex[17] = "0123456789abcdef";
6660         char addrbuf[ISC_SOCKADDR_FORMATSIZE];
6661         isc_uint16_t buflen, i;
6662         unsigned char *p, *buf, *nsid;
6663
6664         /* Allocate buffer for storing hex version of the NSID */
6665         buflen = (isc_uint16_t)nsid_len * 2 + 1;
6666         buf = isc_mem_get(mctx, buflen);
6667         if (buf == NULL)
6668                 return;
6669
6670         /* Convert to hex */
6671         p = buf;
6672         nsid = isc_buffer_current(opt);
6673         for (i = 0; i < nsid_len; i++) {
6674                 *p++ = hex[(nsid[0] >> 4) & 0xf];
6675                 *p++ = hex[nsid[0] & 0xf];
6676                 nsid++;
6677         }
6678         *p = '\0';
6679
6680         isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf,
6681                             sizeof(addrbuf));
6682         isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
6683                       DNS_LOGMODULE_RESOLVER, level,
6684                       "received NSID '%s' from %s", buf, addrbuf);
6685
6686         /* Clean up */
6687         isc_mem_put(mctx, buf, buflen);
6688         return;
6689 }
6690
6691 static void
6692 log_packet(dns_message_t *message, int level, isc_mem_t *mctx) {
6693         isc_buffer_t buffer;
6694         char *buf = NULL;
6695         int len = 1024;
6696         isc_result_t result;
6697
6698         if (! isc_log_wouldlog(dns_lctx, level))
6699                 return;
6700
6701         /*
6702          * Note that these are multiline debug messages.  We want a newline
6703          * to appear in the log after each message.
6704          */
6705
6706         do {
6707                 buf = isc_mem_get(mctx, len);
6708                 if (buf == NULL)
6709                         break;
6710                 isc_buffer_init(&buffer, buf, len);
6711                 result = dns_message_totext(message, &dns_master_style_debug,
6712                                             0, &buffer);
6713                 if (result == ISC_R_NOSPACE) {
6714                         isc_mem_put(mctx, buf, len);
6715                         len += 1024;
6716                 } else if (result == ISC_R_SUCCESS)
6717                         isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
6718                                       DNS_LOGMODULE_RESOLVER, level,
6719                                       "received packet:\n%.*s",
6720                                       (int)isc_buffer_usedlength(&buffer),
6721                                       buf);
6722         } while (result == ISC_R_NOSPACE);
6723
6724         if (buf != NULL)
6725                 isc_mem_put(mctx, buf, len);
6726 }
6727
6728 static isc_boolean_t
6729 iscname(fetchctx_t *fctx) {
6730         isc_result_t result;
6731
6732         result = dns_message_findname(fctx->rmessage, DNS_SECTION_ANSWER,
6733                                       &fctx->name, dns_rdatatype_cname, 0,
6734                                       NULL, NULL);
6735         return (result == ISC_R_SUCCESS ? ISC_TRUE : ISC_FALSE);
6736 }
6737
6738 static isc_boolean_t
6739 betterreferral(fetchctx_t *fctx) {
6740         isc_result_t result;
6741         dns_name_t *name;
6742         dns_rdataset_t *rdataset;
6743         dns_message_t *message = fctx->rmessage;
6744
6745         for (result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
6746              result == ISC_R_SUCCESS;
6747              result = dns_message_nextname(message, DNS_SECTION_AUTHORITY)) {
6748                 name = NULL;
6749                 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
6750                 if (!isstrictsubdomain(name, &fctx->domain))
6751                         continue;
6752                 for (rdataset = ISC_LIST_HEAD(name->list);
6753                      rdataset != NULL;
6754                      rdataset = ISC_LIST_NEXT(rdataset, link))
6755                         if (rdataset->type == dns_rdatatype_ns)
6756                                 return (ISC_TRUE);
6757         }
6758         return (ISC_FALSE);
6759 }
6760
6761 static void
6762 process_opt(resquery_t *query, dns_rdataset_t *opt) {
6763         dns_rdata_t rdata;
6764         isc_buffer_t optbuf;
6765         isc_result_t result;
6766         isc_uint16_t optcode;
6767         isc_uint16_t optlen;
6768
6769         result = dns_rdataset_first(opt);
6770         if (result == ISC_R_SUCCESS) {
6771                 dns_rdata_init(&rdata);
6772                 dns_rdataset_current(opt, &rdata);
6773                 isc_buffer_init(&optbuf, rdata.data, rdata.length);
6774                 isc_buffer_add(&optbuf, rdata.length);
6775                 while (isc_buffer_remaininglength(&optbuf) >= 4) {
6776                         optcode = isc_buffer_getuint16(&optbuf);
6777                         optlen = isc_buffer_getuint16(&optbuf);
6778                         INSIST(optlen <= isc_buffer_remaininglength(&optbuf));
6779                         switch (optcode) {
6780                         case DNS_OPT_NSID:
6781                                 if (query->options & DNS_FETCHOPT_WANTNSID)
6782                                         log_nsid(&optbuf, optlen, query,
6783                                                  ISC_LOG_INFO,
6784                                                  query->fctx->res->mctx);
6785                                 isc_buffer_forward(&optbuf, optlen);
6786                                 break;
6787                         default:
6788                                 isc_buffer_forward(&optbuf, optlen);
6789                                 break;
6790                         }
6791                 }
6792                 INSIST(isc_buffer_remaininglength(&optbuf) == 0U);
6793         }
6794 }
6795
6796 static void
6797 resquery_response(isc_task_t *task, isc_event_t *event) {
6798         isc_result_t result = ISC_R_SUCCESS;
6799         resquery_t *query = event->ev_arg;
6800         dns_dispatchevent_t *devent = (dns_dispatchevent_t *)event;
6801         isc_boolean_t keep_trying, get_nameservers, resend;
6802         isc_boolean_t truncated;
6803         dns_message_t *message;
6804         dns_rdataset_t *opt;
6805         fetchctx_t *fctx;
6806         dns_name_t *fname;
6807         dns_fixedname_t foundname;
6808         isc_stdtime_t now;
6809         isc_time_t tnow, *finish;
6810         dns_adbaddrinfo_t *addrinfo;
6811         unsigned int options;
6812         unsigned int findoptions;
6813         isc_result_t broken_server;
6814         badnstype_t broken_type = badns_response;
6815         isc_boolean_t no_response;
6816
6817         REQUIRE(VALID_QUERY(query));
6818         fctx = query->fctx;
6819         options = query->options;
6820         REQUIRE(VALID_FCTX(fctx));
6821         REQUIRE(event->ev_type == DNS_EVENT_DISPATCH);
6822
6823         QTRACE("response");
6824
6825         if (isc_sockaddr_pf(&query->addrinfo->sockaddr) == PF_INET)
6826                 inc_stats(fctx->res, dns_resstatscounter_responsev4);
6827         else
6828                 inc_stats(fctx->res, dns_resstatscounter_responsev6);
6829
6830         (void)isc_timer_touch(fctx->timer);
6831
6832         keep_trying = ISC_FALSE;
6833         broken_server = ISC_R_SUCCESS;
6834         get_nameservers = ISC_FALSE;
6835         resend = ISC_FALSE;
6836         truncated = ISC_FALSE;
6837         finish = NULL;
6838         no_response = ISC_FALSE;
6839
6840         if (fctx->res->exiting) {
6841                 result = ISC_R_SHUTTINGDOWN;
6842                 goto done;
6843         }
6844
6845         fctx->timeouts = 0;
6846         fctx->timeout = ISC_FALSE;
6847         fctx->addrinfo = query->addrinfo;
6848
6849         /*
6850          * XXXRTH  We should really get the current time just once.  We
6851          *              need a routine to convert from an isc_time_t to an
6852          *              isc_stdtime_t.
6853          */
6854         TIME_NOW(&tnow);
6855         finish = &tnow;
6856         isc_stdtime_get(&now);
6857
6858         /*
6859          * Did the dispatcher have a problem?
6860          */
6861         if (devent->result != ISC_R_SUCCESS) {
6862                 if (devent->result == ISC_R_EOF &&
6863                     (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
6864                         /*
6865                          * The problem might be that they
6866                          * don't understand EDNS0.  Turn it
6867                          * off and try again.
6868                          */
6869                         options |= DNS_FETCHOPT_NOEDNS0;
6870                         resend = ISC_TRUE;
6871                         add_bad_edns(fctx, &query->addrinfo->sockaddr);
6872                 } else {
6873                         /*
6874                          * There's no hope for this query.
6875                          */
6876                         keep_trying = ISC_TRUE;
6877
6878                         /*
6879                          * If this is a network error on an exclusive query
6880                          * socket, mark the server as bad so that we won't try
6881                          * it for this fetch again.  Also adjust finish and
6882                          * no_response so that we penalize this address in SRTT
6883                          * adjustment later.
6884                          */
6885                         if (query->exclusivesocket &&
6886                             (devent->result == ISC_R_HOSTUNREACH ||
6887                              devent->result == ISC_R_NETUNREACH ||
6888                              devent->result == ISC_R_CONNREFUSED ||
6889                              devent->result == ISC_R_CANCELED)) {
6890                                     broken_server = devent->result;
6891                                     broken_type = badns_unreachable;
6892                                     finish = NULL;
6893                                     no_response = ISC_TRUE;
6894                         }
6895                 }
6896                 goto done;
6897         }
6898
6899         message = fctx->rmessage;
6900
6901         if (query->tsig != NULL) {
6902                 result = dns_message_setquerytsig(message, query->tsig);
6903                 if (result != ISC_R_SUCCESS)
6904                         goto done;
6905         }
6906
6907         if (query->tsigkey) {
6908                 result = dns_message_settsigkey(message, query->tsigkey);
6909                 if (result != ISC_R_SUCCESS)
6910                         goto done;
6911         }
6912
6913         result = dns_message_parse(message, &devent->buffer, 0);
6914         if (result != ISC_R_SUCCESS) {
6915                 switch (result) {
6916                 case ISC_R_UNEXPECTEDEND:
6917                         if (!message->question_ok ||
6918                             (message->flags & DNS_MESSAGEFLAG_TC) == 0 ||
6919                             (options & DNS_FETCHOPT_TCP) != 0) {
6920                                 /*
6921                                  * Either the message ended prematurely,
6922                                  * and/or wasn't marked as being truncated,
6923                                  * and/or this is a response to a query we
6924                                  * sent over TCP.  In all of these cases,
6925                                  * something is wrong with the remote
6926                                  * server and we don't want to retry using
6927                                  * TCP.
6928                                  */
6929                                 if ((query->options & DNS_FETCHOPT_NOEDNS0)
6930                                     == 0) {
6931                                         /*
6932                                          * The problem might be that they
6933                                          * don't understand EDNS0.  Turn it
6934                                          * off and try again.
6935                                          */
6936                                         options |= DNS_FETCHOPT_NOEDNS0;
6937                                         resend = ISC_TRUE;
6938                                         add_bad_edns(fctx,
6939                                                     &query->addrinfo->sockaddr);
6940                                         inc_stats(fctx->res,
6941                                                  dns_resstatscounter_edns0fail);
6942                                 } else {
6943                                         broken_server = result;
6944                                         keep_trying = ISC_TRUE;
6945                                 }
6946                                 goto done;
6947                         }
6948                         /*
6949                          * We defer retrying via TCP for a bit so we can
6950                          * check out this message further.
6951                          */
6952                         truncated = ISC_TRUE;
6953                         break;
6954                 case DNS_R_FORMERR:
6955                         if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
6956                                 /*
6957                                  * The problem might be that they
6958                                  * don't understand EDNS0.  Turn it
6959                                  * off and try again.
6960                                  */
6961                                 options |= DNS_FETCHOPT_NOEDNS0;
6962                                 resend = ISC_TRUE;
6963                                 add_bad_edns(fctx, &query->addrinfo->sockaddr);
6964                                 inc_stats(fctx->res,
6965                                                  dns_resstatscounter_edns0fail);
6966                         } else {
6967                                 broken_server = DNS_R_UNEXPECTEDRCODE;
6968                                 keep_trying = ISC_TRUE;
6969                         }
6970                         goto done;
6971                 default:
6972                         /*
6973                          * Something bad has happened.
6974                          */
6975                         goto done;
6976                 }
6977         }
6978
6979
6980         /*
6981          * Log the incoming packet.
6982          */
6983         log_packet(message, ISC_LOG_DEBUG(10), fctx->res->mctx);
6984
6985         /*
6986          * Process receive opt record.
6987          */
6988         opt = dns_message_getopt(message);
6989         if (opt != NULL)
6990                 process_opt(query, opt);
6991
6992         /*
6993          * If the message is signed, check the signature.  If not, this
6994          * returns success anyway.
6995          */
6996         result = dns_message_checksig(message, fctx->res->view);
6997         if (result != ISC_R_SUCCESS)
6998                 goto done;
6999
7000         /*
7001          * The dispatcher should ensure we only get responses with QR set.
7002          */
7003         INSIST((message->flags & DNS_MESSAGEFLAG_QR) != 0);
7004         /*
7005          * INSIST() that the message comes from the place we sent it to,
7006          * since the dispatch code should ensure this.
7007          *
7008          * INSIST() that the message id is correct (this should also be
7009          * ensured by the dispatch code).
7010          */
7011
7012         /*
7013          * We have an affirmative response to the query and we have
7014          * previously got a response from this server which indicated
7015          * EDNS may not be supported so we can now cache the lack of
7016          * EDNS support.
7017          */
7018         if (opt == NULL &&
7019             (message->rcode == dns_rcode_noerror ||
7020              message->rcode == dns_rcode_nxdomain ||
7021              message->rcode == dns_rcode_refused ||
7022              message->rcode == dns_rcode_yxdomain) &&
7023              bad_edns(fctx, &query->addrinfo->sockaddr)) {
7024                 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
7025                 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf,
7026                                     sizeof(addrbuf));
7027                 dns_adb_changeflags(fctx->adb, query->addrinfo,
7028                                     DNS_FETCHOPT_NOEDNS0,
7029                                     DNS_FETCHOPT_NOEDNS0);
7030         }
7031
7032         /*
7033          * Deal with truncated responses by retrying using TCP.
7034          */
7035         if ((message->flags & DNS_MESSAGEFLAG_TC) != 0)
7036                 truncated = ISC_TRUE;
7037
7038         if (truncated) {
7039                 inc_stats(fctx->res, dns_resstatscounter_truncated);
7040                 if ((options & DNS_FETCHOPT_TCP) != 0) {
7041                         broken_server = DNS_R_TRUNCATEDTCP;
7042                         keep_trying = ISC_TRUE;
7043                 } else {
7044                         options |= DNS_FETCHOPT_TCP;
7045                         resend = ISC_TRUE;
7046                 }
7047                 goto done;
7048         }
7049
7050         /*
7051          * Is it a query response?
7052          */
7053         if (message->opcode != dns_opcode_query) {
7054                 /* XXXRTH Log */
7055                 broken_server = DNS_R_UNEXPECTEDOPCODE;
7056                 keep_trying = ISC_TRUE;
7057                 goto done;
7058         }
7059
7060         /*
7061          * Update statistics about erroneous responses.
7062          */
7063         if (message->rcode != dns_rcode_noerror) {
7064                 switch (message->rcode) {
7065                 case dns_rcode_nxdomain:
7066                         inc_stats(fctx->res, dns_resstatscounter_nxdomain);
7067                         break;
7068                 case dns_rcode_servfail:
7069                         inc_stats(fctx->res, dns_resstatscounter_servfail);
7070                         break;
7071                 case dns_rcode_formerr:
7072                         inc_stats(fctx->res, dns_resstatscounter_formerr);
7073                         break;
7074                 default:
7075                         inc_stats(fctx->res, dns_resstatscounter_othererror);
7076                         break;
7077                 }
7078         }
7079
7080         /*
7081          * Is the remote server broken, or does it dislike us?
7082          */
7083         if (message->rcode != dns_rcode_noerror &&
7084             message->rcode != dns_rcode_nxdomain) {
7085                 if (((message->rcode == dns_rcode_formerr ||
7086                       message->rcode == dns_rcode_notimp) ||
7087                      (message->rcode == dns_rcode_servfail &&
7088                       dns_message_getopt(message) == NULL)) &&
7089                     (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
7090                         /*
7091                          * It's very likely they don't like EDNS0.
7092                          * If the response code is SERVFAIL, also check if the
7093                          * response contains an OPT RR and don't cache the
7094                          * failure since it can be returned for various other
7095                          * reasons.
7096                          *
7097                          * XXXRTH  We should check if the question
7098                          *              we're asking requires EDNS0, and
7099                          *              if so, we should bail out.
7100                          */
7101                         options |= DNS_FETCHOPT_NOEDNS0;
7102                         resend = ISC_TRUE;
7103                         /*
7104                          * Remember that they may not like EDNS0.
7105                          */
7106                         add_bad_edns(fctx, &query->addrinfo->sockaddr);
7107                         inc_stats(fctx->res, dns_resstatscounter_edns0fail);
7108                 } else if (message->rcode == dns_rcode_formerr) {
7109                         if (ISFORWARDER(query->addrinfo)) {
7110                                 /*
7111                                  * This forwarder doesn't understand us,
7112                                  * but other forwarders might.  Keep trying.
7113                                  */
7114                                 broken_server = DNS_R_REMOTEFORMERR;
7115                                 keep_trying = ISC_TRUE;
7116                         } else {
7117                                 /*
7118                                  * The server doesn't understand us.  Since
7119                                  * all servers for a zone need similar
7120                                  * capabilities, we assume that we will get
7121                                  * FORMERR from all servers, and thus we
7122                                  * cannot make any more progress with this
7123                                  * fetch.
7124                                  */
7125                                 log_formerr(fctx, "server sent FORMERR");
7126                                 result = DNS_R_FORMERR;
7127                         }
7128                 } else if (message->rcode == dns_rcode_yxdomain) {
7129                         /*
7130                          * DNAME mapping failed because the new name
7131                          * was too long.  There's no chance of success
7132                          * for this fetch.
7133                          */
7134                         result = DNS_R_YXDOMAIN;
7135                 } else if (message->rcode == dns_rcode_badvers) {
7136                         unsigned int flags, mask;
7137                         unsigned int version;
7138
7139                         resend = ISC_TRUE;
7140                         INSIST(opt != NULL);
7141                         version = (opt->ttl >> 16) & 0xff;
7142                         flags = (version << DNS_FETCHOPT_EDNSVERSIONSHIFT) |
7143                                 DNS_FETCHOPT_EDNSVERSIONSET;
7144                         mask = DNS_FETCHOPT_EDNSVERSIONMASK |
7145                                DNS_FETCHOPT_EDNSVERSIONSET;
7146                         switch (version) {
7147                         case 0:
7148                                 dns_adb_changeflags(fctx->adb, query->addrinfo,
7149                                                     flags, mask);
7150                                 break;
7151                         default:
7152                                 broken_server = DNS_R_BADVERS;
7153                                 keep_trying = ISC_TRUE;
7154                                 break;
7155                         }
7156                 } else {
7157                         /*
7158                          * XXXRTH log.
7159                          */
7160                         broken_server = DNS_R_UNEXPECTEDRCODE;
7161                         INSIST(broken_server != ISC_R_SUCCESS);
7162                         keep_trying = ISC_TRUE;
7163                 }
7164                 goto done;
7165         }
7166
7167         /*
7168          * Is the question the same as the one we asked?
7169          */
7170         result = same_question(fctx);
7171         if (result != ISC_R_SUCCESS) {
7172                 /* XXXRTH Log */
7173                 if (result == DNS_R_FORMERR)
7174                         keep_trying = ISC_TRUE;
7175                 goto done;
7176         }
7177
7178         /*
7179          * Is the server lame?
7180          */
7181         if (fctx->res->lame_ttl != 0 && !ISFORWARDER(query->addrinfo) &&
7182             is_lame(fctx)) {
7183                 inc_stats(fctx->res, dns_resstatscounter_lame);
7184                 log_lame(fctx, query->addrinfo);
7185                 result = dns_adb_marklame(fctx->adb, query->addrinfo,
7186                                           &fctx->name, fctx->type,
7187                                           now + fctx->res->lame_ttl);
7188                 if (result != ISC_R_SUCCESS)
7189                         isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
7190                                       DNS_LOGMODULE_RESOLVER, ISC_LOG_ERROR,
7191                                       "could not mark server as lame: %s",
7192                                       isc_result_totext(result));
7193                 broken_server = DNS_R_LAME;
7194                 keep_trying = ISC_TRUE;
7195                 goto done;
7196         }
7197
7198         /*
7199          * Enforce delegations only zones like NET and COM.
7200          */
7201         if (!ISFORWARDER(query->addrinfo) &&
7202             dns_view_isdelegationonly(fctx->res->view, &fctx->domain) &&
7203             !dns_name_equal(&fctx->domain, &fctx->name) &&
7204             fix_mustbedelegationornxdomain(message, fctx)) {
7205                 char namebuf[DNS_NAME_FORMATSIZE];
7206                 char domainbuf[DNS_NAME_FORMATSIZE];
7207                 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
7208                 char classbuf[64];
7209                 char typebuf[64];
7210
7211                 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
7212                 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
7213                 dns_rdatatype_format(fctx->type, typebuf, sizeof(typebuf));
7214                 dns_rdataclass_format(fctx->res->rdclass, classbuf,
7215                                       sizeof(classbuf));
7216                 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf,
7217                                     sizeof(addrbuf));
7218
7219                 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DELEGATION_ONLY,
7220                              DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
7221                              "enforced delegation-only for '%s' (%s/%s/%s) "
7222                              "from %s",
7223                              domainbuf, namebuf, typebuf, classbuf, addrbuf);
7224         }
7225
7226         if ((fctx->res->options & DNS_RESOLVER_CHECKNAMES) != 0)
7227                 checknames(message);
7228
7229         /*
7230          * Clear cache bits.
7231          */
7232         fctx->attributes &= ~(FCTX_ATTR_WANTNCACHE | FCTX_ATTR_WANTCACHE);
7233
7234         /*
7235          * Did we get any answers?
7236          */
7237         if (message->counts[DNS_SECTION_ANSWER] > 0 &&
7238             (message->rcode == dns_rcode_noerror ||
7239              message->rcode == dns_rcode_nxdomain)) {
7240                 /*
7241                  * [normal case]
7242                  * We've got answers.  If it has an authoritative answer or an
7243                  * answer from a forwarder, we're done.
7244                  */
7245                 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0 ||
7246                     ISFORWARDER(query->addrinfo))
7247                         result = answer_response(fctx);
7248                 else if (iscname(fctx) &&
7249                          fctx->type != dns_rdatatype_any &&
7250                          fctx->type != dns_rdatatype_cname) {
7251                         /*
7252                          * A BIND8 server could return a non-authoritative
7253                          * answer when a CNAME is followed.  We should treat
7254                          * it as a valid answer.
7255                          */
7256                         result = answer_response(fctx);
7257                 } else if (fctx->type != dns_rdatatype_ns &&
7258                            !betterreferral(fctx)) {
7259                         /*
7260                          * Lame response !!!.
7261                          */
7262                         result = answer_response(fctx);
7263                 } else {
7264                         if (fctx->type == dns_rdatatype_ns) {
7265                                 /*
7266                                  * A BIND 8 server could incorrectly return a
7267                                  * non-authoritative answer to an NS query
7268                                  * instead of a referral. Since this answer
7269                                  * lacks the SIGs necessary to do DNSSEC
7270                                  * validation, we must invoke the following
7271                                  * special kludge to treat it as a referral.
7272                                  */
7273                                 result = noanswer_response(fctx, NULL,
7274                                                    LOOK_FOR_NS_IN_ANSWER);
7275                         } else {
7276                                 /*
7277                                  * Some other servers may still somehow include
7278                                  * an answer when it should return a referral
7279                                  * with an empty answer.  Check to see if we can
7280                                  * treat this as a referral by ignoring the
7281                                  * answer.  Further more, there may be an
7282                                  * implementation that moves A/AAAA glue records
7283                                  * to the answer section for that type of
7284                                  * delegation when the query is for that glue
7285                                  * record.  LOOK_FOR_GLUE_IN_ANSWER will handle
7286                                  * such a corner case.
7287                                  */
7288                                 result = noanswer_response(fctx, NULL,
7289                                                    LOOK_FOR_GLUE_IN_ANSWER);
7290                         }
7291                         if (result != DNS_R_DELEGATION) {
7292                                 /*
7293                                  * At this point, AA is not set, the response
7294                                  * is not a referral, and the server is not a
7295                                  * forwarder.  It is technically lame and it's
7296                                  * easier to treat it as such than to figure out
7297                                  * some more elaborate course of action.
7298                                  */
7299                                 broken_server = DNS_R_LAME;
7300                                 keep_trying = ISC_TRUE;
7301                                 goto done;
7302                         }
7303                         goto force_referral;
7304                 }
7305                 if (result != ISC_R_SUCCESS) {
7306                         if (result == DNS_R_FORMERR)
7307                                 keep_trying = ISC_TRUE;
7308                         goto done;
7309                 }
7310         } else if (message->counts[DNS_SECTION_AUTHORITY] > 0 ||
7311                    message->rcode == dns_rcode_noerror ||
7312                    message->rcode == dns_rcode_nxdomain) {
7313                 /*
7314                  * NXDOMAIN, NXRDATASET, or referral.
7315                  */
7316                 result = noanswer_response(fctx, NULL, 0);
7317                 switch (result) {
7318                 case ISC_R_SUCCESS:
7319                 case DNS_R_CHASEDSSERVERS:
7320                         break;
7321                 case DNS_R_DELEGATION:
7322  force_referral:
7323                         /*
7324                          * We don't have the answer, but we know a better
7325                          * place to look.
7326                          */
7327                         get_nameservers = ISC_TRUE;
7328                         keep_trying = ISC_TRUE;
7329                         /*
7330                          * We have a new set of name servers, and it
7331                          * has not experienced any restarts yet.
7332                          */
7333                         fctx->restarts = 0;
7334
7335                         /*
7336                          * Update local statistics counters collected for each
7337                          * new zone.
7338                          */
7339                         fctx->referrals++;
7340                         fctx->querysent = 0;
7341                         fctx->lamecount = 0;
7342                         fctx->neterr = 0;
7343                         fctx->badresp = 0;
7344                         fctx->adberr = 0;
7345
7346                         result = ISC_R_SUCCESS;
7347                         break;
7348                 default:
7349                         /*
7350                          * Something has gone wrong.
7351                          */
7352                         if (result == DNS_R_FORMERR)
7353                                 keep_trying = ISC_TRUE;
7354                         goto done;
7355                 }
7356         } else {
7357                 /*
7358                  * The server is insane.
7359                  */
7360                 /* XXXRTH Log */
7361                 broken_server = DNS_R_UNEXPECTEDRCODE;
7362                 keep_trying = ISC_TRUE;
7363                 goto done;
7364         }
7365
7366         /*
7367          * Follow additional section data chains.
7368          */
7369         chase_additional(fctx);
7370
7371         /*
7372          * Cache the cacheable parts of the message.  This may also cause
7373          * work to be queued to the DNSSEC validator.
7374          */
7375         if (WANTCACHE(fctx)) {
7376                 result = cache_message(fctx, query->addrinfo, now);
7377                 if (result != ISC_R_SUCCESS)
7378                         goto done;
7379         }
7380
7381         /*
7382          * Ncache the negatively cacheable parts of the message.  This may
7383          * also cause work to be queued to the DNSSEC validator.
7384          */
7385         if (WANTNCACHE(fctx)) {
7386                 dns_rdatatype_t covers;
7387                 if (message->rcode == dns_rcode_nxdomain)
7388                         covers = dns_rdatatype_any;
7389                 else
7390                         covers = fctx->type;
7391
7392                 /*
7393                  * Cache any negative cache entries in the message.
7394                  */
7395                 result = ncache_message(fctx, query->addrinfo, covers, now);
7396         }
7397
7398  done:
7399         /*
7400          * Remember the query's addrinfo, in case we need to mark the
7401          * server as broken.
7402          */
7403         addrinfo = query->addrinfo;
7404
7405         /*
7406          * Cancel the query.
7407          *
7408          * XXXRTH  Don't cancel the query if waiting for validation?
7409          */
7410         fctx_cancelquery(&query, &devent, finish, no_response);
7411
7412         if (keep_trying) {
7413                 if (result == DNS_R_FORMERR)
7414                         broken_server = DNS_R_FORMERR;
7415                 if (broken_server != ISC_R_SUCCESS) {
7416                         /*
7417                          * Add this server to the list of bad servers for
7418                          * this fctx.
7419                          */
7420                         add_bad(fctx, addrinfo, broken_server, broken_type);
7421                 }
7422
7423                 if (get_nameservers) {
7424                         dns_name_t *name;
7425                         dns_fixedname_init(&foundname);
7426                         fname = dns_fixedname_name(&foundname);
7427                         if (result != ISC_R_SUCCESS) {
7428                                 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7429                                 return;
7430                         }
7431                         findoptions = 0;
7432                         if (dns_rdatatype_atparent(fctx->type))
7433                                 findoptions |= DNS_DBFIND_NOEXACT;
7434                         if ((options & DNS_FETCHOPT_UNSHARED) == 0)
7435                                 name = &fctx->name;
7436                         else
7437                                 name = &fctx->domain;
7438                         result = dns_view_findzonecut(fctx->res->view,
7439                                                       name, fname,
7440                                                       now, findoptions,
7441                                                       ISC_TRUE,
7442                                                       &fctx->nameservers,
7443                                                       NULL);
7444                         if (result != ISC_R_SUCCESS) {
7445                                 FCTXTRACE("couldn't find a zonecut");
7446                                 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7447                                 return;
7448                         }
7449                         if (!dns_name_issubdomain(fname, &fctx->domain)) {
7450                                 /*
7451                                  * The best nameservers are now above our
7452                                  * QDOMAIN.
7453                                  */
7454                                 FCTXTRACE("nameservers now above QDOMAIN");
7455                                 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7456                                 return;
7457                         }
7458                         dns_name_free(&fctx->domain, fctx->mctx);
7459                         dns_name_init(&fctx->domain, NULL);
7460                         result = dns_name_dup(fname, fctx->mctx, &fctx->domain);
7461                         if (result != ISC_R_SUCCESS) {
7462                                 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
7463                                 return;
7464                         }
7465                         fctx->ns_ttl = fctx->nameservers.ttl;
7466                         fctx->ns_ttl_ok = ISC_TRUE;
7467                         fctx_cancelqueries(fctx, ISC_TRUE);
7468                         fctx_cleanupfinds(fctx);
7469                         fctx_cleanupaltfinds(fctx);
7470                         fctx_cleanupforwaddrs(fctx);
7471                         fctx_cleanupaltaddrs(fctx);
7472                 }
7473                 /*
7474                  * Try again.
7475                  */
7476                 fctx_try(fctx, !get_nameservers, ISC_FALSE);
7477         } else if (resend) {
7478                 /*
7479                  * Resend (probably with changed options).
7480                  */
7481                 FCTXTRACE("resend");
7482                 inc_stats(fctx->res, dns_resstatscounter_retry);
7483                 result = fctx_query(fctx, addrinfo, options);
7484                 if (result != ISC_R_SUCCESS)
7485                         fctx_done(fctx, result, __LINE__);
7486         } else if (result == ISC_R_SUCCESS && !HAVE_ANSWER(fctx)) {
7487                 /*
7488                  * All has gone well so far, but we are waiting for the
7489                  * DNSSEC validator to validate the answer.
7490                  */
7491                 FCTXTRACE("wait for validator");
7492                 fctx_cancelqueries(fctx, ISC_TRUE);
7493                 /*
7494                  * We must not retransmit while the validator is working;
7495                  * it has references to the current rmessage.
7496                  */
7497                 result = fctx_stopidletimer(fctx);
7498                 if (result != ISC_R_SUCCESS)
7499                         fctx_done(fctx, result, __LINE__);
7500         } else if (result == DNS_R_CHASEDSSERVERS) {
7501                 unsigned int n;
7502                 add_bad(fctx, addrinfo, result, broken_type);
7503                 fctx_cancelqueries(fctx, ISC_TRUE);
7504                 fctx_cleanupfinds(fctx);
7505                 fctx_cleanupforwaddrs(fctx);
7506
7507                 n = dns_name_countlabels(&fctx->name);
7508                 dns_name_getlabelsequence(&fctx->name, 1, n - 1, &fctx->nsname);
7509
7510                 FCTXTRACE("suspending DS lookup to find parent's NS records");
7511
7512                 result = dns_resolver_createfetch(fctx->res, &fctx->nsname,
7513                                                   dns_rdatatype_ns,
7514                                                   NULL, NULL, NULL, 0, task,
7515                                                   resume_dslookup, fctx,
7516                                                   &fctx->nsrrset, NULL,
7517                                                   &fctx->nsfetch);
7518                 if (result != ISC_R_SUCCESS)
7519                         fctx_done(fctx, result, __LINE__);
7520                 else {
7521                         LOCK(&fctx->res->buckets[fctx->bucketnum].lock);
7522                         fctx->references++;
7523                         UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
7524                         result = fctx_stopidletimer(fctx);
7525                         if (result != ISC_R_SUCCESS)
7526                                 fctx_done(fctx, result, __LINE__);
7527                 }
7528         } else {
7529                 /*
7530                  * We're done.
7531                  */
7532                 fctx_done(fctx, result, __LINE__);
7533         }
7534 }
7535
7536
7537 /***
7538  *** Resolver Methods
7539  ***/
7540 static void
7541 destroy_badcache(dns_resolver_t *res) {
7542         dns_badcache_t *bad, *next;
7543         unsigned int i;
7544
7545         if (res->badcache != NULL) {
7546                 for (i = 0; i < res->badhash; i++)
7547                         for (bad = res->badcache[i]; bad != NULL;
7548                              bad = next) {
7549                                 next = bad->next;
7550                                 isc_mem_put(res->mctx, bad, sizeof(*bad) +
7551                                             bad->name.length);
7552                                 res->badcount--;
7553                         }
7554                 isc_mem_put(res->mctx, res->badcache,
7555                             sizeof(*res->badcache) * res->badhash);
7556                 res->badcache = NULL;
7557                 res->badhash = 0;
7558                 INSIST(res->badcount == 0);
7559         }
7560 }
7561
7562 static void
7563 destroy(dns_resolver_t *res) {
7564         unsigned int i;
7565         alternate_t *a;
7566
7567         REQUIRE(res->references == 0);
7568         REQUIRE(!res->priming);
7569         REQUIRE(res->primefetch == NULL);
7570
7571         RTRACE("destroy");
7572
7573         INSIST(res->nfctx == 0);
7574
7575         DESTROYLOCK(&res->primelock);
7576         DESTROYLOCK(&res->nlock);
7577         DESTROYLOCK(&res->lock);
7578         for (i = 0; i < res->nbuckets; i++) {
7579                 INSIST(ISC_LIST_EMPTY(res->buckets[i].fctxs));
7580                 isc_task_shutdown(res->buckets[i].task);
7581                 isc_task_detach(&res->buckets[i].task);
7582                 DESTROYLOCK(&res->buckets[i].lock);
7583                 isc_mem_detach(&res->buckets[i].mctx);
7584         }
7585         isc_mem_put(res->mctx, res->buckets,
7586                     res->nbuckets * sizeof(fctxbucket_t));
7587         if (res->dispatches4 != NULL)
7588                 dns_dispatchset_destroy(&res->dispatches4);
7589         if (res->dispatches6 != NULL)
7590                 dns_dispatchset_destroy(&res->dispatches6);
7591         while ((a = ISC_LIST_HEAD(res->alternates)) != NULL) {
7592                 ISC_LIST_UNLINK(res->alternates, a, link);
7593                 if (!a->isaddress)
7594                         dns_name_free(&a->_u._n.name, res->mctx);
7595                 isc_mem_put(res->mctx, a, sizeof(*a));
7596         }
7597         dns_resolver_reset_algorithms(res);
7598         destroy_badcache(res);
7599         dns_resolver_resetmustbesecure(res);
7600 #if USE_ALGLOCK
7601         isc_rwlock_destroy(&res->alglock);
7602 #endif
7603 #if USE_MBSLOCK
7604         isc_rwlock_destroy(&res->mbslock);
7605 #endif
7606         isc_timer_detach(&res->spillattimer);
7607         res->magic = 0;
7608         isc_mem_put(res->mctx, res, sizeof(*res));
7609 }
7610
7611 static void
7612 send_shutdown_events(dns_resolver_t *res) {
7613         isc_event_t *event, *next_event;
7614         isc_task_t *etask;
7615
7616         /*
7617          * Caller must be holding the resolver lock.
7618          */
7619
7620         for (event = ISC_LIST_HEAD(res->whenshutdown);
7621              event != NULL;
7622              event = next_event) {
7623                 next_event = ISC_LIST_NEXT(event, ev_link);
7624                 ISC_LIST_UNLINK(res->whenshutdown, event, ev_link);
7625                 etask = event->ev_sender;
7626                 event->ev_sender = res;
7627                 isc_task_sendanddetach(&etask, &event);
7628         }
7629 }
7630
7631 static void
7632 empty_bucket(dns_resolver_t *res) {
7633         RTRACE("empty_bucket");
7634
7635         LOCK(&res->lock);
7636
7637         INSIST(res->activebuckets > 0);
7638         res->activebuckets--;
7639         if (res->activebuckets == 0)
7640                 send_shutdown_events(res);
7641
7642         UNLOCK(&res->lock);
7643 }
7644
7645 static void
7646 spillattimer_countdown(isc_task_t *task, isc_event_t *event) {
7647         dns_resolver_t *res = event->ev_arg;
7648         isc_result_t result;
7649         unsigned int count;
7650         isc_boolean_t logit = ISC_FALSE;
7651
7652         REQUIRE(VALID_RESOLVER(res));
7653
7654         UNUSED(task);
7655
7656         LOCK(&res->lock);
7657         INSIST(!res->exiting);
7658         if (res->spillat > res->spillatmin) {
7659                 res->spillat--;
7660                 logit = ISC_TRUE;
7661         }
7662         if (res->spillat <= res->spillatmin) {
7663                 result = isc_timer_reset(res->spillattimer,
7664                                          isc_timertype_inactive, NULL,
7665                                          NULL, ISC_TRUE);
7666                 RUNTIME_CHECK(result == ISC_R_SUCCESS);
7667         }
7668         count = res->spillat;
7669         UNLOCK(&res->lock);
7670         if (logit)
7671                 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
7672                               DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
7673                               "clients-per-query decreased to %u", count);
7674
7675         isc_event_free(&event);
7676 }
7677
7678 isc_result_t
7679 dns_resolver_create(dns_view_t *view,
7680                     isc_taskmgr_t *taskmgr,
7681                     unsigned int ntasks, unsigned int ndisp,
7682                     isc_socketmgr_t *socketmgr,
7683                     isc_timermgr_t *timermgr,
7684                     unsigned int options,
7685                     dns_dispatchmgr_t *dispatchmgr,
7686                     dns_dispatch_t *dispatchv4,
7687                     dns_dispatch_t *dispatchv6,
7688                     dns_resolver_t **resp)
7689 {
7690         dns_resolver_t *res;
7691         isc_result_t result = ISC_R_SUCCESS;
7692         unsigned int i, buckets_created = 0;
7693         isc_task_t *task = NULL;
7694         char name[16];
7695         unsigned dispattr;
7696
7697         /*
7698          * Create a resolver.
7699          */
7700
7701         REQUIRE(DNS_VIEW_VALID(view));
7702         REQUIRE(ntasks > 0);
7703         REQUIRE(ndisp > 0);
7704         REQUIRE(resp != NULL && *resp == NULL);
7705         REQUIRE(dispatchmgr != NULL);
7706         REQUIRE(dispatchv4 != NULL || dispatchv6 != NULL);
7707
7708         res = isc_mem_get(view->mctx, sizeof(*res));
7709         if (res == NULL)
7710                 return (ISC_R_NOMEMORY);
7711         RTRACE("create");
7712         res->mctx = view->mctx;
7713         res->rdclass = view->rdclass;
7714         res->socketmgr = socketmgr;
7715         res->timermgr = timermgr;
7716         res->taskmgr = taskmgr;
7717         res->dispatchmgr = dispatchmgr;
7718         res->view = view;
7719         res->options = options;
7720         res->lame_ttl = 0;
7721         ISC_LIST_INIT(res->alternates);
7722         res->udpsize = RECV_BUFFER_SIZE;
7723         res->algorithms = NULL;
7724         res->badcache = NULL;
7725         res->badcount = 0;
7726         res->badhash = 0;
7727         res->badsweep = 0;
7728         res->mustbesecure = NULL;
7729         res->spillatmin = res->spillat = 10;
7730         res->spillatmax = 100;
7731         res->spillattimer = NULL;
7732         res->zero_no_soa_ttl = ISC_FALSE;
7733         res->query_timeout = DEFAULT_QUERY_TIMEOUT;
7734         res->nbuckets = ntasks;
7735         res->activebuckets = ntasks;
7736         res->buckets = isc_mem_get(view->mctx,
7737                                    ntasks * sizeof(fctxbucket_t));
7738         if (res->buckets == NULL) {
7739                 result = ISC_R_NOMEMORY;
7740                 goto cleanup_res;
7741         }
7742         for (i = 0; i < ntasks; i++) {
7743                 result = isc_mutex_init(&res->buckets[i].lock);
7744                 if (result != ISC_R_SUCCESS)
7745                         goto cleanup_buckets;
7746                 res->buckets[i].task = NULL;
7747                 result = isc_task_create(taskmgr, 0, &res->buckets[i].task);
7748                 if (result != ISC_R_SUCCESS) {
7749                         DESTROYLOCK(&res->buckets[i].lock);
7750                         goto cleanup_buckets;
7751                 }
7752                 res->buckets[i].mctx = NULL;
7753                 snprintf(name, sizeof(name), "res%u", i);
7754 #ifdef ISC_PLATFORM_USETHREADS
7755                 /*
7756                  * Use a separate memory context for each bucket to reduce
7757                  * contention among multiple threads.  Do this only when
7758                  * enabling threads because it will be require more memory.
7759                  */
7760                 result = isc_mem_create(0, 0, &res->buckets[i].mctx);
7761                 if (result != ISC_R_SUCCESS) {
7762                         isc_task_detach(&res->buckets[i].task);
7763                         DESTROYLOCK(&res->buckets[i].lock);
7764                         goto cleanup_buckets;
7765                 }
7766                 isc_mem_setname(res->buckets[i].mctx, name, NULL);
7767 #else
7768                 isc_mem_attach(view->mctx, &res->buckets[i].mctx);
7769 #endif
7770                 isc_task_setname(res->buckets[i].task, name, res);
7771                 ISC_LIST_INIT(res->buckets[i].fctxs);
7772                 res->buckets[i].exiting = ISC_FALSE;
7773                 buckets_created++;
7774         }
7775
7776         res->dispatches4 = NULL;
7777         if (dispatchv4 != NULL) {
7778                 dns_dispatchset_create(view->mctx, socketmgr, taskmgr,
7779                                        dispatchv4, &res->dispatches4, ndisp);
7780                 dispattr = dns_dispatch_getattributes(dispatchv4);
7781                 res->exclusivev4 =
7782                         ISC_TF((dispattr & DNS_DISPATCHATTR_EXCLUSIVE) != 0);
7783         }
7784
7785         res->dispatches6 = NULL;
7786         if (dispatchv6 != NULL) {
7787                 dns_dispatchset_create(view->mctx, socketmgr, taskmgr,
7788                                        dispatchv6, &res->dispatches6, ndisp);
7789                 dispattr = dns_dispatch_getattributes(dispatchv6);
7790                 res->exclusivev6 =
7791                         ISC_TF((dispattr & DNS_DISPATCHATTR_EXCLUSIVE) != 0);
7792         }
7793
7794         res->references = 1;
7795         res->exiting = ISC_FALSE;
7796         res->frozen = ISC_FALSE;
7797         ISC_LIST_INIT(res->whenshutdown);
7798         res->priming = ISC_FALSE;
7799         res->primefetch = NULL;
7800         res->nfctx = 0;
7801
7802         result = isc_mutex_init(&res->lock);
7803         if (result != ISC_R_SUCCESS)
7804                 goto cleanup_dispatches;
7805
7806         result = isc_mutex_init(&res->nlock);
7807         if (result != ISC_R_SUCCESS)
7808                 goto cleanup_lock;
7809
7810         result = isc_mutex_init(&res->primelock);
7811         if (result != ISC_R_SUCCESS)
7812                 goto cleanup_nlock;
7813
7814         task = NULL;
7815         result = isc_task_create(taskmgr, 0, &task);
7816         if (result != ISC_R_SUCCESS)
7817                 goto cleanup_primelock;
7818
7819         result = isc_timer_create(timermgr, isc_timertype_inactive, NULL, NULL,
7820                                   task, spillattimer_countdown, res,
7821                                   &res->spillattimer);
7822         isc_task_detach(&task);
7823         if (result != ISC_R_SUCCESS)
7824                 goto cleanup_primelock;
7825
7826 #if USE_ALGLOCK
7827         result = isc_rwlock_init(&res->alglock, 0, 0);
7828         if (result != ISC_R_SUCCESS)
7829                 goto cleanup_spillattimer;
7830 #endif
7831 #if USE_MBSLOCK
7832         result = isc_rwlock_init(&res->mbslock, 0, 0);
7833         if (result != ISC_R_SUCCESS)
7834                 goto cleanup_alglock;
7835 #endif
7836
7837         res->magic = RES_MAGIC;
7838
7839         *resp = res;
7840
7841         return (ISC_R_SUCCESS);
7842
7843 #if USE_MBSLOCK
7844  cleanup_alglock:
7845 #if USE_ALGLOCK
7846         isc_rwlock_destroy(&res->alglock);
7847 #endif
7848 #endif
7849 #if USE_ALGLOCK || USE_MBSLOCK
7850  cleanup_spillattimer:
7851         isc_timer_detach(&res->spillattimer);
7852 #endif
7853
7854  cleanup_primelock:
7855         DESTROYLOCK(&res->primelock);
7856
7857  cleanup_nlock:
7858         DESTROYLOCK(&res->nlock);
7859
7860  cleanup_lock:
7861         DESTROYLOCK(&res->lock);
7862
7863  cleanup_dispatches:
7864         if (res->dispatches6 != NULL)
7865                 dns_dispatchset_destroy(&res->dispatches6);
7866         if (res->dispatches4 != NULL)
7867                 dns_dispatchset_destroy(&res->dispatches4);
7868
7869  cleanup_buckets:
7870         for (i = 0; i < buckets_created; i++) {
7871                 isc_mem_detach(&res->buckets[i].mctx);
7872                 DESTROYLOCK(&res->buckets[i].lock);
7873                 isc_task_shutdown(res->buckets[i].task);
7874                 isc_task_detach(&res->buckets[i].task);
7875         }
7876         isc_mem_put(view->mctx, res->buckets,
7877                     res->nbuckets * sizeof(fctxbucket_t));
7878
7879  cleanup_res:
7880         isc_mem_put(view->mctx, res, sizeof(*res));
7881
7882         return (result);
7883 }
7884
7885 #ifdef BIND9
7886 static void
7887 prime_done(isc_task_t *task, isc_event_t *event) {
7888         dns_resolver_t *res;
7889         dns_fetchevent_t *fevent;
7890         dns_fetch_t *fetch;
7891         dns_db_t *db = NULL;
7892
7893         REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
7894         fevent = (dns_fetchevent_t *)event;
7895         res = event->ev_arg;
7896         REQUIRE(VALID_RESOLVER(res));
7897
7898         UNUSED(task);
7899
7900         LOCK(&res->lock);
7901
7902         INSIST(res->priming);
7903         res->priming = ISC_FALSE;
7904         LOCK(&res->primelock);
7905         fetch = res->primefetch;
7906         res->primefetch = NULL;
7907         UNLOCK(&res->primelock);
7908
7909         UNLOCK(&res->lock);
7910
7911         if (fevent->result == ISC_R_SUCCESS &&
7912             res->view->cache != NULL && res->view->hints != NULL) {
7913                 dns_cache_attachdb(res->view->cache, &db);
7914                 dns_root_checkhints(res->view, res->view->hints, db);
7915                 dns_db_detach(&db);
7916         }
7917
7918         if (fevent->node != NULL)
7919                 dns_db_detachnode(fevent->db, &fevent->node);
7920         if (fevent->db != NULL)
7921                 dns_db_detach(&fevent->db);
7922         if (dns_rdataset_isassociated(fevent->rdataset))
7923                 dns_rdataset_disassociate(fevent->rdataset);
7924         INSIST(fevent->sigrdataset == NULL);
7925
7926         isc_mem_put(res->mctx, fevent->rdataset, sizeof(*fevent->rdataset));
7927
7928         isc_event_free(&event);
7929         dns_resolver_destroyfetch(&fetch);
7930 }
7931
7932 void
7933 dns_resolver_prime(dns_resolver_t *res) {
7934         isc_boolean_t want_priming = ISC_FALSE;
7935         dns_rdataset_t *rdataset;
7936         isc_result_t result;
7937
7938         REQUIRE(VALID_RESOLVER(res));
7939         REQUIRE(res->frozen);
7940
7941         RTRACE("dns_resolver_prime");
7942
7943         LOCK(&res->lock);
7944
7945         if (!res->exiting && !res->priming) {
7946                 INSIST(res->primefetch == NULL);
7947                 res->priming = ISC_TRUE;
7948                 want_priming = ISC_TRUE;
7949         }
7950
7951         UNLOCK(&res->lock);
7952
7953         if (want_priming) {
7954                 /*
7955                  * To avoid any possible recursive locking problems, we
7956                  * start the priming fetch like any other fetch, and holding
7957                  * no resolver locks.  No one else will try to start it
7958                  * because we're the ones who set res->priming to true.
7959                  * Any other callers of dns_resolver_prime() while we're
7960                  * running will see that res->priming is already true and
7961                  * do nothing.
7962                  */
7963                 RTRACE("priming");
7964                 rdataset = isc_mem_get(res->mctx, sizeof(*rdataset));
7965                 if (rdataset == NULL) {
7966                         LOCK(&res->lock);
7967                         INSIST(res->priming);
7968                         INSIST(res->primefetch == NULL);
7969                         res->priming = ISC_FALSE;
7970                         UNLOCK(&res->lock);
7971                         return;
7972                 }
7973                 dns_rdataset_init(rdataset);
7974                 LOCK(&res->primelock);
7975                 result = dns_resolver_createfetch(res, dns_rootname,
7976                                                   dns_rdatatype_ns,
7977                                                   NULL, NULL, NULL, 0,
7978                                                   res->buckets[0].task,
7979                                                   prime_done,
7980                                                   res, rdataset, NULL,
7981                                                   &res->primefetch);
7982                 UNLOCK(&res->primelock);
7983                 if (result != ISC_R_SUCCESS) {
7984                         LOCK(&res->lock);
7985                         INSIST(res->priming);
7986                         res->priming = ISC_FALSE;
7987                         UNLOCK(&res->lock);
7988                 }
7989         }
7990 }
7991 #endif /* BIND9 */
7992
7993 void
7994 dns_resolver_freeze(dns_resolver_t *res) {
7995         /*
7996          * Freeze resolver.
7997          */
7998
7999         REQUIRE(VALID_RESOLVER(res));
8000
8001         res->frozen = ISC_TRUE;
8002 }
8003
8004 void
8005 dns_resolver_attach(dns_resolver_t *source, dns_resolver_t **targetp) {
8006         REQUIRE(VALID_RESOLVER(source));
8007         REQUIRE(targetp != NULL && *targetp == NULL);
8008
8009         RRTRACE(source, "attach");
8010         LOCK(&source->lock);
8011         REQUIRE(!source->exiting);
8012
8013         INSIST(source->references > 0);
8014         source->references++;
8015         INSIST(source->references != 0);
8016         UNLOCK(&source->lock);
8017
8018         *targetp = source;
8019 }
8020
8021 void
8022 dns_resolver_whenshutdown(dns_resolver_t *res, isc_task_t *task,
8023                           isc_event_t **eventp)
8024 {
8025         isc_task_t *clone;
8026         isc_event_t *event;
8027
8028         REQUIRE(VALID_RESOLVER(res));
8029         REQUIRE(eventp != NULL);
8030
8031         event = *eventp;
8032         *eventp = NULL;
8033
8034         LOCK(&res->lock);
8035
8036         if (res->exiting && res->activebuckets == 0) {
8037                 /*
8038                  * We're already shutdown.  Send the event.
8039                  */
8040                 event->ev_sender = res;
8041                 isc_task_send(task, &event);
8042         } else {
8043                 clone = NULL;
8044                 isc_task_attach(task, &clone);
8045                 event->ev_sender = clone;
8046                 ISC_LIST_APPEND(res->whenshutdown, event, ev_link);
8047         }
8048
8049         UNLOCK(&res->lock);
8050 }
8051
8052 void
8053 dns_resolver_shutdown(dns_resolver_t *res) {
8054         unsigned int i;
8055         fetchctx_t *fctx;
8056         isc_result_t result;
8057
8058         REQUIRE(VALID_RESOLVER(res));
8059
8060         RTRACE("shutdown");
8061
8062         LOCK(&res->lock);
8063
8064         if (!res->exiting) {
8065                 RTRACE("exiting");
8066                 res->exiting = ISC_TRUE;
8067
8068                 for (i = 0; i < res->nbuckets; i++) {
8069                         LOCK(&res->buckets[i].lock);
8070                         for (fctx = ISC_LIST_HEAD(res->buckets[i].fctxs);
8071                              fctx != NULL;
8072                              fctx = ISC_LIST_NEXT(fctx, link))
8073                                 fctx_shutdown(fctx);
8074                         if (res->dispatches4 != NULL && !res->exclusivev4) {
8075                                 dns_dispatchset_cancelall(res->dispatches4,
8076                                                           res->buckets[i].task);
8077                         }
8078                         if (res->dispatches6 != NULL && !res->exclusivev6) {
8079                                 dns_dispatchset_cancelall(res->dispatches6,
8080                                                           res->buckets[i].task);
8081                         }
8082                         res->buckets[i].exiting = ISC_TRUE;
8083                         if (ISC_LIST_EMPTY(res->buckets[i].fctxs)) {
8084                                 INSIST(res->activebuckets > 0);
8085                                 res->activebuckets--;
8086                         }
8087                         UNLOCK(&res->buckets[i].lock);
8088                 }
8089                 if (res->activebuckets == 0)
8090                         send_shutdown_events(res);
8091                 result = isc_timer_reset(res->spillattimer,
8092                                          isc_timertype_inactive, NULL,
8093                                          NULL, ISC_TRUE);
8094                 RUNTIME_CHECK(result == ISC_R_SUCCESS);
8095         }
8096
8097         UNLOCK(&res->lock);
8098 }
8099
8100 void
8101 dns_resolver_detach(dns_resolver_t **resp) {
8102         dns_resolver_t *res;
8103         isc_boolean_t need_destroy = ISC_FALSE;
8104
8105         REQUIRE(resp != NULL);
8106         res = *resp;
8107         REQUIRE(VALID_RESOLVER(res));
8108
8109         RTRACE("detach");
8110
8111         LOCK(&res->lock);
8112
8113         INSIST(res->references > 0);
8114         res->references--;
8115         if (res->references == 0) {
8116                 INSIST(res->exiting && res->activebuckets == 0);
8117                 need_destroy = ISC_TRUE;
8118         }
8119
8120         UNLOCK(&res->lock);
8121
8122         if (need_destroy)
8123                 destroy(res);
8124
8125         *resp = NULL;
8126 }
8127
8128 static inline isc_boolean_t
8129 fctx_match(fetchctx_t *fctx, dns_name_t *name, dns_rdatatype_t type,
8130            unsigned int options)
8131 {
8132         /*
8133          * Don't match fetch contexts that are shutting down.
8134          */
8135         if (fctx->cloned || fctx->state == fetchstate_done ||
8136             ISC_LIST_EMPTY(fctx->events))
8137                 return (ISC_FALSE);
8138
8139         if (fctx->type != type || fctx->options != options)
8140                 return (ISC_FALSE);
8141         return (dns_name_equal(&fctx->name, name));
8142 }
8143
8144 static inline void
8145 log_fetch(dns_name_t *name, dns_rdatatype_t type) {
8146         char namebuf[DNS_NAME_FORMATSIZE];
8147         char typebuf[DNS_RDATATYPE_FORMATSIZE];
8148         int level = ISC_LOG_DEBUG(1);
8149
8150         if (! isc_log_wouldlog(dns_lctx, level))
8151                 return;
8152
8153         dns_name_format(name, namebuf, sizeof(namebuf));
8154         dns_rdatatype_format(type, typebuf, sizeof(typebuf));
8155
8156         isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
8157                       DNS_LOGMODULE_RESOLVER, level,
8158                       "createfetch: %s %s", namebuf, typebuf);
8159 }
8160
8161 isc_result_t
8162 dns_resolver_createfetch(dns_resolver_t *res, dns_name_t *name,
8163                          dns_rdatatype_t type,
8164                          dns_name_t *domain, dns_rdataset_t *nameservers,
8165                          dns_forwarders_t *forwarders,
8166                          unsigned int options, isc_task_t *task,
8167                          isc_taskaction_t action, void *arg,
8168                          dns_rdataset_t *rdataset,
8169                          dns_rdataset_t *sigrdataset,
8170                          dns_fetch_t **fetchp)
8171 {
8172         return (dns_resolver_createfetch2(res, name, type, domain,
8173                                           nameservers, forwarders, NULL, 0,
8174                                           options, task, action, arg,
8175                                           rdataset, sigrdataset, fetchp));
8176 }
8177
8178 isc_result_t
8179 dns_resolver_createfetch2(dns_resolver_t *res, dns_name_t *name,
8180                           dns_rdatatype_t type,
8181                           dns_name_t *domain, dns_rdataset_t *nameservers,
8182                           dns_forwarders_t *forwarders,
8183                           isc_sockaddr_t *client, dns_messageid_t id,
8184                           unsigned int options, isc_task_t *task,
8185                           isc_taskaction_t action, void *arg,
8186                           dns_rdataset_t *rdataset,
8187                           dns_rdataset_t *sigrdataset,
8188                           dns_fetch_t **fetchp)
8189 {
8190         dns_fetch_t *fetch;
8191         fetchctx_t *fctx = NULL;
8192         isc_result_t result = ISC_R_SUCCESS;
8193         unsigned int bucketnum;
8194         isc_boolean_t new_fctx = ISC_FALSE;
8195         isc_event_t *event;
8196         unsigned int count = 0;
8197         unsigned int spillat;
8198         unsigned int spillatmin;
8199         isc_boolean_t destroy = ISC_FALSE;
8200
8201         UNUSED(forwarders);
8202
8203         REQUIRE(VALID_RESOLVER(res));
8204         REQUIRE(res->frozen);
8205         /* XXXRTH  Check for meta type */
8206         if (domain != NULL) {
8207                 REQUIRE(DNS_RDATASET_VALID(nameservers));
8208                 REQUIRE(nameservers->type == dns_rdatatype_ns);
8209         } else
8210                 REQUIRE(nameservers == NULL);
8211         REQUIRE(forwarders == NULL);
8212         REQUIRE(!dns_rdataset_isassociated(rdataset));
8213         REQUIRE(sigrdataset == NULL ||
8214                 !dns_rdataset_isassociated(sigrdataset));
8215         REQUIRE(fetchp != NULL && *fetchp == NULL);
8216
8217         log_fetch(name, type);
8218
8219         /*
8220          * XXXRTH  use a mempool?
8221          */
8222         fetch = isc_mem_get(res->mctx, sizeof(*fetch));
8223         if (fetch == NULL)
8224                 return (ISC_R_NOMEMORY);
8225
8226         bucketnum = dns_name_fullhash(name, ISC_FALSE) % res->nbuckets;
8227
8228         LOCK(&res->lock);
8229         spillat = res->spillat;
8230         spillatmin = res->spillatmin;
8231         UNLOCK(&res->lock);
8232         LOCK(&res->buckets[bucketnum].lock);
8233
8234         if (res->buckets[bucketnum].exiting) {
8235                 result = ISC_R_SHUTTINGDOWN;
8236                 goto unlock;
8237         }
8238
8239         if ((options & DNS_FETCHOPT_UNSHARED) == 0) {
8240                 for (fctx = ISC_LIST_HEAD(res->buckets[bucketnum].fctxs);
8241                      fctx != NULL;
8242                      fctx = ISC_LIST_NEXT(fctx, link)) {
8243                         if (fctx_match(fctx, name, type, options))
8244                                 break;
8245                 }
8246         }
8247
8248         /*
8249          * Is this a duplicate?
8250          */
8251         if (fctx != NULL && client != NULL) {
8252                 dns_fetchevent_t *fevent;
8253                 for (fevent = ISC_LIST_HEAD(fctx->events);
8254                      fevent != NULL;
8255                      fevent = ISC_LIST_NEXT(fevent, ev_link)) {
8256                         if (fevent->client != NULL && fevent->id == id &&
8257                             isc_sockaddr_equal(fevent->client, client)) {
8258                                 result = DNS_R_DUPLICATE;
8259                                 goto unlock;
8260                         }
8261                         count++;
8262                 }
8263         }
8264         if (count >= spillatmin && spillatmin != 0) {
8265                 INSIST(fctx != NULL);
8266                 if (count >= spillat)
8267                         fctx->spilled = ISC_TRUE;
8268                 if (fctx->spilled) {
8269                         result = DNS_R_DROP;
8270                         goto unlock;
8271                 }
8272         }
8273
8274         if (fctx == NULL) {
8275                 result = fctx_create(res, name, type, domain, nameservers,
8276                                      options, bucketnum, &fctx);
8277                 if (result != ISC_R_SUCCESS)
8278                         goto unlock;
8279                 new_fctx = ISC_TRUE;
8280         }
8281
8282         result = fctx_join(fctx, task, client, id, action, arg,
8283                            rdataset, sigrdataset, fetch);
8284         if (new_fctx) {
8285                 if (result == ISC_R_SUCCESS) {
8286                         /*
8287                          * Launch this fctx.
8288                          */
8289                         event = &fctx->control_event;
8290                         ISC_EVENT_INIT(event, sizeof(*event), 0, NULL,
8291                                        DNS_EVENT_FETCHCONTROL,
8292                                        fctx_start, fctx, NULL,
8293                                        NULL, NULL);
8294                         isc_task_send(res->buckets[bucketnum].task, &event);
8295                 } else {
8296                         /*
8297                          * We don't care about the result of fctx_unlink()
8298                          * since we know we're not exiting.
8299                          */
8300                         (void)fctx_unlink(fctx);
8301                         destroy = ISC_TRUE;
8302                 }
8303         }
8304
8305  unlock:
8306         UNLOCK(&res->buckets[bucketnum].lock);
8307
8308         if (destroy)
8309                 fctx_destroy(fctx);
8310
8311         if (result == ISC_R_SUCCESS) {
8312                 FTRACE("created");
8313                 *fetchp = fetch;
8314         } else
8315                 isc_mem_put(res->mctx, fetch, sizeof(*fetch));
8316
8317         return (result);
8318 }
8319
8320 void
8321 dns_resolver_cancelfetch(dns_fetch_t *fetch) {
8322         fetchctx_t *fctx;
8323         dns_resolver_t *res;
8324         dns_fetchevent_t *event, *next_event;
8325         isc_task_t *etask;
8326
8327         REQUIRE(DNS_FETCH_VALID(fetch));
8328         fctx = fetch->private;
8329         REQUIRE(VALID_FCTX(fctx));
8330         res = fctx->res;
8331
8332         FTRACE("cancelfetch");
8333
8334         LOCK(&res->buckets[fctx->bucketnum].lock);
8335
8336         /*
8337          * Find the completion event for this fetch (as opposed
8338          * to those for other fetches that have joined the same
8339          * fctx) and send it with result = ISC_R_CANCELED.
8340          */
8341         event = NULL;
8342         if (fctx->state != fetchstate_done) {
8343                 for (event = ISC_LIST_HEAD(fctx->events);
8344                      event != NULL;
8345                      event = next_event) {
8346                         next_event = ISC_LIST_NEXT(event, ev_link);
8347                         if (event->fetch == fetch) {
8348                                 ISC_LIST_UNLINK(fctx->events, event, ev_link);
8349                                 break;
8350                         }
8351                 }
8352         }
8353         if (event != NULL) {
8354                 etask = event->ev_sender;
8355                 event->ev_sender = fctx;
8356                 event->result = ISC_R_CANCELED;
8357                 isc_task_sendanddetach(&etask, ISC_EVENT_PTR(&event));
8358         }
8359         /*
8360          * The fctx continues running even if no fetches remain;
8361          * the answer is still cached.
8362          */
8363
8364         UNLOCK(&res->buckets[fctx->bucketnum].lock);
8365 }
8366
8367 void
8368 dns_resolver_destroyfetch(dns_fetch_t **fetchp) {
8369         dns_fetch_t *fetch;
8370         dns_resolver_t *res;
8371         dns_fetchevent_t *event, *next_event;
8372         fetchctx_t *fctx;
8373         unsigned int bucketnum;
8374         isc_boolean_t bucket_empty;
8375
8376         REQUIRE(fetchp != NULL);
8377         fetch = *fetchp;
8378         REQUIRE(DNS_FETCH_VALID(fetch));
8379         fctx = fetch->private;
8380         REQUIRE(VALID_FCTX(fctx));
8381         res = fctx->res;
8382
8383         FTRACE("destroyfetch");
8384
8385         bucketnum = fctx->bucketnum;
8386         LOCK(&res->buckets[bucketnum].lock);
8387
8388         /*
8389          * Sanity check: the caller should have gotten its event before
8390          * trying to destroy the fetch.
8391          */
8392         event = NULL;
8393         if (fctx->state != fetchstate_done) {
8394                 for (event = ISC_LIST_HEAD(fctx->events);
8395                      event != NULL;
8396                      event = next_event) {
8397                         next_event = ISC_LIST_NEXT(event, ev_link);
8398                         RUNTIME_CHECK(event->fetch != fetch);
8399                 }
8400         }
8401
8402         bucket_empty = fctx_decreference(fctx);
8403
8404         UNLOCK(&res->buckets[bucketnum].lock);
8405
8406         isc_mem_put(res->mctx, fetch, sizeof(*fetch));
8407         *fetchp = NULL;
8408
8409         if (bucket_empty)
8410                 empty_bucket(res);
8411 }
8412
8413 void
8414 dns_resolver_logfetch(dns_fetch_t *fetch, isc_log_t *lctx,
8415                       isc_logcategory_t *category, isc_logmodule_t *module,
8416                       int level, isc_boolean_t duplicateok)
8417 {
8418         fetchctx_t *fctx;
8419         dns_resolver_t *res;
8420         char domainbuf[DNS_NAME_FORMATSIZE];
8421
8422         REQUIRE(DNS_FETCH_VALID(fetch));
8423         fctx = fetch->private;
8424         REQUIRE(VALID_FCTX(fctx));
8425         res = fctx->res;
8426
8427         LOCK(&res->buckets[fctx->bucketnum].lock);
8428
8429         INSIST(fctx->exitline >= 0);
8430         if (!fctx->logged || duplicateok) {
8431                 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
8432                 isc_log_write(lctx, category, module, level,
8433                               "fetch completed at %s:%d for %s in "
8434                               "%" ISC_PRINT_QUADFORMAT "u."
8435                               "%06" ISC_PRINT_QUADFORMAT "u: %s/%s "
8436                               "[domain:%s,referral:%u,restart:%u,qrysent:%u,"
8437                               "timeout:%u,lame:%u,neterr:%u,badresp:%u,"
8438                               "adberr:%u,findfail:%u,valfail:%u]",
8439                               __FILE__, fctx->exitline, fctx->info,
8440                               fctx->duration / US_PER_SEC,
8441                               fctx->duration % US_PER_SEC,
8442                               isc_result_totext(fctx->result),
8443                               isc_result_totext(fctx->vresult), domainbuf,
8444                               fctx->referrals, fctx->restarts,
8445                               fctx->querysent, fctx->timeouts, fctx->lamecount,
8446                               fctx->neterr, fctx->badresp, fctx->adberr,
8447                               fctx->findfail, fctx->valfail);
8448                 fctx->logged = ISC_TRUE;
8449         }
8450
8451         UNLOCK(&res->buckets[fctx->bucketnum].lock);
8452 }
8453
8454 dns_dispatchmgr_t *
8455 dns_resolver_dispatchmgr(dns_resolver_t *resolver) {
8456         REQUIRE(VALID_RESOLVER(resolver));
8457         return (resolver->dispatchmgr);
8458 }
8459
8460 dns_dispatch_t *
8461 dns_resolver_dispatchv4(dns_resolver_t *resolver) {
8462         REQUIRE(VALID_RESOLVER(resolver));
8463         return (dns_dispatchset_get(resolver->dispatches4));
8464 }
8465
8466 dns_dispatch_t *
8467 dns_resolver_dispatchv6(dns_resolver_t *resolver) {
8468         REQUIRE(VALID_RESOLVER(resolver));
8469         return (dns_dispatchset_get(resolver->dispatches6));
8470 }
8471
8472 isc_socketmgr_t *
8473 dns_resolver_socketmgr(dns_resolver_t *resolver) {
8474         REQUIRE(VALID_RESOLVER(resolver));
8475         return (resolver->socketmgr);
8476 }
8477
8478 isc_taskmgr_t *
8479 dns_resolver_taskmgr(dns_resolver_t *resolver) {
8480         REQUIRE(VALID_RESOLVER(resolver));
8481         return (resolver->taskmgr);
8482 }
8483
8484 isc_uint32_t
8485 dns_resolver_getlamettl(dns_resolver_t *resolver) {
8486         REQUIRE(VALID_RESOLVER(resolver));
8487         return (resolver->lame_ttl);
8488 }
8489
8490 void
8491 dns_resolver_setlamettl(dns_resolver_t *resolver, isc_uint32_t lame_ttl) {
8492         REQUIRE(VALID_RESOLVER(resolver));
8493         resolver->lame_ttl = lame_ttl;
8494 }
8495
8496 unsigned int
8497 dns_resolver_nrunning(dns_resolver_t *resolver) {
8498         unsigned int n;
8499         LOCK(&resolver->nlock);
8500         n = resolver->nfctx;
8501         UNLOCK(&resolver->nlock);
8502         return (n);
8503 }
8504
8505 isc_result_t
8506 dns_resolver_addalternate(dns_resolver_t *resolver, isc_sockaddr_t *alt,
8507                           dns_name_t *name, in_port_t port) {
8508         alternate_t *a;
8509         isc_result_t result;
8510
8511         REQUIRE(VALID_RESOLVER(resolver));
8512         REQUIRE(!resolver->frozen);
8513         REQUIRE((alt == NULL) ^ (name == NULL));
8514
8515         a = isc_mem_get(resolver->mctx, sizeof(*a));
8516         if (a == NULL)
8517                 return (ISC_R_NOMEMORY);
8518         if (alt != NULL) {
8519                 a->isaddress = ISC_TRUE;
8520                 a->_u.addr = *alt;
8521         } else {
8522                 a->isaddress = ISC_FALSE;
8523                 a->_u._n.port = port;
8524                 dns_name_init(&a->_u._n.name, NULL);
8525                 result = dns_name_dup(name, resolver->mctx, &a->_u._n.name);
8526                 if (result != ISC_R_SUCCESS) {
8527                         isc_mem_put(resolver->mctx, a, sizeof(*a));
8528                         return (result);
8529                 }
8530         }
8531         ISC_LINK_INIT(a, link);
8532         ISC_LIST_APPEND(resolver->alternates, a, link);
8533
8534         return (ISC_R_SUCCESS);
8535 }
8536
8537 void
8538 dns_resolver_setudpsize(dns_resolver_t *resolver, isc_uint16_t udpsize) {
8539         REQUIRE(VALID_RESOLVER(resolver));
8540         resolver->udpsize = udpsize;
8541 }
8542
8543 isc_uint16_t
8544 dns_resolver_getudpsize(dns_resolver_t *resolver) {
8545         REQUIRE(VALID_RESOLVER(resolver));
8546         return (resolver->udpsize);
8547 }
8548
8549 void
8550 dns_resolver_flushbadcache(dns_resolver_t *resolver, dns_name_t *name) {
8551         unsigned int i;
8552         dns_badcache_t *bad, *prev, *next;
8553
8554         REQUIRE(VALID_RESOLVER(resolver));
8555
8556         LOCK(&resolver->lock);
8557         if (resolver->badcache == NULL)
8558                 goto unlock;
8559
8560         if (name != NULL) {
8561                 isc_time_t now;
8562                 isc_result_t result;
8563                 result = isc_time_now(&now);
8564                 if (result != ISC_R_SUCCESS)
8565                         isc_time_settoepoch(&now);
8566                 i = dns_name_hash(name, ISC_FALSE) % resolver->badhash;
8567                 prev = NULL;
8568                 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8569                         int n;
8570                         next = bad->next;
8571                         n = isc_time_compare(&bad->expire, &now);
8572                         if (n < 0 || dns_name_equal(name, &bad->name)) {
8573                                 if (prev == NULL)
8574                                         resolver->badcache[i] = bad->next;
8575                                 else
8576                                         prev->next = bad->next;
8577                                 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8578                                             bad->name.length);
8579                                 resolver->badcount--;
8580                         } else
8581                                 prev = bad;
8582                 }
8583         } else
8584                 destroy_badcache(resolver);
8585
8586  unlock:
8587         UNLOCK(&resolver->lock);
8588
8589 }
8590
8591 static void
8592 resizehash(dns_resolver_t *resolver, isc_time_t *now, isc_boolean_t grow) {
8593         unsigned int newsize;
8594         dns_badcache_t **new, *bad, *next;
8595         unsigned int i;
8596
8597         if (grow)
8598                 newsize = resolver->badhash * 2 + 1;
8599         else
8600                 newsize = (resolver->badhash - 1) / 2;
8601
8602         new = isc_mem_get(resolver->mctx,
8603                           sizeof(*resolver->badcache) * newsize);
8604         if (new == NULL)
8605                 return;
8606         memset(new, 0, sizeof(*resolver->badcache) * newsize);
8607         for (i = 0; i < resolver->badhash; i++) {
8608                 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8609                         next = bad->next;
8610                         if (isc_time_compare(&bad->expire, now) < 0) {
8611                                 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8612                                             bad->name.length);
8613                                 resolver->badcount--;
8614                         } else {
8615                                 bad->next = new[bad->hashval % newsize];
8616                                 new[bad->hashval % newsize] = bad;
8617                         }
8618                 }
8619         }
8620         isc_mem_put(resolver->mctx, resolver->badcache,
8621                     sizeof(*resolver->badcache) * resolver->badhash);
8622         resolver->badhash = newsize;
8623         resolver->badcache = new;
8624 }
8625
8626 void
8627 dns_resolver_addbadcache(dns_resolver_t *resolver, dns_name_t *name,
8628                          dns_rdatatype_t type, isc_time_t *expire)
8629 {
8630         isc_time_t now;
8631         isc_result_t result = ISC_R_SUCCESS;
8632         unsigned int i, hashval;
8633         dns_badcache_t *bad, *prev, *next;
8634
8635         REQUIRE(VALID_RESOLVER(resolver));
8636
8637         LOCK(&resolver->lock);
8638         if (resolver->badcache == NULL) {
8639                 resolver->badcache = isc_mem_get(resolver->mctx,
8640                                                  sizeof(*resolver->badcache) *
8641                                                  DNS_BADCACHE_SIZE);
8642                 if (resolver->badcache == NULL)
8643                         goto cleanup;
8644                 resolver->badhash = DNS_BADCACHE_SIZE;
8645                 memset(resolver->badcache, 0, sizeof(*resolver->badcache) *
8646                        resolver->badhash);
8647         }
8648
8649         result = isc_time_now(&now);
8650         if (result != ISC_R_SUCCESS)
8651                 isc_time_settoepoch(&now);
8652         hashval = dns_name_hash(name, ISC_FALSE);
8653         i = hashval % resolver->badhash;
8654         prev = NULL;
8655         for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8656                 next = bad->next;
8657                 if (bad->type == type && dns_name_equal(name, &bad->name))
8658                         break;
8659                 if (isc_time_compare(&bad->expire, &now) < 0) {
8660                         if (prev == NULL)
8661                                 resolver->badcache[i] = bad->next;
8662                         else
8663                                 prev->next = bad->next;
8664                         isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8665                                     bad->name.length);
8666                         resolver->badcount--;
8667                 } else
8668                         prev = bad;
8669         }
8670         if (bad == NULL) {
8671                 isc_buffer_t buffer;
8672                 bad = isc_mem_get(resolver->mctx, sizeof(*bad) + name->length);
8673                 if (bad == NULL)
8674                         goto cleanup;
8675                 bad->type = type;
8676                 bad->hashval = hashval;
8677                 bad->expire = *expire;
8678                 isc_buffer_init(&buffer, bad + 1, name->length);
8679                 dns_name_init(&bad->name, NULL);
8680                 dns_name_copy(name, &bad->name, &buffer);
8681                 bad->next = resolver->badcache[i];
8682                 resolver->badcache[i] = bad;
8683                 resolver->badcount++;
8684                 if (resolver->badcount > resolver->badhash * 8)
8685                         resizehash(resolver, &now, ISC_TRUE);
8686                 if (resolver->badcount < resolver->badhash * 2 &&
8687                     resolver->badhash > DNS_BADCACHE_SIZE)
8688                         resizehash(resolver, &now, ISC_FALSE);
8689         } else
8690                 bad->expire = *expire;
8691  cleanup:
8692         UNLOCK(&resolver->lock);
8693 }
8694
8695 isc_boolean_t
8696 dns_resolver_getbadcache(dns_resolver_t *resolver, dns_name_t *name,
8697                          dns_rdatatype_t type, isc_time_t *now)
8698 {
8699         dns_badcache_t *bad, *prev, *next;
8700         isc_boolean_t answer = ISC_FALSE;
8701         unsigned int i;
8702
8703         REQUIRE(VALID_RESOLVER(resolver));
8704
8705         LOCK(&resolver->lock);
8706         if (resolver->badcache == NULL)
8707                 goto unlock;
8708
8709         i = dns_name_hash(name, ISC_FALSE) % resolver->badhash;
8710         prev = NULL;
8711         for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8712                 next = bad->next;
8713                 /*
8714                  * Search the hash list. Clean out expired records as we go.
8715                  */
8716                 if (isc_time_compare(&bad->expire, now) < 0) {
8717                         if (prev != NULL)
8718                                 prev->next = bad->next;
8719                         else
8720                                 resolver->badcache[i] = bad->next;
8721                         isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8722                                     bad->name.length);
8723                         resolver->badcount--;
8724                         continue;
8725                 }
8726                 if (bad->type == type && dns_name_equal(name, &bad->name)) {
8727                         answer = ISC_TRUE;
8728                         break;
8729                 }
8730                 prev = bad;
8731         }
8732
8733         /*
8734          * Slow sweep to clean out stale records.
8735          */
8736         i = resolver->badsweep++ % resolver->badhash;
8737         bad = resolver->badcache[i];
8738         if (bad != NULL && isc_time_compare(&bad->expire, now) < 0) {
8739                 resolver->badcache[i] = bad->next;
8740                 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8741                             bad->name.length);
8742                 resolver->badcount--;
8743         }
8744
8745  unlock:
8746         UNLOCK(&resolver->lock);
8747         return (answer);
8748 }
8749
8750 void
8751 dns_resolver_printbadcache(dns_resolver_t *resolver, FILE *fp) {
8752         char namebuf[DNS_NAME_FORMATSIZE];
8753         char typebuf[DNS_RDATATYPE_FORMATSIZE];
8754         dns_badcache_t *bad, *next, *prev;
8755         isc_time_t now;
8756         unsigned int i;
8757         isc_uint64_t t;
8758
8759         LOCK(&resolver->lock);
8760         fprintf(fp, ";\n; Bad cache\n;\n");
8761
8762         if (resolver->badcache == NULL)
8763                 goto unlock;
8764
8765         TIME_NOW(&now);
8766         for (i = 0; i < resolver->badhash; i++) {
8767                 prev = NULL;
8768                 for (bad = resolver->badcache[i]; bad != NULL; bad = next) {
8769                         next = bad->next;
8770                         if (isc_time_compare(&bad->expire, &now) < 0) {
8771                                 if (prev != NULL)
8772                                         prev->next = bad->next;
8773                                 else
8774                                         resolver->badcache[i] = bad->next;
8775                                 isc_mem_put(resolver->mctx, bad, sizeof(*bad) +
8776                                             bad->name.length);
8777                                 resolver->badcount--;
8778                                 continue;
8779                         }
8780                         prev = bad;
8781                         dns_name_format(&bad->name, namebuf, sizeof(namebuf));
8782                         dns_rdatatype_format(bad->type, typebuf,
8783                                              sizeof(typebuf));
8784                         t = isc_time_microdiff(&bad->expire, &now);
8785                         t /= 1000;
8786                         fprintf(fp, "; %s/%s [ttl "
8787                                 "%" ISC_PLATFORM_QUADFORMAT "u]\n",
8788                                 namebuf, typebuf, t);
8789                 }
8790         }
8791
8792  unlock:
8793         UNLOCK(&resolver->lock);
8794 }
8795
8796 static void
8797 free_algorithm(void *node, void *arg) {
8798         unsigned char *algorithms = node;
8799         isc_mem_t *mctx = arg;
8800
8801         isc_mem_put(mctx, algorithms, *algorithms);
8802 }
8803
8804 void
8805 dns_resolver_reset_algorithms(dns_resolver_t *resolver) {
8806
8807         REQUIRE(VALID_RESOLVER(resolver));
8808
8809 #if USE_ALGLOCK
8810         RWLOCK(&resolver->alglock, isc_rwlocktype_write);
8811 #endif
8812         if (resolver->algorithms != NULL)
8813                 dns_rbt_destroy(&resolver->algorithms);
8814 #if USE_ALGLOCK
8815         RWUNLOCK(&resolver->alglock, isc_rwlocktype_write);
8816 #endif
8817 }
8818
8819 isc_result_t
8820 dns_resolver_disable_algorithm(dns_resolver_t *resolver, dns_name_t *name,
8821                                unsigned int alg)
8822 {
8823         unsigned int len, mask;
8824         unsigned char *new;
8825         unsigned char *algorithms;
8826         isc_result_t result;
8827         dns_rbtnode_t *node = NULL;
8828
8829         REQUIRE(VALID_RESOLVER(resolver));
8830         if (alg > 255)
8831                 return (ISC_R_RANGE);
8832
8833 #if USE_ALGLOCK
8834         RWLOCK(&resolver->alglock, isc_rwlocktype_write);
8835 #endif
8836         if (resolver->algorithms == NULL) {
8837                 result = dns_rbt_create(resolver->mctx, free_algorithm,
8838                                         resolver->mctx, &resolver->algorithms);
8839                 if (result != ISC_R_SUCCESS)
8840                         goto cleanup;
8841         }
8842
8843         len = alg/8 + 2;
8844         mask = 1 << (alg%8);
8845
8846         result = dns_rbt_addnode(resolver->algorithms, name, &node);
8847
8848         if (result == ISC_R_SUCCESS || result == ISC_R_EXISTS) {
8849                 algorithms = node->data;
8850                 if (algorithms == NULL || len > *algorithms) {
8851                         new = isc_mem_get(resolver->mctx, len);
8852                         if (new == NULL) {
8853                                 result = ISC_R_NOMEMORY;
8854                                 goto cleanup;
8855                         }
8856                         memset(new, 0, len);
8857                         if (algorithms != NULL)
8858                                 memmove(new, algorithms, *algorithms);
8859                         new[len-1] |= mask;
8860                         *new = len;
8861                         node->data = new;
8862                         if (algorithms != NULL)
8863                                 isc_mem_put(resolver->mctx, algorithms,
8864                                             *algorithms);
8865                 } else
8866                         algorithms[len-1] |= mask;
8867         }
8868         result = ISC_R_SUCCESS;
8869  cleanup:
8870 #if USE_ALGLOCK
8871         RWUNLOCK(&resolver->alglock, isc_rwlocktype_write);
8872 #endif
8873         return (result);
8874 }
8875
8876 isc_boolean_t
8877 dns_resolver_algorithm_supported(dns_resolver_t *resolver, dns_name_t *name,
8878                                  unsigned int alg)
8879 {
8880         unsigned int len, mask;
8881         unsigned char *algorithms;
8882         void *data = NULL;
8883         isc_result_t result;
8884         isc_boolean_t found = ISC_FALSE;
8885
8886         REQUIRE(VALID_RESOLVER(resolver));
8887
8888 #if USE_ALGLOCK
8889         RWLOCK(&resolver->alglock, isc_rwlocktype_read);
8890 #endif
8891         if (resolver->algorithms == NULL)
8892                 goto unlock;
8893         result = dns_rbt_findname(resolver->algorithms, name, 0, NULL, &data);
8894         if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
8895                 len = alg/8 + 2;
8896                 mask = 1 << (alg%8);
8897                 algorithms = data;
8898                 if (len <= *algorithms && (algorithms[len-1] & mask) != 0)
8899                         found = ISC_TRUE;
8900         }
8901  unlock:
8902 #if USE_ALGLOCK
8903         RWUNLOCK(&resolver->alglock, isc_rwlocktype_read);
8904 #endif
8905         if (found)
8906                 return (ISC_FALSE);
8907         return (dst_algorithm_supported(alg));
8908 }
8909
8910 isc_boolean_t
8911 dns_resolver_digest_supported(dns_resolver_t *resolver, unsigned int digest) {
8912
8913         UNUSED(resolver);
8914         return (dns_ds_digest_supported(digest));
8915 }
8916
8917 void
8918 dns_resolver_resetmustbesecure(dns_resolver_t *resolver) {
8919
8920         REQUIRE(VALID_RESOLVER(resolver));
8921
8922 #if USE_MBSLOCK
8923         RWLOCK(&resolver->mbslock, isc_rwlocktype_write);
8924 #endif
8925         if (resolver->mustbesecure != NULL)
8926                 dns_rbt_destroy(&resolver->mustbesecure);
8927 #if USE_MBSLOCK
8928         RWUNLOCK(&resolver->mbslock, isc_rwlocktype_write);
8929 #endif
8930 }
8931
8932 static isc_boolean_t yes = ISC_TRUE, no = ISC_FALSE;
8933
8934 isc_result_t
8935 dns_resolver_setmustbesecure(dns_resolver_t *resolver, dns_name_t *name,
8936                              isc_boolean_t value)
8937 {
8938         isc_result_t result;
8939
8940         REQUIRE(VALID_RESOLVER(resolver));
8941
8942 #if USE_MBSLOCK
8943         RWLOCK(&resolver->mbslock, isc_rwlocktype_write);
8944 #endif
8945         if (resolver->mustbesecure == NULL) {
8946                 result = dns_rbt_create(resolver->mctx, NULL, NULL,
8947                                         &resolver->mustbesecure);
8948                 if (result != ISC_R_SUCCESS)
8949                         goto cleanup;
8950         }
8951         result = dns_rbt_addname(resolver->mustbesecure, name,
8952                                  value ? &yes : &no);
8953  cleanup:
8954 #if USE_MBSLOCK
8955         RWUNLOCK(&resolver->mbslock, isc_rwlocktype_write);
8956 #endif
8957         return (result);
8958 }
8959
8960 isc_boolean_t
8961 dns_resolver_getmustbesecure(dns_resolver_t *resolver, dns_name_t *name) {
8962         void *data = NULL;
8963         isc_boolean_t value = ISC_FALSE;
8964         isc_result_t result;
8965
8966         REQUIRE(VALID_RESOLVER(resolver));
8967
8968 #if USE_MBSLOCK
8969         RWLOCK(&resolver->mbslock, isc_rwlocktype_read);
8970 #endif
8971         if (resolver->mustbesecure == NULL)
8972                 goto unlock;
8973         result = dns_rbt_findname(resolver->mustbesecure, name, 0, NULL, &data);
8974         if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
8975                 value = *(isc_boolean_t*)data;
8976  unlock:
8977 #if USE_MBSLOCK
8978         RWUNLOCK(&resolver->mbslock, isc_rwlocktype_read);
8979 #endif
8980         return (value);
8981 }
8982
8983 void
8984 dns_resolver_getclientsperquery(dns_resolver_t *resolver, isc_uint32_t *cur,
8985                                 isc_uint32_t *min, isc_uint32_t *max)
8986 {
8987         REQUIRE(VALID_RESOLVER(resolver));
8988
8989         LOCK(&resolver->lock);
8990         if (cur != NULL)
8991                 *cur = resolver->spillat;
8992         if (min != NULL)
8993                 *min = resolver->spillatmin;
8994         if (max != NULL)
8995                 *max = resolver->spillatmax;
8996         UNLOCK(&resolver->lock);
8997 }
8998
8999 void
9000 dns_resolver_setclientsperquery(dns_resolver_t *resolver, isc_uint32_t min,
9001                                 isc_uint32_t max)
9002 {
9003         REQUIRE(VALID_RESOLVER(resolver));
9004
9005         LOCK(&resolver->lock);
9006         resolver->spillatmin = resolver->spillat = min;
9007         resolver->spillatmax = max;
9008         UNLOCK(&resolver->lock);
9009 }
9010
9011 isc_boolean_t
9012 dns_resolver_getzeronosoattl(dns_resolver_t *resolver) {
9013         REQUIRE(VALID_RESOLVER(resolver));
9014
9015         return (resolver->zero_no_soa_ttl);
9016 }
9017
9018 void
9019 dns_resolver_setzeronosoattl(dns_resolver_t *resolver, isc_boolean_t state) {
9020         REQUIRE(VALID_RESOLVER(resolver));
9021
9022         resolver->zero_no_soa_ttl = state;
9023 }
9024
9025 unsigned int
9026 dns_resolver_getoptions(dns_resolver_t *resolver) {
9027         REQUIRE(VALID_RESOLVER(resolver));
9028
9029         return (resolver->options);
9030 }
9031
9032 unsigned int
9033 dns_resolver_gettimeout(dns_resolver_t *resolver) {
9034         REQUIRE(VALID_RESOLVER(resolver));
9035
9036         return (resolver->query_timeout);
9037 }
9038
9039 void
9040 dns_resolver_settimeout(dns_resolver_t *resolver, unsigned int seconds) {
9041         REQUIRE(VALID_RESOLVER(resolver));
9042
9043         if (seconds == 0)
9044                 seconds = DEFAULT_QUERY_TIMEOUT;
9045         if (seconds > MAXIMUM_QUERY_TIMEOUT)
9046                 seconds = MAXIMUM_QUERY_TIMEOUT;
9047         if (seconds < MINIMUM_QUERY_TIMEOUT)
9048                 seconds =  MINIMUM_QUERY_TIMEOUT;
9049
9050         resolver->query_timeout = seconds;
9051 }