Import bind 9.5.2 vendor sources.
[dragonfly.git] / contrib / bind-9.5.2 / lib / dns / resolver.c
1 /*
2  * Copyright (C) 2004-2009  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.355.12.44 2009/08/13 04:54:21 marka 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         dns_validator_t                 *validator;
207         ISC_LIST(dns_validator_t)       validators;
208         dns_db_t *                      cache;
209         dns_adb_t *                     adb;
210
211         /*%
212          * The number of events we're waiting for.
213          */
214         unsigned int                    pending;
215
216         /*%
217          * The number of times we've "restarted" the current
218          * nameserver set.  This acts as a failsafe to prevent
219          * us from pounding constantly on a particular set of
220          * servers that, for whatever reason, are not giving
221          * us useful responses, but are responding in such a
222          * way that they are not marked "bad".
223          */
224         unsigned int                    restarts;
225
226         /*%
227          * The number of timeouts that have occurred since we
228          * last successfully received a response packet.  This
229          * is used for EDNS0 black hole detection.
230          */
231         unsigned int                    timeouts;
232
233         /*%
234          * Look aside state for DS lookups.
235          */
236         dns_name_t                      nsname;
237         dns_fetch_t *                   nsfetch;
238         dns_rdataset_t                  nsrrset;
239
240         /*%
241          * Number of queries that reference this context.
242          */
243         unsigned int                    nqueries;
244
245         /*%
246          * The reason to print when logging a successful
247          * response to a query.
248          */
249         const char *                    reason;
250
251         /*%
252          * Fetch-local statistics for detailed logging.
253          */
254         isc_result_t                    result; /*%< fetch result  */
255         isc_result_t                    vresult; /*%< validation result  */
256         int                             exitline;
257         isc_time_t                      start;
258         isc_uint64_t                    duration;
259         isc_boolean_t                   logged;
260         unsigned int                    querysent;
261         unsigned int                    referrals;
262         unsigned int                    lamecount;
263         unsigned int                    neterr;
264         unsigned int                    badresp;
265         unsigned int                    adberr;
266         unsigned int                    findfail;
267         unsigned int                    valfail;
268         isc_boolean_t                   timeout;
269 };
270
271 #define FCTX_MAGIC                      ISC_MAGIC('F', '!', '!', '!')
272 #define VALID_FCTX(fctx)                ISC_MAGIC_VALID(fctx, FCTX_MAGIC)
273
274 #define FCTX_ATTR_HAVEANSWER            0x0001
275 #define FCTX_ATTR_GLUING                0x0002
276 #define FCTX_ATTR_ADDRWAIT              0x0004
277 #define FCTX_ATTR_SHUTTINGDOWN          0x0008
278 #define FCTX_ATTR_WANTCACHE             0x0010
279 #define FCTX_ATTR_WANTNCACHE            0x0020
280 #define FCTX_ATTR_NEEDEDNS0             0x0040
281 #define FCTX_ATTR_TRIEDFIND             0x0080
282 #define FCTX_ATTR_TRIEDALT              0x0100
283
284 #define HAVE_ANSWER(f)          (((f)->attributes & FCTX_ATTR_HAVEANSWER) != \
285                                  0)
286 #define GLUING(f)               (((f)->attributes & FCTX_ATTR_GLUING) != \
287                                  0)
288 #define ADDRWAIT(f)             (((f)->attributes & FCTX_ATTR_ADDRWAIT) != \
289                                  0)
290 #define SHUTTINGDOWN(f)         (((f)->attributes & FCTX_ATTR_SHUTTINGDOWN) \
291                                  != 0)
292 #define WANTCACHE(f)            (((f)->attributes & FCTX_ATTR_WANTCACHE) != 0)
293 #define WANTNCACHE(f)           (((f)->attributes & FCTX_ATTR_WANTNCACHE) != 0)
294 #define NEEDEDNS0(f)            (((f)->attributes & FCTX_ATTR_NEEDEDNS0) != 0)
295 #define TRIEDFIND(f)            (((f)->attributes & FCTX_ATTR_TRIEDFIND) != 0)
296 #define TRIEDALT(f)             (((f)->attributes & FCTX_ATTR_TRIEDALT) != 0)
297
298 typedef struct {
299         dns_adbaddrinfo_t *             addrinfo;
300         fetchctx_t *                    fctx;
301 } dns_valarg_t;
302
303 struct dns_fetch {
304         unsigned int                    magic;
305         fetchctx_t *                    private;
306 };
307
308 #define DNS_FETCH_MAGIC                 ISC_MAGIC('F', 't', 'c', 'h')
309 #define DNS_FETCH_VALID(fetch)          ISC_MAGIC_VALID(fetch, DNS_FETCH_MAGIC)
310
311 typedef struct fctxbucket {
312         isc_task_t *                    task;
313         isc_mutex_t                     lock;
314         ISC_LIST(fetchctx_t)            fctxs;
315         isc_boolean_t                   exiting;
316         isc_mem_t *                     mctx;
317 } fctxbucket_t;
318
319 typedef struct alternate {
320         isc_boolean_t                   isaddress;
321         union   {
322                 isc_sockaddr_t          addr;
323                 struct {
324                         dns_name_t      name;
325                         in_port_t       port;
326                 } _n;
327         } _u;
328         ISC_LINK(struct alternate)      link;
329 } alternate_t;
330
331 struct dns_resolver {
332         /* Unlocked. */
333         unsigned int                    magic;
334         isc_mem_t *                     mctx;
335         isc_mutex_t                     lock;
336         isc_mutex_t                     nlock;
337         isc_mutex_t                     primelock;
338         dns_rdataclass_t                rdclass;
339         isc_socketmgr_t *               socketmgr;
340         isc_timermgr_t *                timermgr;
341         isc_taskmgr_t *                 taskmgr;
342         dns_view_t *                    view;
343         isc_boolean_t                   frozen;
344         unsigned int                    options;
345         dns_dispatchmgr_t *             dispatchmgr;
346         dns_dispatch_t *                dispatchv4;
347         isc_boolean_t                   exclusivev4;
348         dns_dispatch_t *                dispatchv6;
349         isc_boolean_t                   exclusivev6;
350         unsigned int                    ndisps;
351         unsigned int                    nbuckets;
352         fctxbucket_t *                  buckets;
353         isc_uint32_t                    lame_ttl;
354         ISC_LIST(alternate_t)           alternates;
355         isc_uint16_t                    udpsize;
356 #if USE_ALGLOCK
357         isc_rwlock_t                    alglock;
358 #endif
359         dns_rbt_t *                     algorithms;
360 #if USE_MBSLOCK
361         isc_rwlock_t                    mbslock;
362 #endif
363         dns_rbt_t *                     mustbesecure;
364         unsigned int                    spillatmax;
365         unsigned int                    spillatmin;
366         isc_timer_t *                   spillattimer;
367         isc_boolean_t                   zero_no_soa_ttl;
368
369         /* Locked by lock. */
370         unsigned int                    references;
371         isc_boolean_t                   exiting;
372         isc_eventlist_t                 whenshutdown;
373         unsigned int                    activebuckets;
374         isc_boolean_t                   priming;
375         unsigned int                    spillat;        /* clients-per-query */
376         unsigned int                    nextdisp;
377         /* Locked by primelock. */
378         dns_fetch_t *                   primefetch;
379         /* Locked by nlock. */
380         unsigned int                    nfctx;
381 };
382
383 #define RES_MAGIC                       ISC_MAGIC('R', 'e', 's', '!')
384 #define VALID_RESOLVER(res)             ISC_MAGIC_VALID(res, RES_MAGIC)
385
386 /*%
387  * Private addrinfo flags.  These must not conflict with DNS_FETCHOPT_NOEDNS0,
388  * which we also use as an addrinfo flag.
389  */
390 #define FCTX_ADDRINFO_MARK              0x0001
391 #define FCTX_ADDRINFO_FORWARDER         0x1000
392 #define UNMARKED(a)                     (((a)->flags & FCTX_ADDRINFO_MARK) \
393                                          == 0)
394 #define ISFORWARDER(a)                  (((a)->flags & \
395                                          FCTX_ADDRINFO_FORWARDER) != 0)
396
397 #define NXDOMAIN(r) (((r)->attributes & DNS_RDATASETATTR_NXDOMAIN) != 0)
398
399 static void destroy(dns_resolver_t *res);
400 static void empty_bucket(dns_resolver_t *res);
401 static isc_result_t resquery_send(resquery_t *query);
402 static void resquery_response(isc_task_t *task, isc_event_t *event);
403 static void resquery_connected(isc_task_t *task, isc_event_t *event);
404 static void fctx_try(fetchctx_t *fctx, isc_boolean_t retrying);
405 static isc_boolean_t fctx_destroy(fetchctx_t *fctx);
406 static isc_result_t ncache_adderesult(dns_message_t *message,
407                                       dns_db_t *cache, dns_dbnode_t *node,
408                                       dns_rdatatype_t covers,
409                                       isc_stdtime_t now, dns_ttl_t maxttl,
410                                       dns_rdataset_t *ardataset,
411                                       isc_result_t *eresultp);
412 static void validated(isc_task_t *task, isc_event_t *event);
413 static void maybe_destroy(fetchctx_t *fctx);
414 static void add_bad(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
415                     isc_result_t reason, badnstype_t badtype);
416
417 /*%
418  * Increment resolver-related statistics counters.
419  */
420 static inline void
421 inc_stats(dns_resolver_t *res, isc_statscounter_t counter) {
422         if (res->view->resstats != NULL)
423                 isc_stats_increment(res->view->resstats, counter);
424 }
425
426 static isc_result_t
427 valcreate(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, dns_name_t *name,
428           dns_rdatatype_t type, dns_rdataset_t *rdataset,
429           dns_rdataset_t *sigrdataset, unsigned int valoptions,
430           isc_task_t *task)
431 {
432         dns_validator_t *validator = NULL;
433         dns_valarg_t *valarg;
434         isc_result_t result;
435
436         valarg = isc_mem_get(fctx->res->buckets[fctx->bucketnum].mctx,
437                              sizeof(*valarg));
438         if (valarg == NULL)
439                 return (ISC_R_NOMEMORY);
440
441         valarg->fctx = fctx;
442         valarg->addrinfo = addrinfo;
443
444         if (!ISC_LIST_EMPTY(fctx->validators))
445                 INSIST((valoptions & DNS_VALIDATOR_DEFER) != 0);
446
447         result = dns_validator_create(fctx->res->view, name, type, rdataset,
448                                       sigrdataset, fctx->rmessage,
449                                       valoptions, task, validated, valarg,
450                                       &validator);
451         if (result == ISC_R_SUCCESS) {
452                 inc_stats(fctx->res, dns_resstatscounter_val);
453                 if ((valoptions & DNS_VALIDATOR_DEFER) == 0) {
454                         INSIST(fctx->validator == NULL);
455                         fctx->validator  = validator;
456                 }
457                 ISC_LIST_APPEND(fctx->validators, validator, link);
458         } else
459                 isc_mem_put(fctx->res->buckets[fctx->bucketnum].mctx,
460                             valarg, sizeof(*valarg));
461         return (result);
462 }
463
464 static isc_boolean_t
465 rrsig_fromchildzone(fetchctx_t *fctx, dns_rdataset_t *rdataset) {
466         dns_namereln_t namereln;
467         dns_rdata_rrsig_t rrsig;
468         dns_rdata_t rdata = DNS_RDATA_INIT;
469         int order;
470         isc_result_t result;
471         unsigned int labels;
472
473         for (result = dns_rdataset_first(rdataset);
474              result == ISC_R_SUCCESS;
475              result = dns_rdataset_next(rdataset)) {
476                 dns_rdataset_current(rdataset, &rdata);
477                 result = dns_rdata_tostruct(&rdata, &rrsig, NULL);
478                 RUNTIME_CHECK(result == ISC_R_SUCCESS);
479                 namereln = dns_name_fullcompare(&rrsig.signer, &fctx->domain,
480                                                 &order, &labels);
481                 if (namereln == dns_namereln_subdomain)
482                         return (ISC_TRUE);
483                 dns_rdata_reset(&rdata);
484         }
485         return (ISC_FALSE);
486 }
487
488 static isc_boolean_t
489 fix_mustbedelegationornxdomain(dns_message_t *message, fetchctx_t *fctx) {
490         dns_name_t *name;
491         dns_name_t *domain = &fctx->domain;
492         dns_rdataset_t *rdataset;
493         dns_rdatatype_t type;
494         isc_result_t result;
495         isc_boolean_t keep_auth = ISC_FALSE;
496
497         if (message->rcode == dns_rcode_nxdomain)
498                 return (ISC_FALSE);
499
500         /*
501          * A DS RRset can appear anywhere in a zone, even for a delegation-only
502          * zone.  So a response to an explicit query for this type should be
503          * excluded from delegation-only fixup.
504          *
505          * SOA, NS, and DNSKEY can only exist at a zone apex, so a postive
506          * response to a query for these types can never violate the
507          * delegation-only assumption: if the query name is below a
508          * zone cut, the response should normally be a referral, which should
509          * be accepted; if the query name is below a zone cut but the server
510          * happens to have authority for the zone of the query name, the
511          * response is a (non-referral) answer.  But this does not violate
512          * delegation-only because the query name must be in a different zone
513          * due to the "apex-only" nature of these types.  Note that if the
514          * remote server happens to have authority for a child zone of a
515          * delegation-only zone, we may still incorrectly "fix" the response
516          * with NXDOMAIN for queries for other types.  Unfortunately it's
517          * generally impossible to differentiate this case from violation of
518          * the delegation-only assumption.  Once the resolver learns the
519          * correct zone cut, possibly via a separate query for an "apex-only"
520          * type, queries for other types will be resolved correctly.
521          *
522          * A query for type ANY will be accepted if it hits an exceptional
523          * type above in the answer section as it should be from a child
524          * zone.
525          *
526          * Also accept answers with RRSIG records from the child zone.
527          * Direct queries for RRSIG records should not be answered from
528          * the parent zone.
529          */
530
531         if (message->counts[DNS_SECTION_ANSWER] != 0 &&
532             (fctx->type == dns_rdatatype_ns ||
533              fctx->type == dns_rdatatype_ds ||
534              fctx->type == dns_rdatatype_soa ||
535              fctx->type == dns_rdatatype_any ||
536              fctx->type == dns_rdatatype_rrsig ||
537              fctx->type == dns_rdatatype_dnskey)) {
538                 result = dns_message_firstname(message, DNS_SECTION_ANSWER);
539                 while (result == ISC_R_SUCCESS) {
540                         name = NULL;
541                         dns_message_currentname(message, DNS_SECTION_ANSWER,
542                                                 &name);
543                         for (rdataset = ISC_LIST_HEAD(name->list);
544                              rdataset != NULL;
545                              rdataset = ISC_LIST_NEXT(rdataset, link)) {
546                                 if (!dns_name_equal(name, &fctx->name))
547                                         continue;
548                                 type = rdataset->type;
549                                 /*
550                                  * RRsig from child?
551                                  */
552                                 if (type == dns_rdatatype_rrsig &&
553                                     rrsig_fromchildzone(fctx, rdataset))
554                                         return (ISC_FALSE);
555                                 /*
556                                  * Direct query for apex records or DS.
557                                  */
558                                 if (fctx->type == type &&
559                                     (type == dns_rdatatype_ds ||
560                                      type == dns_rdatatype_ns ||
561                                      type == dns_rdatatype_soa ||
562                                      type == dns_rdatatype_dnskey))
563                                         return (ISC_FALSE);
564                                 /*
565                                  * Indirect query for apex records or DS.
566                                  */
567                                 if (fctx->type == dns_rdatatype_any &&
568                                     (type == dns_rdatatype_ns ||
569                                      type == dns_rdatatype_ds ||
570                                      type == dns_rdatatype_soa ||
571                                      type == dns_rdatatype_dnskey))
572                                         return (ISC_FALSE);
573                         }
574                         result = dns_message_nextname(message,
575                                                       DNS_SECTION_ANSWER);
576                 }
577         }
578
579         /*
580          * A NODATA response to a DS query?
581          */
582         if (fctx->type == dns_rdatatype_ds &&
583             message->counts[DNS_SECTION_ANSWER] == 0)
584                 return (ISC_FALSE);
585
586         /* Look for referral or indication of answer from child zone? */
587         if (message->counts[DNS_SECTION_AUTHORITY] == 0)
588                 goto munge;
589
590         result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
591         while (result == ISC_R_SUCCESS) {
592                 name = NULL;
593                 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
594                 for (rdataset = ISC_LIST_HEAD(name->list);
595                      rdataset != NULL;
596                      rdataset = ISC_LIST_NEXT(rdataset, link)) {
597                         type = rdataset->type;
598                         if (type == dns_rdatatype_soa &&
599                             dns_name_equal(name, domain))
600                                 keep_auth = ISC_TRUE;
601
602                         if (type != dns_rdatatype_ns &&
603                             type != dns_rdatatype_soa &&
604                             type != dns_rdatatype_rrsig)
605                                 continue;
606
607                         if (type == dns_rdatatype_rrsig) {
608                                 if (rrsig_fromchildzone(fctx, rdataset))
609                                         return (ISC_FALSE);
610                                 else
611                                         continue;
612                         }
613
614                         /* NS or SOA records. */
615                         if (dns_name_equal(name, domain)) {
616                                 /*
617                                  * If a query for ANY causes a negative
618                                  * response, we can be sure that this is
619                                  * an empty node.  For other type of queries
620                                  * we cannot differentiate an empty node
621                                  * from a node that just doesn't have that
622                                  * type of record.  We only accept the former
623                                  * case.
624                                  */
625                                 if (message->counts[DNS_SECTION_ANSWER] == 0 &&
626                                     fctx->type == dns_rdatatype_any)
627                                         return (ISC_FALSE);
628                         } else if (dns_name_issubdomain(name, domain)) {
629                                 /* Referral or answer from child zone. */
630                                 return (ISC_FALSE);
631                         }
632                 }
633                 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
634         }
635
636  munge:
637         message->rcode = dns_rcode_nxdomain;
638         message->counts[DNS_SECTION_ANSWER] = 0;
639         if (!keep_auth)
640                 message->counts[DNS_SECTION_AUTHORITY] = 0;
641         message->counts[DNS_SECTION_ADDITIONAL] = 0;
642         return (ISC_TRUE);
643 }
644
645 static inline isc_result_t
646 fctx_starttimer(fetchctx_t *fctx) {
647         /*
648          * Start the lifetime timer for fctx.
649          *
650          * This is also used for stopping the idle timer; in that
651          * case we must purge events already posted to ensure that
652          * no further idle events are delivered.
653          */
654         return (isc_timer_reset(fctx->timer, isc_timertype_once,
655                                 &fctx->expires, NULL, ISC_TRUE));
656 }
657
658 static inline void
659 fctx_stoptimer(fetchctx_t *fctx) {
660         isc_result_t result;
661
662         /*
663          * We don't return a result if resetting the timer to inactive fails
664          * since there's nothing to be done about it.  Resetting to inactive
665          * should never fail anyway, since the code as currently written
666          * cannot fail in that case.
667          */
668         result = isc_timer_reset(fctx->timer, isc_timertype_inactive,
669                                   NULL, NULL, ISC_TRUE);
670         if (result != ISC_R_SUCCESS) {
671                 UNEXPECTED_ERROR(__FILE__, __LINE__,
672                                  "isc_timer_reset(): %s",
673                                  isc_result_totext(result));
674         }
675 }
676
677
678 static inline isc_result_t
679 fctx_startidletimer(fetchctx_t *fctx, isc_interval_t *interval) {
680         /*
681          * Start the idle timer for fctx.  The lifetime timer continues
682          * to be in effect.
683          */
684         return (isc_timer_reset(fctx->timer, isc_timertype_once,
685                                 &fctx->expires, interval, ISC_FALSE));
686 }
687
688 /*
689  * Stopping the idle timer is equivalent to calling fctx_starttimer(), but
690  * we use fctx_stopidletimer for readability in the code below.
691  */
692 #define fctx_stopidletimer      fctx_starttimer
693
694
695 static inline void
696 resquery_destroy(resquery_t **queryp) {
697         resquery_t *query;
698
699         REQUIRE(queryp != NULL);
700         query = *queryp;
701         REQUIRE(!ISC_LINK_LINKED(query, link));
702
703         INSIST(query->tcpsocket == NULL);
704
705         query->fctx->nqueries--;
706         if (SHUTTINGDOWN(query->fctx))
707                 maybe_destroy(query->fctx);     /* Locks bucket. */
708         query->magic = 0;
709         isc_mem_put(query->mctx, query, sizeof(*query));
710         *queryp = NULL;
711 }
712
713 static void
714 fctx_cancelquery(resquery_t **queryp, dns_dispatchevent_t **deventp,
715                  isc_time_t *finish, isc_boolean_t no_response)
716 {
717         fetchctx_t *fctx;
718         resquery_t *query;
719         unsigned int rtt, rttms;
720         unsigned int factor;
721         dns_adbfind_t *find;
722         dns_adbaddrinfo_t *addrinfo;
723         isc_socket_t *socket;
724
725         query = *queryp;
726         fctx = query->fctx;
727
728         FCTXTRACE("cancelquery");
729
730         REQUIRE(!RESQUERY_CANCELED(query));
731
732         query->attributes |= RESQUERY_ATTR_CANCELED;
733
734         /*
735          * Should we update the RTT?
736          */
737         if (finish != NULL || no_response) {
738                 if (finish != NULL) {
739                         /*
740                          * We have both the start and finish times for this
741                          * packet, so we can compute a real RTT.
742                          */
743                         rtt = (unsigned int)isc_time_microdiff(finish,
744                                                                &query->start);
745                         factor = DNS_ADB_RTTADJDEFAULT;
746
747                         rttms = rtt / 1000;
748                         if (rttms < DNS_RESOLVER_QRYRTTCLASS0) {
749                                 inc_stats(fctx->res,
750                                           dns_resstatscounter_queryrtt0);
751                         } else if (rttms < DNS_RESOLVER_QRYRTTCLASS1) {
752                                 inc_stats(fctx->res,
753                                           dns_resstatscounter_queryrtt1);
754                         } else if (rttms < DNS_RESOLVER_QRYRTTCLASS2) {
755                                 inc_stats(fctx->res,
756                                           dns_resstatscounter_queryrtt2);
757                         } else if (rttms < DNS_RESOLVER_QRYRTTCLASS3) {
758                                 inc_stats(fctx->res,
759                                           dns_resstatscounter_queryrtt3);
760                         } else if (rttms < DNS_RESOLVER_QRYRTTCLASS4) {
761                                 inc_stats(fctx->res,
762                                           dns_resstatscounter_queryrtt4);
763                         } else {
764                                 inc_stats(fctx->res,
765                                           dns_resstatscounter_queryrtt5);
766                         }
767                 } else {
768                         /*
769                          * We don't have an RTT for this query.  Maybe the
770                          * packet was lost, or maybe this server is very
771                          * slow.  We don't know.  Increase the RTT.
772                          */
773                         INSIST(no_response);
774                         rtt = query->addrinfo->srtt + 200000;
775                         if (rtt > 10000000)
776                                 rtt = 10000000;
777                         /*
778                          * Replace the current RTT with our value.
779                          */
780                         factor = DNS_ADB_RTTADJREPLACE;
781                 }
782                 dns_adb_adjustsrtt(fctx->adb, query->addrinfo, rtt, factor);
783         }
784
785         /*
786          * Age RTTs of servers not tried.
787          */
788         factor = DNS_ADB_RTTADJAGE;
789         if (finish != NULL)
790                 for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
791                      addrinfo != NULL;
792                      addrinfo = ISC_LIST_NEXT(addrinfo, publink))
793                         if (UNMARKED(addrinfo))
794                                 dns_adb_adjustsrtt(fctx->adb, addrinfo,
795                                                    0, factor);
796
797         if (finish != NULL && TRIEDFIND(fctx))
798                 for (find = ISC_LIST_HEAD(fctx->finds);
799                      find != NULL;
800                      find = ISC_LIST_NEXT(find, publink))
801                         for (addrinfo = ISC_LIST_HEAD(find->list);
802                              addrinfo != NULL;
803                              addrinfo = ISC_LIST_NEXT(addrinfo, publink))
804                                 if (UNMARKED(addrinfo))
805                                         dns_adb_adjustsrtt(fctx->adb, addrinfo,
806                                                            0, factor);
807
808         if (finish != NULL && TRIEDALT(fctx)) {
809                 for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
810                      addrinfo != NULL;
811                      addrinfo = ISC_LIST_NEXT(addrinfo, publink))
812                         if (UNMARKED(addrinfo))
813                                 dns_adb_adjustsrtt(fctx->adb, addrinfo,
814                                                    0, factor);
815                 for (find = ISC_LIST_HEAD(fctx->altfinds);
816                      find != NULL;
817                      find = ISC_LIST_NEXT(find, publink))
818                         for (addrinfo = ISC_LIST_HEAD(find->list);
819                              addrinfo != NULL;
820                              addrinfo = ISC_LIST_NEXT(addrinfo, publink))
821                                 if (UNMARKED(addrinfo))
822                                         dns_adb_adjustsrtt(fctx->adb, addrinfo,
823                                                            0, factor);
824         }
825
826         /*
827          * Check for any outstanding socket events.  If they exist, cancel
828          * them and let the event handlers finish the cleanup.  The resolver
829          * only needs to worry about managing the connect and send events;
830          * the dispatcher manages the recv events.
831          */
832         if (RESQUERY_CONNECTING(query)) {
833                 /*
834                  * Cancel the connect.
835                  */
836                 if (query->tcpsocket != NULL) {
837                         isc_socket_cancel(query->tcpsocket, NULL,
838                                           ISC_SOCKCANCEL_CONNECT);
839                 } else if (query->dispentry != NULL) {
840                         INSIST(query->exclusivesocket);
841                         socket = dns_dispatch_getentrysocket(query->dispentry);
842                         if (socket != NULL)
843                                 isc_socket_cancel(socket, NULL,
844                                                   ISC_SOCKCANCEL_CONNECT);
845                 }
846         } else if (RESQUERY_SENDING(query)) {
847                 /*
848                  * Cancel the pending send.
849                  */
850                 if (query->exclusivesocket && query->dispentry != NULL)
851                         socket = dns_dispatch_getentrysocket(query->dispentry);
852                 else
853                         socket = dns_dispatch_getsocket(query->dispatch);
854                 if (socket != NULL)
855                         isc_socket_cancel(socket, NULL, ISC_SOCKCANCEL_SEND);
856         }
857
858         if (query->dispentry != NULL)
859                 dns_dispatch_removeresponse(&query->dispentry, deventp);
860
861         ISC_LIST_UNLINK(fctx->queries, query, link);
862
863         if (query->tsig != NULL)
864                 isc_buffer_free(&query->tsig);
865
866         if (query->tsigkey != NULL)
867                 dns_tsigkey_detach(&query->tsigkey);
868
869         if (query->dispatch != NULL)
870                 dns_dispatch_detach(&query->dispatch);
871
872         if (! (RESQUERY_CONNECTING(query) || RESQUERY_SENDING(query)))
873                 /*
874                  * It's safe to destroy the query now.
875                  */
876                 resquery_destroy(&query);
877 }
878
879 static void
880 fctx_cancelqueries(fetchctx_t *fctx, isc_boolean_t no_response) {
881         resquery_t *query, *next_query;
882
883         FCTXTRACE("cancelqueries");
884
885         for (query = ISC_LIST_HEAD(fctx->queries);
886              query != NULL;
887              query = next_query) {
888                 next_query = ISC_LIST_NEXT(query, link);
889                 fctx_cancelquery(&query, NULL, NULL, no_response);
890         }
891 }
892
893 static void
894 fctx_cleanupfinds(fetchctx_t *fctx) {
895         dns_adbfind_t *find, *next_find;
896
897         REQUIRE(ISC_LIST_EMPTY(fctx->queries));
898
899         for (find = ISC_LIST_HEAD(fctx->finds);
900              find != NULL;
901              find = next_find) {
902                 next_find = ISC_LIST_NEXT(find, publink);
903                 ISC_LIST_UNLINK(fctx->finds, find, publink);
904                 dns_adb_destroyfind(&find);
905         }
906         fctx->find = NULL;
907 }
908
909 static void
910 fctx_cleanupaltfinds(fetchctx_t *fctx) {
911         dns_adbfind_t *find, *next_find;
912
913         REQUIRE(ISC_LIST_EMPTY(fctx->queries));
914
915         for (find = ISC_LIST_HEAD(fctx->altfinds);
916              find != NULL;
917              find = next_find) {
918                 next_find = ISC_LIST_NEXT(find, publink);
919                 ISC_LIST_UNLINK(fctx->altfinds, find, publink);
920                 dns_adb_destroyfind(&find);
921         }
922         fctx->altfind = NULL;
923 }
924
925 static void
926 fctx_cleanupforwaddrs(fetchctx_t *fctx) {
927         dns_adbaddrinfo_t *addr, *next_addr;
928
929         REQUIRE(ISC_LIST_EMPTY(fctx->queries));
930
931         for (addr = ISC_LIST_HEAD(fctx->forwaddrs);
932              addr != NULL;
933              addr = next_addr) {
934                 next_addr = ISC_LIST_NEXT(addr, publink);
935                 ISC_LIST_UNLINK(fctx->forwaddrs, addr, publink);
936                 dns_adb_freeaddrinfo(fctx->adb, &addr);
937         }
938 }
939
940 static void
941 fctx_cleanupaltaddrs(fetchctx_t *fctx) {
942         dns_adbaddrinfo_t *addr, *next_addr;
943
944         REQUIRE(ISC_LIST_EMPTY(fctx->queries));
945
946         for (addr = ISC_LIST_HEAD(fctx->altaddrs);
947              addr != NULL;
948              addr = next_addr) {
949                 next_addr = ISC_LIST_NEXT(addr, publink);
950                 ISC_LIST_UNLINK(fctx->altaddrs, addr, publink);
951                 dns_adb_freeaddrinfo(fctx->adb, &addr);
952         }
953 }
954
955 static inline void
956 fctx_stopeverything(fetchctx_t *fctx, isc_boolean_t no_response) {
957         FCTXTRACE("stopeverything");
958         fctx_cancelqueries(fctx, no_response);
959         fctx_cleanupfinds(fctx);
960         fctx_cleanupaltfinds(fctx);
961         fctx_cleanupforwaddrs(fctx);
962         fctx_cleanupaltaddrs(fctx);
963         fctx_stoptimer(fctx);
964 }
965
966 static inline void
967 fctx_sendevents(fetchctx_t *fctx, isc_result_t result, int line) {
968         dns_fetchevent_t *event, *next_event;
969         isc_task_t *task;
970         unsigned int count = 0;
971         isc_interval_t i;
972         isc_boolean_t logit = ISC_FALSE;
973         isc_time_t now;
974         unsigned int old_spillat;
975         unsigned int new_spillat = 0;   /* initialized to silence
976                                            compiler warnings */
977
978         /*
979          * Caller must be holding the appropriate bucket lock.
980          */
981         REQUIRE(fctx->state == fetchstate_done);
982
983         FCTXTRACE("sendevents");
984
985         /*
986          * Keep some record of fetch result for logging later (if required).
987          */
988         fctx->result = result;
989         fctx->exitline = line;
990         TIME_NOW(&now);
991         fctx->duration = isc_time_microdiff(&now, &fctx->start);
992
993         for (event = ISC_LIST_HEAD(fctx->events);
994              event != NULL;
995              event = next_event) {
996                 next_event = ISC_LIST_NEXT(event, ev_link);
997                 ISC_LIST_UNLINK(fctx->events, event, ev_link);
998                 task = event->ev_sender;
999                 event->ev_sender = fctx;
1000                 if (!HAVE_ANSWER(fctx))
1001                         event->result = result;
1002
1003                 INSIST(result != ISC_R_SUCCESS ||
1004                        dns_rdataset_isassociated(event->rdataset) ||
1005                        fctx->type == dns_rdatatype_any ||
1006                        fctx->type == dns_rdatatype_rrsig ||
1007                        fctx->type == dns_rdatatype_sig);
1008
1009                 /*
1010                  * Negative results must be indicated in event->result.
1011                  */
1012                 if (dns_rdataset_isassociated(event->rdataset) &&
1013                     event->rdataset->type == dns_rdatatype_none) {
1014                         INSIST(event->result == DNS_R_NCACHENXDOMAIN ||
1015                                event->result == DNS_R_NCACHENXRRSET);
1016                 }
1017
1018                 isc_task_sendanddetach(&task, ISC_EVENT_PTR(&event));
1019                 count++;
1020         }
1021
1022         if ((fctx->attributes & FCTX_ATTR_HAVEANSWER) != 0 &&
1023             fctx->spilled &&
1024             (count < fctx->res->spillatmax || fctx->res->spillatmax == 0)) {
1025                 LOCK(&fctx->res->lock);
1026                 if (count == fctx->res->spillat && !fctx->res->exiting) {
1027                         old_spillat = fctx->res->spillat;
1028                         fctx->res->spillat += 5;
1029                         if (fctx->res->spillat > fctx->res->spillatmax &&
1030                             fctx->res->spillatmax != 0)
1031                                 fctx->res->spillat = fctx->res->spillatmax;
1032                         new_spillat = fctx->res->spillat;
1033                         if (new_spillat != old_spillat) {
1034                                 logit = ISC_TRUE;
1035                         }
1036                         isc_interval_set(&i, 20 * 60, 0);
1037                         result = isc_timer_reset(fctx->res->spillattimer,
1038                                                  isc_timertype_ticker, NULL,
1039                                                  &i, ISC_TRUE);
1040                         RUNTIME_CHECK(result == ISC_R_SUCCESS);
1041                 }
1042                 UNLOCK(&fctx->res->lock);
1043                 if (logit)
1044                         isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
1045                                       DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
1046                                       "clients-per-query increased to %u",
1047                                       new_spillat);
1048         }
1049 }
1050
1051 static inline void
1052 log_edns(fetchctx_t *fctx) {
1053         char domainbuf[DNS_NAME_FORMATSIZE];
1054
1055         if (fctx->reason == NULL)
1056                 return;
1057
1058         dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
1059         isc_log_write(dns_lctx, DNS_LOGCATEGORY_EDNS_DISABLED,
1060                       DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
1061                       "too many timeouts resolving '%s' (in '%s'?): %s",
1062                       fctx->info, domainbuf, fctx->reason);
1063
1064         fctx->reason = NULL;
1065 }
1066
1067 static void
1068 fctx_done(fetchctx_t *fctx, isc_result_t result, int line) {
1069         dns_resolver_t *res;
1070         isc_boolean_t no_response;
1071
1072         REQUIRE(line >= 0);
1073
1074         FCTXTRACE("done");
1075
1076         res = fctx->res;
1077
1078         if (result == ISC_R_SUCCESS) {
1079                 /*%
1080                  * Log any deferred EDNS timeout messages.
1081                  */
1082                 log_edns(fctx);
1083                 no_response = ISC_TRUE;
1084          } else
1085                 no_response = ISC_FALSE;
1086
1087         fctx->reason = NULL;
1088         fctx_stopeverything(fctx, no_response);
1089
1090         LOCK(&res->buckets[fctx->bucketnum].lock);
1091
1092         fctx->state = fetchstate_done;
1093         fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
1094         fctx_sendevents(fctx, result, line);
1095
1096         UNLOCK(&res->buckets[fctx->bucketnum].lock);
1097 }
1098
1099 static void
1100 process_sendevent(resquery_t *query, isc_event_t *event) {
1101         isc_socketevent_t *sevent = (isc_socketevent_t *)event;
1102         isc_boolean_t retry = ISC_FALSE;
1103         isc_result_t result;
1104         fetchctx_t *fctx;
1105
1106         fctx = query->fctx;
1107
1108         if (RESQUERY_CANCELED(query)) {
1109                 if (query->sends == 0 && query->connects == 0) {
1110                         /*
1111                          * This query was canceled while the
1112                          * isc_socket_sendto/connect() was in progress.
1113                          */
1114                         if (query->tcpsocket != NULL)
1115                                 isc_socket_detach(&query->tcpsocket);
1116                         resquery_destroy(&query);
1117                 }
1118         } else {
1119                 switch (sevent->result) {
1120                 case ISC_R_SUCCESS:
1121                         break;
1122
1123                 case ISC_R_HOSTUNREACH:
1124                 case ISC_R_NETUNREACH:
1125                 case ISC_R_NOPERM:
1126                 case ISC_R_ADDRNOTAVAIL:
1127                 case ISC_R_CONNREFUSED:
1128
1129                         /*
1130                          * No route to remote.
1131                          */
1132                         add_bad(fctx, query->addrinfo, sevent->result,
1133                                 badns_unreachable);
1134                         fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
1135                         retry = ISC_TRUE;
1136                         break;
1137
1138                 default:
1139                         fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
1140                         break;
1141                 }
1142         }
1143
1144         isc_event_free(&event);
1145
1146         if (retry) {
1147                 /*
1148                  * Behave as if the idle timer has expired.  For TCP
1149                  * this may not actually reflect the latest timer.
1150                  */
1151                 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
1152                 result = fctx_stopidletimer(fctx);
1153                 if (result != ISC_R_SUCCESS)
1154                         fctx_done(fctx, result, __LINE__);
1155                 else
1156                         fctx_try(fctx, ISC_TRUE);
1157         }
1158 }
1159
1160 static void
1161 resquery_udpconnected(isc_task_t *task, isc_event_t *event) {
1162         resquery_t *query = event->ev_arg;
1163
1164         REQUIRE(event->ev_type == ISC_SOCKEVENT_CONNECT);
1165
1166         QTRACE("udpconnected");
1167
1168         UNUSED(task);
1169
1170         INSIST(RESQUERY_CONNECTING(query));
1171
1172         query->connects--;
1173
1174         process_sendevent(query, event);
1175 }
1176
1177 static void
1178 resquery_senddone(isc_task_t *task, isc_event_t *event) {
1179         resquery_t *query = event->ev_arg;
1180
1181         REQUIRE(event->ev_type == ISC_SOCKEVENT_SENDDONE);
1182
1183         QTRACE("senddone");
1184
1185         /*
1186          * XXXRTH
1187          *
1188          * Currently we don't wait for the senddone event before retrying
1189          * a query.  This means that if we get really behind, we may end
1190          * up doing extra work!
1191          */
1192
1193         UNUSED(task);
1194
1195         INSIST(RESQUERY_SENDING(query));
1196
1197         query->sends--;
1198
1199         process_sendevent(query, event);
1200 }
1201
1202 static inline isc_result_t
1203 fctx_addopt(dns_message_t *message, unsigned int version,
1204             isc_uint16_t udpsize, isc_boolean_t request_nsid)
1205 {
1206         dns_rdataset_t *rdataset;
1207         dns_rdatalist_t *rdatalist;
1208         dns_rdata_t *rdata;
1209         isc_result_t result;
1210
1211         rdatalist = NULL;
1212         result = dns_message_gettemprdatalist(message, &rdatalist);
1213         if (result != ISC_R_SUCCESS)
1214                 return (result);
1215         rdata = NULL;
1216         result = dns_message_gettemprdata(message, &rdata);
1217         if (result != ISC_R_SUCCESS)
1218                 return (result);
1219         rdataset = NULL;
1220         result = dns_message_gettemprdataset(message, &rdataset);
1221         if (result != ISC_R_SUCCESS)
1222                 return (result);
1223         dns_rdataset_init(rdataset);
1224
1225         rdatalist->type = dns_rdatatype_opt;
1226         rdatalist->covers = 0;
1227
1228         /*
1229          * Set Maximum UDP buffer size.
1230          */
1231         rdatalist->rdclass = udpsize;
1232
1233         /*
1234          * Set EXTENDED-RCODE and Z to 0, DO to 1.
1235          */
1236         rdatalist->ttl = (version << 16);
1237         rdatalist->ttl |= DNS_MESSAGEEXTFLAG_DO;
1238
1239         /*
1240          * Set EDNS options if applicable
1241          */
1242         if (request_nsid) {
1243                 /* Send empty NSID option (RFC5001) */
1244                 unsigned char data[4];
1245                 isc_buffer_t buf;
1246
1247                 isc_buffer_init(&buf, data, sizeof(data));
1248                 isc_buffer_putuint16(&buf, DNS_OPT_NSID);
1249                 isc_buffer_putuint16(&buf, 0);
1250                 rdata->data = data;
1251                 rdata->length = sizeof(data);
1252         } else {
1253                 rdata->data = NULL;
1254                 rdata->length = 0;
1255         }
1256
1257         rdata->rdclass = rdatalist->rdclass;
1258         rdata->type = rdatalist->type;
1259         rdata->flags = 0;
1260
1261         ISC_LIST_INIT(rdatalist->rdata);
1262         ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
1263         RUNTIME_CHECK(dns_rdatalist_tordataset(rdatalist, rdataset) == ISC_R_SUCCESS);
1264
1265         return (dns_message_setopt(message, rdataset));
1266 }
1267
1268 static inline void
1269 fctx_setretryinterval(fetchctx_t *fctx, unsigned int rtt) {
1270         unsigned int seconds;
1271         unsigned int us;
1272
1273         /*
1274          * We retry every .8 seconds the first two times through the address
1275          * list, and then we do exponential back-off.
1276          */
1277         if (fctx->restarts < 3)
1278                 us = 800000;
1279         else
1280                 us = (800000 << (fctx->restarts - 2));
1281
1282         /*
1283          * Double the round-trip time.
1284          */
1285         rtt *= 2;
1286
1287         /*
1288          * Always wait for at least the doubled round-trip time.
1289          */
1290         if (us < rtt)
1291                 us = rtt;
1292
1293         /*
1294          * But don't ever wait for more than 10 seconds.
1295          */
1296         if (us > 10000000)
1297                 us = 10000000;
1298
1299         seconds = us / 1000000;
1300         us -= seconds * 1000000;
1301         isc_interval_set(&fctx->interval, seconds, us * 1000);
1302 }
1303
1304 static isc_result_t
1305 fctx_query(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
1306            unsigned int options)
1307 {
1308         dns_resolver_t *res;
1309         isc_task_t *task;
1310         isc_result_t result;
1311         resquery_t *query;
1312         isc_sockaddr_t addr;
1313         isc_boolean_t have_addr = ISC_FALSE;
1314
1315         FCTXTRACE("query");
1316
1317         res = fctx->res;
1318         task = res->buckets[fctx->bucketnum].task;
1319
1320         fctx_setretryinterval(fctx, addrinfo->srtt);
1321         result = fctx_startidletimer(fctx, &fctx->interval);
1322         if (result != ISC_R_SUCCESS)
1323                 return (result);
1324
1325         INSIST(ISC_LIST_EMPTY(fctx->validators));
1326
1327         dns_message_reset(fctx->rmessage, DNS_MESSAGE_INTENTPARSE);
1328
1329         query = isc_mem_get(res->buckets[fctx->bucketnum].mctx,
1330                             sizeof(*query));
1331         if (query == NULL) {
1332                 result = ISC_R_NOMEMORY;
1333                 goto stop_idle_timer;
1334         }
1335         query->mctx = res->buckets[fctx->bucketnum].mctx;
1336         query->options = options;
1337         query->attributes = 0;
1338         query->sends = 0;
1339         query->connects = 0;
1340         /*
1341          * Note that the caller MUST guarantee that 'addrinfo' will remain
1342          * valid until this query is canceled.
1343          */
1344         query->addrinfo = addrinfo;
1345         TIME_NOW(&query->start);
1346
1347         /*
1348          * If this is a TCP query, then we need to make a socket and
1349          * a dispatch for it here.  Otherwise we use the resolver's
1350          * shared dispatch.
1351          */
1352         query->dispatchmgr = res->dispatchmgr;
1353         query->dispatch = NULL;
1354         query->exclusivesocket = ISC_FALSE;
1355         query->tcpsocket = NULL;
1356         if (res->view->peers != NULL) {
1357                 dns_peer_t *peer = NULL;
1358                 isc_netaddr_t dstip;
1359                 isc_netaddr_fromsockaddr(&dstip, &addrinfo->sockaddr);
1360                 result = dns_peerlist_peerbyaddr(res->view->peers,
1361                                                  &dstip, &peer);
1362                 if (result == ISC_R_SUCCESS) {
1363                         result = dns_peer_getquerysource(peer, &addr);
1364                         if (result == ISC_R_SUCCESS)
1365                                 have_addr = ISC_TRUE;
1366                 }
1367         }
1368
1369         if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1370                 int pf;
1371
1372                 pf = isc_sockaddr_pf(&addrinfo->sockaddr);
1373                 if (!have_addr) {
1374                         switch (pf) {
1375                         case PF_INET:
1376                                 result =
1377                                   dns_dispatch_getlocaladdress(res->dispatchv4,
1378                                                                &addr);
1379                                 break;
1380                         case PF_INET6:
1381                                 result =
1382                                   dns_dispatch_getlocaladdress(res->dispatchv6,
1383                                                                &addr);
1384                                 break;
1385                         default:
1386                                 result = ISC_R_NOTIMPLEMENTED;
1387                                 break;
1388                         }
1389                         if (result != ISC_R_SUCCESS)
1390                                 goto cleanup_query;
1391                 }
1392                 isc_sockaddr_setport(&addr, 0);
1393
1394                 result = isc_socket_create(res->socketmgr, pf,
1395                                            isc_sockettype_tcp,
1396                                            &query->tcpsocket);
1397                 if (result != ISC_R_SUCCESS)
1398                         goto cleanup_query;
1399
1400 #ifndef BROKEN_TCP_BIND_BEFORE_CONNECT
1401                 result = isc_socket_bind(query->tcpsocket, &addr, 0);
1402                 if (result != ISC_R_SUCCESS)
1403                         goto cleanup_socket;
1404 #endif
1405
1406                 /*
1407                  * A dispatch will be created once the connect succeeds.
1408                  */
1409         } else {
1410                 if (have_addr) {
1411                         unsigned int attrs, attrmask;
1412                         attrs = DNS_DISPATCHATTR_UDP;
1413                         switch (isc_sockaddr_pf(&addr)) {
1414                         case AF_INET:
1415                                 attrs |= DNS_DISPATCHATTR_IPV4;
1416                                 break;
1417                         case AF_INET6:
1418                                 attrs |= DNS_DISPATCHATTR_IPV6;
1419                                 break;
1420                         default:
1421                                 result = ISC_R_NOTIMPLEMENTED;
1422                                 goto cleanup_query;
1423                         }
1424                         attrmask = DNS_DISPATCHATTR_UDP;
1425                         attrmask |= DNS_DISPATCHATTR_TCP;
1426                         attrmask |= DNS_DISPATCHATTR_IPV4;
1427                         attrmask |= DNS_DISPATCHATTR_IPV6;
1428                         result = dns_dispatch_getudp(res->dispatchmgr,
1429                                                      res->socketmgr,
1430                                                      res->taskmgr, &addr,
1431                                                      4096, 1000, 32768, 16411,
1432                                                      16433, attrs, attrmask,
1433                                                      &query->dispatch);
1434                         if (result != ISC_R_SUCCESS)
1435                                 goto cleanup_query;
1436                 } else {
1437                         switch (isc_sockaddr_pf(&addrinfo->sockaddr)) {
1438                         case PF_INET:
1439                                 dns_dispatch_attach(res->dispatchv4,
1440                                                     &query->dispatch);
1441                                 query->exclusivesocket = res->exclusivev4;
1442                                 break;
1443                         case PF_INET6:
1444                                 dns_dispatch_attach(res->dispatchv6,
1445                                                     &query->dispatch);
1446                                 query->exclusivesocket = res->exclusivev6;
1447                                 break;
1448                         default:
1449                                 result = ISC_R_NOTIMPLEMENTED;
1450                                 goto cleanup_query;
1451                         }
1452                 }
1453                 /*
1454                  * We should always have a valid dispatcher here.  If we
1455                  * don't support a protocol family, then its dispatcher
1456                  * will be NULL, but we shouldn't be finding addresses for
1457                  * protocol types we don't support, so the dispatcher
1458                  * we found should never be NULL.
1459                  */
1460                 INSIST(query->dispatch != NULL);
1461         }
1462
1463         query->dispentry = NULL;
1464         query->fctx = fctx;
1465         query->tsig = NULL;
1466         query->tsigkey = NULL;
1467         ISC_LINK_INIT(query, link);
1468         query->magic = QUERY_MAGIC;
1469
1470         if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1471                 /*
1472                  * Connect to the remote server.
1473                  *
1474                  * XXXRTH  Should we attach to the socket?
1475                  */
1476                 result = isc_socket_connect(query->tcpsocket,
1477                                             &addrinfo->sockaddr, task,
1478                                             resquery_connected, query);
1479                 if (result != ISC_R_SUCCESS)
1480                         goto cleanup_socket;
1481                 query->connects++;
1482                 QTRACE("connecting via TCP");
1483         } else {
1484                 result = resquery_send(query);
1485                 if (result != ISC_R_SUCCESS)
1486                         goto cleanup_dispatch;
1487         }
1488         fctx->querysent++;
1489
1490         ISC_LIST_APPEND(fctx->queries, query, link);
1491         query->fctx->nqueries++;
1492         if (isc_sockaddr_pf(&addrinfo->sockaddr) == PF_INET)
1493                 inc_stats(res, dns_resstatscounter_queryv4);
1494         else
1495                 inc_stats(res, dns_resstatscounter_queryv6);
1496         if (res->view->resquerystats != NULL)
1497                 dns_rdatatypestats_increment(res->view->resquerystats,
1498                                              fctx->type);
1499
1500         return (ISC_R_SUCCESS);
1501
1502  cleanup_socket:
1503         isc_socket_detach(&query->tcpsocket);
1504
1505  cleanup_dispatch:
1506         if (query->dispatch != NULL)
1507                 dns_dispatch_detach(&query->dispatch);
1508
1509  cleanup_query:
1510         query->magic = 0;
1511         isc_mem_put(res->buckets[fctx->bucketnum].mctx,
1512                     query, sizeof(*query));
1513
1514  stop_idle_timer:
1515         RUNTIME_CHECK(fctx_stopidletimer(fctx) == ISC_R_SUCCESS);
1516
1517         return (result);
1518 }
1519
1520 static isc_boolean_t
1521 triededns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1522         isc_sockaddr_t *sa;
1523
1524         for (sa = ISC_LIST_HEAD(fctx->edns);
1525              sa != NULL;
1526              sa = ISC_LIST_NEXT(sa, link)) {
1527                 if (isc_sockaddr_equal(sa, address))
1528                         return (ISC_TRUE);
1529         }
1530
1531         return (ISC_FALSE);
1532 }
1533
1534 static void
1535 add_triededns(fetchctx_t *fctx, isc_sockaddr_t *address) {
1536         isc_sockaddr_t *sa;
1537
1538         if (triededns(fctx, address))
1539                 return;
1540
1541         sa = isc_mem_get(fctx->res->buckets[fctx->bucketnum].mctx,
1542                          sizeof(*sa));
1543         if (sa == NULL)
1544                 return;
1545
1546         *sa = *address;
1547         ISC_LIST_INITANDAPPEND(fctx->edns, sa, link);
1548 }
1549
1550 static isc_boolean_t
1551 triededns512(fetchctx_t *fctx, isc_sockaddr_t *address) {
1552         isc_sockaddr_t *sa;
1553
1554         for (sa = ISC_LIST_HEAD(fctx->edns512);
1555              sa != NULL;
1556              sa = ISC_LIST_NEXT(sa, link)) {
1557                 if (isc_sockaddr_equal(sa, address))
1558                         return (ISC_TRUE);
1559         }
1560
1561         return (ISC_FALSE);
1562 }
1563
1564 static void
1565 add_triededns512(fetchctx_t *fctx, isc_sockaddr_t *address) {
1566         isc_sockaddr_t *sa;
1567
1568         if (triededns512(fctx, address))
1569                 return;
1570
1571         sa = isc_mem_get(fctx->res->buckets[fctx->bucketnum].mctx,
1572                          sizeof(*sa));
1573         if (sa == NULL)
1574                 return;
1575
1576         *sa = *address;
1577         ISC_LIST_INITANDAPPEND(fctx->edns512, sa, link);
1578 }
1579
1580 static isc_result_t
1581 resquery_send(resquery_t *query) {
1582         fetchctx_t *fctx;
1583         isc_result_t result;
1584         dns_name_t *qname = NULL;
1585         dns_rdataset_t *qrdataset = NULL;
1586         isc_region_t r;
1587         dns_resolver_t *res;
1588         isc_task_t *task;
1589         isc_socket_t *socket;
1590         isc_buffer_t tcpbuffer;
1591         isc_sockaddr_t *address;
1592         isc_buffer_t *buffer;
1593         isc_netaddr_t ipaddr;
1594         dns_tsigkey_t *tsigkey = NULL;
1595         dns_peer_t *peer = NULL;
1596         isc_boolean_t useedns;
1597         dns_compress_t cctx;
1598         isc_boolean_t cleanup_cctx = ISC_FALSE;
1599         isc_boolean_t secure_domain;
1600
1601         fctx = query->fctx;
1602         QTRACE("send");
1603
1604         res = fctx->res;
1605         task = res->buckets[fctx->bucketnum].task;
1606         address = NULL;
1607
1608         if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1609                 /*
1610                  * Reserve space for the TCP message length.
1611                  */
1612                 isc_buffer_init(&tcpbuffer, query->data, sizeof(query->data));
1613                 isc_buffer_init(&query->buffer, query->data + 2,
1614                                 sizeof(query->data) - 2);
1615                 buffer = &tcpbuffer;
1616         } else {
1617                 isc_buffer_init(&query->buffer, query->data,
1618                                 sizeof(query->data));
1619                 buffer = &query->buffer;
1620         }
1621
1622         result = dns_message_gettempname(fctx->qmessage, &qname);
1623         if (result != ISC_R_SUCCESS)
1624                 goto cleanup_temps;
1625         result = dns_message_gettemprdataset(fctx->qmessage, &qrdataset);
1626         if (result != ISC_R_SUCCESS)
1627                 goto cleanup_temps;
1628
1629         /*
1630          * Get a query id from the dispatch.
1631          */
1632         result = dns_dispatch_addresponse2(query->dispatch,
1633                                            &query->addrinfo->sockaddr,
1634                                            task,
1635                                            resquery_response,
1636                                            query,
1637                                            &query->id,
1638                                            &query->dispentry,
1639                                            res->socketmgr);
1640         if (result != ISC_R_SUCCESS)
1641                 goto cleanup_temps;
1642
1643         fctx->qmessage->opcode = dns_opcode_query;
1644
1645         /*
1646          * Set up question.
1647          */
1648         dns_name_init(qname, NULL);
1649         dns_name_clone(&fctx->name, qname);
1650         dns_rdataset_init(qrdataset);
1651         dns_rdataset_makequestion(qrdataset, res->rdclass, fctx->type);
1652         ISC_LIST_APPEND(qname->list, qrdataset, link);
1653         dns_message_addname(fctx->qmessage, qname, DNS_SECTION_QUESTION);
1654         qname = NULL;
1655         qrdataset = NULL;
1656
1657         /*
1658          * Set RD if the client has requested that we do a recursive query,
1659          * or if we're sending to a forwarder.
1660          */
1661         if ((query->options & DNS_FETCHOPT_RECURSIVE) != 0 ||
1662             ISFORWARDER(query->addrinfo))
1663                 fctx->qmessage->flags |= DNS_MESSAGEFLAG_RD;
1664
1665         /*
1666          * Set CD if the client says don't validate or the question is
1667          * under a secure entry point.
1668          */
1669         if ((query->options & DNS_FETCHOPT_NOVALIDATE) != 0) {
1670                 fctx->qmessage->flags |= DNS_MESSAGEFLAG_CD;
1671         } else if (res->view->enablevalidation) {
1672                 result = dns_keytable_issecuredomain(res->view->secroots,
1673                                                      &fctx->name,
1674                                                      &secure_domain);
1675                 if (result != ISC_R_SUCCESS)
1676                         secure_domain = ISC_FALSE;
1677                 if (res->view->dlv != NULL)
1678                         secure_domain = ISC_TRUE;
1679                 if (secure_domain)
1680                         fctx->qmessage->flags |= DNS_MESSAGEFLAG_CD;
1681         }
1682
1683         /*
1684          * We don't have to set opcode because it defaults to query.
1685          */
1686         fctx->qmessage->id = query->id;
1687
1688         /*
1689          * Convert the question to wire format.
1690          */
1691         result = dns_compress_init(&cctx, -1, fctx->res->mctx);
1692         if (result != ISC_R_SUCCESS)
1693                 goto cleanup_message;
1694         cleanup_cctx = ISC_TRUE;
1695
1696         result = dns_message_renderbegin(fctx->qmessage, &cctx,
1697                                          &query->buffer);
1698         if (result != ISC_R_SUCCESS)
1699                 goto cleanup_message;
1700
1701         result = dns_message_rendersection(fctx->qmessage,
1702                                            DNS_SECTION_QUESTION, 0);
1703         if (result != ISC_R_SUCCESS)
1704                 goto cleanup_message;
1705
1706         peer = NULL;
1707         isc_netaddr_fromsockaddr(&ipaddr, &query->addrinfo->sockaddr);
1708         (void) dns_peerlist_peerbyaddr(fctx->res->view->peers, &ipaddr, &peer);
1709
1710         /*
1711          * The ADB does not know about servers with "edns no".  Check this,
1712          * and then inform the ADB for future use.
1713          */
1714         if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) == 0 &&
1715             peer != NULL &&
1716             dns_peer_getsupportedns(peer, &useedns) == ISC_R_SUCCESS &&
1717             !useedns)
1718         {
1719                 query->options |= DNS_FETCHOPT_NOEDNS0;
1720                 dns_adb_changeflags(fctx->adb, query->addrinfo,
1721                                     DNS_FETCHOPT_NOEDNS0,
1722                                     DNS_FETCHOPT_NOEDNS0);
1723         }
1724
1725         /* Sync NOEDNS0 flag in addrinfo->flags and options now. */
1726         if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) != 0)
1727                 query->options |= DNS_FETCHOPT_NOEDNS0;
1728
1729         /*
1730          * Handle timeouts by reducing the UDP response size to 512 bytes
1731          * then if that doesn't work disabling EDNS (includes DO) and CD.
1732          *
1733          * These timeout can be due to:
1734          *      * broken nameservers that don't respond to EDNS queries.
1735          *      * broken/misconfigured firewalls and NAT implementations
1736          *        that don't handle IP fragmentation.
1737          *      * broken/misconfigured firewalls that don't handle responses
1738          *        greater than 512 bytes.
1739          *      * broken/misconfigured firewalls that don't handle EDNS, DO
1740          *        or CD.
1741          *      * packet loss / link outage.
1742          */
1743         if (fctx->timeout) {
1744                 if ((triededns512(fctx, &query->addrinfo->sockaddr) ||
1745                      fctx->timeouts >= (MAX_EDNS0_TIMEOUTS * 2)) &&
1746                     (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
1747                         query->options |= DNS_FETCHOPT_NOEDNS0;
1748                         fctx->reason = "disabling EDNS";
1749                 } else if ((triededns(fctx, &query->addrinfo->sockaddr) ||
1750                             fctx->timeouts >= MAX_EDNS0_TIMEOUTS) &&
1751                            (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
1752                         query->options |= DNS_FETCHOPT_EDNS512;
1753                         fctx->reason = "reducing the advertised EDNS UDP "
1754                                        "packet size to 512 octets";
1755                 }
1756                 fctx->timeout = ISC_FALSE;
1757         }
1758
1759         /*
1760          * Use EDNS0, unless the caller doesn't want it, or we know that
1761          * the remote server doesn't like it.
1762          */
1763         if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
1764                 if ((query->addrinfo->flags & DNS_FETCHOPT_NOEDNS0) == 0) {
1765                         unsigned int version = 0;       /* Default version. */
1766                         unsigned int flags;
1767                         isc_uint16_t udpsize = res->udpsize;
1768                         isc_boolean_t reqnsid = res->view->requestnsid;
1769
1770                         flags = query->addrinfo->flags;
1771                         if ((flags & DNS_FETCHOPT_EDNSVERSIONSET) != 0) {
1772                                 version = flags & DNS_FETCHOPT_EDNSVERSIONMASK;
1773                                 version >>= DNS_FETCHOPT_EDNSVERSIONSHIFT;
1774                         }
1775                         if ((query->options & DNS_FETCHOPT_EDNS512) != 0)
1776                                 udpsize = 512;
1777                         else if (peer != NULL)
1778                                 (void)dns_peer_getudpsize(peer, &udpsize);
1779
1780                         /* request NSID for current view or peer? */
1781                         if (peer != NULL)
1782                                 (void) dns_peer_getrequestnsid(peer, &reqnsid);
1783                         result = fctx_addopt(fctx->qmessage, version,
1784                                              udpsize, reqnsid);
1785                         if (reqnsid && result == ISC_R_SUCCESS) {
1786                                 query->options |= DNS_FETCHOPT_WANTNSID;
1787                         } else if (result != ISC_R_SUCCESS) {
1788                                 /*
1789                                  * We couldn't add the OPT, but we'll press on.
1790                                  * We're not using EDNS0, so set the NOEDNS0
1791                                  * bit.
1792                                  */
1793                                 query->options |= DNS_FETCHOPT_NOEDNS0;
1794                         }
1795                 } else {
1796                         /*
1797                          * We know this server doesn't like EDNS0, so we
1798                          * won't use it.  Set the NOEDNS0 bit since we're
1799                          * not using EDNS0.
1800                          */
1801                         query->options |= DNS_FETCHOPT_NOEDNS0;
1802                 }
1803         }
1804
1805         /*
1806          * If we need EDNS0 to do this query and aren't using it, we lose.
1807          */
1808         if (NEEDEDNS0(fctx) && (query->options & DNS_FETCHOPT_NOEDNS0) != 0) {
1809                 result = DNS_R_SERVFAIL;
1810                 goto cleanup_message;
1811         }
1812
1813         if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0)
1814                 add_triededns(fctx, &query->addrinfo->sockaddr);
1815
1816         if ((query->options & DNS_FETCHOPT_EDNS512) != 0)
1817                 add_triededns512(fctx, &query->addrinfo->sockaddr);
1818
1819         /*
1820          * Clear CD if EDNS is not in use.
1821          */
1822         if ((query->options & DNS_FETCHOPT_NOEDNS0) != 0)
1823                 fctx->qmessage->flags &= ~DNS_MESSAGEFLAG_CD;
1824
1825         /*
1826          * Add TSIG record tailored to the current recipient.
1827          */
1828         result = dns_view_getpeertsig(fctx->res->view, &ipaddr, &tsigkey);
1829         if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND)
1830                 goto cleanup_message;
1831
1832         if (tsigkey != NULL) {
1833                 result = dns_message_settsigkey(fctx->qmessage, tsigkey);
1834                 dns_tsigkey_detach(&tsigkey);
1835                 if (result != ISC_R_SUCCESS)
1836                         goto cleanup_message;
1837         }
1838
1839         result = dns_message_rendersection(fctx->qmessage,
1840                                            DNS_SECTION_ADDITIONAL, 0);
1841         if (result != ISC_R_SUCCESS)
1842                 goto cleanup_message;
1843
1844         result = dns_message_renderend(fctx->qmessage);
1845         if (result != ISC_R_SUCCESS)
1846                 goto cleanup_message;
1847
1848         dns_compress_invalidate(&cctx);
1849         cleanup_cctx = ISC_FALSE;
1850
1851         if (dns_message_gettsigkey(fctx->qmessage) != NULL) {
1852                 dns_tsigkey_attach(dns_message_gettsigkey(fctx->qmessage),
1853                                    &query->tsigkey);
1854                 result = dns_message_getquerytsig(fctx->qmessage,
1855                                                   fctx->res->mctx,
1856                                                   &query->tsig);
1857                 if (result != ISC_R_SUCCESS)
1858                         goto cleanup_message;
1859         }
1860
1861         /*
1862          * If using TCP, write the length of the message at the beginning
1863          * of the buffer.
1864          */
1865         if ((query->options & DNS_FETCHOPT_TCP) != 0) {
1866                 isc_buffer_usedregion(&query->buffer, &r);
1867                 isc_buffer_putuint16(&tcpbuffer, (isc_uint16_t)r.length);
1868                 isc_buffer_add(&tcpbuffer, r.length);
1869         }
1870
1871         /*
1872          * We're now done with the query message.
1873          */
1874         dns_message_reset(fctx->qmessage, DNS_MESSAGE_INTENTRENDER);
1875
1876         if (query->exclusivesocket)
1877                 socket = dns_dispatch_getentrysocket(query->dispentry);
1878         else
1879                 socket = dns_dispatch_getsocket(query->dispatch);
1880         /*
1881          * Send the query!
1882          */
1883         if ((query->options & DNS_FETCHOPT_TCP) == 0) {
1884                 address = &query->addrinfo->sockaddr;
1885                 if (query->exclusivesocket) {
1886                         result = isc_socket_connect(socket, address, task,
1887                                                     resquery_udpconnected,
1888                                                     query);
1889                         if (result != ISC_R_SUCCESS)
1890                                 goto cleanup_message;
1891                         query->connects++;
1892                 }
1893         }
1894         isc_buffer_usedregion(buffer, &r);
1895
1896         /*
1897          * XXXRTH  Make sure we don't send to ourselves!  We should probably
1898          *              prune out these addresses when we get them from the ADB.
1899          */
1900         result = isc_socket_sendto(socket, &r, task, resquery_senddone,
1901                                    query, address, NULL);
1902         if (result != ISC_R_SUCCESS)
1903                 goto cleanup_message;
1904
1905         query->sends++;
1906
1907         QTRACE("sent");
1908
1909         return (ISC_R_SUCCESS);
1910
1911  cleanup_message:
1912         if (cleanup_cctx)
1913                 dns_compress_invalidate(&cctx);
1914
1915         dns_message_reset(fctx->qmessage, DNS_MESSAGE_INTENTRENDER);
1916
1917         /*
1918          * Stop the dispatcher from listening.
1919          */
1920         dns_dispatch_removeresponse(&query->dispentry, NULL);
1921
1922  cleanup_temps:
1923         if (qname != NULL)
1924                 dns_message_puttempname(fctx->qmessage, &qname);
1925         if (qrdataset != NULL)
1926                 dns_message_puttemprdataset(fctx->qmessage, &qrdataset);
1927
1928         return (result);
1929 }
1930
1931 static void
1932 resquery_connected(isc_task_t *task, isc_event_t *event) {
1933         isc_socketevent_t *sevent = (isc_socketevent_t *)event;
1934         resquery_t *query = event->ev_arg;
1935         isc_boolean_t retry = ISC_FALSE;
1936         isc_interval_t interval;
1937         isc_result_t result;
1938         unsigned int attrs;
1939         fetchctx_t *fctx;
1940
1941         REQUIRE(event->ev_type == ISC_SOCKEVENT_CONNECT);
1942         REQUIRE(VALID_QUERY(query));
1943
1944         QTRACE("connected");
1945
1946         UNUSED(task);
1947
1948         /*
1949          * XXXRTH
1950          *
1951          * Currently we don't wait for the connect event before retrying
1952          * a query.  This means that if we get really behind, we may end
1953          * up doing extra work!
1954          */
1955
1956         query->connects--;
1957         fctx = query->fctx;
1958
1959         if (RESQUERY_CANCELED(query)) {
1960                 /*
1961                  * This query was canceled while the connect() was in
1962                  * progress.
1963                  */
1964                 isc_socket_detach(&query->tcpsocket);
1965                 resquery_destroy(&query);
1966         } else {
1967                 switch (sevent->result) {
1968                 case ISC_R_SUCCESS:
1969
1970                         /*
1971                          * Extend the idle timer for TCP.  20 seconds
1972                          * should be long enough for a TCP connection to be
1973                          * established, a single DNS request to be sent,
1974                          * and the response received.
1975                          */
1976                         isc_interval_set(&interval, 20, 0);
1977                         result = fctx_startidletimer(query->fctx, &interval);
1978                         if (result != ISC_R_SUCCESS) {
1979                                 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
1980                                 fctx_done(fctx, result, __LINE__);
1981                                 break;
1982                         }
1983                         /*
1984                          * We are connected.  Create a dispatcher and
1985                          * send the query.
1986                          */
1987                         attrs = 0;
1988                         attrs |= DNS_DISPATCHATTR_TCP;
1989                         attrs |= DNS_DISPATCHATTR_PRIVATE;
1990                         attrs |= DNS_DISPATCHATTR_CONNECTED;
1991                         if (isc_sockaddr_pf(&query->addrinfo->sockaddr) ==
1992                             AF_INET)
1993                                 attrs |= DNS_DISPATCHATTR_IPV4;
1994                         else
1995                                 attrs |= DNS_DISPATCHATTR_IPV6;
1996                         attrs |= DNS_DISPATCHATTR_MAKEQUERY;
1997
1998                         result = dns_dispatch_createtcp(query->dispatchmgr,
1999                                                      query->tcpsocket,
2000                                                      query->fctx->res->taskmgr,
2001                                                      4096, 2, 1, 1, 3, attrs,
2002                                                      &query->dispatch);
2003
2004                         /*
2005                          * Regardless of whether dns_dispatch_create()
2006                          * succeeded or not, we don't need our reference
2007                          * to the socket anymore.
2008                          */
2009                         isc_socket_detach(&query->tcpsocket);
2010
2011                         if (result == ISC_R_SUCCESS)
2012                                 result = resquery_send(query);
2013
2014                         if (result != ISC_R_SUCCESS) {
2015                                 fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
2016                                 fctx_done(fctx, result, __LINE__);
2017                         }
2018                         break;
2019
2020                 case ISC_R_NETUNREACH:
2021                 case ISC_R_HOSTUNREACH:
2022                 case ISC_R_CONNREFUSED:
2023                 case ISC_R_NOPERM:
2024                 case ISC_R_ADDRNOTAVAIL:
2025                 case ISC_R_CONNECTIONRESET:
2026                         /*
2027                          * No route to remote.
2028                          */
2029                         isc_socket_detach(&query->tcpsocket);
2030                         fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
2031                         retry = ISC_TRUE;
2032                         break;
2033
2034                 default:
2035                         isc_socket_detach(&query->tcpsocket);
2036                         fctx_cancelquery(&query, NULL, NULL, ISC_FALSE);
2037                         break;
2038                 }
2039         }
2040
2041         isc_event_free(&event);
2042
2043         if (retry) {
2044                 /*
2045                  * Behave as if the idle timer has expired.  For TCP
2046                  * connections this may not actually reflect the latest timer.
2047                  */
2048                 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
2049                 result = fctx_stopidletimer(fctx);
2050                 if (result != ISC_R_SUCCESS)
2051                         fctx_done(fctx, result, __LINE__);
2052                 else
2053                         fctx_try(fctx, ISC_TRUE);
2054         }
2055 }
2056
2057 static void
2058 fctx_finddone(isc_task_t *task, isc_event_t *event) {
2059         fetchctx_t *fctx;
2060         dns_adbfind_t *find;
2061         dns_resolver_t *res;
2062         isc_boolean_t want_try = ISC_FALSE;
2063         isc_boolean_t want_done = ISC_FALSE;
2064         isc_boolean_t bucket_empty = ISC_FALSE;
2065         unsigned int bucketnum;
2066
2067         find = event->ev_sender;
2068         fctx = event->ev_arg;
2069         REQUIRE(VALID_FCTX(fctx));
2070         res = fctx->res;
2071
2072         UNUSED(task);
2073
2074         FCTXTRACE("finddone");
2075
2076         INSIST(fctx->pending > 0);
2077         fctx->pending--;
2078
2079         if (ADDRWAIT(fctx)) {
2080                 /*
2081                  * The fetch is waiting for a name to be found.
2082                  */
2083                 INSIST(!SHUTTINGDOWN(fctx));
2084                 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
2085                 if (event->ev_type == DNS_EVENT_ADBMOREADDRESSES)
2086                         want_try = ISC_TRUE;
2087                 else {
2088                         fctx->findfail++;
2089                         if (fctx->pending == 0) {
2090                                 /*
2091                                  * We've got nothing else to wait for and don't
2092                                  * know the answer.  There's nothing to do but
2093                                  * fail the fctx.
2094                                  */
2095                                 want_done = ISC_TRUE;
2096                         }
2097                 }
2098         } else if (SHUTTINGDOWN(fctx) && fctx->pending == 0 &&
2099                    fctx->nqueries == 0 && ISC_LIST_EMPTY(fctx->validators)) {
2100                 bucketnum = fctx->bucketnum;
2101                 LOCK(&res->buckets[bucketnum].lock);
2102                 /*
2103                  * Note that we had to wait until we had the lock before
2104                  * looking at fctx->references.
2105                  */
2106                 if (fctx->references == 0)
2107                         bucket_empty = fctx_destroy(fctx);
2108                 UNLOCK(&res->buckets[bucketnum].lock);
2109         }
2110
2111         isc_event_free(&event);
2112         dns_adb_destroyfind(&find);
2113
2114         if (want_try)
2115                 fctx_try(fctx, ISC_TRUE);
2116         else if (want_done)
2117                 fctx_done(fctx, ISC_R_FAILURE, __LINE__);
2118         else if (bucket_empty)
2119                 empty_bucket(res);
2120 }
2121
2122
2123 static inline isc_boolean_t
2124 bad_server(fetchctx_t *fctx, isc_sockaddr_t *address) {
2125         isc_sockaddr_t *sa;
2126
2127         for (sa = ISC_LIST_HEAD(fctx->bad);
2128              sa != NULL;
2129              sa = ISC_LIST_NEXT(sa, link)) {
2130                 if (isc_sockaddr_equal(sa, address))
2131                         return (ISC_TRUE);
2132         }
2133
2134         return (ISC_FALSE);
2135 }
2136
2137 static inline isc_boolean_t
2138 mark_bad(fetchctx_t *fctx) {
2139         dns_adbfind_t *curr;
2140         dns_adbaddrinfo_t *addrinfo;
2141         isc_boolean_t all_bad = ISC_TRUE;
2142
2143         /*
2144          * Mark all known bad servers, so we don't try to talk to them
2145          * again.
2146          */
2147
2148         /*
2149          * Mark any bad nameservers.
2150          */
2151         for (curr = ISC_LIST_HEAD(fctx->finds);
2152              curr != NULL;
2153              curr = ISC_LIST_NEXT(curr, publink)) {
2154                 for (addrinfo = ISC_LIST_HEAD(curr->list);
2155                      addrinfo != NULL;
2156                      addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2157                         if (bad_server(fctx, &addrinfo->sockaddr))
2158                                 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2159                         else
2160                                 all_bad = ISC_FALSE;
2161                 }
2162         }
2163
2164         /*
2165          * Mark any bad forwarders.
2166          */
2167         for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
2168              addrinfo != NULL;
2169              addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2170                 if (bad_server(fctx, &addrinfo->sockaddr))
2171                         addrinfo->flags |= FCTX_ADDRINFO_MARK;
2172                 else
2173                         all_bad = ISC_FALSE;
2174         }
2175
2176         /*
2177          * Mark any bad alternates.
2178          */
2179         for (curr = ISC_LIST_HEAD(fctx->altfinds);
2180              curr != NULL;
2181              curr = ISC_LIST_NEXT(curr, publink)) {
2182                 for (addrinfo = ISC_LIST_HEAD(curr->list);
2183                      addrinfo != NULL;
2184                      addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2185                         if (bad_server(fctx, &addrinfo->sockaddr))
2186                                 addrinfo->flags |= FCTX_ADDRINFO_MARK;
2187                         else
2188                                 all_bad = ISC_FALSE;
2189                 }
2190         }
2191
2192         for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
2193              addrinfo != NULL;
2194              addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2195                 if (bad_server(fctx, &addrinfo->sockaddr))
2196                         addrinfo->flags |= FCTX_ADDRINFO_MARK;
2197                 else
2198                         all_bad = ISC_FALSE;
2199         }
2200
2201         return (all_bad);
2202 }
2203
2204 static void
2205 add_bad(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, isc_result_t reason,
2206         badnstype_t badtype)
2207 {
2208         char namebuf[DNS_NAME_FORMATSIZE];
2209         char addrbuf[ISC_SOCKADDR_FORMATSIZE];
2210         char classbuf[64];
2211         char typebuf[64];
2212         char code[64];
2213         isc_buffer_t b;
2214         isc_sockaddr_t *sa;
2215         const char *sep1, *sep2;
2216         isc_sockaddr_t *address = &addrinfo->sockaddr;
2217
2218         if (reason == DNS_R_LAME)
2219                 fctx->lamecount++;
2220         else {
2221                 switch (badtype) {
2222                 case badns_unreachable:
2223                         fctx->neterr++;
2224                         break;
2225                 case badns_response:
2226                         fctx->badresp++;
2227                         break;
2228                 case badns_validation:
2229                         break;  /* counted as 'valfail' */
2230                 }
2231         }
2232
2233         if (bad_server(fctx, address)) {
2234                 /*
2235                  * We already know this server is bad.
2236                  */
2237                 return;
2238         }
2239
2240         FCTXTRACE("add_bad");
2241
2242         sa = isc_mem_get(fctx->res->buckets[fctx->bucketnum].mctx,
2243                          sizeof(*sa));
2244         if (sa == NULL)
2245                 return;
2246         *sa = *address;
2247         ISC_LIST_INITANDAPPEND(fctx->bad, sa, link);
2248
2249         if (reason == DNS_R_LAME)       /* already logged */
2250                 return;
2251
2252         if (reason == DNS_R_UNEXPECTEDRCODE &&
2253             fctx->rmessage->rcode == dns_rcode_servfail &&
2254             ISFORWARDER(addrinfo))
2255                 return;
2256
2257         if (reason == DNS_R_UNEXPECTEDRCODE) {
2258                 isc_buffer_init(&b, code, sizeof(code) - 1);
2259                 dns_rcode_totext(fctx->rmessage->rcode, &b);
2260                 code[isc_buffer_usedlength(&b)] = '\0';
2261                 sep1 = "(";
2262                 sep2 = ") ";
2263         } else if (reason == DNS_R_UNEXPECTEDOPCODE) {
2264                 isc_buffer_init(&b, code, sizeof(code) - 1);
2265                 dns_opcode_totext((dns_opcode_t)fctx->rmessage->opcode, &b);
2266                 code[isc_buffer_usedlength(&b)] = '\0';
2267                 sep1 = "(";
2268                 sep2 = ") ";
2269         } else {
2270                 code[0] = '\0';
2271                 sep1 = "";
2272                 sep2 = "";
2273         }
2274         dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
2275         dns_rdatatype_format(fctx->type, typebuf, sizeof(typebuf));
2276         dns_rdataclass_format(fctx->res->rdclass, classbuf, sizeof(classbuf));
2277         isc_sockaddr_format(address, addrbuf, sizeof(addrbuf));
2278         isc_log_write(dns_lctx, DNS_LOGCATEGORY_LAME_SERVERS,
2279                       DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
2280                       "%s %s%s%sresolving '%s/%s/%s': %s",
2281                       dns_result_totext(reason), sep1, code, sep2,
2282                       namebuf, typebuf, classbuf, addrbuf);
2283 }
2284
2285 static void
2286 sort_adbfind(dns_adbfind_t *find) {
2287         dns_adbaddrinfo_t *best, *curr;
2288         dns_adbaddrinfolist_t sorted;
2289
2290         /*
2291          * Lame N^2 bubble sort.
2292          */
2293
2294         ISC_LIST_INIT(sorted);
2295         while (!ISC_LIST_EMPTY(find->list)) {
2296                 best = ISC_LIST_HEAD(find->list);
2297                 curr = ISC_LIST_NEXT(best, publink);
2298                 while (curr != NULL) {
2299                         if (curr->srtt < best->srtt)
2300                                 best = curr;
2301                         curr = ISC_LIST_NEXT(curr, publink);
2302                 }
2303                 ISC_LIST_UNLINK(find->list, best, publink);
2304                 ISC_LIST_APPEND(sorted, best, publink);
2305         }
2306         find->list = sorted;
2307 }
2308
2309 static void
2310 sort_finds(fetchctx_t *fctx) {
2311         dns_adbfind_t *best, *curr;
2312         dns_adbfindlist_t sorted;
2313         dns_adbaddrinfo_t *addrinfo, *bestaddrinfo;
2314
2315         /*
2316          * Lame N^2 bubble sort.
2317          */
2318
2319         ISC_LIST_INIT(sorted);
2320         while (!ISC_LIST_EMPTY(fctx->finds)) {
2321                 best = ISC_LIST_HEAD(fctx->finds);
2322                 bestaddrinfo = ISC_LIST_HEAD(best->list);
2323                 INSIST(bestaddrinfo != NULL);
2324                 curr = ISC_LIST_NEXT(best, publink);
2325                 while (curr != NULL) {
2326                         addrinfo = ISC_LIST_HEAD(curr->list);
2327                         INSIST(addrinfo != NULL);
2328                         if (addrinfo->srtt < bestaddrinfo->srtt) {
2329                                 best = curr;
2330                                 bestaddrinfo = addrinfo;
2331                         }
2332                         curr = ISC_LIST_NEXT(curr, publink);
2333                 }
2334                 ISC_LIST_UNLINK(fctx->finds, best, publink);
2335                 ISC_LIST_APPEND(sorted, best, publink);
2336         }
2337         fctx->finds = sorted;
2338
2339         ISC_LIST_INIT(sorted);
2340         while (!ISC_LIST_EMPTY(fctx->altfinds)) {
2341                 best = ISC_LIST_HEAD(fctx->altfinds);
2342                 bestaddrinfo = ISC_LIST_HEAD(best->list);
2343                 INSIST(bestaddrinfo != NULL);
2344                 curr = ISC_LIST_NEXT(best, publink);
2345                 while (curr != NULL) {
2346                         addrinfo = ISC_LIST_HEAD(curr->list);
2347                         INSIST(addrinfo != NULL);
2348                         if (addrinfo->srtt < bestaddrinfo->srtt) {
2349                                 best = curr;
2350                                 bestaddrinfo = addrinfo;
2351                         }
2352                         curr = ISC_LIST_NEXT(curr, publink);
2353                 }
2354                 ISC_LIST_UNLINK(fctx->altfinds, best, publink);
2355                 ISC_LIST_APPEND(sorted, best, publink);
2356         }
2357         fctx->altfinds = sorted;
2358 }
2359
2360 static void
2361 findname(fetchctx_t *fctx, dns_name_t *name, in_port_t port,
2362          unsigned int options, unsigned int flags, isc_stdtime_t now,
2363          isc_boolean_t *need_alternate)
2364 {
2365         dns_adbaddrinfo_t *ai;
2366         dns_adbfind_t *find;
2367         dns_resolver_t *res;
2368         isc_boolean_t unshared;
2369         isc_result_t result;
2370
2371         res = fctx->res;
2372         unshared = ISC_TF((fctx->options | DNS_FETCHOPT_UNSHARED) != 0);
2373         /*
2374          * If this name is a subdomain of the query domain, tell
2375          * the ADB to start looking using zone/hint data. This keeps us
2376          * from getting stuck if the nameserver is beneath the zone cut
2377          * and we don't know its address (e.g. because the A record has
2378          * expired).
2379          */
2380         if (dns_name_issubdomain(name, &fctx->domain))
2381                 options |= DNS_ADBFIND_STARTATZONE;
2382         options |= DNS_ADBFIND_GLUEOK;
2383         options |= DNS_ADBFIND_HINTOK;
2384
2385         /*
2386          * See what we know about this address.
2387          */
2388         find = NULL;
2389         result = dns_adb_createfind(fctx->adb,
2390                                     res->buckets[fctx->bucketnum].task,
2391                                     fctx_finddone, fctx, name,
2392                                     &fctx->name, fctx->type,
2393                                     options, now, NULL,
2394                                     res->view->dstport, &find);
2395         if (result != ISC_R_SUCCESS) {
2396                 if (result == DNS_R_ALIAS) {
2397                         /*
2398                          * XXXRTH  Follow the CNAME/DNAME chain?
2399                          */
2400                         dns_adb_destroyfind(&find);
2401                         fctx->adberr++;
2402                 }
2403         } else if (!ISC_LIST_EMPTY(find->list)) {
2404                 /*
2405                  * We have at least some of the addresses for the
2406                  * name.
2407                  */
2408                 INSIST((find->options & DNS_ADBFIND_WANTEVENT) == 0);
2409                 sort_adbfind(find);
2410                 if (flags != 0 || port != 0) {
2411                         for (ai = ISC_LIST_HEAD(find->list);
2412                              ai != NULL;
2413                              ai = ISC_LIST_NEXT(ai, publink)) {
2414                                 ai->flags |= flags;
2415                                 if (port != 0)
2416                                         isc_sockaddr_setport(&ai->sockaddr,
2417                                                              port);
2418                         }
2419                 }
2420                 if ((flags & FCTX_ADDRINFO_FORWARDER) != 0)
2421                         ISC_LIST_APPEND(fctx->altfinds, find, publink);
2422                 else
2423                         ISC_LIST_APPEND(fctx->finds, find, publink);
2424         } else {
2425                 /*
2426                  * We don't know any of the addresses for this
2427                  * name.
2428                  */
2429                 if ((find->options & DNS_ADBFIND_WANTEVENT) != 0) {
2430                         /*
2431                          * We're looking for them and will get an
2432                          * event about it later.
2433                          */
2434                         fctx->pending++;
2435                         /*
2436                          * Bootstrap.
2437                          */
2438                         if (need_alternate != NULL &&
2439                             !*need_alternate && unshared &&
2440                             ((res->dispatchv4 == NULL &&
2441                               find->result_v6 != DNS_R_NXDOMAIN) ||
2442                              (res->dispatchv6 == NULL &&
2443                               find->result_v4 != DNS_R_NXDOMAIN)))
2444                                 *need_alternate = ISC_TRUE;
2445                 } else {
2446                         if ((find->options & DNS_ADBFIND_LAMEPRUNED) != 0)
2447                                 fctx->lamecount++; /* cached lame server */
2448                         else
2449                                 fctx->adberr++; /* unreachable server, etc. */
2450
2451                         /*
2452                          * If we know there are no addresses for
2453                          * the family we are using then try to add
2454                          * an alternative server.
2455                          */
2456                         if (need_alternate != NULL && !*need_alternate &&
2457                             ((res->dispatchv4 == NULL &&
2458                               find->result_v6 == DNS_R_NXRRSET) ||
2459                              (res->dispatchv6 == NULL &&
2460                               find->result_v4 == DNS_R_NXRRSET)))
2461                                 *need_alternate = ISC_TRUE;
2462                         dns_adb_destroyfind(&find);
2463                 }
2464         }
2465 }
2466
2467 static isc_boolean_t
2468 isstrictsubdomain(dns_name_t *name1, dns_name_t *name2) {
2469         int order;
2470         unsigned int nlabels;
2471         dns_namereln_t namereln;
2472
2473         namereln = dns_name_fullcompare(name1, name2, &order, &nlabels);
2474         return (ISC_TF(namereln == dns_namereln_subdomain));
2475 }
2476
2477 static isc_result_t
2478 fctx_getaddresses(fetchctx_t *fctx) {
2479         dns_rdata_t rdata = DNS_RDATA_INIT;
2480         isc_result_t result;
2481         dns_resolver_t *res;
2482         isc_stdtime_t now;
2483         unsigned int stdoptions;
2484         isc_sockaddr_t *sa;
2485         dns_adbaddrinfo_t *ai;
2486         isc_boolean_t all_bad;
2487         dns_rdata_ns_t ns;
2488         isc_boolean_t need_alternate = ISC_FALSE;
2489
2490         FCTXTRACE("getaddresses");
2491
2492         /*
2493          * Don't pound on remote servers.  (Failsafe!)
2494          */
2495         fctx->restarts++;
2496         if (fctx->restarts > 10) {
2497                 FCTXTRACE("too many restarts");
2498                 return (DNS_R_SERVFAIL);
2499         }
2500
2501         res = fctx->res;
2502         stdoptions = 0;         /* Keep compiler happy. */
2503
2504         /*
2505          * Forwarders.
2506          */
2507
2508         INSIST(ISC_LIST_EMPTY(fctx->forwaddrs));
2509         INSIST(ISC_LIST_EMPTY(fctx->altaddrs));
2510
2511         /*
2512          * If this fctx has forwarders, use them; otherwise use any
2513          * selective forwarders specified in the view; otherwise use the
2514          * resolver's forwarders (if any).
2515          */
2516         sa = ISC_LIST_HEAD(fctx->forwarders);
2517         if (sa == NULL) {
2518                 dns_forwarders_t *forwarders = NULL;
2519                 dns_name_t *name = &fctx->name;
2520                 dns_name_t suffix;
2521                 unsigned int labels;
2522                 dns_fixedname_t fixed;
2523                 dns_name_t *domain;
2524
2525                 /*
2526                  * DS records are found in the parent server.
2527                  * Strip label to get the correct forwarder (if any).
2528                  */
2529                 if (dns_rdatatype_atparent(fctx->type) &&
2530                     dns_name_countlabels(name) > 1) {
2531                         dns_name_init(&suffix, NULL);
2532                         labels = dns_name_countlabels(name);
2533                         dns_name_getlabelsequence(name, 1, labels - 1, &suffix);
2534                         name = &suffix;
2535                 }
2536
2537                 dns_fixedname_init(&fixed);
2538                 domain = dns_fixedname_name(&fixed);
2539                 result = dns_fwdtable_find2(fctx->res->view->fwdtable, name,
2540                                             domain, &forwarders);
2541                 if (result == ISC_R_SUCCESS) {
2542                         sa = ISC_LIST_HEAD(forwarders->addrs);
2543                         fctx->fwdpolicy = forwarders->fwdpolicy;
2544                         if (fctx->fwdpolicy == dns_fwdpolicy_only &&
2545                             isstrictsubdomain(domain, &fctx->domain)) {
2546                                 isc_mem_t *mctx;
2547
2548                                 mctx = res->buckets[fctx->bucketnum].mctx;
2549                                 dns_name_free(&fctx->domain, mctx);
2550                                 dns_name_init(&fctx->domain, NULL);
2551                                 result = dns_name_dup(domain, mctx,
2552                                                       &fctx->domain);
2553                                 if (result != ISC_R_SUCCESS)
2554                                         return (result);
2555                         }
2556                 }
2557         }
2558
2559         while (sa != NULL) {
2560                 if ((isc_sockaddr_pf(sa) == AF_INET &&
2561                          fctx->res->dispatchv4 == NULL) ||
2562                     (isc_sockaddr_pf(sa) == AF_INET6 &&
2563                         fctx->res->dispatchv6 == NULL)) {
2564                                 sa = ISC_LIST_NEXT(sa, link);
2565                                 continue;
2566                 }
2567                 ai = NULL;
2568                 result = dns_adb_findaddrinfo(fctx->adb,
2569                                               sa, &ai, 0);  /* XXXMLG */
2570                 if (result == ISC_R_SUCCESS) {
2571                         dns_adbaddrinfo_t *cur;
2572                         ai->flags |= FCTX_ADDRINFO_FORWARDER;
2573                         cur = ISC_LIST_HEAD(fctx->forwaddrs);
2574                         while (cur != NULL && cur->srtt < ai->srtt)
2575                                 cur = ISC_LIST_NEXT(cur, publink);
2576                         if (cur != NULL)
2577                                 ISC_LIST_INSERTBEFORE(fctx->forwaddrs, cur,
2578                                                       ai, publink);
2579                         else
2580                                 ISC_LIST_APPEND(fctx->forwaddrs, ai, publink);
2581                 }
2582                 sa = ISC_LIST_NEXT(sa, link);
2583         }
2584
2585         /*
2586          * If the forwarding policy is "only", we don't need the addresses
2587          * of the nameservers.
2588          */
2589         if (fctx->fwdpolicy == dns_fwdpolicy_only)
2590                 goto out;
2591
2592         /*
2593          * Normal nameservers.
2594          */
2595
2596         stdoptions = DNS_ADBFIND_WANTEVENT | DNS_ADBFIND_EMPTYEVENT;
2597         if (fctx->restarts == 1) {
2598                 /*
2599                  * To avoid sending out a flood of queries likely to
2600                  * result in NXRRSET, we suppress fetches for address
2601                  * families we don't have the first time through,
2602                  * provided that we have addresses in some family we
2603                  * can use.
2604                  *
2605                  * We don't want to set this option all the time, since
2606                  * if fctx->restarts > 1, we've clearly been having trouble
2607                  * with the addresses we had, so getting more could help.
2608                  */
2609                 stdoptions |= DNS_ADBFIND_AVOIDFETCHES;
2610         }
2611         if (res->dispatchv4 != NULL)
2612                 stdoptions |= DNS_ADBFIND_INET;
2613         if (res->dispatchv6 != NULL)
2614                 stdoptions |= DNS_ADBFIND_INET6;
2615         isc_stdtime_get(&now);
2616
2617         INSIST(ISC_LIST_EMPTY(fctx->finds));
2618         INSIST(ISC_LIST_EMPTY(fctx->altfinds));
2619
2620         for (result = dns_rdataset_first(&fctx->nameservers);
2621              result == ISC_R_SUCCESS;
2622              result = dns_rdataset_next(&fctx->nameservers))
2623         {
2624                 dns_rdataset_current(&fctx->nameservers, &rdata);
2625                 /*
2626                  * Extract the name from the NS record.
2627                  */
2628                 result = dns_rdata_tostruct(&rdata, &ns, NULL);
2629                 if (result != ISC_R_SUCCESS)
2630                         continue;
2631
2632                 findname(fctx, &ns.name, 0, stdoptions, 0, now,
2633                          &need_alternate);
2634                 dns_rdata_reset(&rdata);
2635                 dns_rdata_freestruct(&ns);
2636         }
2637         if (result != ISC_R_NOMORE)
2638                 return (result);
2639
2640         /*
2641          * Do we need to use 6 to 4?
2642          */
2643         if (need_alternate) {
2644                 int family;
2645                 alternate_t *a;
2646                 family = (res->dispatchv6 != NULL) ? AF_INET6 : AF_INET;
2647                 for (a = ISC_LIST_HEAD(fctx->res->alternates);
2648                      a != NULL;
2649                      a = ISC_LIST_NEXT(a, link)) {
2650                         if (!a->isaddress) {
2651                                 findname(fctx, &a->_u._n.name, a->_u._n.port,
2652                                          stdoptions, FCTX_ADDRINFO_FORWARDER,
2653                                          now, NULL);
2654                                 continue;
2655                         }
2656                         if (isc_sockaddr_pf(&a->_u.addr) != family)
2657                                 continue;
2658                         ai = NULL;
2659                         result = dns_adb_findaddrinfo(fctx->adb, &a->_u.addr,
2660                                                       &ai, 0);
2661                         if (result == ISC_R_SUCCESS) {
2662                                 dns_adbaddrinfo_t *cur;
2663                                 ai->flags |= FCTX_ADDRINFO_FORWARDER;
2664                                 cur = ISC_LIST_HEAD(fctx->altaddrs);
2665                                 while (cur != NULL && cur->srtt < ai->srtt)
2666                                         cur = ISC_LIST_NEXT(cur, publink);
2667                                 if (cur != NULL)
2668                                         ISC_LIST_INSERTBEFORE(fctx->altaddrs,
2669                                                               cur, ai, publink);
2670                                 else
2671                                         ISC_LIST_APPEND(fctx->altaddrs, ai,
2672                                                         publink);
2673                         }
2674                 }
2675         }
2676
2677  out:
2678         /*
2679          * Mark all known bad servers.
2680          */
2681         all_bad = mark_bad(fctx);
2682
2683         /*
2684          * How are we doing?
2685          */
2686         if (all_bad) {
2687                 /*
2688                  * We've got no addresses.
2689                  */
2690                 if (fctx->pending > 0) {
2691                         /*
2692                          * We're fetching the addresses, but don't have any
2693                          * yet.   Tell the caller to wait for an answer.
2694                          */
2695                         result = DNS_R_WAIT;
2696                 } else {
2697                         /*
2698                          * We've lost completely.  We don't know any
2699                          * addresses, and the ADB has told us it can't get
2700                          * them.
2701                          */
2702                         FCTXTRACE("no addresses");
2703                         result = ISC_R_FAILURE;
2704                 }
2705         } else {
2706                 /*
2707                  * We've found some addresses.  We might still be looking
2708                  * for more addresses.
2709                  */
2710                 sort_finds(fctx);
2711                 result = ISC_R_SUCCESS;
2712         }
2713
2714         return (result);
2715 }
2716
2717 static inline void
2718 possibly_mark(fetchctx_t *fctx, dns_adbaddrinfo_t *addr)
2719 {
2720         isc_netaddr_t na;
2721         char buf[ISC_NETADDR_FORMATSIZE];
2722         isc_sockaddr_t *sa;
2723         isc_boolean_t aborted = ISC_FALSE;
2724         isc_boolean_t bogus;
2725         dns_acl_t *blackhole;
2726         isc_netaddr_t ipaddr;
2727         dns_peer_t *peer = NULL;
2728         dns_resolver_t *res;
2729         const char *msg = NULL;
2730
2731         sa = &addr->sockaddr;
2732
2733         res = fctx->res;
2734         isc_netaddr_fromsockaddr(&ipaddr, sa);
2735         blackhole = dns_dispatchmgr_getblackhole(res->dispatchmgr);
2736         (void) dns_peerlist_peerbyaddr(res->view->peers, &ipaddr, &peer);
2737
2738         if (blackhole != NULL) {
2739                 int match;
2740
2741                 if (dns_acl_match(&ipaddr, NULL, blackhole,
2742                                   &res->view->aclenv,
2743                                   &match, NULL) == ISC_R_SUCCESS &&
2744                     match > 0)
2745                         aborted = ISC_TRUE;
2746         }
2747
2748         if (peer != NULL &&
2749             dns_peer_getbogus(peer, &bogus) == ISC_R_SUCCESS &&
2750             bogus)
2751                 aborted = ISC_TRUE;
2752
2753         if (aborted) {
2754                 addr->flags |= FCTX_ADDRINFO_MARK;
2755                 msg = "ignoring blackholed / bogus server: ";
2756         } else if (isc_sockaddr_ismulticast(sa)) {
2757                 addr->flags |= FCTX_ADDRINFO_MARK;
2758                 msg = "ignoring multicast address: ";
2759         } else if (isc_sockaddr_isexperimental(sa)) {
2760                 addr->flags |= FCTX_ADDRINFO_MARK;
2761                 msg = "ignoring experimental address: ";
2762         } else if (sa->type.sa.sa_family != AF_INET6) {
2763                 return;
2764         } else if (IN6_IS_ADDR_V4MAPPED(&sa->type.sin6.sin6_addr)) {
2765                 addr->flags |= FCTX_ADDRINFO_MARK;
2766                 msg = "ignoring IPv6 mapped IPV4 address: ";
2767         } else if (IN6_IS_ADDR_V4COMPAT(&sa->type.sin6.sin6_addr)) {
2768                 addr->flags |= FCTX_ADDRINFO_MARK;
2769                 msg = "ignoring IPv6 compatibility IPV4 address: ";
2770         } else
2771                 return;
2772
2773         if (!isc_log_wouldlog(dns_lctx, ISC_LOG_DEBUG(3)))
2774                 return;
2775
2776         isc_netaddr_fromsockaddr(&na, sa);
2777         isc_netaddr_format(&na, buf, sizeof(buf));
2778         FCTXTRACE2(msg, buf);
2779 }
2780
2781 static inline dns_adbaddrinfo_t *
2782 fctx_nextaddress(fetchctx_t *fctx) {
2783         dns_adbfind_t *find, *start;
2784         dns_adbaddrinfo_t *addrinfo;
2785         dns_adbaddrinfo_t *faddrinfo;
2786
2787         /*
2788          * Return the next untried address, if any.
2789          */
2790
2791         /*
2792          * Find the first unmarked forwarder (if any).
2793          */
2794         for (addrinfo = ISC_LIST_HEAD(fctx->forwaddrs);
2795              addrinfo != NULL;
2796              addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2797                 if (!UNMARKED(addrinfo))
2798                         continue;
2799                 possibly_mark(fctx, addrinfo);
2800                 if (UNMARKED(addrinfo)) {
2801                         addrinfo->flags |= FCTX_ADDRINFO_MARK;
2802                         fctx->find = NULL;
2803                         return (addrinfo);
2804                 }
2805         }
2806
2807         /*
2808          * No forwarders.  Move to the next find.
2809          */
2810
2811         fctx->attributes |= FCTX_ATTR_TRIEDFIND;
2812
2813         find = fctx->find;
2814         if (find == NULL)
2815                 find = ISC_LIST_HEAD(fctx->finds);
2816         else {
2817                 find = ISC_LIST_NEXT(find, publink);
2818                 if (find == NULL)
2819                         find = ISC_LIST_HEAD(fctx->finds);
2820         }
2821
2822         /*
2823          * Find the first unmarked addrinfo.
2824          */
2825         addrinfo = NULL;
2826         if (find != NULL) {
2827                 start = find;
2828                 do {
2829                         for (addrinfo = ISC_LIST_HEAD(find->list);
2830                              addrinfo != NULL;
2831                              addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2832                                 if (!UNMARKED(addrinfo))
2833                                         continue;
2834                                 possibly_mark(fctx, addrinfo);
2835                                 if (UNMARKED(addrinfo)) {
2836                                         addrinfo->flags |= FCTX_ADDRINFO_MARK;
2837                                         break;
2838                                 }
2839                         }
2840                         if (addrinfo != NULL)
2841                                 break;
2842                         find = ISC_LIST_NEXT(find, publink);
2843                         if (find == NULL)
2844                                 find = ISC_LIST_HEAD(fctx->finds);
2845                 } while (find != start);
2846         }
2847
2848         fctx->find = find;
2849         if (addrinfo != NULL)
2850                 return (addrinfo);
2851
2852         /*
2853          * No nameservers left.  Try alternates.
2854          */
2855
2856         fctx->attributes |= FCTX_ATTR_TRIEDALT;
2857
2858         find = fctx->altfind;
2859         if (find == NULL)
2860                 find = ISC_LIST_HEAD(fctx->altfinds);
2861         else {
2862                 find = ISC_LIST_NEXT(find, publink);
2863                 if (find == NULL)
2864                         find = ISC_LIST_HEAD(fctx->altfinds);
2865         }
2866
2867         /*
2868          * Find the first unmarked addrinfo.
2869          */
2870         addrinfo = NULL;
2871         if (find != NULL) {
2872                 start = find;
2873                 do {
2874                         for (addrinfo = ISC_LIST_HEAD(find->list);
2875                              addrinfo != NULL;
2876                              addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2877                                 if (!UNMARKED(addrinfo))
2878                                         continue;
2879                                 possibly_mark(fctx, addrinfo);
2880                                 if (UNMARKED(addrinfo)) {
2881                                         addrinfo->flags |= FCTX_ADDRINFO_MARK;
2882                                         break;
2883                                 }
2884                         }
2885                         if (addrinfo != NULL)
2886                                 break;
2887                         find = ISC_LIST_NEXT(find, publink);
2888                         if (find == NULL)
2889                                 find = ISC_LIST_HEAD(fctx->altfinds);
2890                 } while (find != start);
2891         }
2892
2893         faddrinfo = addrinfo;
2894
2895         /*
2896          * See if we have a better alternate server by address.
2897          */
2898
2899         for (addrinfo = ISC_LIST_HEAD(fctx->altaddrs);
2900              addrinfo != NULL;
2901              addrinfo = ISC_LIST_NEXT(addrinfo, publink)) {
2902                 if (!UNMARKED(addrinfo))
2903                         continue;
2904                 possibly_mark(fctx, addrinfo);
2905                 if (UNMARKED(addrinfo) &&
2906                     (faddrinfo == NULL ||
2907                      addrinfo->srtt < faddrinfo->srtt)) {
2908                         if (faddrinfo != NULL)
2909                                 faddrinfo->flags &= ~FCTX_ADDRINFO_MARK;
2910                         addrinfo->flags |= FCTX_ADDRINFO_MARK;
2911                         break;
2912                 }
2913         }
2914
2915         if (addrinfo == NULL) {
2916                 addrinfo = faddrinfo;
2917                 fctx->altfind = find;
2918         }
2919
2920         return (addrinfo);
2921 }
2922
2923 static void
2924 fctx_try(fetchctx_t *fctx, isc_boolean_t retrying) {
2925         isc_result_t result;
2926         dns_adbaddrinfo_t *addrinfo;
2927
2928         FCTXTRACE("try");
2929
2930         REQUIRE(!ADDRWAIT(fctx));
2931
2932         addrinfo = fctx_nextaddress(fctx);
2933         if (addrinfo == NULL) {
2934                 /*
2935                  * We have no more addresses.  Start over.
2936                  */
2937                 fctx_cancelqueries(fctx, ISC_TRUE);
2938                 fctx_cleanupfinds(fctx);
2939                 fctx_cleanupaltfinds(fctx);
2940                 fctx_cleanupforwaddrs(fctx);
2941                 fctx_cleanupaltaddrs(fctx);
2942                 result = fctx_getaddresses(fctx);
2943                 if (result == DNS_R_WAIT) {
2944                         /*
2945                          * Sleep waiting for addresses.
2946                          */
2947                         FCTXTRACE("addrwait");
2948                         fctx->attributes |= FCTX_ATTR_ADDRWAIT;
2949                         return;
2950                 } else if (result != ISC_R_SUCCESS) {
2951                         /*
2952                          * Something bad happened.
2953                          */
2954                         fctx_done(fctx, result, __LINE__);
2955                         return;
2956                 }
2957
2958                 addrinfo = fctx_nextaddress(fctx);
2959                 /*
2960                  * While we may have addresses from the ADB, they
2961                  * might be bad ones.  In this case, return SERVFAIL.
2962                  */
2963                 if (addrinfo == NULL) {
2964                         fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
2965                         return;
2966                 }
2967         }
2968
2969         result = fctx_query(fctx, addrinfo, fctx->options);
2970         if (result != ISC_R_SUCCESS)
2971                 fctx_done(fctx, result, __LINE__);
2972         else if (retrying)
2973                 inc_stats(fctx->res, dns_resstatscounter_retry);
2974 }
2975
2976 static isc_boolean_t
2977 fctx_destroy(fetchctx_t *fctx) {
2978         dns_resolver_t *res;
2979         unsigned int bucketnum;
2980         isc_sockaddr_t *sa, *next_sa;
2981
2982         /*
2983          * Caller must be holding the bucket lock.
2984          */
2985
2986         REQUIRE(VALID_FCTX(fctx));
2987         REQUIRE(fctx->state == fetchstate_done ||
2988                 fctx->state == fetchstate_init);
2989         REQUIRE(ISC_LIST_EMPTY(fctx->events));
2990         REQUIRE(ISC_LIST_EMPTY(fctx->queries));
2991         REQUIRE(ISC_LIST_EMPTY(fctx->finds));
2992         REQUIRE(ISC_LIST_EMPTY(fctx->altfinds));
2993         REQUIRE(fctx->pending == 0);
2994         REQUIRE(fctx->references == 0);
2995         REQUIRE(ISC_LIST_EMPTY(fctx->validators));
2996
2997         FCTXTRACE("destroy");
2998
2999         res = fctx->res;
3000         bucketnum = fctx->bucketnum;
3001
3002         ISC_LIST_UNLINK(res->buckets[bucketnum].fctxs, fctx, link);
3003
3004         /*
3005          * Free bad.
3006          */
3007         for (sa = ISC_LIST_HEAD(fctx->bad);
3008              sa != NULL;
3009              sa = next_sa) {
3010                 next_sa = ISC_LIST_NEXT(sa, link);
3011                 ISC_LIST_UNLINK(fctx->bad, sa, link);
3012                 isc_mem_put(res->buckets[bucketnum].mctx, sa, sizeof(*sa));
3013         }
3014
3015         for (sa = ISC_LIST_HEAD(fctx->edns);
3016              sa != NULL;
3017              sa = next_sa) {
3018                 next_sa = ISC_LIST_NEXT(sa, link);
3019                 ISC_LIST_UNLINK(fctx->edns, sa, link);
3020                 isc_mem_put(res->buckets[bucketnum].mctx, sa, sizeof(*sa));
3021         }
3022
3023         for (sa = ISC_LIST_HEAD(fctx->edns512);
3024              sa != NULL;
3025              sa = next_sa) {
3026                 next_sa = ISC_LIST_NEXT(sa, link);
3027                 ISC_LIST_UNLINK(fctx->edns512, sa, link);
3028                 isc_mem_put(res->buckets[bucketnum].mctx, sa, sizeof(*sa));
3029         }
3030
3031         isc_timer_detach(&fctx->timer);
3032         dns_message_destroy(&fctx->rmessage);
3033         dns_message_destroy(&fctx->qmessage);
3034         if (dns_name_countlabels(&fctx->domain) > 0)
3035                 dns_name_free(&fctx->domain, res->buckets[bucketnum].mctx);
3036         if (dns_rdataset_isassociated(&fctx->nameservers))
3037                 dns_rdataset_disassociate(&fctx->nameservers);
3038         dns_name_free(&fctx->name, res->buckets[bucketnum].mctx);
3039         dns_db_detach(&fctx->cache);
3040         dns_adb_detach(&fctx->adb);
3041         isc_mem_free(res->buckets[bucketnum].mctx, fctx->info);
3042         isc_mem_put(res->buckets[bucketnum].mctx, fctx, sizeof(*fctx));
3043
3044         LOCK(&res->nlock);
3045         res->nfctx--;
3046         UNLOCK(&res->nlock);
3047
3048         if (res->buckets[bucketnum].exiting &&
3049             ISC_LIST_EMPTY(res->buckets[bucketnum].fctxs))
3050                 return (ISC_TRUE);
3051
3052         return (ISC_FALSE);
3053 }
3054
3055 /*
3056  * Fetch event handlers.
3057  */
3058
3059 static void
3060 fctx_timeout(isc_task_t *task, isc_event_t *event) {
3061         fetchctx_t *fctx = event->ev_arg;
3062         isc_timerevent_t *tevent = (isc_timerevent_t *)event;
3063         resquery_t *query;
3064
3065         REQUIRE(VALID_FCTX(fctx));
3066
3067         UNUSED(task);
3068
3069         FCTXTRACE("timeout");
3070
3071         inc_stats(fctx->res, dns_resstatscounter_querytimeout);
3072
3073         if (event->ev_type == ISC_TIMEREVENT_LIFE) {
3074                 fctx->reason = NULL;
3075                 fctx_done(fctx, ISC_R_TIMEDOUT, __LINE__);
3076         } else {
3077                 isc_result_t result;
3078
3079                 fctx->timeouts++;
3080                 fctx->timeout = ISC_TRUE;
3081                 /*
3082                  * We could cancel the running queries here, or we could let
3083                  * them keep going.  Since we normally use separate sockets for
3084                  * different queries, we adopt the former approach to reduce
3085                  * the number of open sockets: cancel the oldest query if it
3086                  * expired after the query had started (this is usually the
3087                  * case but is not always so, depending on the task schedule
3088                  * timing).
3089                  */
3090                 query = ISC_LIST_HEAD(fctx->queries);
3091                 if (query != NULL &&
3092                     isc_time_compare(&tevent->due, &query->start) >= 0) {
3093                         fctx_cancelquery(&query, NULL, NULL, ISC_TRUE);
3094                 }
3095                 fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
3096                 /*
3097                  * Our timer has triggered.  Reestablish the fctx lifetime
3098                  * timer.
3099                  */
3100                 result = fctx_starttimer(fctx);
3101                 if (result != ISC_R_SUCCESS)
3102                         fctx_done(fctx, result, __LINE__);
3103                 else
3104                         /*
3105                          * Keep trying.
3106                          */
3107                         fctx_try(fctx, ISC_TRUE);
3108         }
3109
3110         isc_event_free(&event);
3111 }
3112
3113 static void
3114 fctx_shutdown(fetchctx_t *fctx) {
3115         isc_event_t *cevent;
3116
3117         /*
3118          * Start the shutdown process for fctx, if it isn't already underway.
3119          */
3120
3121         FCTXTRACE("shutdown");
3122
3123         /*
3124          * The caller must be holding the appropriate bucket lock.
3125          */
3126
3127         if (fctx->want_shutdown)
3128                 return;
3129
3130         fctx->want_shutdown = ISC_TRUE;
3131
3132         /*
3133          * Unless we're still initializing (in which case the
3134          * control event is still outstanding), we need to post
3135          * the control event to tell the fetch we want it to
3136          * exit.
3137          */
3138         if (fctx->state != fetchstate_init) {
3139                 cevent = &fctx->control_event;
3140                 isc_task_send(fctx->res->buckets[fctx->bucketnum].task,
3141                               &cevent);
3142         }
3143 }
3144
3145 static void
3146 fctx_doshutdown(isc_task_t *task, isc_event_t *event) {
3147         fetchctx_t *fctx = event->ev_arg;
3148         isc_boolean_t bucket_empty = ISC_FALSE;
3149         dns_resolver_t *res;
3150         unsigned int bucketnum;
3151         dns_validator_t *validator;
3152
3153         REQUIRE(VALID_FCTX(fctx));
3154
3155         UNUSED(task);
3156
3157         res = fctx->res;
3158         bucketnum = fctx->bucketnum;
3159
3160         FCTXTRACE("doshutdown");
3161
3162         /*
3163          * An fctx that is shutting down is no longer in ADDRWAIT mode.
3164          */
3165         fctx->attributes &= ~FCTX_ATTR_ADDRWAIT;
3166
3167         /*
3168          * Cancel all pending validators.  Note that this must be done
3169          * without the bucket lock held, since that could cause deadlock.
3170          */
3171         validator = ISC_LIST_HEAD(fctx->validators);
3172         while (validator != NULL) {
3173                 dns_validator_cancel(validator);
3174                 validator = ISC_LIST_NEXT(validator, link);
3175         }
3176
3177         if (fctx->nsfetch != NULL)
3178                 dns_resolver_cancelfetch(fctx->nsfetch);
3179
3180         /*
3181          * Shut down anything that is still running on behalf of this
3182          * fetch.  To avoid deadlock with the ADB, we must do this
3183          * before we lock the bucket lock.
3184          */
3185         fctx_stopeverything(fctx, ISC_FALSE);
3186
3187         LOCK(&res->buckets[bucketnum].lock);
3188
3189         fctx->attributes |= FCTX_ATTR_SHUTTINGDOWN;
3190
3191         INSIST(fctx->state == fetchstate_active ||
3192                fctx->state == fetchstate_done);
3193         INSIST(fctx->want_shutdown);
3194
3195         if (fctx->state != fetchstate_done) {
3196                 fctx->state = fetchstate_done;
3197                 fctx_sendevents(fctx, ISC_R_CANCELED, __LINE__);
3198         }
3199
3200         if (fctx->references == 0 && fctx->pending == 0 &&
3201             fctx->nqueries == 0 && ISC_LIST_EMPTY(fctx->validators))
3202                 bucket_empty = fctx_destroy(fctx);
3203
3204         UNLOCK(&res->buckets[bucketnum].lock);
3205
3206         if (bucket_empty)
3207                 empty_bucket(res);
3208 }
3209
3210 static void
3211 fctx_start(isc_task_t *task, isc_event_t *event) {
3212         fetchctx_t *fctx = event->ev_arg;
3213         isc_boolean_t done = ISC_FALSE, bucket_empty = ISC_FALSE;
3214         dns_resolver_t *res;
3215         unsigned int bucketnum;
3216
3217         REQUIRE(VALID_FCTX(fctx));
3218
3219         UNUSED(task);
3220
3221         res = fctx->res;
3222         bucketnum = fctx->bucketnum;
3223
3224         FCTXTRACE("start");
3225
3226         LOCK(&res->buckets[bucketnum].lock);
3227
3228         INSIST(fctx->state == fetchstate_init);
3229         if (fctx->want_shutdown) {
3230                 /*
3231                  * We haven't started this fctx yet, and we've been requested
3232                  * to shut it down.
3233                  */
3234                 fctx->attributes |= FCTX_ATTR_SHUTTINGDOWN;
3235                 fctx->state = fetchstate_done;
3236                 fctx_sendevents(fctx, ISC_R_CANCELED, __LINE__);
3237                 /*
3238                  * Since we haven't started, we INSIST that we have no
3239                  * pending ADB finds and no pending validations.
3240                  */
3241                 INSIST(fctx->pending == 0);
3242                 INSIST(fctx->nqueries == 0);
3243                 INSIST(ISC_LIST_EMPTY(fctx->validators));
3244                 if (fctx->references == 0) {
3245                         /*
3246                          * It's now safe to destroy this fctx.
3247                          */
3248                         bucket_empty = fctx_destroy(fctx);
3249                 }
3250                 done = ISC_TRUE;
3251         } else {
3252                 /*
3253                  * Normal fctx startup.
3254                  */
3255                 fctx->state = fetchstate_active;
3256                 /*
3257                  * Reset the control event for later use in shutting down
3258                  * the fctx.
3259                  */
3260                 ISC_EVENT_INIT(event, sizeof(*event), 0, NULL,
3261                                DNS_EVENT_FETCHCONTROL, fctx_doshutdown, fctx,
3262                                NULL, NULL, NULL);
3263         }
3264
3265         UNLOCK(&res->buckets[bucketnum].lock);
3266
3267         if (!done) {
3268                 isc_result_t result;
3269
3270                 /*
3271                  * All is well.  Start working on the fetch.
3272                  */
3273                 result = fctx_starttimer(fctx);
3274                 if (result != ISC_R_SUCCESS)
3275                         fctx_done(fctx, result, __LINE__);
3276                 else
3277                         fctx_try(fctx, ISC_FALSE);
3278         } else if (bucket_empty)
3279                 empty_bucket(res);
3280 }
3281
3282 /*
3283  * Fetch Creation, Joining, and Cancelation.
3284  */
3285
3286 static inline isc_result_t
3287 fctx_join(fetchctx_t *fctx, isc_task_t *task, isc_sockaddr_t *client,
3288           dns_messageid_t id, isc_taskaction_t action, void *arg,
3289           dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset,
3290           dns_fetch_t *fetch)
3291 {
3292         isc_task_t *clone;
3293         dns_fetchevent_t *event;
3294
3295         FCTXTRACE("join");
3296
3297         /*
3298          * We store the task we're going to send this event to in the
3299          * sender field.  We'll make the fetch the sender when we actually
3300          * send the event.
3301          */
3302         clone = NULL;
3303         isc_task_attach(task, &clone);
3304         event = (dns_fetchevent_t *)
3305                 isc_event_allocate(fctx->res->mctx, clone, DNS_EVENT_FETCHDONE,
3306                                    action, arg, sizeof(*event));
3307         if (event == NULL) {
3308                 isc_task_detach(&clone);
3309                 return (ISC_R_NOMEMORY);
3310         }
3311         event->result = DNS_R_SERVFAIL;
3312         event->qtype = fctx->type;
3313         event->db = NULL;
3314         event->node = NULL;
3315         event->rdataset = rdataset;
3316         event->sigrdataset = sigrdataset;
3317         event->fetch = fetch;
3318         event->client = client;
3319         event->id = id;
3320         dns_fixedname_init(&event->foundname);
3321
3322         /*
3323          * Make sure that we can store the sigrdataset in the
3324          * first event if it is needed by any of the events.
3325          */
3326         if (event->sigrdataset != NULL)
3327                 ISC_LIST_PREPEND(fctx->events, event, ev_link);
3328         else
3329                 ISC_LIST_APPEND(fctx->events, event, ev_link);
3330         fctx->references++;
3331
3332         fetch->magic = DNS_FETCH_MAGIC;
3333         fetch->private = fctx;
3334
3335         return (ISC_R_SUCCESS);
3336 }
3337
3338 static isc_result_t
3339 fctx_create(dns_resolver_t *res, dns_name_t *name, dns_rdatatype_t type,
3340             dns_name_t *domain, dns_rdataset_t *nameservers,
3341             unsigned int options, unsigned int bucketnum, fetchctx_t **fctxp)
3342 {
3343         fetchctx_t *fctx;
3344         isc_result_t result;
3345         isc_result_t iresult;
3346         isc_interval_t interval;
3347         dns_fixedname_t fixed;
3348         unsigned int findoptions = 0;
3349         char buf[DNS_NAME_FORMATSIZE + DNS_RDATATYPE_FORMATSIZE];
3350         char typebuf[DNS_RDATATYPE_FORMATSIZE];
3351         dns_name_t suffix;
3352
3353         /*
3354          * Caller must be holding the lock for bucket number 'bucketnum'.
3355          */
3356         REQUIRE(fctxp != NULL && *fctxp == NULL);
3357
3358         fctx = isc_mem_get(res->buckets[bucketnum].mctx, sizeof(*fctx));
3359         if (fctx == NULL)
3360                 return (ISC_R_NOMEMORY);
3361         dns_name_format(name, buf, sizeof(buf));
3362         dns_rdatatype_format(type, typebuf, sizeof(typebuf));
3363         strcat(buf, "/");       /* checked */
3364         strcat(buf, typebuf);   /* checked */
3365         fctx->info = isc_mem_strdup(res->buckets[bucketnum].mctx, buf);
3366         if (fctx->info == NULL) {
3367                 result = ISC_R_NOMEMORY;
3368                 goto cleanup_fetch;
3369         }
3370         FCTXTRACE("create");
3371         dns_name_init(&fctx->name, NULL);
3372         result = dns_name_dup(name, res->buckets[bucketnum].mctx, &fctx->name);
3373         if (result != ISC_R_SUCCESS)
3374                 goto cleanup_info;
3375         dns_name_init(&fctx->domain, NULL);
3376         dns_rdataset_init(&fctx->nameservers);
3377
3378         fctx->type = type;
3379         fctx->options = options;
3380         /*
3381          * Note!  We do not attach to the task.  We are relying on the
3382          * resolver to ensure that this task doesn't go away while we are
3383          * using it.
3384          */
3385         fctx->res = res;
3386         fctx->references = 0;
3387         fctx->bucketnum = bucketnum;
3388         fctx->state = fetchstate_init;
3389         fctx->want_shutdown = ISC_FALSE;
3390         fctx->cloned = ISC_FALSE;
3391         ISC_LIST_INIT(fctx->queries);
3392         ISC_LIST_INIT(fctx->finds);
3393         ISC_LIST_INIT(fctx->altfinds);
3394         ISC_LIST_INIT(fctx->forwaddrs);
3395         ISC_LIST_INIT(fctx->altaddrs);
3396         ISC_LIST_INIT(fctx->forwarders);
3397         fctx->fwdpolicy = dns_fwdpolicy_none;
3398         ISC_LIST_INIT(fctx->bad);
3399         ISC_LIST_INIT(fctx->edns);
3400         ISC_LIST_INIT(fctx->edns512);
3401         ISC_LIST_INIT(fctx->validators);
3402         fctx->validator = NULL;
3403         fctx->find = NULL;
3404         fctx->altfind = NULL;
3405         fctx->pending = 0;
3406         fctx->restarts = 0;
3407         fctx->querysent = 0;
3408         fctx->referrals = 0;
3409         TIME_NOW(&fctx->start);
3410         fctx->timeouts = 0;
3411         fctx->lamecount = 0;
3412         fctx->adberr = 0;
3413         fctx->neterr = 0;
3414         fctx->badresp = 0;
3415         fctx->findfail = 0;
3416         fctx->valfail = 0;
3417         fctx->result = ISC_R_FAILURE;
3418         fctx->vresult = ISC_R_SUCCESS;
3419         fctx->exitline = -1;    /* sentinel */
3420         fctx->logged = ISC_FALSE;
3421         fctx->attributes = 0;
3422         fctx->spilled = ISC_FALSE;
3423         fctx->nqueries = 0;
3424         fctx->reason = NULL;
3425         fctx->timeout = ISC_FALSE;
3426
3427         dns_name_init(&fctx->nsname, NULL);
3428         fctx->nsfetch = NULL;
3429         dns_rdataset_init(&fctx->nsrrset);
3430
3431         if (domain == NULL) {
3432                 dns_forwarders_t *forwarders = NULL;
3433                 unsigned int labels;
3434                 dns_name_t *fwdname = name;
3435
3436                 /*
3437                  * DS records are found in the parent server.
3438                  * Strip label to get the correct forwarder (if any).
3439                  */
3440                 if (dns_rdatatype_atparent(fctx->type) &&
3441                     dns_name_countlabels(name) > 1) {
3442                         dns_name_init(&suffix, NULL);
3443                         labels = dns_name_countlabels(name);
3444                         dns_name_getlabelsequence(name, 1, labels - 1, &suffix);
3445                         fwdname = &suffix;
3446                 }
3447                 dns_fixedname_init(&fixed);
3448                 domain = dns_fixedname_name(&fixed);
3449                 result = dns_fwdtable_find2(fctx->res->view->fwdtable, fwdname,
3450                                             domain, &forwarders);
3451                 if (result == ISC_R_SUCCESS)
3452                         fctx->fwdpolicy = forwarders->fwdpolicy;
3453
3454                 if (fctx->fwdpolicy != dns_fwdpolicy_only) {
3455                         /*
3456                          * The caller didn't supply a query domain and
3457                          * nameservers, and we're not in forward-only mode,
3458                          * so find the best nameservers to use.
3459                          */
3460                         if (dns_rdatatype_atparent(fctx->type))
3461                                 findoptions |= DNS_DBFIND_NOEXACT;
3462                         result = dns_view_findzonecut(res->view, name, domain,
3463                                                       0, findoptions, ISC_TRUE,
3464                                                       &fctx->nameservers,
3465                                                       NULL);
3466                         if (result != ISC_R_SUCCESS)
3467                                 goto cleanup_name;
3468                         result = dns_name_dup(domain,
3469                                               res->buckets[bucketnum].mctx,
3470                                               &fctx->domain);
3471                         if (result != ISC_R_SUCCESS) {
3472                                 dns_rdataset_disassociate(&fctx->nameservers);
3473                                 goto cleanup_name;
3474                         }
3475                 } else {
3476                         /*
3477                          * We're in forward-only mode.  Set the query domain.
3478                          */
3479                         result = dns_name_dup(domain,
3480                                               res->buckets[bucketnum].mctx,
3481                                               &fctx->domain);
3482                         if (result