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