1 /* SPDX-License-Identifier: LGPL-2.1+ */
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 "string-util.h"
11 /* Never allow more than 1K entries */
14 void dns_zone_item_probe_stop(DnsZoneItem
*i
) {
18 if (!i
->probe_transaction
)
21 t
= TAKE_PTR(i
->probe_transaction
);
23 set_remove(t
->notify_zone_items
, i
);
24 set_remove(t
->notify_zone_items_done
, i
);
25 dns_transaction_gc(t
);
28 static void dns_zone_item_free(DnsZoneItem
*i
) {
32 dns_zone_item_probe_stop(i
);
33 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
) {
86 LIST_FOREACH(by_key
, i
, hashmap_get(z
->by_key
, rr
->key
))
87 if (dns_resource_record_equal(i
->rr
, rr
) > 0)
93 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
) {
169 if (i
->probe_transaction
)
172 t
= dns_scope_find_transaction(i
->scope
, &DNS_RESOURCE_KEY_CONST(i
->rr
->key
->class, DNS_TYPE_ANY
, dns_resource_key_name(i
->rr
->key
)), false);
174 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*key
= NULL
;
176 key
= dns_resource_key_new(i
->rr
->key
->class, DNS_TYPE_ANY
, dns_resource_key_name(i
->rr
->key
));
180 r
= dns_transaction_new(&t
, i
->scope
, key
);
185 r
= set_ensure_allocated(&t
->notify_zone_items
, NULL
);
189 r
= set_ensure_allocated(&t
->notify_zone_items_done
, NULL
);
193 r
= set_put(t
->notify_zone_items
, i
);
197 i
->probe_transaction
= t
;
200 if (t
->state
== DNS_TRANSACTION_NULL
) {
203 r
= dns_transaction_go(t
);
207 dns_zone_item_probe_stop(i
);
212 dns_zone_item_notify(i
);
216 dns_transaction_gc(t
);
220 int dns_zone_put(DnsZone
*z
, DnsScope
*s
, DnsResourceRecord
*rr
, bool probe
) {
221 _cleanup_(dns_zone_item_freep
) DnsZoneItem
*i
= NULL
;
222 DnsZoneItem
*existing
;
229 if (dns_class_is_pseudo(rr
->key
->class))
231 if (dns_type_is_pseudo(rr
->key
->type
))
234 existing
= dns_zone_get(z
, rr
);
238 r
= dns_zone_init(z
);
242 i
= new0(DnsZoneItem
, 1);
247 i
->rr
= dns_resource_record_ref(rr
);
248 i
->probing_enabled
= probe
;
250 r
= dns_zone_link_item(z
, i
);
255 DnsZoneItem
*first
, *j
;
256 bool established
= false;
258 /* Check if there's already an RR with the same name
259 * established. If so, it has been probed already, and
260 * we don't ned to probe again. */
262 LIST_FIND_HEAD(by_name
, i
, first
);
263 LIST_FOREACH(by_name
, j
, first
) {
267 if (j
->state
== DNS_ZONE_ITEM_ESTABLISHED
)
272 i
->state
= DNS_ZONE_ITEM_ESTABLISHED
;
274 i
->state
= DNS_ZONE_ITEM_PROBING
;
276 r
= dns_zone_item_probe_start(i
);
278 dns_zone_item_remove_and_free(z
, i
);
284 i
->state
= DNS_ZONE_ITEM_ESTABLISHED
;
290 static int dns_zone_add_authenticated_answer(DnsAnswer
*a
, DnsZoneItem
*i
, int ifindex
) {
291 DnsAnswerFlags flags
;
293 /* From RFC 6762, Section 10.2
294 * "They (the rules about when to set the cache-flush bit) apply to
295 * startup announcements as described in Section 8.3, "Announcing",
296 * and to responses generated as a result of receiving query messages."
297 * So, set the cache-flush bit for mDNS answers except for DNS-SD
298 * service enumeration PTRs described in RFC 6763, Section 4.1. */
299 if (i
->scope
->protocol
== DNS_PROTOCOL_MDNS
&&
300 !dns_resource_key_is_dnssd_ptr(i
->rr
->key
))
301 flags
= DNS_ANSWER_AUTHENTICATED
|DNS_ANSWER_CACHE_FLUSH
;
303 flags
= DNS_ANSWER_AUTHENTICATED
;
305 return dns_answer_add(a
, i
->rr
, ifindex
, flags
);
308 int dns_zone_lookup(DnsZone
*z
, DnsResourceKey
*key
, int ifindex
, DnsAnswer
**ret_answer
, DnsAnswer
**ret_soa
, bool *ret_tentative
) {
309 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
, *soa
= NULL
;
310 unsigned n_answer
= 0;
311 DnsZoneItem
*j
, *first
;
312 bool tentative
= true, need_soa
= false;
315 /* Note that we don't actually need the ifindex for anything. However when it is passed we'll initialize the
316 * ifindex field in the answer with it */
322 /* First iteration, count what we have */
324 if (key
->type
== DNS_TYPE_ANY
|| key
->class == DNS_CLASS_ANY
) {
325 bool found
= false, added
= false;
328 /* If this is a generic match, then we have to
329 * go through the list by the name and look
330 * for everything manually */
332 first
= hashmap_get(z
->by_name
, dns_resource_key_name(key
));
333 LIST_FOREACH(by_name
, j
, first
) {
334 if (!IN_SET(j
->state
, DNS_ZONE_ITEM_PROBING
, DNS_ZONE_ITEM_ESTABLISHED
, DNS_ZONE_ITEM_VERIFYING
))
339 k
= dns_resource_key_match_rr(key
, j
->rr
, NULL
);
355 /* If this is a specific match, then look for
356 * the right key immediately */
358 first
= hashmap_get(z
->by_key
, key
);
359 LIST_FOREACH(by_key
, j
, first
) {
360 if (!IN_SET(j
->state
, DNS_ZONE_ITEM_PROBING
, DNS_ZONE_ITEM_ESTABLISHED
, DNS_ZONE_ITEM_VERIFYING
))
368 first
= hashmap_get(z
->by_name
, dns_resource_key_name(key
));
369 LIST_FOREACH(by_name
, j
, first
) {
370 if (!IN_SET(j
->state
, DNS_ZONE_ITEM_PROBING
, DNS_ZONE_ITEM_ESTABLISHED
, DNS_ZONE_ITEM_VERIFYING
))
379 if (n_answer
<= 0 && !need_soa
)
383 answer
= dns_answer_new(n_answer
);
389 soa
= dns_answer_new(1);
394 /* Second iteration, actually add the RRs to the answers */
395 if (key
->type
== DNS_TYPE_ANY
|| key
->class == DNS_CLASS_ANY
) {
396 bool found
= false, added
= false;
399 first
= hashmap_get(z
->by_name
, dns_resource_key_name(key
));
400 LIST_FOREACH(by_name
, j
, first
) {
401 if (!IN_SET(j
->state
, DNS_ZONE_ITEM_PROBING
, DNS_ZONE_ITEM_ESTABLISHED
, DNS_ZONE_ITEM_VERIFYING
))
406 if (j
->state
!= DNS_ZONE_ITEM_PROBING
)
409 k
= dns_resource_key_match_rr(key
, j
->rr
, NULL
);
413 r
= dns_zone_add_authenticated_answer(answer
, j
, ifindex
);
421 if (found
&& !added
) {
422 r
= dns_answer_add_soa(soa
, dns_resource_key_name(key
), LLMNR_DEFAULT_TTL
, ifindex
);
429 first
= hashmap_get(z
->by_key
, key
);
430 LIST_FOREACH(by_key
, j
, first
) {
431 if (!IN_SET(j
->state
, DNS_ZONE_ITEM_PROBING
, DNS_ZONE_ITEM_ESTABLISHED
, DNS_ZONE_ITEM_VERIFYING
))
436 if (j
->state
!= DNS_ZONE_ITEM_PROBING
)
439 r
= dns_zone_add_authenticated_answer(answer
, j
, ifindex
);
445 bool add_soa
= false;
447 first
= hashmap_get(z
->by_name
, dns_resource_key_name(key
));
448 LIST_FOREACH(by_name
, j
, first
) {
449 if (!IN_SET(j
->state
, DNS_ZONE_ITEM_PROBING
, DNS_ZONE_ITEM_ESTABLISHED
, DNS_ZONE_ITEM_VERIFYING
))
452 if (j
->state
!= DNS_ZONE_ITEM_PROBING
)
459 r
= dns_answer_add_soa(soa
, dns_resource_key_name(key
), LLMNR_DEFAULT_TTL
, ifindex
);
466 /* If the caller sets ret_tentative to NULL, then use this as
467 * indication to not return tentative entries */
469 if (!ret_tentative
&& tentative
)
472 *ret_answer
= TAKE_PTR(answer
);
475 *ret_soa
= TAKE_PTR(soa
);
478 *ret_tentative
= tentative
;
489 *ret_tentative
= false;
494 void dns_zone_item_conflict(DnsZoneItem
*i
) {
497 if (!IN_SET(i
->state
, DNS_ZONE_ITEM_PROBING
, DNS_ZONE_ITEM_VERIFYING
, DNS_ZONE_ITEM_ESTABLISHED
))
500 log_info("Detected conflict on %s", strna(dns_resource_record_to_string(i
->rr
)));
502 dns_zone_item_probe_stop(i
);
504 /* Withdraw the conflict item */
505 i
->state
= DNS_ZONE_ITEM_WITHDRAWN
;
507 dnssd_signal_conflict(i
->scope
->manager
, dns_resource_key_name(i
->rr
->key
));
509 /* Maybe change the hostname */
510 if (manager_is_own_hostname(i
->scope
->manager
, dns_resource_key_name(i
->rr
->key
)) > 0)
511 manager_next_hostname(i
->scope
->manager
);
514 void dns_zone_item_notify(DnsZoneItem
*i
) {
516 assert(i
->probe_transaction
);
518 if (i
->block_ready
> 0)
521 if (IN_SET(i
->probe_transaction
->state
, DNS_TRANSACTION_NULL
, DNS_TRANSACTION_PENDING
, DNS_TRANSACTION_VALIDATING
))
524 if (i
->probe_transaction
->state
== DNS_TRANSACTION_SUCCESS
) {
525 bool we_lost
= false;
527 /* The probe got a successful reply. If we so far
528 * weren't established we just give up.
530 * In LLMNR case if we already
531 * were established, and the peer has the
532 * lexicographically larger IP address we continue
535 if (!IN_SET(i
->state
, DNS_ZONE_ITEM_ESTABLISHED
, DNS_ZONE_ITEM_VERIFYING
)) {
536 log_debug("Got a successful probe for not yet established RR, we lost.");
538 } else if (i
->probe_transaction
->scope
->protocol
== DNS_PROTOCOL_LLMNR
) {
539 assert(i
->probe_transaction
->received
);
540 we_lost
= memcmp(&i
->probe_transaction
->received
->sender
, &i
->probe_transaction
->received
->destination
, FAMILY_ADDRESS_SIZE(i
->probe_transaction
->received
->family
)) < 0;
542 log_debug("Got a successful probe reply for an established RR, and we have a lexicographically larger IP address and thus lost.");
546 dns_zone_item_conflict(i
);
550 log_debug("Got a successful probe reply, but peer has lexicographically lower IP address and thus lost.");
553 log_debug("Record %s successfully probed.", strna(dns_resource_record_to_string(i
->rr
)));
555 dns_zone_item_probe_stop(i
);
556 i
->state
= DNS_ZONE_ITEM_ESTABLISHED
;
559 static int dns_zone_item_verify(DnsZoneItem
*i
) {
564 if (i
->state
!= DNS_ZONE_ITEM_ESTABLISHED
)
567 log_debug("Verifying RR %s", strna(dns_resource_record_to_string(i
->rr
)));
569 i
->state
= DNS_ZONE_ITEM_VERIFYING
;
570 r
= dns_zone_item_probe_start(i
);
572 log_error_errno(r
, "Failed to start probing for verifying RR: %m");
573 i
->state
= DNS_ZONE_ITEM_ESTABLISHED
;
580 int dns_zone_check_conflicts(DnsZone
*zone
, DnsResourceRecord
*rr
) {
581 DnsZoneItem
*i
, *first
;
587 /* This checks whether a response RR we received from somebody
588 * else is one that we actually thought was uniquely ours. If
589 * so, we'll verify our RRs. */
591 /* No conflict if we don't have the name at all. */
592 first
= hashmap_get(zone
->by_name
, dns_resource_key_name(rr
->key
));
596 /* No conflict if we have the exact same RR */
597 if (dns_zone_get(zone
, rr
))
600 /* No conflict if it is DNS-SD RR used for service enumeration. */
601 if (dns_resource_key_is_dnssd_ptr(rr
->key
))
604 /* OK, somebody else has RRs for the same name. Yuck! Let's
605 * start probing again */
607 LIST_FOREACH(by_name
, i
, first
) {
608 if (dns_resource_record_equal(i
->rr
, rr
))
611 dns_zone_item_verify(i
);
618 int dns_zone_verify_conflicts(DnsZone
*zone
, DnsResourceKey
*key
) {
619 DnsZoneItem
*i
, *first
;
624 /* Somebody else notified us about a possible conflict. Let's
625 * verify if that's true. */
627 first
= hashmap_get(zone
->by_name
, dns_resource_key_name(key
));
631 LIST_FOREACH(by_name
, i
, first
) {
632 dns_zone_item_verify(i
);
639 void dns_zone_verify_all(DnsZone
*zone
) {
645 HASHMAP_FOREACH(i
, zone
->by_key
, iterator
) {
648 LIST_FOREACH(by_key
, j
, i
)
649 dns_zone_item_verify(j
);
653 void dns_zone_dump(DnsZone
*zone
, FILE *f
) {
663 HASHMAP_FOREACH(i
, zone
->by_key
, iterator
) {
666 LIST_FOREACH(by_key
, j
, i
) {
669 t
= dns_resource_record_to_string(j
->rr
);
682 bool dns_zone_is_empty(DnsZone
*zone
) {
686 return hashmap_isempty(zone
->by_key
);
689 bool dns_zone_contains_name(DnsZone
*z
, const char *name
) {
690 DnsZoneItem
*i
, *first
;
692 first
= hashmap_get(z
->by_name
, name
);
696 LIST_FOREACH(by_name
, i
, first
) {
697 if (!IN_SET(i
->state
, DNS_ZONE_ITEM_PROBING
, DNS_ZONE_ITEM_ESTABLISHED
, DNS_ZONE_ITEM_VERIFYING
))