1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include "alloc-util.h"
7 #include "dns-domain.h"
8 #include "format-util.h"
9 #include "resolved-dns-answer.h"
10 #include "resolved-dns-cache.h"
11 #include "resolved-dns-packet.h"
12 #include "string-util.h"
14 /* Never cache more than 4K entries. RFC 1536, Section 5 suggests to
15 * leave DNS caches unbounded, but that's crazy. */
16 #define CACHE_MAX 4096
18 /* We never keep any item longer than 2h in our cache unless StaleRetentionSec is greater than zero. */
19 #define CACHE_TTL_MAX_USEC (2 * USEC_PER_HOUR)
21 /* The max TTL for stale data is set to 30 seconds. See RFC 8767, Section 6. */
22 #define CACHE_STALE_TTL_MAX_USEC (30 * USEC_PER_SEC)
24 /* How long to cache strange rcodes, i.e. rcodes != SUCCESS and != NXDOMAIN (specifically: that's only SERVFAIL for
26 #define CACHE_TTL_STRANGE_RCODE_USEC (10 * USEC_PER_SEC)
28 #define CACHEABLE_QUERY_FLAGS (SD_RESOLVED_AUTHENTICATED|SD_RESOLVED_CONFIDENTIAL)
30 typedef enum DnsCacheItemType DnsCacheItemType
;
31 typedef struct DnsCacheItem DnsCacheItem
;
33 enum DnsCacheItemType
{
37 DNS_CACHE_RCODE
, /* "strange" RCODE (effective only SERVFAIL for now) */
41 DnsCacheItemType type
;
43 DnsResourceKey
*key
; /* The key for this item, i.e. the lookup key */
44 DnsResourceRecord
*rr
; /* The RR for this item, i.e. the lookup value for positive queries */
45 DnsAnswer
*answer
; /* The full validated answer, if this is an RRset acquired via a "primary" lookup */
46 DnsPacket
*full_packet
; /* The full packet this information was acquired with */
48 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. */
49 usec_t until_valid
; /* The key is for storing the time when the TTL set to expire. */
50 uint64_t query_flags
; /* SD_RESOLVED_AUTHENTICATED and/or SD_RESOLVED_CONFIDENTIAL */
51 DnssecResult dnssec_result
;
55 union in_addr_union owner_address
;
58 LIST_FIELDS(DnsCacheItem
, by_key
);
63 /* Returns true if this is a cache item created as result of an explicit lookup, or created as "side-effect"
64 * of another request. "Primary" entries will carry the full answer data (with NSEC, …) that can aso prove
65 * wildcard expansion, non-existence and such, while entries that were created as "side-effect" just contain
66 * immediate RR data for the specified RR key, but nothing else. */
67 #define DNS_CACHE_ITEM_IS_PRIMARY(item) (!!(item)->answer)
69 static const char *dns_cache_item_type_to_string(DnsCacheItem
*item
) {
74 case DNS_CACHE_POSITIVE
:
77 case DNS_CACHE_NODATA
:
80 case DNS_CACHE_NXDOMAIN
:
84 return dns_rcode_to_string(item
->rcode
);
90 static DnsCacheItem
* dns_cache_item_free(DnsCacheItem
*i
) {
94 dns_resource_record_unref(i
->rr
);
95 dns_resource_key_unref(i
->key
);
96 dns_answer_unref(i
->answer
);
97 dns_packet_unref(i
->full_packet
);
100 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsCacheItem
*, dns_cache_item_free
);
102 static void dns_cache_item_unlink_and_free(DnsCache
*c
, DnsCacheItem
*i
) {
110 first
= hashmap_get(c
->by_key
, i
->key
);
111 LIST_REMOVE(by_key
, first
, i
);
114 assert_se(hashmap_replace(c
->by_key
, first
->key
, first
) >= 0);
116 hashmap_remove(c
->by_key
, i
->key
);
118 prioq_remove(c
->by_expiry
, i
, &i
->prioq_idx
);
120 dns_cache_item_free(i
);
123 static bool dns_cache_remove_by_rr(DnsCache
*c
, DnsResourceRecord
*rr
) {
127 first
= hashmap_get(c
->by_key
, rr
->key
);
128 LIST_FOREACH(by_key
, i
, first
) {
129 r
= dns_resource_record_equal(i
->rr
, rr
);
133 dns_cache_item_unlink_and_free(c
, i
);
141 static bool dns_cache_remove_by_key(DnsCache
*c
, DnsResourceKey
*key
) {
147 first
= hashmap_remove(c
->by_key
, key
);
151 LIST_FOREACH(by_key
, i
, first
) {
152 prioq_remove(c
->by_expiry
, i
, &i
->prioq_idx
);
153 dns_cache_item_free(i
);
159 void dns_cache_flush(DnsCache
*c
) {
164 while ((key
= hashmap_first_key(c
->by_key
)))
165 dns_cache_remove_by_key(c
, key
);
167 assert(hashmap_size(c
->by_key
) == 0);
168 assert(prioq_size(c
->by_expiry
) == 0);
170 c
->by_key
= hashmap_free(c
->by_key
);
171 c
->by_expiry
= prioq_free(c
->by_expiry
);
174 static void dns_cache_make_space(DnsCache
*c
, unsigned add
) {
180 /* Makes space for n new entries. Note that we actually allow
181 * the cache to grow beyond CACHE_MAX, but only when we shall
182 * add more RRs to the cache than CACHE_MAX at once. In that
183 * case the cache will be emptied completely otherwise. */
186 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
189 if (prioq_size(c
->by_expiry
) <= 0)
192 if (prioq_size(c
->by_expiry
) + add
< CACHE_MAX
)
195 i
= prioq_peek(c
->by_expiry
);
198 /* Take an extra reference to the key so that it
199 * doesn't go away in the middle of the remove call */
200 key
= dns_resource_key_ref(i
->key
);
201 dns_cache_remove_by_key(c
, key
);
205 void dns_cache_prune(DnsCache
*c
) {
210 /* Remove all entries that are past their TTL */
214 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
216 i
= prioq_peek(c
->by_expiry
);
221 t
= now(CLOCK_BOOTTIME
);
226 /* Depending whether this is an mDNS shared entry
227 * either remove only this one RR or the whole RRset */
228 log_debug("Removing %scache entry for %s (expired "USEC_FMT
"s ago)",
229 i
->shared_owner
? "shared " : "",
230 dns_resource_key_to_string(i
->key
, key_str
, sizeof key_str
),
231 (t
- i
->until
) / USEC_PER_SEC
);
234 dns_cache_item_unlink_and_free(c
, i
);
236 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
238 /* Take an extra reference to the key so that it
239 * doesn't go away in the middle of the remove call */
240 key
= dns_resource_key_ref(i
->key
);
241 dns_cache_remove_by_key(c
, key
);
246 static int dns_cache_item_prioq_compare_func(const void *a
, const void *b
) {
247 const DnsCacheItem
*x
= a
, *y
= b
;
249 return CMP(x
->until
, y
->until
);
252 static int dns_cache_init(DnsCache
*c
) {
257 r
= prioq_ensure_allocated(&c
->by_expiry
, dns_cache_item_prioq_compare_func
);
261 r
= hashmap_ensure_allocated(&c
->by_key
, &dns_resource_key_hash_ops
);
268 static int dns_cache_link_item(DnsCache
*c
, DnsCacheItem
*i
) {
275 r
= prioq_put(c
->by_expiry
, i
, &i
->prioq_idx
);
279 first
= hashmap_get(c
->by_key
, i
->key
);
281 _unused_
_cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*k
= NULL
;
283 /* Keep a reference to the original key, while we manipulate the list. */
284 k
= dns_resource_key_ref(first
->key
);
286 /* Now, try to reduce the number of keys we keep */
287 dns_resource_key_reduce(&first
->key
, &i
->key
);
290 dns_resource_key_reduce(&first
->rr
->key
, &i
->key
);
292 dns_resource_key_reduce(&i
->rr
->key
, &i
->key
);
294 LIST_PREPEND(by_key
, first
, i
);
295 assert_se(hashmap_replace(c
->by_key
, first
->key
, first
) >= 0);
297 r
= hashmap_put(c
->by_key
, i
->key
, i
);
299 prioq_remove(c
->by_expiry
, i
, &i
->prioq_idx
);
307 static DnsCacheItem
* dns_cache_get(DnsCache
*c
, DnsResourceRecord
*rr
) {
311 LIST_FOREACH(by_key
, i
, (DnsCacheItem
*) hashmap_get(c
->by_key
, rr
->key
))
312 if (i
->rr
&& dns_resource_record_equal(i
->rr
, rr
) > 0)
318 static usec_t
calculate_until_valid(
319 DnsResourceRecord
*rr
,
323 bool use_soa_minimum
) {
330 ttl
= MIN(min_ttl
, nsec_ttl
);
331 if (rr
->key
->type
== DNS_TYPE_SOA
&& use_soa_minimum
) {
332 /* If this is a SOA RR, and it is requested, clamp to the SOA's minimum field. This is used
333 * when we do negative caching, to determine the TTL for the negative caching entry. See RFC
334 * 2308, Section 5. */
336 if (ttl
> rr
->soa
.minimum
)
337 ttl
= rr
->soa
.minimum
;
340 u
= ttl
* USEC_PER_SEC
;
341 if (u
> CACHE_TTL_MAX_USEC
)
342 u
= CACHE_TTL_MAX_USEC
;
344 if (rr
->expiry
!= USEC_INFINITY
) {
347 /* Make use of the DNSSEC RRSIG expiry info, if we have it */
349 left
= LESS_BY(rr
->expiry
, now(CLOCK_REALTIME
));
354 return timestamp
+ u
;
357 static usec_t
calculate_until(
359 usec_t stale_retention_usec
) {
361 return stale_retention_usec
> 0 ? usec_add(until_valid
, stale_retention_usec
) : until_valid
;
364 static void dns_cache_item_update_positive(
367 DnsResourceRecord
*rr
,
369 DnsPacket
*full_packet
,
371 uint64_t query_flags
,
373 DnssecResult dnssec_result
,
377 const union in_addr_union
*owner_address
,
378 usec_t stale_retention_usec
) {
383 assert(owner_address
);
385 i
->type
= DNS_CACHE_POSITIVE
;
388 /* We are the first item in the list, we need to
389 * update the key used in the hashmap */
391 assert_se(hashmap_replace(c
->by_key
, rr
->key
, i
) >= 0);
393 DNS_RR_REPLACE(i
->rr
, dns_resource_record_ref(rr
));
395 DNS_RESOURCE_KEY_REPLACE(i
->key
, dns_resource_key_ref(rr
->key
));
397 DNS_ANSWER_REPLACE(i
->answer
, dns_answer_ref(answer
));
399 DNS_PACKET_REPLACE(i
->full_packet
, dns_packet_ref(full_packet
));
401 i
->until_valid
= calculate_until_valid(rr
, min_ttl
, UINT32_MAX
, timestamp
, false);
402 i
->until
= calculate_until(i
->until_valid
, stale_retention_usec
);
403 i
->query_flags
= query_flags
& CACHEABLE_QUERY_FLAGS
;
404 i
->shared_owner
= shared_owner
;
405 i
->dnssec_result
= dnssec_result
;
407 i
->ifindex
= ifindex
;
409 i
->owner_family
= owner_family
;
410 i
->owner_address
= *owner_address
;
412 prioq_reshuffle(c
->by_expiry
, i
, &i
->prioq_idx
);
415 static int dns_cache_put_positive(
417 DnsProtocol protocol
,
418 DnsResourceRecord
*rr
,
420 DnsPacket
*full_packet
,
421 uint64_t query_flags
,
423 DnssecResult dnssec_result
,
427 const union in_addr_union
*owner_address
,
428 usec_t stale_retention_usec
) {
430 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
431 DnsCacheItem
*existing
;
437 assert(owner_address
);
439 /* Never cache pseudo RRs */
440 if (dns_class_is_pseudo(rr
->key
->class))
442 if (dns_type_is_pseudo(rr
->key
->type
))
445 /* Determine the minimal TTL of all RRs in the answer plus the one by the main RR we are supposed to
446 * cache. Since we cache whole answers to questions we should never return answers where only some
447 * RRs are still valid, hence find the lowest here */
448 min_ttl
= MIN(dns_answer_min_ttl(answer
), rr
->ttl
);
450 /* New TTL is 0? Delete this specific entry... */
452 r
= dns_cache_remove_by_rr(c
, rr
);
454 r
> 0 ? "Removed zero TTL entry from cache" : "Not caching zero TTL cache entry",
455 dns_resource_key_to_string(rr
->key
, key_str
, sizeof key_str
));
459 /* Entry exists already? Update TTL, timestamp and owner */
460 existing
= dns_cache_get(c
, rr
);
462 dns_cache_item_update_positive(
476 stale_retention_usec
);
480 /* Do not cache mDNS goodbye packet. */
481 if (protocol
== DNS_PROTOCOL_MDNS
&& rr
->ttl
<= 1)
484 /* Otherwise, add the new RR */
485 r
= dns_cache_init(c
);
489 dns_cache_make_space(c
, 1);
491 _cleanup_(dns_cache_item_freep
) DnsCacheItem
*i
= new(DnsCacheItem
, 1);
495 /* If StaleRetentionSec is greater than zero, the 'until' property is set to a duration
496 * of StaleRetentionSec from the time of TTL expiry.
497 * If StaleRetentionSec is zero, both the 'until' and 'until_valid' are set to the TTL duration,
498 * leading to the eviction of the record once the TTL expires.*/
499 usec_t until_valid
= calculate_until_valid(rr
, min_ttl
, UINT32_MAX
, timestamp
, false);
500 *i
= (DnsCacheItem
) {
501 .type
= DNS_CACHE_POSITIVE
,
502 .key
= dns_resource_key_ref(rr
->key
),
503 .rr
= dns_resource_record_ref(rr
),
504 .answer
= dns_answer_ref(answer
),
505 .full_packet
= dns_packet_ref(full_packet
),
506 .until
= calculate_until(until_valid
, stale_retention_usec
),
507 .until_valid
= until_valid
,
508 .query_flags
= query_flags
& CACHEABLE_QUERY_FLAGS
,
509 .shared_owner
= shared_owner
,
510 .dnssec_result
= dnssec_result
,
512 .owner_family
= owner_family
,
513 .owner_address
= *owner_address
,
514 .prioq_idx
= PRIOQ_IDX_NULL
,
517 r
= dns_cache_link_item(c
, i
);
521 log_debug("Added positive %s %s%s cache entry for %s "USEC_FMT
"s on %s/%s/%s",
522 FLAGS_SET(i
->query_flags
, SD_RESOLVED_AUTHENTICATED
) ? "authenticated" : "unauthenticated",
523 FLAGS_SET(i
->query_flags
, SD_RESOLVED_CONFIDENTIAL
) ? "confidential" : "non-confidential",
524 i
->shared_owner
? " shared" : "",
525 dns_resource_key_to_string(i
->key
, key_str
, sizeof key_str
),
526 (i
->until
- timestamp
) / USEC_PER_SEC
,
527 i
->ifindex
== 0 ? "*" : FORMAT_IFNAME(i
->ifindex
),
528 af_to_name_short(i
->owner_family
),
529 IN_ADDR_TO_STRING(i
->owner_family
, &i
->owner_address
));
535 static int dns_cache_put_negative(
540 DnsPacket
*full_packet
,
541 uint64_t query_flags
,
542 DnssecResult dnssec_result
,
545 DnsResourceRecord
*soa
,
547 const union in_addr_union
*owner_address
) {
549 _cleanup_(dns_cache_item_freep
) DnsCacheItem
*i
= NULL
;
550 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
555 assert(owner_address
);
557 /* Never cache pseudo RR keys. DNS_TYPE_ANY is particularly
558 * important to filter out as we use this as a pseudo-type for
559 * NXDOMAIN entries */
560 if (dns_class_is_pseudo(key
->class))
562 if (dns_type_is_pseudo(key
->type
))
565 if (IN_SET(rcode
, DNS_RCODE_SUCCESS
, DNS_RCODE_NXDOMAIN
)) {
569 /* For negative replies, check if we have a TTL of a SOA */
570 if (nsec_ttl
<= 0 || soa
->soa
.minimum
<= 0 || soa
->ttl
<= 0) {
571 log_debug("Not caching negative entry with zero SOA/NSEC/NSEC3 TTL: %s",
572 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
575 } else if (rcode
!= DNS_RCODE_SERVFAIL
)
578 r
= dns_cache_init(c
);
582 dns_cache_make_space(c
, 1);
584 i
= new(DnsCacheItem
, 1);
588 *i
= (DnsCacheItem
) {
590 rcode
== DNS_RCODE_SUCCESS
? DNS_CACHE_NODATA
:
591 rcode
== DNS_RCODE_NXDOMAIN
? DNS_CACHE_NXDOMAIN
: DNS_CACHE_RCODE
,
592 .query_flags
= query_flags
& CACHEABLE_QUERY_FLAGS
,
593 .dnssec_result
= dnssec_result
,
594 .owner_family
= owner_family
,
595 .owner_address
= *owner_address
,
596 .prioq_idx
= PRIOQ_IDX_NULL
,
598 .answer
= dns_answer_ref(answer
),
599 .full_packet
= dns_packet_ref(full_packet
),
602 /* Determine how long to cache this entry. In case we have some RRs in the answer use the lowest TTL
603 * of any of them. Typically that's the SOA's TTL, which is OK, but could possibly be lower because
604 * of some other RR. Let's better take the lowest option here than a needlessly high one */
605 i
->until
= i
->until_valid
=
606 i
->type
== DNS_CACHE_RCODE
? timestamp
+ CACHE_TTL_STRANGE_RCODE_USEC
:
607 calculate_until_valid(soa
, dns_answer_min_ttl(answer
), nsec_ttl
, timestamp
, true);
609 if (i
->type
== DNS_CACHE_NXDOMAIN
) {
610 /* NXDOMAIN entries should apply equally to all types, so we use ANY as
611 * a pseudo type for this purpose here. */
612 i
->key
= dns_resource_key_new(key
->class, DNS_TYPE_ANY
, dns_resource_key_name(key
));
616 /* Make sure to remove any previous entry for this
617 * specific ANY key. (For non-ANY keys the cache data
618 * is already cleared by the caller.) Note that we
619 * don't bother removing positive or NODATA cache
620 * items in this case, because it would either be slow
621 * or require explicit indexing by name */
622 dns_cache_remove_by_key(c
, key
);
624 i
->key
= dns_resource_key_ref(key
);
626 r
= dns_cache_link_item(c
, i
);
630 log_debug("Added %s cache entry for %s "USEC_FMT
"s",
631 dns_cache_item_type_to_string(i
),
632 dns_resource_key_to_string(i
->key
, key_str
, sizeof key_str
),
633 (i
->until
- timestamp
) / USEC_PER_SEC
);
639 static void dns_cache_remove_previous(
644 DnsResourceRecord
*rr
;
645 DnsAnswerFlags flags
;
649 /* First, if we were passed a key (i.e. on LLMNR/DNS, but
650 * not on mDNS), delete all matching old RRs, so that we only
651 * keep complete by_key in place. */
653 dns_cache_remove_by_key(c
, key
);
655 /* Second, flush all entries matching the answer, unless this
656 * is an RR that is explicitly marked to be "shared" between
657 * peers (i.e. mDNS RRs without the flush-cache bit set). */
658 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
659 if ((flags
& DNS_ANSWER_CACHEABLE
) == 0)
662 if (flags
& DNS_ANSWER_SHARED_OWNER
)
665 dns_cache_remove_by_key(c
, rr
->key
);
669 static bool rr_eligible(DnsResourceRecord
*rr
) {
672 /* When we see an NSEC/NSEC3 RR, we'll only cache it if it is from the lower zone, not the upper zone, since
673 * that's where the interesting bits are (with exception of DS RRs). Of course, this way we cannot derive DS
674 * existence from any cached NSEC/NSEC3, but that should be fine. */
676 switch (rr
->key
->type
) {
679 return !bitmap_isset(rr
->nsec
.types
, DNS_TYPE_NS
) ||
680 bitmap_isset(rr
->nsec
.types
, DNS_TYPE_SOA
);
683 return !bitmap_isset(rr
->nsec3
.types
, DNS_TYPE_NS
) ||
684 bitmap_isset(rr
->nsec3
.types
, DNS_TYPE_SOA
);
693 DnsCacheMode cache_mode
,
694 DnsProtocol protocol
,
698 DnsPacket
*full_packet
,
699 uint64_t query_flags
,
700 DnssecResult dnssec_result
,
703 const union in_addr_union
*owner_address
,
704 usec_t stale_retention_usec
) {
706 DnsResourceRecord
*soa
= NULL
;
707 bool weird_rcode
= false;
709 DnsAnswerFlags flags
;
715 assert(owner_address
);
717 dns_cache_remove_previous(c
, key
, answer
);
719 /* We only care for positive replies and NXDOMAINs, on all other replies we will simply flush the respective
720 * entries, and that's it. (Well, with one further exception: since some DNS zones (akamai!) return SERVFAIL
721 * consistently for some lookups, and forwarders tend to propagate that we'll cache that too, but only for a
724 if (IN_SET(rcode
, DNS_RCODE_SUCCESS
, DNS_RCODE_NXDOMAIN
)) {
725 if (dns_answer_isempty(answer
)) {
727 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
729 log_debug("Not caching negative entry without a SOA record: %s",
730 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
737 /* Only cache SERVFAIL as "weird" rcode for now. We can add more later, should that turn out to be
739 if (rcode
!= DNS_RCODE_SERVFAIL
)
745 cache_keys
= dns_answer_size(answer
);
749 /* Make some space for our new entries */
750 dns_cache_make_space(c
, cache_keys
);
752 timestamp
= now(CLOCK_BOOTTIME
);
754 /* Second, add in positive entries for all contained RRs */
755 DNS_ANSWER_FOREACH_ITEM(item
, answer
) {
758 if (!FLAGS_SET(item
->flags
, DNS_ANSWER_CACHEABLE
) ||
759 !rr_eligible(item
->rr
))
763 /* We store the auxiliary RRs and packet data in the cache only if they were in
764 * direct response to the original query. If we cache an RR we also received, and
765 * that is just auxiliary information we can't use the data, hence don't. */
767 primary
= dns_resource_key_match_rr(key
, item
->rr
, NULL
);
771 primary
= dns_resource_key_match_cname_or_dname(key
, item
->rr
->key
, NULL
);
780 /* Do not replace existing cache items for primary lookups with non-primary
781 * data. After all the primary lookup data is a lot more useful. */
782 first
= hashmap_get(c
->by_key
, item
->rr
->key
);
783 if (first
&& DNS_CACHE_ITEM_IS_PRIMARY(first
))
787 r
= dns_cache_put_positive(
791 primary
? answer
: NULL
,
792 primary
? full_packet
: NULL
,
793 ((item
->flags
& DNS_ANSWER_AUTHENTICATED
) ? SD_RESOLVED_AUTHENTICATED
: 0) |
794 (query_flags
& SD_RESOLVED_CONFIDENTIAL
),
795 item
->flags
& DNS_ANSWER_SHARED_OWNER
,
801 stale_retention_usec
);
806 if (!key
) /* mDNS doesn't know negative caching, really */
809 /* Third, add in negative entries if the key has no RR */
810 r
= dns_answer_match_key(answer
, key
, NULL
);
816 /* But not if it has a matching CNAME/DNAME (the negative caching will be done on the canonical name,
817 * not on the alias) */
818 r
= dns_answer_find_cname_or_dname(answer
, key
, NULL
, NULL
);
824 /* See https://tools.ietf.org/html/rfc2308, which say that a matching SOA record in the packet is used to
825 * enable negative caching. We apply one exception though: if we are about to cache a weird rcode we do so
826 * regardless of a SOA. */
827 r
= dns_answer_find_soa(answer
, key
, &soa
, &flags
);
830 if (r
== 0 && !weird_rcode
)
833 /* Refuse using the SOA data if it is unsigned, but the key is signed */
834 if (FLAGS_SET(query_flags
, SD_RESOLVED_AUTHENTICATED
) &&
835 (flags
& DNS_ANSWER_AUTHENTICATED
) == 0)
839 if (cache_mode
== DNS_CACHE_MODE_NO_NEGATIVE
) {
840 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
841 log_debug("Not caching negative entry for: %s, cache mode set to no-negative",
842 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
846 r
= dns_cache_put_negative(
865 /* Adding all RRs failed. Let's clean up what we already
866 * added, just in case */
869 dns_cache_remove_by_key(c
, key
);
871 DNS_ANSWER_FOREACH_ITEM(item
, answer
) {
872 if ((item
->flags
& DNS_ANSWER_CACHEABLE
) == 0)
875 dns_cache_remove_by_key(c
, item
->rr
->key
);
881 static DnsCacheItem
*dns_cache_get_by_key_follow_cname_dname_nsec(DnsCache
*c
, DnsResourceKey
*k
) {
889 /* If we hit some OOM error, or suchlike, we don't care too
890 * much, after all this is just a cache */
892 i
= hashmap_get(c
->by_key
, k
);
896 n
= dns_resource_key_name(k
);
898 /* Check if we have an NXDOMAIN cache item for the name, notice that we use
899 * the pseudo-type ANY for NXDOMAIN cache items. */
900 i
= hashmap_get(c
->by_key
, &DNS_RESOURCE_KEY_CONST(k
->class, DNS_TYPE_ANY
, n
));
901 if (i
&& i
->type
== DNS_CACHE_NXDOMAIN
)
904 if (dns_type_may_redirect(k
->type
)) {
905 /* Check if we have a CNAME record instead */
906 i
= hashmap_get(c
->by_key
, &DNS_RESOURCE_KEY_CONST(k
->class, DNS_TYPE_CNAME
, n
));
907 if (i
&& i
->type
!= DNS_CACHE_NODATA
)
910 /* OK, let's look for cached DNAME records. */
915 i
= hashmap_get(c
->by_key
, &DNS_RESOURCE_KEY_CONST(k
->class, DNS_TYPE_DNAME
, n
));
916 if (i
&& i
->type
!= DNS_CACHE_NODATA
)
919 /* Jump one label ahead */
920 r
= dns_name_parent(&n
);
926 if (k
->type
!= DNS_TYPE_NSEC
) {
927 /* Check if we have an NSEC record instead for the name. */
928 i
= hashmap_get(c
->by_key
, &DNS_RESOURCE_KEY_CONST(k
->class, DNS_TYPE_NSEC
, n
));
936 static int answer_add_clamp_ttl(
938 DnsResourceRecord
*rr
,
940 DnsAnswerFlags answer_flags
,
941 DnsResourceRecord
*rrsig
,
942 uint64_t query_flags
,
946 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*patched
= NULL
, *patched_rrsig
= NULL
;
952 if (FLAGS_SET(query_flags
, SD_RESOLVED_CLAMP_TTL
)) {
957 /* Let's determine how much time is left for this cache entry. Note that we round down, but
958 * clamp this to be 1s at minimum, since we usually want records to remain cached better too
959 * short a time than too long a time, but otoh don't want to return 0 ever, since that has
960 * special semantics in various contexts — in particular in mDNS */
962 left_ttl
= MAX(1U, LESS_BY(until
, current
) / USEC_PER_SEC
);
964 patched
= dns_resource_record_ref(rr
);
966 r
= dns_resource_record_clamp_ttl(&patched
, left_ttl
);
973 patched_rrsig
= dns_resource_record_ref(rrsig
);
974 r
= dns_resource_record_clamp_ttl(&patched_rrsig
, left_ttl
);
978 rrsig
= patched_rrsig
;
982 r
= dns_answer_add_extend(answer
, rr
, ifindex
, answer_flags
, rrsig
);
989 int dns_cache_lookup(
992 uint64_t query_flags
,
994 DnsAnswer
**ret_answer
,
995 DnsPacket
**ret_full_packet
,
996 uint64_t *ret_query_flags
,
997 DnssecResult
*ret_dnssec_result
) {
999 _cleanup_(dns_packet_unrefp
) DnsPacket
*full_packet
= NULL
;
1000 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
1001 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
1004 bool nxdomain
= false;
1005 DnsCacheItem
*first
, *nsec
= NULL
;
1006 bool have_authenticated
= false, have_non_authenticated
= false, have_confidential
= false, have_non_confidential
= false;
1008 int found_rcode
= -1;
1009 DnssecResult dnssec_result
= -1;
1010 int have_dnssec_result
= -1;
1015 if (key
->type
== DNS_TYPE_ANY
|| key
->class == DNS_CLASS_ANY
) {
1016 /* If we have ANY lookups we don't use the cache, so that the caller refreshes via the
1019 log_debug("Ignoring cache for ANY lookup: %s",
1020 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
1024 first
= dns_cache_get_by_key_follow_cname_dname_nsec(c
, key
);
1026 /* If one question cannot be answered we need to refresh */
1028 log_debug("Cache miss for %s",
1029 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
1033 if ((query_flags
& (SD_RESOLVED_CLAMP_TTL
| SD_RESOLVED_NO_STALE
)) != 0) {
1034 /* 'current' is always passed to answer_add_clamp_ttl(), but is only used conditionally.
1035 * We'll do the same assert there to make sure that it was initialized properly.
1036 * 'current' is also used below when SD_RESOLVED_NO_STALE is set. */
1037 current
= now(CLOCK_BOOTTIME
);
1038 assert(current
> 0);
1041 LIST_FOREACH(by_key
, j
, first
) {
1042 /* If the caller doesn't allow us to answer questions from cache data learned from
1043 * "side-effect", skip this entry. */
1044 if (FLAGS_SET(query_flags
, SD_RESOLVED_REQUIRE_PRIMARY
) &&
1045 !DNS_CACHE_ITEM_IS_PRIMARY(j
)) {
1046 log_debug("Primary answer was requested for cache lookup for %s, which we don't have.",
1047 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
1052 /* Skip the next part if ttl is expired and requested with no stale flag. */
1053 if (FLAGS_SET(query_flags
, SD_RESOLVED_NO_STALE
) && j
->until_valid
< current
) {
1054 log_debug("Requested with no stale and TTL expired for %s",
1055 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
1060 if (j
->type
== DNS_CACHE_NXDOMAIN
)
1062 else if (j
->type
== DNS_CACHE_RCODE
)
1063 found_rcode
= j
->rcode
;
1065 if (j
->rr
->key
->type
== DNS_TYPE_NSEC
)
1071 if (FLAGS_SET(j
->query_flags
, SD_RESOLVED_AUTHENTICATED
))
1072 have_authenticated
= true;
1074 have_non_authenticated
= true;
1076 if (FLAGS_SET(j
->query_flags
, SD_RESOLVED_CONFIDENTIAL
))
1077 have_confidential
= true;
1079 have_non_confidential
= true;
1081 if (j
->dnssec_result
< 0) {
1082 have_dnssec_result
= false; /* an entry without dnssec result? then invalidate things for good */
1083 dnssec_result
= _DNSSEC_RESULT_INVALID
;
1084 } else if (have_dnssec_result
< 0) {
1085 have_dnssec_result
= true; /* So far no result seen, let's pick this one up */
1086 dnssec_result
= j
->dnssec_result
;
1087 } else if (have_dnssec_result
> 0 && j
->dnssec_result
!= dnssec_result
) {
1088 have_dnssec_result
= false; /* conflicting result seen? then invalidate for good */
1089 dnssec_result
= _DNSSEC_RESULT_INVALID
;
1092 /* If the question is being resolved using stale data, the clamp TTL will be set to CACHE_STALE_TTL_MAX_USEC. */
1093 usec_t until
= FLAGS_SET(query_flags
, SD_RESOLVED_NO_STALE
) ? j
->until_valid
1094 : usec_add(current
, CACHE_STALE_TTL_MAX_USEC
);
1096 /* Append the answer RRs to our answer. Ideally we have the answer object, which we
1097 * preferably use. But if the cached entry was generated as "side-effect" of a reply,
1098 * i.e. from validated auxiliary records rather than from the main reply, then we use the
1099 * individual RRs only instead. */
1102 /* Minor optimization, if the full answer object of this and the previous RR is the
1103 * same, don't bother adding it again. Typically we store a full RRset here, hence
1104 * that should be the case. */
1105 if (!j
->by_key_prev
|| j
->answer
!= j
->by_key_prev
->answer
) {
1106 DnsAnswerItem
*item
;
1108 DNS_ANSWER_FOREACH_ITEM(item
, j
->answer
) {
1109 r
= answer_add_clamp_ttl(
1124 r
= answer_add_clamp_ttl(
1128 FLAGS_SET(j
->query_flags
, SD_RESOLVED_AUTHENTICATED
) ? DNS_ANSWER_AUTHENTICATED
: 0,
1137 /* We'll return any packet we have for this. Typically all cache entries for the same key
1138 * should come from the same packet anyway, hence it doesn't really matter which packet we
1139 * return here, they should all resolve to the same anyway. */
1140 if (!full_packet
&& j
->full_packet
)
1141 full_packet
= dns_packet_ref(j
->full_packet
);
1144 if (found_rcode
>= 0) {
1145 log_debug("RCODE %s cache hit for %s",
1146 FORMAT_DNS_RCODE(found_rcode
),
1147 dns_resource_key_to_string(key
, key_str
, sizeof(key_str
)));
1150 *ret_rcode
= found_rcode
;
1152 *ret_answer
= TAKE_PTR(answer
);
1153 if (ret_full_packet
)
1154 *ret_full_packet
= TAKE_PTR(full_packet
);
1155 if (ret_query_flags
)
1156 *ret_query_flags
= 0;
1157 if (ret_dnssec_result
)
1158 *ret_dnssec_result
= dnssec_result
;
1164 if (nsec
&& !IN_SET(key
->type
, DNS_TYPE_NSEC
, DNS_TYPE_DS
)) {
1165 /* Note that we won't derive information for DS RRs from an NSEC, because we only cache NSEC
1166 * RRs from the lower-zone of a zone cut, but the DS RRs are on the upper zone. */
1168 log_debug("NSEC NODATA cache hit for %s",
1169 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
1171 /* We only found an NSEC record that matches our name. If it says the type doesn't exist
1172 * report NODATA. Otherwise report a cache miss. */
1175 *ret_rcode
= DNS_RCODE_SUCCESS
;
1177 *ret_answer
= TAKE_PTR(answer
);
1178 if (ret_full_packet
)
1179 *ret_full_packet
= TAKE_PTR(full_packet
);
1180 if (ret_query_flags
)
1181 *ret_query_flags
= nsec
->query_flags
;
1182 if (ret_dnssec_result
)
1183 *ret_dnssec_result
= nsec
->dnssec_result
;
1185 if (!bitmap_isset(nsec
->rr
->nsec
.types
, key
->type
) &&
1186 !bitmap_isset(nsec
->rr
->nsec
.types
, DNS_TYPE_CNAME
) &&
1187 !bitmap_isset(nsec
->rr
->nsec
.types
, DNS_TYPE_DNAME
)) {
1196 log_debug("%s cache hit for %s",
1197 n
> 0 ? "Positive" :
1198 nxdomain
? "NXDOMAIN" : "NODATA",
1199 dns_resource_key_to_string(key
, key_str
, sizeof key_str
));
1205 *ret_rcode
= nxdomain
? DNS_RCODE_NXDOMAIN
: DNS_RCODE_SUCCESS
;
1207 *ret_answer
= TAKE_PTR(answer
);
1208 if (ret_full_packet
)
1209 *ret_full_packet
= TAKE_PTR(full_packet
);
1210 if (ret_query_flags
)
1212 ((have_authenticated
&& !have_non_authenticated
) ? SD_RESOLVED_AUTHENTICATED
: 0) |
1213 ((have_confidential
&& !have_non_confidential
) ? SD_RESOLVED_CONFIDENTIAL
: 0);
1214 if (ret_dnssec_result
)
1215 *ret_dnssec_result
= dnssec_result
;
1223 *ret_rcode
= DNS_RCODE_SUCCESS
;
1225 *ret_answer
= TAKE_PTR(answer
);
1226 if (ret_full_packet
)
1227 *ret_full_packet
= TAKE_PTR(full_packet
);
1228 if (ret_query_flags
)
1230 ((have_authenticated
&& !have_non_authenticated
) ? SD_RESOLVED_AUTHENTICATED
: 0) |
1231 ((have_confidential
&& !have_non_confidential
) ? SD_RESOLVED_CONFIDENTIAL
: 0);
1232 if (ret_dnssec_result
)
1233 *ret_dnssec_result
= dnssec_result
;
1239 *ret_rcode
= DNS_RCODE_SUCCESS
;
1242 if (ret_full_packet
)
1243 *ret_full_packet
= NULL
;
1244 if (ret_query_flags
)
1245 *ret_query_flags
= 0;
1246 if (ret_dnssec_result
)
1247 *ret_dnssec_result
= _DNSSEC_RESULT_INVALID
;
1253 int dns_cache_check_conflicts(DnsCache
*cache
, DnsResourceRecord
*rr
, int owner_family
, const union in_addr_union
*owner_address
) {
1254 DnsCacheItem
*first
;
1255 bool same_owner
= true;
1260 dns_cache_prune(cache
);
1262 /* See if there's a cache entry for the same key. If there
1263 * isn't there's no conflict */
1264 first
= hashmap_get(cache
->by_key
, rr
->key
);
1268 /* See if the RR key is owned by the same owner, if so, there
1269 * isn't a conflict either */
1270 LIST_FOREACH(by_key
, i
, first
) {
1271 if (i
->owner_family
!= owner_family
||
1272 !in_addr_equal(owner_family
, &i
->owner_address
, owner_address
)) {
1280 /* See if there's the exact same RR in the cache. If yes, then
1281 * there's no conflict. */
1282 if (dns_cache_get(cache
, rr
))
1285 /* There's a conflict */
1289 int dns_cache_export_shared_to_packet(DnsCache
*cache
, DnsPacket
*p
, usec_t ts
, unsigned max_rr
) {
1290 unsigned ancount
= 0;
1296 assert(p
->protocol
== DNS_PROTOCOL_MDNS
);
1298 HASHMAP_FOREACH(i
, cache
->by_key
)
1299 LIST_FOREACH(by_key
, j
, i
) {
1303 if (!j
->shared_owner
)
1306 /* RFC6762 7.1: Don't append records with less than half the TTL remaining
1307 * as known answers. */
1308 if (usec_sub_unsigned(j
->until
, ts
) < j
->rr
->ttl
* USEC_PER_SEC
/ 2)
1311 r
= dns_packet_append_rr(p
, j
->rr
, 0, NULL
, NULL
);
1312 if (r
== -EMSGSIZE
) {
1314 /* If max_rr == 0, do not allocate more packets. */
1317 /* If we're unable to stuff all known answers into the given packet, allocate
1318 * a new one, push the RR into that one and link it to the current one. */
1320 DNS_PACKET_HEADER(p
)->ancount
= htobe16(ancount
);
1323 r
= dns_packet_new_query(&p
->more
, p
->protocol
, 0, true);
1327 /* continue with new packet */
1329 r
= dns_packet_append_rr(p
, j
->rr
, 0, NULL
, NULL
);
1336 if (max_rr
> 0 && ancount
>= max_rr
) {
1337 DNS_PACKET_HEADER(p
)->ancount
= htobe16(ancount
);
1340 r
= dns_packet_new_query(&p
->more
, p
->protocol
, 0, true);
1351 DNS_PACKET_HEADER(p
)->ancount
= htobe16(ancount
);
1356 void dns_cache_dump(DnsCache
*cache
, FILE *f
) {
1365 HASHMAP_FOREACH(i
, cache
->by_key
)
1366 LIST_FOREACH(by_key
, j
, i
) {
1372 t
= dns_resource_record_to_string(j
->rr
);
1381 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
1383 fputs(dns_resource_key_to_string(j
->key
, key_str
, sizeof key_str
), f
);
1385 fputs(dns_cache_item_type_to_string(j
), f
);
1391 int dns_cache_dump_to_json(DnsCache
*cache
, JsonVariant
**ret
) {
1392 _cleanup_(json_variant_unrefp
) JsonVariant
*c
= NULL
;
1399 HASHMAP_FOREACH(i
, cache
->by_key
) {
1400 _cleanup_(json_variant_unrefp
) JsonVariant
*d
= NULL
, *k
= NULL
;
1402 r
= dns_resource_key_to_json(i
->key
, &k
);
1407 _cleanup_(json_variant_unrefp
) JsonVariant
*l
= NULL
;
1409 LIST_FOREACH(by_key
, j
, i
) {
1410 _cleanup_(json_variant_unrefp
) JsonVariant
*rj
= NULL
;
1414 r
= dns_resource_record_to_json(j
->rr
, &rj
);
1418 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 */
1422 r
= json_variant_append_arrayb(
1425 JSON_BUILD_PAIR_VARIANT("rr", rj
),
1426 JSON_BUILD_PAIR_BASE64("raw", j
->rr
->wire_format
, j
->rr
->wire_format_size
)));
1432 r
= json_variant_new_array(&l
, NULL
, 0);
1439 JSON_BUILD_PAIR_VARIANT("key", k
),
1440 JSON_BUILD_PAIR_VARIANT("rrs", l
),
1441 JSON_BUILD_PAIR_UNSIGNED("until", i
->until
)));
1442 } else if (i
->type
== DNS_CACHE_NODATA
) {
1445 JSON_BUILD_PAIR_VARIANT("key", k
),
1446 JSON_BUILD_PAIR_EMPTY_ARRAY("rrs"),
1447 JSON_BUILD_PAIR_UNSIGNED("until", i
->until
)));
1451 JSON_BUILD_PAIR_VARIANT("key", k
),
1452 JSON_BUILD_PAIR_STRING("type", dns_cache_item_type_to_string(i
)),
1453 JSON_BUILD_PAIR_UNSIGNED("until", i
->until
)));
1457 r
= json_variant_append_array(&c
, d
);
1463 return json_variant_new_array(ret
, NULL
, 0);
1469 bool dns_cache_is_empty(DnsCache
*cache
) {
1473 return hashmap_isempty(cache
->by_key
);
1476 unsigned dns_cache_size(DnsCache
*cache
) {
1480 return hashmap_size(cache
->by_key
);