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