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