1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include "alloc-util.h"
4 #include "dns-domain.h"
6 #include "resolved-dns-packet.h"
7 #include "resolved-dns-zone.h"
8 #include "resolved-dnssd.h"
9 #include "resolved-manager.h"
10 #include "string-util.h"
12 /* Never allow more than 1K entries */
15 void dns_zone_item_probe_stop(DnsZoneItem
*i
) {
19 if (!i
->probe_transaction
)
22 t
= TAKE_PTR(i
->probe_transaction
);
24 set_remove(t
->notify_zone_items
, i
);
25 set_remove(t
->notify_zone_items_done
, i
);
26 dns_transaction_gc(t
);
29 static DnsZoneItem
* dns_zone_item_free(DnsZoneItem
*i
) {
33 dns_zone_item_probe_stop(i
);
34 dns_resource_record_unref(i
->rr
);
38 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsZoneItem
*, dns_zone_item_free
);
40 static void dns_zone_item_remove_and_free(DnsZone
*z
, DnsZoneItem
*i
) {
48 first
= hashmap_get(z
->by_key
, i
->rr
->key
);
49 LIST_REMOVE(by_key
, first
, i
);
51 assert_se(hashmap_replace(z
->by_key
, first
->rr
->key
, first
) >= 0);
53 hashmap_remove(z
->by_key
, i
->rr
->key
);
55 first
= hashmap_get(z
->by_name
, dns_resource_key_name(i
->rr
->key
));
56 LIST_REMOVE(by_name
, first
, i
);
58 assert_se(hashmap_replace(z
->by_name
, dns_resource_key_name(first
->rr
->key
), first
) >= 0);
60 hashmap_remove(z
->by_name
, dns_resource_key_name(i
->rr
->key
));
62 dns_zone_item_free(i
);
65 void dns_zone_flush(DnsZone
*z
) {
70 while ((i
= hashmap_first(z
->by_key
)))
71 dns_zone_item_remove_and_free(z
, i
);
73 assert(hashmap_size(z
->by_key
) == 0);
74 assert(hashmap_size(z
->by_name
) == 0);
76 z
->by_key
= hashmap_free(z
->by_key
);
77 z
->by_name
= hashmap_free(z
->by_name
);
80 DnsZoneItem
* dns_zone_get(DnsZone
*z
, DnsResourceRecord
*rr
) {
84 LIST_FOREACH(by_key
, i
, (DnsZoneItem
*) hashmap_get(z
->by_key
, rr
->key
))
85 if (dns_resource_record_equal(i
->rr
, rr
) > 0)
91 void dns_zone_remove_rr(DnsZone
*z
, DnsResourceRecord
*rr
) {
99 i
= dns_zone_get(z
, rr
);
101 dns_zone_item_remove_and_free(z
, i
);
104 int dns_zone_remove_rrs_by_key(DnsZone
*z
, DnsResourceKey
*key
) {
105 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
, *soa
= NULL
;
106 DnsResourceRecord
*rr
;
110 r
= dns_zone_lookup(z
, key
, 0, &answer
, &soa
, &tentative
);
114 DNS_ANSWER_FOREACH(rr
, answer
)
115 dns_zone_remove_rr(z
, rr
);
120 static int dns_zone_init(DnsZone
*z
) {
125 r
= hashmap_ensure_allocated(&z
->by_key
, &dns_resource_key_hash_ops
);
129 r
= hashmap_ensure_allocated(&z
->by_name
, &dns_name_hash_ops
);
136 static int dns_zone_link_item(DnsZone
*z
, DnsZoneItem
*i
) {
140 first
= hashmap_get(z
->by_key
, i
->rr
->key
);
142 LIST_PREPEND(by_key
, first
, i
);
143 assert_se(hashmap_replace(z
->by_key
, first
->rr
->key
, first
) >= 0);
145 r
= hashmap_put(z
->by_key
, i
->rr
->key
, i
);
150 first
= hashmap_get(z
->by_name
, dns_resource_key_name(i
->rr
->key
));
152 LIST_PREPEND(by_name
, first
, i
);
153 assert_se(hashmap_replace(z
->by_name
, dns_resource_key_name(first
->rr
->key
), first
) >= 0);
155 r
= hashmap_put(z
->by_name
, dns_resource_key_name(i
->rr
->key
), i
);
163 static int dns_zone_item_probe_start(DnsZoneItem
*i
) {
164 _cleanup_(dns_transaction_gcp
) DnsTransaction
*t
= NULL
;
169 if (i
->probe_transaction
)
172 t
= dns_scope_find_transaction(
174 &DNS_RESOURCE_KEY_CONST(i
->rr
->key
->class, DNS_TYPE_ANY
, dns_resource_key_name(i
->rr
->key
)),
175 SD_RESOLVED_NO_CACHE
|SD_RESOLVED_NO_ZONE
);
177 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
179 key
= dns_resource_key_new(i
->rr
->key
->class, DNS_TYPE_ANY
, dns_resource_key_name(i
->rr
->key
));
183 r
= dns_transaction_new(&t
, i
->scope
, key
, NULL
, SD_RESOLVED_NO_CACHE
|SD_RESOLVED_NO_ZONE
);
188 r
= set_ensure_allocated(&t
->notify_zone_items_done
, NULL
);
192 r
= set_ensure_put(&t
->notify_zone_items
, NULL
, i
);
197 i
->probe_transaction
= TAKE_PTR(t
);
199 if (i
->probe_transaction
->state
== DNS_TRANSACTION_NULL
) {
201 r
= dns_transaction_go(i
->probe_transaction
);
205 dns_zone_item_probe_stop(i
);
210 dns_zone_item_notify(i
);
214 int dns_zone_put(DnsZone
*z
, DnsScope
*s
, DnsResourceRecord
*rr
, bool probe
) {
215 _cleanup_(dns_zone_item_freep
) DnsZoneItem
*i
= NULL
;
216 DnsZoneItem
*existing
;
223 if (dns_class_is_pseudo(rr
->key
->class))
225 if (dns_type_is_pseudo(rr
->key
->type
))
228 existing
= dns_zone_get(z
, rr
);
232 r
= dns_zone_init(z
);
236 i
= new(DnsZoneItem
, 1);
242 .rr
= dns_resource_record_ref(rr
),
243 .probing_enabled
= probe
,
246 r
= dns_zone_link_item(z
, i
);
251 bool established
= false;
253 /* Check if there's already an RR with the same name
254 * established. If so, it has been probed already, and
255 * we don't need to probe again. */
257 LIST_FOREACH_OTHERS(by_name
, j
, i
)
258 if (j
->state
== DNS_ZONE_ITEM_ESTABLISHED
)
262 i
->state
= DNS_ZONE_ITEM_ESTABLISHED
;
264 i
->state
= DNS_ZONE_ITEM_PROBING
;
266 r
= dns_zone_item_probe_start(i
);
268 dns_zone_item_remove_and_free(z
, i
);
274 i
->state
= DNS_ZONE_ITEM_ESTABLISHED
;
280 static int dns_zone_add_authenticated_answer(DnsAnswer
*a
, DnsZoneItem
*i
, int ifindex
) {
281 DnsAnswerFlags flags
;
283 /* From RFC 6762, Section 10.2
284 * "They (the rules about when to set the cache-flush bit) apply to
285 * startup announcements as described in Section 8.3, "Announcing",
286 * and to responses generated as a result of receiving query messages."
287 * So, set the cache-flush bit for mDNS answers except for DNS-SD
288 * service enumeration PTRs described in RFC 6763, Section 4.1. */
289 if (i
->scope
->protocol
== DNS_PROTOCOL_MDNS
&&
290 !dns_resource_key_is_dnssd_ptr(i
->rr
->key
))
291 flags
= DNS_ANSWER_AUTHENTICATED
|DNS_ANSWER_CACHE_FLUSH
;
293 flags
= DNS_ANSWER_AUTHENTICATED
;
295 return dns_answer_add(a
, i
->rr
, ifindex
, flags
, NULL
);
298 int dns_zone_lookup(DnsZone
*z
, DnsResourceKey
*key
, int ifindex
, DnsAnswer
**ret_answer
, DnsAnswer
**ret_soa
, bool *ret_tentative
) {
299 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
, *soa
= NULL
;
300 unsigned n_answer
= 0;
302 bool tentative
= true, need_soa
= false;
305 /* Note that we don't actually need the ifindex for anything. However when it is passed we'll initialize the
306 * ifindex field in the answer with it */
312 /* First iteration, count what we have */
314 if (key
->type
== DNS_TYPE_ANY
|| key
->class == DNS_CLASS_ANY
) {
315 bool found
= false, added
= false;
318 /* If this is a generic match, then we have to
319 * go through the list by the name and look
320 * for everything manually */
322 first
= hashmap_get(z
->by_name
, dns_resource_key_name(key
));
323 LIST_FOREACH(by_name
, j
, first
) {
324 if (!IN_SET(j
->state
, DNS_ZONE_ITEM_PROBING
, DNS_ZONE_ITEM_ESTABLISHED
, DNS_ZONE_ITEM_VERIFYING
))
329 k
= dns_resource_key_match_rr(key
, j
->rr
, NULL
);
345 /* If this is a specific match, then look for
346 * the right key immediately */
348 first
= hashmap_get(z
->by_key
, key
);
349 LIST_FOREACH(by_key
, j
, first
) {
350 if (!IN_SET(j
->state
, DNS_ZONE_ITEM_PROBING
, DNS_ZONE_ITEM_ESTABLISHED
, DNS_ZONE_ITEM_VERIFYING
))
358 first
= hashmap_get(z
->by_name
, dns_resource_key_name(key
));
359 LIST_FOREACH(by_name
, j
, first
) {
360 if (!IN_SET(j
->state
, DNS_ZONE_ITEM_PROBING
, DNS_ZONE_ITEM_ESTABLISHED
, DNS_ZONE_ITEM_VERIFYING
))
369 if (n_answer
<= 0 && !need_soa
)
373 answer
= dns_answer_new(n_answer
);
379 soa
= dns_answer_new(1);
384 /* Second iteration, actually add the RRs to the answers */
385 if (key
->type
== DNS_TYPE_ANY
|| key
->class == DNS_CLASS_ANY
) {
386 bool found
= false, added
= false;
389 first
= hashmap_get(z
->by_name
, dns_resource_key_name(key
));
390 LIST_FOREACH(by_name
, j
, first
) {
391 if (!IN_SET(j
->state
, DNS_ZONE_ITEM_PROBING
, DNS_ZONE_ITEM_ESTABLISHED
, DNS_ZONE_ITEM_VERIFYING
))
396 if (j
->state
!= DNS_ZONE_ITEM_PROBING
)
399 k
= dns_resource_key_match_rr(key
, j
->rr
, NULL
);
403 r
= dns_zone_add_authenticated_answer(answer
, j
, ifindex
);
411 if (found
&& !added
) {
412 r
= dns_answer_add_soa(soa
, dns_resource_key_name(key
), LLMNR_DEFAULT_TTL
, ifindex
);
419 first
= hashmap_get(z
->by_key
, key
);
420 LIST_FOREACH(by_key
, j
, first
) {
421 if (!IN_SET(j
->state
, DNS_ZONE_ITEM_PROBING
, DNS_ZONE_ITEM_ESTABLISHED
, DNS_ZONE_ITEM_VERIFYING
))
426 if (j
->state
!= DNS_ZONE_ITEM_PROBING
)
429 r
= dns_zone_add_authenticated_answer(answer
, j
, ifindex
);
435 bool add_soa
= false;
437 first
= hashmap_get(z
->by_name
, dns_resource_key_name(key
));
438 LIST_FOREACH(by_name
, j
, first
) {
439 if (!IN_SET(j
->state
, DNS_ZONE_ITEM_PROBING
, DNS_ZONE_ITEM_ESTABLISHED
, DNS_ZONE_ITEM_VERIFYING
))
442 if (j
->state
!= DNS_ZONE_ITEM_PROBING
)
449 r
= dns_answer_add_soa(soa
, dns_resource_key_name(key
), LLMNR_DEFAULT_TTL
, ifindex
);
456 /* If the caller sets ret_tentative to NULL, then use this as
457 * indication to not return tentative entries */
459 if (!ret_tentative
&& tentative
)
462 *ret_answer
= TAKE_PTR(answer
);
465 *ret_soa
= TAKE_PTR(soa
);
468 *ret_tentative
= tentative
;
479 *ret_tentative
= false;
484 void dns_zone_item_conflict(DnsZoneItem
*i
) {
487 if (!IN_SET(i
->state
, DNS_ZONE_ITEM_PROBING
, DNS_ZONE_ITEM_VERIFYING
, DNS_ZONE_ITEM_ESTABLISHED
))
490 log_info("Detected conflict on %s", strna(dns_resource_record_to_string(i
->rr
)));
492 dns_zone_item_probe_stop(i
);
494 /* Withdraw the conflict item */
495 i
->state
= DNS_ZONE_ITEM_WITHDRAWN
;
497 (void) dnssd_signal_conflict(i
->scope
->manager
, dns_resource_key_name(i
->rr
->key
));
499 /* Maybe change the hostname */
500 if (manager_is_own_hostname(i
->scope
->manager
, dns_resource_key_name(i
->rr
->key
)) > 0)
501 manager_next_hostname(i
->scope
->manager
);
504 void dns_zone_item_notify(DnsZoneItem
*i
) {
506 assert(i
->probe_transaction
);
508 if (i
->block_ready
> 0)
511 if (IN_SET(i
->probe_transaction
->state
, DNS_TRANSACTION_NULL
, DNS_TRANSACTION_PENDING
, DNS_TRANSACTION_VALIDATING
))
514 if (i
->probe_transaction
->state
== DNS_TRANSACTION_SUCCESS
) {
515 bool we_lost
= false;
517 /* The probe got a successful reply. If we so far
518 * weren't established we just give up.
520 * In LLMNR case if we already
521 * were established, and the peer has the
522 * lexicographically larger IP address we continue
525 if (!IN_SET(i
->state
, DNS_ZONE_ITEM_ESTABLISHED
, DNS_ZONE_ITEM_VERIFYING
)) {
526 log_debug("Got a successful probe for not yet established RR, we lost.");
528 } else if (i
->probe_transaction
->scope
->protocol
== DNS_PROTOCOL_LLMNR
) {
529 assert(i
->probe_transaction
->received
);
530 we_lost
= memcmp(&i
->probe_transaction
->received
->sender
, &i
->probe_transaction
->received
->destination
, FAMILY_ADDRESS_SIZE(i
->probe_transaction
->received
->family
)) < 0;
532 log_debug("Got a successful probe reply for an established RR, and we have a lexicographically larger IP address and thus lost.");
536 dns_zone_item_conflict(i
);
540 log_debug("Got a successful probe reply, but peer has lexicographically lower IP address and thus lost.");
543 log_debug("Record %s successfully probed.", strna(dns_resource_record_to_string(i
->rr
)));
545 dns_zone_item_probe_stop(i
);
546 i
->state
= DNS_ZONE_ITEM_ESTABLISHED
;
549 static int dns_zone_item_verify(DnsZoneItem
*i
) {
554 if (i
->state
!= DNS_ZONE_ITEM_ESTABLISHED
)
557 log_debug("Verifying RR %s", strna(dns_resource_record_to_string(i
->rr
)));
559 i
->state
= DNS_ZONE_ITEM_VERIFYING
;
560 r
= dns_zone_item_probe_start(i
);
562 log_error_errno(r
, "Failed to start probing for verifying RR: %m");
563 i
->state
= DNS_ZONE_ITEM_ESTABLISHED
;
570 int dns_zone_check_conflicts(DnsZone
*zone
, DnsResourceRecord
*rr
) {
577 /* This checks whether a response RR we received from somebody
578 * else is one that we actually thought was uniquely ours. If
579 * so, we'll verify our RRs. */
581 /* No conflict if we don't have the name at all. */
582 first
= hashmap_get(zone
->by_name
, dns_resource_key_name(rr
->key
));
586 /* No conflict if we have the exact same RR */
587 if (dns_zone_get(zone
, rr
))
590 /* No conflict if it is DNS-SD RR used for service enumeration. */
591 if (dns_resource_key_is_dnssd_ptr(rr
->key
))
594 /* OK, somebody else has RRs for the same name. Yuck! Let's
595 * start probing again */
597 LIST_FOREACH(by_name
, i
, first
) {
598 if (dns_resource_record_equal(i
->rr
, rr
))
601 dns_zone_item_verify(i
);
608 int dns_zone_verify_conflicts(DnsZone
*zone
, DnsResourceKey
*key
) {
614 /* Somebody else notified us about a possible conflict. Let's
615 * verify if that's true. */
617 first
= hashmap_get(zone
->by_name
, dns_resource_key_name(key
));
621 LIST_FOREACH(by_name
, i
, first
) {
622 dns_zone_item_verify(i
);
629 void dns_zone_verify_all(DnsZone
*zone
) {
634 HASHMAP_FOREACH(i
, zone
->by_key
)
635 LIST_FOREACH(by_key
, j
, i
)
636 dns_zone_item_verify(j
);
639 void dns_zone_dump(DnsZone
*zone
, FILE *f
) {
648 HASHMAP_FOREACH(i
, zone
->by_key
)
649 LIST_FOREACH(by_key
, j
, i
) {
652 t
= dns_resource_record_to_string(j
->rr
);
664 bool dns_zone_is_empty(DnsZone
*zone
) {
668 return hashmap_isempty(zone
->by_key
);
671 bool dns_zone_contains_name(DnsZone
*z
, const char *name
) {
674 first
= hashmap_get(z
->by_name
, name
);
678 LIST_FOREACH(by_name
, i
, first
) {
679 if (!IN_SET(i
->state
, DNS_ZONE_ITEM_PROBING
, DNS_ZONE_ITEM_ESTABLISHED
, DNS_ZONE_ITEM_VERIFYING
))