1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include "alloc-util.h"
8 #include "dns-domain.h"
9 #include "format-ifname.h"
12 #include "resolve-util.h"
13 #include "resolved-dns-answer.h"
14 #include "resolved-dns-cache.h"
15 #include "resolved-dns-dnssec.h"
16 #include "resolved-dns-packet.h"
17 #include "resolved-dns-rr.h"
18 #include "string-util.h"
19 #include "time-util.h"
21 /* Never cache more than 4K entries. RFC 1536, Section 5 suggests to
22 * leave DNS caches unbounded, but that's crazy. */
23 #define CACHE_MAX 4096
25 /* We never keep any item longer than 2h in our cache unless StaleRetentionSec is greater than zero. */
26 #define CACHE_TTL_MAX_USEC (2 * USEC_PER_HOUR)
28 /* The max TTL for stale data is set to 30 seconds. See RFC 8767, Section 6. */
29 #define CACHE_STALE_TTL_MAX_USEC (30 * USEC_PER_SEC)
31 /* How long to cache strange rcodes, i.e. rcodes != SUCCESS and != NXDOMAIN (specifically: that's only SERVFAIL for
33 #define CACHE_TTL_STRANGE_RCODE_USEC (10 * USEC_PER_SEC)
35 #define CACHEABLE_QUERY_FLAGS (SD_RESOLVED_AUTHENTICATED|SD_RESOLVED_CONFIDENTIAL)
37 typedef enum DnsCacheItemType DnsCacheItemType
;
38 typedef struct DnsCacheItem DnsCacheItem
;
40 enum DnsCacheItemType
{
44 DNS_CACHE_RCODE
, /* "strange" RCODE (effective only SERVFAIL for now) */
48 DnsCacheItemType type
;
50 DnsResourceKey
*key
; /* The key for this item, i.e. the lookup key */
51 DnsResourceRecord
*rr
; /* The RR for this item, i.e. the lookup value for positive queries */
52 DnsAnswer
*answer
; /* The full validated answer, if this is an RRset acquired via a "primary" lookup */
53 DnsPacket
*full_packet
; /* The full packet this information was acquired with */
55 usec_t until
; /* If StaleRetentionSec is greater than zero, until is set to a duration of StaleRetentionSec from the time of TTL expiry. If StaleRetentionSec is zero, both until and until_valid will be set to ttl. */
56 usec_t until_valid
; /* The key is for storing the time when the TTL set to expire. */
57 uint64_t query_flags
; /* SD_RESOLVED_AUTHENTICATED and/or SD_RESOLVED_CONFIDENTIAL */
58 DnssecResult dnssec_result
;
62 union in_addr_union owner_address
;
65 LIST_FIELDS(DnsCacheItem
, by_key
);
70 /* Returns true if this is a cache item created as result of an explicit lookup, or created as "side-effect"
71 * of another request. "Primary" entries will carry the full answer data (with NSEC, …) that can aso prove
72 * wildcard expansion, non-existence and such, while entries that were created as "side-effect" just contain
73 * immediate RR data for the specified RR key, but nothing else. */
74 #define DNS_CACHE_ITEM_IS_PRIMARY(item) (!!(item)->answer)
76 static const char *dns_cache_item_type_to_string(DnsCacheItem
*item
) {
81 case DNS_CACHE_POSITIVE
:
84 case DNS_CACHE_NODATA
:
87 case DNS_CACHE_NXDOMAIN
:
91 return dns_rcode_to_string(item
->rcode
);
97 static DnsCacheItem
* dns_cache_item_free(DnsCacheItem
*i
) {
101 dns_resource_record_unref(i
->rr
);
102 dns_resource_key_unref(i
->key
);
103 dns_answer_unref(i
->answer
);
104 dns_packet_unref(i
->full_packet
);
107 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsCacheItem
*, dns_cache_item_free
);
109 static void dns_cache_item_unlink_and_free(DnsCache
*c
, DnsCacheItem
*i
) {
117 first
= hashmap_get(c
->by_key
, i
->key
);
118 LIST_REMOVE(by_key
, first
, i
);
121 assert_se(hashmap_replace(c
->by_key
, first
->key
, first
) >= 0);
123 hashmap_remove(c
->by_key
, i
->key
);
125 prioq_remove(c
->by_expiry
, i
, &i
->prioq_idx
);
127 dns_cache_item_free(i
);
130 static bool dns_cache_remove_by_rr(DnsCache
*c
, DnsResourceRecord
*rr
) {
134 first
= hashmap_get(c
->by_key
, rr
->key
);
135 LIST_FOREACH(by_key
, i
, first
) {
136 r
= dns_resource_record_equal(i
->rr
, rr
);
140 dns_cache_item_unlink_and_free(c
, i
);
148 static bool dns_cache_remove_by_key(DnsCache
*c
, DnsResourceKey
*key
) {
154 first
= hashmap_remove(c
->by_key
, key
);
158 LIST_FOREACH(by_key
, i
, first
) {
159 prioq_remove(c
->by_expiry
, i
, &i
->prioq_idx
);
160 dns_cache_item_free(i
);
166 void dns_cache_flush(DnsCache
*c
) {
171 while ((key
= hashmap_first_key(c
->by_key
)))
172 dns_cache_remove_by_key(c
, key
);
174 assert(hashmap_isempty(c
->by_key
));
175 assert(prioq_isempty(c
->by_expiry
));
177 c
->by_key
= hashmap_free(c
->by_key
);
178 c
->by_expiry
= prioq_free(c
->by_expiry
);
181 static void dns_cache_make_space(DnsCache
*c
, unsigned add
) {
187 /* Makes space for n new entries. Note that we actually allow
188 * the cache to grow beyond CACHE_MAX, but only when we shall
189 * add more RRs to the cache than CACHE_MAX at once. In that
190 * case the cache will be emptied completely otherwise. */
193 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
196 if (prioq_isempty(c
->by_expiry
))
199 if (prioq_size(c
->by_expiry
) + add
< CACHE_MAX
)
202 i
= prioq_peek(c
->by_expiry
);
205 /* Take an extra reference to the key so that it
206 * doesn't go away in the middle of the remove call */
207 key
= dns_resource_key_ref(i
->key
);
208 dns_cache_remove_by_key(c
, key
);
212 void dns_cache_prune(DnsCache
*c
) {
217 /* Remove all entries that are past their TTL */
221 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
223 i
= prioq_peek(c
->by_expiry
);
228 t
= now(CLOCK_BOOTTIME
);
233 /* Depending whether this is an mDNS shared entry
234 * either remove only this one RR or the whole RRset */
235 log_debug("Removing %scache entry for %s (expired "USEC_FMT
"s ago)",
236 i
->shared_owner
? "shared " : "",
237 dns_resource_key_to_string(i
->key
, key_str
, sizeof key_str
),
238 (t
- i
->until
) / USEC_PER_SEC
);
241 dns_cache_item_unlink_and_free(c
, i
);
243 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
245 /* Take an extra reference to the key so that it
246 * doesn't go away in the middle of the remove call */
247 key
= dns_resource_key_ref(i
->key
);
248 dns_cache_remove_by_key(c
, key
);
253 bool dns_cache_expiry_in_one_second(DnsCache
*c
, usec_t t
) {
258 /* Check if any items expire within the next second */
259 i
= prioq_peek(c
->by_expiry
);
263 if (i
->until
<= usec_add(t
, USEC_PER_SEC
))
269 static int dns_cache_item_prioq_compare_func(const void *a
, const void *b
) {
270 const DnsCacheItem
*x
= a
, *y
= b
;
272 return CMP(x
->until
, y
->until
);
275 static int dns_cache_init(DnsCache
*c
) {
280 r
= prioq_ensure_allocated(&c
->by_expiry
, dns_cache_item_prioq_compare_func
);
284 r
= hashmap_ensure_allocated(&c
->by_key
, &dns_resource_key_hash_ops
);
291 static int dns_cache_link_item(DnsCache
*c
, DnsCacheItem
*i
) {
298 r
= prioq_put(c
->by_expiry
, i
, &i
->prioq_idx
);
302 first
= hashmap_get(c
->by_key
, i
->key
);
304 _unused_
_cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*k
= NULL
;
306 /* Keep a reference to the original key, while we manipulate the list. */
307 k
= dns_resource_key_ref(first
->key
);
309 /* Now, try to reduce the number of keys we keep */
310 dns_resource_key_reduce(&first
->key
, &i
->key
);
313 dns_resource_key_reduce(&first
->rr
->key
, &i
->key
);
315 dns_resource_key_reduce(&i
->rr
->key
, &i
->key
);
317 LIST_PREPEND(by_key
, first
, i
);
318 assert_se(hashmap_replace(c
->by_key
, first
->key
, first
) >= 0);
320 r
= hashmap_put(c
->by_key
, i
->key
, i
);
322 prioq_remove(c
->by_expiry
, i
, &i
->prioq_idx
);
330 static DnsCacheItem
* dns_cache_get(DnsCache
*c
, DnsResourceRecord
*rr
) {
334 LIST_FOREACH(by_key
, i
, (DnsCacheItem
*) hashmap_get(c
->by_key
, rr
->key
))
335 if (i
->rr
&& dns_resource_record_equal(i
->rr
, rr
) > 0)
341 static usec_t
calculate_until_valid(
342 DnsResourceRecord
*rr
,
346 bool use_soa_minimum
) {
353 ttl
= MIN(min_ttl
, nsec_ttl
);
354 if (rr
->key
->type
== DNS_TYPE_SOA
&& use_soa_minimum
) {
355 /* If this is a SOA RR, and it is requested, clamp to the SOA's minimum field. This is used
356 * when we do negative caching, to determine the TTL for the negative caching entry. See RFC
357 * 2308, Section 5. */
359 if (ttl
> rr
->soa
.minimum
)
360 ttl
= rr
->soa
.minimum
;
363 u
= ttl
* USEC_PER_SEC
;
364 if (u
> CACHE_TTL_MAX_USEC
)
365 u
= CACHE_TTL_MAX_USEC
;
367 if (rr
->expiry
!= USEC_INFINITY
) {
370 /* Make use of the DNSSEC RRSIG expiry info, if we have it */
372 left
= LESS_BY(rr
->expiry
, now(CLOCK_REALTIME
));
377 return timestamp
+ u
;
380 static usec_t
calculate_until(
382 usec_t stale_retention_usec
) {
384 return stale_retention_usec
> 0 ? usec_add(until_valid
, stale_retention_usec
) : until_valid
;
387 static void dns_cache_item_update_positive(
390 DnsResourceRecord
*rr
,
392 DnsPacket
*full_packet
,
394 uint64_t query_flags
,
396 DnssecResult dnssec_result
,
400 const union in_addr_union
*owner_address
,
401 usec_t stale_retention_usec
) {
406 assert(owner_address
);
408 i
->type
= DNS_CACHE_POSITIVE
;
411 /* We are the first item in the list, we need to
412 * update the key used in the hashmap */
414 assert_se(hashmap_replace(c
->by_key
, rr
->key
, i
) >= 0);
416 DNS_RR_REPLACE(i
->rr
, dns_resource_record_ref(rr
));
418 DNS_RESOURCE_KEY_REPLACE(i
->key
, dns_resource_key_ref(rr
->key
));
420 DNS_ANSWER_REPLACE(i
->answer
, dns_answer_ref(answer
));
422 DNS_PACKET_REPLACE(i
->full_packet
, dns_packet_ref(full_packet
));
424 i
->until_valid
= calculate_until_valid(rr
, min_ttl
, UINT32_MAX
, timestamp
, false);
425 i
->until
= calculate_until(i
->until_valid
, stale_retention_usec
);
426 i
->query_flags
= query_flags
& CACHEABLE_QUERY_FLAGS
;
427 i
->shared_owner
= shared_owner
;
428 i
->dnssec_result
= dnssec_result
;
430 i
->ifindex
= ifindex
;
432 i
->owner_family
= owner_family
;
433 i
->owner_address
= *owner_address
;
435 prioq_reshuffle(c
->by_expiry
, i
, &i
->prioq_idx
);
438 static int dns_cache_put_positive(
440 DnsProtocol protocol
,
441 DnsResourceRecord
*rr
,
443 DnsPacket
*full_packet
,
444 uint64_t query_flags
,
446 DnssecResult dnssec_result
,
450 const union in_addr_union
*owner_address
,
451 usec_t stale_retention_usec
) {
453 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
454 DnsCacheItem
*existing
;
460 assert(owner_address
);
462 /* Never cache pseudo RRs */
463 if (dns_class_is_pseudo(rr
->key
->class))
465 if (dns_type_is_pseudo(rr
->key
->type
))
468 /* Determine the minimal TTL of all RRs in the answer plus the one by the main RR we are supposed to
469 * cache. Since we cache whole answers to questions we should never return answers where only some
470 * RRs are still valid, hence find the lowest here */
471 min_ttl
= MIN(dns_answer_min_ttl(answer
), rr
->ttl
);
473 /* New TTL is 0? Delete this specific entry... */
475 r
= dns_cache_remove_by_rr(c
, rr
);
477 r
> 0 ? "Removed zero TTL entry from cache" : "Not caching zero TTL cache entry",
478 dns_resource_key_to_string(rr
->key
, key_str
, sizeof key_str
));
482 /* Entry exists already? Update TTL, timestamp and owner */
483 existing
= dns_cache_get(c
, rr
);
485 dns_cache_item_update_positive(
499 stale_retention_usec
);
503 /* Do not cache mDNS goodbye packet. */
504 if (protocol
== DNS_PROTOCOL_MDNS
&& rr
->ttl
<= 1)
507 /* Otherwise, add the new RR */
508 r
= dns_cache_init(c
);
512 dns_cache_make_space(c
, 1);
514 _cleanup_(dns_cache_item_freep
) DnsCacheItem
*i
= new(DnsCacheItem
, 1);
518 /* If StaleRetentionSec is greater than zero, the 'until' property is set to a duration
519 * of StaleRetentionSec from the time of TTL expiry.
520 * If StaleRetentionSec is zero, both the 'until' and 'until_valid' are set to the TTL duration,
521 * leading to the eviction of the record once the TTL expires. */
522 usec_t until_valid
= calculate_until_valid(rr
, min_ttl
, UINT32_MAX
, timestamp
, false);
523 *i
= (DnsCacheItem
) {
524 .type
= DNS_CACHE_POSITIVE
,
525 .key
= dns_resource_key_ref(rr
->key
),
526 .rr
= dns_resource_record_ref(rr
),
527 .answer
= dns_answer_ref(answer
),
528 .full_packet
= dns_packet_ref(full_packet
),
529 .until
= calculate_until(until_valid
, stale_retention_usec
),
530 .until_valid
= until_valid
,
531 .query_flags
= query_flags
& CACHEABLE_QUERY_FLAGS
,
532 .shared_owner
= shared_owner
,
533 .dnssec_result
= dnssec_result
,
535 .owner_family
= owner_family
,
536 .owner_address
= *owner_address
,
537 .prioq_idx
= PRIOQ_IDX_NULL
,
540 r
= dns_cache_link_item(c
, i
);
544 log_debug("Added positive %s %s%s cache entry for %s "USEC_FMT
"s on %s/%s/%s",
545 FLAGS_SET(i
->query_flags
, SD_RESOLVED_AUTHENTICATED
) ? "authenticated" : "unauthenticated",
546 FLAGS_SET(i
->query_flags
, SD_RESOLVED_CONFIDENTIAL
) ? "confidential" : "non-confidential",
547 i
->shared_owner
? " shared" : "",
548 dns_resource_key_to_string(i
->key
, key_str
, sizeof key_str
),
549 (i
->until
- timestamp
) / USEC_PER_SEC
,
550 i
->ifindex
== 0 ? "*" : FORMAT_IFNAME(i
->ifindex
),
551 af_to_name_short(i
->owner_family
),
552 IN_ADDR_TO_STRING(i
->owner_family
, &i
->owner_address
));
557 /* https://www.iana.org/assignments/special-use-domain-names/special-use-domain-names.xhtml */
558 /* https://www.iana.org/assignments/locally-served-dns-zones/locally-served-dns-zones.xhtml#transport-independent */
559 static bool dns_special_use_domain_invalid_answer(DnsResourceKey
*key
, int rcode
) {
560 /* Sometimes we know a domain exists, even if broken nameservers say otherwise. Make sure not to
561 * cache any answers we know are wrong. */
563 /* RFC9462 § 6.4: resolvers SHOULD respond to queries of any type other than SVCB for
564 * _dns.resolver.arpa. with NODATA and queries of any type for any domain name under resolver.arpa
566 if (dns_name_endswith(dns_resource_key_name(key
), "resolver.arpa") > 0 && rcode
== DNS_RCODE_NXDOMAIN
)
572 static int dns_cache_put_negative(
577 DnsPacket
*full_packet
,
578 uint64_t query_flags
,
579 DnssecResult dnssec_result
,
582 DnsResourceRecord
*soa
,
584 const union in_addr_union
*owner_address
) {
586 _cleanup_(dns_cache_item_freep
) DnsCacheItem
*i
= NULL
;
587 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
592 assert(owner_address
);
594 /* Never cache pseudo RR keys. DNS_TYPE_ANY is particularly
595 * important to filter out as we use this as a pseudo-type for
596 * NXDOMAIN entries */
597 if (dns_class_is_pseudo(key
->class))
599 if (dns_type_is_pseudo(key
->type
))
601 if (dns_special_use_domain_invalid_answer(key
, rcode
))
604 if (IN_SET(rcode
, DNS_RCODE_SUCCESS
, DNS_RCODE_NXDOMAIN
)) {
608 /* For negative replies, check if we have a TTL of a SOA */
609 if (nsec_ttl
<= 0 || soa
->soa
.minimum
<= 0 || soa
->ttl
<= 0) {
610 log_debug("Not caching negative entry with zero SOA/NSEC/NSEC3 TTL: %s",
611 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
614 } else if (rcode
!= DNS_RCODE_SERVFAIL
)
617 r
= dns_cache_init(c
);
621 dns_cache_make_space(c
, 1);
623 i
= new(DnsCacheItem
, 1);
627 *i
= (DnsCacheItem
) {
629 rcode
== DNS_RCODE_SUCCESS
? DNS_CACHE_NODATA
:
630 rcode
== DNS_RCODE_NXDOMAIN
? DNS_CACHE_NXDOMAIN
: DNS_CACHE_RCODE
,
631 .query_flags
= query_flags
& CACHEABLE_QUERY_FLAGS
,
632 .dnssec_result
= dnssec_result
,
633 .owner_family
= owner_family
,
634 .owner_address
= *owner_address
,
635 .prioq_idx
= PRIOQ_IDX_NULL
,
637 .answer
= dns_answer_ref(answer
),
638 .full_packet
= dns_packet_ref(full_packet
),
641 /* Determine how long to cache this entry. In case we have some RRs in the answer use the lowest TTL
642 * of any of them. Typically that's the SOA's TTL, which is OK, but could possibly be lower because
643 * of some other RR. Let's better take the lowest option here than a needlessly high one */
644 i
->until
= i
->until_valid
=
645 i
->type
== DNS_CACHE_RCODE
? timestamp
+ CACHE_TTL_STRANGE_RCODE_USEC
:
646 calculate_until_valid(soa
, dns_answer_min_ttl(answer
), nsec_ttl
, timestamp
, true);
648 if (i
->type
== DNS_CACHE_NXDOMAIN
) {
649 /* NXDOMAIN entries should apply equally to all types, so we use ANY as
650 * a pseudo type for this purpose here. */
651 i
->key
= dns_resource_key_new(key
->class, DNS_TYPE_ANY
, dns_resource_key_name(key
));
655 /* Make sure to remove any previous entry for this
656 * specific ANY key. (For non-ANY keys the cache data
657 * is already cleared by the caller.) Note that we
658 * don't bother removing positive or NODATA cache
659 * items in this case, because it would either be slow
660 * or require explicit indexing by name */
661 dns_cache_remove_by_key(c
, key
);
663 i
->key
= dns_resource_key_ref(key
);
665 r
= dns_cache_link_item(c
, i
);
669 log_debug("Added %s cache entry for %s "USEC_FMT
"s",
670 dns_cache_item_type_to_string(i
),
671 dns_resource_key_to_string(i
->key
, key_str
, sizeof key_str
),
672 (i
->until
- timestamp
) / USEC_PER_SEC
);
678 static void dns_cache_remove_previous(
683 DnsResourceRecord
*rr
;
684 DnsAnswerFlags flags
;
688 /* First, if we were passed a key (i.e. on LLMNR/DNS, but
689 * not on mDNS), delete all matching old RRs, so that we only
690 * keep complete by_key in place. */
692 dns_cache_remove_by_key(c
, key
);
694 /* Second, flush all entries matching the answer, unless this
695 * is an RR that is explicitly marked to be "shared" between
696 * peers (i.e. mDNS RRs without the flush-cache bit set). */
697 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
698 if ((flags
& DNS_ANSWER_CACHEABLE
) == 0)
701 if (flags
& DNS_ANSWER_SHARED_OWNER
)
704 dns_cache_remove_by_key(c
, rr
->key
);
708 static bool rr_eligible(DnsResourceRecord
*rr
) {
711 /* When we see an NSEC/NSEC3 RR, we'll only cache it if it is from the lower zone, not the upper zone, since
712 * that's where the interesting bits are (with exception of DS RRs). Of course, this way we cannot derive DS
713 * existence from any cached NSEC/NSEC3, but that should be fine. */
715 switch (rr
->key
->type
) {
718 return !bitmap_isset(rr
->nsec
.types
, DNS_TYPE_NS
) ||
719 bitmap_isset(rr
->nsec
.types
, DNS_TYPE_SOA
);
722 return !bitmap_isset(rr
->nsec3
.types
, DNS_TYPE_NS
) ||
723 bitmap_isset(rr
->nsec3
.types
, DNS_TYPE_SOA
);
732 DnsCacheMode cache_mode
,
733 DnsProtocol protocol
,
737 DnsPacket
*full_packet
,
738 uint64_t query_flags
,
739 DnssecResult dnssec_result
,
742 const union in_addr_union
*owner_address
,
743 usec_t stale_retention_usec
) {
745 DnsResourceRecord
*soa
= NULL
;
746 bool weird_rcode
= false;
748 DnsAnswerFlags flags
;
754 assert(owner_address
);
756 dns_cache_remove_previous(c
, key
, answer
);
758 /* We only care for positive replies and NXDOMAINs, on all other replies we will simply flush the respective
759 * entries, and that's it. (Well, with one further exception: since some DNS zones (akamai!) return SERVFAIL
760 * consistently for some lookups, and forwarders tend to propagate that we'll cache that too, but only for a
763 if (IN_SET(rcode
, DNS_RCODE_SUCCESS
, DNS_RCODE_NXDOMAIN
)) {
764 if (dns_answer_isempty(answer
)) {
766 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
768 log_debug("Not caching negative entry without a SOA record: %s",
769 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
776 /* Only cache SERVFAIL as "weird" rcode for now. We can add more later, should that turn out to be
778 if (rcode
!= DNS_RCODE_SERVFAIL
)
784 cache_keys
= dns_answer_size(answer
);
788 /* Make some space for our new entries */
789 dns_cache_make_space(c
, cache_keys
);
791 timestamp
= now(CLOCK_BOOTTIME
);
793 /* Second, add in positive entries for all contained RRs */
794 DNS_ANSWER_FOREACH_ITEM(item
, answer
) {
797 if (!FLAGS_SET(item
->flags
, DNS_ANSWER_CACHEABLE
) ||
798 !rr_eligible(item
->rr
))
802 /* We store the auxiliary RRs and packet data in the cache only if they were in
803 * direct response to the original query. If we cache an RR we also received, and
804 * that is just auxiliary information we can't use the data, hence don't. */
806 primary
= dns_resource_key_match_rr(key
, item
->rr
, NULL
);
810 primary
= dns_resource_key_match_cname_or_dname(key
, item
->rr
->key
, NULL
);
819 /* Do not replace existing cache items for primary lookups with non-primary
820 * data. After all the primary lookup data is a lot more useful. */
821 first
= hashmap_get(c
->by_key
, item
->rr
->key
);
822 if (first
&& DNS_CACHE_ITEM_IS_PRIMARY(first
))
826 r
= dns_cache_put_positive(
830 primary
? answer
: NULL
,
831 primary
? full_packet
: NULL
,
832 ((item
->flags
& DNS_ANSWER_AUTHENTICATED
) ? SD_RESOLVED_AUTHENTICATED
: 0) |
833 (query_flags
& SD_RESOLVED_CONFIDENTIAL
),
834 item
->flags
& DNS_ANSWER_SHARED_OWNER
,
840 stale_retention_usec
);
845 if (!key
) /* mDNS doesn't know negative caching, really */
848 /* Third, add in negative entries if the key has no RR */
849 r
= dns_answer_match_key(answer
, key
, NULL
);
855 /* But not if it has a matching CNAME/DNAME (the negative caching will be done on the canonical name,
856 * not on the alias) */
857 r
= dns_answer_find_cname_or_dname(answer
, key
, NULL
, NULL
);
863 /* See https://tools.ietf.org/html/rfc2308, which say that a matching SOA record in the packet is used to
864 * enable negative caching. We apply one exception though: if we are about to cache a weird rcode we do so
865 * regardless of a SOA. */
866 r
= dns_answer_find_soa(answer
, key
, &soa
, &flags
);
869 if (r
== 0 && !weird_rcode
)
872 /* Refuse using the SOA data if it is unsigned, but the key is signed */
873 if (FLAGS_SET(query_flags
, SD_RESOLVED_AUTHENTICATED
) &&
874 (flags
& DNS_ANSWER_AUTHENTICATED
) == 0)
878 if (cache_mode
== DNS_CACHE_MODE_NO_NEGATIVE
) {
879 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
880 log_debug("Not caching negative entry for: %s, cache mode set to no-negative",
881 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
885 r
= dns_cache_put_negative(
904 /* Adding all RRs failed. Let's clean up what we already
905 * added, just in case */
908 dns_cache_remove_by_key(c
, key
);
910 DNS_ANSWER_FOREACH_ITEM(item
, answer
) {
911 if ((item
->flags
& DNS_ANSWER_CACHEABLE
) == 0)
914 dns_cache_remove_by_key(c
, item
->rr
->key
);
920 static DnsCacheItem
*dns_cache_get_by_key_follow_cname_dname_nsec(
923 uint64_t query_flags
) {
932 /* If we hit some OOM error, or suchlike, we don't care too
933 * much, after all this is just a cache */
935 i
= hashmap_get(c
->by_key
, k
);
939 n
= dns_resource_key_name(k
);
941 /* Check if we have an NXDOMAIN cache item for the name, notice that we use
942 * the pseudo-type ANY for NXDOMAIN cache items. */
943 i
= hashmap_get(c
->by_key
, &DNS_RESOURCE_KEY_CONST(k
->class, DNS_TYPE_ANY
, n
));
944 if (i
&& i
->type
== DNS_CACHE_NXDOMAIN
)
947 if (dns_type_may_redirect(k
->type
) && !FLAGS_SET(query_flags
, SD_RESOLVED_NO_CNAME
)) {
948 /* Check if we have a CNAME record instead */
949 i
= hashmap_get(c
->by_key
, &DNS_RESOURCE_KEY_CONST(k
->class, DNS_TYPE_CNAME
, n
));
950 if (i
&& i
->type
!= DNS_CACHE_NODATA
)
953 /* OK, let's look for cached DNAME records. */
958 i
= hashmap_get(c
->by_key
, &DNS_RESOURCE_KEY_CONST(k
->class, DNS_TYPE_DNAME
, n
));
959 if (i
&& i
->type
!= DNS_CACHE_NODATA
)
962 /* Jump one label ahead */
963 r
= dns_name_parent(&n
);
969 if (k
->type
!= DNS_TYPE_NSEC
) {
970 /* Check if we have an NSEC record instead for the name. */
971 i
= hashmap_get(c
->by_key
, &DNS_RESOURCE_KEY_CONST(k
->class, DNS_TYPE_NSEC
, n
));
979 static int answer_add_clamp_ttl(
981 DnsResourceRecord
*rr
,
983 DnsAnswerFlags answer_flags
,
984 DnsResourceRecord
*rrsig
,
985 uint64_t query_flags
,
989 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*patched
= NULL
, *patched_rrsig
= NULL
;
995 if (FLAGS_SET(query_flags
, SD_RESOLVED_CLAMP_TTL
)) {
1000 /* Let's determine how much time is left for this cache entry. Note that we round down, but
1001 * clamp this to be 1s at minimum, since we usually want records to remain cached better too
1002 * short a time than too long a time, but otoh don't want to return 0 ever, since that has
1003 * special semantics in various contexts — in particular in mDNS */
1005 left_ttl
= MAX(1U, LESS_BY(until
, current
) / USEC_PER_SEC
);
1007 patched
= dns_resource_record_ref(rr
);
1009 r
= dns_resource_record_clamp_ttl(&patched
, left_ttl
);
1016 patched_rrsig
= dns_resource_record_ref(rrsig
);
1017 r
= dns_resource_record_clamp_ttl(&patched_rrsig
, left_ttl
);
1021 rrsig
= patched_rrsig
;
1025 r
= dns_answer_add_extend(answer
, rr
, ifindex
, answer_flags
, rrsig
);
1032 int dns_cache_lookup(
1034 DnsResourceKey
*key
,
1035 uint64_t query_flags
,
1037 DnsAnswer
**ret_answer
,
1038 DnsPacket
**ret_full_packet
,
1039 uint64_t *ret_query_flags
,
1040 DnssecResult
*ret_dnssec_result
) {
1042 _cleanup_(dns_packet_unrefp
) DnsPacket
*full_packet
= NULL
;
1043 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
1044 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
1047 bool nxdomain
= false;
1048 DnsCacheItem
*first
, *nsec
= NULL
;
1049 bool have_authenticated
= false, have_non_authenticated
= false, have_confidential
= false, have_non_confidential
= false;
1051 int found_rcode
= -1;
1052 DnssecResult dnssec_result
= -1;
1053 int have_dnssec_result
= -1;
1058 if (key
->type
== DNS_TYPE_ANY
|| key
->class == DNS_CLASS_ANY
) {
1059 /* If we have ANY lookups we don't use the cache, so that the caller refreshes via the
1062 log_debug("Ignoring cache for ANY lookup: %s",
1063 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
1067 first
= dns_cache_get_by_key_follow_cname_dname_nsec(c
, key
, query_flags
);
1069 /* If one question cannot be answered we need to refresh */
1071 log_debug("Cache miss for %s",
1072 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
1076 if ((query_flags
& (SD_RESOLVED_CLAMP_TTL
| SD_RESOLVED_NO_STALE
)) != 0) {
1077 /* 'current' is always passed to answer_add_clamp_ttl(), but is only used conditionally.
1078 * We'll do the same assert there to make sure that it was initialized properly.
1079 * 'current' is also used below when SD_RESOLVED_NO_STALE is set. */
1080 current
= now(CLOCK_BOOTTIME
);
1081 assert(current
> 0);
1084 LIST_FOREACH(by_key
, j
, first
) {
1085 /* If the caller doesn't allow us to answer questions from cache data learned from
1086 * "side-effect", skip this entry. */
1087 if (FLAGS_SET(query_flags
, SD_RESOLVED_REQUIRE_PRIMARY
) &&
1088 !DNS_CACHE_ITEM_IS_PRIMARY(j
)) {
1089 log_debug("Primary answer was requested for cache lookup for %s, which we don't have.",
1090 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
1095 /* Skip the next part if ttl is expired and requested with no stale flag. */
1096 if (FLAGS_SET(query_flags
, SD_RESOLVED_NO_STALE
) && j
->until_valid
< current
) {
1097 log_debug("Requested with no stale and TTL expired for %s",
1098 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
1103 if (j
->type
== DNS_CACHE_NXDOMAIN
)
1105 else if (j
->type
== DNS_CACHE_RCODE
)
1106 found_rcode
= j
->rcode
;
1108 if (j
->rr
->key
->type
== DNS_TYPE_NSEC
)
1114 if (FLAGS_SET(j
->query_flags
, SD_RESOLVED_AUTHENTICATED
))
1115 have_authenticated
= true;
1117 have_non_authenticated
= true;
1119 if (FLAGS_SET(j
->query_flags
, SD_RESOLVED_CONFIDENTIAL
))
1120 have_confidential
= true;
1122 have_non_confidential
= true;
1124 if (j
->dnssec_result
< 0) {
1125 have_dnssec_result
= false; /* an entry without dnssec result? then invalidate things for good */
1126 dnssec_result
= _DNSSEC_RESULT_INVALID
;
1127 } else if (have_dnssec_result
< 0) {
1128 have_dnssec_result
= true; /* So far no result seen, let's pick this one up */
1129 dnssec_result
= j
->dnssec_result
;
1130 } else if (have_dnssec_result
> 0 && j
->dnssec_result
!= dnssec_result
) {
1131 have_dnssec_result
= false; /* conflicting result seen? then invalidate for good */
1132 dnssec_result
= _DNSSEC_RESULT_INVALID
;
1135 /* If the question is being resolved using stale data, the clamp TTL will be set to CACHE_STALE_TTL_MAX_USEC. */
1136 usec_t until
= FLAGS_SET(query_flags
, SD_RESOLVED_NO_STALE
) ? j
->until_valid
1137 : usec_add(current
, CACHE_STALE_TTL_MAX_USEC
);
1139 /* Append the answer RRs to our answer. Ideally we have the answer object, which we
1140 * preferably use. But if the cached entry was generated as "side-effect" of a reply,
1141 * i.e. from validated auxiliary records rather than from the main reply, then we use the
1142 * individual RRs only instead. */
1145 /* Minor optimization, if the full answer object of this and the previous RR is the
1146 * same, don't bother adding it again. Typically we store a full RRset here, hence
1147 * that should be the case. */
1148 if (!j
->by_key_prev
|| j
->answer
!= j
->by_key_prev
->answer
) {
1149 DnsAnswerItem
*item
;
1151 DNS_ANSWER_FOREACH_ITEM(item
, j
->answer
) {
1152 r
= answer_add_clamp_ttl(
1167 r
= answer_add_clamp_ttl(
1171 FLAGS_SET(j
->query_flags
, SD_RESOLVED_AUTHENTICATED
) ? DNS_ANSWER_AUTHENTICATED
: 0,
1180 /* We'll return any packet we have for this. Typically all cache entries for the same key
1181 * should come from the same packet anyway, hence it doesn't really matter which packet we
1182 * return here, they should all resolve to the same anyway. */
1183 if (!full_packet
&& j
->full_packet
)
1184 full_packet
= dns_packet_ref(j
->full_packet
);
1187 if (found_rcode
>= 0) {
1188 log_debug("RCODE %s cache hit for %s",
1189 FORMAT_DNS_RCODE(found_rcode
),
1190 dns_resource_key_to_string(key
, key_str
, sizeof(key_str
)));
1193 *ret_rcode
= found_rcode
;
1195 *ret_answer
= TAKE_PTR(answer
);
1196 if (ret_full_packet
)
1197 *ret_full_packet
= TAKE_PTR(full_packet
);
1198 if (ret_query_flags
)
1199 *ret_query_flags
= 0;
1200 if (ret_dnssec_result
)
1201 *ret_dnssec_result
= dnssec_result
;
1207 if (nsec
&& !IN_SET(key
->type
, DNS_TYPE_NSEC
, DNS_TYPE_DS
)) {
1208 /* Note that we won't derive information for DS RRs from an NSEC, because we only cache NSEC
1209 * RRs from the lower-zone of a zone cut, but the DS RRs are on the upper zone. */
1211 log_debug("NSEC NODATA cache hit for %s",
1212 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
1214 /* We only found an NSEC record that matches our name. If it says the type doesn't exist
1215 * report NODATA. Otherwise report a cache miss. */
1218 *ret_rcode
= DNS_RCODE_SUCCESS
;
1220 *ret_answer
= TAKE_PTR(answer
);
1221 if (ret_full_packet
)
1222 *ret_full_packet
= TAKE_PTR(full_packet
);
1223 if (ret_query_flags
)
1224 *ret_query_flags
= nsec
->query_flags
;
1225 if (ret_dnssec_result
)
1226 *ret_dnssec_result
= nsec
->dnssec_result
;
1228 if (!bitmap_isset(nsec
->rr
->nsec
.types
, key
->type
) &&
1229 !bitmap_isset(nsec
->rr
->nsec
.types
, DNS_TYPE_CNAME
) &&
1230 !bitmap_isset(nsec
->rr
->nsec
.types
, DNS_TYPE_DNAME
)) {
1239 log_debug("%s cache hit for %s",
1240 n
> 0 ? "Positive" :
1241 nxdomain
? "NXDOMAIN" : "NODATA",
1242 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
1248 *ret_rcode
= nxdomain
? DNS_RCODE_NXDOMAIN
: DNS_RCODE_SUCCESS
;
1250 *ret_answer
= TAKE_PTR(answer
);
1251 if (ret_full_packet
)
1252 *ret_full_packet
= TAKE_PTR(full_packet
);
1253 if (ret_query_flags
)
1255 ((have_authenticated
&& !have_non_authenticated
) ? SD_RESOLVED_AUTHENTICATED
: 0) |
1256 ((have_confidential
&& !have_non_confidential
) ? SD_RESOLVED_CONFIDENTIAL
: 0);
1257 if (ret_dnssec_result
)
1258 *ret_dnssec_result
= dnssec_result
;
1266 *ret_rcode
= DNS_RCODE_SUCCESS
;
1268 *ret_answer
= TAKE_PTR(answer
);
1269 if (ret_full_packet
)
1270 *ret_full_packet
= TAKE_PTR(full_packet
);
1271 if (ret_query_flags
)
1273 ((have_authenticated
&& !have_non_authenticated
) ? SD_RESOLVED_AUTHENTICATED
: 0) |
1274 ((have_confidential
&& !have_non_confidential
) ? SD_RESOLVED_CONFIDENTIAL
: 0);
1275 if (ret_dnssec_result
)
1276 *ret_dnssec_result
= dnssec_result
;
1282 *ret_rcode
= DNS_RCODE_SUCCESS
;
1285 if (ret_full_packet
)
1286 *ret_full_packet
= NULL
;
1287 if (ret_query_flags
)
1288 *ret_query_flags
= 0;
1289 if (ret_dnssec_result
)
1290 *ret_dnssec_result
= _DNSSEC_RESULT_INVALID
;
1296 int dns_cache_check_conflicts(DnsCache
*cache
, DnsResourceRecord
*rr
, int owner_family
, const union in_addr_union
*owner_address
) {
1297 DnsCacheItem
*first
;
1298 bool same_owner
= true;
1303 dns_cache_prune(cache
);
1305 /* See if there's a cache entry for the same key. If there
1306 * isn't there's no conflict */
1307 first
= hashmap_get(cache
->by_key
, rr
->key
);
1311 /* See if the RR key is owned by the same owner, if so, there
1312 * isn't a conflict either */
1313 LIST_FOREACH(by_key
, i
, first
) {
1314 if (i
->owner_family
!= owner_family
||
1315 !in_addr_equal(owner_family
, &i
->owner_address
, owner_address
)) {
1323 /* See if there's the exact same RR in the cache. If yes, then
1324 * there's no conflict. */
1325 if (dns_cache_get(cache
, rr
))
1328 /* There's a conflict */
1332 int dns_cache_export_shared_to_packet(DnsCache
*cache
, DnsPacket
*p
, usec_t ts
, unsigned max_rr
) {
1333 unsigned ancount
= 0;
1339 assert(p
->protocol
== DNS_PROTOCOL_MDNS
);
1341 HASHMAP_FOREACH(i
, cache
->by_key
)
1342 LIST_FOREACH(by_key
, j
, i
) {
1346 if (!j
->shared_owner
)
1349 /* Ignore cached goodby packet. See on_mdns_packet() and RFC 6762 section 10.1. */
1350 if (j
->rr
->ttl
<= 1)
1353 /* RFC6762 7.1: Don't append records with less than half the TTL remaining
1354 * as known answers. */
1355 if (usec_sub_unsigned(j
->until
, ts
) < j
->rr
->ttl
* USEC_PER_SEC
/ 2)
1358 if (max_rr
> 0 && ancount
>= max_rr
) {
1359 DNS_PACKET_HEADER(p
)->ancount
= htobe16(ancount
);
1362 r
= dns_packet_new_query(&p
->more
, p
->protocol
, 0, true);
1371 r
= dns_packet_append_rr(p
, j
->rr
, 0, NULL
, NULL
);
1372 if (r
== -EMSGSIZE
) {
1374 /* If max_rr == 0, do not allocate more packets. */
1377 /* If we're unable to stuff all known answers into the given packet, allocate
1378 * a new one, push the RR into that one and link it to the current one. */
1380 DNS_PACKET_HEADER(p
)->ancount
= htobe16(ancount
);
1383 r
= dns_packet_new_query(&p
->more
, p
->protocol
, 0, true);
1387 /* continue with new packet */
1389 r
= dns_packet_append_rr(p
, j
->rr
, 0, NULL
, NULL
);
1399 DNS_PACKET_HEADER(p
)->ancount
= htobe16(ancount
);
1404 void dns_cache_dump(DnsCache
*cache
, FILE *f
) {
1413 HASHMAP_FOREACH(i
, cache
->by_key
)
1414 LIST_FOREACH(by_key
, j
, i
) {
1420 t
= dns_resource_record_to_string(j
->rr
);
1429 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
1431 fputs(dns_resource_key_to_string(j
->key
, key_str
, sizeof key_str
), f
);
1433 fputs(dns_cache_item_type_to_string(j
), f
);
1439 int dns_cache_dump_to_json(DnsCache
*cache
, sd_json_variant
**ret
) {
1440 _cleanup_(sd_json_variant_unrefp
) sd_json_variant
*c
= NULL
;
1447 HASHMAP_FOREACH(i
, cache
->by_key
) {
1448 _cleanup_(sd_json_variant_unrefp
) sd_json_variant
*d
= NULL
, *k
= NULL
;
1450 r
= dns_resource_key_to_json(i
->key
, &k
);
1455 _cleanup_(sd_json_variant_unrefp
) sd_json_variant
*l
= NULL
;
1457 LIST_FOREACH(by_key
, j
, i
) {
1458 _cleanup_(sd_json_variant_unrefp
) sd_json_variant
*rj
= NULL
;
1462 r
= dns_resource_record_to_json(j
->rr
, &rj
);
1466 r
= dns_resource_record_to_wire_format(j
->rr
, /* canonical= */ false); /* don't use DNSSEC canonical format, since it removes casing, but we want that for DNS_SD compat */
1470 r
= sd_json_variant_append_arraybo(
1472 SD_JSON_BUILD_PAIR_VARIANT("rr", rj
),
1473 SD_JSON_BUILD_PAIR_BASE64("raw", j
->rr
->wire_format
, j
->rr
->wire_format_size
));
1479 r
= sd_json_variant_new_array(&l
, NULL
, 0);
1486 SD_JSON_BUILD_PAIR_VARIANT("key", k
),
1487 SD_JSON_BUILD_PAIR_VARIANT("rrs", l
),
1488 SD_JSON_BUILD_PAIR_UNSIGNED("until", i
->until
));
1489 } else if (i
->type
== DNS_CACHE_NODATA
) {
1492 SD_JSON_BUILD_PAIR_VARIANT("key", k
),
1493 SD_JSON_BUILD_PAIR_EMPTY_ARRAY("rrs"),
1494 SD_JSON_BUILD_PAIR_UNSIGNED("until", i
->until
));
1498 SD_JSON_BUILD_PAIR_VARIANT("key", k
),
1499 SD_JSON_BUILD_PAIR_STRING("type", dns_cache_item_type_to_string(i
)),
1500 SD_JSON_BUILD_PAIR_UNSIGNED("until", i
->until
));
1504 r
= sd_json_variant_append_array(&c
, d
);
1510 return sd_json_variant_new_array(ret
, NULL
, 0);
1516 bool dns_cache_is_empty(DnsCache
*cache
) {
1520 return hashmap_isempty(cache
->by_key
);
1523 unsigned dns_cache_size(DnsCache
*cache
) {
1527 return hashmap_size(cache
->by_key
);