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