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