adb.c revision d92770e851ebbc1005b3bf121e3c9f13b67a3f42
1da14e066c23245c255dedb311d5a9cf0e5bb667Automatic Updater * Copyright (C) 1999-2001 Internet Software Consortium.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Permission to use, copy, modify, and distribute this software for any
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * purpose with or without fee is hereby granted, provided that the above
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * copyright notice and this permission notice appear in all copies.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews/* $Id: adb.c,v 1.200 2002/11/16 17:20:03 marka Exp $ */
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence * Implementation notes
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * --------------------
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * In finds, if task == NULL, no events will be generated, and no events
a5c077e40c784cf9e25c95a1ab94db2faab04ae9Brian Wellington * have been sent. If task != NULL but taskaction == NULL, an event has been
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafsson * posted but not yet freed. If neither are NULL, no event was posted.
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * After we have cleaned all buckets, dump the database contents.
62700b67eb8abb7d13f9c3c1bc4b60a1477d35d8Mark Andrews#include <isc/string.h> /* Required for HP/UX (and others?) */
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define DNS_ADB_MAGIC ISC_MAGIC('D', 'a', 'd', 'b')
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews#define DNS_ADB_VALID(x) ISC_MAGIC_VALID(x, DNS_ADB_MAGIC)
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews#define DNS_ADBNAME_MAGIC ISC_MAGIC('a', 'd', 'b', 'N')
2f012d936b5ccdf6520c96a4de23721dc58a2221Automatic Updater#define DNS_ADBNAME_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAME_MAGIC)
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define DNS_ADBNAMEHOOK_MAGIC ISC_MAGIC('a', 'd', 'N', 'H')
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define DNS_ADBNAMEHOOK_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAMEHOOK_MAGIC)
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define DNS_ADBZONEINFO_MAGIC ISC_MAGIC('a', 'd', 'b', 'Z')
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define DNS_ADBZONEINFO_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBZONEINFO_MAGIC)
7b68fa6229f1edadac44c7ec459c9ed77a8368c8Mark Andrews#define DNS_ADBENTRY_MAGIC ISC_MAGIC('a', 'd', 'b', 'E')
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews#define DNS_ADBENTRY_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBENTRY_MAGIC)
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define DNS_ADBFETCH_MAGIC ISC_MAGIC('a', 'd', 'F', '4')
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define DNS_ADBFETCH_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH_MAGIC)
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define DNS_ADBFETCH6_MAGIC ISC_MAGIC('a', 'd', 'F', '6')
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define DNS_ADBFETCH6_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH6_MAGIC)
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * The number of buckets needs to be a prime (for good hashing).
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews * XXXRTH How many buckets do we need?
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define NBUCKETS 1009 /* how many buckets for names/addrs */
7b68fa6229f1edadac44c7ec459c9ed77a8368c8Mark Andrews * For type 3 negative cache entries, we will remember that the address is
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * broken for this long. XXXMLG This is also used for actual addresses, too.
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * The intent is to keep us from constantly asking about A/A6/AAAA records
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * if the zone has extremely low TTLs.
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define ADB_CACHE_MINIMUM 10 /* seconds */
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define ADB_CACHE_MAXIMUM 86400 /* seconds (86400 = 24 hours) */
ad5bc22a819190839bdcc4d102d023782dc23660Mark Andrews * Wake up every CLEAN_SECONDS and clean CLEAN_BUCKETS buckets, so that all
c7e266b7e5675e12d1ca3cc929f24b3e86d41f8eEvan Hunt * buckets are cleaned in CLEAN_PERIOD seconds.
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define CLEAN_BUCKETS ((NBUCKETS * CLEAN_SECONDS) / CLEAN_PERIOD)
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define FREE_ITEMS 64 /* free count for memory pools */
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define FILL_COUNT 16 /* fill count for memory pools */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define DNS_ADB_INVALIDBUCKET (-1) /* invalid bucket address */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define DNS_ADB_MINADBSIZE (1024*1024) /* 1 Megabyte */
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrewstypedef ISC_LIST(dns_adbname_t) dns_adbnamelist_t;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrewstypedef struct dns_adbnamehook dns_adbnamehook_t;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtontypedef ISC_LIST(dns_adbnamehook_t) dns_adbnamehooklist_t;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtontypedef struct dns_adbzoneinfo dns_adbzoneinfo_t;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtontypedef ISC_LIST(dns_adbentry_t) dns_adbentrylist_t;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtontypedef struct dns_adbfetch6 dns_adbfetch6_t;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_mutex_t reflock; /* Covers irefcnt, erefcnt */
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews unsigned int irefcnt;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews unsigned int erefcnt;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_mempool_t *nhmp; /* dns_adbnamehook_t */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_mempool_t *zimp; /* dns_adbzoneinfo_t */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_mempool_t *aimp; /* dns_adbaddrinfo_t */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_mempool_t *af6mp; /* dns_adbfetch6_t */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * Bucketized locks and lists for names.
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * XXXRTH Have a per-bucket structure that contains all of these?
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * Bucketized locks for entries.
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * XXXRTH Have a per-bucket structure that contains all of these?
6e373c502584f9292e964378411d296c8259026bMark Andrews isc_boolean_t entry_sd[NBUCKETS]; /* shutting down */
92f60809e854ccf5f115883c6347e370da048848Mark Andrews * XXXMLG Document these structures.
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews * dns_adbnamehook_t
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * This is a small widget that dangles off a dns_adbname_t. It contains a
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * pointer to the address information about this host, and a link to the next
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * namehook that will contain the next address this host has.
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews unsigned int magic;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * dns_adbzoneinfo_t
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * This is a small widget that holds zone-specific information about an
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * address. Currently limited to lameness, but could just as easily be
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * extended to other types of information about zones.
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * An address entry. It holds quite a bit of information about addresses,
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * including edns state (in "flags"), rtt, and of course the address of
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington unsigned int srtt;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * A nonzero 'expires' field indicates that the entry should
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews * persist until that time. This allows entries found
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews * using dns_adb_findaddrinfo() to persist for a limited time
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * even though they are not necessarily associated with a
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafsson * Internal functions (and prototypes).
d5e72d5dba7b77ae0036c53578bcabcf3af1f4b7Andreas Gustafssonstatic inline dns_adbname_t *new_adbname(dns_adb_t *, dns_name_t *);
d5e72d5dba7b77ae0036c53578bcabcf3af1f4b7Andreas Gustafssonstatic inline void free_adbname(dns_adb_t *, dns_adbname_t **);
d5e72d5dba7b77ae0036c53578bcabcf3af1f4b7Andreas Gustafssonstatic inline dns_adbnamehook_t *new_adbnamehook(dns_adb_t *,
2f012d936b5ccdf6520c96a4de23721dc58a2221Automatic Updaterstatic inline void free_adbnamehook(dns_adb_t *, dns_adbnamehook_t **);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline dns_adbzoneinfo_t *new_adbzoneinfo(dns_adb_t *, dns_name_t *);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline void free_adbzoneinfo(dns_adb_t *, dns_adbzoneinfo_t **);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline dns_adbentry_t *new_adbentry(dns_adb_t *);
2f012d936b5ccdf6520c96a4de23721dc58a2221Automatic Updaterstatic inline void free_adbentry(dns_adb_t *, dns_adbentry_t **);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline dns_adbfind_t *new_adbfind(dns_adb_t *);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline isc_boolean_t free_adbfind(dns_adb_t *, dns_adbfind_t **);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline dns_adbaddrinfo_t *new_adbaddrinfo(dns_adb_t *, dns_adbentry_t *,
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline dns_adbfetch_t *new_adbfetch(dns_adb_t *);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline void free_adbfetch(dns_adb_t *, dns_adbfetch_t **);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline dns_adbfetch6_t *new_adbfetch6(dns_adb_t *, dns_adbname_t *,
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline void free_adbfetch6(dns_adb_t *, dns_adbfetch6_t **);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline dns_adbname_t *find_name_and_lock(dns_adb_t *, dns_name_t *,
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafsson unsigned int, int *);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline dns_adbentry_t *find_entry_and_lock(dns_adb_t *,
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic void dump_adb(dns_adb_t *, FILE *, isc_boolean_t debug);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic void print_dns_name(FILE *, dns_name_t *);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic void print_namehook_list(FILE *, const char *legend,
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic void print_find_list(FILE *, dns_adbname_t *);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic void print_fetch_list(FILE *, dns_adbname_t *);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinstatic inline isc_boolean_t dec_adb_irefcnt(dns_adb_t *);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline void inc_adb_irefcnt(dns_adb_t *);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline void inc_adb_erefcnt(dns_adb_t *);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline void inc_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline isc_boolean_t dec_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline void violate_locking_hierarchy(isc_mutex_t *, isc_mutex_t *);
d5e72d5dba7b77ae0036c53578bcabcf3af1f4b7Andreas Gustafssonstatic isc_boolean_t clean_namehooks(dns_adb_t *, dns_adbnamehooklist_t *);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic void clean_target(dns_adb_t *, dns_name_t *);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic void clean_finds_at_name(dns_adbname_t *, isc_eventtype_t,
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafsson unsigned int);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic isc_boolean_t check_expire_namehooks(dns_adbname_t *, isc_stdtime_t,
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrewsstatic void cancel_fetches_at_name(dns_adbname_t *);
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrewsstatic isc_result_t dbfind_name(dns_adbname_t *, isc_stdtime_t,
1cf54d1966b3de8f6593e9e80eae9a80a1c011adMark Andrewsstatic isc_result_t fetch_name_v4(dns_adbname_t *, isc_boolean_t);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic isc_result_t fetch_name_aaaa(dns_adbname_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic isc_result_t fetch_name_a6(dns_adbname_t *, isc_boolean_t);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void timer_cleanup(isc_task_t *, isc_event_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic isc_boolean_t shutdown_names(dns_adb_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic isc_boolean_t shutdown_entries(dns_adb_t *);
1cf54d1966b3de8f6593e9e80eae9a80a1c011adMark Andrewsstatic inline void link_name(dns_adb_t *, int, dns_adbname_t *);
1cf54d1966b3de8f6593e9e80eae9a80a1c011adMark Andrewsstatic inline isc_boolean_t unlink_name(dns_adb_t *, dns_adbname_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void link_entry(dns_adb_t *, int, dns_adbentry_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline isc_boolean_t unlink_entry(dns_adb_t *, dns_adbentry_t *);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graffstatic isc_boolean_t kill_name(dns_adbname_t **, isc_eventtype_t);
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrewsstatic void fetch_callback_a6(isc_task_t *, isc_event_t *);
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrewsstatic isc_result_t dbfind_a6(dns_adbname_t *, isc_stdtime_t);
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews * MUST NOT overlap DNS_ADBFIND_* flags!
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews#define FIND_EVENTSENT(h) (((h)->flags & FIND_EVENT_SENT) != 0)
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews#define FIND_EVENTFREED(h) (((h)->flags & FIND_EVENT_FREED) != 0)
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define NAME_STARTATROOT DNS_ADBFIND_STARTATROOT
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafsson#define NAME_DEAD(n) (((n)->flags & NAME_IS_DEAD) != 0)
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafsson#define NAME_NEEDSPOKE(n) (((n)->flags & NAME_NEEDS_POKE) != 0)
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafsson#define NAME_GLUEOK(n) (((n)->flags & NAME_GLUE_OK) != 0)
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafsson#define NAME_HINTOK(n) (((n)->flags & NAME_HINT_OK) != 0)
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * To the name, address classes are all that really exist. If it has a
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * V6 address it doesn't care if it came from an A6 chain or an AAAA query.
87f4715d6c0a22f3449eb3291c91aa45ba86c955Mark Andrews#define NAME_HAS_V4(n) (!ISC_LIST_EMPTY((n)->v4))
476386968b1f287a695f73c48862e961011af99bMark Andrews#define NAME_HAS_V6(n) (!ISC_LIST_EMPTY((n)->v6))
476386968b1f287a695f73c48862e961011af99bMark Andrews#define NAME_HAS_ADDRS(n) (NAME_HAS_V4(n) || NAME_HAS_V6(n))
1672cff96d0b02badab6f94524e10285dde851fcMark Andrews * Fetches are broken out into A, AAAA, and A6 types. In some cases,
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews * however, it makes more sense to test for a particular class of fetches,
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews * like V4 or V6 above.
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define NAME_FETCH_A(n) ((n)->fetch_a != NULL)
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define NAME_FETCH_AAAA(n) ((n)->fetch_aaaa != NULL)
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define NAME_FETCH_A6(n) (!ISC_LIST_EMPTY((n)->fetches_a6))
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define NAME_FETCH_V6(n) (NAME_FETCH_AAAA(n) || NAME_FETCH_A6(n))
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define NAME_FETCH(n) (NAME_FETCH_V4(n) || NAME_FETCH_V6(n))
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * Was this fetch started using the hints database?
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * Was this the initial fetch for the A6 record? If so, we might want to
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * start AAAA queries if it fails.
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define FETCH_FIRSTA6(f) (((f)->flags & FETCH_FIRST_A6) != 0)
de9833be77ef92c17b35c02d138a0ad8df34dd91Mark Andrews * Find options and tests to see if there are addresses on the list.
de9833be77ef92c17b35c02d138a0ad8df34dd91Mark Andrews#define FIND_WANTEVENT(fn) (((fn)->options & DNS_ADBFIND_WANTEVENT) != 0)
de9833be77ef92c17b35c02d138a0ad8df34dd91Mark Andrews#define FIND_WANTEMPTYEVENT(fn) (((fn)->options & DNS_ADBFIND_EMPTYEVENT) != 0)
de9833be77ef92c17b35c02d138a0ad8df34dd91Mark Andrews#define FIND_AVOIDFETCHES(fn) (((fn)->options & DNS_ADBFIND_AVOIDFETCHES) \
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson#define FIND_STARTATROOT(fn) (((fn)->options & DNS_ADBFIND_STARTATROOT) \
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define FIND_HINTOK(fn) (((fn)->options & DNS_ADBFIND_HINTOK) != 0)
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define FIND_GLUEOK(fn) (((fn)->options & DNS_ADBFIND_GLUEOK) != 0)
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define FIND_HAS_ADDRS(fn) (!ISC_LIST_EMPTY((fn)->list))
de9833be77ef92c17b35c02d138a0ad8df34dd91Mark Andrews#define FIND_RETURNLAME(fn) (((fn)->options & DNS_ADBFIND_RETURNLAME) != 0)
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * These are currently used on simple unsigned ints, so they are
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * not really associated with any particular type.
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define WANT_INET(x) (((x) & DNS_ADBFIND_INET) != 0)
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define WANT_INET6(x) (((x) & DNS_ADBFIND_INET6) != 0)
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define EXPIRE_OK(exp, now) ((exp == INT_MAX) || (exp < now))
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafsson * Find out if the flags on a name (nf) indicate if it is a hint or
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews * glue, and compare this to the appropriate bits set in o, to see if
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews * this is ok.
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews#define GLUE_OK(nf, o) (!NAME_GLUEOK(nf) || (((o) & DNS_ADBFIND_GLUEOK) != 0))
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews#define HINT_OK(nf, o) (!NAME_HINTOK(nf) || (((o) & DNS_ADBFIND_HINTOK) != 0))
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews#define GLUEHINT_OK(nf, o) (GLUE_OK(nf, o) || HINT_OK(nf, o))
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews#define STARTATROOT_MATCHES(nf, o) (((nf)->flags & NAME_STARTATROOT) == \
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews#define NCACHE_RESULT(r) ((r) == DNS_R_NCACHENXDOMAIN || \
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews#define NXDOMAIN_RESULT(r) ((r) == DNS_R_NXDOMAIN || \
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define NXRRSET_RESULT(r) ((r) == DNS_R_NCACHENXRRSET || \
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews * Error state rankings.
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrewsstatic const char *errnames[] = {
207f0a15bb486d8dc27cf5ff963fac6068ee2972Mark Andrews "unexpected",
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define NEWERR(old, new) (ISC_MIN((old), (new)))
8249eee42adc8c9c06c6ff9aaecc7437e259c687Mark Andrewsstatic isc_result_t find_err_map[FIND_ERR_MAX] = {
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian WellingtonDP(int level, const char *format, ...) ISC_FORMAT_PRINTF(2, 3);
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * Requires the adbname bucket be locked and that no entry buckets be locked.
a3a11c4f3fc9ba972802b811c4d95a9884d6ff4aMichael Sawyer * This code handles A and AAAA rdatasets only.
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafssonimport_rdataset(dns_adbname_t *adbname, dns_rdataset_t *rdataset,
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_adbentry_t *foundentry; /* NO CLEAN UP! */
ef653fbdb122e9e251bdfbdd4609d03a208bb79dMichael Graff INSIST((rdtype == dns_rdatatype_a) || (rdtype == dns_rdatatype_aaaa));
ca84283333d22c64abfbcb87872dd5e6d9172c5aMark Andrews foundentry = find_entry_and_lock(adb, &sockaddr, &addr_bucket);
687b7ef9989c9ab9040f4ccb5f1816b96fa4e43fAndreas Gustafsson if (addr_bucket != DNS_ADB_INVALIDBUCKET)
687b7ef9989c9ab9040f4ccb5f1816b96fa4e43fAndreas Gustafsson DP(NCACHE_LEVEL, "expire_v4 set to MIN(%u,%u) import_rdataset",
687b7ef9989c9ab9040f4ccb5f1816b96fa4e43fAndreas Gustafsson adbname->expire_v4, now + rdataset->ttl);
687b7ef9989c9ab9040f4ccb5f1816b96fa4e43fAndreas Gustafsson adbname->expire_v4 = ISC_MIN(adbname->expire_v4,
8cccaeaee13993c49009d3915806c1d0bd03743bAndreas Gustafsson DP(NCACHE_LEVEL, "expire_v6 set to MIN(%u,%u) import_rdataset",
8cccaeaee13993c49009d3915806c1d0bd03743bAndreas Gustafsson adbname->expire_v6, now + rdataset->ttl);
1672cff96d0b02badab6f94524e10285dde851fcMark Andrews adbname->expire_v6 = ISC_MIN(adbname->expire_v6,
1672cff96d0b02badab6f94524e10285dde851fcMark Andrews * Lie a little here. This is more or less so code that cares
1672cff96d0b02badab6f94524e10285dde851fcMark Andrews * can find out if any new information was added or not.
2c15fcdeac4c2402258867fbac24d7475ef98259Mark Andrews DP(ENTER_LEVEL, "ENTER: import_a6() name %p", name);
2c15fcdeac4c2402258867fbac24d7475ef98259Mark Andrews name->partial_result |= DNS_ADBFIND_INET6; /* clear for AAAA */
a1bc94109313bf4ebb6e6ff655d71d45582d2e43Mark Andrews isc_sockaddr_fromin6(&sockaddr, &a6ctx->in6addr, 0);
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson foundentry = find_entry_and_lock(adb, &sockaddr, &addr_bucket);
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson name->partial_result |= DNS_ADBFIND_INET6;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington DP(NCACHE_LEVEL, "expire_v6 set to MIN(%u,%u) in import_v6",
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington name->expire_v6 = ISC_MIN(name->expire_v6, a6ctx->expiration);
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews * Requires the name's bucket be locked.
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrewskill_name(dns_adbname_t **n, isc_eventtype_t ev) {
a5c077e40c784cf9e25c95a1ab94db2faab04ae9Brian Wellington * If we're dead already, just check to see if we should go
a5c077e40c784cf9e25c95a1ab94db2faab04ae9Brian Wellington * away now or not.
a5c077e40c784cf9e25c95a1ab94db2faab04ae9Brian Wellington if (NAME_DEAD(name) && !NAME_FETCH(name)) {
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews * Clean up the name's various lists. These two are destructive
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews * in that they will always empty the list.
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews clean_finds_at_name(name, ev, DNS_ADBFIND_ADDRESSMASK);
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews * If fetches are running, cancel them. If none are running, we can
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews * just kill the name here.
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews * Requires the name's bucket be locked and no entry buckets be locked.
508f61f8d699c46f962b682f388e54b446a7194dMark Andrewscheck_expire_namehooks(dns_adbname_t *name, isc_stdtime_t now,
2f012d936b5ccdf6520c96a4de23721dc58a2221Automatic Updater * Check to see if we need to remove the v4 addresses
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews DP(DEF_LEVEL, "expiring v4 for name %p", name);
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews * Check to see if we need to remove the v6 addresses
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews DP(DEF_LEVEL, "expiring v6 for name %p", name);
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews * Check to see if we need to remove the alias target.
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews if (expire || EXPIRE_OK(name->expire_target, now)) {
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * Requires the name's bucket be locked.
2674e1a455d4f71de09b2b60e7a8304b9a305588Mark Andrewsstatic inline void
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtonlink_name(dns_adb_t *adb, int bucket, dns_adbname_t *name) {
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington INSIST(name->lock_bucket == DNS_ADB_INVALIDBUCKET);
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington ISC_LIST_PREPEND(adb->names[bucket], name, plink);
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * Requires the name's bucket be locked.
a12d9cfa59b5981c52e1aaafedf652d5128f3448Brian Wellingtonunlink_name(dns_adb_t *adb, dns_adbname_t *name) {
87f4715d6c0a22f3449eb3291c91aa45ba86c955Mark Andrews ISC_LIST_UNLINK(adb->names[bucket], name, plink);
87f4715d6c0a22f3449eb3291c91aa45ba86c955Mark Andrews if (adb->name_sd[bucket] && adb->name_refcnt[bucket] == 0)
bb56f556f520621bcab33688c31d655953774adcAndreas Gustafsson * Requires the entry's bucket be locked.
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtonstatic inline void
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtonlink_entry(dns_adb_t *adb, int bucket, dns_adbentry_t *entry) {
17dba29ba5db791976e505114baee53a1dde88aaBrian Wellington ISC_LIST_PREPEND(adb->entries[bucket], entry, plink);
cb9aa603ab2d019032e1b7b2d274e69adf0980f8Michael Graff * Requires the entry's bucket be locked.
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtonunlink_entry(dns_adb_t *adb, dns_adbentry_t *entry) {
cb9aa603ab2d019032e1b7b2d274e69adf0980f8Michael Graff ISC_LIST_UNLINK(adb->entries[bucket], entry, plink);
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington if (adb->entry_sd[bucket] && adb->entry_refcnt[bucket] == 0)
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafssonstatic inline void
62700b67eb8abb7d13f9c3c1bc4b60a1477d35d8Mark Andrewsviolate_locking_hierarchy(isc_mutex_t *have, isc_mutex_t *want) {
bb60abb44549428414cd55a022f2b8cc4488f7adAndreas Gustafsson if (isc_mutex_trylock(want) != ISC_R_SUCCESS) {
b500de3be9ba5318da157364bf9fbbda5f88f203Mark Andrews * The ADB _MUST_ be locked before calling. Also, exit conditions must be
476386968b1f287a695f73c48862e961011af99bMark Andrews * checked after calling this function.
476386968b1f287a695f73c48862e961011af99bMark Andrews for (bucket = 0; bucket < NBUCKETS; bucket++) {
ce28ea0f2f8a6fbbafb8944dbe86cd0b98689b84Tatuya JINMEI 神明達哉 name = ISC_LIST_HEAD(adb->names[bucket]);
476386968b1f287a695f73c48862e961011af99bMark Andrews * This bucket has no names. We must decrement the
476386968b1f287a695f73c48862e961011af99bMark Andrews * irefcnt ourselves, since it will not be
476386968b1f287a695f73c48862e961011af99bMark Andrews * automatically triggered by a name being unlinked.
a45a6ea2b03448751d7c44931e8ac7666e7cc2ceMark Andrews * Run through the list. For each name, clean up finds
a45a6ea2b03448751d7c44931e8ac7666e7cc2ceMark Andrews * found there, and cancel any fetches running. When
a45a6ea2b03448751d7c44931e8ac7666e7cc2ceMark Andrews * all the fetches are canceled, the name will destroy
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * The ADB _MUST_ be locked before calling. Also, exit conditions must be
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * checked after calling this function.
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington for (bucket = 0; bucket < NBUCKETS; bucket++) {
ff4322d44f8404683b6fb6c86a38a2bc14f6c083Andreas Gustafsson entry = ISC_LIST_HEAD(adb->entries[bucket]);
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson * This bucket has no entries. We must decrement the
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson * irefcnt ourselves, since it will not be
if (result)
return (result);
static isc_boolean_t
int addr_bucket;
return (result);
static isc_result_t
int order;
return (result);
return (result);
return (result);
return (result);
return (result);
return (result);
NULL);
return (result);
return (result);
return (ISC_R_SUCCESS);
unsigned int addrs)
switch (evtype) {
if ((notify) != 0) {
if (wanted == 0)
if (process) {
static inline isc_boolean_t
return (result);
int bucket;
if (lock)
if (lock)
static inline isc_boolean_t
int bucket;
if (lock)
if (lock)
if (!destroy_entry)
return (result);
if (result)
return (result);
static inline dns_adbname_t *
return (NULL);
return (NULL);
return (name);
dns_adbname_t *n;
n = *name;
n->magic = 0;
static inline dns_adbnamehook_t *
return (NULL);
return (nh);
static inline dns_adbzoneinfo_t *
return (NULL);
return (NULL);
return (zi);
static inline dns_adbentry_t *
dns_adbentry_t *e;
isc_uint32_t r;
if (e == NULL)
return (NULL);
e->refcnt = 0;
e->flags = 0;
isc_random_get(&r);
e->expires = 0;
dns_adbentry_t *e;
e = *entry;
e->magic = 0;
static inline dns_adbfind_t *
dns_adbfind_t *h;
if (h == NULL)
return (NULL);
h->magic = 0;
h->partial_result = 0;
h->options = 0;
h->flags = 0;
return (NULL);
static inline dns_adbfetch_t *
dns_adbfetch_t *f;
if (f == NULL)
return (NULL);
f->magic = 0;
goto err;
goto err;
err:
return (NULL);
dns_adbfetch_t *f;
f = *fetch;
f->magic = 0;
static isc_result_t
return (result);
goto cleanup;
static inline dns_adbfetch6_t *
dns_adbfetch6_t *f;
if (f == NULL)
return (NULL);
f->magic = 0;
f->flags = 0;
goto err;
goto err;
err:
return (NULL);
dns_adbfetch6_t *f;
f = *fetch;
f->magic = 0;
static inline isc_boolean_t
static inline dns_adbaddrinfo_t *
return (NULL);
return (ai);
static inline dns_adbname_t *
int bucket;
return (adbname);
return (NULL);
static inline dns_adbentry_t *
int bucket;
return (entry);
return (NULL);
static isc_boolean_t
return (ISC_FALSE);
return (is_bad);
int bucket;
goto nextv4;
goto out;
goto nextv6;
goto out;
out:
static isc_boolean_t
return (result);
return (result);
return (result);
return (result);
return (result);
return (result);
static isc_boolean_t
return (result);
return (result);
if (result)
return (result);
static isc_boolean_t
return (result);
if (!result)
return (result);
static isc_boolean_t
return (result);
for (i = 0; i < CLEAN_BUCKETS; i++) {
#ifdef DUMP_ADB_AFTER_CLEANING
return (ISC_R_NOMEMORY);
goto fail0b;
goto fail0c;
goto fail0d;
goto fail1;
for (i = 0; i < NBUCKETS; i++) {
for (i = 0; i < NBUCKETS; i++) {
goto fail2;
#define MPINIT(t, p, n) do { \
goto fail3; \
isc_mempool_setname((p), n); \
goto fail3;
goto fail3;
return (ISC_R_SUCCESS);
return (result);
if (need_exit_check) {
if (!need_check_exit)
if (need_check_exit)
int bucket;
unsigned int wanted_addresses;
unsigned int wanted_fetches;
unsigned int query_pending;
wanted_fetches = 0;
query_pending = 0;
if (now == 0)
return (ISC_R_NOMEMORY);
goto out;
goto out;
adbname);
goto post_copy;
adbname);
goto v6;
adbname);
goto post_copy;
goto fetch;
goto v6;
v6:
adbname);
goto fetch;
adbname);
goto post_copy;
goto fetch;
adbname);
goto fetch;
adbname);
goto post_copy;
goto fetch;
if (wanted_fetches != 0 &&
adbname);
adbname);
if (alias)
if (want_event) {
if (alias) {
goto out;
out:
if (want_event) {
return (result);
int bucket;
int bucket;
int unlock_bucket;
goto cleanup;
if (debug)
for (i = 0; i < NBUCKETS; i++)
for (i = 0; i < NBUCKETS; i++)
for (i = 0; i < NBUCKETS; i++) {
if (debug)
if (debug)
if (debug)
if (debug)
for (i = 0; i < NBUCKETS; i++)
for (i = 0; i < NBUCKETS; i++)
if (debug)
const char *tmpp;
case AF_INET:
case AF_INET6:
if (debug)
if (NAME_FETCH_A(n))
if (NAME_FETCH_AAAA(n))
static isc_result_t
switch (result) {
case DNS_R_GLUE:
case DNS_R_HINT:
case ISC_R_SUCCESS:
case DNS_R_NXDOMAIN:
case DNS_R_NXRRSET:
adbname);
adbname);
case DNS_R_NCACHENXDOMAIN:
case DNS_R_NCACHENXRRSET:
case DNS_R_CNAME:
case DNS_R_DNAME:
adbname);
return (result);
static isc_result_t
switch (result) {
case DNS_R_GLUE:
case DNS_R_HINT:
case ISC_R_SUCCESS:
case DNS_R_NXDOMAIN:
case DNS_R_NXRRSET:
adbname);
case DNS_R_NCACHENXDOMAIN:
case DNS_R_NCACHENXRRSET:
case DNS_R_CNAME:
case DNS_R_DNAME:
adbname);
return (result);
int bucket;
unsigned int address_type;
address_type = 0;
if (want_check_exit) {
goto out;
name);
goto check_result;
goto out;
out:
int bucket;
if (want_check_exit) {
name);
goto out;
name);
goto out;
goto out;
name);
goto out;
goto out;
out:
static isc_result_t
unsigned int options;
options = 0;
if (start_at_root) {
adbname);
goto cleanup;
goto cleanup;
goto cleanup;
return (result);
static isc_result_t
goto cleanup;
goto cleanup;
return (result);
static isc_result_t
unsigned int options;
options = 0;
if (start_at_root) {
adbname);
goto cleanup;
goto cleanup;
goto cleanup;
return (result);
int bucket;
return (ISC_R_NOMEMORY);
return (ISC_R_SUCCESS);
int bucket;
unsigned int new_srtt;
int bucket;
int bucket;
goto unlock;
goto unlock;
return (result);
int bucket;
if (want_check_exit) {
for (i = 0; i < NBUCKETS; i++) {
#ifdef DUMP_ADB_AFTER_CLEANING
int bucket;
== ISC_FALSE);
if (overmem) {