PageRenderTime 59ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/contrib/bind9/lib/dns/adb.c

https://bitbucket.org/freebsd/freebsd-head/
C | 4113 lines | 2943 code | 573 blank | 597 comment | 770 complexity | 4b87144099769830b153bfe500bfa4ea MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, LGPL-2.0, LGPL-2.1, BSD-2-Clause, 0BSD, JSON, AGPL-1.0, GPL-2.0
  1. /*
  2. * Copyright (C) 2004-2012 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 1999-2003 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. * PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* $Id$ */
  18. /*! \file
  19. *
  20. * \note
  21. * In finds, if task == NULL, no events will be generated, and no events
  22. * have been sent. If task != NULL but taskaction == NULL, an event has been
  23. * posted but not yet freed. If neither are NULL, no event was posted.
  24. *
  25. */
  26. #include <config.h>
  27. #include <limits.h>
  28. #include <isc/mutexblock.h>
  29. #include <isc/netaddr.h>
  30. #include <isc/random.h>
  31. #include <isc/stats.h>
  32. #include <isc/string.h> /* Required for HP/UX (and others?) */
  33. #include <isc/task.h>
  34. #include <isc/util.h>
  35. #include <dns/adb.h>
  36. #include <dns/db.h>
  37. #include <dns/events.h>
  38. #include <dns/log.h>
  39. #include <dns/rdata.h>
  40. #include <dns/rdataset.h>
  41. #include <dns/rdatastruct.h>
  42. #include <dns/rdatatype.h>
  43. #include <dns/resolver.h>
  44. #include <dns/result.h>
  45. #include <dns/stats.h>
  46. #define DNS_ADB_MAGIC ISC_MAGIC('D', 'a', 'd', 'b')
  47. #define DNS_ADB_VALID(x) ISC_MAGIC_VALID(x, DNS_ADB_MAGIC)
  48. #define DNS_ADBNAME_MAGIC ISC_MAGIC('a', 'd', 'b', 'N')
  49. #define DNS_ADBNAME_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAME_MAGIC)
  50. #define DNS_ADBNAMEHOOK_MAGIC ISC_MAGIC('a', 'd', 'N', 'H')
  51. #define DNS_ADBNAMEHOOK_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAMEHOOK_MAGIC)
  52. #define DNS_ADBLAMEINFO_MAGIC ISC_MAGIC('a', 'd', 'b', 'Z')
  53. #define DNS_ADBLAMEINFO_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBLAMEINFO_MAGIC)
  54. #define DNS_ADBENTRY_MAGIC ISC_MAGIC('a', 'd', 'b', 'E')
  55. #define DNS_ADBENTRY_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBENTRY_MAGIC)
  56. #define DNS_ADBFETCH_MAGIC ISC_MAGIC('a', 'd', 'F', '4')
  57. #define DNS_ADBFETCH_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH_MAGIC)
  58. #define DNS_ADBFETCH6_MAGIC ISC_MAGIC('a', 'd', 'F', '6')
  59. #define DNS_ADBFETCH6_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH6_MAGIC)
  60. /*!
  61. * For type 3 negative cache entries, we will remember that the address is
  62. * broken for this long. XXXMLG This is also used for actual addresses, too.
  63. * The intent is to keep us from constantly asking about A/AAAA records
  64. * if the zone has extremely low TTLs.
  65. */
  66. #define ADB_CACHE_MINIMUM 10 /*%< seconds */
  67. #define ADB_CACHE_MAXIMUM 86400 /*%< seconds (86400 = 24 hours) */
  68. #define ADB_ENTRY_WINDOW 1800 /*%< seconds */
  69. /*%
  70. * The period in seconds after which an ADB name entry is regarded as stale
  71. * and forced to be cleaned up.
  72. * TODO: This should probably be configurable at run-time.
  73. */
  74. #ifndef ADB_STALE_MARGIN
  75. #define ADB_STALE_MARGIN 1800
  76. #endif
  77. #define FREE_ITEMS 64 /*%< free count for memory pools */
  78. #define FILL_COUNT 16 /*%< fill count for memory pools */
  79. #define DNS_ADB_INVALIDBUCKET (-1) /*%< invalid bucket address */
  80. #define DNS_ADB_MINADBSIZE (1024*1024) /*%< 1 Megabyte */
  81. typedef ISC_LIST(dns_adbname_t) dns_adbnamelist_t;
  82. typedef struct dns_adbnamehook dns_adbnamehook_t;
  83. typedef ISC_LIST(dns_adbnamehook_t) dns_adbnamehooklist_t;
  84. typedef struct dns_adblameinfo dns_adblameinfo_t;
  85. typedef ISC_LIST(dns_adbentry_t) dns_adbentrylist_t;
  86. typedef struct dns_adbfetch dns_adbfetch_t;
  87. typedef struct dns_adbfetch6 dns_adbfetch6_t;
  88. /*% dns adb structure */
  89. struct dns_adb {
  90. unsigned int magic;
  91. isc_mutex_t lock;
  92. isc_mutex_t reflock; /*%< Covers irefcnt, erefcnt */
  93. isc_mutex_t overmemlock; /*%< Covers overmem */
  94. isc_mem_t *mctx;
  95. dns_view_t *view;
  96. isc_taskmgr_t *taskmgr;
  97. isc_task_t *task;
  98. isc_interval_t tick_interval;
  99. int next_cleanbucket;
  100. unsigned int irefcnt;
  101. unsigned int erefcnt;
  102. isc_mutex_t mplock;
  103. isc_mempool_t *nmp; /*%< dns_adbname_t */
  104. isc_mempool_t *nhmp; /*%< dns_adbnamehook_t */
  105. isc_mempool_t *limp; /*%< dns_adblameinfo_t */
  106. isc_mempool_t *emp; /*%< dns_adbentry_t */
  107. isc_mempool_t *ahmp; /*%< dns_adbfind_t */
  108. isc_mempool_t *aimp; /*%< dns_adbaddrinfo_t */
  109. isc_mempool_t *afmp; /*%< dns_adbfetch_t */
  110. /*!
  111. * Bucketized locks and lists for names.
  112. *
  113. * XXXRTH Have a per-bucket structure that contains all of these?
  114. */
  115. unsigned int nnames;
  116. isc_mutex_t namescntlock;
  117. unsigned int namescnt;
  118. dns_adbnamelist_t *names;
  119. dns_adbnamelist_t *deadnames;
  120. isc_mutex_t *namelocks;
  121. isc_boolean_t *name_sd;
  122. unsigned int *name_refcnt;
  123. /*!
  124. * Bucketized locks and lists for entries.
  125. *
  126. * XXXRTH Have a per-bucket structure that contains all of these?
  127. */
  128. unsigned int nentries;
  129. isc_mutex_t entriescntlock;
  130. unsigned int entriescnt;
  131. dns_adbentrylist_t *entries;
  132. dns_adbentrylist_t *deadentries;
  133. isc_mutex_t *entrylocks;
  134. isc_boolean_t *entry_sd; /*%< shutting down */
  135. unsigned int *entry_refcnt;
  136. isc_event_t cevent;
  137. isc_boolean_t cevent_sent;
  138. isc_boolean_t shutting_down;
  139. isc_eventlist_t whenshutdown;
  140. isc_event_t growentries;
  141. isc_boolean_t growentries_sent;
  142. isc_event_t grownames;
  143. isc_boolean_t grownames_sent;
  144. };
  145. /*
  146. * XXXMLG Document these structures.
  147. */
  148. /*% dns_adbname structure */
  149. struct dns_adbname {
  150. unsigned int magic;
  151. dns_name_t name;
  152. dns_adb_t *adb;
  153. unsigned int partial_result;
  154. unsigned int flags;
  155. int lock_bucket;
  156. dns_name_t target;
  157. isc_stdtime_t expire_target;
  158. isc_stdtime_t expire_v4;
  159. isc_stdtime_t expire_v6;
  160. unsigned int chains;
  161. dns_adbnamehooklist_t v4;
  162. dns_adbnamehooklist_t v6;
  163. dns_adbfetch_t *fetch_a;
  164. dns_adbfetch_t *fetch_aaaa;
  165. unsigned int fetch_err;
  166. unsigned int fetch6_err;
  167. dns_adbfindlist_t finds;
  168. /* for LRU-based management */
  169. isc_stdtime_t last_used;
  170. ISC_LINK(dns_adbname_t) plink;
  171. };
  172. /*% The adbfetch structure */
  173. struct dns_adbfetch {
  174. unsigned int magic;
  175. dns_fetch_t *fetch;
  176. dns_rdataset_t rdataset;
  177. };
  178. /*%
  179. * This is a small widget that dangles off a dns_adbname_t. It contains a
  180. * pointer to the address information about this host, and a link to the next
  181. * namehook that will contain the next address this host has.
  182. */
  183. struct dns_adbnamehook {
  184. unsigned int magic;
  185. dns_adbentry_t *entry;
  186. ISC_LINK(dns_adbnamehook_t) plink;
  187. };
  188. /*%
  189. * This is a small widget that holds qname-specific information about an
  190. * address. Currently limited to lameness, but could just as easily be
  191. * extended to other types of information about zones.
  192. */
  193. struct dns_adblameinfo {
  194. unsigned int magic;
  195. dns_name_t qname;
  196. dns_rdatatype_t qtype;
  197. isc_stdtime_t lame_timer;
  198. ISC_LINK(dns_adblameinfo_t) plink;
  199. };
  200. /*%
  201. * An address entry. It holds quite a bit of information about addresses,
  202. * including edns state (in "flags"), rtt, and of course the address of
  203. * the host.
  204. */
  205. struct dns_adbentry {
  206. unsigned int magic;
  207. int lock_bucket;
  208. unsigned int refcnt;
  209. unsigned int flags;
  210. unsigned int srtt;
  211. isc_sockaddr_t sockaddr;
  212. isc_stdtime_t expires;
  213. /*%<
  214. * A nonzero 'expires' field indicates that the entry should
  215. * persist until that time. This allows entries found
  216. * using dns_adb_findaddrinfo() to persist for a limited time
  217. * even though they are not necessarily associated with a
  218. * name.
  219. */
  220. ISC_LIST(dns_adblameinfo_t) lameinfo;
  221. ISC_LINK(dns_adbentry_t) plink;
  222. };
  223. /*
  224. * Internal functions (and prototypes).
  225. */
  226. static inline dns_adbname_t *new_adbname(dns_adb_t *, dns_name_t *);
  227. static inline void free_adbname(dns_adb_t *, dns_adbname_t **);
  228. static inline dns_adbnamehook_t *new_adbnamehook(dns_adb_t *,
  229. dns_adbentry_t *);
  230. static inline void free_adbnamehook(dns_adb_t *, dns_adbnamehook_t **);
  231. static inline dns_adblameinfo_t *new_adblameinfo(dns_adb_t *, dns_name_t *,
  232. dns_rdatatype_t);
  233. static inline void free_adblameinfo(dns_adb_t *, dns_adblameinfo_t **);
  234. static inline dns_adbentry_t *new_adbentry(dns_adb_t *);
  235. static inline void free_adbentry(dns_adb_t *, dns_adbentry_t **);
  236. static inline dns_adbfind_t *new_adbfind(dns_adb_t *);
  237. static inline isc_boolean_t free_adbfind(dns_adb_t *, dns_adbfind_t **);
  238. static inline dns_adbaddrinfo_t *new_adbaddrinfo(dns_adb_t *, dns_adbentry_t *,
  239. in_port_t);
  240. static inline dns_adbfetch_t *new_adbfetch(dns_adb_t *);
  241. static inline void free_adbfetch(dns_adb_t *, dns_adbfetch_t **);
  242. static inline dns_adbname_t *find_name_and_lock(dns_adb_t *, dns_name_t *,
  243. unsigned int, int *);
  244. static inline dns_adbentry_t *find_entry_and_lock(dns_adb_t *,
  245. isc_sockaddr_t *, int *,
  246. isc_stdtime_t);
  247. static void dump_adb(dns_adb_t *, FILE *, isc_boolean_t debug, isc_stdtime_t);
  248. static void print_dns_name(FILE *, dns_name_t *);
  249. static void print_namehook_list(FILE *, const char *legend,
  250. dns_adbnamehooklist_t *list,
  251. isc_boolean_t debug,
  252. isc_stdtime_t now);
  253. static void print_find_list(FILE *, dns_adbname_t *);
  254. static void print_fetch_list(FILE *, dns_adbname_t *);
  255. static inline isc_boolean_t dec_adb_irefcnt(dns_adb_t *);
  256. static inline void inc_adb_irefcnt(dns_adb_t *);
  257. static inline void inc_adb_erefcnt(dns_adb_t *);
  258. static inline void inc_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
  259. isc_boolean_t);
  260. static inline isc_boolean_t dec_entry_refcnt(dns_adb_t *, isc_boolean_t,
  261. dns_adbentry_t *, isc_boolean_t);
  262. static inline void violate_locking_hierarchy(isc_mutex_t *, isc_mutex_t *);
  263. static isc_boolean_t clean_namehooks(dns_adb_t *, dns_adbnamehooklist_t *);
  264. static void clean_target(dns_adb_t *, dns_name_t *);
  265. static void clean_finds_at_name(dns_adbname_t *, isc_eventtype_t,
  266. unsigned int);
  267. static isc_boolean_t check_expire_namehooks(dns_adbname_t *, isc_stdtime_t);
  268. static isc_boolean_t check_expire_entry(dns_adb_t *, dns_adbentry_t **,
  269. isc_stdtime_t);
  270. static void cancel_fetches_at_name(dns_adbname_t *);
  271. static isc_result_t dbfind_name(dns_adbname_t *, isc_stdtime_t,
  272. dns_rdatatype_t);
  273. static isc_result_t fetch_name(dns_adbname_t *, isc_boolean_t,
  274. dns_rdatatype_t);
  275. static inline void check_exit(dns_adb_t *);
  276. static void destroy(dns_adb_t *);
  277. static isc_boolean_t shutdown_names(dns_adb_t *);
  278. static isc_boolean_t shutdown_entries(dns_adb_t *);
  279. static inline void link_name(dns_adb_t *, int, dns_adbname_t *);
  280. static inline isc_boolean_t unlink_name(dns_adb_t *, dns_adbname_t *);
  281. static inline void link_entry(dns_adb_t *, int, dns_adbentry_t *);
  282. static inline isc_boolean_t unlink_entry(dns_adb_t *, dns_adbentry_t *);
  283. static isc_boolean_t kill_name(dns_adbname_t **, isc_eventtype_t);
  284. static void water(void *, int);
  285. static void dump_entry(FILE *, dns_adbentry_t *, isc_boolean_t, isc_stdtime_t);
  286. /*
  287. * MUST NOT overlap DNS_ADBFIND_* flags!
  288. */
  289. #define FIND_EVENT_SENT 0x40000000
  290. #define FIND_EVENT_FREED 0x80000000
  291. #define FIND_EVENTSENT(h) (((h)->flags & FIND_EVENT_SENT) != 0)
  292. #define FIND_EVENTFREED(h) (((h)->flags & FIND_EVENT_FREED) != 0)
  293. #define NAME_NEEDS_POKE 0x80000000
  294. #define NAME_IS_DEAD 0x40000000
  295. #define NAME_HINT_OK DNS_ADBFIND_HINTOK
  296. #define NAME_GLUE_OK DNS_ADBFIND_GLUEOK
  297. #define NAME_STARTATZONE DNS_ADBFIND_STARTATZONE
  298. #define NAME_DEAD(n) (((n)->flags & NAME_IS_DEAD) != 0)
  299. #define NAME_NEEDSPOKE(n) (((n)->flags & NAME_NEEDS_POKE) != 0)
  300. #define NAME_GLUEOK(n) (((n)->flags & NAME_GLUE_OK) != 0)
  301. #define NAME_HINTOK(n) (((n)->flags & NAME_HINT_OK) != 0)
  302. /*
  303. * Private flag(s) for entries.
  304. * MUST NOT overlap FCTX_ADDRINFO_xxx and DNS_FETCHOPT_NOEDNS0.
  305. */
  306. #define ENTRY_IS_DEAD 0x80000000
  307. /*
  308. * To the name, address classes are all that really exist. If it has a
  309. * V6 address it doesn't care if it came from a AAAA query.
  310. */
  311. #define NAME_HAS_V4(n) (!ISC_LIST_EMPTY((n)->v4))
  312. #define NAME_HAS_V6(n) (!ISC_LIST_EMPTY((n)->v6))
  313. #define NAME_HAS_ADDRS(n) (NAME_HAS_V4(n) || NAME_HAS_V6(n))
  314. /*
  315. * Fetches are broken out into A and AAAA types. In some cases,
  316. * however, it makes more sense to test for a particular class of fetches,
  317. * like V4 or V6 above.
  318. * Note: since we have removed the support of A6 in adb, FETCH_A and FETCH_AAAA
  319. * are now equal to FETCH_V4 and FETCH_V6, respectively.
  320. */
  321. #define NAME_FETCH_A(n) ((n)->fetch_a != NULL)
  322. #define NAME_FETCH_AAAA(n) ((n)->fetch_aaaa != NULL)
  323. #define NAME_FETCH_V4(n) (NAME_FETCH_A(n))
  324. #define NAME_FETCH_V6(n) (NAME_FETCH_AAAA(n))
  325. #define NAME_FETCH(n) (NAME_FETCH_V4(n) || NAME_FETCH_V6(n))
  326. /*
  327. * Find options and tests to see if there are addresses on the list.
  328. */
  329. #define FIND_WANTEVENT(fn) (((fn)->options & DNS_ADBFIND_WANTEVENT) != 0)
  330. #define FIND_WANTEMPTYEVENT(fn) (((fn)->options & DNS_ADBFIND_EMPTYEVENT) != 0)
  331. #define FIND_AVOIDFETCHES(fn) (((fn)->options & DNS_ADBFIND_AVOIDFETCHES) \
  332. != 0)
  333. #define FIND_STARTATZONE(fn) (((fn)->options & DNS_ADBFIND_STARTATZONE) \
  334. != 0)
  335. #define FIND_HINTOK(fn) (((fn)->options & DNS_ADBFIND_HINTOK) != 0)
  336. #define FIND_GLUEOK(fn) (((fn)->options & DNS_ADBFIND_GLUEOK) != 0)
  337. #define FIND_HAS_ADDRS(fn) (!ISC_LIST_EMPTY((fn)->list))
  338. #define FIND_RETURNLAME(fn) (((fn)->options & DNS_ADBFIND_RETURNLAME) != 0)
  339. /*
  340. * These are currently used on simple unsigned ints, so they are
  341. * not really associated with any particular type.
  342. */
  343. #define WANT_INET(x) (((x) & DNS_ADBFIND_INET) != 0)
  344. #define WANT_INET6(x) (((x) & DNS_ADBFIND_INET6) != 0)
  345. #define EXPIRE_OK(exp, now) ((exp == INT_MAX) || (exp < now))
  346. /*
  347. * Find out if the flags on a name (nf) indicate if it is a hint or
  348. * glue, and compare this to the appropriate bits set in o, to see if
  349. * this is ok.
  350. */
  351. #define GLUE_OK(nf, o) (!NAME_GLUEOK(nf) || (((o) & DNS_ADBFIND_GLUEOK) != 0))
  352. #define HINT_OK(nf, o) (!NAME_HINTOK(nf) || (((o) & DNS_ADBFIND_HINTOK) != 0))
  353. #define GLUEHINT_OK(nf, o) (GLUE_OK(nf, o) || HINT_OK(nf, o))
  354. #define STARTATZONE_MATCHES(nf, o) (((nf)->flags & NAME_STARTATZONE) == \
  355. ((o) & DNS_ADBFIND_STARTATZONE))
  356. #define ENTER_LEVEL ISC_LOG_DEBUG(50)
  357. #define EXIT_LEVEL ENTER_LEVEL
  358. #define CLEAN_LEVEL ISC_LOG_DEBUG(100)
  359. #define DEF_LEVEL ISC_LOG_DEBUG(5)
  360. #define NCACHE_LEVEL ISC_LOG_DEBUG(20)
  361. #define NCACHE_RESULT(r) ((r) == DNS_R_NCACHENXDOMAIN || \
  362. (r) == DNS_R_NCACHENXRRSET)
  363. #define AUTH_NX(r) ((r) == DNS_R_NXDOMAIN || \
  364. (r) == DNS_R_NXRRSET)
  365. #define NXDOMAIN_RESULT(r) ((r) == DNS_R_NXDOMAIN || \
  366. (r) == DNS_R_NCACHENXDOMAIN)
  367. #define NXRRSET_RESULT(r) ((r) == DNS_R_NCACHENXRRSET || \
  368. (r) == DNS_R_NXRRSET || \
  369. (r) == DNS_R_HINTNXRRSET)
  370. /*
  371. * Error state rankings.
  372. */
  373. #define FIND_ERR_SUCCESS 0 /* highest rank */
  374. #define FIND_ERR_CANCELED 1
  375. #define FIND_ERR_FAILURE 2
  376. #define FIND_ERR_NXDOMAIN 3
  377. #define FIND_ERR_NXRRSET 4
  378. #define FIND_ERR_UNEXPECTED 5
  379. #define FIND_ERR_NOTFOUND 6
  380. #define FIND_ERR_MAX 7
  381. static const char *errnames[] = {
  382. "success",
  383. "canceled",
  384. "failure",
  385. "nxdomain",
  386. "nxrrset",
  387. "unexpected",
  388. "not_found"
  389. };
  390. #define NEWERR(old, new) (ISC_MIN((old), (new)))
  391. static isc_result_t find_err_map[FIND_ERR_MAX] = {
  392. ISC_R_SUCCESS,
  393. ISC_R_CANCELED,
  394. ISC_R_FAILURE,
  395. DNS_R_NXDOMAIN,
  396. DNS_R_NXRRSET,
  397. ISC_R_UNEXPECTED,
  398. ISC_R_NOTFOUND /* not YET found */
  399. };
  400. static void
  401. DP(int level, const char *format, ...) ISC_FORMAT_PRINTF(2, 3);
  402. static void
  403. DP(int level, const char *format, ...) {
  404. va_list args;
  405. va_start(args, format);
  406. isc_log_vwrite(dns_lctx,
  407. DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_ADB,
  408. level, format, args);
  409. va_end(args);
  410. }
  411. /*%
  412. * Increment resolver-related statistics counters.
  413. */
  414. static inline void
  415. inc_stats(dns_adb_t *adb, isc_statscounter_t counter) {
  416. if (adb->view->resstats != NULL)
  417. isc_stats_increment(adb->view->resstats, counter);
  418. }
  419. static inline dns_ttl_t
  420. ttlclamp(dns_ttl_t ttl) {
  421. if (ttl < ADB_CACHE_MINIMUM)
  422. ttl = ADB_CACHE_MINIMUM;
  423. if (ttl > ADB_CACHE_MAXIMUM)
  424. ttl = ADB_CACHE_MAXIMUM;
  425. return (ttl);
  426. }
  427. /*
  428. * Hashing is most efficient if the number of buckets is prime.
  429. * The sequence below is the closest previous primes to 2^n and
  430. * 1.5 * 2^n, for values of n from 10 to 28. (The tables will
  431. * no longer grow beyond 2^28 entries.)
  432. */
  433. static const unsigned nbuckets[] = { 1021, 1531, 2039, 3067, 4093, 6143,
  434. 8191, 12281, 16381, 24571, 32749,
  435. 49193, 65521, 98299, 131071, 199603,
  436. 262139, 393209, 524287, 768431, 1048573,
  437. 1572853, 2097143, 3145721, 4194301,
  438. 6291449, 8388593, 12582893, 16777213,
  439. 25165813, 33554393, 50331599, 67108859,
  440. 100663291, 134217689, 201326557,
  441. 268535431, 0 };
  442. static void
  443. grow_entries(isc_task_t *task, isc_event_t *ev) {
  444. dns_adb_t *adb;
  445. dns_adbentry_t *e;
  446. dns_adbentrylist_t *newdeadentries = NULL;
  447. dns_adbentrylist_t *newentries = NULL;
  448. isc_boolean_t *newentry_sd = NULL;
  449. isc_mutex_t *newentrylocks = NULL;
  450. isc_result_t result;
  451. unsigned int *newentry_refcnt = NULL;
  452. unsigned int i, n, bucket;
  453. adb = ev->ev_arg;
  454. INSIST(DNS_ADB_VALID(adb));
  455. isc_event_free(&ev);
  456. isc_task_beginexclusive(task);
  457. i = 0;
  458. while (nbuckets[i] != 0 && adb->nentries >= nbuckets[i])
  459. i++;
  460. if (nbuckets[i] != 0)
  461. n = nbuckets[i];
  462. else
  463. goto done;
  464. DP(ISC_LOG_INFO, "adb: grow_entries to %u starting", n);
  465. /*
  466. * Are we shutting down?
  467. */
  468. for (i = 0; i < adb->nentries; i++)
  469. if (adb->entry_sd[i])
  470. goto cleanup;
  471. /*
  472. * Grab all the resources we need.
  473. */
  474. newentries = isc_mem_get(adb->mctx, sizeof(*newentries) * n);
  475. newdeadentries = isc_mem_get(adb->mctx, sizeof(*newdeadentries) * n);
  476. newentrylocks = isc_mem_get(adb->mctx, sizeof(*newentrylocks) * n);
  477. newentry_sd = isc_mem_get(adb->mctx, sizeof(*newentry_sd) * n);
  478. newentry_refcnt = isc_mem_get(adb->mctx, sizeof(*newentry_refcnt) * n);
  479. if (newentries == NULL || newdeadentries == NULL ||
  480. newentrylocks == NULL || newentry_sd == NULL ||
  481. newentry_refcnt == NULL)
  482. goto cleanup;
  483. /*
  484. * Initialise the new resources.
  485. */
  486. result = isc_mutexblock_init(newentrylocks, n);
  487. if (result != ISC_R_SUCCESS)
  488. goto cleanup;
  489. for (i = 0; i < n; i++) {
  490. ISC_LIST_INIT(newentries[i]);
  491. ISC_LIST_INIT(newdeadentries[i]);
  492. newentry_sd[i] = ISC_FALSE;
  493. newentry_refcnt[i] = 0;
  494. adb->irefcnt++;
  495. }
  496. /*
  497. * Move entries to new arrays.
  498. */
  499. for (i = 0; i < adb->nentries; i++) {
  500. e = ISC_LIST_HEAD(adb->entries[i]);
  501. while (e != NULL) {
  502. ISC_LIST_UNLINK(adb->entries[i], e, plink);
  503. bucket = isc_sockaddr_hash(&e->sockaddr, ISC_TRUE) % n;
  504. e->lock_bucket = bucket;
  505. ISC_LIST_APPEND(newentries[bucket], e, plink);
  506. INSIST(adb->entry_refcnt[i] > 0);
  507. adb->entry_refcnt[i]--;
  508. newentry_refcnt[bucket]++;
  509. e = ISC_LIST_HEAD(adb->entries[i]);
  510. }
  511. e = ISC_LIST_HEAD(adb->deadentries[i]);
  512. while (e != NULL) {
  513. ISC_LIST_UNLINK(adb->deadentries[i], e, plink);
  514. bucket = isc_sockaddr_hash(&e->sockaddr, ISC_TRUE) % n;
  515. e->lock_bucket = bucket;
  516. ISC_LIST_APPEND(newdeadentries[bucket], e, plink);
  517. INSIST(adb->entry_refcnt[i] > 0);
  518. adb->entry_refcnt[i]--;
  519. newentry_refcnt[bucket]++;
  520. e = ISC_LIST_HEAD(adb->deadentries[i]);
  521. }
  522. INSIST(adb->entry_refcnt[i] == 0);
  523. adb->irefcnt--;
  524. }
  525. /*
  526. * Cleanup old resources.
  527. */
  528. DESTROYMUTEXBLOCK(adb->entrylocks, adb->nentries);
  529. isc_mem_put(adb->mctx, adb->entries,
  530. sizeof(*adb->entries) * adb->nentries);
  531. isc_mem_put(adb->mctx, adb->deadentries,
  532. sizeof(*adb->deadentries) * adb->nentries);
  533. isc_mem_put(adb->mctx, adb->entrylocks,
  534. sizeof(*adb->entrylocks) * adb->nentries);
  535. isc_mem_put(adb->mctx, adb->entry_sd,
  536. sizeof(*adb->entry_sd) * adb->nentries);
  537. isc_mem_put(adb->mctx, adb->entry_refcnt,
  538. sizeof(*adb->entry_refcnt) * adb->nentries);
  539. /*
  540. * Install new resources.
  541. */
  542. adb->entries = newentries;
  543. adb->deadentries = newdeadentries;
  544. adb->entrylocks = newentrylocks;
  545. adb->entry_sd = newentry_sd;
  546. adb->entry_refcnt = newentry_refcnt;
  547. adb->nentries = n;
  548. /*
  549. * Only on success do we set adb->growentries_sent to ISC_FALSE.
  550. * This will prevent us being continuously being called on error.
  551. */
  552. adb->growentries_sent = ISC_FALSE;
  553. goto done;
  554. cleanup:
  555. if (newentries != NULL)
  556. isc_mem_put(adb->mctx, newentries,
  557. sizeof(*newentries) * n);
  558. if (newdeadentries != NULL)
  559. isc_mem_put(adb->mctx, newdeadentries,
  560. sizeof(*newdeadentries) * n);
  561. if (newentrylocks != NULL)
  562. isc_mem_put(adb->mctx, newentrylocks,
  563. sizeof(*newentrylocks) * n);
  564. if (newentry_sd != NULL)
  565. isc_mem_put(adb->mctx, newentry_sd,
  566. sizeof(*newentry_sd) * n);
  567. if (newentry_refcnt != NULL)
  568. isc_mem_put(adb->mctx, newentry_refcnt,
  569. sizeof(*newentry_refcnt) * n);
  570. done:
  571. isc_task_endexclusive(task);
  572. LOCK(&adb->lock);
  573. if (dec_adb_irefcnt(adb))
  574. check_exit(adb);
  575. UNLOCK(&adb->lock);
  576. DP(ISC_LOG_INFO, "adb: grow_entries finished");
  577. }
  578. static void
  579. grow_names(isc_task_t *task, isc_event_t *ev) {
  580. dns_adb_t *adb;
  581. dns_adbname_t *name;
  582. dns_adbnamelist_t *newdeadnames = NULL;
  583. dns_adbnamelist_t *newnames = NULL;
  584. isc_boolean_t *newname_sd = NULL;
  585. isc_mutex_t *newnamelocks = NULL;
  586. isc_result_t result;
  587. unsigned int *newname_refcnt = NULL;
  588. unsigned int i, n, bucket;
  589. adb = ev->ev_arg;
  590. INSIST(DNS_ADB_VALID(adb));
  591. isc_event_free(&ev);
  592. isc_task_beginexclusive(task);
  593. i = 0;
  594. while (nbuckets[i] != 0 && adb->nnames >= nbuckets[i])
  595. i++;
  596. if (nbuckets[i] != 0)
  597. n = nbuckets[i];
  598. else
  599. goto done;
  600. DP(ISC_LOG_INFO, "adb: grow_names to %u starting", n);
  601. /*
  602. * Are we shutting down?
  603. */
  604. for (i = 0; i < adb->nnames; i++)
  605. if (adb->name_sd[i])
  606. goto cleanup;
  607. /*
  608. * Grab all the resources we need.
  609. */
  610. newnames = isc_mem_get(adb->mctx, sizeof(*newnames) * n);
  611. newdeadnames = isc_mem_get(adb->mctx, sizeof(*newdeadnames) * n);
  612. newnamelocks = isc_mem_get(adb->mctx, sizeof(*newnamelocks) * n);
  613. newname_sd = isc_mem_get(adb->mctx, sizeof(*newname_sd) * n);
  614. newname_refcnt = isc_mem_get(adb->mctx, sizeof(*newname_refcnt) * n);
  615. if (newnames == NULL || newdeadnames == NULL ||
  616. newnamelocks == NULL || newname_sd == NULL ||
  617. newname_refcnt == NULL)
  618. goto cleanup;
  619. /*
  620. * Initialise the new resources.
  621. */
  622. result = isc_mutexblock_init(newnamelocks, n);
  623. if (result != ISC_R_SUCCESS)
  624. goto cleanup;
  625. for (i = 0; i < n; i++) {
  626. ISC_LIST_INIT(newnames[i]);
  627. ISC_LIST_INIT(newdeadnames[i]);
  628. newname_sd[i] = ISC_FALSE;
  629. newname_refcnt[i] = 0;
  630. adb->irefcnt++;
  631. }
  632. /*
  633. * Move names to new arrays.
  634. */
  635. for (i = 0; i < adb->nnames; i++) {
  636. name = ISC_LIST_HEAD(adb->names[i]);
  637. while (name != NULL) {
  638. ISC_LIST_UNLINK(adb->names[i], name, plink);
  639. bucket = dns_name_fullhash(&name->name, ISC_TRUE) % n;
  640. name->lock_bucket = bucket;
  641. ISC_LIST_APPEND(newnames[bucket], name, plink);
  642. INSIST(adb->name_refcnt[i] > 0);
  643. adb->name_refcnt[i]--;
  644. newname_refcnt[bucket]++;
  645. name = ISC_LIST_HEAD(adb->names[i]);
  646. }
  647. name = ISC_LIST_HEAD(adb->deadnames[i]);
  648. while (name != NULL) {
  649. ISC_LIST_UNLINK(adb->deadnames[i], name, plink);
  650. bucket = dns_name_fullhash(&name->name, ISC_TRUE) % n;
  651. name->lock_bucket = bucket;
  652. ISC_LIST_APPEND(newdeadnames[bucket], name, plink);
  653. INSIST(adb->name_refcnt[i] > 0);
  654. adb->name_refcnt[i]--;
  655. newname_refcnt[bucket]++;
  656. name = ISC_LIST_HEAD(adb->deadnames[i]);
  657. }
  658. INSIST(adb->name_refcnt[i] == 0);
  659. adb->irefcnt--;
  660. }
  661. /*
  662. * Cleanup old resources.
  663. */
  664. DESTROYMUTEXBLOCK(adb->namelocks, adb->nnames);
  665. isc_mem_put(adb->mctx, adb->names,
  666. sizeof(*adb->names) * adb->nnames);
  667. isc_mem_put(adb->mctx, adb->deadnames,
  668. sizeof(*adb->deadnames) * adb->nnames);
  669. isc_mem_put(adb->mctx, adb->namelocks,
  670. sizeof(*adb->namelocks) * adb->nnames);
  671. isc_mem_put(adb->mctx, adb->name_sd,
  672. sizeof(*adb->name_sd) * adb->nnames);
  673. isc_mem_put(adb->mctx, adb->name_refcnt,
  674. sizeof(*adb->name_refcnt) * adb->nnames);
  675. /*
  676. * Install new resources.
  677. */
  678. adb->names = newnames;
  679. adb->deadnames = newdeadnames;
  680. adb->namelocks = newnamelocks;
  681. adb->name_sd = newname_sd;
  682. adb->name_refcnt = newname_refcnt;
  683. adb->nnames = n;
  684. /*
  685. * Only on success do we set adb->grownames_sent to ISC_FALSE.
  686. * This will prevent us being continuously being called on error.
  687. */
  688. adb->grownames_sent = ISC_FALSE;
  689. goto done;
  690. cleanup:
  691. if (newnames != NULL)
  692. isc_mem_put(adb->mctx, newnames, sizeof(*newnames) * n);
  693. if (newdeadnames != NULL)
  694. isc_mem_put(adb->mctx, newdeadnames, sizeof(*newdeadnames) * n);
  695. if (newnamelocks != NULL)
  696. isc_mem_put(adb->mctx, newnamelocks, sizeof(*newnamelocks) * n);
  697. if (newname_sd != NULL)
  698. isc_mem_put(adb->mctx, newname_sd, sizeof(*newname_sd) * n);
  699. if (newname_refcnt != NULL)
  700. isc_mem_put(adb->mctx, newname_refcnt,
  701. sizeof(*newname_refcnt) * n);
  702. done:
  703. isc_task_endexclusive(task);
  704. LOCK(&adb->lock);
  705. if (dec_adb_irefcnt(adb))
  706. check_exit(adb);
  707. UNLOCK(&adb->lock);
  708. DP(ISC_LOG_INFO, "adb: grow_names finished");
  709. }
  710. /*
  711. * Requires the adbname bucket be locked and that no entry buckets be locked.
  712. *
  713. * This code handles A and AAAA rdatasets only.
  714. */
  715. static isc_result_t
  716. import_rdataset(dns_adbname_t *adbname, dns_rdataset_t *rdataset,
  717. isc_stdtime_t now)
  718. {
  719. isc_result_t result;
  720. dns_adb_t *adb;
  721. dns_adbnamehook_t *nh;
  722. dns_adbnamehook_t *anh;
  723. dns_rdata_t rdata = DNS_RDATA_INIT;
  724. struct in_addr ina;
  725. struct in6_addr in6a;
  726. isc_sockaddr_t sockaddr;
  727. dns_adbentry_t *foundentry; /* NO CLEAN UP! */
  728. int addr_bucket;
  729. isc_boolean_t new_addresses_added;
  730. dns_rdatatype_t rdtype;
  731. unsigned int findoptions;
  732. dns_adbnamehooklist_t *hookhead;
  733. INSIST(DNS_ADBNAME_VALID(adbname));
  734. adb = adbname->adb;
  735. INSIST(DNS_ADB_VALID(adb));
  736. rdtype = rdataset->type;
  737. INSIST((rdtype == dns_rdatatype_a) || (rdtype == dns_rdatatype_aaaa));
  738. if (rdtype == dns_rdatatype_a)
  739. findoptions = DNS_ADBFIND_INET;
  740. else
  741. findoptions = DNS_ADBFIND_INET6;
  742. addr_bucket = DNS_ADB_INVALIDBUCKET;
  743. new_addresses_added = ISC_FALSE;
  744. nh = NULL;
  745. result = dns_rdataset_first(rdataset);
  746. while (result == ISC_R_SUCCESS) {
  747. dns_rdata_reset(&rdata);
  748. dns_rdataset_current(rdataset, &rdata);
  749. if (rdtype == dns_rdatatype_a) {
  750. INSIST(rdata.length == 4);
  751. memcpy(&ina.s_addr, rdata.data, 4);
  752. isc_sockaddr_fromin(&sockaddr, &ina, 0);
  753. hookhead = &adbname->v4;
  754. } else {
  755. INSIST(rdata.length == 16);
  756. memcpy(in6a.s6_addr, rdata.data, 16);
  757. isc_sockaddr_fromin6(&sockaddr, &in6a, 0);
  758. hookhead = &adbname->v6;
  759. }
  760. INSIST(nh == NULL);
  761. nh = new_adbnamehook(adb, NULL);
  762. if (nh == NULL) {
  763. adbname->partial_result |= findoptions;
  764. result = ISC_R_NOMEMORY;
  765. goto fail;
  766. }
  767. foundentry = find_entry_and_lock(adb, &sockaddr, &addr_bucket,
  768. now);
  769. if (foundentry == NULL) {
  770. dns_adbentry_t *entry;
  771. entry = new_adbentry(adb);
  772. if (entry == NULL) {
  773. adbname->partial_result |= findoptions;
  774. result = ISC_R_NOMEMORY;
  775. goto fail;
  776. }
  777. entry->sockaddr = sockaddr;
  778. entry->refcnt = 1;
  779. nh->entry = entry;
  780. link_entry(adb, addr_bucket, entry);
  781. } else {
  782. for (anh = ISC_LIST_HEAD(*hookhead);
  783. anh != NULL;
  784. anh = ISC_LIST_NEXT(anh, plink))
  785. if (anh->entry == foundentry)
  786. break;
  787. if (anh == NULL) {
  788. foundentry->refcnt++;
  789. nh->entry = foundentry;
  790. } else
  791. free_adbnamehook(adb, &nh);
  792. }
  793. new_addresses_added = ISC_TRUE;
  794. if (nh != NULL)
  795. ISC_LIST_APPEND(*hookhead, nh, plink);
  796. nh = NULL;
  797. result = dns_rdataset_next(rdataset);
  798. }
  799. fail:
  800. if (nh != NULL)
  801. free_adbnamehook(adb, &nh);
  802. if (addr_bucket != DNS_ADB_INVALIDBUCKET)
  803. UNLOCK(&adb->entrylocks[addr_bucket]);
  804. if (rdataset->trust == dns_trust_glue ||
  805. rdataset->trust == dns_trust_additional)
  806. rdataset->ttl = ADB_CACHE_MINIMUM;
  807. else if (rdataset->trust == dns_trust_ultimate)
  808. rdataset->ttl = 0;
  809. else
  810. rdataset->ttl = ttlclamp(rdataset->ttl);
  811. if (rdtype == dns_rdatatype_a) {
  812. DP(NCACHE_LEVEL, "expire_v4 set to MIN(%u,%u) import_rdataset",
  813. adbname->expire_v4, now + rdataset->ttl);
  814. adbname->expire_v4 = ISC_MIN(adbname->expire_v4,
  815. now + rdataset->ttl);
  816. } else {
  817. DP(NCACHE_LEVEL, "expire_v6 set to MIN(%u,%u) import_rdataset",
  818. adbname->expire_v6, now + rdataset->ttl);
  819. adbname->expire_v6 = ISC_MIN(adbname->expire_v6,
  820. now + rdataset->ttl);
  821. }
  822. if (new_addresses_added) {
  823. /*
  824. * Lie a little here. This is more or less so code that cares
  825. * can find out if any new information was added or not.
  826. */
  827. return (ISC_R_SUCCESS);
  828. }
  829. return (result);
  830. }
  831. /*
  832. * Requires the name's bucket be locked.
  833. */
  834. static isc_boolean_t
  835. kill_name(dns_adbname_t **n, isc_eventtype_t ev) {
  836. dns_adbname_t *name;
  837. isc_boolean_t result = ISC_FALSE;
  838. isc_boolean_t result4, result6;
  839. int bucket;
  840. dns_adb_t *adb;
  841. INSIST(n != NULL);
  842. name = *n;
  843. *n = NULL;
  844. INSIST(DNS_ADBNAME_VALID(name));
  845. adb = name->adb;
  846. INSIST(DNS_ADB_VALID(adb));
  847. DP(DEF_LEVEL, "killing name %p", name);
  848. /*
  849. * If we're dead already, just check to see if we should go
  850. * away now or not.
  851. */
  852. if (NAME_DEAD(name) && !NAME_FETCH(name)) {
  853. result = unlink_name(adb, name);
  854. free_adbname(adb, &name);
  855. if (result)
  856. result = dec_adb_irefcnt(adb);
  857. return (result);
  858. }
  859. /*
  860. * Clean up the name's various lists. These two are destructive
  861. * in that they will always empty the list.
  862. */
  863. clean_finds_at_name(name, ev, DNS_ADBFIND_ADDRESSMASK);
  864. result4 = clean_namehooks(adb, &name->v4);
  865. result6 = clean_namehooks(adb, &name->v6);
  866. clean_target(adb, &name->target);
  867. result = ISC_TF(result4 || result6);
  868. /*
  869. * If fetches are running, cancel them. If none are running, we can
  870. * just kill the name here.
  871. */
  872. if (!NAME_FETCH(name)) {
  873. INSIST(result == ISC_FALSE);
  874. result = unlink_name(adb, name);
  875. free_adbname(adb, &name);
  876. if (result)
  877. result = dec_adb_irefcnt(adb);
  878. } else {
  879. cancel_fetches_at_name(name);
  880. if (!NAME_DEAD(name)) {
  881. bucket = name->lock_bucket;
  882. ISC_LIST_UNLINK(adb->names[bucket], name, plink);
  883. ISC_LIST_APPEND(adb->deadnames[bucket], name, plink);
  884. name->flags |= NAME_IS_DEAD;
  885. }
  886. }
  887. return (result);
  888. }
  889. /*
  890. * Requires the name's bucket be locked and no entry buckets be locked.
  891. */
  892. static isc_boolean_t
  893. check_expire_namehooks(dns_adbname_t *name, isc_stdtime_t now) {
  894. dns_adb_t *adb;
  895. isc_boolean_t result4 = ISC_FALSE;
  896. isc_boolean_t result6 = ISC_FALSE;
  897. INSIST(DNS_ADBNAME_VALID(name));
  898. adb = name->adb;
  899. INSIST(DNS_ADB_VALID(adb));
  900. /*
  901. * Check to see if we need to remove the v4 addresses
  902. */
  903. if (!NAME_FETCH_V4(name) && EXPIRE_OK(name->expire_v4, now)) {
  904. if (NAME_HAS_V4(name)) {
  905. DP(DEF_LEVEL, "expiring v4 for name %p", name);
  906. result4 = clean_namehooks(adb, &name->v4);
  907. name->partial_result &= ~DNS_ADBFIND_INET;
  908. }
  909. name->expire_v4 = INT_MAX;
  910. name->fetch_err = FIND_ERR_UNEXPECTED;
  911. }
  912. /*
  913. * Check to see if we need to remove the v6 addresses
  914. */
  915. if (!NAME_FETCH_V6(name) && EXPIRE_OK(name->expire_v6, now)) {
  916. if (NAME_HAS_V6(name)) {
  917. DP(DEF_LEVEL, "expiring v6 for name %p", name);
  918. result6 = clean_namehooks(adb, &name->v6);
  919. name->partial_result &= ~DNS_ADBFIND_INET6;
  920. }
  921. name->expire_v6 = INT_MAX;
  922. name->fetch6_err = FIND_ERR_UNEXPECTED;
  923. }
  924. /*
  925. * Check to see if we need to remove the alias target.
  926. */
  927. if (EXPIRE_OK(name->expire_target, now)) {
  928. clean_target(adb, &name->target);
  929. name->expire_target = INT_MAX;
  930. }
  931. return (ISC_TF(result4 || result6));
  932. }
  933. /*
  934. * Requires the name's bucket be locked.
  935. */
  936. static inline void
  937. link_name(dns_adb_t *adb, int bucket, dns_adbname_t *name) {
  938. INSIST(name->lock_bucket == DNS_ADB_INVALIDBUCKET);
  939. ISC_LIST_PREPEND(adb->names[bucket], name, plink);
  940. name->lock_bucket = bucket;
  941. adb->name_refcnt[bucket]++;
  942. }
  943. /*
  944. * Requires the name's bucket be locked.
  945. */
  946. static inline isc_boolean_t
  947. unlink_name(dns_adb_t *adb, dns_adbname_t *name) {
  948. int bucket;
  949. isc_boolean_t result = ISC_FALSE;
  950. bucket = name->lock_bucket;
  951. INSIST(bucket != DNS_ADB_INVALIDBUCKET);
  952. if (NAME_DEAD(name))
  953. ISC_LIST_UNLINK(adb->deadnames[bucket], name, plink);
  954. else
  955. ISC_LIST_UNLINK(adb->names[bucket], name, plink);
  956. name->lock_bucket = DNS_ADB_INVALIDBUCKET;
  957. INSIST(adb->name_refcnt[bucket] > 0);
  958. adb->name_refcnt[bucket]--;
  959. if (adb->name_sd[bucket] && adb->name_refcnt[bucket] == 0)
  960. result = ISC_TRUE;
  961. return (result);
  962. }
  963. /*
  964. * Requires the entry's bucket be locked.
  965. */
  966. static inline void
  967. link_entry(dns_adb_t *adb, int bucket, dns_adbentry_t *entry) {
  968. int i;
  969. dns_adbentry_t *e;
  970. if (isc_mem_isovermem(adb->mctx)) {
  971. for (i = 0; i < 2; i++) {
  972. e = ISC_LIST_TAIL(adb->entries[bucket]);
  973. if (e == NULL)
  974. break;
  975. if (e->refcnt == 0) {
  976. unlink_entry(adb, e);
  977. free_adbentry(adb, &e);
  978. continue;
  979. }
  980. INSIST((e->flags & ENTRY_IS_DEAD) == 0);
  981. e->flags |= ENTRY_IS_DEAD;
  982. ISC_LIST_UNLINK(adb->entries[bucket], e, plink);
  983. ISC_LIST_PREPEND(adb->deadentries[bucket], e, plink);
  984. }
  985. }
  986. ISC_LIST_PREPEND(adb->entries[bucket], entry, plink);
  987. entry->lock_bucket = bucket;
  988. adb->entry_refcnt[bucket]++;
  989. }
  990. /*
  991. * Requires the entry's bucket be locked.
  992. */
  993. static inline isc_boolean_t
  994. unlink_entry(dns_adb_t *adb, dns_adbentry_t *entry) {
  995. int bucket;
  996. isc_boolean_t result = ISC_FALSE;
  997. bucket = entry->lock_bucket;
  998. INSIST(bucket != DNS_ADB_INVALIDBUCKET);
  999. if ((entry->flags & ENTRY_IS_DEAD) != 0)
  1000. ISC_LIST_UNLINK(adb->deadentries[bucket], entry, plink);
  1001. else
  1002. ISC_LIST_UNLINK(adb->entries[bucket], entry, plink);
  1003. entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
  1004. INSIST(adb->entry_refcnt[bucket] > 0);
  1005. adb->entry_refcnt[bucket]--;
  1006. if (adb->entry_sd[bucket] && adb->entry_refcnt[bucket] == 0)
  1007. result = ISC_TRUE;
  1008. return (result);
  1009. }
  1010. static inline void
  1011. violate_locking_hierarchy(isc_mutex_t *have, isc_mutex_t *want) {
  1012. if (isc_mutex_trylock(want) != ISC_R_SUCCESS) {
  1013. UNLOCK(have);
  1014. LOCK(want);
  1015. LOCK(have);
  1016. }
  1017. }
  1018. /*
  1019. * The ADB _MUST_ be locked before calling. Also, exit conditions must be
  1020. * checked after calling this function.
  1021. */
  1022. static isc_boolean_t
  1023. shutdown_names(dns_adb_t *adb) {
  1024. unsigned int bucket;
  1025. isc_boolean_t result = ISC_FALSE;
  1026. dns_adbname_t *name;
  1027. dns_adbname_t *next_name;
  1028. for (bucket = 0; bucket < adb->nnames; bucket++) {
  1029. LOCK(&adb->namelocks[bucket]);
  1030. adb->name_sd[bucket] = ISC_TRUE;
  1031. name = ISC_LIST_HEAD(adb->names[bucket]);
  1032. if (name == NULL) {
  1033. /*
  1034. * This bucket has no names. We must decrement the
  1035. * irefcnt ourselves, since it will not be
  1036. * automatically triggered by a name being unlinked.
  1037. */
  1038. INSIST(result == ISC_FALSE);
  1039. result = dec_adb_irefcnt(adb);
  1040. } else {
  1041. /*
  1042. * Run through the list. For each name, clean up finds
  1043. * found there, and cancel any fetches running. When
  1044. * all the fetches are canceled, the name will destroy
  1045. * itself.
  1046. */
  1047. while (name != NULL) {
  1048. next_name = ISC_LIST_NEXT(name, plink);
  1049. INSIST(result == ISC_FALSE);
  1050. result = kill_name(&name,
  1051. DNS_EVENT_ADBSHUTDOWN);
  1052. name = next_name;
  1053. }
  1054. }
  1055. UNLOCK(&adb->namelocks[bucket]);
  1056. }
  1057. return (result);
  1058. }
  1059. /*
  1060. * The ADB _MUST_ be locked before calling. Also, exit conditions must be
  1061. * checked after calling this function.
  1062. */
  1063. static isc_boolean_t
  1064. shutdown_entries(dns_adb_t *adb) {
  1065. unsigned int bucket;
  1066. isc_boolean_t result = ISC_FALSE;
  1067. dns_adbentry_t *entry;
  1068. dns_adbentry_t *next_entry;
  1069. for (bucket = 0; bucket < adb->nentries; bucket++) {
  1070. LOCK(&adb->entrylocks[bucket]);
  1071. adb->entry_sd[bucket] = ISC_TRUE;
  1072. entry = ISC_LIST_HEAD(adb->entries[bucket]);
  1073. if (adb->entry_refcnt[bucket] == 0) {
  1074. /*
  1075. * This bucket has no entries. We must decrement the
  1076. * irefcnt ourselves, since it will not be
  1077. * automatically triggered by an entry being unlinked.
  1078. */
  1079. result = dec_adb_irefcnt(adb);
  1080. } else {
  1081. /*
  1082. * Run through the list. Cleanup any entries not
  1083. * associated with names, and which are not in use.
  1084. */
  1085. while (entry != NULL) {
  1086. next_entry = ISC_LIST_NEXT(entry, plink);
  1087. if (entry->refcnt == 0 &&
  1088. entry->expires != 0) {
  1089. result = unlink_entry(adb, entry);
  1090. free_adbentry(adb, &entry);
  1091. if (result)
  1092. result = dec_adb_irefcnt(adb);
  1093. }
  1094. entry = next_entry;
  1095. }
  1096. }
  1097. UNLOCK(&adb->entrylocks[bucket]);
  1098. }
  1099. return (result);
  1100. }
  1101. /*
  1102. * Name bucket must be locked
  1103. */
  1104. static void
  1105. cancel_fetches_at_name(dns_adbname_t *name) {
  1106. if (NAME_FETCH_A(name))
  1107. dns_resolver_cancelfetch(name->fetch_a->fetch);
  1108. if (NAME_FETCH_AAAA(name))
  1109. dns_resolver_cancelfetch(name->fetch_aaaa->fetch);
  1110. }
  1111. /*
  1112. * Assumes the name bucket is locked.
  1113. */
  1114. static isc_boolean_t
  1115. clean_namehooks(dns_adb_t *adb, dns_adbnamehooklist_t *namehooks) {
  1116. dns_adbentry_t *entry;
  1117. dns_adbnamehook_t *namehook;
  1118. int addr_bucket;
  1119. isc_boolean_t result = ISC_FALSE;
  1120. isc_boolean_t overmem = isc_mem_isovermem(adb->mctx);
  1121. addr_bucket = DNS_ADB_INVALIDBUCKET;
  1122. namehook = ISC_LIST_HEAD(*namehooks);
  1123. while (namehook != NULL) {
  1124. INSIST(DNS_ADBNAMEHOOK_VALID(namehook));
  1125. /*
  1126. * Clean up the entry if needed.
  1127. */
  1128. entry = namehook->entry;
  1129. if (entry != NULL) {
  1130. INSIST(DNS_ADBENTRY_VALID(entry));
  1131. if (addr_bucket != entry->lock_bucket) {
  1132. if (addr_bucket != DNS_ADB_INVALIDBUCKET)
  1133. UNLOCK(&adb->entrylocks[addr_bucket]);
  1134. addr_bucket = entry->lock_bucket;
  1135. LOCK(&adb->entrylocks[addr_bucket]);
  1136. }
  1137. result = dec_entry_refcnt(adb, overmem, entry,
  1138. ISC_FALSE);
  1139. }
  1140. /*
  1141. * Free the namehook
  1142. */
  1143. namehook->entry = NULL;
  1144. ISC_LIST_UNLINK(*namehooks, namehook, plink);
  1145. free_adbnamehook(adb, &namehook);
  1146. namehook = ISC_LIST_HEAD(*namehooks);
  1147. }
  1148. if (addr_bucket != DNS_ADB_INVALIDBUCKET)
  1149. UNLOCK(&adb->entrylocks[addr_bucket]);
  1150. return (result);
  1151. }
  1152. static void
  1153. clean_target(dns_adb_t *adb, dns_name_t *target) {
  1154. if (dns_name_countlabels(target) > 0) {
  1155. dns_name_free(target, adb->mctx);
  1156. dns_name_init(target, NULL);
  1157. }
  1158. }
  1159. static isc_result_t
  1160. set_target(dns_adb_t *adb, dns_name_t *name, dns_name_t *fname,
  1161. dns_rdataset_t *rdataset, dns_name_t *target)
  1162. {
  1163. isc_result_t result;
  1164. dns_namereln_t namereln;
  1165. unsigned int nlabels;
  1166. int order;
  1167. dns_rdata_t rdata = DNS_RDATA_INIT;
  1168. dns_fixedname_t fixed1, fixed2;
  1169. dns_name_t *prefix, *new_target;
  1170. REQUIRE(dns_name_countlabels(target) == 0);
  1171. if (rdataset->type == dns_rdatatype_cname) {
  1172. dns_rdata_cname_t cname;
  1173. /*
  1174. * Copy the CNAME's target into the target name.
  1175. */
  1176. result = dns_rdataset_first(rdataset);
  1177. if (result != ISC_R_SUCCESS)
  1178. return (result);
  1179. dns_rdataset_current(rdataset, &rdata);
  1180. result = dns_rdata_tostruct(&rdata, &cname, NULL);
  1181. if (result != ISC_R_SUCCESS)
  1182. return (result);
  1183. result = dns_name_dup(&cname.cname, adb->mctx, target);
  1184. dns_rdata_freestruct(&cname);
  1185. if (result != ISC_R_SUCCESS)
  1186. return (result);
  1187. } else {
  1188. dns_rdata_dname_t dname;
  1189. INSIST(rdataset->type == dns_rdatatype_dname);
  1190. namereln = dns_name_fullcompare(name, fname, &order, &nlabels);
  1191. INSIST(namereln == dns_namereln_subdomain);
  1192. /*
  1193. * Get the target name of the DNAME.
  1194. */
  1195. result = dns_rdataset_first(rdataset);
  1196. if (result != ISC_R_SUCCESS)
  1197. return (result);
  1198. dns_rdataset_current(rdataset, &rdata);
  1199. result = dns_rdata_tostruct(&rdata, &dname, NULL);
  1200. if (result != ISC_R_SUCCESS)
  1201. return (result);
  1202. /*
  1203. * Construct the new target name.
  1204. */
  1205. dns_fixedname_init(&fixed1);
  1206. prefix = dns_fixedname_name(&fixed1);
  1207. dns_fixedname_init(&fixed2);
  1208. new_target = dns_fixedname_name(&fixed2);
  1209. dns_name_split(name, nlabels, prefix, NULL);
  1210. result = dns_name_concatenate(prefix, &dname.dname, new_target,
  1211. NULL);
  1212. dns_rdata_freestruct(&dname);
  1213. if (result != ISC_R_SUCCESS)
  1214. return (result);
  1215. result = dns_name_dup(new_target, adb->mctx, target);
  1216. if (result != ISC_R_SUCCESS)
  1217. return (result);
  1218. }
  1219. return (ISC_R_SUCCESS);
  1220. }
  1221. /*
  1222. * Assumes nothing is locked, since this is called by the client.
  1223. */
  1224. static void
  1225. event_free(isc_event_t *event) {
  1226. dns_adbfind_t *find;
  1227. INSIST(event != NULL);
  1228. find = event->ev_destroy_arg;
  1229. INSIST(DNS_ADBFIND_VALID(find));
  1230. LOCK(&find->lock);
  1231. find->flags |= FIND_EVENT_FREED;
  1232. event->ev_destroy_arg = NULL;
  1233. UNLOCK(&find->lock);
  1234. }
  1235. /*
  1236. * Assumes the name bucket is locked.
  1237. */
  1238. static void
  1239. clean_finds_at_name(dns_adbname_t *name, isc_eventtype_t evtype,
  1240. unsigned int addrs)
  1241. {
  1242. isc_event_t *ev;
  1243. isc_task_t *task;
  1244. dns_adbfind_t *find;
  1245. dns_adbfind_t *next_find;
  1246. isc_boolean_t process;
  1247. unsigned int wanted, notify;
  1248. DP(ENTER_LEVEL,
  1249. "ENTER clean_finds_at_name, name %p, evtype %08x, addrs %08x",
  1250. name, evtype, addrs);
  1251. find = ISC_LIST_HEAD(name->finds);
  1252. while (find != NULL) {
  1253. LOCK(&find->lock);
  1254. next_find = ISC_LIST_NEXT(find, plink);
  1255. process = ISC_FALSE;
  1256. wanted = find->flags & DNS_ADBFIND_ADDRESSMASK;
  1257. notify = wanted & addrs;
  1258. switch (evtype) {
  1259. case DNS_EVENT_ADBMOREADDRESSES:
  1260. DP(ISC_LOG_DEBUG(3), "DNS_EVENT_ADBMOREADDRESSES");
  1261. if ((notify) != 0) {
  1262. find->flags &= ~addrs;
  1263. process = ISC_TRUE;
  1264. }
  1265. break;
  1266. case DNS_EVENT_ADBNOMOREADDRESSES:
  1267. DP(ISC_LOG_DEBUG(3), "DNS_EVENT_ADBNOMOREADDRESSES");
  1268. find->flags &= ~addrs;
  1269. wanted = find->flags & DNS_ADBFIND_ADDRESSMASK;
  1270. if (wanted == 0)
  1271. process = ISC_TRUE;
  1272. break;
  1273. default:
  1274. find->flags &= ~addrs;
  1275. process = ISC_TRUE;
  1276. }
  1277. if (process) {
  1278. DP(DEF_LEVEL, "cfan: processing find %p", find);
  1279. /*
  1280. * Unlink the find from the name, letting the caller
  1281. * call dns_adb_destroyfind() on it to clean it up
  1282. * later.
  1283. */
  1284. ISC_LIST_UNLINK(name->finds, find, plink);
  1285. find->adbname = NULL;
  1286. find->name_bucket = DNS_ADB_INVALIDBUCKET;
  1287. INSIST(!FIND_EVENTSENT(find));
  1288. ev = &find->event;
  1289. task = ev->ev_sender;
  1290. ev->ev_sender = find;
  1291. find->result_v4 = find_err_map[name->fetch_err];
  1292. find->result_v6 = find_err_map[name->fetch6_err];
  1293. ev->ev_type = evtype;
  1294. ev->ev_destroy = event_free;
  1295. ev->ev_destroy_arg = find;
  1296. DP(DEF_LEVEL,
  1297. "sending event %p to task %p for find %p",
  1298. ev, task, find);
  1299. isc_task_sendanddetach(&task, (isc_event_t **)&ev);
  1300. } else {
  1301. DP(DEF_LEVEL, "cfan: skipping find %p", find);
  1302. }
  1303. UNLOCK(&find->lock);
  1304. find = next_find;
  1305. }
  1306. DP(ENTER_LEVEL, "EXIT clean_finds_at_name, name %p", name);
  1307. }
  1308. static inline void
  1309. check_exit(dns_adb_t *adb) {
  1310. isc_event_t *event;
  1311. /*
  1312. * The caller must be holding the adb lock.
  1313. */
  1314. if (adb->shutting_down) {
  1315. /*
  1316. * If there aren't any external references either, we're
  1317. * done. Send the control event to initiate shutdown.
  1318. */
  1319. INSIST(!adb->cevent_sent); /* Sanity check. */
  1320. event = &adb->cevent;
  1321. isc_task_send(adb->task, &event);
  1322. adb->cevent_sent = ISC_TRUE;
  1323. }
  1324. }
  1325. static inline isc_boolean_t
  1326. dec_adb_irefcnt(dns_adb_t *adb) {
  1327. isc_event_t *event;
  1328. isc_task_t *etask;
  1329. isc_boolean_t result = ISC_FALSE;
  1330. LOCK(&adb->reflock);
  1331. INSIST(adb->irefcnt > 0);
  1332. adb->irefcnt--;
  1333. if (adb->irefcnt == 0) {
  1334. event = ISC_LIST_HEAD(adb->whenshutdown);
  1335. while (event != NULL) {
  1336. ISC_LIST_UNLINK(adb->whenshutdown, event, ev_link);
  1337. etask = event->ev_sender;
  1338. event->ev_sender = adb;
  1339. isc_task_sendanddetach(&etask, &event);
  1340. event = ISC_LIST_HEAD(adb->whenshutdown);
  1341. }
  1342. }
  1343. if (adb->irefcnt == 0 && adb->erefcnt == 0)
  1344. result = ISC_TRUE;
  1345. UNLOCK(&adb->reflock);
  1346. return (result);
  1347. }
  1348. static inline void
  1349. inc_adb_irefcnt(dns_adb_t *adb) {
  1350. LOCK(&adb->reflock);
  1351. adb->irefcnt++;
  1352. UNLOCK(&adb->reflock);
  1353. }
  1354. static inline void
  1355. inc_adb_erefcnt(dns_adb_t *adb) {
  1356. LOCK(&adb->reflock);
  1357. adb->erefcnt++;
  1358. UNLOCK(&adb->reflock);
  1359. }
  1360. static inline void
  1361. inc_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock) {
  1362. int bucket;
  1363. bucket = entry->lock_bucket;
  1364. if (lock)
  1365. LOCK(&adb->entrylocks[bucket]);
  1366. entry->refcnt++;
  1367. if (lock)
  1368. UNLOCK(&adb->entrylocks[bucket]);
  1369. }
  1370. static inline isc_boolean_t
  1371. dec_entry_refcnt(dns_adb_t *adb, isc_boolean_t overmem, dns_adbentry_t *entry,
  1372. isc_boolean_t lock)
  1373. {
  1374. int bucket;
  1375. isc_boolean_t destroy_entry;
  1376. isc_boolean_t result = ISC_FALSE;
  1377. bucket = entry->lock_bucket;
  1378. if (lock)
  1379. LOCK(&adb->entrylocks[bucket]);
  1380. INSIST(entry->refcnt > 0);
  1381. entry->refcnt--;
  1382. destroy_entry = ISC_FALSE;
  1383. if (entry->refcnt == 0 &&
  1384. (adb->entry_sd[bucket] || entry->expires == 0 || overmem ||
  1385. (entry->flags & ENTRY_IS_DEAD) != 0)) {
  1386. destroy_entry = ISC_TRUE;
  1387. result = unlink_entry(adb, entry);
  1388. }
  1389. if (lock)
  1390. UNLOCK(&adb->entrylocks[bucket]);
  1391. if (!destroy_entry)
  1392. return (result);
  1393. entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
  1394. free_adbentry(adb, &entry);
  1395. if (result)
  1396. result = dec_adb_irefcnt(adb);
  1397. return (result);
  1398. }
  1399. static inline dns_adbname_t *
  1400. new_adbname(dns_adb_t *adb, dns_name_t *dnsname) {
  1401. dns_adbname_t *name;
  1402. name = isc_mempool_get(adb->nmp);
  1403. if (name == NULL)
  1404. return (NULL);
  1405. dns_name_init(&name->name, NULL);
  1406. if (dns_name_dup(dnsname, adb->mctx, &name->name) != ISC_R_SUCCESS) {
  1407. isc_mempool_put(adb->nmp, name);
  1408. return (NULL);
  1409. }
  1410. dns_name_init(&name->target, NULL);
  1411. name->magic = DNS_ADBNAME_MAGIC;
  1412. name->adb = adb;
  1413. name->partial_result = 0;
  1414. name->flags = 0;
  1415. name->expire_v4 = INT_MAX;
  1416. name->expire_v6 = INT_MAX;
  1417. name->expire_target = INT_MAX;
  1418. name->chains = 0;
  1419. name->lock_bucket = DNS_ADB_INVALIDBUCKET;
  1420. ISC_LIST_INIT(name->v4);
  1421. ISC_LIST_INIT(name->v6);
  1422. name->fetch_a = NULL;
  1423. name->fetch_aaaa = NULL;
  1424. name->fetch_err = FIND_ERR_UNEXPECTED;
  1425. name->fetch6_err = FIND_ERR_UNEXPECTED;
  1426. ISC_LIST_INIT(name->finds);
  1427. ISC_LINK_INIT(name, plink);
  1428. LOCK(&adb->namescntlock);
  1429. adb->namescnt++;
  1430. if (!adb->grownames_sent && adb->namescnt > (adb->nnames * 8)) {
  1431. isc_event_t *event = &adb->grownames;
  1432. inc_adb_irefcnt(adb);
  1433. isc_task_send(adb->task, &event);
  1434. adb->grownames_sent = ISC_TRUE;
  1435. }
  1436. UNLOCK(&adb->namescntlock);
  1437. return (name);
  1438. }
  1439. static inline void
  1440. free_adbname(dns_adb_t *adb, dns_adbname_t **name) {
  1441. dns_adbname_t *n;
  1442. INSIST(name != NULL && DNS_ADBNAME_VALID(*name));
  1443. n = *name;
  1444. *name = NULL;
  1445. INSIST(!NAME_HAS_V4(n));
  1446. INSIST(!NAME_HAS_V6(n));
  1447. INSIST(!NAME_FETCH(n));
  1448. INSIST(ISC_LIST_EMPTY(n->finds));
  1449. INSIST(!ISC_LINK_LINKED(n, plink));
  1450. INSIST(n->lock_bucket == DNS_ADB_INVALIDBUCKET);
  1451. INSIST(n->adb == adb);
  1452. n->magic = 0;
  1453. dns_name_free(&n->name, adb->mctx);
  1454. isc_mempool_put(adb->nmp, n);
  1455. LOCK(&adb->namescntlock);
  1456. adb->namescnt--;
  1457. UNLOCK(&adb->namescntlock);
  1458. }
  1459. static inline dns_adbnamehook_t *
  1460. new_adbnamehook(dns_adb_t *adb, dns_adbentry_t *entry) {
  1461. dns_adbnamehook_t *nh;
  1462. nh = isc_mempool_get(adb->nhmp);
  1463. if (nh == NULL)
  1464. return (NULL);
  1465. nh->magic = DNS_ADBNAMEHOOK_MAGIC;
  1466. nh->entry = entry;
  1467. ISC_LINK_INIT(nh, plink);
  1468. return (nh);
  1469. }
  1470. static inline void
  1471. free_adbnamehook(dns_adb_t *adb, dns_adbnamehook_t **namehook) {
  1472. dns_adbnamehook_t *nh;
  1473. INSIST(namehook != NULL && DNS_ADBNAMEHOOK_VALID(*namehook));
  1474. nh = *namehook;
  1475. *namehook = NULL;
  1476. INSIST(nh->entry == NULL);
  1477. INSIST(!ISC_LINK_LINKED(nh, plink));
  1478. nh->magic = 0;
  1479. isc_mempool_put(adb->nhmp, nh);
  1480. }
  1481. static inline dns_adblameinfo_t *
  1482. new_adblameinfo(dns_adb_t *adb, dns_name_t *qname, dns_rdatatype_t qtype) {
  1483. dns_adblameinfo_t *li;
  1484. li = isc_mempool_get(adb->limp);
  1485. if (li == NULL)
  1486. return (NULL);
  1487. dns_name_init(&li->qname, NULL);
  1488. if (dns_name_dup(qname, adb->mctx, &li->qname) != ISC_R_SUCCESS) {
  1489. isc_mempool_put(adb->limp, li);
  1490. return (NULL);
  1491. }
  1492. li->magic = DNS_ADBLAMEINFO_MAGIC;
  1493. li->lame_timer = 0;
  1494. li->qtype = qtype;
  1495. ISC_LINK_INIT(li, plink);
  1496. return (li);
  1497. }
  1498. static inline void
  1499. free_adblameinfo(dns_adb_t *adb, dns_adblameinfo_t **lameinfo) {
  1500. dns_adblameinfo_t *li;
  1501. INSIST(lameinfo != NULL && DNS_ADBLAMEINFO_VALID(*lameinfo));
  1502. li = *lameinfo;
  1503. *lameinfo = NULL;
  1504. INSIST(!ISC_LINK_LINKED(li, plink));
  1505. dns_name_free(&li->qname, adb->mctx);
  1506. li->magic = 0;
  1507. isc_mempool_put(adb->limp, li);
  1508. }
  1509. static inline dns_adbentry_t *
  1510. new_adbentry(dns_adb_t *adb) {
  1511. dns_adbentry_t *e;
  1512. isc_uint32_t r;
  1513. e = isc_mempool_get(adb->emp);
  1514. if (e == NULL)
  1515. return (NULL);
  1516. e->magic = DNS_ADBENTRY_MAGIC;
  1517. e->lock_bucket = DNS_ADB_INVALIDBUCKET;
  1518. e->refcnt = 0;
  1519. e->flags = 0;
  1520. isc_random_get(&r);
  1521. e->srtt = (r & 0x1f) + 1;
  1522. e->expires = 0;
  1523. ISC_LIST_INIT(e->lameinfo);
  1524. ISC_LINK_INIT(e, plink);
  1525. LOCK(&adb->entriescntlock);
  1526. adb->entriescnt++;
  1527. if (!adb->growentries_sent &&
  1528. adb->entriescnt > (adb->nentries * 8)) {
  1529. isc_event_t *event = &adb->growentries;
  1530. inc_adb_irefcnt(adb);
  1531. isc_task_send(adb->task, &event);
  1532. adb->growentries_sent = ISC_TRUE;
  1533. }
  1534. UNLOCK(&adb->entriescntlock);
  1535. return (e);
  1536. }
  1537. static inline void
  1538. free_adbentry(dns_adb_t *adb, dns_adbentry_t **entry) {
  1539. dns_adbentry_t *e;
  1540. dns_adblameinfo_t *li;
  1541. INSIST(entry != NULL && DNS_ADBENTRY_VALID(*entry));
  1542. e = *entry;
  1543. *entry = NULL;
  1544. INSIST(e->lock_bucket == DNS_ADB_INVALIDBUCKET);
  1545. INSIST(e->refcnt == 0);
  1546. INSIST(!ISC_LINK_LINKED(e, plink));
  1547. e->magic = 0;
  1548. li = ISC_LIST_HEAD(e->lameinfo);
  1549. while (li != NULL) {
  1550. ISC_LIST_UNLINK(e->lameinfo, li, plink);
  1551. free_adblameinfo(adb, &li);
  1552. li = ISC_LIST_HEAD(e->lameinfo);
  1553. }
  1554. isc_mempool_put(adb->emp, e);
  1555. LOCK(&adb->entriescntlock);
  1556. adb->entriescnt--;
  1557. UNLOCK(&adb->entriescntlock);
  1558. }
  1559. static inline dns_adbfind_t *
  1560. new_adbfind(dns_adb_t *adb) {
  1561. dns_adbfind_t *h;
  1562. isc_result_t result;
  1563. h = isc_mempool_get(adb->ahmp);
  1564. if (h == NULL)
  1565. return (NULL);
  1566. /*
  1567. * Public members.
  1568. */
  1569. h->magic = 0;
  1570. h->adb = adb;
  1571. h->partial_result = 0;
  1572. h->options = 0;
  1573. h->flags = 0;
  1574. h->result_v4 = ISC_R_UNEXPECTED;
  1575. h->result_v6 = ISC_R_UNEXPECTED;
  1576. ISC_LINK_INIT(h, publink);
  1577. ISC_LINK_INIT(h, plink);
  1578. ISC_LIST_INIT(h->list);
  1579. h->adbname = NULL;
  1580. h->name_bucket = DNS_ADB_INVALIDBUCKET;
  1581. /*
  1582. * private members
  1583. */
  1584. result = isc_mutex_init(&h->lock);
  1585. if (result != ISC_R_SUCCESS) {
  1586. isc_mempool_put(adb->ahmp, h);
  1587. return (NULL);
  1588. }
  1589. ISC_EVENT_INIT(&h->event, sizeof(isc_event_t), 0, 0, 0, NULL, NULL,
  1590. NULL, NULL, h);
  1591. inc_adb_irefcnt(adb);
  1592. h->magic = DNS_ADBFIND_MAGIC;
  1593. return (h);
  1594. }
  1595. static inline dns_adbfetch_t *
  1596. new_adbfetch(dns_adb_t *adb) {
  1597. dns_adbfetch_t *f;
  1598. f = isc_mempool_get(adb->afmp);
  1599. if (f == NULL)
  1600. return (NULL);
  1601. f->magic = 0;
  1602. f->fetch = NULL;
  1603. dns_rdataset_init(&f->rdataset);
  1604. f->magic = DNS_ADBFETCH_MAGIC;
  1605. return (f);
  1606. }
  1607. static inline void
  1608. free_adbfetch(dns_adb_t *adb, dns_adbfetch_t **fetch) {
  1609. dns_adbfetch_t *f;
  1610. INSIST(fetch != NULL && DNS_ADBFETCH_VALID(*fetch));
  1611. f = *fetch;
  1612. *fetch = NULL;
  1613. f->magic = 0;
  1614. if (dns_rdataset_isassociated(&f->rdataset))
  1615. dns_rdataset_disassociate(&f->rdataset);
  1616. isc_mempool_put(adb->afmp, f);
  1617. }
  1618. static inline isc_boolean_t
  1619. free_adbfind(dns_adb_t *adb, dns_adbfind_t **findp) {
  1620. dns_adbfind_t *find;
  1621. INSIST(findp != NULL && DNS_ADBFIND_VALID(*findp));
  1622. find = *findp;
  1623. *findp = NULL;
  1624. INSIST(!FIND_HAS_ADDRS(find));
  1625. INSIST(!ISC_LINK_LINKED(find, publink));
  1626. INSIST(!ISC_LINK_LINKED(find, plink));
  1627. INSIST(find->name_bucket == DNS_ADB_INVALIDBUCKET);
  1628. INSIST(find->adbname == NULL);
  1629. find->magic = 0;
  1630. DESTROYLOCK(&find->lock);
  1631. isc_mempool_put(adb->ahmp, find);
  1632. return (dec_adb_irefcnt(adb));
  1633. }
  1634. /*
  1635. * Copy bits from the entry into the newly allocated addrinfo. The entry
  1636. * must be locked, and the reference count must be bumped up by one
  1637. * if this function returns a valid pointer.
  1638. */
  1639. static inline dns_adbaddrinfo_t *
  1640. new_adbaddrinfo(dns_adb_t *adb, dns_adbentry_t *entry, in_port_t port) {
  1641. dns_adbaddrinfo_t *ai;
  1642. ai = isc_mempool_get(adb->aimp);
  1643. if (ai == NULL)
  1644. return (NULL);
  1645. ai->magic = DNS_ADBADDRINFO_MAGIC;
  1646. ai->sockaddr = entry->sockaddr;
  1647. isc_sockaddr_setport(&ai->sockaddr, port);
  1648. ai->srtt = entry->srtt;
  1649. ai->flags = entry->flags;
  1650. ai->entry = entry;
  1651. ISC_LINK_INIT(ai, publink);
  1652. return (ai);
  1653. }
  1654. static inline void
  1655. free_adbaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **ainfo) {
  1656. dns_adbaddrinfo_t *ai;
  1657. INSIST(ainfo != NULL && DNS_ADBADDRINFO_VALID(*ainfo));
  1658. ai = *ainfo;
  1659. *ainfo = NULL;
  1660. INSIST(ai->entry == NULL);
  1661. INSIST(!ISC_LINK_LINKED(ai, publink));
  1662. ai->magic = 0;
  1663. isc_mempool_put(adb->aimp, ai);
  1664. }
  1665. /*
  1666. * Search for the name. NOTE: The bucket is kept locked on both
  1667. * success and failure, so it must always be unlocked by the caller!
  1668. *
  1669. * On the first call to this function, *bucketp must be set to
  1670. * DNS_ADB_INVALIDBUCKET.
  1671. */
  1672. static inline dns_adbname_t *
  1673. find_name_and_lock(dns_adb_t *adb, dns_name_t *name,
  1674. unsigned int options, int *bucketp)
  1675. {
  1676. dns_adbname_t *adbname;
  1677. int bucket;
  1678. bucket = dns_name_fullhash(name, ISC_FALSE) % adb->nnames;
  1679. if (*bucketp == DNS_ADB_INVALIDBUCKET) {
  1680. LOCK(&adb->namelocks[bucket]);
  1681. *bucketp = bucket;
  1682. } else if (*bucketp != bucket) {
  1683. UNLOCK(&adb->namelocks[*bucketp]);
  1684. LOCK(&adb->namelocks[bucket]);
  1685. *bucketp = bucket;
  1686. }
  1687. adbname = ISC_LIST_HEAD(adb->names[bucket]);
  1688. while (adbname != NULL) {
  1689. if (!NAME_DEAD(adbname)) {
  1690. if (dns_name_equal(name, &adbname->name)
  1691. && GLUEHINT_OK(adbname, options)
  1692. && STARTATZONE_MATCHES(adbname, options))
  1693. return (adbname);
  1694. }
  1695. adbname = ISC_LIST_NEXT(adbname, plink);
  1696. }
  1697. return (NULL);
  1698. }
  1699. /*
  1700. * Search for the address. NOTE: The bucket is kept locked on both
  1701. * success and failure, so it must always be unlocked by the caller.
  1702. *
  1703. * On the first call to this function, *bucketp must be set to
  1704. * DNS_ADB_INVALIDBUCKET. This will cause a lock to occur. On
  1705. * later calls (within the same "lock path") it can be left alone, so
  1706. * if this function is called multiple times locking is only done if
  1707. * the bucket changes.
  1708. */
  1709. static inline dns_adbentry_t *
  1710. find_entry_and_lock(dns_adb_t *adb, isc_sockaddr_t *addr, int *bucketp,
  1711. isc_stdtime_t now)
  1712. {
  1713. dns_adbentry_t *entry, *entry_next;
  1714. int bucket;
  1715. bucket = isc_sockaddr_hash(addr, ISC_TRUE) % adb->nentries;
  1716. if (*bucketp == DNS_ADB_INVALIDBUCKET) {
  1717. LOCK(&adb->entrylocks[bucket]);
  1718. *bucketp = bucket;
  1719. } else if (*bucketp != bucket) {
  1720. UNLOCK(&adb->entrylocks[*bucketp]);
  1721. LOCK(&adb->entrylocks[bucket]);
  1722. *bucketp = bucket;
  1723. }
  1724. /* Search the list, while cleaning up expired entries. */
  1725. for (entry = ISC_LIST_HEAD(adb->entries[bucket]);
  1726. entry != NULL;
  1727. entry = entry_next) {
  1728. entry_next = ISC_LIST_NEXT(entry, plink);
  1729. (void)check_expire_entry(adb, &entry, now);
  1730. if (entry != NULL &&
  1731. isc_sockaddr_equal(addr, &entry->sockaddr)) {
  1732. ISC_LIST_UNLINK(adb->entries[bucket], entry, plink);
  1733. ISC_LIST_PREPEND(adb->entries[bucket], entry, plink);
  1734. return (entry);
  1735. }
  1736. }
  1737. return (NULL);
  1738. }
  1739. /*
  1740. * Entry bucket MUST be locked!
  1741. */
  1742. static isc_boolean_t
  1743. entry_is_lame(dns_adb_t *adb, dns_adbentry_t *entry, dns_name_t *qname,
  1744. dns_rdatatype_t qtype, isc_stdtime_t now)
  1745. {
  1746. dns_adblameinfo_t *li, *next_li;
  1747. isc_boolean_t is_bad;
  1748. is_bad = ISC_FALSE;
  1749. li = ISC_LIST_HEAD(entry->lameinfo);
  1750. if (li == NULL)
  1751. return (ISC_FALSE);
  1752. while (li != NULL) {
  1753. next_li = ISC_LIST_NEXT(li, plink);
  1754. /*
  1755. * Has the entry expired?
  1756. */
  1757. if (li->lame_timer < now) {
  1758. ISC_LIST_UNLINK(entry->lameinfo, li, plink);
  1759. free_adblameinfo(adb, &li);
  1760. }
  1761. /*
  1762. * Order tests from least to most expensive.
  1763. *
  1764. * We do not break out of the main loop here as
  1765. * we use the loop for house keeping.
  1766. */
  1767. if (li != NULL && !is_bad && li->qtype == qtype &&
  1768. dns_name_equal(qname, &li->qname))
  1769. is_bad = ISC_TRUE;
  1770. li = next_li;
  1771. }
  1772. return (is_bad);
  1773. }
  1774. static void
  1775. copy_namehook_lists(dns_adb_t *adb, dns_adbfind_t *find, dns_name_t *qname,
  1776. dns_rdatatype_t qtype, dns_adbname_t *name,
  1777. isc_stdtime_t now)
  1778. {
  1779. dns_adbnamehook_t *namehook;
  1780. dns_adbaddrinfo_t *addrinfo;
  1781. dns_adbentry_t *entry;
  1782. int bucket;
  1783. bucket = DNS_ADB_INVALIDBUCKET;
  1784. if (find->options & DNS_ADBFIND_INET) {
  1785. namehook = ISC_LIST_HEAD(name->v4);
  1786. while (namehook != NULL) {
  1787. entry = namehook->entry;
  1788. bucket = entry->lock_bucket;
  1789. LOCK(&adb->entrylocks[bucket]);
  1790. if (!FIND_RETURNLAME(find)
  1791. && entry_is_lame(adb, entry, qname, qtype, now)) {
  1792. find->options |= DNS_ADBFIND_LAMEPRUNED;
  1793. goto nextv4;
  1794. }
  1795. addrinfo = new_adbaddrinfo(adb, entry, find->port);
  1796. if (addrinfo == NULL) {
  1797. find->partial_result |= DNS_ADBFIND_INET;
  1798. goto out;
  1799. }
  1800. /*
  1801. * Found a valid entry. Add it to the find's list.
  1802. */
  1803. inc_entry_refcnt(adb, entry, ISC_FALSE);
  1804. ISC_LIST_APPEND(find->list, addrinfo, publink);
  1805. addrinfo = NULL;
  1806. nextv4:
  1807. UNLOCK(&adb->entrylocks[bucket]);
  1808. bucket = DNS_ADB_INVALIDBUCKET;
  1809. namehook = ISC_LIST_NEXT(namehook, plink);
  1810. }
  1811. }
  1812. if (find->options & DNS_ADBFIND_INET6) {
  1813. namehook = ISC_LIST_HEAD(name->v6);
  1814. while (namehook != NULL) {
  1815. entry = namehook->entry;
  1816. bucket = entry->lock_bucket;
  1817. LOCK(&adb->entrylocks[bucket]);
  1818. if (!FIND_RETURNLAME(find)
  1819. && entry_is_lame(adb, entry, qname, qtype, now)) {
  1820. find->options |= DNS_ADBFIND_LAMEPRUNED;
  1821. goto nextv6;
  1822. }
  1823. addrinfo = new_adbaddrinfo(adb, entry, find->port);
  1824. if (addrinfo == NULL) {
  1825. find->partial_result |= DNS_ADBFIND_INET6;
  1826. goto out;
  1827. }
  1828. /*
  1829. * Found a valid entry. Add it to the find's list.
  1830. */
  1831. inc_entry_refcnt(adb, entry, ISC_FALSE);
  1832. ISC_LIST_APPEND(find->list, addrinfo, publink);
  1833. addrinfo = NULL;
  1834. nextv6:
  1835. UNLOCK(&adb->entrylocks[bucket]);
  1836. bucket = DNS_ADB_INVALIDBUCKET;
  1837. namehook = ISC_LIST_NEXT(namehook, plink);
  1838. }
  1839. }
  1840. out:
  1841. if (bucket != DNS_ADB_INVALIDBUCKET)
  1842. UNLOCK(&adb->entrylocks[bucket]);
  1843. }
  1844. static void
  1845. shutdown_task(isc_task_t *task, isc_event_t *ev) {
  1846. dns_adb_t *adb;
  1847. UNUSED(task);
  1848. adb = ev->ev_arg;
  1849. INSIST(DNS_ADB_VALID(adb));
  1850. isc_event_free(&ev);
  1851. /*
  1852. * Wait for lock around check_exit() call to be released.
  1853. */
  1854. LOCK(&adb->lock);
  1855. UNLOCK(&adb->lock);
  1856. destroy(adb);
  1857. }
  1858. /*
  1859. * Name bucket must be locked; adb may be locked; no other locks held.
  1860. */
  1861. static isc_boolean_t
  1862. check_expire_name(dns_adbname_t **namep, isc_stdtime_t now) {
  1863. dns_adbname_t *name;
  1864. isc_boolean_t result = ISC_FALSE;
  1865. INSIST(namep != NULL && DNS_ADBNAME_VALID(*namep));
  1866. name = *namep;
  1867. if (NAME_HAS_V4(name) || NAME_HAS_V6(name))
  1868. return (result);
  1869. if (NAME_FETCH(name))
  1870. return (result);
  1871. if (!EXPIRE_OK(name->expire_v4, now))
  1872. return (result);
  1873. if (!EXPIRE_OK(name->expire_v6, now))
  1874. return (result);
  1875. if (!EXPIRE_OK(name->expire_target, now))
  1876. return (result);
  1877. /*
  1878. * The name is empty. Delete it.
  1879. */
  1880. result = kill_name(&name, DNS_EVENT_ADBEXPIRED);
  1881. *namep = NULL;
  1882. /*
  1883. * Our caller, or one of its callers, will be calling check_exit() at
  1884. * some point, so we don't need to do it here.
  1885. */
  1886. return (result);
  1887. }
  1888. /*%
  1889. * Examine the tail entry of the LRU list to see if it expires or is stale
  1890. * (unused for some period); if so, the name entry will be freed. If the ADB
  1891. * is in the overmem condition, the tail and the next to tail entries
  1892. * will be unconditionally removed (unless they have an outstanding fetch).
  1893. * We don't care about a race on 'overmem' at the risk of causing some
  1894. * collateral damage or a small delay in starting cleanup, so we don't bother
  1895. * to lock ADB (if it's not locked).
  1896. *
  1897. * Name bucket must be locked; adb may be locked; no other locks held.
  1898. */
  1899. static void
  1900. check_stale_name(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
  1901. int victims, max_victims;
  1902. dns_adbname_t *victim, *next_victim;
  1903. isc_boolean_t overmem = isc_mem_isovermem(adb->mctx);
  1904. int scans = 0;
  1905. INSIST(bucket != DNS_ADB_INVALIDBUCKET);
  1906. max_victims = overmem ? 2 : 1;
  1907. /*
  1908. * We limit the number of scanned entries to 10 (arbitrary choice)
  1909. * in order to avoid examining too many entries when there are many
  1910. * tail entries that have fetches (this should be rare, but could
  1911. * happen).
  1912. */
  1913. victim = ISC_LIST_TAIL(adb->names[bucket]);
  1914. for (victims = 0;
  1915. victim != NULL && victims < max_victims && scans < 10;
  1916. victim = next_victim) {
  1917. INSIST(!NAME_DEAD(victim));
  1918. scans++;
  1919. next_victim = ISC_LIST_PREV(victim, plink);
  1920. (void)check_expire_name(&victim, now);
  1921. if (victim == NULL) {
  1922. victims++;
  1923. goto next;
  1924. }
  1925. if (!NAME_FETCH(victim) &&
  1926. (overmem || victim->last_used + ADB_STALE_MARGIN <= now)) {
  1927. RUNTIME_CHECK(kill_name(&victim,
  1928. DNS_EVENT_ADBCANCELED) ==
  1929. ISC_FALSE);
  1930. victims++;
  1931. }
  1932. next:
  1933. if (!overmem)
  1934. break;
  1935. }
  1936. }
  1937. /*
  1938. * Entry bucket must be locked; adb may be locked; no other locks held.
  1939. */
  1940. static isc_boolean_t
  1941. check_expire_entry(dns_adb_t *adb, dns_adbentry_t **entryp, isc_stdtime_t now)
  1942. {
  1943. dns_adbentry_t *entry;
  1944. isc_boolean_t result = ISC_FALSE;
  1945. INSIST(entryp != NULL && DNS_ADBENTRY_VALID(*entryp));
  1946. entry = *entryp;
  1947. if (entry->refcnt != 0)
  1948. return (result);
  1949. if (entry->expires == 0 || entry->expires > now)
  1950. return (result);
  1951. /*
  1952. * The entry is not in use. Delete it.
  1953. */
  1954. DP(DEF_LEVEL, "killing entry %p", entry);
  1955. INSIST(ISC_LINK_LINKED(entry, plink));
  1956. result = unlink_entry(adb, entry);
  1957. free_adbentry(adb, &entry);
  1958. if (result)
  1959. dec_adb_irefcnt(adb);
  1960. *entryp = NULL;
  1961. return (result);
  1962. }
  1963. /*
  1964. * ADB must be locked, and no other locks held.
  1965. */
  1966. static isc_boolean_t
  1967. cleanup_names(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
  1968. dns_adbname_t *name;
  1969. dns_adbname_t *next_name;
  1970. isc_boolean_t result = ISC_FALSE;
  1971. DP(CLEAN_LEVEL, "cleaning name bucket %d", bucket);
  1972. LOCK(&adb->namelocks[bucket]);
  1973. if (adb->name_sd[bucket]) {
  1974. UNLOCK(&adb->namelocks[bucket]);
  1975. return (result);
  1976. }
  1977. name = ISC_LIST_HEAD(adb->names[bucket]);
  1978. while (name != NULL) {
  1979. next_name = ISC_LIST_NEXT(name, plink);
  1980. INSIST(result == ISC_FALSE);
  1981. result = check_expire_namehooks(name, now);
  1982. if (!result)
  1983. result = check_expire_name(&name, now);
  1984. name = next_name;
  1985. }
  1986. UNLOCK(&adb->namelocks[bucket]);
  1987. return (result);
  1988. }
  1989. /*
  1990. * ADB must be locked, and no other locks held.
  1991. */
  1992. static isc_boolean_t
  1993. cleanup_entries(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
  1994. dns_adbentry_t *entry, *next_entry;
  1995. isc_boolean_t result = ISC_FALSE;
  1996. DP(CLEAN_LEVEL, "cleaning entry bucket %d", bucket);
  1997. LOCK(&adb->entrylocks[bucket]);
  1998. entry = ISC_LIST_HEAD(adb->entries[bucket]);
  1999. while (entry != NULL) {
  2000. next_entry = ISC_LIST_NEXT(entry, plink);
  2001. INSIST(result == ISC_FALSE);
  2002. result = check_expire_entry(adb, &entry, now);
  2003. entry = next_entry;
  2004. }
  2005. UNLOCK(&adb->entrylocks[bucket]);
  2006. return (result);
  2007. }
  2008. static void
  2009. destroy(dns_adb_t *adb) {
  2010. adb->magic = 0;
  2011. isc_task_detach(&adb->task);
  2012. isc_mempool_destroy(&adb->nmp);
  2013. isc_mempool_destroy(&adb->nhmp);
  2014. isc_mempool_destroy(&adb->limp);
  2015. isc_mempool_destroy(&adb->emp);
  2016. isc_mempool_destroy(&adb->ahmp);
  2017. isc_mempool_destroy(&adb->aimp);
  2018. isc_mempool_destroy(&adb->afmp);
  2019. DESTROYMUTEXBLOCK(adb->entrylocks, adb->nentries);
  2020. isc_mem_put(adb->mctx, adb->entries,
  2021. sizeof(*adb->entries) * adb->nentries);
  2022. isc_mem_put(adb->mctx, adb->deadentries,
  2023. sizeof(*adb->deadentries) * adb->nentries);
  2024. isc_mem_put(adb->mctx, adb->entrylocks,
  2025. sizeof(*adb->entrylocks) * adb->nentries);
  2026. isc_mem_put(adb->mctx, adb->entry_sd,
  2027. sizeof(*adb->entry_sd) * adb->nentries);
  2028. isc_mem_put(adb->mctx, adb->entry_refcnt,
  2029. sizeof(*adb->entry_refcnt) * adb->nentries);
  2030. DESTROYMUTEXBLOCK(adb->namelocks, adb->nnames);
  2031. isc_mem_put(adb->mctx, adb->names,
  2032. sizeof(*adb->names) * adb->nnames);
  2033. isc_mem_put(adb->mctx, adb->deadnames,
  2034. sizeof(*adb->deadnames) * adb->nnames);
  2035. isc_mem_put(adb->mctx, adb->namelocks,
  2036. sizeof(*adb->namelocks) * adb->nnames);
  2037. isc_mem_put(adb->mctx, adb->name_sd,
  2038. sizeof(*adb->name_sd) * adb->nnames);
  2039. isc_mem_put(adb->mctx, adb->name_refcnt,
  2040. sizeof(*adb->name_refcnt) * adb->nnames);
  2041. DESTROYLOCK(&adb->reflock);
  2042. DESTROYLOCK(&adb->lock);
  2043. DESTROYLOCK(&adb->mplock);
  2044. DESTROYLOCK(&adb->overmemlock);
  2045. DESTROYLOCK(&adb->entriescntlock);
  2046. DESTROYLOCK(&adb->namescntlock);
  2047. isc_mem_putanddetach(&adb->mctx, adb, sizeof(dns_adb_t));
  2048. }
  2049. /*
  2050. * Public functions.
  2051. */
  2052. isc_result_t
  2053. dns_adb_create(isc_mem_t *mem, dns_view_t *view, isc_timermgr_t *timermgr,
  2054. isc_taskmgr_t *taskmgr, dns_adb_t **newadb)
  2055. {
  2056. dns_adb_t *adb;
  2057. isc_result_t result;
  2058. unsigned int i;
  2059. REQUIRE(mem != NULL);
  2060. REQUIRE(view != NULL);
  2061. REQUIRE(timermgr != NULL); /* this is actually unused */
  2062. REQUIRE(taskmgr != NULL);
  2063. REQUIRE(newadb != NULL && *newadb == NULL);
  2064. UNUSED(timermgr);
  2065. adb = isc_mem_get(mem, sizeof(dns_adb_t));
  2066. if (adb == NULL)
  2067. return (ISC_R_NOMEMORY);
  2068. /*
  2069. * Initialize things here that cannot fail, and especially things
  2070. * that must be NULL for the error return to work properly.
  2071. */
  2072. adb->magic = 0;
  2073. adb->erefcnt = 1;
  2074. adb->irefcnt = 0;
  2075. adb->nmp = NULL;
  2076. adb->nhmp = NULL;
  2077. adb->limp = NULL;
  2078. adb->emp = NULL;
  2079. adb->ahmp = NULL;
  2080. adb->aimp = NULL;
  2081. adb->afmp = NULL;
  2082. adb->task = NULL;
  2083. adb->mctx = NULL;
  2084. adb->view = view;
  2085. adb->taskmgr = taskmgr;
  2086. adb->next_cleanbucket = 0;
  2087. ISC_EVENT_INIT(&adb->cevent, sizeof(adb->cevent), 0, NULL,
  2088. DNS_EVENT_ADBCONTROL, shutdown_task, adb,
  2089. adb, NULL, NULL);
  2090. adb->cevent_sent = ISC_FALSE;
  2091. adb->shutting_down = ISC_FALSE;
  2092. ISC_LIST_INIT(adb->whenshutdown);
  2093. adb->nentries = nbuckets[0];
  2094. adb->entriescnt = 0;
  2095. adb->entries = NULL;
  2096. adb->deadentries = NULL;
  2097. adb->entry_sd = NULL;
  2098. adb->entry_refcnt = NULL;
  2099. adb->entrylocks = NULL;
  2100. ISC_EVENT_INIT(&adb->growentries, sizeof(adb->growentries), 0, NULL,
  2101. DNS_EVENT_ADBGROWENTRIES, grow_entries, adb,
  2102. adb, NULL, NULL);
  2103. adb->growentries_sent = ISC_FALSE;
  2104. adb->nnames = nbuckets[0];
  2105. adb->namescnt = 0;
  2106. adb->names = NULL;
  2107. adb->deadnames = NULL;
  2108. adb->name_sd = NULL;
  2109. adb->name_refcnt = NULL;
  2110. adb->namelocks = NULL;
  2111. ISC_EVENT_INIT(&adb->grownames, sizeof(adb->grownames), 0, NULL,
  2112. DNS_EVENT_ADBGROWNAMES, grow_names, adb,
  2113. adb, NULL, NULL);
  2114. adb->grownames_sent = ISC_FALSE;
  2115. isc_mem_attach(mem, &adb->mctx);
  2116. result = isc_mutex_init(&adb->lock);
  2117. if (result != ISC_R_SUCCESS)
  2118. goto fail0b;
  2119. result = isc_mutex_init(&adb->mplock);
  2120. if (result != ISC_R_SUCCESS)
  2121. goto fail0c;
  2122. result = isc_mutex_init(&adb->reflock);
  2123. if (result != ISC_R_SUCCESS)
  2124. goto fail0d;
  2125. result = isc_mutex_init(&adb->overmemlock);
  2126. if (result != ISC_R_SUCCESS)
  2127. goto fail0e;
  2128. result = isc_mutex_init(&adb->entriescntlock);
  2129. if (result != ISC_R_SUCCESS)
  2130. goto fail0f;
  2131. result = isc_mutex_init(&adb->namescntlock);
  2132. if (result != ISC_R_SUCCESS)
  2133. goto fail0g;
  2134. #define ALLOCENTRY(adb, el) \
  2135. do { \
  2136. (adb)->el = isc_mem_get((adb)->mctx, \
  2137. sizeof(*(adb)->el) * (adb)->nentries); \
  2138. if ((adb)->el == NULL) { \
  2139. result = ISC_R_NOMEMORY; \
  2140. goto fail1; \
  2141. }\
  2142. } while (0)
  2143. ALLOCENTRY(adb, entries);
  2144. ALLOCENTRY(adb, deadentries);
  2145. ALLOCENTRY(adb, entrylocks);
  2146. ALLOCENTRY(adb, entry_sd);
  2147. ALLOCENTRY(adb, entry_refcnt);
  2148. #undef ALLOCENTRY
  2149. #define ALLOCNAME(adb, el) \
  2150. do { \
  2151. (adb)->el = isc_mem_get((adb)->mctx, \
  2152. sizeof(*(adb)->el) * (adb)->nnames); \
  2153. if ((adb)->el == NULL) { \
  2154. result = ISC_R_NOMEMORY; \
  2155. goto fail1; \
  2156. }\
  2157. } while (0)
  2158. ALLOCNAME(adb, names);
  2159. ALLOCNAME(adb, deadnames);
  2160. ALLOCNAME(adb, namelocks);
  2161. ALLOCNAME(adb, name_sd);
  2162. ALLOCNAME(adb, name_refcnt);
  2163. #undef ALLOCNAME
  2164. /*
  2165. * Initialize the bucket locks for names and elements.
  2166. * May as well initialize the list heads, too.
  2167. */
  2168. result = isc_mutexblock_init(adb->namelocks, adb->nnames);
  2169. if (result != ISC_R_SUCCESS)
  2170. goto fail1;
  2171. for (i = 0; i < adb->nnames; i++) {
  2172. ISC_LIST_INIT(adb->names[i]);
  2173. ISC_LIST_INIT(adb->deadnames[i]);
  2174. adb->name_sd[i] = ISC_FALSE;
  2175. adb->name_refcnt[i] = 0;
  2176. adb->irefcnt++;
  2177. }
  2178. for (i = 0; i < adb->nentries; i++) {
  2179. ISC_LIST_INIT(adb->entries[i]);
  2180. ISC_LIST_INIT(adb->deadentries[i]);
  2181. adb->entry_sd[i] = ISC_FALSE;
  2182. adb->entry_refcnt[i] = 0;
  2183. adb->irefcnt++;
  2184. }
  2185. result = isc_mutexblock_init(adb->entrylocks, adb->nentries);
  2186. if (result != ISC_R_SUCCESS)
  2187. goto fail2;
  2188. /*
  2189. * Memory pools
  2190. */
  2191. #define MPINIT(t, p, n) do { \
  2192. result = isc_mempool_create(mem, sizeof(t), &(p)); \
  2193. if (result != ISC_R_SUCCESS) \
  2194. goto fail3; \
  2195. isc_mempool_setfreemax((p), FREE_ITEMS); \
  2196. isc_mempool_setfillcount((p), FILL_COUNT); \
  2197. isc_mempool_setname((p), n); \
  2198. isc_mempool_associatelock((p), &adb->mplock); \
  2199. } while (0)
  2200. MPINIT(dns_adbname_t, adb->nmp, "adbname");
  2201. MPINIT(dns_adbnamehook_t, adb->nhmp, "adbnamehook");
  2202. MPINIT(dns_adblameinfo_t, adb->limp, "adblameinfo");
  2203. MPINIT(dns_adbentry_t, adb->emp, "adbentry");
  2204. MPINIT(dns_adbfind_t, adb->ahmp, "adbfind");
  2205. MPINIT(dns_adbaddrinfo_t, adb->aimp, "adbaddrinfo");
  2206. MPINIT(dns_adbfetch_t, adb->afmp, "adbfetch");
  2207. #undef MPINIT
  2208. /*
  2209. * Allocate an internal task.
  2210. */
  2211. result = isc_task_create(adb->taskmgr, 0, &adb->task);
  2212. if (result != ISC_R_SUCCESS)
  2213. goto fail3;
  2214. isc_task_setname(adb->task, "ADB", adb);
  2215. /*
  2216. * Normal return.
  2217. */
  2218. adb->magic = DNS_ADB_MAGIC;
  2219. *newadb = adb;
  2220. return (ISC_R_SUCCESS);
  2221. fail3:
  2222. if (adb->task != NULL)
  2223. isc_task_detach(&adb->task);
  2224. /* clean up entrylocks */
  2225. DESTROYMUTEXBLOCK(adb->entrylocks, adb->nentries);
  2226. fail2: /* clean up namelocks */
  2227. DESTROYMUTEXBLOCK(adb->namelocks, adb->nnames);
  2228. fail1: /* clean up only allocated memory */
  2229. if (adb->entries != NULL)
  2230. isc_mem_put(adb->mctx, adb->entries,
  2231. sizeof(*adb->entries) * adb->nentries);
  2232. if (adb->deadentries != NULL)
  2233. isc_mem_put(adb->mctx, adb->deadentries,
  2234. sizeof(*adb->deadentries) * adb->nentries);
  2235. if (adb->entrylocks != NULL)
  2236. isc_mem_put(adb->mctx, adb->entrylocks,
  2237. sizeof(*adb->entrylocks) * adb->nentries);
  2238. if (adb->entry_sd != NULL)
  2239. isc_mem_put(adb->mctx, adb->entry_sd,
  2240. sizeof(*adb->entry_sd) * adb->nentries);
  2241. if (adb->entry_refcnt != NULL)
  2242. isc_mem_put(adb->mctx, adb->entry_refcnt,
  2243. sizeof(*adb->entry_refcnt) * adb->nentries);
  2244. if (adb->names != NULL)
  2245. isc_mem_put(adb->mctx, adb->names,
  2246. sizeof(*adb->names) * adb->nnames);
  2247. if (adb->deadnames != NULL)
  2248. isc_mem_put(adb->mctx, adb->deadnames,
  2249. sizeof(*adb->deadnames) * adb->nnames);
  2250. if (adb->namelocks != NULL)
  2251. isc_mem_put(adb->mctx, adb->namelocks,
  2252. sizeof(*adb->namelocks) * adb->nnames);
  2253. if (adb->name_sd != NULL)
  2254. isc_mem_put(adb->mctx, adb->name_sd,
  2255. sizeof(*adb->name_sd) * adb->nnames);
  2256. if (adb->name_refcnt != NULL)
  2257. isc_mem_put(adb->mctx, adb->name_refcnt,
  2258. sizeof(*adb->name_refcnt) * adb->nnames);
  2259. if (adb->nmp != NULL)
  2260. isc_mempool_destroy(&adb->nmp);
  2261. if (adb->nhmp != NULL)
  2262. isc_mempool_destroy(&adb->nhmp);
  2263. if (adb->limp != NULL)
  2264. isc_mempool_destroy(&adb->limp);
  2265. if (adb->emp != NULL)
  2266. isc_mempool_destroy(&adb->emp);
  2267. if (adb->ahmp != NULL)
  2268. isc_mempool_destroy(&adb->ahmp);
  2269. if (adb->aimp != NULL)
  2270. isc_mempool_destroy(&adb->aimp);
  2271. if (adb->afmp != NULL)
  2272. isc_mempool_destroy(&adb->afmp);
  2273. DESTROYLOCK(&adb->namescntlock);
  2274. fail0g:
  2275. DESTROYLOCK(&adb->entriescntlock);
  2276. fail0f:
  2277. DESTROYLOCK(&adb->overmemlock);
  2278. fail0e:
  2279. DESTROYLOCK(&adb->reflock);
  2280. fail0d:
  2281. DESTROYLOCK(&adb->mplock);
  2282. fail0c:
  2283. DESTROYLOCK(&adb->lock);
  2284. fail0b:
  2285. isc_mem_putanddetach(&adb->mctx, adb, sizeof(dns_adb_t));
  2286. return (result);
  2287. }
  2288. void
  2289. dns_adb_attach(dns_adb_t *adb, dns_adb_t **adbx) {
  2290. REQUIRE(DNS_ADB_VALID(adb));
  2291. REQUIRE(adbx != NULL && *adbx == NULL);
  2292. inc_adb_erefcnt(adb);
  2293. *adbx = adb;
  2294. }
  2295. void
  2296. dns_adb_detach(dns_adb_t **adbx) {
  2297. dns_adb_t *adb;
  2298. isc_boolean_t need_exit_check;
  2299. REQUIRE(adbx != NULL && DNS_ADB_VALID(*adbx));
  2300. adb = *adbx;
  2301. *adbx = NULL;
  2302. INSIST(adb->erefcnt > 0);
  2303. LOCK(&adb->reflock);
  2304. adb->erefcnt--;
  2305. need_exit_check = ISC_TF(adb->erefcnt == 0 && adb->irefcnt == 0);
  2306. UNLOCK(&adb->reflock);
  2307. if (need_exit_check) {
  2308. LOCK(&adb->lock);
  2309. INSIST(adb->shutting_down);
  2310. check_exit(adb);
  2311. UNLOCK(&adb->lock);
  2312. }
  2313. }
  2314. void
  2315. dns_adb_whenshutdown(dns_adb_t *adb, isc_task_t *task, isc_event_t **eventp) {
  2316. isc_task_t *clone;
  2317. isc_event_t *event;
  2318. isc_boolean_t zeroirefcnt = ISC_FALSE;
  2319. /*
  2320. * Send '*eventp' to 'task' when 'adb' has shutdown.
  2321. */
  2322. REQUIRE(DNS_ADB_VALID(adb));
  2323. REQUIRE(eventp != NULL);
  2324. event = *eventp;
  2325. *eventp = NULL;
  2326. LOCK(&adb->lock);
  2327. LOCK(&adb->reflock);
  2328. zeroirefcnt = ISC_TF(adb->irefcnt == 0);
  2329. if (adb->shutting_down && zeroirefcnt &&
  2330. isc_mempool_getallocated(adb->ahmp) == 0) {
  2331. /*
  2332. * We're already shutdown. Send the event.
  2333. */
  2334. event->ev_sender = adb;
  2335. isc_task_send(task, &event);
  2336. } else {
  2337. clone = NULL;
  2338. isc_task_attach(task, &clone);
  2339. event->ev_sender = clone;
  2340. ISC_LIST_APPEND(adb->whenshutdown, event, ev_link);
  2341. }
  2342. UNLOCK(&adb->reflock);
  2343. UNLOCK(&adb->lock);
  2344. }
  2345. void
  2346. dns_adb_shutdown(dns_adb_t *adb) {
  2347. isc_boolean_t need_check_exit;
  2348. /*
  2349. * Shutdown 'adb'.
  2350. */
  2351. LOCK(&adb->lock);
  2352. if (!adb->shutting_down) {
  2353. adb->shutting_down = ISC_TRUE;
  2354. isc_mem_setwater(adb->mctx, water, adb, 0, 0);
  2355. need_check_exit = shutdown_names(adb);
  2356. if (!need_check_exit)
  2357. need_check_exit = shutdown_entries(adb);
  2358. if (need_check_exit)
  2359. check_exit(adb);
  2360. }
  2361. UNLOCK(&adb->lock);
  2362. }
  2363. isc_result_t
  2364. dns_adb_createfind(dns_adb_t *adb, isc_task_t *task, isc_taskaction_t action,
  2365. void *arg, dns_name_t *name, dns_name_t *qname,
  2366. dns_rdatatype_t qtype, unsigned int options,
  2367. isc_stdtime_t now, dns_name_t *target,
  2368. in_port_t port, dns_adbfind_t **findp)
  2369. {
  2370. dns_adbfind_t *find;
  2371. dns_adbname_t *adbname;
  2372. int bucket;
  2373. isc_boolean_t want_event, start_at_zone, alias, have_address;
  2374. isc_result_t result;
  2375. unsigned int wanted_addresses;
  2376. unsigned int wanted_fetches;
  2377. unsigned int query_pending;
  2378. REQUIRE(DNS_ADB_VALID(adb));
  2379. if (task != NULL) {
  2380. REQUIRE(action != NULL);
  2381. }
  2382. REQUIRE(name != NULL);
  2383. REQUIRE(qname != NULL);
  2384. REQUIRE(findp != NULL && *findp == NULL);
  2385. REQUIRE(target == NULL || dns_name_hasbuffer(target));
  2386. REQUIRE((options & DNS_ADBFIND_ADDRESSMASK) != 0);
  2387. result = ISC_R_UNEXPECTED;
  2388. POST(result);
  2389. wanted_addresses = (options & DNS_ADBFIND_ADDRESSMASK);
  2390. wanted_fetches = 0;
  2391. query_pending = 0;
  2392. want_event = ISC_FALSE;
  2393. start_at_zone = ISC_FALSE;
  2394. alias = ISC_FALSE;
  2395. if (now == 0)
  2396. isc_stdtime_get(&now);
  2397. /*
  2398. * XXXMLG Move this comment somewhere else!
  2399. *
  2400. * Look up the name in our internal database.
  2401. *
  2402. * Possibilities: Note that these are not always exclusive.
  2403. *
  2404. * No name found. In this case, allocate a new name header and
  2405. * an initial namehook or two. If any of these allocations
  2406. * fail, clean up and return ISC_R_NOMEMORY.
  2407. *
  2408. * Name found, valid addresses present. Allocate one addrinfo
  2409. * structure for each found and append it to the linked list
  2410. * of addresses for this header.
  2411. *
  2412. * Name found, queries pending. In this case, if a task was
  2413. * passed in, allocate a job id, attach it to the name's job
  2414. * list and remember to tell the caller that there will be
  2415. * more info coming later.
  2416. */
  2417. find = new_adbfind(adb);
  2418. if (find == NULL)
  2419. return (ISC_R_NOMEMORY);
  2420. find->port = port;
  2421. /*
  2422. * Remember what types of addresses we are interested in.
  2423. */
  2424. find->options = options;
  2425. find->flags |= wanted_addresses;
  2426. if (FIND_WANTEVENT(find)) {
  2427. REQUIRE(task != NULL);
  2428. }
  2429. /*
  2430. * Try to see if we know anything about this name at all.
  2431. */
  2432. bucket = DNS_ADB_INVALIDBUCKET;
  2433. adbname = find_name_and_lock(adb, name, find->options, &bucket);
  2434. INSIST(bucket != DNS_ADB_INVALIDBUCKET);
  2435. if (adb->name_sd[bucket]) {
  2436. DP(DEF_LEVEL,
  2437. "dns_adb_createfind: returning ISC_R_SHUTTINGDOWN");
  2438. RUNTIME_CHECK(free_adbfind(adb, &find) == ISC_FALSE);
  2439. result = ISC_R_SHUTTINGDOWN;
  2440. goto out;
  2441. }
  2442. /*
  2443. * Nothing found. Allocate a new adbname structure for this name.
  2444. */
  2445. if (adbname == NULL) {
  2446. /*
  2447. * See if there is any stale name at the end of list, and purge
  2448. * it if so.
  2449. */
  2450. check_stale_name(adb, bucket, now);
  2451. adbname = new_adbname(adb, name);
  2452. if (adbname == NULL) {
  2453. RUNTIME_CHECK(free_adbfind(adb, &find) == ISC_FALSE);
  2454. result = ISC_R_NOMEMORY;
  2455. goto out;
  2456. }
  2457. link_name(adb, bucket, adbname);
  2458. if (FIND_HINTOK(find))
  2459. adbname->flags |= NAME_HINT_OK;
  2460. if (FIND_GLUEOK(find))
  2461. adbname->flags |= NAME_GLUE_OK;
  2462. if (FIND_STARTATZONE(find))
  2463. adbname->flags |= NAME_STARTATZONE;
  2464. } else {
  2465. /* Move this name forward in the LRU list */
  2466. ISC_LIST_UNLINK(adb->names[bucket], adbname, plink);
  2467. ISC_LIST_PREPEND(adb->names[bucket], adbname, plink);
  2468. }
  2469. adbname->last_used = now;
  2470. /*
  2471. * Expire old entries, etc.
  2472. */
  2473. RUNTIME_CHECK(check_expire_namehooks(adbname, now) == ISC_FALSE);
  2474. /*
  2475. * Do we know that the name is an alias?
  2476. */
  2477. if (!EXPIRE_OK(adbname->expire_target, now)) {
  2478. /*
  2479. * Yes, it is.
  2480. */
  2481. DP(DEF_LEVEL,
  2482. "dns_adb_createfind: name %p is an alias (cached)",
  2483. adbname);
  2484. alias = ISC_TRUE;
  2485. goto post_copy;
  2486. }
  2487. /*
  2488. * Try to populate the name from the database and/or
  2489. * start fetches. First try looking for an A record
  2490. * in the database.
  2491. */
  2492. if (!NAME_HAS_V4(adbname) && EXPIRE_OK(adbname->expire_v4, now)
  2493. && WANT_INET(wanted_addresses)) {
  2494. result = dbfind_name(adbname, now, dns_rdatatype_a);
  2495. if (result == ISC_R_SUCCESS) {
  2496. DP(DEF_LEVEL,
  2497. "dns_adb_createfind: found A for name %p in db",
  2498. adbname);
  2499. goto v6;
  2500. }
  2501. /*
  2502. * Did we get a CNAME or DNAME?
  2503. */
  2504. if (result == DNS_R_ALIAS) {
  2505. DP(DEF_LEVEL,
  2506. "dns_adb_createfind: name %p is an alias",
  2507. adbname);
  2508. alias = ISC_TRUE;
  2509. goto post_copy;
  2510. }
  2511. /*
  2512. * If the name doesn't exist at all, don't bother with
  2513. * v6 queries; they won't work.
  2514. *
  2515. * If the name does exist but we didn't get our data, go
  2516. * ahead and try AAAA.
  2517. *
  2518. * If the result is neither of these, try a fetch for A.
  2519. */
  2520. if (NXDOMAIN_RESULT(result))
  2521. goto fetch;
  2522. else if (NXRRSET_RESULT(result))
  2523. goto v6;
  2524. if (!NAME_FETCH_V4(adbname))
  2525. wanted_fetches |= DNS_ADBFIND_INET;
  2526. }
  2527. v6:
  2528. if (!NAME_HAS_V6(adbname) && EXPIRE_OK(adbname->expire_v6, now)
  2529. && WANT_INET6(wanted_addresses)) {
  2530. result = dbfind_name(adbname, now, dns_rdatatype_aaaa);
  2531. if (result == ISC_R_SUCCESS) {
  2532. DP(DEF_LEVEL,
  2533. "dns_adb_createfind: found AAAA for name %p",
  2534. adbname);
  2535. goto fetch;
  2536. }
  2537. /*
  2538. * Did we get a CNAME or DNAME?
  2539. */
  2540. if (result == DNS_R_ALIAS) {
  2541. DP(DEF_LEVEL,
  2542. "dns_adb_createfind: name %p is an alias",
  2543. adbname);
  2544. alias = ISC_TRUE;
  2545. goto post_copy;
  2546. }
  2547. /*
  2548. * Listen to negative cache hints, and don't start
  2549. * another query.
  2550. */
  2551. if (NCACHE_RESULT(result) || AUTH_NX(result))
  2552. goto fetch;
  2553. if (!NAME_FETCH_V6(adbname))
  2554. wanted_fetches |= DNS_ADBFIND_INET6;
  2555. }
  2556. fetch:
  2557. if ((WANT_INET(wanted_addresses) && NAME_HAS_V4(adbname)) ||
  2558. (WANT_INET6(wanted_addresses) && NAME_HAS_V6(adbname)))
  2559. have_address = ISC_TRUE;
  2560. else
  2561. have_address = ISC_FALSE;
  2562. if (wanted_fetches != 0 &&
  2563. ! (FIND_AVOIDFETCHES(find) && have_address)) {
  2564. /*
  2565. * We're missing at least one address family. Either the
  2566. * caller hasn't instructed us to avoid fetches, or we don't
  2567. * know anything about any of the address families that would
  2568. * be acceptable so we have to launch fetches.
  2569. */
  2570. if (FIND_STARTATZONE(find))
  2571. start_at_zone = ISC_TRUE;
  2572. /*
  2573. * Start V4.
  2574. */
  2575. if (WANT_INET(wanted_fetches) &&
  2576. fetch_name(adbname, start_at_zone,
  2577. dns_rdatatype_a) == ISC_R_SUCCESS) {
  2578. DP(DEF_LEVEL,
  2579. "dns_adb_createfind: started A fetch for name %p",
  2580. adbname);
  2581. }
  2582. /*
  2583. * Start V6.
  2584. */
  2585. if (WANT_INET6(wanted_fetches) &&
  2586. fetch_name(adbname, start_at_zone,
  2587. dns_rdatatype_aaaa) == ISC_R_SUCCESS) {
  2588. DP(DEF_LEVEL,
  2589. "dns_adb_createfind: "
  2590. "started AAAA fetch for name %p",
  2591. adbname);
  2592. }
  2593. }
  2594. /*
  2595. * Run through the name and copy out the bits we are
  2596. * interested in.
  2597. */
  2598. copy_namehook_lists(adb, find, qname, qtype, adbname, now);
  2599. post_copy:
  2600. if (NAME_FETCH_V4(adbname))
  2601. query_pending |= DNS_ADBFIND_INET;
  2602. if (NAME_FETCH_V6(adbname))
  2603. query_pending |= DNS_ADBFIND_INET6;
  2604. /*
  2605. * Attach to the name's query list if there are queries
  2606. * already running, and we have been asked to.
  2607. */
  2608. want_event = ISC_TRUE;
  2609. if (!FIND_WANTEVENT(find))
  2610. want_event = ISC_FALSE;
  2611. if (FIND_WANTEMPTYEVENT(find) && FIND_HAS_ADDRS(find))
  2612. want_event = ISC_FALSE;
  2613. if ((wanted_addresses & query_pending) == 0)
  2614. want_event = ISC_FALSE;
  2615. if (alias)
  2616. want_event = ISC_FALSE;
  2617. if (want_event) {
  2618. find->adbname = adbname;
  2619. find->name_bucket = bucket;
  2620. ISC_LIST_APPEND(adbname->finds, find, plink);
  2621. find->query_pending = (query_pending & wanted_addresses);
  2622. find->flags &= ~DNS_ADBFIND_ADDRESSMASK;
  2623. find->flags |= (find->query_pending & DNS_ADBFIND_ADDRESSMASK);
  2624. DP(DEF_LEVEL, "createfind: attaching find %p to adbname %p",
  2625. find, adbname);
  2626. } else {
  2627. /*
  2628. * Remove the flag so the caller knows there will never
  2629. * be an event, and set internal flags to fake that
  2630. * the event was sent and freed, so dns_adb_destroyfind() will
  2631. * do the right thing.
  2632. */
  2633. find->query_pending = (query_pending & wanted_addresses);
  2634. find->options &= ~DNS_ADBFIND_WANTEVENT;
  2635. find->flags |= (FIND_EVENT_SENT | FIND_EVENT_FREED);
  2636. find->flags &= ~DNS_ADBFIND_ADDRESSMASK;
  2637. }
  2638. find->partial_result |= (adbname->partial_result & wanted_addresses);
  2639. if (alias) {
  2640. if (target != NULL) {
  2641. result = dns_name_copy(&adbname->target, target, NULL);
  2642. if (result != ISC_R_SUCCESS)
  2643. goto out;
  2644. }
  2645. result = DNS_R_ALIAS;
  2646. } else
  2647. result = ISC_R_SUCCESS;
  2648. /*
  2649. * Copy out error flags from the name structure into the find.
  2650. */
  2651. find->result_v4 = find_err_map[adbname->fetch_err];
  2652. find->result_v6 = find_err_map[adbname->fetch6_err];
  2653. out:
  2654. if (find != NULL) {
  2655. *findp = find;
  2656. if (want_event) {
  2657. isc_task_t *taskp;
  2658. INSIST((find->flags & DNS_ADBFIND_ADDRESSMASK) != 0);
  2659. taskp = NULL;
  2660. isc_task_attach(task, &taskp);
  2661. find->event.ev_sender = taskp;
  2662. find->event.ev_action = action;
  2663. find->event.ev_arg = arg;
  2664. }
  2665. }
  2666. UNLOCK(&adb->namelocks[bucket]);
  2667. return (result);
  2668. }
  2669. void
  2670. dns_adb_destroyfind(dns_adbfind_t **findp) {
  2671. dns_adbfind_t *find;
  2672. dns_adbentry_t *entry;
  2673. dns_adbaddrinfo_t *ai;
  2674. int bucket;
  2675. dns_adb_t *adb;
  2676. isc_boolean_t overmem;
  2677. REQUIRE(findp != NULL && DNS_ADBFIND_VALID(*findp));
  2678. find = *findp;
  2679. *findp = NULL;
  2680. LOCK(&find->lock);
  2681. DP(DEF_LEVEL, "dns_adb_destroyfind on find %p", find);
  2682. adb = find->adb;
  2683. REQUIRE(DNS_ADB_VALID(adb));
  2684. REQUIRE(FIND_EVENTFREED(find));
  2685. bucket = find->name_bucket;
  2686. INSIST(bucket == DNS_ADB_INVALIDBUCKET);
  2687. UNLOCK(&find->lock);
  2688. /*
  2689. * The find doesn't exist on any list, and nothing is locked.
  2690. * Return the find to the memory pool, and decrement the adb's
  2691. * reference count.
  2692. */
  2693. overmem = isc_mem_isovermem(adb->mctx);
  2694. ai = ISC_LIST_HEAD(find->list);
  2695. while (ai != NULL) {
  2696. ISC_LIST_UNLINK(find->list, ai, publink);
  2697. entry = ai->entry;
  2698. ai->entry = NULL;
  2699. INSIST(DNS_ADBENTRY_VALID(entry));
  2700. RUNTIME_CHECK(dec_entry_refcnt(adb, overmem, entry, ISC_TRUE) ==
  2701. ISC_FALSE);
  2702. free_adbaddrinfo(adb, &ai);
  2703. ai = ISC_LIST_HEAD(find->list);
  2704. }
  2705. /*
  2706. * WARNING: The find is freed with the adb locked. This is done
  2707. * to avoid a race condition where we free the find, some other
  2708. * thread tests to see if it should be destroyed, detects it should
  2709. * be, destroys it, and then we try to lock it for our check, but the
  2710. * lock is destroyed.
  2711. */
  2712. LOCK(&adb->lock);
  2713. if (free_adbfind(adb, &find))
  2714. check_exit(adb);
  2715. UNLOCK(&adb->lock);
  2716. }
  2717. void
  2718. dns_adb_cancelfind(dns_adbfind_t *find) {
  2719. isc_event_t *ev;
  2720. isc_task_t *task;
  2721. dns_adb_t *adb;
  2722. int bucket;
  2723. int unlock_bucket;
  2724. LOCK(&find->lock);
  2725. DP(DEF_LEVEL, "dns_adb_cancelfind on find %p", find);
  2726. adb = find->adb;
  2727. REQUIRE(DNS_ADB_VALID(adb));
  2728. REQUIRE(!FIND_EVENTFREED(find));
  2729. REQUIRE(FIND_WANTEVENT(find));
  2730. bucket = find->name_bucket;
  2731. if (bucket == DNS_ADB_INVALIDBUCKET)
  2732. goto cleanup;
  2733. /*
  2734. * We need to get the adbname's lock to unlink the find.
  2735. */
  2736. unlock_bucket = bucket;
  2737. violate_locking_hierarchy(&find->lock, &adb->namelocks[unlock_bucket]);
  2738. bucket = find->name_bucket;
  2739. if (bucket != DNS_ADB_INVALIDBUCKET) {
  2740. ISC_LIST_UNLINK(find->adbname->finds, find, plink);
  2741. find->adbname = NULL;
  2742. find->name_bucket = DNS_ADB_INVALIDBUCKET;
  2743. }
  2744. UNLOCK(&adb->namelocks[unlock_bucket]);
  2745. bucket = DNS_ADB_INVALIDBUCKET;
  2746. POST(bucket);
  2747. cleanup:
  2748. if (!FIND_EVENTSENT(find)) {
  2749. ev = &find->event;
  2750. task = ev->ev_sender;
  2751. ev->ev_sender = find;
  2752. ev->ev_type = DNS_EVENT_ADBCANCELED;
  2753. ev->ev_destroy = event_free;
  2754. ev->ev_destroy_arg = find;
  2755. find->result_v4 = ISC_R_CANCELED;
  2756. find->result_v6 = ISC_R_CANCELED;
  2757. DP(DEF_LEVEL, "sending event %p to task %p for find %p",
  2758. ev, task, find);
  2759. isc_task_sendanddetach(&task, (isc_event_t **)&ev);
  2760. }
  2761. UNLOCK(&find->lock);
  2762. }
  2763. void
  2764. dns_adb_dump(dns_adb_t *adb, FILE *f) {
  2765. unsigned int i;
  2766. isc_stdtime_t now;
  2767. REQUIRE(DNS_ADB_VALID(adb));
  2768. REQUIRE(f != NULL);
  2769. /*
  2770. * Lock the adb itself, lock all the name buckets, then lock all
  2771. * the entry buckets. This should put the adb into a state where
  2772. * nothing can change, so we can iterate through everything and
  2773. * print at our leisure.
  2774. */
  2775. LOCK(&adb->lock);
  2776. isc_stdtime_get(&now);
  2777. for (i = 0; i < adb->nnames; i++)
  2778. RUNTIME_CHECK(cleanup_names(adb, i, now) == ISC_FALSE);
  2779. for (i = 0; i < adb->nentries; i++)
  2780. RUNTIME_CHECK(cleanup_entries(adb, i, now) == ISC_FALSE);
  2781. dump_adb(adb, f, ISC_FALSE, now);
  2782. UNLOCK(&adb->lock);
  2783. }
  2784. static void
  2785. dump_ttl(FILE *f, const char *legend, isc_stdtime_t value, isc_stdtime_t now) {
  2786. if (value == INT_MAX)
  2787. return;
  2788. fprintf(f, " [%s TTL %d]", legend, value - now);
  2789. }
  2790. static void
  2791. dump_adb(dns_adb_t *adb, FILE *f, isc_boolean_t debug, isc_stdtime_t now) {
  2792. unsigned int i;
  2793. dns_adbname_t *name;
  2794. dns_adbentry_t *entry;
  2795. fprintf(f, ";\n; Address database dump\n;\n");
  2796. if (debug)
  2797. fprintf(f, "; addr %p, erefcnt %u, irefcnt %u, finds out %u\n",
  2798. adb, adb->erefcnt, adb->irefcnt,
  2799. isc_mempool_getallocated(adb->nhmp));
  2800. for (i = 0; i < adb->nnames; i++)
  2801. LOCK(&adb->namelocks[i]);
  2802. for (i = 0; i < adb->nentries; i++)
  2803. LOCK(&adb->entrylocks[i]);
  2804. /*
  2805. * Dump the names
  2806. */
  2807. for (i = 0; i < adb->nnames; i++) {
  2808. name = ISC_LIST_HEAD(adb->names[i]);
  2809. if (name == NULL)
  2810. continue;
  2811. if (debug)
  2812. fprintf(f, "; bucket %d\n", i);
  2813. for (;
  2814. name != NULL;
  2815. name = ISC_LIST_NEXT(name, plink))
  2816. {
  2817. if (debug)
  2818. fprintf(f, "; name %p (flags %08x)\n",
  2819. name, name->flags);
  2820. fprintf(f, "; ");
  2821. print_dns_name(f, &name->name);
  2822. if (dns_name_countlabels(&name->target) > 0) {
  2823. fprintf(f, " alias ");
  2824. print_dns_name(f, &name->target);
  2825. }
  2826. dump_ttl(f, "v4", name->expire_v4, now);
  2827. dump_ttl(f, "v6", name->expire_v6, now);
  2828. dump_ttl(f, "target", name->expire_target, now);
  2829. fprintf(f, " [v4 %s] [v6 %s]",
  2830. errnames[name->fetch_err],
  2831. errnames[name->fetch6_err]);
  2832. fprintf(f, "\n");
  2833. print_namehook_list(f, "v4", &name->v4, debug, now);
  2834. print_namehook_list(f, "v6", &name->v6, debug, now);
  2835. if (debug)
  2836. print_fetch_list(f, name);
  2837. if (debug)
  2838. print_find_list(f, name);
  2839. }
  2840. }
  2841. fprintf(f, ";\n; Unassociated entries\n;\n");
  2842. for (i = 0; i < adb->nentries; i++) {
  2843. entry = ISC_LIST_HEAD(adb->entries[i]);
  2844. while (entry != NULL) {
  2845. if (entry->refcnt == 0)
  2846. dump_entry(f, entry, debug, now);
  2847. entry = ISC_LIST_NEXT(entry, plink);
  2848. }
  2849. }
  2850. /*
  2851. * Unlock everything
  2852. */
  2853. for (i = 0; i < adb->nentries; i++)
  2854. UNLOCK(&adb->entrylocks[i]);
  2855. for (i = 0; i < adb->nnames; i++)
  2856. UNLOCK(&adb->namelocks[i]);
  2857. }
  2858. static void
  2859. dump_entry(FILE *f, dns_adbentry_t *entry, isc_boolean_t debug,
  2860. isc_stdtime_t now)
  2861. {
  2862. char addrbuf[ISC_NETADDR_FORMATSIZE];
  2863. char typebuf[DNS_RDATATYPE_FORMATSIZE];
  2864. isc_netaddr_t netaddr;
  2865. dns_adblameinfo_t *li;
  2866. isc_netaddr_fromsockaddr(&netaddr, &entry->sockaddr);
  2867. isc_netaddr_format(&netaddr, addrbuf, sizeof(addrbuf));
  2868. if (debug)
  2869. fprintf(f, ";\t%p: refcnt %u\n", entry, entry->refcnt);
  2870. fprintf(f, ";\t%s [srtt %u] [flags %08x]",
  2871. addrbuf, entry->srtt, entry->flags);
  2872. if (entry->expires != 0)
  2873. fprintf(f, " [ttl %d]", entry->expires - now);
  2874. fprintf(f, "\n");
  2875. for (li = ISC_LIST_HEAD(entry->lameinfo);
  2876. li != NULL;
  2877. li = ISC_LIST_NEXT(li, plink)) {
  2878. fprintf(f, ";\t\t");
  2879. print_dns_name(f, &li->qname);
  2880. dns_rdatatype_format(li->qtype, typebuf, sizeof(typebuf));
  2881. fprintf(f, " %s [lame TTL %d]\n", typebuf,
  2882. li->lame_timer - now);
  2883. }
  2884. }
  2885. void
  2886. dns_adb_dumpfind(dns_adbfind_t *find, FILE *f) {
  2887. char tmp[512];
  2888. const char *tmpp;
  2889. dns_adbaddrinfo_t *ai;
  2890. isc_sockaddr_t *sa;
  2891. /*
  2892. * Not used currently, in the API Just In Case we
  2893. * want to dump out the name and/or entries too.
  2894. */
  2895. LOCK(&find->lock);
  2896. fprintf(f, ";Find %p\n", find);
  2897. fprintf(f, ";\tqpending %08x partial %08x options %08x flags %08x\n",
  2898. find->query_pending, find->partial_result,
  2899. find->options, find->flags);
  2900. fprintf(f, ";\tname_bucket %d, name %p, event sender %p\n",
  2901. find->name_bucket, find->adbname, find->event.ev_sender);
  2902. ai = ISC_LIST_HEAD(find->list);
  2903. if (ai != NULL)
  2904. fprintf(f, "\tAddresses:\n");
  2905. while (ai != NULL) {
  2906. sa = &ai->sockaddr;
  2907. switch (sa->type.sa.sa_family) {
  2908. case AF_INET:
  2909. tmpp = inet_ntop(AF_INET, &sa->type.sin.sin_addr,
  2910. tmp, sizeof(tmp));
  2911. break;
  2912. case AF_INET6:
  2913. tmpp = inet_ntop(AF_INET6, &sa->type.sin6.sin6_addr,
  2914. tmp, sizeof(tmp));
  2915. break;
  2916. default:
  2917. tmpp = "UnkFamily";
  2918. }
  2919. if (tmpp == NULL)
  2920. tmpp = "BadAddress";
  2921. fprintf(f, "\t\tentry %p, flags %08x"
  2922. " srtt %u addr %s\n",
  2923. ai->entry, ai->flags, ai->srtt, tmpp);
  2924. ai = ISC_LIST_NEXT(ai, publink);
  2925. }
  2926. UNLOCK(&find->lock);
  2927. }
  2928. static void
  2929. print_dns_name(FILE *f, dns_name_t *name) {
  2930. char buf[DNS_NAME_FORMATSIZE];
  2931. INSIST(f != NULL);
  2932. dns_name_format(name, buf, sizeof(buf));
  2933. fprintf(f, "%s", buf);
  2934. }
  2935. static void
  2936. print_namehook_list(FILE *f, const char *legend, dns_adbnamehooklist_t *list,
  2937. isc_boolean_t debug, isc_stdtime_t now)
  2938. {
  2939. dns_adbnamehook_t *nh;
  2940. for (nh = ISC_LIST_HEAD(*list);
  2941. nh != NULL;
  2942. nh = ISC_LIST_NEXT(nh, plink))
  2943. {
  2944. if (debug)
  2945. fprintf(f, ";\tHook(%s) %p\n", legend, nh);
  2946. dump_entry(f, nh->entry, debug, now);
  2947. }
  2948. }
  2949. static inline void
  2950. print_fetch(FILE *f, dns_adbfetch_t *ft, const char *type) {
  2951. fprintf(f, "\t\tFetch(%s): %p -> { fetch %p }\n",
  2952. type, ft, ft->fetch);
  2953. }
  2954. static void
  2955. print_fetch_list(FILE *f, dns_adbname_t *n) {
  2956. if (NAME_FETCH_A(n))
  2957. print_fetch(f, n->fetch_a, "A");
  2958. if (NAME_FETCH_AAAA(n))
  2959. print_fetch(f, n->fetch_aaaa, "AAAA");
  2960. }
  2961. static void
  2962. print_find_list(FILE *f, dns_adbname_t *name) {
  2963. dns_adbfind_t *find;
  2964. find = ISC_LIST_HEAD(name->finds);
  2965. while (find != NULL) {
  2966. dns_adb_dumpfind(find, f);
  2967. find = ISC_LIST_NEXT(find, plink);
  2968. }
  2969. }
  2970. static isc_result_t
  2971. dbfind_name(dns_adbname_t *adbname, isc_stdtime_t now, dns_rdatatype_t rdtype)
  2972. {
  2973. isc_result_t result;
  2974. dns_rdataset_t rdataset;
  2975. dns_adb_t *adb;
  2976. dns_fixedname_t foundname;
  2977. dns_name_t *fname;
  2978. INSIST(DNS_ADBNAME_VALID(adbname));
  2979. adb = adbname->adb;
  2980. INSIST(DNS_ADB_VALID(adb));
  2981. INSIST(rdtype == dns_rdatatype_a || rdtype == dns_rdatatype_aaaa);
  2982. dns_fixedname_init(&foundname);
  2983. fname = dns_fixedname_name(&foundname);
  2984. dns_rdataset_init(&rdataset);
  2985. if (rdtype == dns_rdatatype_a)
  2986. adbname->fetch_err = FIND_ERR_UNEXPECTED;
  2987. else
  2988. adbname->fetch6_err = FIND_ERR_UNEXPECTED;
  2989. /*
  2990. * We need to specify whether to search static-stub zones (if
  2991. * configured) depending on whether this is a "start at zone" lookup,
  2992. * i.e., whether it's a "bailiwick" glue. If it's bailiwick (in which
  2993. * case NAME_STARTATZONE is set) we need to stop the search at any
  2994. * matching static-stub zone without looking into the cache to honor
  2995. * the configuration on which server we should send queries to.
  2996. */
  2997. result = dns_view_find2(adb->view, &adbname->name, rdtype, now,
  2998. NAME_GLUEOK(adbname) ? DNS_DBFIND_GLUEOK : 0,
  2999. ISC_TF(NAME_HINTOK(adbname)),
  3000. (adbname->flags & NAME_STARTATZONE) != 0 ?
  3001. ISC_TRUE : ISC_FALSE,
  3002. NULL, NULL, fname, &rdataset, NULL);
  3003. /* XXXVIX this switch statement is too sparse to gen a jump table. */
  3004. switch (result) {
  3005. case DNS_R_GLUE:
  3006. case DNS_R_HINT:
  3007. case ISC_R_SUCCESS:
  3008. /*
  3009. * Found in the database. Even if we can't copy out
  3010. * any information, return success, or else a fetch
  3011. * will be made, which will only make things worse.
  3012. */
  3013. if (rdtype == dns_rdatatype_a)
  3014. adbname->fetch_err = FIND_ERR_SUCCESS;
  3015. else
  3016. adbname->fetch6_err = FIND_ERR_SUCCESS;
  3017. result = import_rdataset(adbname, &rdataset, now);
  3018. break;
  3019. case DNS_R_NXDOMAIN:
  3020. case DNS_R_NXRRSET:
  3021. /*
  3022. * We're authoritative and the data doesn't exist.
  3023. * Make up a negative cache entry so we don't ask again
  3024. * for a while.
  3025. *
  3026. * XXXRTH What time should we use? I'm putting in 30 seconds
  3027. * for now.
  3028. */
  3029. if (rdtype == dns_rdatatype_a) {
  3030. adbname->expire_v4 = now + 30;
  3031. DP(NCACHE_LEVEL,
  3032. "adb name %p: Caching auth negative entry for A",
  3033. adbname);
  3034. if (result == DNS_R_NXDOMAIN)
  3035. adbname->fetch_err = FIND_ERR_NXDOMAIN;
  3036. else
  3037. adbname->fetch_err = FIND_ERR_NXRRSET;
  3038. } else {
  3039. DP(NCACHE_LEVEL,
  3040. "adb name %p: Caching auth negative entry for AAAA",
  3041. adbname);
  3042. adbname->expire_v6 = now + 30;
  3043. if (result == DNS_R_NXDOMAIN)
  3044. adbname->fetch6_err = FIND_ERR_NXDOMAIN;
  3045. else
  3046. adbname->fetch6_err = FIND_ERR_NXRRSET;
  3047. }
  3048. break;
  3049. case DNS_R_NCACHENXDOMAIN:
  3050. case DNS_R_NCACHENXRRSET:
  3051. /*
  3052. * We found a negative cache entry. Pull the TTL from it
  3053. * so we won't ask again for a while.
  3054. */
  3055. rdataset.ttl = ttlclamp(rdataset.ttl);
  3056. if (rdtype == dns_rdatatype_a) {
  3057. adbname->expire_v4 = rdataset.ttl + now;
  3058. if (result == DNS_R_NCACHENXDOMAIN)
  3059. adbname->fetch_err = FIND_ERR_NXDOMAIN;
  3060. else
  3061. adbname->fetch_err = FIND_ERR_NXRRSET;
  3062. DP(NCACHE_LEVEL,
  3063. "adb name %p: Caching negative entry for A (ttl %u)",
  3064. adbname, rdataset.ttl);
  3065. } else {
  3066. DP(NCACHE_LEVEL,
  3067. "adb name %p: Caching negative entry for AAAA (ttl %u)",
  3068. adbname, rdataset.ttl);
  3069. adbname->expire_v6 = rdataset.ttl + now;
  3070. if (result == DNS_R_NCACHENXDOMAIN)
  3071. adbname->fetch6_err = FIND_ERR_NXDOMAIN;
  3072. else
  3073. adbname->fetch6_err = FIND_ERR_NXRRSET;
  3074. }
  3075. break;
  3076. case DNS_R_CNAME:
  3077. case DNS_R_DNAME:
  3078. /*
  3079. * Clear the hint and glue flags, so this will match
  3080. * more often.
  3081. */
  3082. adbname->flags &= ~(DNS_ADBFIND_GLUEOK | DNS_ADBFIND_HINTOK);
  3083. rdataset.ttl = ttlclamp(rdataset.ttl);
  3084. clean_target(adb, &adbname->target);
  3085. adbname->expire_target = INT_MAX;
  3086. result = set_target(adb, &adbname->name, fname, &rdataset,
  3087. &adbname->target);
  3088. if (result == ISC_R_SUCCESS) {
  3089. result = DNS_R_ALIAS;
  3090. DP(NCACHE_LEVEL,
  3091. "adb name %p: caching alias target",
  3092. adbname);
  3093. adbname->expire_target = rdataset.ttl + now;
  3094. }
  3095. if (rdtype == dns_rdatatype_a)
  3096. adbname->fetch_err = FIND_ERR_SUCCESS;
  3097. else
  3098. adbname->fetch6_err = FIND_ERR_SUCCESS;
  3099. break;
  3100. }
  3101. if (dns_rdataset_isassociated(&rdataset))
  3102. dns_rdataset_disassociate(&rdataset);
  3103. return (result);
  3104. }
  3105. static void
  3106. fetch_callback(isc_task_t *task, isc_event_t *ev) {
  3107. dns_fetchevent_t *dev;
  3108. dns_adbname_t *name;
  3109. dns_adb_t *adb;
  3110. dns_adbfetch_t *fetch;
  3111. int bucket;
  3112. isc_eventtype_t ev_status;
  3113. isc_stdtime_t now;
  3114. isc_result_t result;
  3115. unsigned int address_type;
  3116. isc_boolean_t want_check_exit = ISC_FALSE;
  3117. UNUSED(task);
  3118. INSIST(ev->ev_type == DNS_EVENT_FETCHDONE);
  3119. dev = (dns_fetchevent_t *)ev;
  3120. name = ev->ev_arg;
  3121. INSIST(DNS_ADBNAME_VALID(name));
  3122. adb = name->adb;
  3123. INSIST(DNS_ADB_VALID(adb));
  3124. bucket = name->lock_bucket;
  3125. LOCK(&adb->namelocks[bucket]);
  3126. INSIST(NAME_FETCH_A(name) || NAME_FETCH_AAAA(name));
  3127. address_type = 0;
  3128. if (NAME_FETCH_A(name) && (name->fetch_a->fetch == dev->fetch)) {
  3129. address_type = DNS_ADBFIND_INET;
  3130. fetch = name->fetch_a;
  3131. name->fetch_a = NULL;
  3132. } else if (NAME_FETCH_AAAA(name)
  3133. && (name->fetch_aaaa->fetch == dev->fetch)) {
  3134. address_type = DNS_ADBFIND_INET6;
  3135. fetch = name->fetch_aaaa;
  3136. name->fetch_aaaa = NULL;
  3137. } else
  3138. fetch = NULL;
  3139. INSIST(address_type != 0 && fetch != NULL);
  3140. dns_resolver_destroyfetch(&fetch->fetch);
  3141. dev->fetch = NULL;
  3142. ev_status = DNS_EVENT_ADBNOMOREADDRESSES;
  3143. /*
  3144. * Cleanup things we don't care about.
  3145. */
  3146. if (dev->node != NULL)
  3147. dns_db_detachnode(dev->db, &dev->node);
  3148. if (dev->db != NULL)
  3149. dns_db_detach(&dev->db);
  3150. /*
  3151. * If this name is marked as dead, clean up, throwing away
  3152. * potentially good data.
  3153. */
  3154. if (NAME_DEAD(name)) {
  3155. free_adbfetch(adb, &fetch);
  3156. isc_event_free(&ev);
  3157. want_check_exit = kill_name(&name, DNS_EVENT_ADBCANCELED);
  3158. UNLOCK(&adb->namelocks[bucket]);
  3159. if (want_check_exit) {
  3160. LOCK(&adb->lock);
  3161. check_exit(adb);
  3162. UNLOCK(&adb->lock);
  3163. }
  3164. return;
  3165. }
  3166. isc_stdtime_get(&now);
  3167. /*
  3168. * If we got a negative cache response, remember it.
  3169. */
  3170. if (NCACHE_RESULT(dev->result)) {
  3171. dev->rdataset->ttl = ttlclamp(dev->rdataset->ttl);
  3172. if (address_type == DNS_ADBFIND_INET) {
  3173. DP(NCACHE_LEVEL, "adb fetch name %p: "
  3174. "caching negative entry for A (ttl %u)",
  3175. name, dev->rdataset->ttl);
  3176. name->expire_v4 = ISC_MIN(name->expire_v4,
  3177. dev->rdataset->ttl + now);
  3178. if (dev->result == DNS_R_NCACHENXDOMAIN)
  3179. name->fetch_err = FIND_ERR_NXDOMAIN;
  3180. else
  3181. name->fetch_err = FIND_ERR_NXRRSET;
  3182. inc_stats(adb, dns_resstatscounter_gluefetchv4fail);
  3183. } else {
  3184. DP(NCACHE_LEVEL, "adb fetch name %p: "
  3185. "caching negative entry for AAAA (ttl %u)",
  3186. name, dev->rdataset->ttl);
  3187. name->expire_v6 = ISC_MIN(name->expire_v6,
  3188. dev->rdataset->ttl + now);
  3189. if (dev->result == DNS_R_NCACHENXDOMAIN)
  3190. name->fetch6_err = FIND_ERR_NXDOMAIN;
  3191. else
  3192. name->fetch6_err = FIND_ERR_NXRRSET;
  3193. inc_stats(adb, dns_resstatscounter_gluefetchv6fail);
  3194. }
  3195. goto out;
  3196. }
  3197. /*
  3198. * Handle CNAME/DNAME.
  3199. */
  3200. if (dev->result == DNS_R_CNAME || dev->result == DNS_R_DNAME) {
  3201. dev->rdataset->ttl = ttlclamp(dev->rdataset->ttl);
  3202. clean_target(adb, &name->target);
  3203. name->expire_target = INT_MAX;
  3204. result = set_target(adb, &name->name,
  3205. dns_fixedname_name(&dev->foundname),
  3206. dev->rdataset,
  3207. &name->target);
  3208. if (result == ISC_R_SUCCESS) {
  3209. DP(NCACHE_LEVEL,
  3210. "adb fetch name %p: caching alias target",
  3211. name);
  3212. name->expire_target = dev->rdataset->ttl + now;
  3213. }
  3214. goto check_result;
  3215. }
  3216. /*
  3217. * Did we get back junk? If so, and there are no more fetches
  3218. * sitting out there, tell all the finds about it.
  3219. */
  3220. if (dev->result != ISC_R_SUCCESS) {
  3221. char buf[DNS_NAME_FORMATSIZE];
  3222. dns_name_format(&name->name, buf, sizeof(buf));
  3223. DP(DEF_LEVEL, "adb: fetch of '%s' %s failed: %s",
  3224. buf, address_type == DNS_ADBFIND_INET ? "A" : "AAAA",
  3225. dns_result_totext(dev->result));
  3226. /* XXXMLG Don't pound on bad servers. */
  3227. if (address_type == DNS_ADBFIND_INET) {
  3228. name->expire_v4 = ISC_MIN(name->expire_v4, now + 300);
  3229. name->fetch_err = FIND_ERR_FAILURE;
  3230. inc_stats(adb, dns_resstatscounter_gluefetchv4fail);
  3231. } else {
  3232. name->expire_v6 = ISC_MIN(name->expire_v6, now + 300);
  3233. name->fetch6_err = FIND_ERR_FAILURE;
  3234. inc_stats(adb, dns_resstatscounter_gluefetchv6fail);
  3235. }
  3236. goto out;
  3237. }
  3238. /*
  3239. * We got something potentially useful.
  3240. */
  3241. result = import_rdataset(name, &fetch->rdataset, now);
  3242. check_result:
  3243. if (result == ISC_R_SUCCESS) {
  3244. ev_status = DNS_EVENT_ADBMOREADDRESSES;
  3245. if (address_type == DNS_ADBFIND_INET)
  3246. name->fetch_err = FIND_ERR_SUCCESS;
  3247. else
  3248. name->fetch6_err = FIND_ERR_SUCCESS;
  3249. }
  3250. out:
  3251. free_adbfetch(adb, &fetch);
  3252. isc_event_free(&ev);
  3253. clean_finds_at_name(name, ev_status, address_type);
  3254. UNLOCK(&adb->namelocks[bucket]);
  3255. }
  3256. static isc_result_t
  3257. fetch_name(dns_adbname_t *adbname,
  3258. isc_boolean_t start_at_zone,
  3259. dns_rdatatype_t type)
  3260. {
  3261. isc_result_t result;
  3262. dns_adbfetch_t *fetch = NULL;
  3263. dns_adb_t *adb;
  3264. dns_fixedname_t fixed;
  3265. dns_name_t *name;
  3266. dns_rdataset_t rdataset;
  3267. dns_rdataset_t *nameservers;
  3268. unsigned int options;
  3269. INSIST(DNS_ADBNAME_VALID(adbname));
  3270. adb = adbname->adb;
  3271. INSIST(DNS_ADB_VALID(adb));
  3272. INSIST((type == dns_rdatatype_a && !NAME_FETCH_V4(adbname)) ||
  3273. (type == dns_rdatatype_aaaa && !NAME_FETCH_V6(adbname)));
  3274. adbname->fetch_err = FIND_ERR_NOTFOUND;
  3275. name = NULL;
  3276. nameservers = NULL;
  3277. dns_rdataset_init(&rdataset);
  3278. options = DNS_FETCHOPT_NOVALIDATE;
  3279. if (start_at_zone) {
  3280. DP(ENTER_LEVEL,
  3281. "fetch_name: starting at zone for name %p",
  3282. adbname);
  3283. dns_fixedname_init(&fixed);
  3284. name = dns_fixedname_name(&fixed);
  3285. result = dns_view_findzonecut2(adb->view, &adbname->name, name,
  3286. 0, 0, ISC_TRUE, ISC_FALSE,
  3287. &rdataset, NULL);
  3288. if (result != ISC_R_SUCCESS && result != DNS_R_HINT)
  3289. goto cleanup;
  3290. nameservers = &rdataset;
  3291. options |= DNS_FETCHOPT_UNSHARED;
  3292. }
  3293. fetch = new_adbfetch(adb);
  3294. if (fetch == NULL) {
  3295. result = ISC_R_NOMEMORY;
  3296. goto cleanup;
  3297. }
  3298. result = dns_resolver_createfetch(adb->view->resolver, &adbname->name,
  3299. type, name, nameservers, NULL,
  3300. options, adb->task, fetch_callback,
  3301. adbname, &fetch->rdataset, NULL,
  3302. &fetch->fetch);
  3303. if (result != ISC_R_SUCCESS)
  3304. goto cleanup;
  3305. if (type == dns_rdatatype_a) {
  3306. adbname->fetch_a = fetch;
  3307. inc_stats(adb, dns_resstatscounter_gluefetchv4);
  3308. } else {
  3309. adbname->fetch_aaaa = fetch;
  3310. inc_stats(adb, dns_resstatscounter_gluefetchv6);
  3311. }
  3312. fetch = NULL; /* Keep us from cleaning this up below. */
  3313. cleanup:
  3314. if (fetch != NULL)
  3315. free_adbfetch(adb, &fetch);
  3316. if (dns_rdataset_isassociated(&rdataset))
  3317. dns_rdataset_disassociate(&rdataset);
  3318. return (result);
  3319. }
  3320. /*
  3321. * XXXMLG Needs to take a find argument and an address info, no zone or adb,
  3322. * since these can be extracted from the find itself.
  3323. */
  3324. isc_result_t
  3325. dns_adb_marklame(dns_adb_t *adb, dns_adbaddrinfo_t *addr, dns_name_t *qname,
  3326. dns_rdatatype_t qtype, isc_stdtime_t expire_time)
  3327. {
  3328. dns_adblameinfo_t *li;
  3329. int bucket;
  3330. isc_result_t result = ISC_R_SUCCESS;
  3331. REQUIRE(DNS_ADB_VALID(adb));
  3332. REQUIRE(DNS_ADBADDRINFO_VALID(addr));
  3333. REQUIRE(qname != NULL);
  3334. bucket = addr->entry->lock_bucket;
  3335. LOCK(&adb->entrylocks[bucket]);
  3336. li = ISC_LIST_HEAD(addr->entry->lameinfo);
  3337. while (li != NULL &&
  3338. (li->qtype != qtype || !dns_name_equal(qname, &li->qname)))
  3339. li = ISC_LIST_NEXT(li, plink);
  3340. if (li != NULL) {
  3341. if (expire_time > li->lame_timer)
  3342. li->lame_timer = expire_time;
  3343. goto unlock;
  3344. }
  3345. li = new_adblameinfo(adb, qname, qtype);
  3346. if (li == NULL) {
  3347. result = ISC_R_NOMEMORY;
  3348. goto unlock;
  3349. }
  3350. li->lame_timer = expire_time;
  3351. ISC_LIST_PREPEND(addr->entry->lameinfo, li, plink);
  3352. unlock:
  3353. UNLOCK(&adb->entrylocks[bucket]);
  3354. return (result);
  3355. }
  3356. void
  3357. dns_adb_adjustsrtt(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
  3358. unsigned int rtt, unsigned int factor)
  3359. {
  3360. int bucket;
  3361. unsigned int new_srtt;
  3362. isc_stdtime_t now;
  3363. REQUIRE(DNS_ADB_VALID(adb));
  3364. REQUIRE(DNS_ADBADDRINFO_VALID(addr));
  3365. REQUIRE(factor <= 10);
  3366. bucket = addr->entry->lock_bucket;
  3367. LOCK(&adb->entrylocks[bucket]);
  3368. if (factor == DNS_ADB_RTTADJAGE)
  3369. new_srtt = addr->entry->srtt * 98 / 100;
  3370. else
  3371. new_srtt = (addr->entry->srtt / 10 * factor)
  3372. + (rtt / 10 * (10 - factor));
  3373. addr->entry->srtt = new_srtt;
  3374. addr->srtt = new_srtt;
  3375. isc_stdtime_get(&now);
  3376. addr->entry->expires = now + ADB_ENTRY_WINDOW;
  3377. UNLOCK(&adb->entrylocks[bucket]);
  3378. }
  3379. void
  3380. dns_adb_changeflags(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
  3381. unsigned int bits, unsigned int mask)
  3382. {
  3383. int bucket;
  3384. REQUIRE(DNS_ADB_VALID(adb));
  3385. REQUIRE(DNS_ADBADDRINFO_VALID(addr));
  3386. bucket = addr->entry->lock_bucket;
  3387. LOCK(&adb->entrylocks[bucket]);
  3388. addr->entry->flags = (addr->entry->flags & ~mask) | (bits & mask);
  3389. /*
  3390. * Note that we do not update the other bits in addr->flags with
  3391. * the most recent values from addr->entry->flags.
  3392. */
  3393. addr->flags = (addr->flags & ~mask) | (bits & mask);
  3394. UNLOCK(&adb->entrylocks[bucket]);
  3395. }
  3396. isc_result_t
  3397. dns_adb_findaddrinfo(dns_adb_t *adb, isc_sockaddr_t *sa,
  3398. dns_adbaddrinfo_t **addrp, isc_stdtime_t now)
  3399. {
  3400. int bucket;
  3401. dns_adbentry_t *entry;
  3402. dns_adbaddrinfo_t *addr;
  3403. isc_result_t result;
  3404. in_port_t port;
  3405. REQUIRE(DNS_ADB_VALID(adb));
  3406. REQUIRE(addrp != NULL && *addrp == NULL);
  3407. UNUSED(now);
  3408. result = ISC_R_SUCCESS;
  3409. bucket = DNS_ADB_INVALIDBUCKET;
  3410. entry = find_entry_and_lock(adb, sa, &bucket, now);
  3411. INSIST(bucket != DNS_ADB_INVALIDBUCKET);
  3412. if (adb->entry_sd[bucket]) {
  3413. result = ISC_R_SHUTTINGDOWN;
  3414. goto unlock;
  3415. }
  3416. if (entry == NULL) {
  3417. /*
  3418. * We don't know anything about this address.
  3419. */
  3420. entry = new_adbentry(adb);
  3421. if (entry == NULL) {
  3422. result = ISC_R_NOMEMORY;
  3423. goto unlock;
  3424. }
  3425. entry->sockaddr = *sa;
  3426. link_entry(adb, bucket, entry);
  3427. DP(ENTER_LEVEL, "findaddrinfo: new entry %p", entry);
  3428. } else
  3429. DP(ENTER_LEVEL, "findaddrinfo: found entry %p", entry);
  3430. port = isc_sockaddr_getport(sa);
  3431. addr = new_adbaddrinfo(adb, entry, port);
  3432. if (addr == NULL) {
  3433. result = ISC_R_NOMEMORY;
  3434. } else {
  3435. inc_entry_refcnt(adb, entry, ISC_FALSE);
  3436. *addrp = addr;
  3437. }
  3438. unlock:
  3439. UNLOCK(&adb->entrylocks[bucket]);
  3440. return (result);
  3441. }
  3442. void
  3443. dns_adb_freeaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **addrp) {
  3444. dns_adbaddrinfo_t *addr;
  3445. dns_adbentry_t *entry;
  3446. int bucket;
  3447. isc_stdtime_t now;
  3448. isc_boolean_t want_check_exit = ISC_FALSE;
  3449. isc_boolean_t overmem;
  3450. REQUIRE(DNS_ADB_VALID(adb));
  3451. REQUIRE(addrp != NULL);
  3452. addr = *addrp;
  3453. REQUIRE(DNS_ADBADDRINFO_VALID(addr));
  3454. entry = addr->entry;
  3455. REQUIRE(DNS_ADBENTRY_VALID(entry));
  3456. isc_stdtime_get(&now);
  3457. *addrp = NULL;
  3458. overmem = isc_mem_isovermem(adb->mctx);
  3459. bucket = addr->entry->lock_bucket;
  3460. LOCK(&adb->entrylocks[bucket]);
  3461. entry->expires = now + ADB_ENTRY_WINDOW;
  3462. want_check_exit = dec_entry_refcnt(adb, overmem, entry, ISC_FALSE);
  3463. UNLOCK(&adb->entrylocks[bucket]);
  3464. addr->entry = NULL;
  3465. free_adbaddrinfo(adb, &addr);
  3466. if (want_check_exit) {
  3467. LOCK(&adb->lock);
  3468. check_exit(adb);
  3469. UNLOCK(&adb->lock);
  3470. }
  3471. }
  3472. void
  3473. dns_adb_flush(dns_adb_t *adb) {
  3474. unsigned int i;
  3475. INSIST(DNS_ADB_VALID(adb));
  3476. LOCK(&adb->lock);
  3477. /*
  3478. * Call our cleanup routines.
  3479. */
  3480. for (i = 0; i < adb->nnames; i++)
  3481. RUNTIME_CHECK(cleanup_names(adb, i, INT_MAX) == ISC_FALSE);
  3482. for (i = 0; i < adb->nentries; i++)
  3483. RUNTIME_CHECK(cleanup_entries(adb, i, INT_MAX) == ISC_FALSE);
  3484. #ifdef DUMP_ADB_AFTER_CLEANING
  3485. dump_adb(adb, stdout, ISC_TRUE, INT_MAX);
  3486. #endif
  3487. UNLOCK(&adb->lock);
  3488. }
  3489. void
  3490. dns_adb_flushname(dns_adb_t *adb, dns_name_t *name) {
  3491. dns_adbname_t *adbname;
  3492. dns_adbname_t *nextname;
  3493. int bucket;
  3494. INSIST(DNS_ADB_VALID(adb));
  3495. LOCK(&adb->lock);
  3496. bucket = dns_name_hash(name, ISC_FALSE) % adb->nnames;
  3497. LOCK(&adb->namelocks[bucket]);
  3498. adbname = ISC_LIST_HEAD(adb->names[bucket]);
  3499. while (adbname != NULL) {
  3500. nextname = ISC_LIST_NEXT(adbname, plink);
  3501. if (!NAME_DEAD(adbname) &&
  3502. dns_name_equal(name, &adbname->name)) {
  3503. RUNTIME_CHECK(kill_name(&adbname,
  3504. DNS_EVENT_ADBCANCELED) ==
  3505. ISC_FALSE);
  3506. }
  3507. adbname = nextname;
  3508. }
  3509. UNLOCK(&adb->namelocks[bucket]);
  3510. UNLOCK(&adb->lock);
  3511. }
  3512. static void
  3513. water(void *arg, int mark) {
  3514. /*
  3515. * We're going to change the way to handle overmem condition: use
  3516. * isc_mem_isovermem() instead of storing the state via this callback,
  3517. * since the latter way tends to cause race conditions.
  3518. * To minimize the change, and in case we re-enable the callback
  3519. * approach, however, keep this function at the moment.
  3520. */
  3521. dns_adb_t *adb = arg;
  3522. isc_boolean_t overmem = ISC_TF(mark == ISC_MEM_HIWATER);
  3523. REQUIRE(DNS_ADB_VALID(adb));
  3524. DP(ISC_LOG_DEBUG(1),
  3525. "adb reached %s water mark", overmem ? "high" : "low");
  3526. }
  3527. void
  3528. dns_adb_setadbsize(dns_adb_t *adb, isc_uint32_t size) {
  3529. isc_uint32_t hiwater;
  3530. isc_uint32_t lowater;
  3531. INSIST(DNS_ADB_VALID(adb));
  3532. if (size != 0 && size < DNS_ADB_MINADBSIZE)
  3533. size = DNS_ADB_MINADBSIZE;
  3534. hiwater = size - (size >> 3); /* Approximately 7/8ths. */
  3535. lowater = size - (size >> 2); /* Approximately 3/4ths. */
  3536. if (size == 0 || hiwater == 0 || lowater == 0)
  3537. isc_mem_setwater(adb->mctx, water, adb, 0, 0);
  3538. else
  3539. isc_mem_setwater(adb->mctx, water, adb, hiwater, lowater);
  3540. }