Detect FPU by checking CPUID features.
[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 != ISC_R_SUCCESS)
3483                                 goto cleanup_name;
3484                 }
3485         } else {
3486                 result = dns_name_dup(domain,
3487                                       res->buckets[bucketnum].mctx,
3488                                       &fctx->domain);
3489                 if (result != ISC_R_SUCCESS)
3490                         goto cleanup_name;
3491                 dns_rdataset_clone(nameservers, &fctx->nameservers);
3492         }
3493
3494         INSIST(dns_name_issubdomain(&fctx->name, &fctx->domain));
3495
3496         fctx->qmessage = NULL;
3497         result = dns_message_create(res->buckets[bucketnum].mctx,
3498                                     DNS_MESSAGE_INTENTRENDER,
3499                                     &fctx->qmessage);
3500
3501         if (result != ISC_R_SUCCESS)
3502                 goto cleanup_domain;
3503
3504         fctx->rmessage = NULL;
3505         result = dns_message_create(res->buckets[bucketnum].mctx,
3506                                     DNS_MESSAGE_INTENTPARSE,
3507                                     &fctx->rmessage);
3508
3509         if (result != ISC_R_SUCCESS)
3510                 goto cleanup_qmessage;
3511
3512         /*
3513          * Compute an expiration time for the entire fetch.
3514          */
3515         isc_interval_set(&interval, 30, 0);             /* XXXRTH constant */
3516         iresult = isc_time_nowplusinterval(&fctx->expires, &interval);
3517         if (iresult != ISC_R_SUCCESS) {
3518                 UNEXPECTED_ERROR(__FILE__, __LINE__,
3519                                  "isc_time_nowplusinterval: %s",
3520                                  isc_result_totext(iresult));
3521                 result = ISC_R_UNEXPECTED;
3522                 goto cleanup_rmessage;
3523         }
3524
3525         /*
3526          * Default retry interval initialization.  We set the interval now
3527          * mostly so it won't be uninitialized.  It will be set to the
3528          * correct value before a query is issued.
3529          */
3530         isc_interval_set(&fctx->interval, 2, 0);
3531
3532         /*
3533          * Create an inactive timer.  It will be made active when the fetch
3534          * is actually started.
3535          */
3536         fctx->timer = NULL;
3537         iresult = isc_timer_create(res->timermgr, isc_timertype_inactive,
3538                                    NULL, NULL,
3539                                    res->buckets[bucketnum].task, fctx_timeout,
3540                                    fctx, &fctx->timer);
3541         if (iresult != ISC_R_SUCCESS) {
3542                 UNEXPECTED_ERROR(__FILE__, __LINE__,
3543                                  "isc_timer_create: %s",
3544                                  isc_result_totext(iresult));
3545                 result = ISC_R_UNEXPECTED;
3546                 goto cleanup_rmessage;
3547         }
3548
3549         /*
3550          * Attach to the view's cache and adb.
3551          */
3552         fctx->cache = NULL;
3553         dns_db_attach(res->view->cachedb, &fctx->cache);
3554         fctx->adb = NULL;
3555         dns_adb_attach(res->view->adb, &fctx->adb);
3556
3557         ISC_LIST_INIT(fctx->events);
3558         ISC_LINK_INIT(fctx, link);
3559         fctx->magic = FCTX_MAGIC;
3560
3561         ISC_LIST_APPEND(res->buckets[bucketnum].fctxs, fctx, link);
3562
3563         LOCK(&res->nlock);
3564         res->nfctx++;
3565         UNLOCK(&res->nlock);
3566
3567         *fctxp = fctx;
3568
3569         return (ISC_R_SUCCESS);
3570
3571  cleanup_rmessage:
3572         dns_message_destroy(&fctx->rmessage);
3573
3574  cleanup_qmessage:
3575         dns_message_destroy(&fctx->qmessage);
3576
3577  cleanup_domain:
3578         if (dns_name_countlabels(&fctx->domain) > 0)
3579                 dns_name_free(&fctx->domain, res->buckets[bucketnum].mctx);
3580         if (dns_rdataset_isassociated(&fctx->nameservers))
3581                 dns_rdataset_disassociate(&fctx->nameservers);
3582
3583  cleanup_name:
3584         dns_name_free(&fctx->name, res->buckets[bucketnum].mctx);
3585
3586  cleanup_info:
3587         isc_mem_free(res->buckets[bucketnum].mctx, fctx->info);
3588
3589  cleanup_fetch:
3590         isc_mem_put(res->buckets[bucketnum].mctx, fctx, sizeof(*fctx));
3591
3592         return (result);
3593 }
3594
3595 /*
3596  * Handle Responses
3597  */
3598 static inline isc_boolean_t
3599 is_lame(fetchctx_t *fctx) {
3600         dns_message_t *message = fctx->rmessage;
3601         dns_name_t *name;
3602         dns_rdataset_t *rdataset;
3603         isc_result_t result;
3604
3605         if (message->rcode != dns_rcode_noerror &&
3606             message->rcode != dns_rcode_nxdomain)
3607                 return (ISC_FALSE);
3608
3609         if (message->counts[DNS_SECTION_ANSWER] != 0)
3610                 return (ISC_FALSE);
3611
3612         if (message->counts[DNS_SECTION_AUTHORITY] == 0)
3613                 return (ISC_FALSE);
3614
3615         result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
3616         while (result == ISC_R_SUCCESS) {
3617                 name = NULL;
3618                 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
3619                 for (rdataset = ISC_LIST_HEAD(name->list);
3620                      rdataset != NULL;
3621                      rdataset = ISC_LIST_NEXT(rdataset, link)) {
3622                         dns_namereln_t namereln;
3623                         int order;
3624                         unsigned int labels;
3625                         if (rdataset->type != dns_rdatatype_ns)
3626                                 continue;
3627                         namereln = dns_name_fullcompare(name, &fctx->domain,
3628                                                         &order, &labels);
3629                         if (namereln == dns_namereln_equal &&
3630                             (message->flags & DNS_MESSAGEFLAG_AA) != 0)
3631                                 return (ISC_FALSE);
3632                         if (namereln == dns_namereln_subdomain)
3633                                 return (ISC_FALSE);
3634                         return (ISC_TRUE);
3635                 }
3636                 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
3637         }
3638
3639         return (ISC_FALSE);
3640 }
3641
3642 static inline void
3643 log_lame(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo) {
3644         char namebuf[DNS_NAME_FORMATSIZE];
3645         char domainbuf[DNS_NAME_FORMATSIZE];
3646         char addrbuf[ISC_SOCKADDR_FORMATSIZE];
3647
3648         dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
3649         dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
3650         isc_sockaddr_format(&addrinfo->sockaddr, addrbuf, sizeof(addrbuf));
3651         isc_log_write(dns_lctx, DNS_LOGCATEGORY_LAME_SERVERS,
3652                       DNS_LOGMODULE_RESOLVER, ISC_LOG_INFO,
3653                       "lame server resolving '%s' (in '%s'?): %s",
3654                       namebuf, domainbuf, addrbuf);
3655 }
3656
3657 static inline isc_result_t
3658 same_question(fetchctx_t *fctx) {
3659         isc_result_t result;
3660         dns_message_t *message = fctx->rmessage;
3661         dns_name_t *name;
3662         dns_rdataset_t *rdataset;
3663
3664         /*
3665          * Caller must be holding the fctx lock.
3666          */
3667
3668         /*
3669          * XXXRTH  Currently we support only one question.
3670          */
3671         if (message->counts[DNS_SECTION_QUESTION] != 1)
3672                 return (DNS_R_FORMERR);
3673
3674         result = dns_message_firstname(message, DNS_SECTION_QUESTION);
3675         if (result != ISC_R_SUCCESS)
3676                 return (result);
3677         name = NULL;
3678         dns_message_currentname(message, DNS_SECTION_QUESTION, &name);
3679         rdataset = ISC_LIST_HEAD(name->list);
3680         INSIST(rdataset != NULL);
3681         INSIST(ISC_LIST_NEXT(rdataset, link) == NULL);
3682         if (fctx->type != rdataset->type ||
3683             fctx->res->rdclass != rdataset->rdclass ||
3684             !dns_name_equal(&fctx->name, name))
3685                 return (DNS_R_FORMERR);
3686
3687         return (ISC_R_SUCCESS);
3688 }
3689
3690 static void
3691 clone_results(fetchctx_t *fctx) {
3692         dns_fetchevent_t *event, *hevent;
3693         isc_result_t result;
3694         dns_name_t *name, *hname;
3695
3696         FCTXTRACE("clone_results");
3697
3698         /*
3699          * Set up any other events to have the same data as the first
3700          * event.
3701          *
3702          * Caller must be holding the appropriate lock.
3703          */
3704
3705         fctx->cloned = ISC_TRUE;
3706         hevent = ISC_LIST_HEAD(fctx->events);
3707         if (hevent == NULL)
3708                 return;
3709         hname = dns_fixedname_name(&hevent->foundname);
3710         for (event = ISC_LIST_NEXT(hevent, ev_link);
3711              event != NULL;
3712              event = ISC_LIST_NEXT(event, ev_link)) {
3713                 name = dns_fixedname_name(&event->foundname);
3714                 result = dns_name_copy(hname, name, NULL);
3715                 if (result != ISC_R_SUCCESS)
3716                         event->result = result;
3717                 else
3718                         event->result = hevent->result;
3719                 dns_db_attach(hevent->db, &event->db);
3720                 dns_db_attachnode(hevent->db, hevent->node, &event->node);
3721                 INSIST(hevent->rdataset != NULL);
3722                 INSIST(event->rdataset != NULL);
3723                 if (dns_rdataset_isassociated(hevent->rdataset))
3724                         dns_rdataset_clone(hevent->rdataset, event->rdataset);
3725                 INSIST(! (hevent->sigrdataset == NULL &&
3726                           event->sigrdataset != NULL));
3727                 if (hevent->sigrdataset != NULL &&
3728                     dns_rdataset_isassociated(hevent->sigrdataset) &&
3729                     event->sigrdataset != NULL)
3730                         dns_rdataset_clone(hevent->sigrdataset,
3731                                            event->sigrdataset);
3732         }
3733 }
3734
3735 #define CACHE(r)        (((r)->attributes & DNS_RDATASETATTR_CACHE) != 0)
3736 #define ANSWER(r)       (((r)->attributes & DNS_RDATASETATTR_ANSWER) != 0)
3737 #define ANSWERSIG(r)    (((r)->attributes & DNS_RDATASETATTR_ANSWERSIG) != 0)
3738 #define EXTERNAL(r)     (((r)->attributes & DNS_RDATASETATTR_EXTERNAL) != 0)
3739 #define CHAINING(r)     (((r)->attributes & DNS_RDATASETATTR_CHAINING) != 0)
3740 #define CHASE(r)        (((r)->attributes & DNS_RDATASETATTR_CHASE) != 0)
3741 #define CHECKNAMES(r)   (((r)->attributes & DNS_RDATASETATTR_CHECKNAMES) != 0)
3742
3743
3744 /*
3745  * Destroy '*fctx' if it is ready to be destroyed (i.e., if it has
3746  * no references and is no longer waiting for any events).  If this
3747  * was the last fctx in the resolver, destroy the resolver.
3748  *
3749  * Requires:
3750  *      '*fctx' is shutting down.
3751  */
3752 static void
3753 maybe_destroy(fetchctx_t *fctx) {
3754         unsigned int bucketnum;
3755         isc_boolean_t bucket_empty = ISC_FALSE;
3756         dns_resolver_t *res = fctx->res;
3757         dns_validator_t *validator, *next_validator;
3758
3759         REQUIRE(SHUTTINGDOWN(fctx));
3760
3761         if (fctx->pending != 0 || fctx->nqueries != 0)
3762                 return;
3763
3764         for (validator = ISC_LIST_HEAD(fctx->validators);
3765              validator != NULL; validator = next_validator) {
3766                 next_validator = ISC_LIST_NEXT(validator, link);
3767                 dns_validator_cancel(validator);
3768                 /*
3769                  * If this is a active validator wait for the cancel
3770                  * to complete before calling dns_validator_destroy().
3771                  */
3772                 if (validator == fctx->validator)
3773                         continue;
3774                 ISC_LIST_UNLINK(fctx->validators, validator, link);
3775                 dns_validator_destroy(&validator);
3776         }
3777
3778         bucketnum = fctx->bucketnum;
3779         LOCK(&res->buckets[bucketnum].lock);
3780         if (fctx->references == 0 && ISC_LIST_EMPTY(fctx->validators))
3781                 bucket_empty = fctx_destroy(fctx);
3782         UNLOCK(&res->buckets[bucketnum].lock);
3783
3784         if (bucket_empty)
3785                 empty_bucket(res);
3786 }
3787
3788 /*
3789  * The validator has finished.
3790  */
3791 static void
3792 validated(isc_task_t *task, isc_event_t *event) {
3793         isc_result_t result = ISC_R_SUCCESS;
3794         isc_result_t eresult = ISC_R_SUCCESS;
3795         isc_stdtime_t now;
3796         fetchctx_t *fctx;
3797         dns_validatorevent_t *vevent;
3798         dns_fetchevent_t *hevent;
3799         dns_rdataset_t *ardataset = NULL;
3800         dns_rdataset_t *asigrdataset = NULL;
3801         dns_dbnode_t *node = NULL;
3802         isc_boolean_t negative;
3803         isc_boolean_t chaining;
3804         isc_boolean_t sentresponse;
3805         isc_uint32_t ttl;
3806         dns_dbnode_t *nsnode = NULL;
3807         dns_name_t *name;
3808         dns_rdataset_t *rdataset;
3809         dns_rdataset_t *sigrdataset;
3810         dns_valarg_t *valarg;
3811         dns_adbaddrinfo_t *addrinfo;
3812
3813         UNUSED(task); /* for now */
3814
3815         REQUIRE(event->ev_type == DNS_EVENT_VALIDATORDONE);
3816         valarg = event->ev_arg;
3817         fctx = valarg->fctx;
3818         addrinfo = valarg->addrinfo;
3819         REQUIRE(VALID_FCTX(fctx));
3820         REQUIRE(!ISC_LIST_EMPTY(fctx->validators));
3821
3822         vevent = (dns_validatorevent_t *)event;
3823
3824         FCTXTRACE("received validation completion event");
3825
3826         ISC_LIST_UNLINK(fctx->validators, vevent->validator, link);
3827         fctx->validator = NULL;
3828
3829         /*
3830          * Destroy the validator early so that we can
3831          * destroy the fctx if necessary.
3832          */
3833         dns_validator_destroy(&vevent->validator);
3834         isc_mem_put(fctx->res->buckets[fctx->bucketnum].mctx,
3835                     valarg, sizeof(*valarg));
3836
3837         negative = ISC_TF(vevent->rdataset == NULL);
3838
3839         sentresponse = ISC_TF((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0);
3840
3841         /*
3842          * If shutting down, ignore the results.  Check to see if we're
3843          * done waiting for validator completions and ADB pending events; if
3844          * so, destroy the fctx.
3845          */
3846         if (SHUTTINGDOWN(fctx) && !sentresponse) {
3847                 maybe_destroy(fctx);    /* Locks bucket. */
3848                 goto cleanup_event;
3849         }
3850
3851         LOCK(&fctx->res->buckets[fctx->bucketnum].lock);
3852
3853         /*
3854          * If chaining, we need to make sure that the right result code is
3855          * returned, and that the rdatasets are bound.
3856          */
3857         if (vevent->result == ISC_R_SUCCESS &&
3858             !negative &&
3859             vevent->rdataset != NULL &&
3860             CHAINING(vevent->rdataset))
3861         {
3862                 if (vevent->rdataset->type == dns_rdatatype_cname)
3863                         eresult = DNS_R_CNAME;
3864                 else {
3865                         INSIST(vevent->rdataset->type == dns_rdatatype_dname);
3866                         eresult = DNS_R_DNAME;
3867                 }
3868                 chaining = ISC_TRUE;
3869         } else
3870                 chaining = ISC_FALSE;
3871
3872         /*
3873          * Either we're not shutting down, or we are shutting down but want
3874          * to cache the result anyway (if this was a validation started by
3875          * a query with cd set)
3876          */
3877
3878         hevent = ISC_LIST_HEAD(fctx->events);
3879         if (hevent != NULL) {
3880                 if (!negative && !chaining &&
3881                     (fctx->type == dns_rdatatype_any ||
3882                      fctx->type == dns_rdatatype_rrsig ||
3883                      fctx->type == dns_rdatatype_sig)) {
3884                         /*
3885                          * Don't bind rdatasets; the caller
3886                          * will iterate the node.
3887                          */
3888                 } else {
3889                         ardataset = hevent->rdataset;
3890                         asigrdataset = hevent->sigrdataset;
3891                 }
3892         }
3893
3894         if (vevent->result != ISC_R_SUCCESS) {
3895                 FCTXTRACE("validation failed");
3896                 inc_stats(fctx->res, dns_resstatscounter_valfail);
3897                 fctx->valfail++;
3898                 fctx->vresult = vevent->result;
3899                 result = ISC_R_NOTFOUND;
3900                 if (vevent->rdataset != NULL)
3901                         result = dns_db_findnode(fctx->cache, vevent->name,
3902                                                  ISC_TRUE, &node);
3903                 if (result == ISC_R_SUCCESS)
3904                         (void)dns_db_deleterdataset(fctx->cache, node, NULL,
3905                                                     vevent->type, 0);
3906                 if (result == ISC_R_SUCCESS && vevent->sigrdataset != NULL)
3907                         (void)dns_db_deleterdataset(fctx->cache, node, NULL,
3908                                                     dns_rdatatype_rrsig,
3909                                                     vevent->type);
3910                 if (result == ISC_R_SUCCESS)
3911                         dns_db_detachnode(fctx->cache, &node);
3912                 result = vevent->result;
3913                 add_bad(fctx, addrinfo, result, badns_validation);
3914                 isc_event_free(&event);
3915                 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
3916                 INSIST(fctx->validator == NULL);
3917                 fctx->validator = ISC_LIST_HEAD(fctx->validators);
3918                 if (fctx->validator != NULL) {
3919                         dns_validator_send(fctx->validator);
3920                 } else if (sentresponse)
3921                         fctx_done(fctx, result, __LINE__); /* Locks bucket. */
3922                 else
3923                         fctx_try(fctx, ISC_TRUE);       /* Locks bucket. */
3924                 return;
3925         }
3926
3927         isc_stdtime_get(&now);
3928
3929         if (negative) {
3930                 dns_rdatatype_t covers;
3931                 FCTXTRACE("nonexistence validation OK");
3932
3933                 inc_stats(fctx->res, dns_resstatscounter_valnegsuccess);
3934
3935                 if (fctx->rmessage->rcode == dns_rcode_nxdomain)
3936                         covers = dns_rdatatype_any;
3937                 else
3938                         covers = fctx->type;
3939
3940                 result = dns_db_findnode(fctx->cache, vevent->name, ISC_TRUE,
3941                                          &node);
3942                 if (result != ISC_R_SUCCESS)
3943                         goto noanswer_response;
3944
3945                 /*
3946                  * If we are asking for a SOA record set the cache time
3947                  * to zero to facilitate locating the containing zone of
3948                  * a arbitrary zone.
3949                  */
3950                 ttl = fctx->res->view->maxncachettl;
3951                 if (fctx->type == dns_rdatatype_soa &&
3952                     covers == dns_rdatatype_any &&
3953                     fctx->res->zero_no_soa_ttl)
3954                         ttl = 0;
3955
3956                 result = ncache_adderesult(fctx->rmessage, fctx->cache, node,
3957                                            covers, now, ttl,
3958                                            ardataset, &eresult);
3959                 if (result != ISC_R_SUCCESS)
3960                         goto noanswer_response;
3961                 goto answer_response;
3962         } else
3963                 inc_stats(fctx->res, dns_resstatscounter_valsuccess);
3964
3965         FCTXTRACE("validation OK");
3966
3967         if (vevent->proofs[DNS_VALIDATOR_NOQNAMEPROOF] != NULL) {
3968
3969                 result = dns_rdataset_addnoqname(vevent->rdataset,
3970                                    vevent->proofs[DNS_VALIDATOR_NOQNAMEPROOF]);
3971                 RUNTIME_CHECK(result == ISC_R_SUCCESS);
3972                 INSIST(vevent->sigrdataset != NULL);
3973                 vevent->sigrdataset->ttl = vevent->rdataset->ttl;
3974         }
3975
3976         /*
3977          * The data was already cached as pending data.
3978          * Re-cache it as secure and bind the cached
3979          * rdatasets to the first event on the fetch
3980          * event list.
3981          */
3982         result = dns_db_findnode(fctx->cache, vevent->name, ISC_TRUE, &node);
3983         if (result != ISC_R_SUCCESS)
3984                 goto noanswer_response;
3985
3986         result = dns_db_addrdataset(fctx->cache, node, NULL, now,
3987                                     vevent->rdataset, 0, ardataset);
3988         if (result != ISC_R_SUCCESS &&
3989             result != DNS_R_UNCHANGED)
3990                 goto noanswer_response;
3991         if (ardataset != NULL && ardataset->type == 0) {
3992                 if (NXDOMAIN(ardataset))
3993                         eresult = DNS_R_NCACHENXDOMAIN;
3994                 else
3995                         eresult = DNS_R_NCACHENXRRSET;
3996         } else if (vevent->sigrdataset != NULL) {
3997                 result = dns_db_addrdataset(fctx->cache, node, NULL, now,
3998                                             vevent->sigrdataset, 0,
3999                                             asigrdataset);
4000                 if (result != ISC_R_SUCCESS &&
4001                     result != DNS_R_UNCHANGED)
4002                         goto noanswer_response;
4003         }
4004
4005         if (sentresponse) {
4006                 /*
4007                  * If we only deferred the destroy because we wanted to cache
4008                  * the data, destroy now.
4009                  */
4010                 dns_db_detachnode(fctx->cache, &node);
4011                 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4012                 if (SHUTTINGDOWN(fctx))
4013                         maybe_destroy(fctx);    /* Locks bucket. */
4014                 goto cleanup_event;
4015         }
4016
4017         if (!ISC_LIST_EMPTY(fctx->validators)) {
4018                 INSIST(!negative);
4019                 INSIST(fctx->type == dns_rdatatype_any ||
4020                        fctx->type == dns_rdatatype_rrsig ||
4021                        fctx->type == dns_rdatatype_sig);
4022                 /*
4023                  * Don't send a response yet - we have
4024                  * more rdatasets that still need to
4025                  * be validated.
4026                  */
4027                 dns_db_detachnode(fctx->cache, &node);
4028                 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4029                 dns_validator_send(ISC_LIST_HEAD(fctx->validators));
4030                 goto cleanup_event;
4031         }
4032
4033  answer_response:
4034         /*
4035          * Cache any NS/NSEC records that happened to be validated.
4036          */
4037         result = dns_message_firstname(fctx->rmessage, DNS_SECTION_AUTHORITY);
4038         while (result == ISC_R_SUCCESS) {
4039                 name = NULL;
4040                 dns_message_currentname(fctx->rmessage, DNS_SECTION_AUTHORITY,
4041                                         &name);
4042                 for (rdataset = ISC_LIST_HEAD(name->list);
4043                      rdataset != NULL;
4044                      rdataset = ISC_LIST_NEXT(rdataset, link)) {
4045                         if ((rdataset->type != dns_rdatatype_ns &&
4046                              rdataset->type != dns_rdatatype_nsec) ||
4047                             rdataset->trust != dns_trust_secure)
4048                                 continue;
4049                         for (sigrdataset = ISC_LIST_HEAD(name->list);
4050                              sigrdataset != NULL;
4051                              sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4052                                 if (sigrdataset->type != dns_rdatatype_rrsig ||
4053                                     sigrdataset->covers != rdataset->type)
4054                                         continue;
4055                                 break;
4056                         }
4057                         if (sigrdataset == NULL ||
4058                             sigrdataset->trust != dns_trust_secure)
4059                                 continue;
4060                         result = dns_db_findnode(fctx->cache, name, ISC_TRUE,
4061                                                  &nsnode);
4062                         if (result != ISC_R_SUCCESS)
4063                                 continue;
4064
4065                         result = dns_db_addrdataset(fctx->cache, nsnode, NULL,
4066                                                     now, rdataset, 0, NULL);
4067                         if (result == ISC_R_SUCCESS)
4068                                 result = dns_db_addrdataset(fctx->cache, nsnode,
4069                                                             NULL, now,
4070                                                             sigrdataset, 0,
4071                                                             NULL);
4072                         dns_db_detachnode(fctx->cache, &nsnode);
4073                 }
4074                 result = dns_message_nextname(fctx->rmessage,
4075                                               DNS_SECTION_AUTHORITY);
4076         }
4077
4078         result = ISC_R_SUCCESS;
4079
4080         /*
4081          * Respond with an answer, positive or negative,
4082          * as opposed to an error.  'node' must be non-NULL.
4083          */
4084
4085         fctx->attributes |= FCTX_ATTR_HAVEANSWER;
4086
4087         if (hevent != NULL) {
4088                 hevent->result = eresult;
4089                 RUNTIME_CHECK(dns_name_copy(vevent->name,
4090                               dns_fixedname_name(&hevent->foundname), NULL)
4091                               == ISC_R_SUCCESS);
4092                 dns_db_attach(fctx->cache, &hevent->db);
4093                 dns_db_transfernode(fctx->cache, &node, &hevent->node);
4094                 clone_results(fctx);
4095         }
4096
4097  noanswer_response:
4098         if (node != NULL)
4099                 dns_db_detachnode(fctx->cache, &node);
4100
4101         UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4102
4103         fctx_done(fctx, result, __LINE__); /* Locks bucket. */
4104
4105  cleanup_event:
4106         INSIST(node == NULL);
4107         isc_event_free(&event);
4108 }
4109
4110 static inline isc_result_t
4111 cache_name(fetchctx_t *fctx, dns_name_t *name, dns_adbaddrinfo_t *addrinfo,
4112            isc_stdtime_t now)
4113 {
4114         dns_rdataset_t *rdataset, *sigrdataset;
4115         dns_rdataset_t *addedrdataset, *ardataset, *asigrdataset;
4116         dns_rdataset_t *valrdataset = NULL, *valsigrdataset = NULL;
4117         dns_dbnode_t *node, **anodep;
4118         dns_db_t **adbp;
4119         dns_name_t *aname;
4120         dns_resolver_t *res;
4121         isc_boolean_t need_validation, secure_domain, have_answer;
4122         isc_result_t result, eresult;
4123         dns_fetchevent_t *event;
4124         unsigned int options;
4125         isc_task_t *task;
4126         isc_boolean_t fail;
4127         unsigned int valoptions = 0;
4128
4129         /*
4130          * The appropriate bucket lock must be held.
4131          */
4132
4133         res = fctx->res;
4134         need_validation = ISC_FALSE;
4135         secure_domain = ISC_FALSE;
4136         have_answer = ISC_FALSE;
4137         eresult = ISC_R_SUCCESS;
4138         task = res->buckets[fctx->bucketnum].task;
4139
4140         /*
4141          * Is DNSSEC validation required for this name?
4142          */
4143         if (res->view->enablevalidation) {
4144                 result = dns_keytable_issecuredomain(res->view->secroots, name,
4145                                                      &secure_domain);
4146                 if (result != ISC_R_SUCCESS)
4147                         return (result);
4148
4149                 if (!secure_domain && res->view->dlv != NULL) {
4150                         valoptions = DNS_VALIDATOR_DLV;
4151                         secure_domain = ISC_TRUE;
4152                 }
4153         }
4154
4155         if ((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0)
4156                 need_validation = ISC_FALSE;
4157         else
4158                 need_validation = secure_domain;
4159
4160         adbp = NULL;
4161         aname = NULL;
4162         anodep = NULL;
4163         ardataset = NULL;
4164         asigrdataset = NULL;
4165         event = NULL;
4166         if ((name->attributes & DNS_NAMEATTR_ANSWER) != 0 &&
4167             !need_validation) {
4168                 have_answer = ISC_TRUE;
4169                 event = ISC_LIST_HEAD(fctx->events);
4170                 if (event != NULL) {
4171                         adbp = &event->db;
4172                         aname = dns_fixedname_name(&event->foundname);
4173                         result = dns_name_copy(name, aname, NULL);
4174                         if (result != ISC_R_SUCCESS)
4175                                 return (result);
4176                         anodep = &event->node;
4177                         /*
4178                          * If this is an ANY, SIG or RRSIG query, we're not
4179                          * going to return any rdatasets, unless we encountered
4180                          * a CNAME or DNAME as "the answer".  In this case,
4181                          * we're going to return DNS_R_CNAME or DNS_R_DNAME
4182                          * and we must set up the rdatasets.
4183                          */
4184                         if ((fctx->type != dns_rdatatype_any &&
4185                              fctx->type != dns_rdatatype_rrsig &&
4186                              fctx->type != dns_rdatatype_sig) ||
4187                             (name->attributes & DNS_NAMEATTR_CHAINING) != 0) {
4188                                 ardataset = event->rdataset;
4189                                 asigrdataset = event->sigrdataset;
4190                         }
4191                 }
4192         }
4193
4194         /*
4195          * Find or create the cache node.
4196          */
4197         node = NULL;
4198         result = dns_db_findnode(fctx->cache, name, ISC_TRUE, &node);
4199         if (result != ISC_R_SUCCESS)
4200                 return (result);
4201
4202         /*
4203          * Cache or validate each cacheable rdataset.
4204          */
4205         fail = ISC_TF((fctx->res->options & DNS_RESOLVER_CHECKNAMESFAIL) != 0);
4206         for (rdataset = ISC_LIST_HEAD(name->list);
4207              rdataset != NULL;
4208              rdataset = ISC_LIST_NEXT(rdataset, link)) {
4209                 if (!CACHE(rdataset))
4210                         continue;
4211                 if (CHECKNAMES(rdataset)) {
4212                         char namebuf[DNS_NAME_FORMATSIZE];
4213                         char typebuf[DNS_RDATATYPE_FORMATSIZE];
4214                         char classbuf[DNS_RDATATYPE_FORMATSIZE];
4215
4216                         dns_name_format(name, namebuf, sizeof(namebuf));
4217                         dns_rdatatype_format(rdataset->type, typebuf,
4218                                              sizeof(typebuf));
4219                         dns_rdataclass_format(rdataset->rdclass, classbuf,
4220                                               sizeof(classbuf));
4221                         isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
4222                                       DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
4223                                       "check-names %s %s/%s/%s",
4224                                       fail ? "failure" : "warning",
4225                                       namebuf, typebuf, classbuf);
4226                         if (fail) {
4227                                 if (ANSWER(rdataset)) {
4228                                         dns_db_detachnode(fctx->cache, &node);
4229                                         return (DNS_R_BADNAME);
4230                                 }
4231                                 continue;
4232                         }
4233                 }
4234
4235                 /*
4236                  * Enforce the configure maximum cache TTL.
4237                  */
4238                 if (rdataset->ttl > res->view->maxcachettl)
4239                         rdataset->ttl = res->view->maxcachettl;
4240
4241                 /*
4242                  * If this rrset is in a secure domain, do DNSSEC validation
4243                  * for it, unless it is glue.
4244                  */
4245                 if (secure_domain && rdataset->trust != dns_trust_glue) {
4246                         /*
4247                          * RRSIGs are validated as part of validating the
4248                          * type they cover.
4249                          */
4250                         if (rdataset->type == dns_rdatatype_rrsig)
4251                                 continue;
4252                         /*
4253                          * Find the SIG for this rdataset, if we have it.
4254                          */
4255                         for (sigrdataset = ISC_LIST_HEAD(name->list);
4256                              sigrdataset != NULL;
4257                              sigrdataset = ISC_LIST_NEXT(sigrdataset, link)) {
4258                                 if (sigrdataset->type == dns_rdatatype_rrsig &&
4259                                     sigrdataset->covers == rdataset->type)
4260                                         break;
4261                         }
4262                         if (sigrdataset == NULL) {
4263                                 if (!ANSWER(rdataset) && need_validation) {
4264                                         /*
4265                                          * Ignore non-answer rdatasets that
4266                                          * are missing signatures.
4267                                          */
4268                                         continue;
4269                                 }
4270                         }
4271
4272                         /*
4273                          * Normalize the rdataset and sigrdataset TTLs.
4274                          */
4275                         if (sigrdataset != NULL) {
4276                                 rdataset->ttl = ISC_MIN(rdataset->ttl,
4277                                                         sigrdataset->ttl);
4278                                 sigrdataset->ttl = rdataset->ttl;
4279                         }
4280
4281                         /*
4282                          * Cache this rdataset/sigrdataset pair as
4283                          * pending data.
4284                          */
4285                         rdataset->trust = dns_trust_pending;
4286                         if (sigrdataset != NULL)
4287                                 sigrdataset->trust = dns_trust_pending;
4288                         if (!need_validation || !ANSWER(rdataset)) {
4289                                 addedrdataset = ardataset;
4290                                 result = dns_db_addrdataset(fctx->cache, node,
4291                                                             NULL, now, rdataset,
4292                                                             0, addedrdataset);
4293                                 if (result == DNS_R_UNCHANGED) {
4294                                         result = ISC_R_SUCCESS;
4295                                         if (!need_validation &&
4296                                             ardataset != NULL &&
4297                                             ardataset->type == 0) {
4298                                                 /*
4299                                                  * The answer in the cache is
4300                                                  * better than the answer we
4301                                                  * found, and is a negative
4302                                                  * cache entry, so we must set
4303                                                  * eresult appropriately.
4304                                                  */
4305                                                 if (NXDOMAIN(ardataset))
4306                                                         eresult =
4307                                                            DNS_R_NCACHENXDOMAIN;
4308                                                 else
4309                                                         eresult =
4310                                                            DNS_R_NCACHENXRRSET;
4311                                                 /*
4312                                                  * We have a negative response
4313                                                  * from the cache so don't
4314                                                  * attempt to add the RRSIG
4315                                                  * rrset.
4316                                                  */
4317                                                 continue;
4318                                         }
4319                                 }
4320                                 if (result != ISC_R_SUCCESS)
4321                                         break;
4322                                 if (sigrdataset != NULL) {
4323                                         addedrdataset = asigrdataset;
4324                                         result = dns_db_addrdataset(fctx->cache,
4325                                                                 node, NULL, now,
4326                                                                 sigrdataset, 0,
4327                                                                 addedrdataset);
4328                                         if (result == DNS_R_UNCHANGED)
4329                                                 result = ISC_R_SUCCESS;
4330                                         if (result != ISC_R_SUCCESS)
4331                                                 break;
4332                                 } else if (!ANSWER(rdataset))
4333                                         continue;
4334                         }
4335
4336                         if (ANSWER(rdataset) && need_validation) {
4337                                 if (fctx->type != dns_rdatatype_any &&
4338                                     fctx->type != dns_rdatatype_rrsig &&
4339                                     fctx->type != dns_rdatatype_sig) {
4340                                         /*
4341                                          * This is The Answer.  We will
4342                                          * validate it, but first we cache
4343                                          * the rest of the response - it may
4344                                          * contain useful keys.
4345                                          */
4346                                         INSIST(valrdataset == NULL &&
4347                                                valsigrdataset == NULL);
4348                                         valrdataset = rdataset;
4349                                         valsigrdataset = sigrdataset;
4350                                 } else {
4351                                         /*
4352                                          * This is one of (potentially)
4353                                          * multiple answers to an ANY
4354                                          * or SIG query.  To keep things
4355                                          * simple, we just start the
4356                                          * validator right away rather
4357                                          * than caching first and
4358                                          * having to remember which
4359                                          * rdatasets needed validation.
4360                                          */
4361                                         result = valcreate(fctx, addrinfo,
4362                                                            name, rdataset->type,
4363                                                            rdataset,
4364                                                            sigrdataset,
4365                                                            valoptions, task);
4366                                         /*
4367                                          * Defer any further validations.
4368                                          * This prevents multiple validators
4369                                          * from manipulating fctx->rmessage
4370                                          * simultaneously.
4371                                          */
4372                                         valoptions |= DNS_VALIDATOR_DEFER;
4373                                 }
4374                         } else if (CHAINING(rdataset)) {
4375                                 if (rdataset->type == dns_rdatatype_cname)
4376                                         eresult = DNS_R_CNAME;
4377                                 else {
4378                                         INSIST(rdataset->type ==
4379                                                dns_rdatatype_dname);
4380                                         eresult = DNS_R_DNAME;
4381                                 }
4382                         }
4383                 } else if (!EXTERNAL(rdataset)) {
4384                         /*
4385                          * It's OK to cache this rdataset now.
4386                          */
4387                         if (ANSWER(rdataset))
4388                                 addedrdataset = ardataset;
4389                         else if (ANSWERSIG(rdataset))
4390                                 addedrdataset = asigrdataset;
4391                         else
4392                                 addedrdataset = NULL;
4393                         if (CHAINING(rdataset)) {
4394                                 if (rdataset->type == dns_rdatatype_cname)
4395                                         eresult = DNS_R_CNAME;
4396                                 else {
4397                                         INSIST(rdataset->type ==
4398                                                dns_rdatatype_dname);
4399                                         eresult = DNS_R_DNAME;
4400                                 }
4401                         }
4402                         if (rdataset->trust == dns_trust_glue &&
4403                             (rdataset->type == dns_rdatatype_ns ||
4404                              (rdataset->type == dns_rdatatype_rrsig &&
4405                               rdataset->covers == dns_rdatatype_ns))) {
4406                                 /*
4407                                  * If the trust level is 'dns_trust_glue'
4408                                  * then we are adding data from a referral
4409                                  * we got while executing the search algorithm.
4410                                  * New referral data always takes precedence
4411                                  * over the existing cache contents.
4412                                  */
4413                                 options = DNS_DBADD_FORCE;
4414                         } else
4415                                 options = 0;
4416                         /*
4417                          * Now we can add the rdataset.
4418                          */
4419                         result = dns_db_addrdataset(fctx->cache,
4420                                                     node, NULL, now,
4421                                                     rdataset,
4422                                                     options,
4423                                                     addedrdataset);
4424                         if (result == DNS_R_UNCHANGED) {
4425                                 if (ANSWER(rdataset) &&
4426                                     ardataset != NULL &&
4427                                     ardataset->type == 0) {
4428                                         /*
4429                                          * The answer in the cache is better
4430                                          * than the answer we found, and is
4431                                          * a negative cache entry, so we
4432                                          * must set eresult appropriately.
4433                                          */
4434                                         if (NXDOMAIN(ardataset))
4435                                                 eresult = DNS_R_NCACHENXDOMAIN;
4436                                         else
4437                                                 eresult = DNS_R_NCACHENXRRSET;
4438                                 }
4439                                 result = ISC_R_SUCCESS;
4440                         } else if (result != ISC_R_SUCCESS)
4441                                 break;
4442                 }
4443         }
4444
4445         if (valrdataset != NULL)
4446                 result = valcreate(fctx, addrinfo, name, fctx->type,
4447                                    valrdataset, valsigrdataset, valoptions,
4448                                    task);
4449
4450         if (result == ISC_R_SUCCESS && have_answer) {
4451                 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
4452                 if (event != NULL) {
4453                         /*
4454                          * Negative results must be indicated in event->result.
4455                          */
4456                         if (dns_rdataset_isassociated(event->rdataset) &&
4457                             event->rdataset->type == dns_rdatatype_none) {
4458                                 INSIST(eresult == DNS_R_NCACHENXDOMAIN ||
4459                                        eresult == DNS_R_NCACHENXRRSET);
4460                         }
4461                         event->result = eresult;
4462                         dns_db_attach(fctx->cache, adbp);
4463                         dns_db_transfernode(fctx->cache, &node, anodep);
4464                         clone_results(fctx);
4465                 }
4466         }
4467
4468         if (node != NULL)
4469                 dns_db_detachnode(fctx->cache, &node);
4470
4471         return (result);
4472 }
4473
4474 static inline isc_result_t
4475 cache_message(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo, isc_stdtime_t now)
4476 {
4477         isc_result_t result;
4478         dns_section_t section;
4479         dns_name_t *name;
4480
4481         FCTXTRACE("cache_message");
4482
4483         fctx->attributes &= ~FCTX_ATTR_WANTCACHE;
4484
4485         LOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4486
4487         for (section = DNS_SECTION_ANSWER;
4488              section <= DNS_SECTION_ADDITIONAL;
4489              section++) {
4490                 result = dns_message_firstname(fctx->rmessage, section);
4491                 while (result == ISC_R_SUCCESS) {
4492                         name = NULL;
4493                         dns_message_currentname(fctx->rmessage, section,
4494                                                 &name);
4495                         if ((name->attributes & DNS_NAMEATTR_CACHE) != 0) {
4496                                 result = cache_name(fctx, name, addrinfo, now);
4497                                 if (result != ISC_R_SUCCESS)
4498                                         break;
4499                         }
4500                         result = dns_message_nextname(fctx->rmessage, section);
4501                 }
4502                 if (result != ISC_R_NOMORE)
4503                         break;
4504         }
4505         if (result == ISC_R_NOMORE)
4506                 result = ISC_R_SUCCESS;
4507
4508         UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
4509
4510         return (result);
4511 }
4512
4513 /*
4514  * Do what dns_ncache_add() does, and then compute an appropriate eresult.
4515  */
4516 static isc_result_t
4517 ncache_adderesult(dns_message_t *message, dns_db_t *cache, dns_dbnode_t *node,
4518                   dns_rdatatype_t covers, isc_stdtime_t now, dns_ttl_t maxttl,
4519                   dns_rdataset_t *ardataset,
4520                   isc_result_t *eresultp)
4521 {
4522         isc_result_t result;
4523         dns_rdataset_t rdataset;
4524
4525         if (ardataset == NULL) {
4526                 dns_rdataset_init(&rdataset);
4527                 ardataset = &rdataset;
4528         }
4529         result = dns_ncache_add(message, cache, node, covers, now,
4530                                 maxttl, ardataset);
4531         if (result == DNS_R_UNCHANGED || result == ISC_R_SUCCESS) {
4532                 /*
4533                  * If the cache now contains a negative entry and we
4534                  * care about whether it is DNS_R_NCACHENXDOMAIN or
4535                  * DNS_R_NCACHENXRRSET then extract it.
4536                  */
4537                 if (ardataset->type == 0) {
4538                         /*
4539                          * The cache data is a negative cache entry.
4540                          */
4541                         if (NXDOMAIN(ardataset))
4542                                 *eresultp = DNS_R_NCACHENXDOMAIN;
4543                         else
4544                                 *eresultp = DNS_R_NCACHENXRRSET;
4545                 } else {
4546                         /*
4547                          * Either we don't care about the nature of the
4548                          * cache rdataset (because no fetch is interested
4549                          * in the outcome), or the cache rdataset is not
4550                          * a negative cache entry.  Whichever case it is,
4551                          * we can return success.
4552                          *
4553                          * XXXRTH  There's a CNAME/DNAME problem here.
4554                          */
4555                         *eresultp = ISC_R_SUCCESS;
4556                 }
4557                 result = ISC_R_SUCCESS;
4558         }
4559         if (ardataset == &rdataset && dns_rdataset_isassociated(ardataset))
4560                 dns_rdataset_disassociate(ardataset);
4561
4562         return (result);
4563 }
4564
4565 static inline isc_result_t
4566 ncache_message(fetchctx_t *fctx, dns_adbaddrinfo_t *addrinfo,
4567                dns_rdatatype_t covers, isc_stdtime_t now)
4568 {
4569         isc_result_t result, eresult;
4570         dns_name_t *name;
4571         dns_resolver_t *res;
4572         dns_db_t **adbp;
4573         dns_dbnode_t *node, **anodep;
4574         dns_rdataset_t *ardataset;
4575         isc_boolean_t need_validation, secure_domain;
4576         dns_name_t *aname;
4577         dns_fetchevent_t *event;
4578         isc_uint32_t ttl;
4579         unsigned int valoptions = 0;
4580
4581         FCTXTRACE("ncache_message");
4582
4583         fctx->attributes &= ~FCTX_ATTR_WANTNCACHE;
4584
4585         res = fctx->res;
4586         need_validation = ISC_FALSE;
4587         secure_domain = ISC_FALSE;
4588         eresult = ISC_R_SUCCESS;
4589         name = &fctx->name;
4590         node = NULL;
4591
4592         /*
4593          * XXXMPA remove when we follow cnames and adjust the setting
4594          * of FCTX_ATTR_WANTNCACHE in noanswer_response().
4595          */
4596         INSIST(fctx->rmessage->counts[DNS_SECTION_ANSWER] == 0);
4597
4598         /*
4599          * Is DNSSEC validation required for this name?
4600          */
4601         if (fctx->res->view->enablevalidation) {
4602                 result = dns_keytable_issecuredomain(res->view->secroots, name,
4603                                                      &secure_domain);
4604                 if (result != ISC_R_SUCCESS)
4605                         return (result);
4606
4607                 if (!secure_domain && res->view->dlv != NULL) {
4608                         valoptions = DNS_VALIDATOR_DLV;
4609                         secure_domain = ISC_TRUE;
4610                 }
4611         }
4612
4613         if ((fctx->options & DNS_FETCHOPT_NOVALIDATE) != 0)
4614                 need_validation = ISC_FALSE;
4615         else
4616                 need_validation = secure_domain;
4617
4618         if (secure_domain) {
4619                 /*
4620                  * Mark all rdatasets as pending.
4621                  */
4622                 dns_rdataset_t *trdataset;
4623                 dns_name_t *tname;
4624
4625                 result = dns_message_firstname(fctx->rmessage,
4626                                                DNS_SECTION_AUTHORITY);
4627                 while (result == ISC_R_SUCCESS) {
4628                         tname = NULL;
4629                         dns_message_currentname(fctx->rmessage,
4630                                                 DNS_SECTION_AUTHORITY,
4631                                                 &tname);
4632                         for (trdataset = ISC_LIST_HEAD(tname->list);
4633                              trdataset != NULL;
4634                              trdataset = ISC_LIST_NEXT(trdataset, link))
4635                                 trdataset->trust = dns_trust_pending;
4636                         result = dns_message_nextname(fctx->rmessage,
4637                                                       DNS_SECTION_AUTHORITY);
4638                 }
4639                 if (result != ISC_R_NOMORE)
4640                         return (result);
4641
4642         }
4643
4644         if (need_validation) {
4645                 /*
4646                  * Do negative response validation.
4647                  */
4648                 result = valcreate(fctx, addrinfo, name, fctx->type,
4649                                    NULL, NULL, valoptions,
4650                                    res->buckets[fctx->bucketnum].task);
4651                 /*
4652                  * If validation is necessary, return now.  Otherwise continue
4653                  * to process the message, letting the validation complete
4654                  * in its own good time.
4655                  */
4656                 return (result);
4657         }
4658
4659         LOCK(&res->buckets[fctx->bucketnum].lock);
4660
4661         adbp = NULL;
4662         aname = NULL;
4663         anodep = NULL;
4664         ardataset = NULL;
4665         if (!HAVE_ANSWER(fctx)) {
4666                 event = ISC_LIST_HEAD(fctx->events);
4667                 if (event != NULL) {
4668                         adbp = &event->db;
4669                         aname = dns_fixedname_name(&event->foundname);
4670                         result = dns_name_copy(name, aname, NULL);
4671                         if (result != ISC_R_SUCCESS)
4672                                 goto unlock;
4673                         anodep = &event->node;
4674                         ardataset = event->rdataset;
4675                 }
4676         } else
4677                 event = NULL;
4678
4679         result = dns_db_findnode(fctx->cache, name, ISC_TRUE, &node);
4680         if (result != ISC_R_SUCCESS)
4681                 goto unlock;
4682
4683         /*
4684          * If we are asking for a SOA record set the cache time
4685          * to zero to facilitate locating the containing zone of
4686          * a arbitrary zone.
4687          */
4688         ttl = fctx->res->view->maxncachettl;
4689         if (fctx->type == dns_rdatatype_soa &&
4690             covers == dns_rdatatype_any &&
4691             fctx->res->zero_no_soa_ttl)
4692                 ttl = 0;
4693
4694         result = ncache_adderesult(fctx->rmessage, fctx->cache, node,
4695                                    covers, now, ttl, ardataset, &eresult);
4696         if (result != ISC_R_SUCCESS)
4697                 goto unlock;
4698
4699         if (!HAVE_ANSWER(fctx)) {
4700                 fctx->attributes |= FCTX_ATTR_HAVEANSWER;
4701                 if (event != NULL) {
4702                         event->result = eresult;
4703                         dns_db_attach(fctx->cache, adbp);
4704                         dns_db_transfernode(fctx->cache, &node, anodep);
4705                         clone_results(fctx);
4706                 }
4707         }
4708
4709  unlock:
4710         UNLOCK(&res->buckets[fctx->bucketnum].lock);
4711
4712         if (node != NULL)
4713                 dns_db_detachnode(fctx->cache, &node);
4714
4715         return (result);
4716 }
4717
4718 static inline void
4719 mark_related(dns_name_t *name, dns_rdataset_t *rdataset,
4720              isc_boolean_t external, isc_boolean_t gluing)
4721 {
4722         name->attributes |= DNS_NAMEATTR_CACHE;
4723         if (gluing) {
4724                 rdataset->trust = dns_trust_glue;
4725                 /*
4726                  * Glue with 0 TTL causes problems.  We force the TTL to
4727                  * 1 second to prevent this.
4728                  */
4729                 if (rdataset->ttl == 0)
4730                         rdataset->ttl = 1;
4731         } else
4732                 rdataset->trust = dns_trust_additional;
4733         /*
4734          * Avoid infinite loops by only marking new rdatasets.
4735          */
4736         if (!CACHE(rdataset)) {
4737                 name->attributes |= DNS_NAMEATTR_CHASE;
4738                 rdataset->attributes |= DNS_RDATASETATTR_CHASE;
4739         }
4740         rdataset->attributes |= DNS_RDATASETATTR_CACHE;
4741         if (external)
4742                 rdataset->attributes |= DNS_RDATASETATTR_EXTERNAL;
4743 }
4744
4745 static isc_result_t
4746 check_related(void *arg, dns_name_t *addname, dns_rdatatype_t type) {
4747         fetchctx_t *fctx = arg;
4748         isc_result_t result;
4749         dns_name_t *name;
4750         dns_rdataset_t *rdataset;
4751         isc_boolean_t external;
4752         dns_rdatatype_t rtype;
4753         isc_boolean_t gluing;
4754
4755         REQUIRE(VALID_FCTX(fctx));
4756
4757         if (GLUING(fctx))
4758                 gluing = ISC_TRUE;
4759         else
4760                 gluing = ISC_FALSE;
4761         name = NULL;
4762         rdataset = NULL;
4763         result = dns_message_findname(fctx->rmessage, DNS_SECTION_ADDITIONAL,
4764                                       addname, dns_rdatatype_any, 0, &name,
4765                                       NULL);
4766         if (result == ISC_R_SUCCESS) {
4767                 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
4768                 if (type == dns_rdatatype_a) {
4769                         for (rdataset = ISC_LIST_HEAD(name->list);
4770                              rdataset != NULL;
4771                              rdataset = ISC_LIST_NEXT(rdataset, link)) {
4772                                 if (rdataset->type == dns_rdatatype_rrsig)
4773                                         rtype = rdataset->covers;
4774                                 else
4775                                         rtype = rdataset->type;
4776                                 if (rtype == dns_rdatatype_a ||
4777                                     rtype == dns_rdatatype_aaaa)
4778                                         mark_related(name, rdataset, external,
4779                                                      gluing);
4780                         }
4781                 } else {
4782                         result = dns_message_findtype(name, type, 0,
4783                                                       &rdataset);
4784                         if (result == ISC_R_SUCCESS) {
4785                                 mark_related(name, rdataset, external, gluing);
4786                                 /*
4787                                  * Do we have its SIG too?
4788                                  */
4789                                 rdataset = NULL;
4790                                 result = dns_message_findtype(name,
4791                                                       dns_rdatatype_rrsig,
4792                                                       type, &rdataset);
4793                                 if (result == ISC_R_SUCCESS)
4794                                         mark_related(name, rdataset, external,
4795                                                      gluing);
4796                         }
4797                 }
4798         }
4799
4800         return (ISC_R_SUCCESS);
4801 }
4802
4803 static void
4804 chase_additional(fetchctx_t *fctx) {
4805         isc_boolean_t rescan;
4806         dns_section_t section = DNS_SECTION_ADDITIONAL;
4807         isc_result_t result;
4808
4809  again:
4810         rescan = ISC_FALSE;
4811
4812         for (result = dns_message_firstname(fctx->rmessage, section);
4813              result == ISC_R_SUCCESS;
4814              result = dns_message_nextname(fctx->rmessage, section)) {
4815                 dns_name_t *name = NULL;
4816                 dns_rdataset_t *rdataset;
4817                 dns_message_currentname(fctx->rmessage, DNS_SECTION_ADDITIONAL,
4818                                         &name);
4819                 if ((name->attributes & DNS_NAMEATTR_CHASE) == 0)
4820                         continue;
4821                 name->attributes &= ~DNS_NAMEATTR_CHASE;
4822                 for (rdataset = ISC_LIST_HEAD(name->list);
4823                      rdataset != NULL;
4824                      rdataset = ISC_LIST_NEXT(rdataset, link)) {
4825                         if (CHASE(rdataset)) {
4826                                 rdataset->attributes &= ~DNS_RDATASETATTR_CHASE;
4827                                 (void)dns_rdataset_additionaldata(rdataset,
4828                                                                   check_related,
4829                                                                   fctx);
4830                                 rescan = ISC_TRUE;
4831                         }
4832                 }
4833         }
4834         if (rescan)
4835                 goto again;
4836 }
4837
4838 static inline isc_result_t
4839 cname_target(dns_rdataset_t *rdataset, dns_name_t *tname) {
4840         isc_result_t result;
4841         dns_rdata_t rdata = DNS_RDATA_INIT;
4842         dns_rdata_cname_t cname;
4843
4844         result = dns_rdataset_first(rdataset);
4845         if (result != ISC_R_SUCCESS)
4846                 return (result);
4847         dns_rdataset_current(rdataset, &rdata);
4848         result = dns_rdata_tostruct(&rdata, &cname, NULL);
4849         if (result != ISC_R_SUCCESS)
4850                 return (result);
4851         dns_name_init(tname, NULL);
4852         dns_name_clone(&cname.cname, tname);
4853         dns_rdata_freestruct(&cname);
4854
4855         return (ISC_R_SUCCESS);
4856 }
4857
4858 static inline isc_result_t
4859 dname_target(dns_rdataset_t *rdataset, dns_name_t *qname, dns_name_t *oname,
4860              dns_fixedname_t *fixeddname)
4861 {
4862         isc_result_t result;
4863         dns_rdata_t rdata = DNS_RDATA_INIT;
4864         unsigned int nlabels;
4865         int order;
4866         dns_namereln_t namereln;
4867         dns_rdata_dname_t dname;
4868         dns_fixedname_t prefix;
4869
4870         /*
4871          * Get the target name of the DNAME.
4872          */
4873
4874         result = dns_rdataset_first(rdataset);
4875         if (result != ISC_R_SUCCESS)
4876                 return (result);
4877         dns_rdataset_current(rdataset, &rdata);
4878         result = dns_rdata_tostruct(&rdata, &dname, NULL);
4879         if (result != ISC_R_SUCCESS)
4880                 return (result);
4881
4882         /*
4883          * Get the prefix of qname.
4884          */
4885         namereln = dns_name_fullcompare(qname, oname, &order, &nlabels);
4886         if (namereln != dns_namereln_subdomain) {
4887                 dns_rdata_freestruct(&dname);
4888                 return (DNS_R_FORMERR);
4889         }
4890         dns_fixedname_init(&prefix);
4891         dns_name_split(qname, nlabels, dns_fixedname_name(&prefix), NULL);
4892         dns_fixedname_init(fixeddname);
4893         result = dns_name_concatenate(dns_fixedname_name(&prefix),
4894                                       &dname.dname,
4895                                       dns_fixedname_name(fixeddname), NULL);
4896         dns_rdata_freestruct(&dname);
4897         return (result);
4898 }
4899
4900 /*
4901  * Handle a no-answer response (NXDOMAIN, NXRRSET, or referral).
4902  * If bind8_ns_resp is ISC_TRUE, this is a suspected BIND 8
4903  * response to an NS query that should be treated as a referral
4904  * even though the NS records occur in the answer section
4905  * rather than the authority section.
4906  */
4907 static isc_result_t
4908 noanswer_response(fetchctx_t *fctx, dns_name_t *oqname,
4909                   isc_boolean_t bind8_ns_resp)
4910 {
4911         isc_result_t result;
4912         dns_message_t *message;
4913         dns_name_t *name, *qname, *ns_name, *soa_name, *ds_name;
4914         dns_rdataset_t *rdataset, *ns_rdataset;
4915         isc_boolean_t aa, negative_response;
4916         dns_rdatatype_t type;
4917         dns_section_t section =
4918                 bind8_ns_resp ? DNS_SECTION_ANSWER : DNS_SECTION_AUTHORITY;
4919
4920         FCTXTRACE("noanswer_response");
4921
4922         message = fctx->rmessage;
4923
4924         /*
4925          * Setup qname.
4926          */
4927         if (oqname == NULL) {
4928                 /*
4929                  * We have a normal, non-chained negative response or
4930                  * referral.
4931                  */
4932                 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0)
4933                         aa = ISC_TRUE;
4934                 else
4935                         aa = ISC_FALSE;
4936                 qname = &fctx->name;
4937         } else {
4938                 /*
4939                  * We're being invoked by answer_response() after it has
4940                  * followed a CNAME/DNAME chain.
4941                  */
4942                 qname = oqname;
4943                 aa = ISC_FALSE;
4944                 /*
4945                  * If the current qname is not a subdomain of the query
4946                  * domain, there's no point in looking at the authority
4947                  * section without doing DNSSEC validation.
4948                  *
4949                  * Until we do that validation, we'll just return success
4950                  * in this case.
4951                  */
4952                 if (!dns_name_issubdomain(qname, &fctx->domain))
4953                         return (ISC_R_SUCCESS);
4954         }
4955
4956         /*
4957          * We have to figure out if this is a negative response, or a
4958          * referral.
4959          */
4960
4961         /*
4962          * Sometimes we can tell if its a negative response by looking at
4963          * the message header.
4964          */
4965         negative_response = ISC_FALSE;
4966         if (message->rcode == dns_rcode_nxdomain ||
4967             (message->counts[DNS_SECTION_ANSWER] == 0 &&
4968              message->counts[DNS_SECTION_AUTHORITY] == 0))
4969                 negative_response = ISC_TRUE;
4970
4971         /*
4972          * Process the authority section.
4973          */
4974         ns_name = NULL;
4975         ns_rdataset = NULL;
4976         soa_name = NULL;
4977         ds_name = NULL;
4978         result = dns_message_firstname(message, section);
4979         while (result == ISC_R_SUCCESS) {
4980                 name = NULL;
4981                 dns_message_currentname(message, section, &name);
4982                 if (dns_name_issubdomain(name, &fctx->domain)) {
4983                         /*
4984                          * Look for NS/SOA RRsets first.
4985                          */
4986                         for (rdataset = ISC_LIST_HEAD(name->list);
4987                              rdataset != NULL;
4988                              rdataset = ISC_LIST_NEXT(rdataset, link)) {
4989                                 type = rdataset->type;
4990                                 if (type == dns_rdatatype_rrsig)
4991                                         type = rdataset->covers;
4992                                 if (((type == dns_rdatatype_ns ||
4993                                       type == dns_rdatatype_soa) &&
4994                                      !dns_name_issubdomain(qname, name)))
4995                                         return (DNS_R_FORMERR);
4996                                 if (type == dns_rdatatype_ns) {
4997                                         /*
4998                                          * NS or RRSIG NS.
4999                                          *
5000                                          * Only one set of NS RRs is allowed.
5001                                          */
5002                                         if (rdataset->type ==
5003                                             dns_rdatatype_ns) {
5004                                                 if (ns_name != NULL &&
5005                                                     name != ns_name)
5006                                                         return (DNS_R_FORMERR);
5007                                                 ns_name = name;
5008                                                 ns_rdataset = rdataset;
5009                                         }
5010                                         name->attributes |=
5011                                                 DNS_NAMEATTR_CACHE;
5012                                         rdataset->attributes |=
5013                                                 DNS_RDATASETATTR_CACHE;
5014                                         rdataset->trust = dns_trust_glue;
5015                                 }
5016                                 if (type == dns_rdatatype_soa) {
5017                                         /*
5018                                          * SOA, or RRSIG SOA.
5019                                          *
5020                                          * Only one SOA is allowed.
5021                                          */
5022                                         if (rdataset->type ==
5023                                             dns_rdatatype_soa) {
5024                                                 if (soa_name != NULL &&
5025                                                     name != soa_name)
5026                                                         return (DNS_R_FORMERR);
5027                                                 soa_name = name;
5028                                         }
5029                                         name->attributes |=
5030                                                 DNS_NAMEATTR_NCACHE;
5031                                         rdataset->attributes |=
5032                                                 DNS_RDATASETATTR_NCACHE;
5033                                         if (aa)
5034                                                 rdataset->trust =
5035                                                     dns_trust_authauthority;
5036                                         else
5037                                                 rdataset->trust =
5038                                                         dns_trust_additional;
5039                                 }
5040                         }
5041                 }
5042                 result = dns_message_nextname(message, section);
5043                 if (result == ISC_R_NOMORE)
5044                         break;
5045                 else if (result != ISC_R_SUCCESS)
5046                         return (result);
5047         }
5048
5049         /*
5050          * A negative response has a SOA record (Type 2)
5051          * and a optional NS RRset (Type 1) or it has neither
5052          * a SOA or a NS RRset (Type 3, handled above) or
5053          * rcode is NXDOMAIN (handled above) in which case
5054          * the NS RRset is allowed (Type 4).
5055          */
5056         if (soa_name != NULL)
5057                 negative_response = ISC_TRUE;
5058
5059         result = dns_message_firstname(message, section);
5060         while (result == ISC_R_SUCCESS) {
5061                 name = NULL;
5062                 dns_message_currentname(message, section, &name);
5063                 if (dns_name_issubdomain(name, &fctx->domain)) {
5064                         for (rdataset = ISC_LIST_HEAD(name->list);
5065                              rdataset != NULL;
5066                              rdataset = ISC_LIST_NEXT(rdataset, link)) {
5067                                 type = rdataset->type;
5068                                 if (type == dns_rdatatype_rrsig)
5069                                         type = rdataset->covers;
5070                                 if (type == dns_rdatatype_nsec) {
5071                                         /*
5072                                          * NSEC or RRSIG NSEC.
5073                                          */
5074                                         if (negative_response) {
5075                                                 name->attributes |=
5076                                                         DNS_NAMEATTR_NCACHE;
5077                                                 rdataset->attributes |=
5078                                                         DNS_RDATASETATTR_NCACHE;
5079                                         } else {
5080                                                 name->attributes |=
5081                                                         DNS_NAMEATTR_CACHE;
5082                                                 rdataset->attributes |=
5083                                                         DNS_RDATASETATTR_CACHE;
5084                                         }
5085                                         if (aa)
5086                                                 rdataset->trust =
5087                                                     dns_trust_authauthority;
5088                                         else
5089                                                 rdataset->trust =
5090                                                         dns_trust_additional;
5091                                         /*
5092                                          * No additional data needs to be
5093                                          * marked.
5094                                          */
5095                                 } else if (type == dns_rdatatype_ds) {
5096                                         /*
5097                                          * DS or SIG DS.
5098                                          *
5099                                          * These should only be here if
5100                                          * this is a referral, and there
5101                                          * should only be one DS.
5102                                          */
5103                                         if (ns_name == NULL)
5104                                                 return (DNS_R_FORMERR);
5105                                         if (rdataset->type ==
5106                                             dns_rdatatype_ds) {
5107                                                 if (ds_name != NULL &&
5108                                                     name != ds_name)
5109                                                         return (DNS_R_FORMERR);
5110                                                 ds_name = name;
5111                                         }
5112                                         name->attributes |=
5113                                                 DNS_NAMEATTR_CACHE;
5114                                         rdataset->attributes |=
5115                                                 DNS_RDATASETATTR_CACHE;
5116                                         if (aa)
5117                                                 rdataset->trust =
5118                                                     dns_trust_authauthority;
5119                                         else
5120                                                 rdataset->trust =
5121                                                         dns_trust_additional;
5122                                 }
5123                         }
5124                 }
5125                 result = dns_message_nextname(message, section);
5126                 if (result == ISC_R_NOMORE)
5127                         break;
5128                 else if (result != ISC_R_SUCCESS)
5129                         return (result);
5130         }
5131
5132         /*
5133          * Trigger lookups for DNS nameservers.
5134          */
5135         if (negative_response && message->rcode == dns_rcode_noerror &&
5136             fctx->type == dns_rdatatype_ds && soa_name != NULL &&
5137             dns_name_equal(soa_name, qname) &&
5138             !dns_name_equal(qname, dns_rootname))
5139                 return (DNS_R_CHASEDSSERVERS);
5140
5141         /*
5142          * Did we find anything?
5143          */
5144         if (!negative_response && ns_name == NULL) {
5145                 /*
5146                  * Nope.
5147                  */
5148                 if (oqname != NULL) {
5149                         /*
5150                          * We've already got a partial CNAME/DNAME chain,
5151                          * and haven't found else anything useful here, but
5152                          * no error has occurred since we have an answer.
5153                          */
5154                         return (ISC_R_SUCCESS);
5155                 } else {
5156                         /*
5157                          * The responder is insane.
5158                          */
5159                         return (DNS_R_FORMERR);
5160                 }
5161         }
5162
5163         /*
5164          * If we found both NS and SOA, they should be the same name.
5165          */
5166         if (ns_name != NULL && soa_name != NULL && ns_name != soa_name)
5167                 return (DNS_R_FORMERR);
5168
5169         /*
5170          * Do we have a referral?  (We only want to follow a referral if
5171          * we're not following a chain.)
5172          */
5173         if (!negative_response && ns_name != NULL && oqname == NULL) {
5174                 /*
5175                  * We already know ns_name is a subdomain of fctx->domain.
5176                  * If ns_name is equal to fctx->domain, we're not making
5177                  * progress.  We return DNS_R_FORMERR so that we'll keep
5178                  * trying other servers.
5179                  */
5180                 if (dns_name_equal(ns_name, &fctx->domain))
5181                         return (DNS_R_FORMERR);
5182
5183                 /*
5184                  * If the referral name is not a parent of the query
5185                  * name, consider the responder insane.
5186                  */
5187                 if (! dns_name_issubdomain(&fctx->name, ns_name)) {
5188                         FCTXTRACE("referral to non-parent");
5189                         return (DNS_R_FORMERR);
5190                 }
5191
5192                 /*
5193                  * Mark any additional data related to this rdataset.
5194                  * It's important that we do this before we change the
5195                  * query domain.
5196                  */
5197                 INSIST(ns_rdataset != NULL);
5198                 fctx->attributes |= FCTX_ATTR_GLUING;
5199                 (void)dns_rdataset_additionaldata(ns_rdataset, check_related,
5200                                                   fctx);
5201                 fctx->attributes &= ~FCTX_ATTR_GLUING;
5202                 /*
5203                  * NS rdatasets with 0 TTL cause problems.
5204                  * dns_view_findzonecut() will not find them when we
5205                  * try to follow the referral, and we'll SERVFAIL
5206                  * because the best nameservers are now above QDOMAIN.
5207                  * We force the TTL to 1 second to prevent this.
5208                  */
5209                 if (ns_rdataset->ttl == 0)
5210                         ns_rdataset->ttl = 1;
5211                 /*
5212                  * Set the current query domain to the referral name.
5213                  *
5214                  * XXXRTH  We should check if we're in forward-only mode, and
5215                  *              if so we should bail out.
5216                  */
5217                 INSIST(dns_name_countlabels(&fctx->domain) > 0);
5218                 dns_name_free(&fctx->domain,
5219                               fctx->res->buckets[fctx->bucketnum].mctx);
5220                 if (dns_rdataset_isassociated(&fctx->nameservers))
5221                         dns_rdataset_disassociate(&fctx->nameservers);
5222                 dns_name_init(&fctx->domain, NULL);
5223                 result = dns_name_dup(ns_name,
5224                                       fctx->res->buckets[fctx->bucketnum].mctx,
5225                                       &fctx->domain);
5226                 if (result != ISC_R_SUCCESS)
5227                         return (result);
5228                 fctx->attributes |= FCTX_ATTR_WANTCACHE;
5229                 return (DNS_R_DELEGATION);
5230         }
5231
5232         /*
5233          * Since we're not doing a referral, we don't want to cache any
5234          * NS RRs we may have found.
5235          */
5236         if (ns_name != NULL)
5237                 ns_name->attributes &= ~DNS_NAMEATTR_CACHE;
5238
5239         if (negative_response && oqname == NULL)
5240                 fctx->attributes |= FCTX_ATTR_WANTNCACHE;
5241
5242         return (ISC_R_SUCCESS);
5243 }
5244
5245 static isc_result_t
5246 answer_response(fetchctx_t *fctx) {
5247         isc_result_t result;
5248         dns_message_t *message;
5249         dns_name_t *name, *qname, tname;
5250         dns_rdataset_t *rdataset;
5251         isc_boolean_t done, external, chaining, aa, found, want_chaining;
5252         isc_boolean_t have_answer, found_cname, found_type, wanted_chaining;
5253         unsigned int aflag;
5254         dns_rdatatype_t type;
5255         dns_fixedname_t dname, fqname;
5256
5257         FCTXTRACE("answer_response");
5258
5259         message = fctx->rmessage;
5260
5261         /*
5262          * Examine the answer section, marking those rdatasets which are
5263          * part of the answer and should be cached.
5264          */
5265
5266         done = ISC_FALSE;
5267         found_cname = ISC_FALSE;
5268         found_type = ISC_FALSE;
5269         chaining = ISC_FALSE;
5270         have_answer = ISC_FALSE;
5271         want_chaining = ISC_FALSE;
5272         if ((message->flags & DNS_MESSAGEFLAG_AA) != 0)
5273                 aa = ISC_TRUE;
5274         else
5275                 aa = ISC_FALSE;
5276         qname = &fctx->name;
5277         type = fctx->type;
5278         result = dns_message_firstname(message, DNS_SECTION_ANSWER);
5279         while (!done && result == ISC_R_SUCCESS) {
5280                 name = NULL;
5281                 dns_message_currentname(message, DNS_SECTION_ANSWER, &name);
5282                 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
5283                 if (dns_name_equal(name, qname)) {
5284                         wanted_chaining = ISC_FALSE;
5285                         for (rdataset = ISC_LIST_HEAD(name->list);
5286                              rdataset != NULL;
5287                              rdataset = ISC_LIST_NEXT(rdataset, link)) {
5288                                 found = ISC_FALSE;
5289                                 want_chaining = ISC_FALSE;
5290                                 aflag = 0;
5291                                 if (rdataset->type == type && !found_cname) {
5292                                         /*
5293                                          * We've found an ordinary answer.
5294                                          */
5295                                         found = ISC_TRUE;
5296                                         found_type = ISC_TRUE;
5297                                         done = ISC_TRUE;
5298                                         aflag = DNS_RDATASETATTR_ANSWER;
5299                                 } else if (type == dns_rdatatype_any) {
5300                                         /*
5301                                          * We've found an answer matching
5302                                          * an ANY query.  There may be
5303                                          * more.
5304                                          */
5305                                         found = ISC_TRUE;
5306                                         aflag = DNS_RDATASETATTR_ANSWER;
5307                                 } else if (rdataset->type == dns_rdatatype_rrsig
5308                                            && rdataset->covers == type
5309                                            && !found_cname) {
5310                                         /*
5311                                          * We've found a signature that
5312                                          * covers the type we're looking for.
5313                                          */
5314                                         found = ISC_TRUE;
5315                                         found_type = ISC_TRUE;
5316                                         aflag = DNS_RDATASETATTR_ANSWERSIG;
5317                                 } else if (rdataset->type ==
5318                                            dns_rdatatype_cname
5319                                            && !found_type) {
5320                                         /*
5321                                          * We're looking for something else,
5322                                          * but we found a CNAME.
5323                                          *
5324                                          * Getting a CNAME response for some
5325                                          * query types is an error.
5326                                          */
5327                                         if (type == dns_rdatatype_rrsig ||
5328                                             type == dns_rdatatype_dnskey ||
5329                                             type == dns_rdatatype_nsec)
5330                                                 return (DNS_R_FORMERR);
5331                                         found = ISC_TRUE;
5332                                         found_cname = ISC_TRUE;
5333                                         want_chaining = ISC_TRUE;
5334                                         aflag = DNS_RDATASETATTR_ANSWER;
5335                                         result = cname_target(rdataset,
5336                                                               &tname);
5337                                         if (result != ISC_R_SUCCESS)
5338                                                 return (result);
5339                                 } else if (rdataset->type == dns_rdatatype_rrsig
5340                                            && rdataset->covers ==
5341                                            dns_rdatatype_cname
5342                                            && !found_type) {
5343                                         /*
5344                                          * We're looking for something else,
5345                                          * but we found a SIG CNAME.
5346                                          */
5347                                         found = ISC_TRUE;
5348                                         found_cname = ISC_TRUE;
5349                                         aflag = DNS_RDATASETATTR_ANSWERSIG;
5350                                 }
5351
5352                                 if (found) {
5353                                         /*
5354                                          * We've found an answer to our
5355                                          * question.
5356                                          */
5357                                         name->attributes |=
5358                                                 DNS_NAMEATTR_CACHE;
5359                                         rdataset->attributes |=
5360                                                 DNS_RDATASETATTR_CACHE;
5361                                         rdataset->trust = dns_trust_answer;
5362                                         if (!chaining) {
5363                                                 /*
5364                                                  * This data is "the" answer
5365                                                  * to our question only if
5366                                                  * we're not chaining (i.e.
5367                                                  * if we haven't followed
5368                                                  * a CNAME or DNAME).
5369                                                  */
5370                                                 INSIST(!external);
5371                                                 if (aflag ==
5372                                                     DNS_RDATASETATTR_ANSWER)
5373                                                         have_answer = ISC_TRUE;
5374                                                 name->attributes |=
5375                                                         DNS_NAMEATTR_ANSWER;
5376                                                 rdataset->attributes |= aflag;
5377                                                 if (aa)
5378                                                         rdataset->trust =
5379                                                           dns_trust_authanswer;
5380                                         } else if (external) {
5381                                                 /*
5382                                                  * This data is outside of
5383                                                  * our query domain, and
5384                                                  * may only be cached if it
5385                                                  * comes from a secure zone
5386                                                  * and validates.
5387                                                  */
5388                                                 rdataset->attributes |=
5389                                                     DNS_RDATASETATTR_EXTERNAL;
5390                                         }
5391
5392                                         /*
5393                                          * Mark any additional data related
5394                                          * to this rdataset.
5395                                          */
5396                                         (void)dns_rdataset_additionaldata(
5397                                                         rdataset,
5398                                                         check_related,
5399                                                         fctx);
5400
5401                                         /*
5402                                          * CNAME chaining.
5403                                          */
5404                                         if (want_chaining) {
5405                                                 wanted_chaining = ISC_TRUE;
5406                                                 name->attributes |=
5407                                                         DNS_NAMEATTR_CHAINING;
5408                                                 rdataset->attributes |=
5409                                                     DNS_RDATASETATTR_CHAINING;
5410                                                 qname = &tname;
5411                                         }
5412                                 }
5413                                 /*
5414                                  * We could add an "else" clause here and
5415                                  * log that we're ignoring this rdataset.
5416                                  */
5417                         }
5418                         /*
5419                          * If wanted_chaining is true, we've done
5420                          * some chaining as the result of processing
5421                          * this node, and thus we need to set
5422                          * chaining to true.
5423                          *
5424                          * We don't set chaining inside of the
5425                          * rdataset loop because doing that would
5426                          * cause us to ignore the signatures of
5427                          * CNAMEs.
5428                          */
5429                         if (wanted_chaining)
5430                                 chaining = ISC_TRUE;
5431                 } else {
5432                         /*
5433                          * Look for a DNAME (or its SIG).  Anything else is
5434                          * ignored.
5435                          */
5436                         wanted_chaining = ISC_FALSE;
5437                         for (rdataset = ISC_LIST_HEAD(name->list);
5438                              rdataset != NULL;
5439                              rdataset = ISC_LIST_NEXT(rdataset, link)) {
5440                                 isc_boolean_t found_dname = ISC_FALSE;
5441                                 found = ISC_FALSE;
5442                                 aflag = 0;
5443                                 if (rdataset->type == dns_rdatatype_dname) {
5444                                         /*
5445                                          * We're looking for something else,
5446                                          * but we found a DNAME.
5447                                          *
5448                                          * If we're not chaining, then the
5449                                          * DNAME should not be external.
5450                                          */
5451                                         if (!chaining && external)
5452                                                 return (DNS_R_FORMERR);
5453                                         found = ISC_TRUE;
5454                                         want_chaining = ISC_TRUE;
5455                                         aflag = DNS_RDATASETATTR_ANSWER;
5456                                         result = dname_target(rdataset,
5457                                                               qname, name,
5458                                                               &dname);
5459                                         if (result == ISC_R_NOSPACE) {
5460                                                 /*
5461                                                  * We can't construct the
5462                                                  * DNAME target.  Do not
5463                                                  * try to continue.
5464                                                  */
5465                                                 want_chaining = ISC_FALSE;
5466                                         } else if (result != ISC_R_SUCCESS)
5467                                                 return (result);
5468                                         else
5469                                                 found_dname = ISC_TRUE;
5470                                 } else if (rdataset->type == dns_rdatatype_rrsig
5471                                            && rdataset->covers ==
5472                                            dns_rdatatype_dname) {
5473                                         /*
5474                                          * We've found a signature that
5475                                          * covers the DNAME.
5476                                          */
5477                                         found = ISC_TRUE;
5478                                         aflag = DNS_RDATASETATTR_ANSWERSIG;
5479                                 }
5480
5481                                 if (found) {
5482                                         /*
5483                                          * We've found an answer to our
5484                                          * question.
5485                                          */
5486                                         name->attributes |=
5487                                                 DNS_NAMEATTR_CACHE;
5488                                         rdataset->attributes |=
5489                                                 DNS_RDATASETATTR_CACHE;
5490                                         rdataset->trust = dns_trust_answer;
5491                                         if (!chaining) {
5492                                                 /*
5493                                                  * This data is "the" answer
5494                                                  * to our question only if
5495                                                  * we're not chaining.
5496                                                  */
5497                                                 INSIST(!external);
5498                                                 if (aflag ==
5499                                                     DNS_RDATASETATTR_ANSWER)
5500                                                         have_answer = ISC_TRUE;
5501                                                 name->attributes |=
5502                                                         DNS_NAMEATTR_ANSWER;
5503                                                 rdataset->attributes |= aflag;
5504                                                 if (aa)
5505                                                         rdataset->trust =
5506                                                           dns_trust_authanswer;
5507                                         } else if (external) {
5508                                                 rdataset->attributes |=
5509                                                     DNS_RDATASETATTR_EXTERNAL;
5510                                         }
5511
5512                                         /*
5513                                          * DNAME chaining.
5514                                          */
5515                                         if (found_dname) {
5516                                                 /*
5517                                                  * Copy the dname into the
5518                                                  * qname fixed name.
5519                                                  *
5520                                                  * Although we check for
5521                                                  * failure of the copy
5522                                                  * operation, in practice it
5523                                                  * should never fail since
5524                                                  * we already know that the
5525                                                  * result fits in a fixedname.
5526                                                  */
5527                                                 dns_fixedname_init(&fqname);
5528                                                 result = dns_name_copy(
5529                                                   dns_fixedname_name(&dname),
5530                                                   dns_fixedname_name(&fqname),
5531                                                   NULL);
5532                                                 if (result != ISC_R_SUCCESS)
5533                                                         return (result);
5534                                                 wanted_chaining = ISC_TRUE;
5535                                                 name->attributes |=
5536                                                         DNS_NAMEATTR_CHAINING;
5537                                                 rdataset->attributes |=
5538                                                     DNS_RDATASETATTR_CHAINING;
5539                                                 qname = dns_fixedname_name(
5540                                                                    &fqname);
5541                                         }
5542                                 }
5543                         }
5544                         if (wanted_chaining)
5545                                 chaining = ISC_TRUE;
5546                 }
5547                 result = dns_message_nextname(message, DNS_SECTION_ANSWER);
5548         }
5549         if (result == ISC_R_NOMORE)
5550                 result = ISC_R_SUCCESS;
5551         if (result != ISC_R_SUCCESS)
5552                 return (result);
5553
5554         /*
5555          * We should have found an answer.
5556          */
5557         if (!have_answer)
5558                 return (DNS_R_FORMERR);
5559
5560         /*
5561          * This response is now potentially cacheable.
5562          */
5563         fctx->attributes |= FCTX_ATTR_WANTCACHE;
5564
5565         /*
5566          * Did chaining end before we got the final answer?
5567          */
5568         if (chaining) {
5569                 /*
5570                  * Yes.  This may be a negative reply, so hand off
5571                  * authority section processing to the noanswer code.
5572                  * If it isn't a noanswer response, no harm will be
5573                  * done.
5574                  */
5575                 return (noanswer_response(fctx, qname, ISC_FALSE));
5576         }
5577
5578         /*
5579          * We didn't end with an incomplete chain, so the rcode should be
5580          * "no error".
5581          */
5582         if (message->rcode != dns_rcode_noerror)
5583                 return (DNS_R_FORMERR);
5584
5585         /*
5586          * Examine the authority section (if there is one).
5587          *
5588          * We expect there to be only one owner name for all the rdatasets
5589          * in this section, and we expect that it is not external.
5590          */
5591         done = ISC_FALSE;
5592         result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
5593         while (!done && result == ISC_R_SUCCESS) {
5594                 name = NULL;
5595                 dns_message_currentname(message, DNS_SECTION_AUTHORITY, &name);
5596                 external = ISC_TF(!dns_name_issubdomain(name, &fctx->domain));
5597                 if (!external) {
5598                         /*
5599                          * We expect to find NS or SIG NS rdatasets, and
5600                          * nothing else.
5601                          */
5602                         for (rdataset = ISC_LIST_HEAD(name->list);
5603                              rdataset != NULL;
5604                              rdataset = ISC_LIST_NEXT(rdataset, link)) {
5605                                 if (rdataset->type == dns_rdatatype_ns ||
5606                                     (rdataset->type == dns_rdatatype_rrsig &&
5607                                      rdataset->covers == dns_rdatatype_ns)) {
5608                                         name->attributes |=
5609                                                 DNS_NAMEATTR_CACHE;
5610                                         rdataset->attributes |=
5611                                                 DNS_RDATASETATTR_CACHE;
5612                                         if (aa && !chaining)
5613                                                 rdataset->trust =
5614                                                     dns_trust_authauthority;
5615                                         else
5616                                                 rdataset->trust =
5617                                                     dns_trust_additional;
5618
5619                                         /*
5620                                          * Mark any additional data related
5621                                          * to this rdataset.
5622                                          */
5623                                         (void)dns_rdataset_additionaldata(
5624                                                         rdataset,
5625                                                         check_related,
5626                                                         fctx);
5627                                         done = ISC_TRUE;
5628                                 }
5629                         }
5630                 }
5631                 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
5632         }
5633         if (result == ISC_R_NOMORE)
5634                 result = ISC_R_SUCCESS;
5635
5636         return (result);
5637 }
5638
5639 static void
5640 resume_dslookup(isc_task_t *task, isc_event_t *event) {
5641         dns_fetchevent_t *fevent;
5642         dns_resolver_t *res;
5643         fetchctx_t *fctx;
5644         isc_result_t result;
5645         isc_boolean_t bucket_empty = ISC_FALSE;
5646         isc_boolean_t locked = ISC_FALSE;
5647         unsigned int bucketnum;
5648         dns_rdataset_t nameservers;
5649         dns_fixedname_t fixed;
5650         dns_name_t *domain;
5651
5652         REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
5653         fevent = (dns_fetchevent_t *)event;
5654         fctx = event->ev_arg;
5655         REQUIRE(VALID_FCTX(fctx));
5656         res = fctx->res;
5657
5658         UNUSED(task);
5659         FCTXTRACE("resume_dslookup");
5660
5661         if (fevent->node != NULL)
5662                 dns_db_detachnode(fevent->db, &fevent->node);
5663         if (fevent->db != NULL)
5664                 dns_db_detach(&fevent->db);
5665
5666         dns_rdataset_init(&nameservers);
5667
5668         bucketnum = fctx->bucketnum;
5669         if (fevent->result == ISC_R_CANCELED) {
5670                 dns_resolver_destroyfetch(&fctx->nsfetch);
5671                 fctx_done(fctx, ISC_R_CANCELED, __LINE__);
5672         } else if (fevent->result == ISC_R_SUCCESS) {
5673
5674                 FCTXTRACE("resuming DS lookup");
5675
5676                 dns_resolver_destroyfetch(&fctx->nsfetch);
5677                 if (dns_rdataset_isassociated(&fctx->nameservers))
5678                         dns_rdataset_disassociate(&fctx->nameservers);
5679                 dns_rdataset_clone(fevent->rdataset, &fctx->nameservers);
5680                 dns_name_free(&fctx->domain,
5681                               fctx->res->buckets[bucketnum].mctx);
5682                 dns_name_init(&fctx->domain, NULL);
5683                 result = dns_name_dup(&fctx->nsname,
5684                                       fctx->res->buckets[bucketnum].mctx,
5685                                       &fctx->domain);
5686                 if (result != ISC_R_SUCCESS) {
5687                         fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
5688                         goto cleanup;
5689                 }
5690                 /*
5691                  * Try again.
5692                  */
5693                 fctx_try(fctx, ISC_TRUE);
5694         } else {
5695                 unsigned int n;
5696                 dns_rdataset_t *nsrdataset = NULL;
5697
5698                 /*
5699                  * Retrieve state from fctx->nsfetch before we destroy it.
5700                  */
5701                 dns_fixedname_init(&fixed);
5702                 domain = dns_fixedname_name(&fixed);
5703                 dns_name_copy(&fctx->nsfetch->private->domain, domain, NULL);
5704                 if (dns_name_equal(&fctx->nsname, domain)) {
5705                         fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
5706                         dns_resolver_destroyfetch(&fctx->nsfetch);
5707                         goto cleanup;
5708                 }
5709                 if (dns_rdataset_isassociated(
5710                     &fctx->nsfetch->private->nameservers)) {
5711                         dns_rdataset_clone(
5712                             &fctx->nsfetch->private->nameservers,
5713                             &nameservers);
5714                         nsrdataset = &nameservers;
5715                 } else
5716                         domain = NULL;
5717                 dns_resolver_destroyfetch(&fctx->nsfetch);
5718                 n = dns_name_countlabels(&fctx->nsname);
5719                 dns_name_getlabelsequence(&fctx->nsname, 1, n - 1,
5720                                           &fctx->nsname);
5721
5722                 if (dns_rdataset_isassociated(fevent->rdataset))
5723                         dns_rdataset_disassociate(fevent->rdataset);
5724                 FCTXTRACE("continuing to look for parent's NS records");
5725                 result = dns_resolver_createfetch(fctx->res, &fctx->nsname,
5726                                                   dns_rdatatype_ns, domain,
5727                                                   nsrdataset, NULL, 0, task,
5728                                                   resume_dslookup, fctx,
5729                                                   &fctx->nsrrset, NULL,
5730                                                   &fctx->nsfetch);
5731                 if (result != ISC_R_SUCCESS)
5732                         fctx_done(fctx, result, __LINE__);
5733                 else {
5734                         LOCK(&res->buckets[bucketnum].lock);
5735                         locked = ISC_TRUE;
5736                         fctx->references++;
5737                 }
5738         }
5739
5740  cleanup:
5741         if (dns_rdataset_isassociated(&nameservers))
5742                 dns_rdataset_disassociate(&nameservers);
5743         if (dns_rdataset_isassociated(fevent->rdataset))
5744                 dns_rdataset_disassociate(fevent->rdataset);
5745         INSIST(fevent->sigrdataset == NULL);
5746         isc_event_free(&event);
5747         if (!locked)
5748                 LOCK(&res->buckets[bucketnum].lock);
5749         fctx->references--;
5750         if (fctx->references == 0)
5751                 bucket_empty = fctx_destroy(fctx);
5752         UNLOCK(&res->buckets[bucketnum].lock);
5753         if (bucket_empty)
5754                 empty_bucket(res);
5755 }
5756
5757 static inline void
5758 checknamessection(dns_message_t *message, dns_section_t section) {
5759         isc_result_t result;
5760         dns_name_t *name;
5761         dns_rdata_t rdata = DNS_RDATA_INIT;
5762         dns_rdataset_t *rdataset;
5763
5764         for (result = dns_message_firstname(message, section);
5765              result == ISC_R_SUCCESS;
5766              result = dns_message_nextname(message, section))
5767         {
5768                 name = NULL;
5769                 dns_message_currentname(message, section, &name);
5770                 for (rdataset = ISC_LIST_HEAD(name->list);
5771                      rdataset != NULL;
5772                      rdataset = ISC_LIST_NEXT(rdataset, link)) {
5773                         for (result = dns_rdataset_first(rdataset);
5774                              result == ISC_R_SUCCESS;
5775                              result = dns_rdataset_next(rdataset)) {
5776                                 dns_rdataset_current(rdataset, &rdata);
5777                                 if (!dns_rdata_checkowner(name, rdata.rdclass,
5778                                                           rdata.type,
5779                                                           ISC_FALSE) ||
5780                                     !dns_rdata_checknames(&rdata, name, NULL))
5781                                 {
5782                                         rdataset->attributes |=
5783                                                 DNS_RDATASETATTR_CHECKNAMES;
5784                                 }
5785                                 dns_rdata_reset(&rdata);
5786                         }
5787                 }
5788         }
5789 }
5790
5791 static void
5792 checknames(dns_message_t *message) {
5793
5794         checknamessection(message, DNS_SECTION_ANSWER);
5795         checknamessection(message, DNS_SECTION_AUTHORITY);
5796         checknamessection(message, DNS_SECTION_ADDITIONAL);
5797 }
5798
5799 /*
5800  * Log server NSID at log level 'level'
5801  */
5802 static isc_result_t
5803 log_nsid(dns_rdataset_t *opt, resquery_t *query, int level, isc_mem_t *mctx)
5804 {
5805         static const char hex[17] = "0123456789abcdef";
5806         char addrbuf[ISC_SOCKADDR_FORMATSIZE];
5807         isc_uint16_t optcode, nsid_len, buflen, i;
5808         isc_result_t result;
5809         isc_buffer_t nsidbuf;
5810         dns_rdata_t rdata;
5811         unsigned char *p, *buf, *nsid;
5812
5813         /* Extract rdata from OPT rdataset */
5814         result = dns_rdataset_first(opt);
5815         if (result != ISC_R_SUCCESS)
5816                 return (ISC_R_FAILURE);
5817
5818         dns_rdata_init(&rdata);
5819         dns_rdataset_current(opt, &rdata);
5820         if (rdata.length < 4)
5821                 return (ISC_R_FAILURE);
5822
5823         /* Check for NSID */
5824         isc_buffer_init(&nsidbuf, rdata.data, rdata.length);
5825         isc_buffer_add(&nsidbuf, rdata.length);
5826         optcode = isc_buffer_getuint16(&nsidbuf);
5827         nsid_len = isc_buffer_getuint16(&nsidbuf);
5828         if (optcode != DNS_OPT_NSID || nsid_len == 0)
5829                 return (ISC_R_FAILURE);
5830
5831         /* Allocate buffer for storing hex version of the NSID */
5832         buflen = nsid_len * 2 + 1;
5833         buf = isc_mem_get(mctx, buflen);
5834         if (buf == NULL)
5835                 return (ISC_R_NOSPACE);
5836
5837         /* Convert to hex */
5838         p = buf;
5839         nsid = rdata.data + 4;
5840         for (i = 0; i < nsid_len; i++) {
5841                 *p++ = hex[(nsid[0] >> 4) & 0xf];
5842                 *p++ = hex[nsid[0] & 0xf];
5843                 nsid++;
5844         }
5845         *p = '\0';
5846
5847         isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf,
5848                             sizeof(addrbuf));
5849         isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
5850                       DNS_LOGMODULE_RESOLVER, level,
5851                       "received NSID '%s' from %s", buf, addrbuf);
5852
5853         /* Clean up */
5854         isc_mem_put(mctx, buf, buflen);
5855         return (ISC_R_SUCCESS);
5856 }
5857
5858 static void
5859 log_packet(dns_message_t *message, int level, isc_mem_t *mctx) {
5860         isc_buffer_t buffer;
5861         char *buf = NULL;
5862         int len = 1024;
5863         isc_result_t result;
5864
5865         if (! isc_log_wouldlog(dns_lctx, level))
5866                 return;
5867
5868         /*
5869          * Note that these are multiline debug messages.  We want a newline
5870          * to appear in the log after each message.
5871          */
5872
5873         do {
5874                 buf = isc_mem_get(mctx, len);
5875                 if (buf == NULL)
5876                         break;
5877                 isc_buffer_init(&buffer, buf, len);
5878                 result = dns_message_totext(message, &dns_master_style_debug,
5879                                             0, &buffer);
5880                 if (result == ISC_R_NOSPACE) {
5881                         isc_mem_put(mctx, buf, len);
5882                         len += 1024;
5883                 } else if (result == ISC_R_SUCCESS)
5884                         isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
5885                                       DNS_LOGMODULE_RESOLVER, level,
5886                                       "received packet:\n%.*s",
5887                                       (int)isc_buffer_usedlength(&buffer),
5888                                       buf);
5889         } while (result == ISC_R_NOSPACE);
5890
5891         if (buf != NULL)
5892                 isc_mem_put(mctx, buf, len);
5893 }
5894
5895 static void
5896 resquery_response(isc_task_t *task, isc_event_t *event) {
5897         isc_result_t result = ISC_R_SUCCESS;
5898         resquery_t *query = event->ev_arg;
5899         dns_dispatchevent_t *devent = (dns_dispatchevent_t *)event;
5900         isc_boolean_t keep_trying, get_nameservers, resend;
5901         isc_boolean_t truncated;
5902         dns_message_t *message;
5903         dns_rdataset_t *opt;
5904         fetchctx_t *fctx;
5905         dns_name_t *fname;
5906         dns_fixedname_t foundname;
5907         isc_stdtime_t now;
5908         isc_time_t tnow, *finish;
5909         dns_adbaddrinfo_t *addrinfo;
5910         unsigned int options;
5911         unsigned int findoptions;
5912         isc_result_t broken_server;
5913         badnstype_t broken_type = badns_response;
5914
5915         REQUIRE(VALID_QUERY(query));
5916         fctx = query->fctx;
5917         options = query->options;
5918         REQUIRE(VALID_FCTX(fctx));
5919         REQUIRE(event->ev_type == DNS_EVENT_DISPATCH);
5920
5921         QTRACE("response");
5922
5923         if (isc_sockaddr_pf(&query->addrinfo->sockaddr) == PF_INET)
5924                 inc_stats(fctx->res, dns_resstatscounter_responsev4);
5925         else
5926                 inc_stats(fctx->res, dns_resstatscounter_responsev6);
5927
5928         (void)isc_timer_touch(fctx->timer);
5929
5930         keep_trying = ISC_FALSE;
5931         broken_server = ISC_R_SUCCESS;
5932         get_nameservers = ISC_FALSE;
5933         resend = ISC_FALSE;
5934         truncated = ISC_FALSE;
5935         finish = NULL;
5936
5937         if (fctx->res->exiting) {
5938                 result = ISC_R_SHUTTINGDOWN;
5939                 goto done;
5940         }
5941
5942         fctx->timeouts = 0;
5943         fctx->timeout = ISC_FALSE;
5944
5945         /*
5946          * XXXRTH  We should really get the current time just once.  We
5947          *              need a routine to convert from an isc_time_t to an
5948          *              isc_stdtime_t.
5949          */
5950         TIME_NOW(&tnow);
5951         finish = &tnow;
5952         isc_stdtime_get(&now);
5953
5954         /*
5955          * Did the dispatcher have a problem?
5956          */
5957         if (devent->result != ISC_R_SUCCESS) {
5958                 if (devent->result == ISC_R_EOF &&
5959                     (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
5960                         /*
5961                          * The problem might be that they
5962                          * don't understand EDNS0.  Turn it
5963                          * off and try again.
5964                          */
5965                         options |= DNS_FETCHOPT_NOEDNS0;
5966                         resend = ISC_TRUE;
5967                         /*
5968                          * Remember that they don't like EDNS0.
5969                          */
5970                         dns_adb_changeflags(fctx->adb,
5971                                             query->addrinfo,
5972                                             DNS_FETCHOPT_NOEDNS0,
5973                                             DNS_FETCHOPT_NOEDNS0);
5974                 } else {
5975                         /*
5976                          * There's no hope for this query.
5977                          */
5978                         keep_trying = ISC_TRUE;
5979
5980                         /*
5981                          * If this is a network error on an exclusive query
5982                          * socket, mark the server as bad so that we won't try
5983                          * it for this fetch again.
5984                          */
5985                         if (query->exclusivesocket &&
5986                             (devent->result == ISC_R_HOSTUNREACH ||
5987                              devent->result == ISC_R_NETUNREACH ||
5988                              devent->result == ISC_R_CONNREFUSED ||
5989                              devent->result == ISC_R_CANCELED)) {
5990                                     broken_server = devent->result;
5991                                     broken_type = badns_unreachable;
5992                         }
5993                 }
5994                 goto done;
5995         }
5996
5997         message = fctx->rmessage;
5998
5999         if (query->tsig != NULL) {
6000                 result = dns_message_setquerytsig(message, query->tsig);
6001                 if (result != ISC_R_SUCCESS)
6002                         goto done;
6003         }
6004
6005         if (query->tsigkey) {
6006                 result = dns_message_settsigkey(message, query->tsigkey);
6007                 if (result != ISC_R_SUCCESS)
6008                         goto done;
6009         }
6010
6011         result = dns_message_parse(message, &devent->buffer, 0);
6012         if (result != ISC_R_SUCCESS) {
6013                 switch (result) {
6014                 case ISC_R_UNEXPECTEDEND:
6015                         if (!message->question_ok ||
6016                             (message->flags & DNS_MESSAGEFLAG_TC) == 0 ||
6017                             (options & DNS_FETCHOPT_TCP) != 0) {
6018                                 /*
6019                                  * Either the message ended prematurely,
6020                                  * and/or wasn't marked as being truncated,
6021                                  * and/or this is a response to a query we
6022                                  * sent over TCP.  In all of these cases,
6023                                  * something is wrong with the remote
6024                                  * server and we don't want to retry using
6025                                  * TCP.
6026                                  */
6027                                 if ((query->options & DNS_FETCHOPT_NOEDNS0)
6028                                     == 0) {
6029                                         /*
6030                                          * The problem might be that they
6031                                          * don't understand EDNS0.  Turn it
6032                                          * off and try again.
6033                                          */
6034                                         options |= DNS_FETCHOPT_NOEDNS0;
6035                                         resend = ISC_TRUE;
6036                                         /*
6037                                          * Remember that they don't like EDNS0.
6038                                          */
6039                                         dns_adb_changeflags(
6040                                                         fctx->adb,
6041                                                         query->addrinfo,
6042                                                         DNS_FETCHOPT_NOEDNS0,
6043                                                         DNS_FETCHOPT_NOEDNS0);
6044                                         inc_stats(fctx->res,
6045                                                  dns_resstatscounter_edns0fail);
6046                                 } else {
6047                                         broken_server = result;
6048                                         keep_trying = ISC_TRUE;
6049                                 }
6050                                 goto done;
6051                         }
6052                         /*
6053                          * We defer retrying via TCP for a bit so we can
6054                          * check out this message further.
6055                          */
6056                         truncated = ISC_TRUE;
6057                         break;
6058                 case DNS_R_FORMERR:
6059                         if ((query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
6060                                 /*
6061                                  * The problem might be that they
6062                                  * don't understand EDNS0.  Turn it
6063                                  * off and try again.
6064                                  */
6065                                 options |= DNS_FETCHOPT_NOEDNS0;
6066                                 resend = ISC_TRUE;
6067                                 /*
6068                                  * Remember that they don't like EDNS0.
6069                                  */
6070                                 dns_adb_changeflags(fctx->adb,
6071                                                     query->addrinfo,
6072                                                     DNS_FETCHOPT_NOEDNS0,
6073                                                     DNS_FETCHOPT_NOEDNS0);
6074                                 inc_stats(fctx->res,
6075                                                  dns_resstatscounter_edns0fail);
6076                         } else {
6077                                 broken_server = DNS_R_UNEXPECTEDRCODE;
6078                                 keep_trying = ISC_TRUE;
6079                         }
6080                         goto done;
6081                 default:
6082                         /*
6083                          * Something bad has happened.
6084                          */
6085                         goto done;
6086                 }
6087         }
6088
6089
6090         /*
6091          * Log the incoming packet.
6092          */
6093         log_packet(message, ISC_LOG_DEBUG(10), fctx->res->mctx);
6094
6095         /*
6096          * Did we request NSID?  If so, and if the response contains
6097          * NSID data, log it at INFO level.
6098          */
6099         opt = dns_message_getopt(message);
6100         if (opt != NULL && (query->options & DNS_FETCHOPT_WANTNSID) != 0)
6101                 log_nsid(opt, query, ISC_LOG_INFO, fctx->res->mctx);
6102
6103         /*
6104          * If the message is signed, check the signature.  If not, this
6105          * returns success anyway.
6106          */
6107         result = dns_message_checksig(message, fctx->res->view);
6108         if (result != ISC_R_SUCCESS)
6109                 goto done;
6110
6111         /*
6112          * The dispatcher should ensure we only get responses with QR set.
6113          */
6114         INSIST((message->flags & DNS_MESSAGEFLAG_QR) != 0);
6115         /*
6116          * INSIST() that the message comes from the place we sent it to,
6117          * since the dispatch code should ensure this.
6118          *
6119          * INSIST() that the message id is correct (this should also be
6120          * ensured by the dispatch code).
6121          */
6122
6123
6124         /*
6125          * Deal with truncated responses by retrying using TCP.
6126          */
6127         if ((message->flags & DNS_MESSAGEFLAG_TC) != 0)
6128                 truncated = ISC_TRUE;
6129
6130         if (truncated) {
6131                 inc_stats(fctx->res, dns_resstatscounter_truncated);
6132                 if ((options & DNS_FETCHOPT_TCP) != 0) {
6133                         broken_server = DNS_R_TRUNCATEDTCP;
6134                         keep_trying = ISC_TRUE;
6135                 } else {
6136                         options |= DNS_FETCHOPT_TCP;
6137                         resend = ISC_TRUE;
6138                 }
6139                 goto done;
6140         }
6141
6142         /*
6143          * Is it a query response?
6144          */
6145         if (message->opcode != dns_opcode_query) {
6146                 /* XXXRTH Log */
6147                 broken_server = DNS_R_UNEXPECTEDOPCODE;
6148                 keep_trying = ISC_TRUE;
6149                 goto done;
6150         }
6151
6152         /*
6153          * Update statistics about erroneous responses.
6154          */
6155         if (message->rcode != dns_rcode_noerror) {
6156                 switch (message->rcode) {
6157                 case dns_rcode_nxdomain:
6158                         inc_stats(fctx->res, dns_resstatscounter_nxdomain);
6159                         break;
6160                 case dns_rcode_servfail:
6161                         inc_stats(fctx->res, dns_resstatscounter_servfail);
6162                         break;
6163                 case dns_rcode_formerr:
6164                         inc_stats(fctx->res, dns_resstatscounter_formerr);
6165                         break;
6166                 default:
6167                         inc_stats(fctx->res, dns_resstatscounter_othererror);
6168                         break;
6169                 }
6170         }
6171
6172         /*
6173          * Is the remote server broken, or does it dislike us?
6174          */
6175         if (message->rcode != dns_rcode_noerror &&
6176             message->rcode != dns_rcode_nxdomain) {
6177                 if (((message->rcode == dns_rcode_formerr ||
6178                      message->rcode == dns_rcode_notimp) ||
6179                     (message->rcode == dns_rcode_servfail &&
6180                      dns_message_getopt(message) == NULL)) &&
6181                     (query->options & DNS_FETCHOPT_NOEDNS0) == 0) {
6182                         /*
6183                          * It's very likely they don't like EDNS0.
6184                          * If the response code is SERVFAIL, also check if the
6185                          * response contains an OPT RR and don't cache the
6186                          * failure since it can be returned for various other
6187                          * reasons.
6188                          *
6189                          * XXXRTH  We should check if the question
6190                          *              we're asking requires EDNS0, and
6191                          *              if so, we should bail out.
6192                          */
6193                         options |= DNS_FETCHOPT_NOEDNS0;
6194                         resend = ISC_TRUE;
6195                         /*
6196                          * Remember that they don't like EDNS0.
6197                          */
6198                         if (message->rcode != dns_rcode_servfail)
6199                                 dns_adb_changeflags(fctx->adb, query->addrinfo,
6200                                                     DNS_FETCHOPT_NOEDNS0,
6201                                                     DNS_FETCHOPT_NOEDNS0);
6202                         inc_stats(fctx->res, dns_resstatscounter_edns0fail);
6203                 } else if (message->rcode == dns_rcode_formerr) {
6204                         if (ISFORWARDER(query->addrinfo)) {
6205                                 /*
6206                                  * This forwarder doesn't understand us,
6207                                  * but other forwarders might.  Keep trying.
6208                                  */
6209                                 broken_server = DNS_R_REMOTEFORMERR;
6210                                 keep_trying = ISC_TRUE;
6211                         } else {
6212                                 /*
6213                                  * The server doesn't understand us.  Since
6214                                  * all servers for a zone need similar
6215                                  * capabilities, we assume that we will get
6216                                  * FORMERR from all servers, and thus we
6217                                  * cannot make any more progress with this
6218                                  * fetch.
6219                                  */
6220                                 result = DNS_R_FORMERR;
6221                         }
6222                 } else if (message->rcode == dns_rcode_yxdomain) {
6223                         /*
6224                          * DNAME mapping failed because the new name
6225                          * was too long.  There's no chance of success
6226                          * for this fetch.
6227                          */
6228                         result = DNS_R_YXDOMAIN;
6229                 } else if (message->rcode == dns_rcode_badvers) {
6230                         unsigned int flags, mask;
6231                         unsigned int version;
6232
6233                         resend = ISC_TRUE;
6234                         version = (opt->ttl >> 16) & 0xff;
6235                         flags = (version << DNS_FETCHOPT_EDNSVERSIONSHIFT) |
6236                                 DNS_FETCHOPT_EDNSVERSIONSET;
6237                         mask = DNS_FETCHOPT_EDNSVERSIONMASK |
6238                                DNS_FETCHOPT_EDNSVERSIONSET;
6239                         switch (version) {
6240                         case 0:
6241                                 dns_adb_changeflags(fctx->adb, query->addrinfo,
6242                                                     flags, mask);
6243                                 break;
6244                         default:
6245                                 broken_server = DNS_R_BADVERS;
6246                                 keep_trying = ISC_TRUE;
6247                                 break;
6248                         }
6249                 } else {
6250                         /*
6251                          * XXXRTH log.
6252                          */
6253                         broken_server = DNS_R_UNEXPECTEDRCODE;
6254                         INSIST(broken_server != ISC_R_SUCCESS);
6255                         keep_trying = ISC_TRUE;
6256                 }
6257                 goto done;
6258         }
6259
6260         /*
6261          * Is the question the same as the one we asked?
6262          */
6263         result = same_question(fctx);
6264         if (result != ISC_R_SUCCESS) {
6265                 /* XXXRTH Log */
6266                 if (result == DNS_R_FORMERR)
6267                         keep_trying = ISC_TRUE;
6268                 goto done;
6269         }
6270
6271         /*
6272          * Is the server lame?
6273          */
6274         if (fctx->res->lame_ttl != 0 && !ISFORWARDER(query->addrinfo) &&
6275             is_lame(fctx)) {
6276                 inc_stats(fctx->res, dns_resstatscounter_lame);
6277                 log_lame(fctx, query->addrinfo);
6278                 result = dns_adb_marklame(fctx->adb, query->addrinfo,
6279                                           &fctx->name, fctx->type,
6280                                           now + fctx->res->lame_ttl);
6281                 if (result != ISC_R_SUCCESS)
6282                         isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
6283                                       DNS_LOGMODULE_RESOLVER, ISC_LOG_ERROR,
6284                                       "could not mark server as lame: %s",
6285                                       isc_result_totext(result));
6286                 broken_server = DNS_R_LAME;
6287                 keep_trying = ISC_TRUE;
6288                 goto done;
6289         }
6290
6291         /*
6292          * Enforce delegations only zones like NET and COM.
6293          */
6294         if (!ISFORWARDER(query->addrinfo) &&
6295             dns_view_isdelegationonly(fctx->res->view, &fctx->domain) &&
6296             !dns_name_equal(&fctx->domain, &fctx->name) &&
6297             fix_mustbedelegationornxdomain(message, fctx)) {
6298                 char namebuf[DNS_NAME_FORMATSIZE];
6299                 char domainbuf[DNS_NAME_FORMATSIZE];
6300                 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
6301                 char classbuf[64];
6302                 char typebuf[64];
6303
6304                 dns_name_format(&fctx->name, namebuf, sizeof(namebuf));
6305                 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
6306                 dns_rdatatype_format(fctx->type, typebuf, sizeof(typebuf));
6307                 dns_rdataclass_format(fctx->res->rdclass, classbuf,
6308                                       sizeof(classbuf));
6309                 isc_sockaddr_format(&query->addrinfo->sockaddr, addrbuf,
6310                                     sizeof(addrbuf));
6311
6312                 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DELEGATION_ONLY,
6313                              DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
6314                              "enforced delegation-only for '%s' (%s/%s/%s) "
6315                              "from %s",
6316                              domainbuf, namebuf, typebuf, classbuf, addrbuf);
6317         }
6318
6319         if ((fctx->res->options & DNS_RESOLVER_CHECKNAMES) != 0)
6320                 checknames(message);
6321
6322         /*
6323          * Clear cache bits.
6324          */
6325         fctx->attributes &= ~(FCTX_ATTR_WANTNCACHE | FCTX_ATTR_WANTCACHE);
6326
6327         /*
6328          * Did we get any answers?
6329          */
6330         if (message->counts[DNS_SECTION_ANSWER] > 0 &&
6331             (message->rcode == dns_rcode_noerror ||
6332              message->rcode == dns_rcode_nxdomain)) {
6333                 /*
6334                  * We've got answers.  However, if we sent
6335                  * a BIND 8 server an NS query, it may have
6336                  * incorrectly responded with a non-authoritative
6337                  * answer instead of a referral.  Since this
6338                  * answer lacks the SIGs necessary to do DNSSEC
6339                  * validation, we must invoke the following special
6340                  * kludge to treat it as a referral.
6341                  */
6342                 if (fctx->type == dns_rdatatype_ns &&
6343                     (message->flags & DNS_MESSAGEFLAG_AA) == 0 &&
6344                     !ISFORWARDER(query->addrinfo))
6345                 {
6346                         result = noanswer_response(fctx, NULL, ISC_TRUE);
6347                         if (result != DNS_R_DELEGATION) {
6348                                 /*
6349                                  * The answer section must have contained
6350                                  * something other than the NS records
6351                                  * we asked for.  Since AA is not set
6352                                  * and the server is not a forwarder,
6353                                  * it is technically lame and it's easier
6354                                  * to treat it as such than to figure out
6355                                  * some more elaborate course of action.
6356                                  */
6357                                 broken_server = DNS_R_LAME;
6358                                 keep_trying = ISC_TRUE;
6359                                 goto done;
6360                         }
6361                         goto force_referral;
6362                 }
6363                 result = answer_response(fctx);
6364                 if (result != ISC_R_SUCCESS) {
6365                         if (result == DNS_R_FORMERR)
6366                                 keep_trying = ISC_TRUE;
6367                         goto done;
6368                 }
6369         } else if (message->counts[DNS_SECTION_AUTHORITY] > 0 ||
6370                    message->rcode == dns_rcode_noerror ||
6371                    message->rcode == dns_rcode_nxdomain) {
6372                 /*
6373                  * NXDOMAIN, NXRDATASET, or referral.
6374                  */
6375                 result = noanswer_response(fctx, NULL, ISC_FALSE);
6376                 if (result == DNS_R_CHASEDSSERVERS) {
6377                 } else if (result == DNS_R_DELEGATION) {
6378                 force_referral:
6379                         /*
6380                          * We don't have the answer, but we know a better
6381                          * place to look.
6382                          */
6383                         get_nameservers = ISC_TRUE;
6384                         keep_trying = ISC_TRUE;
6385                         /*
6386                          * We have a new set of name servers, and it
6387                          * has not experienced any restarts yet.
6388                          */
6389                         fctx->restarts = 0;
6390
6391                         /*
6392                          * Update local statistics counters collected for each
6393                          * new zone.
6394                          */
6395                         fctx->referrals++;
6396                         fctx->querysent = 0;
6397                         fctx->lamecount = 0;
6398                         fctx->neterr = 0;
6399                         fctx->badresp = 0;
6400                         fctx->adberr = 0;
6401
6402                         result = ISC_R_SUCCESS;
6403                 } else if (result != ISC_R_SUCCESS) {
6404                         /*
6405                          * Something has gone wrong.
6406                          */
6407                         if (result == DNS_R_FORMERR)
6408                                 keep_trying = ISC_TRUE;
6409                         goto done;
6410                 }
6411         } else {
6412                 /*
6413                  * The server is insane.
6414                  */
6415                 /* XXXRTH Log */
6416                 broken_server = DNS_R_UNEXPECTEDRCODE;
6417                 keep_trying = ISC_TRUE;
6418                 goto done;
6419         }
6420
6421         /*
6422          * Follow additional section data chains.
6423          */
6424         chase_additional(fctx);
6425
6426         /*
6427          * Cache the cacheable parts of the message.  This may also cause
6428          * work to be queued to the DNSSEC validator.
6429          */
6430         if (WANTCACHE(fctx)) {
6431                 result = cache_message(fctx, query->addrinfo, now);
6432                 if (result != ISC_R_SUCCESS)
6433                         goto done;
6434         }
6435
6436         /*
6437          * Ncache the negatively cacheable parts of the message.  This may
6438          * also cause work to be queued to the DNSSEC validator.
6439          */
6440         if (WANTNCACHE(fctx)) {
6441                 dns_rdatatype_t covers;
6442                 if (message->rcode == dns_rcode_nxdomain)
6443                         covers = dns_rdatatype_any;
6444                 else
6445                         covers = fctx->type;
6446
6447                 /*
6448                  * Cache any negative cache entries in the message.
6449                  */
6450                 result = ncache_message(fctx, query->addrinfo, covers, now);
6451         }
6452
6453  done:
6454         /*
6455          * Remember the query's addrinfo, in case we need to mark the
6456          * server as broken.
6457          */
6458         addrinfo = query->addrinfo;
6459
6460         /*
6461          * Cancel the query.
6462          *
6463          * XXXRTH  Don't cancel the query if waiting for validation?
6464          */
6465         fctx_cancelquery(&query, &devent, finish, ISC_FALSE);
6466
6467         if (keep_trying) {
6468                 if (result == DNS_R_FORMERR)
6469                         broken_server = DNS_R_FORMERR;
6470                 if (broken_server != ISC_R_SUCCESS) {
6471                         /*
6472                          * Add this server to the list of bad servers for
6473                          * this fctx.
6474                          */
6475                         add_bad(fctx, addrinfo, broken_server, broken_type);
6476                 }
6477
6478                 if (get_nameservers) {
6479                         dns_name_t *name;
6480                         dns_fixedname_init(&foundname);
6481                         fname = dns_fixedname_name(&foundname);
6482                         if (result != ISC_R_SUCCESS) {
6483                                 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
6484                                 return;
6485                         }
6486                         findoptions = 0;
6487                         if (dns_rdatatype_atparent(fctx->type))
6488                                 findoptions |= DNS_DBFIND_NOEXACT;
6489                         if ((options & DNS_FETCHOPT_UNSHARED) == 0)
6490                                 name = &fctx->name;
6491                         else
6492                                 name = &fctx->domain;
6493                         result = dns_view_findzonecut(fctx->res->view,
6494                                                       name, fname,
6495                                                       now, findoptions,
6496                                                       ISC_TRUE,
6497                                                       &fctx->nameservers,
6498                                                       NULL);
6499                         if (result != ISC_R_SUCCESS) {
6500                                 FCTXTRACE("couldn't find a zonecut");
6501                                 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
6502                                 return;
6503                         }
6504                         if (!dns_name_issubdomain(fname, &fctx->domain)) {
6505                                 /*
6506                                  * The best nameservers are now above our
6507                                  * QDOMAIN.
6508                                  */
6509                                 FCTXTRACE("nameservers now above QDOMAIN");
6510                                 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
6511                                 return;
6512                         }
6513                         dns_name_free(&fctx->domain,
6514                                       fctx->res->buckets[fctx->bucketnum].mctx);
6515                         dns_name_init(&fctx->domain, NULL);
6516                         result = dns_name_dup(fname,
6517                                               fctx->res->buckets[fctx->bucketnum].mctx,
6518                                               &fctx->domain);
6519                         if (result != ISC_R_SUCCESS) {
6520                                 fctx_done(fctx, DNS_R_SERVFAIL, __LINE__);
6521                                 return;
6522                         }
6523                         fctx_cancelqueries(fctx, ISC_TRUE);
6524                         fctx_cleanupfinds(fctx);
6525                         fctx_cleanupaltfinds(fctx);
6526                         fctx_cleanupforwaddrs(fctx);
6527                         fctx_cleanupaltaddrs(fctx);
6528                 }
6529                 /*
6530                  * Try again.
6531                  */
6532                 fctx_try(fctx, !get_nameservers);
6533         } else if (resend) {
6534                 /*
6535                  * Resend (probably with changed options).
6536                  */
6537                 FCTXTRACE("resend");
6538                 inc_stats(fctx->res, dns_resstatscounter_retry);
6539                 result = fctx_query(fctx, addrinfo, options);
6540                 if (result != ISC_R_SUCCESS)
6541                         fctx_done(fctx, result, __LINE__);
6542         } else if (result == ISC_R_SUCCESS && !HAVE_ANSWER(fctx)) {
6543                 /*
6544                  * All has gone well so far, but we are waiting for the
6545                  * DNSSEC validator to validate the answer.
6546                  */
6547                 FCTXTRACE("wait for validator");
6548                 fctx_cancelqueries(fctx, ISC_TRUE);
6549                 /*
6550                  * We must not retransmit while the validator is working;
6551                  * it has references to the current rmessage.
6552                  */
6553                 result = fctx_stopidletimer(fctx);
6554                 if (result != ISC_R_SUCCESS)
6555                         fctx_done(fctx, result, __LINE__);
6556         } else if (result == DNS_R_CHASEDSSERVERS) {
6557                 unsigned int n;
6558                 add_bad(fctx, addrinfo, result, broken_type);
6559                 fctx_cancelqueries(fctx, ISC_TRUE);
6560                 fctx_cleanupfinds(fctx);
6561                 fctx_cleanupforwaddrs(fctx);
6562
6563                 n = dns_name_countlabels(&fctx->name);
6564                 dns_name_getlabelsequence(&fctx->name, 1, n - 1, &fctx->nsname);
6565
6566                 FCTXTRACE("suspending DS lookup to find parent's NS records");
6567
6568                 result = dns_resolver_createfetch(fctx->res, &fctx->nsname,
6569                                                   dns_rdatatype_ns,
6570                                                   NULL, NULL, NULL, 0, task,
6571                                                   resume_dslookup, fctx,
6572                                                   &fctx->nsrrset, NULL,
6573                                                   &fctx->nsfetch);
6574                 if (result != ISC_R_SUCCESS)
6575                         fctx_done(fctx, result, __LINE__);
6576                 LOCK(&fctx->res->buckets[fctx->bucketnum].lock);
6577                 fctx->references++;
6578                 UNLOCK(&fctx->res->buckets[fctx->bucketnum].lock);
6579                 result = fctx_stopidletimer(fctx);
6580                 if (result != ISC_R_SUCCESS)
6581                         fctx_done(fctx, result, __LINE__);
6582         } else {
6583                 /*
6584                  * We're done.
6585                  */
6586                 fctx_done(fctx, result, __LINE__);
6587         }
6588 }
6589
6590
6591 /***
6592  *** Resolver Methods
6593  ***/
6594
6595 static void
6596 destroy(dns_resolver_t *res) {
6597         unsigned int i;
6598         alternate_t *a;
6599
6600         REQUIRE(res->references == 0);
6601         REQUIRE(!res->priming);
6602         REQUIRE(res->primefetch == NULL);
6603
6604         RTRACE("destroy");
6605
6606         INSIST(res->nfctx == 0);
6607
6608         DESTROYLOCK(&res->primelock);
6609         DESTROYLOCK(&res->nlock);
6610         DESTROYLOCK(&res->lock);
6611         for (i = 0; i < res->nbuckets; i++) {
6612                 INSIST(ISC_LIST_EMPTY(res->buckets[i].fctxs));
6613                 isc_task_shutdown(res->buckets[i].task);
6614                 isc_task_detach(&res->buckets[i].task);
6615                 DESTROYLOCK(&res->buckets[i].lock);
6616                 isc_mem_detach(&res->buckets[i].mctx);
6617         }
6618         isc_mem_put(res->mctx, res->buckets,
6619                     res->nbuckets * sizeof(fctxbucket_t));
6620         if (res->dispatchv4 != NULL)
6621                 dns_dispatch_detach(&res->dispatchv4);
6622         if (res->dispatchv6 != NULL)
6623                 dns_dispatch_detach(&res->dispatchv6);
6624         while ((a = ISC_LIST_HEAD(res->alternates)) != NULL) {
6625                 ISC_LIST_UNLINK(res->alternates, a, link);
6626                 if (!a->isaddress)
6627                         dns_name_free(&a->_u._n.name, res->mctx);
6628                 isc_mem_put(res->mctx, a, sizeof(*a));
6629         }
6630         dns_resolver_reset_algorithms(res);
6631         dns_resolver_resetmustbesecure(res);
6632 #if USE_ALGLOCK
6633         isc_rwlock_destroy(&res->alglock);
6634 #endif
6635 #if USE_MBSLOCK
6636         isc_rwlock_destroy(&res->mbslock);
6637 #endif
6638         isc_timer_detach(&res->spillattimer);
6639         res->magic = 0;
6640         isc_mem_put(res->mctx, res, sizeof(*res));
6641 }
6642
6643 static void
6644 send_shutdown_events(dns_resolver_t *res) {
6645         isc_event_t *event, *next_event;
6646         isc_task_t *etask;
6647
6648         /*
6649          * Caller must be holding the resolver lock.
6650          */
6651
6652         for (event = ISC_LIST_HEAD(res->whenshutdown);
6653              event != NULL;
6654              event = next_event) {
6655                 next_event = ISC_LIST_NEXT(event, ev_link);
6656                 ISC_LIST_UNLINK(res->whenshutdown, event, ev_link);
6657                 etask = event->ev_sender;
6658                 event->ev_sender = res;
6659                 isc_task_sendanddetach(&etask, &event);
6660         }
6661 }
6662
6663 static void
6664 empty_bucket(dns_resolver_t *res) {
6665         RTRACE("empty_bucket");
6666
6667         LOCK(&res->lock);
6668
6669         INSIST(res->activebuckets > 0);
6670         res->activebuckets--;
6671         if (res->activebuckets == 0)
6672                 send_shutdown_events(res);
6673
6674         UNLOCK(&res->lock);
6675 }
6676
6677 static void
6678 spillattimer_countdown(isc_task_t *task, isc_event_t *event) {
6679         dns_resolver_t *res = event->ev_arg;
6680         isc_result_t result;
6681         unsigned int count;
6682         isc_boolean_t logit = ISC_FALSE;
6683
6684         REQUIRE(VALID_RESOLVER(res));
6685
6686         UNUSED(task);
6687
6688         LOCK(&res->lock);
6689         INSIST(!res->exiting);
6690         if (res->spillat > res->spillatmin) {
6691                 res->spillat--;
6692                 logit = ISC_TRUE;
6693         }
6694         if (res->spillat <= res->spillatmin) {
6695                 result = isc_timer_reset(res->spillattimer,
6696                                          isc_timertype_inactive, NULL,
6697                                          NULL, ISC_TRUE);
6698                 RUNTIME_CHECK(result == ISC_R_SUCCESS);
6699         }
6700         count = res->spillat;
6701         UNLOCK(&res->lock);
6702         if (logit)
6703                 isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
6704                               DNS_LOGMODULE_RESOLVER, ISC_LOG_NOTICE,
6705                               "clients-per-query decreased to %u", count);
6706
6707         isc_event_free(&event);
6708 }
6709
6710 isc_result_t
6711 dns_resolver_create(dns_view_t *view,
6712                     isc_taskmgr_t *taskmgr, unsigned int ntasks,
6713                     isc_socketmgr_t *socketmgr,
6714                     isc_timermgr_t *timermgr,
6715                     unsigned int options,
6716                     dns_dispatchmgr_t *dispatchmgr,
6717                     dns_dispatch_t *dispatchv4,
6718                     dns_dispatch_t *dispatchv6,
6719                     dns_resolver_t **resp)
6720 {
6721         dns_resolver_t *res;
6722         isc_result_t result = ISC_R_SUCCESS;
6723         unsigned int i, buckets_created = 0;
6724         isc_task_t *task = NULL;
6725         char name[16];
6726         unsigned dispattr;
6727
6728         /*
6729          * Create a resolver.
6730          */
6731
6732         REQUIRE(DNS_VIEW_VALID(view));
6733         REQUIRE(ntasks > 0);
6734         REQUIRE(resp != NULL && *resp == NULL);
6735         REQUIRE(dispatchmgr != NULL);
6736         REQUIRE(dispatchv4 != NULL || dispatchv6 != NULL);
6737
6738         res = isc_mem_get(view->mctx, sizeof(*res));
6739         if (res == NULL)
6740                 return (ISC_R_NOMEMORY);
6741         RTRACE("create");
6742         res->mctx = view->mctx;
6743         res->rdclass = view->rdclass;
6744         res->socketmgr = socketmgr;
6745         res->timermgr = timermgr;
6746         res->taskmgr = taskmgr;
6747         res->dispatchmgr = dispatchmgr;
6748         res->view = view;
6749         res->options = options;
6750         res->lame_ttl = 0;
6751         ISC_LIST_INIT(res->alternates);
6752         res->udpsize = RECV_BUFFER_SIZE;
6753         res->algorithms = NULL;
6754         res->mustbesecure = NULL;
6755         res->spillatmin = res->spillat = 10;
6756         res->spillatmax = 100;
6757         res->spillattimer = NULL;
6758         res->zero_no_soa_ttl = ISC_FALSE;
6759         res->ndisps = 0;
6760         res->nextdisp = 0; /* meaningless at this point, but init it */
6761         res->nbuckets = ntasks;
6762         res->activebuckets = ntasks;
6763         res->buckets = isc_mem_get(view->mctx,
6764                                    ntasks * sizeof(fctxbucket_t));
6765         if (res->buckets == NULL) {
6766                 result = ISC_R_NOMEMORY;
6767                 goto cleanup_res;
6768         }
6769         for (i = 0; i < ntasks; i++) {
6770                 result = isc_mutex_init(&res->buckets[i].lock);
6771                 if (result != ISC_R_SUCCESS)
6772                         goto cleanup_buckets;
6773                 res->buckets[i].task = NULL;
6774                 result = isc_task_create(taskmgr, 0, &res->buckets[i].task);
6775                 if (result != ISC_R_SUCCESS) {
6776                         DESTROYLOCK(&res->buckets[i].lock);
6777                         goto cleanup_buckets;
6778                 }
6779                 res->buckets[i].mctx = NULL;
6780                 snprintf(name, sizeof(name), "res%u", i);
6781 #ifdef ISC_PLATFORM_USETHREADS
6782                 /*
6783                  * Use a separate memory context for each bucket to reduce
6784                  * contention among multiple threads.  Do this only when
6785                  * enabling threads because it will be require more memory.
6786                  */
6787                 result = isc_mem_create(0, 0, &res->buckets[i].mctx);
6788                 if (result != ISC_R_SUCCESS) {
6789                         isc_task_detach(&res->buckets[i].task);
6790                         DESTROYLOCK(&res->buckets[i].lock);
6791                         goto cleanup_buckets;
6792                 }
6793                 isc_mem_setname(res->buckets[i].mctx, name, NULL);
6794 #else
6795                 isc_mem_attach(view->mctx, &res->buckets[i].mctx);
6796 #endif
6797                 isc_task_setname(res->buckets[i].task, name, res);
6798                 ISC_LIST_INIT(res->buckets[i].fctxs);
6799                 res->buckets[i].exiting = ISC_FALSE;
6800                 buckets_created++;
6801         }
6802
6803         res->dispatchv4 = NULL;
6804         if (dispatchv4 != NULL) {
6805                 dns_dispatch_attach(dispatchv4, &res->dispatchv4);
6806                 dispattr = dns_dispatch_getattributes(dispatchv4);
6807                 res->exclusivev4 =
6808                         ISC_TF((dispattr & DNS_DISPATCHATTR_EXCLUSIVE) != 0);
6809         }
6810
6811         res->dispatchv6 = NULL;
6812         if (dispatchv6 != NULL) {
6813                 dns_dispatch_attach(dispatchv6, &res->dispatchv6);
6814                 dispattr = dns_dispatch_getattributes(dispatchv6);
6815                 res->exclusivev6 =
6816                         ISC_TF((dispattr & DNS_DISPATCHATTR_EXCLUSIVE) != 0);
6817         }
6818
6819         res->references = 1;
6820         res->exiting = ISC_FALSE;
6821         res->frozen = ISC_FALSE;
6822         ISC_LIST_INIT(res->whenshutdown);
6823         res->priming = ISC_FALSE;
6824         res->primefetch = NULL;
6825         res->nfctx = 0;
6826
6827         result = isc_mutex_init(&res->lock);
6828         if (result != ISC_R_SUCCESS)
6829                 goto cleanup_dispatches;
6830
6831         result = isc_mutex_init(&res->nlock);
6832         if (result != ISC_R_SUCCESS)
6833                 goto cleanup_lock;
6834
6835         result = isc_mutex_init(&res->primelock);
6836         if (result != ISC_R_SUCCESS)
6837                 goto cleanup_nlock;
6838
6839         task = NULL;
6840         result = isc_task_create(taskmgr, 0, &task);
6841         if (result != ISC_R_SUCCESS)
6842                 goto cleanup_primelock;
6843
6844         result = isc_timer_create(timermgr, isc_timertype_inactive, NULL, NULL,
6845                                   task, spillattimer_countdown, res,
6846                                   &res->spillattimer);
6847         isc_task_detach(&task);
6848         if (result != ISC_R_SUCCESS)
6849                 goto cleanup_primelock;
6850
6851 #if USE_ALGLOCK
6852         result = isc_rwlock_init(&res->alglock, 0, 0);
6853         if (result != ISC_R_SUCCESS)
6854                 goto cleanup_spillattimer;
6855 #endif
6856 #if USE_MBSLOCK
6857         result = isc_rwlock_init(&res->mbslock, 0, 0);
6858         if (result != ISC_R_SUCCESS)
6859                 goto cleanup_alglock;
6860 #endif
6861
6862         res->magic = RES_MAGIC;
6863
6864         *resp = res;
6865
6866         return (ISC_R_SUCCESS);
6867
6868 #if USE_MBSLOCK
6869  cleanup_alglock:
6870 #if USE_ALGLOCK
6871         isc_rwlock_destroy(&res->alglock);
6872 #endif
6873 #endif
6874 #if USE_ALGLOCK || USE_MBSLOCK
6875  cleanup_spillattimer:
6876         isc_timer_detach(&res->spillattimer);
6877 #endif
6878
6879  cleanup_primelock:
6880         DESTROYLOCK(&res->primelock);
6881
6882  cleanup_nlock:
6883         DESTROYLOCK(&res->nlock);
6884
6885  cleanup_lock:
6886         DESTROYLOCK(&res->lock);
6887
6888  cleanup_dispatches:
6889         if (res->dispatchv6 != NULL)
6890                 dns_dispatch_detach(&res->dispatchv6);
6891         if (res->dispatchv4 != NULL)
6892                 dns_dispatch_detach(&res->dispatchv4);
6893
6894  cleanup_buckets:
6895         for (i = 0; i < buckets_created; i++) {
6896                 isc_mem_detach(&res->buckets[i].mctx);
6897                 DESTROYLOCK(&res->buckets[i].lock);
6898                 isc_task_shutdown(res->buckets[i].task);
6899                 isc_task_detach(&res->buckets[i].task);
6900         }
6901         isc_mem_put(view->mctx, res->buckets,
6902                     res->nbuckets * sizeof(fctxbucket_t));
6903
6904  cleanup_res:
6905         isc_mem_put(view->mctx, res, sizeof(*res));
6906
6907         return (result);
6908 }
6909
6910 static void
6911 prime_done(isc_task_t *task, isc_event_t *event) {
6912         dns_resolver_t *res;
6913         dns_fetchevent_t *fevent;
6914         dns_fetch_t *fetch;
6915         dns_db_t *db = NULL;
6916
6917         REQUIRE(event->ev_type == DNS_EVENT_FETCHDONE);
6918         fevent = (dns_fetchevent_t *)event;
6919         res = event->ev_arg;
6920         REQUIRE(VALID_RESOLVER(res));
6921
6922         UNUSED(task);
6923
6924         LOCK(&res->lock);
6925
6926         INSIST(res->priming);
6927         res->priming = ISC_FALSE;
6928         LOCK(&res->primelock);
6929         fetch = res->primefetch;
6930         res->primefetch = NULL;
6931         UNLOCK(&res->primelock);
6932
6933         UNLOCK(&res->lock);
6934
6935         if (fevent->result == ISC_R_SUCCESS &&
6936             res->view->cache != NULL && res->view->hints != NULL) {
6937                 dns_cache_attachdb(res->view->cache, &db);
6938                 dns_root_checkhints(res->view, res->view->hints, db);
6939                 dns_db_detach(&db);
6940         }
6941
6942         if (fevent->node != NULL)
6943                 dns_db_detachnode(fevent->db, &fevent->node);
6944         if (fevent->db != NULL)
6945                 dns_db_detach(&fevent->db);
6946         if (dns_rdataset_isassociated(fevent->rdataset))
6947                 dns_rdataset_disassociate(fevent->rdataset);
6948         INSIST(fevent->sigrdataset == NULL);
6949
6950         isc_mem_put(res->mctx, fevent->rdataset, sizeof(*fevent->rdataset));
6951
6952         isc_event_free(&event);
6953         dns_resolver_destroyfetch(&fetch);
6954 }
6955
6956 void
6957 dns_resolver_prime(dns_resolver_t *res) {
6958         isc_boolean_t want_priming = ISC_FALSE;
6959         dns_rdataset_t *rdataset;
6960         isc_result_t result;
6961
6962         REQUIRE(VALID_RESOLVER(res));
6963         REQUIRE(res->frozen);
6964
6965         RTRACE("dns_resolver_prime");
6966
6967         LOCK(&res->lock);
6968
6969         if (!res->exiting && !res->priming) {
6970                 INSIST(res->primefetch == NULL);
6971                 res->priming = ISC_TRUE;
6972                 want_priming = ISC_TRUE;
6973         }
6974
6975         UNLOCK(&res->lock);
6976
6977         if (want_priming) {
6978                 /*
6979                  * To avoid any possible recursive locking problems, we
6980                  * start the priming fetch like any other fetch, and holding
6981                  * no resolver locks.  No one else will try to start it
6982                  * because we're the ones who set res->priming to true.
6983                  * Any other callers of dns_resolver_prime() while we're
6984                  * running will see that res->priming is already true and
6985                  * do nothing.
6986                  */
6987                 RTRACE("priming");
6988                 rdataset = isc_mem_get(res->mctx, sizeof(*rdataset));
6989                 if (rdataset == NULL) {
6990                         LOCK(&res->lock);
6991                         INSIST(res->priming);
6992                         INSIST(res->primefetch == NULL);
6993                         res->priming = ISC_FALSE;
6994                         UNLOCK(&res->lock);
6995                         return;
6996                 }
6997                 dns_rdataset_init(rdataset);
6998                 LOCK(&res->primelock);
6999                 result = dns_resolver_createfetch(res, dns_rootname,
7000                                                   dns_rdatatype_ns,
7001                                                   NULL, NULL, NULL, 0,
7002                                                   res->buckets[0].task,
7003                                                   prime_done,
7004                                                   res, rdataset, NULL,
7005                                                   &res->primefetch);
7006                 UNLOCK(&res->primelock);
7007                 if (result != ISC_R_SUCCESS) {
7008                         LOCK(&res->lock);
7009                         INSIST(res->priming);
7010                         res->priming = ISC_FALSE;
7011                         UNLOCK(&res->lock);
7012                 }
7013         }
7014 }
7015
7016 void
7017 dns_resolver_freeze(dns_resolver_t *res) {
7018
7019         /*
7020          * Freeze resolver.
7021          */
7022
7023         REQUIRE(VALID_RESOLVER(res));
7024         REQUIRE(!res->frozen);
7025
7026         res->frozen = ISC_TRUE;
7027 }
7028
7029 void
7030 dns_resolver_attach(dns_resolver_t *source, dns_resolver_t **targetp) {
7031         REQUIRE(VALID_RESOLVER(source));
7032         REQUIRE(targetp != NULL && *targetp == NULL);
7033
7034         RRTRACE(source, "attach");
7035         LOCK(&source->lock);
7036         REQUIRE(!source->exiting);
7037
7038         INSIST(source->references > 0);
7039         source->references++;
7040         INSIST(source->references != 0);
7041         UNLOCK(&source->lock);
7042
7043         *targetp = source;
7044 }
7045
7046 void
7047 dns_resolver_whenshutdown(dns_resolver_t *res, isc_task_t *task,
7048                           isc_event_t **eventp)
7049 {
7050         isc_task_t *clone;
7051         isc_event_t *event;
7052
7053         REQUIRE(VALID_RESOLVER(res));
7054         REQUIRE(eventp != NULL);
7055
7056         event = *eventp;
7057         *eventp = NULL;
7058
7059         LOCK(&res->lock);
7060
7061         if (res->exiting && res->activebuckets == 0) {
7062                 /*
7063                  * We're already shutdown.  Send the event.
7064                  */
7065                 event->ev_sender = res;
7066                 isc_task_send(task, &event);
7067         } else {
7068                 clone = NULL;
7069                 isc_task_attach(task, &clone);
7070                 event->ev_sender = clone;
7071                 ISC_LIST_APPEND(res->whenshutdown, event, ev_link);
7072         }
7073
7074         UNLOCK(&res->lock);
7075 }
7076
7077 void
7078 dns_resolver_shutdown(dns_resolver_t *res) {
7079         unsigned int i;
7080         fetchctx_t *fctx;
7081         isc_socket_t *sock;
7082         isc_result_t result;
7083
7084         REQUIRE(VALID_RESOLVER(res));
7085
7086         RTRACE("shutdown");
7087
7088         LOCK(&res->lock);
7089
7090         if (!res->exiting) {
7091                 RTRACE("exiting");
7092                 res->exiting = ISC_TRUE;
7093
7094                 for (i = 0; i < res->nbuckets; i++) {
7095                         LOCK(&res->buckets[i].lock);
7096                         for (fctx = ISC_LIST_HEAD(res->buckets[i].fctxs);
7097                              fctx != NULL;
7098                              fctx = ISC_LIST_NEXT(fctx, link))
7099                                 fctx_shutdown(fctx);
7100                         if (res->dispatchv4 != NULL && !res->exclusivev4) {
7101                                 sock = dns_dispatch_getsocket(res->dispatchv4);
7102                                 isc_socket_cancel(sock, res->buckets[i].task,
7103                                                   ISC_SOCKCANCEL_ALL);
7104                         }
7105                         if (res->dispatchv6 != NULL && !res->exclusivev6) {
7106                                 sock = dns_dispatch_getsocket(res->dispatchv6);
7107                                 isc_socket_cancel(sock, res->buckets[i].task,
7108                                                   ISC_SOCKCANCEL_ALL);
7109                         }
7110                         res->buckets[i].exiting = ISC_TRUE;
7111                         if (ISC_LIST_EMPTY(res->buckets[i].fctxs)) {
7112                                 INSIST(res->activebuckets > 0);
7113                                 res->activebuckets--;
7114                         }
7115                         UNLOCK(&res->buckets[i].lock);
7116                 }
7117                 if (res->activebuckets == 0)
7118                         send_shutdown_events(res);
7119                 result = isc_timer_reset(res->spillattimer,
7120                                          isc_timertype_inactive, NULL,
7121                                          NULL, ISC_TRUE);
7122                 RUNTIME_CHECK(result == ISC_R_SUCCESS);
7123         }
7124
7125         UNLOCK(&res->lock);
7126 }
7127
7128 void
7129 dns_resolver_detach(dns_resolver_t **resp) {
7130         dns_resolver_t *res;
7131         isc_boolean_t need_destroy = ISC_FALSE;
7132
7133         REQUIRE(resp != NULL);
7134         res = *resp;
7135         REQUIRE(VALID_RESOLVER(res));
7136
7137         RTRACE("detach");
7138
7139         LOCK(&res->lock);
7140
7141         INSIST(res->references > 0);
7142         res->references--;
7143         if (res->references == 0) {
7144                 INSIST(res->exiting && res->activebuckets == 0);
7145                 need_destroy = ISC_TRUE;
7146         }
7147
7148         UNLOCK(&res->lock);
7149
7150         if (need_destroy)
7151                 destroy(res);
7152
7153         *resp = NULL;
7154 }
7155
7156 static inline isc_boolean_t
7157 fctx_match(fetchctx_t *fctx, dns_name_t *name, dns_rdatatype_t type,
7158            unsigned int options)
7159 {
7160         if (fctx->type != type || fctx->options != options)
7161                 return (ISC_FALSE);
7162         return (dns_name_equal(&fctx->name, name));
7163 }
7164
7165 static inline void
7166 log_fetch(dns_name_t *name, dns_rdatatype_t type) {
7167         char namebuf[DNS_NAME_FORMATSIZE];
7168         char typebuf[DNS_RDATATYPE_FORMATSIZE];
7169         int level = ISC_LOG_DEBUG(1);
7170
7171         if (! isc_log_wouldlog(dns_lctx, level))
7172                 return;
7173
7174         dns_name_format(name, namebuf, sizeof(namebuf));
7175         dns_rdatatype_format(type, typebuf, sizeof(typebuf));
7176
7177         isc_log_write(dns_lctx, DNS_LOGCATEGORY_RESOLVER,
7178                       DNS_LOGMODULE_RESOLVER, level,
7179                       "createfetch: %s %s", namebuf, typebuf);
7180 }
7181
7182 isc_result_t
7183 dns_resolver_createfetch(dns_resolver_t *res, dns_name_t *name,
7184                          dns_rdatatype_t type,
7185                          dns_name_t *domain, dns_rdataset_t *nameservers,
7186                          dns_forwarders_t *forwarders,
7187                          unsigned int options, isc_task_t *task,
7188                          isc_taskaction_t action, void *arg,
7189                          dns_rdataset_t *rdataset,
7190                          dns_rdataset_t *sigrdataset,
7191                          dns_fetch_t **fetchp)
7192 {
7193         return (dns_resolver_createfetch2(res, name, type, domain,
7194                                           nameservers, forwarders, NULL, 0,
7195                                           options, task, action, arg,
7196                                           rdataset, sigrdataset, fetchp));
7197 }
7198
7199 isc_result_t
7200 dns_resolver_createfetch2(dns_resolver_t *res, dns_name_t *name,
7201                           dns_rdatatype_t type,
7202                           dns_name_t *domain, dns_rdataset_t *nameservers,
7203                           dns_forwarders_t *forwarders,
7204                           isc_sockaddr_t *client, dns_messageid_t id,
7205                           unsigned int options, isc_task_t *task,
7206                           isc_taskaction_t action, void *arg,
7207                           dns_rdataset_t *rdataset,
7208                           dns_rdataset_t *sigrdataset,
7209                           dns_fetch_t **fetchp)
7210 {
7211         dns_fetch_t *fetch;
7212         fetchctx_t *fctx = NULL;
7213         isc_result_t result = ISC_R_SUCCESS;
7214         unsigned int bucketnum;
7215         isc_boolean_t new_fctx = ISC_FALSE;
7216         isc_event_t *event;
7217         unsigned int count = 0;
7218         unsigned int spillat;
7219         unsigned int spillatmin;
7220
7221         UNUSED(forwarders);
7222
7223         REQUIRE(VALID_RESOLVER(res));
7224         REQUIRE(res->frozen);
7225         /* XXXRTH  Check for meta type */
7226         if (domain != NULL) {
7227                 REQUIRE(DNS_RDATASET_VALID(nameservers));
7228                 REQUIRE(nameservers->type == dns_rdatatype_ns);
7229         } else
7230                 REQUIRE(nameservers == NULL);
7231         REQUIRE(forwarders == NULL);
7232         REQUIRE(!dns_rdataset_isassociated(rdataset));
7233         REQUIRE(sigrdataset == NULL ||
7234                 !dns_rdataset_isassociated(sigrdataset));
7235         REQUIRE(fetchp != NULL && *fetchp == NULL);
7236
7237         log_fetch(name, type);
7238
7239         /*
7240          * XXXRTH  use a mempool?
7241          */
7242         fetch = isc_mem_get(res->mctx, sizeof(*fetch));
7243         if (fetch == NULL)
7244                 return (ISC_R_NOMEMORY);
7245
7246         bucketnum = dns_name_fullhash(name, ISC_FALSE) % res->nbuckets;
7247
7248         LOCK(&res->lock);
7249         spillat = res->spillat;
7250         spillatmin = res->spillatmin;
7251         UNLOCK(&res->lock);
7252         LOCK(&res->buckets[bucketnum].lock);
7253
7254         if (res->buckets[bucketnum].exiting) {
7255                 result = ISC_R_SHUTTINGDOWN;
7256                 goto unlock;
7257         }
7258
7259         if ((options & DNS_FETCHOPT_UNSHARED) == 0) {
7260                 for (fctx = ISC_LIST_HEAD(res->buckets[bucketnum].fctxs);
7261                      fctx != NULL;
7262                      fctx = ISC_LIST_NEXT(fctx, link)) {
7263                         if (fctx_match(fctx, name, type, options))
7264                                 break;
7265                 }
7266         }
7267
7268         /*
7269          * Is this a duplicate?
7270          */
7271         if (fctx != NULL && client != NULL) {
7272                 dns_fetchevent_t *fevent;
7273                 for (fevent = ISC_LIST_HEAD(fctx->events);
7274                      fevent != NULL;
7275                      fevent = ISC_LIST_NEXT(fevent, ev_link)) {
7276                         if (fevent->client != NULL && fevent->id == id &&
7277                             isc_sockaddr_equal(fevent->client, client)) {
7278                                 result = DNS_R_DUPLICATE;
7279                                 goto unlock;
7280                         }
7281                         count++;
7282                 }
7283         }
7284         if (count >= spillatmin && spillatmin != 0) {
7285                 INSIST(fctx != NULL);
7286                 if (count >= spillat)
7287                         fctx->spilled = ISC_TRUE;
7288                 if (fctx->spilled) {
7289                         result = DNS_R_DROP;
7290                         goto unlock;
7291                 }
7292         }
7293
7294         /*
7295          * If we didn't have a fetch, would attach to a done fetch, this
7296          * fetch has already cloned its results, or if the fetch has gone
7297          * "idle" (no one was interested in it), we need to start a new
7298          * fetch instead of joining with the existing one.
7299          */
7300         if (fctx == NULL ||
7301             fctx->state == fetchstate_done ||
7302             fctx->cloned ||
7303             ISC_LIST_EMPTY(fctx->events)) {
7304                 fctx = NULL;
7305                 result = fctx_create(res, name, type, domain, nameservers,
7306                                      options, bucketnum, &fctx);
7307                 if (result != ISC_R_SUCCESS)
7308                         goto unlock;
7309                 new_fctx = ISC_TRUE;
7310         }
7311
7312         result = fctx_join(fctx, task, client, id, action, arg,
7313                            rdataset, sigrdataset, fetch);
7314         if (new_fctx) {
7315                 if (result == ISC_R_SUCCESS) {
7316                         /*
7317                          * Launch this fctx.
7318                          */
7319                         event = &fctx->control_event;
7320                         ISC_EVENT_INIT(event, sizeof(*event), 0, NULL,
7321                                        DNS_EVENT_FETCHCONTROL,
7322                                        fctx_start, fctx, NULL,
7323                                        NULL, NULL);
7324                         isc_task_send(res->buckets[bucketnum].task, &event);
7325                 } else {
7326                         /*
7327                          * We don't care about the result of fctx_destroy()
7328                          * since we know we're not exiting.
7329                          */
7330                         (void)fctx_destroy(fctx);
7331                 }
7332         }
7333
7334  unlock:
7335         UNLOCK(&res->buckets[bucketnum].lock);
7336
7337         if (result == ISC_R_SUCCESS) {
7338                 FTRACE("created");
7339                 *fetchp = fetch;
7340         } else
7341                 isc_mem_put(res->mctx, fetch, sizeof(*fetch));
7342
7343         return (result);
7344 }
7345
7346 void
7347 dns_resolver_cancelfetch(dns_fetch_t *fetch) {
7348         fetchctx_t *fctx;
7349         dns_resolver_t *res;
7350         dns_fetchevent_t *event, *next_event;
7351         isc_task_t *etask;
7352
7353         REQUIRE(DNS_FETCH_VALID(fetch));
7354         fctx = fetch->private;
7355         REQUIRE(VALID_FCTX(fctx));
7356         res = fctx->res;
7357
7358         FTRACE("cancelfetch");
7359
7360         LOCK(&res->buckets[fctx->bucketnum].lock);
7361
7362         /*
7363          * Find the completion event for this fetch (as opposed
7364          * to those for other fetches that have joined the same
7365          * fctx) and send it with result = ISC_R_CANCELED.
7366          */
7367         event = NULL;
7368         if (fctx->state != fetchstate_done) {
7369                 for (event = ISC_LIST_HEAD(fctx->events);
7370                      event != NULL;
7371                      event = next_event) {
7372                         next_event = ISC_LIST_NEXT(event, ev_link);
7373                         if (event->fetch == fetch) {
7374                                 ISC_LIST_UNLINK(fctx->events, event, ev_link);
7375                                 break;
7376                         }
7377                 }
7378         }
7379         if (event != NULL) {
7380                 etask = event->ev_sender;
7381                 event->ev_sender = fctx;
7382                 event->result = ISC_R_CANCELED;
7383                 isc_task_sendanddetach(&etask, ISC_EVENT_PTR(&event));
7384         }
7385         /*
7386          * The fctx continues running even if no fetches remain;
7387          * the answer is still cached.
7388          */
7389
7390         UNLOCK(&res->buckets[fctx->bucketnum].lock);
7391 }
7392
7393 void
7394 dns_resolver_destroyfetch(dns_fetch_t **fetchp) {
7395         dns_fetch_t *fetch;
7396         dns_resolver_t *res;
7397         dns_fetchevent_t *event, *next_event;
7398         fetchctx_t *fctx;
7399         unsigned int bucketnum;
7400         isc_boolean_t bucket_empty = ISC_FALSE;
7401
7402         REQUIRE(fetchp != NULL);
7403         fetch = *fetchp;
7404         REQUIRE(DNS_FETCH_VALID(fetch));
7405         fctx = fetch->private;
7406         REQUIRE(VALID_FCTX(fctx));
7407         res = fctx->res;
7408
7409         FTRACE("destroyfetch");
7410
7411         bucketnum = fctx->bucketnum;
7412         LOCK(&res->buckets[bucketnum].lock);
7413
7414         /*
7415          * Sanity check: the caller should have gotten its event before
7416          * trying to destroy the fetch.
7417          */
7418         event = NULL;
7419         if (fctx->state != fetchstate_done) {
7420                 for (event = ISC_LIST_HEAD(fctx->events);
7421                      event != NULL;
7422                      event = next_event) {
7423                         next_event = ISC_LIST_NEXT(event, ev_link);
7424                         RUNTIME_CHECK(event->fetch != fetch);
7425                 }
7426         }
7427
7428         INSIST(fctx->references > 0);
7429         fctx->references--;
7430         if (fctx->references == 0) {
7431                 /*
7432                  * No one cares about the result of this fetch anymore.
7433                  */
7434                 if (fctx->pending == 0 && fctx->nqueries == 0 &&
7435                     ISC_LIST_EMPTY(fctx->validators) &&
7436                     SHUTTINGDOWN(fctx)) {
7437                         /*
7438                          * This fctx is already shutdown; we were just
7439                          * waiting for the last reference to go away.
7440                          */
7441                         bucket_empty = fctx_destroy(fctx);
7442                 } else {
7443                         /*
7444                          * Initiate shutdown.
7445                          */
7446                         fctx_shutdown(fctx);
7447                 }
7448         }
7449
7450         UNLOCK(&res->buckets[bucketnum].lock);
7451
7452         isc_mem_put(res->mctx, fetch, sizeof(*fetch));
7453         *fetchp = NULL;
7454
7455         if (bucket_empty)
7456                 empty_bucket(res);
7457 }
7458
7459 void
7460 dns_resolver_logfetch(dns_fetch_t *fetch, isc_log_t *lctx,
7461                       isc_logcategory_t *category, isc_logmodule_t *module,
7462                       int level, isc_boolean_t duplicateok)
7463 {
7464         fetchctx_t *fctx;
7465         dns_resolver_t *res;
7466         char domainbuf[DNS_NAME_FORMATSIZE];
7467
7468         REQUIRE(DNS_FETCH_VALID(fetch));
7469         fctx = fetch->private;
7470         REQUIRE(VALID_FCTX(fctx));
7471         res = fctx->res;
7472
7473         LOCK(&res->buckets[fctx->bucketnum].lock);
7474
7475         INSIST(fctx->exitline >= 0);
7476         if (!fctx->logged || duplicateok) {
7477                 dns_name_format(&fctx->domain, domainbuf, sizeof(domainbuf));
7478                 isc_log_write(lctx, category, module, level,
7479                               "fetch completed at %s:%d for %s in "
7480                               "%" ISC_PRINT_QUADFORMAT "u."
7481                               "%06" ISC_PRINT_QUADFORMAT "u: %s/%s "
7482                               "[domain:%s,referral:%u,restart:%u,qrysent:%u,"
7483                               "timeout:%u,lame:%u,neterr:%u,badresp:%u,"
7484                               "adberr:%u,findfail:%u,valfail:%u]",
7485                               __FILE__, fctx->exitline, fctx->info,
7486                               fctx->duration / 1000000,
7487                               fctx->duration % 1000000,
7488                               isc_result_totext(fctx->result),
7489                               isc_result_totext(fctx->vresult), domainbuf,
7490                               fctx->referrals, fctx->restarts,
7491                               fctx->querysent, fctx->timeouts, fctx->lamecount,
7492                               fctx->neterr, fctx->badresp, fctx->adberr,
7493                               fctx->findfail, fctx->valfail);
7494                 fctx->logged = ISC_TRUE;
7495         }
7496
7497         UNLOCK(&res->buckets[fctx->bucketnum].lock);
7498 }
7499
7500 dns_dispatchmgr_t *
7501 dns_resolver_dispatchmgr(dns_resolver_t *resolver) {
7502         REQUIRE(VALID_RESOLVER(resolver));
7503         return (resolver->dispatchmgr);
7504 }
7505
7506 dns_dispatch_t *
7507 dns_resolver_dispatchv4(dns_resolver_t *resolver) {
7508         REQUIRE(VALID_RESOLVER(resolver));
7509         return (resolver->dispatchv4);
7510 }
7511
7512 dns_dispatch_t *
7513 dns_resolver_dispatchv6(dns_resolver_t *resolver) {
7514         REQUIRE(VALID_RESOLVER(resolver));
7515         return (resolver->dispatchv6);
7516 }
7517
7518 isc_socketmgr_t *
7519 dns_resolver_socketmgr(dns_resolver_t *resolver) {
7520         REQUIRE(VALID_RESOLVER(resolver));
7521         return (resolver->socketmgr);
7522 }
7523
7524 isc_taskmgr_t *
7525 dns_resolver_taskmgr(dns_resolver_t *resolver) {
7526         REQUIRE(VALID_RESOLVER(resolver));
7527         return (resolver->taskmgr);
7528 }
7529
7530 isc_uint32_t
7531 dns_resolver_getlamettl(dns_resolver_t *resolver) {
7532         REQUIRE(VALID_RESOLVER(resolver));
7533         return (resolver->lame_ttl);
7534 }
7535
7536 void
7537 dns_resolver_setlamettl(dns_resolver_t *resolver, isc_uint32_t lame_ttl) {
7538         REQUIRE(VALID_RESOLVER(resolver));
7539         resolver->lame_ttl = lame_ttl;
7540 }
7541
7542 unsigned int
7543 dns_resolver_nrunning(dns_resolver_t *resolver) {
7544         unsigned int n;
7545         LOCK(&resolver->nlock);
7546         n = resolver->nfctx;
7547         UNLOCK(&resolver->nlock);
7548         return (n);
7549 }
7550
7551 isc_result_t
7552 dns_resolver_addalternate(dns_resolver_t *resolver, isc_sockaddr_t *alt,
7553                           dns_name_t *name, in_port_t port) {
7554         alternate_t *a;
7555         isc_result_t result;
7556
7557         REQUIRE(VALID_RESOLVER(resolver));
7558         REQUIRE(!resolver->frozen);
7559         REQUIRE((alt == NULL) ^ (name == NULL));
7560
7561         a = isc_mem_get(resolver->mctx, sizeof(*a));
7562         if (a == NULL)
7563                 return (ISC_R_NOMEMORY);
7564         if (alt != NULL) {
7565                 a->isaddress = ISC_TRUE;
7566                 a->_u.addr = *alt;
7567         } else {
7568                 a->isaddress = ISC_FALSE;
7569                 a->_u._n.port = port;
7570                 dns_name_init(&a->_u._n.name, NULL);
7571                 result = dns_name_dup(name, resolver->mctx, &a->_u._n.name);
7572                 if (result != ISC_R_SUCCESS) {
7573                         isc_mem_put(resolver->mctx, a, sizeof(*a));
7574                         return (result);
7575                 }
7576         }
7577         ISC_LINK_INIT(a, link);
7578         ISC_LIST_APPEND(resolver->alternates, a, link);
7579
7580         return (ISC_R_SUCCESS);
7581 }
7582
7583 void
7584 dns_resolver_setudpsize(dns_resolver_t *resolver, isc_uint16_t udpsize) {
7585         REQUIRE(VALID_RESOLVER(resolver));
7586         resolver->udpsize = udpsize;
7587 }
7588
7589 isc_uint16_t
7590 dns_resolver_getudpsize(dns_resolver_t *resolver) {
7591         REQUIRE(VALID_RESOLVER(resolver));
7592         return (resolver->udpsize);
7593 }
7594
7595 static void
7596 free_algorithm(void *node, void *arg) {
7597         unsigned char *algorithms = node;
7598         isc_mem_t *mctx = arg;
7599
7600         isc_mem_put(mctx, algorithms, *algorithms);
7601 }
7602
7603 void
7604 dns_resolver_reset_algorithms(dns_resolver_t *resolver) {
7605
7606         REQUIRE(VALID_RESOLVER(resolver));
7607
7608 #if USE_ALGLOCK
7609         RWLOCK(&resolver->alglock, isc_rwlocktype_write);
7610 #endif
7611         if (resolver->algorithms != NULL)
7612                 dns_rbt_destroy(&resolver->algorithms);
7613 #if USE_ALGLOCK
7614         RWUNLOCK(&resolver->alglock, isc_rwlocktype_write);
7615 #endif
7616 }
7617
7618 isc_result_t
7619 dns_resolver_disable_algorithm(dns_resolver_t *resolver, dns_name_t *name,
7620                                unsigned int alg)
7621 {
7622         unsigned int len, mask;
7623         unsigned char *new;
7624         unsigned char *algorithms;
7625         isc_result_t result;
7626         dns_rbtnode_t *node = NULL;
7627
7628         REQUIRE(VALID_RESOLVER(resolver));
7629         if (alg > 255)
7630                 return (ISC_R_RANGE);
7631
7632 #if USE_ALGLOCK
7633         RWLOCK(&resolver->alglock, isc_rwlocktype_write);
7634 #endif
7635         if (resolver->algorithms == NULL) {
7636                 result = dns_rbt_create(resolver->mctx, free_algorithm,
7637                                         resolver->mctx, &resolver->algorithms);
7638                 if (result != ISC_R_SUCCESS)
7639                         goto cleanup;
7640         }
7641
7642         len = alg/8 + 2;
7643         mask = 1 << (alg%8);
7644
7645         result = dns_rbt_addnode(resolver->algorithms, name, &node);
7646
7647         if (result == ISC_R_SUCCESS || result == ISC_R_EXISTS) {
7648                 algorithms = node->data;
7649                 if (algorithms == NULL || len > *algorithms) {
7650                         new = isc_mem_get(resolver->mctx, len);
7651                         if (new == NULL) {
7652                                 result = ISC_R_NOMEMORY;
7653                                 goto cleanup;
7654                         }
7655                         memset(new, 0, len);
7656                         if (algorithms != NULL)
7657                                 memcpy(new, algorithms, *algorithms);
7658                         new[len-1] |= mask;
7659                         *new = len;
7660                         node->data = new;
7661                         if (algorithms != NULL)
7662                                 isc_mem_put(resolver->mctx, algorithms,
7663                                             *algorithms);
7664                 } else
7665                         algorithms[len-1] |= mask;
7666         }
7667         result = ISC_R_SUCCESS;
7668  cleanup:
7669 #if USE_ALGLOCK
7670         RWUNLOCK(&resolver->alglock, isc_rwlocktype_write);
7671 #endif
7672         return (result);
7673 }
7674
7675 isc_boolean_t
7676 dns_resolver_algorithm_supported(dns_resolver_t *resolver, dns_name_t *name,
7677                                  unsigned int alg)
7678 {
7679         unsigned int len, mask;
7680         unsigned char *algorithms;
7681         void *data = NULL;
7682         isc_result_t result;
7683         isc_boolean_t found = ISC_FALSE;
7684
7685         REQUIRE(VALID_RESOLVER(resolver));
7686
7687 #if USE_ALGLOCK
7688         RWLOCK(&resolver->alglock, isc_rwlocktype_read);
7689 #endif
7690         if (resolver->algorithms == NULL)
7691                 goto unlock;
7692         result = dns_rbt_findname(resolver->algorithms, name, 0, NULL, &data);
7693         if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
7694                 len = alg/8 + 2;
7695                 mask = 1 << (alg%8);
7696                 algorithms = data;
7697                 if (len <= *algorithms && (algorithms[len-1] & mask) != 0)
7698                         found = ISC_TRUE;
7699         }
7700  unlock:
7701 #if USE_ALGLOCK
7702         RWUNLOCK(&resolver->alglock, isc_rwlocktype_read);
7703 #endif
7704         if (found)
7705                 return (ISC_FALSE);
7706         return (dst_algorithm_supported(alg));
7707 }
7708
7709 isc_boolean_t
7710 dns_resolver_digest_supported(dns_resolver_t *resolver, unsigned int digest) {
7711
7712         UNUSED(resolver);
7713         return (dns_ds_digest_supported(digest));
7714 }
7715
7716 void
7717 dns_resolver_resetmustbesecure(dns_resolver_t *resolver) {
7718
7719         REQUIRE(VALID_RESOLVER(resolver));
7720
7721 #if USE_MBSLOCK
7722         RWLOCK(&resolver->mbslock, isc_rwlocktype_write);
7723 #endif
7724         if (resolver->mustbesecure != NULL)
7725                 dns_rbt_destroy(&resolver->mustbesecure);
7726 #if USE_MBSLOCK
7727         RWUNLOCK(&resolver->mbslock, isc_rwlocktype_write);
7728 #endif
7729 }
7730
7731 static isc_boolean_t yes = ISC_TRUE, no = ISC_FALSE;
7732
7733 isc_result_t
7734 dns_resolver_setmustbesecure(dns_resolver_t *resolver, dns_name_t *name,
7735                              isc_boolean_t value)
7736 {
7737         isc_result_t result;
7738
7739         REQUIRE(VALID_RESOLVER(resolver));
7740
7741 #if USE_MBSLOCK
7742         RWLOCK(&resolver->mbslock, isc_rwlocktype_write);
7743 #endif
7744         if (resolver->mustbesecure == NULL) {
7745                 result = dns_rbt_create(resolver->mctx, NULL, NULL,
7746                                         &resolver->mustbesecure);
7747                 if (result != ISC_R_SUCCESS)
7748                         goto cleanup;
7749         }
7750         result = dns_rbt_addname(resolver->mustbesecure, name,
7751                                  value ? &yes : &no);
7752  cleanup:
7753 #if USE_MBSLOCK
7754         RWUNLOCK(&resolver->mbslock, isc_rwlocktype_write);
7755 #endif
7756         return (result);
7757 }
7758
7759 isc_boolean_t
7760 dns_resolver_getmustbesecure(dns_resolver_t *resolver, dns_name_t *name) {
7761         void *data = NULL;
7762         isc_boolean_t value = ISC_FALSE;
7763         isc_result_t result;
7764
7765         REQUIRE(VALID_RESOLVER(resolver));
7766
7767 #if USE_MBSLOCK
7768         RWLOCK(&resolver->mbslock, isc_rwlocktype_read);
7769 #endif
7770         if (resolver->mustbesecure == NULL)
7771                 goto unlock;
7772         result = dns_rbt_findname(resolver->mustbesecure, name, 0, NULL, &data);
7773         if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
7774                 value = *(isc_boolean_t*)data;
7775  unlock:
7776 #if USE_MBSLOCK
7777         RWUNLOCK(&resolver->mbslock, isc_rwlocktype_read);
7778 #endif
7779         return (value);
7780 }
7781
7782 void
7783 dns_resolver_getclientsperquery(dns_resolver_t *resolver, isc_uint32_t *cur,
7784                                 isc_uint32_t *min, isc_uint32_t *max)
7785 {
7786         REQUIRE(VALID_RESOLVER(resolver));
7787
7788         LOCK(&resolver->lock);
7789         if (cur != NULL)
7790                 *cur = resolver->spillat;
7791         if (min != NULL)
7792                 *min = resolver->spillatmin;
7793         if (max != NULL)
7794                 *max = resolver->spillatmax;
7795         UNLOCK(&resolver->lock);
7796 }
7797
7798 void
7799 dns_resolver_setclientsperquery(dns_resolver_t *resolver, isc_uint32_t min,
7800                                 isc_uint32_t max)
7801 {
7802         REQUIRE(VALID_RESOLVER(resolver));
7803
7804         LOCK(&resolver->lock);
7805         resolver->spillatmin = resolver->spillat = min;
7806         resolver->spillatmax = max;
7807         UNLOCK(&resolver->lock);
7808 }
7809
7810 isc_boolean_t
7811 dns_resolver_getzeronosoattl(dns_resolver_t *resolver) {
7812         REQUIRE(VALID_RESOLVER(resolver));
7813
7814         return (resolver->zero_no_soa_ttl);
7815 }
7816
7817 void
7818 dns_resolver_setzeronosoattl(dns_resolver_t *resolver, isc_boolean_t state) {
7819         REQUIRE(VALID_RESOLVER(resolver));
7820
7821         resolver->zero_no_soa_ttl = state;
7822 }
7823
7824 unsigned int
7825 dns_resolver_getoptions(dns_resolver_t *resolver) {
7826         REQUIRE(VALID_RESOLVER(resolver));
7827
7828         return (resolver->options);
7829 }