1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sd-messages.h>
25 #include "alloc-util.h"
26 #include "dns-domain.h"
27 #include "errno-list.h"
29 #include "random-util.h"
30 #include "resolved-dns-cache.h"
31 #include "resolved-dns-transaction.h"
32 #include "resolved-llmnr.h"
33 #include "string-table.h"
35 #define TRANSACTIONS_MAX 4096
37 static void dns_transaction_reset_answer(DnsTransaction
*t
) {
40 t
->received
= dns_packet_unref(t
->received
);
41 t
->answer
= dns_answer_unref(t
->answer
);
43 t
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
44 t
->answer_source
= _DNS_TRANSACTION_SOURCE_INVALID
;
45 t
->answer_authenticated
= false;
46 t
->answer_nsec_ttl
= (uint32_t) -1;
50 static void dns_transaction_flush_dnssec_transactions(DnsTransaction
*t
) {
55 while ((z
= set_steal_first(t
->dnssec_transactions
))) {
56 set_remove(z
->notify_transactions
, t
);
57 dns_transaction_gc(z
);
61 static void dns_transaction_close_connection(DnsTransaction
*t
) {
64 t
->stream
= dns_stream_free(t
->stream
);
65 t
->dns_udp_event_source
= sd_event_source_unref(t
->dns_udp_event_source
);
66 t
->dns_udp_fd
= safe_close(t
->dns_udp_fd
);
69 static void dns_transaction_stop_timeout(DnsTransaction
*t
) {
72 t
->timeout_event_source
= sd_event_source_unref(t
->timeout_event_source
);
75 DnsTransaction
* dns_transaction_free(DnsTransaction
*t
) {
83 log_debug("Freeing transaction %" PRIu16
".", t
->id
);
85 dns_transaction_close_connection(t
);
86 dns_transaction_stop_timeout(t
);
88 dns_packet_unref(t
->sent
);
89 dns_transaction_reset_answer(t
);
91 dns_server_unref(t
->server
);
94 hashmap_remove_value(t
->scope
->transactions_by_key
, t
->key
, t
);
95 LIST_REMOVE(transactions_by_scope
, t
->scope
->transactions
, t
);
98 hashmap_remove(t
->scope
->manager
->dns_transactions
, UINT_TO_PTR(t
->id
));
101 while ((c
= set_steal_first(t
->notify_query_candidates
)))
102 set_remove(c
->transactions
, t
);
103 set_free(t
->notify_query_candidates
);
105 while ((i
= set_steal_first(t
->notify_zone_items
)))
106 i
->probe_transaction
= NULL
;
107 set_free(t
->notify_zone_items
);
109 while ((z
= set_steal_first(t
->notify_transactions
)))
110 set_remove(z
->dnssec_transactions
, t
);
111 set_free(t
->notify_transactions
);
113 dns_transaction_flush_dnssec_transactions(t
);
114 set_free(t
->dnssec_transactions
);
116 dns_answer_unref(t
->validated_keys
);
117 dns_resource_key_unref(t
->key
);
124 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsTransaction
*, dns_transaction_free
);
126 bool dns_transaction_gc(DnsTransaction
*t
) {
132 if (set_isempty(t
->notify_query_candidates
) &&
133 set_isempty(t
->notify_zone_items
) &&
134 set_isempty(t
->notify_transactions
)) {
135 dns_transaction_free(t
);
142 static uint16_t pick_new_id(Manager
*m
) {
145 /* Find a fresh, unused transaction id. Note that this loop is bounded because there's a limit on the number of
146 * transactions, and it's much lower than the space of IDs. */
148 assert_cc(TRANSACTIONS_MAX
< 0xFFFF);
151 random_bytes(&new_id
, sizeof(new_id
));
152 while (new_id
== 0 ||
153 hashmap_get(m
->dns_transactions
, UINT_TO_PTR(new_id
)));
158 int dns_transaction_new(DnsTransaction
**ret
, DnsScope
*s
, DnsResourceKey
*key
) {
159 _cleanup_(dns_transaction_freep
) DnsTransaction
*t
= NULL
;
166 /* Don't allow looking up invalid or pseudo RRs */
167 if (!dns_type_is_valid_query(key
->type
))
169 if (dns_type_is_obsolete(key
->type
))
172 /* We only support the IN class */
173 if (key
->class != DNS_CLASS_IN
&& key
->class != DNS_CLASS_ANY
)
176 if (hashmap_size(s
->manager
->dns_transactions
) >= TRANSACTIONS_MAX
)
179 r
= hashmap_ensure_allocated(&s
->manager
->dns_transactions
, NULL
);
183 r
= hashmap_ensure_allocated(&s
->transactions_by_key
, &dns_resource_key_hash_ops
);
187 t
= new0(DnsTransaction
, 1);
192 t
->answer_source
= _DNS_TRANSACTION_SOURCE_INVALID
;
193 t
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
194 t
->answer_nsec_ttl
= (uint32_t) -1;
195 t
->key
= dns_resource_key_ref(key
);
196 t
->current_feature_level
= _DNS_SERVER_FEATURE_LEVEL_INVALID
;
198 t
->id
= pick_new_id(s
->manager
);
200 r
= hashmap_put(s
->manager
->dns_transactions
, UINT_TO_PTR(t
->id
), t
);
206 r
= hashmap_replace(s
->transactions_by_key
, t
->key
, t
);
208 hashmap_remove(s
->manager
->dns_transactions
, UINT_TO_PTR(t
->id
));
212 LIST_PREPEND(transactions_by_scope
, s
->transactions
, t
);
215 s
->manager
->n_transactions_total
++;
225 static void dns_transaction_shuffle_id(DnsTransaction
*t
) {
229 /* Pick a new ID for this transaction. */
231 new_id
= pick_new_id(t
->scope
->manager
);
232 assert_se(hashmap_remove_and_put(t
->scope
->manager
->dns_transactions
, UINT_TO_PTR(t
->id
), UINT_TO_PTR(new_id
), t
) >= 0);
234 log_debug("Transaction %" PRIu16
" is now %" PRIu16
".", t
->id
, new_id
);
237 /* Make sure we generate a new packet with the new ID */
238 t
->sent
= dns_packet_unref(t
->sent
);
241 static void dns_transaction_tentative(DnsTransaction
*t
, DnsPacket
*p
) {
242 _cleanup_free_
char *pretty
= NULL
;
248 if (manager_our_packet(t
->scope
->manager
, p
) != 0)
251 in_addr_to_string(p
->family
, &p
->sender
, &pretty
);
253 log_debug("Transaction %" PRIu16
" for <%s> on scope %s on %s/%s got tentative packet from %s.",
255 dns_transaction_key_string(t
),
256 dns_protocol_to_string(t
->scope
->protocol
),
257 t
->scope
->link
? t
->scope
->link
->name
: "*",
258 t
->scope
->family
== AF_UNSPEC
? "*" : af_to_name(t
->scope
->family
),
261 /* RFC 4795, Section 4.1 says that the peer with the
262 * lexicographically smaller IP address loses */
263 if (memcmp(&p
->sender
, &p
->destination
, FAMILY_ADDRESS_SIZE(p
->family
)) >= 0) {
264 log_debug("Peer has lexicographically larger IP address and thus lost in the conflict.");
268 log_debug("We have the lexicographically larger IP address and thus lost in the conflict.");
271 while ((z
= set_first(t
->notify_zone_items
))) {
272 /* First, make sure the zone item drops the reference
274 dns_zone_item_probe_stop(z
);
276 /* Secondly, report this as conflict, so that we might
277 * look for a different hostname */
278 dns_zone_item_conflict(z
);
282 dns_transaction_gc(t
);
285 void dns_transaction_complete(DnsTransaction
*t
, DnsTransactionState state
) {
286 DnsQueryCandidate
*c
;
293 assert(!DNS_TRANSACTION_IS_LIVE(state
));
295 if (state
== DNS_TRANSACTION_DNSSEC_FAILED
)
296 log_struct(LOG_NOTICE
,
297 LOG_MESSAGE_ID(SD_MESSAGE_DNSSEC_FAILURE
),
298 LOG_MESSAGE("DNSSEC validation failed for question %s: %s", dns_transaction_key_string(t
), dnssec_result_to_string(t
->answer_dnssec_result
)),
299 "DNS_TRANSACTION=%" PRIu16
, t
->id
,
300 "DNS_QUESTION=%s", dns_transaction_key_string(t
),
301 "DNSSEC_RESULT=%s", dnssec_result_to_string(t
->answer_dnssec_result
),
302 "DNS_SERVER=%s", dns_server_string(t
->server
),
303 "DNS_SERVER_FEATURE_LEVEL=%s", dns_server_feature_level_to_string(t
->server
->possible_feature_level
),
306 /* Note that this call might invalidate the query. Callers
307 * should hence not attempt to access the query or transaction
308 * after calling this function. */
310 if (state
== DNS_TRANSACTION_ERRNO
)
311 st
= errno_to_name(t
->answer_errno
);
313 st
= dns_transaction_state_to_string(state
);
315 log_debug("Transaction %" PRIu16
" for <%s> on scope %s on %s/%s now complete with <%s> from %s (%s).",
317 dns_transaction_key_string(t
),
318 dns_protocol_to_string(t
->scope
->protocol
),
319 t
->scope
->link
? t
->scope
->link
->name
: "*",
320 t
->scope
->family
== AF_UNSPEC
? "*" : af_to_name(t
->scope
->family
),
322 t
->answer_source
< 0 ? "none" : dns_transaction_source_to_string(t
->answer_source
),
323 t
->answer_authenticated
? "authenticated" : "unsigned");
327 dns_transaction_close_connection(t
);
328 dns_transaction_stop_timeout(t
);
330 /* Notify all queries that are interested, but make sure the
331 * transaction isn't freed while we are still looking at it */
334 SET_FOREACH(c
, t
->notify_query_candidates
, i
)
335 dns_query_candidate_notify(c
);
336 SET_FOREACH(z
, t
->notify_zone_items
, i
)
337 dns_zone_item_notify(z
);
339 if (!set_isempty(t
->notify_transactions
)) {
343 /* We need to be careful when notifying other
344 * transactions, as that might destroy other
345 * transactions in our list. Hence, in order to be
346 * able to safely iterate through the list of
347 * transactions, take a GC lock on all of them
348 * first. Then, in a second loop, notify them, but
349 * first unlock that specific transaction. */
351 nt
= newa(DnsTransaction
*, set_size(t
->notify_transactions
));
352 SET_FOREACH(d
, t
->notify_transactions
, i
) {
357 assert(n
== set_size(t
->notify_transactions
));
359 for (j
= 0; j
< n
; j
++) {
360 if (set_contains(t
->notify_transactions
, nt
[j
]))
361 dns_transaction_notify(nt
[j
], t
);
364 dns_transaction_gc(nt
[j
]);
369 dns_transaction_gc(t
);
372 static int dns_transaction_pick_server(DnsTransaction
*t
) {
376 assert(t
->scope
->protocol
== DNS_PROTOCOL_DNS
);
378 server
= dns_scope_get_dns_server(t
->scope
);
382 t
->current_feature_level
= dns_server_possible_feature_level(server
);
384 if (server
== t
->server
)
387 dns_server_unref(t
->server
);
388 t
->server
= dns_server_ref(server
);
393 static void dns_transaction_retry(DnsTransaction
*t
) {
398 log_debug("Retrying transaction %" PRIu16
".", t
->id
);
400 /* Before we try again, switch to a new server. */
401 dns_scope_next_dns_server(t
->scope
);
403 r
= dns_transaction_go(t
);
405 t
->answer_errno
= -r
;
406 dns_transaction_complete(t
, DNS_TRANSACTION_ERRNO
);
410 static int dns_transaction_maybe_restart(DnsTransaction
*t
) {
416 if (t
->current_feature_level
<= dns_server_possible_feature_level(t
->server
))
419 /* The server's current feature level is lower than when we sent the original query. We learnt something from
420 the response or possibly an auxiliary DNSSEC response that we didn't know before. We take that as reason to
421 restart the whole transaction. This is a good idea to deal with servers that respond rubbish if we include
422 OPT RR or DO bit. One of these cases is documented here, for example:
423 https://open.nlnetlabs.nl/pipermail/dnssec-trigger/2014-November/000376.html */
425 log_debug("Server feature level is now lower than when we began our transaction. Restarting with new ID.");
426 dns_transaction_shuffle_id(t
);
427 return dns_transaction_go(t
);
430 static int on_stream_complete(DnsStream
*s
, int error
) {
431 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
435 assert(s
->transaction
);
437 /* Copy the data we care about out of the stream before we
440 p
= dns_packet_ref(s
->read_packet
);
442 t
->stream
= dns_stream_free(t
->stream
);
444 if (ERRNO_IS_DISCONNECT(error
)) {
447 if (t
->scope
->protocol
== DNS_PROTOCOL_LLMNR
) {
448 /* If the LLMNR/TCP connection failed, the host doesn't support LLMNR, and we cannot answer the
449 * question on this scope. */
450 dns_transaction_complete(t
, DNS_TRANSACTION_NOT_FOUND
);
454 log_debug_errno(error
, "Connection failure for DNS TCP stream: %m");
455 assert_se(sd_event_now(t
->scope
->manager
->event
, clock_boottime_or_monotonic(), &usec
) >= 0);
456 dns_server_packet_lost(t
->server
, IPPROTO_TCP
, t
->current_feature_level
, usec
- t
->start_usec
);
458 dns_transaction_retry(t
);
462 t
->answer_errno
= error
;
463 dns_transaction_complete(t
, DNS_TRANSACTION_ERRNO
);
467 if (dns_packet_validate_reply(p
) <= 0) {
468 log_debug("Invalid TCP reply packet.");
469 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
473 dns_scope_check_conflicts(t
->scope
, p
);
476 dns_transaction_process_reply(t
, p
);
479 /* If the response wasn't useful, then complete the transition
480 * now. After all, we are the worst feature set now with TCP
481 * sockets, and there's really no point in retrying. */
482 if (t
->state
== DNS_TRANSACTION_PENDING
)
483 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
485 dns_transaction_gc(t
);
490 static int dns_transaction_open_tcp(DnsTransaction
*t
) {
491 _cleanup_close_
int fd
= -1;
496 dns_transaction_close_connection(t
);
498 switch (t
->scope
->protocol
) {
500 case DNS_PROTOCOL_DNS
:
501 r
= dns_transaction_pick_server(t
);
505 if (!dns_server_dnssec_supported(t
->server
) && dns_type_is_dnssec(t
->key
->type
))
508 r
= dns_server_adjust_opt(t
->server
, t
->sent
, t
->current_feature_level
);
512 fd
= dns_scope_socket_tcp(t
->scope
, AF_UNSPEC
, NULL
, t
->server
, 53);
515 case DNS_PROTOCOL_LLMNR
:
516 /* When we already received a reply to this (but it was truncated), send to its sender address */
518 fd
= dns_scope_socket_tcp(t
->scope
, t
->received
->family
, &t
->received
->sender
, NULL
, t
->received
->sender_port
);
520 union in_addr_union address
;
521 int family
= AF_UNSPEC
;
523 /* Otherwise, try to talk to the owner of a
524 * the IP address, in case this is a reverse
527 r
= dns_name_address(DNS_RESOURCE_KEY_NAME(t
->key
), &family
, &address
);
532 if (family
!= t
->scope
->family
)
535 fd
= dns_scope_socket_tcp(t
->scope
, family
, &address
, NULL
, LLMNR_PORT
);
541 return -EAFNOSUPPORT
;
547 r
= dns_stream_new(t
->scope
->manager
, &t
->stream
, t
->scope
->protocol
, fd
);
552 r
= dns_stream_write_packet(t
->stream
, t
->sent
);
554 t
->stream
= dns_stream_free(t
->stream
);
558 t
->stream
->complete
= on_stream_complete
;
559 t
->stream
->transaction
= t
;
561 /* The interface index is difficult to determine if we are
562 * connecting to the local host, hence fill this in right away
563 * instead of determining it from the socket */
565 t
->stream
->ifindex
= t
->scope
->link
->ifindex
;
567 dns_transaction_reset_answer(t
);
569 t
->tried_stream
= true;
574 static void dns_transaction_cache_answer(DnsTransaction
*t
) {
577 /* For mDNS we cache whenever we get the packet, rather than
578 * in each transaction. */
579 if (!IN_SET(t
->scope
->protocol
, DNS_PROTOCOL_DNS
, DNS_PROTOCOL_LLMNR
))
582 /* We never cache if this packet is from the local host, under
583 * the assumption that a locally running DNS server would
584 * cache this anyway, and probably knows better when to flush
585 * the cache then we could. */
586 if (!DNS_PACKET_SHALL_CACHE(t
->received
))
589 dns_cache_put(&t
->scope
->cache
,
593 t
->answer_authenticated
,
597 &t
->received
->sender
);
600 static bool dns_transaction_dnssec_is_live(DnsTransaction
*t
) {
606 SET_FOREACH(dt
, t
->dnssec_transactions
, i
)
607 if (DNS_TRANSACTION_IS_LIVE(dt
->state
))
613 static int dns_transaction_dnssec_ready(DnsTransaction
*t
) {
619 /* Checks whether the auxiliary DNSSEC transactions of our transaction have completed, or are still
620 * ongoing. Returns 0, if we aren't ready for the DNSSEC validation, positive if we are. */
622 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
626 case DNS_TRANSACTION_NULL
:
627 case DNS_TRANSACTION_PENDING
:
628 case DNS_TRANSACTION_VALIDATING
:
632 case DNS_TRANSACTION_RCODE_FAILURE
:
633 if (dt
->answer_rcode
!= DNS_RCODE_NXDOMAIN
) {
634 log_debug("Auxiliary DNSSEC RR query failed with rcode=%s.", dns_rcode_to_string(dt
->answer_rcode
));
638 /* Fall-through: NXDOMAIN is good enough for us. This is because some DNS servers erronously
639 * return NXDOMAIN for empty non-terminals (Akamai...), and we need to handle that nicely, when
640 * asking for parent SOA or similar RRs to make unsigned proofs. */
642 case DNS_TRANSACTION_SUCCESS
:
646 case DNS_TRANSACTION_DNSSEC_FAILED
:
647 /* We handle DNSSEC failures different from other errors, as we care about the DNSSEC
648 * validationr result */
650 log_debug("Auxiliary DNSSEC RR query failed validation: %s", dnssec_result_to_string(dt
->answer_dnssec_result
));
651 t
->answer_dnssec_result
= dt
->answer_dnssec_result
; /* Copy error code over */
652 dns_transaction_complete(t
, DNS_TRANSACTION_DNSSEC_FAILED
);
657 log_debug("Auxiliary DNSSEC RR query failed with %s", dns_transaction_state_to_string(dt
->state
));
662 /* All is ready, we can go and validate */
666 t
->answer_dnssec_result
= DNSSEC_FAILED_AUXILIARY
;
667 dns_transaction_complete(t
, DNS_TRANSACTION_DNSSEC_FAILED
);
671 static void dns_transaction_process_dnssec(DnsTransaction
*t
) {
676 /* Are there ongoing DNSSEC transactions? If so, let's wait for them. */
677 r
= dns_transaction_dnssec_ready(t
);
680 if (r
== 0) /* We aren't ready yet (or one of our auxiliary transactions failed, and we shouldn't validate now */
683 /* See if we learnt things from the additional DNSSEC transactions, that we didn't know before, and better
684 * restart the lookup immediately. */
685 r
= dns_transaction_maybe_restart(t
);
688 if (r
> 0) /* Transaction got restarted... */
691 /* All our auxiliary DNSSEC transactions are complete now. Try
692 * to validate our RRset now. */
693 r
= dns_transaction_validate_dnssec(t
);
695 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
701 if (t
->answer_dnssec_result
== DNSSEC_INCOMPATIBLE_SERVER
&&
702 t
->scope
->dnssec_mode
== DNSSEC_YES
) {
703 /* We are not in automatic downgrade mode, and the
704 * server is bad, refuse operation. */
705 dns_transaction_complete(t
, DNS_TRANSACTION_DNSSEC_FAILED
);
709 if (!IN_SET(t
->answer_dnssec_result
,
710 _DNSSEC_RESULT_INVALID
, /* No DNSSEC validation enabled */
711 DNSSEC_VALIDATED
, /* Answer is signed and validated successfully */
712 DNSSEC_UNSIGNED
, /* Answer is right-fully unsigned */
713 DNSSEC_INCOMPATIBLE_SERVER
)) { /* Server does not do DNSSEC (Yay, we are downgrade attack vulnerable!) */
714 dns_transaction_complete(t
, DNS_TRANSACTION_DNSSEC_FAILED
);
718 if (t
->answer_dnssec_result
== DNSSEC_INCOMPATIBLE_SERVER
)
719 dns_server_warn_downgrade(t
->server
);
721 dns_transaction_cache_answer(t
);
723 if (t
->answer_rcode
== DNS_RCODE_SUCCESS
)
724 dns_transaction_complete(t
, DNS_TRANSACTION_SUCCESS
);
726 dns_transaction_complete(t
, DNS_TRANSACTION_RCODE_FAILURE
);
731 t
->answer_errno
= -r
;
732 dns_transaction_complete(t
, DNS_TRANSACTION_ERRNO
);
735 static int dns_transaction_has_positive_answer(DnsTransaction
*t
, DnsAnswerFlags
*flags
) {
740 /* Checks whether the answer is positive, i.e. either a direct
741 * answer to the question, or a CNAME/DNAME for it */
743 r
= dns_answer_match_key(t
->answer
, t
->key
, flags
);
747 r
= dns_answer_find_cname_or_dname(t
->answer
, t
->key
, NULL
, flags
);
754 static int dns_transaction_fix_rcode(DnsTransaction
*t
) {
759 /* Fix up the RCODE to SUCCESS if we get at least one matching RR in a response. Note that this contradicts the
760 * DNS RFCs a bit. Specifically, RFC 6604 Section 3 clarifies that the RCODE shall say something about a
761 * CNAME/DNAME chain element coming after the last chain element contained in the message, and not the first
762 * one included. However, it also indicates that not all DNS servers implement this correctly. Moreover, when
763 * using DNSSEC we usually only can prove the first element of a CNAME/DNAME chain anyway, hence let's settle
764 * on always processing the RCODE as referring to the immediate look-up we do, i.e. the first element of a
765 * CNAME/DNAME chain. This way, we uniformly handle CNAME/DNAME chains, regardless if the DNS server
766 * incorrectly implements RCODE, whether DNSSEC is in use, or whether the DNS server only supplied us with an
767 * incomplete CNAME/DNAME chain.
769 * Or in other words: if we get at least one positive reply in a message we patch NXDOMAIN to become SUCCESS,
770 * and then rely on the CNAME chasing logic to figure out that there's actually a CNAME error with a new
773 if (t
->answer_rcode
!= DNS_RCODE_NXDOMAIN
)
776 r
= dns_transaction_has_positive_answer(t
, NULL
);
780 t
->answer_rcode
= DNS_RCODE_SUCCESS
;
784 void dns_transaction_process_reply(DnsTransaction
*t
, DnsPacket
*p
) {
791 assert(t
->scope
->manager
);
793 if (t
->state
!= DNS_TRANSACTION_PENDING
)
796 /* Note that this call might invalidate the query. Callers
797 * should hence not attempt to access the query or transaction
798 * after calling this function. */
800 log_debug("Processing incoming packet on transaction %" PRIu16
".", t
->id
);
802 switch (t
->scope
->protocol
) {
804 case DNS_PROTOCOL_LLMNR
:
805 assert(t
->scope
->link
);
807 /* For LLMNR we will not accept any packets from other
810 if (p
->ifindex
!= t
->scope
->link
->ifindex
)
813 if (p
->family
!= t
->scope
->family
)
816 /* Tentative packets are not full responses but still
817 * useful for identifying uniqueness conflicts during
819 if (DNS_PACKET_LLMNR_T(p
)) {
820 dns_transaction_tentative(t
, p
);
826 case DNS_PROTOCOL_MDNS
:
827 assert(t
->scope
->link
);
829 /* For mDNS we will not accept any packets from other interfaces */
830 if (p
->ifindex
!= t
->scope
->link
->ifindex
)
833 if (p
->family
!= t
->scope
->family
)
838 case DNS_PROTOCOL_DNS
:
839 /* Note that we do not need to verify the
840 * addresses/port numbers of incoming traffic, as we
841 * invoked connect() on our UDP socket in which case
842 * the kernel already does the needed verification for
847 assert_not_reached("Invalid DNS protocol.");
850 if (t
->received
!= p
) {
851 dns_packet_unref(t
->received
);
852 t
->received
= dns_packet_ref(p
);
855 t
->answer_source
= DNS_TRANSACTION_NETWORK
;
857 if (p
->ipproto
== IPPROTO_TCP
) {
858 if (DNS_PACKET_TC(p
)) {
859 /* Truncated via TCP? Somebody must be fucking with us */
860 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
864 if (DNS_PACKET_ID(p
) != t
->id
) {
865 /* Not the reply to our query? Somebody must be fucking with us */
866 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
871 assert_se(sd_event_now(t
->scope
->manager
->event
, clock_boottime_or_monotonic(), &ts
) >= 0);
873 switch (t
->scope
->protocol
) {
875 case DNS_PROTOCOL_DNS
:
878 if (IN_SET(DNS_PACKET_RCODE(p
), DNS_RCODE_FORMERR
, DNS_RCODE_SERVFAIL
, DNS_RCODE_NOTIMP
)) {
880 /* Request failed, immediately try again with reduced features */
881 log_debug("Server returned error: %s", dns_rcode_to_string(DNS_PACKET_RCODE(p
)));
883 dns_server_packet_failed(t
->server
, t
->current_feature_level
);
884 dns_transaction_retry(t
);
886 } else if (DNS_PACKET_TC(p
))
887 dns_server_packet_truncated(t
->server
, t
->current_feature_level
);
891 case DNS_PROTOCOL_LLMNR
:
892 case DNS_PROTOCOL_MDNS
:
893 dns_scope_packet_received(t
->scope
, ts
- t
->start_usec
);
897 assert_not_reached("Invalid DNS protocol.");
900 if (DNS_PACKET_TC(p
)) {
902 /* Truncated packets for mDNS are not allowed. Give up immediately. */
903 if (t
->scope
->protocol
== DNS_PROTOCOL_MDNS
) {
904 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
908 log_debug("Reply truncated, retrying via TCP.");
910 /* Response was truncated, let's try again with good old TCP */
911 r
= dns_transaction_open_tcp(t
);
913 /* No servers found? Damn! */
914 dns_transaction_complete(t
, DNS_TRANSACTION_NO_SERVERS
);
917 if (r
== -EOPNOTSUPP
) {
918 /* Tried to ask for DNSSEC RRs, on a server that doesn't do DNSSEC */
919 dns_transaction_complete(t
, DNS_TRANSACTION_RR_TYPE_UNSUPPORTED
);
923 /* On LLMNR, if we cannot connect to the host,
924 * we immediately give up */
925 if (t
->scope
->protocol
!= DNS_PROTOCOL_DNS
)
928 /* On DNS, couldn't send? Try immediately again, with a new server */
929 dns_transaction_retry(t
);
935 /* After the superficial checks, actually parse the message. */
936 r
= dns_packet_extract(p
);
938 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
942 /* Report that the OPT RR was missing */
945 dns_server_packet_bad_opt(t
->server
, t
->current_feature_level
);
947 dns_server_packet_received(t
->server
, p
->ipproto
, t
->current_feature_level
, ts
- t
->start_usec
, p
->size
);
950 /* See if we know things we didn't know before that indicate we better restart the lookup immediately. */
951 r
= dns_transaction_maybe_restart(t
);
954 if (r
> 0) /* Transaction got restarted... */
957 if (IN_SET(t
->scope
->protocol
, DNS_PROTOCOL_DNS
, DNS_PROTOCOL_LLMNR
)) {
959 /* Only consider responses with equivalent query section to the request */
960 r
= dns_packet_is_reply_for(p
, t
->key
);
964 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
968 /* Install the answer as answer to the transaction */
969 dns_answer_unref(t
->answer
);
970 t
->answer
= dns_answer_ref(p
->answer
);
971 t
->answer_rcode
= DNS_PACKET_RCODE(p
);
972 t
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
973 t
->answer_authenticated
= false;
975 r
= dns_transaction_fix_rcode(t
);
979 /* Block GC while starting requests for additional DNSSEC RRs */
981 r
= dns_transaction_request_dnssec_keys(t
);
984 /* Maybe the transaction is ready for GC'ing now? If so, free it and return. */
985 if (!dns_transaction_gc(t
))
988 /* Requesting additional keys might have resulted in
989 * this transaction to fail, since the auxiliary
990 * request failed for some reason. If so, we are not
991 * in pending state anymore, and we should exit
993 if (t
->state
!= DNS_TRANSACTION_PENDING
)
998 /* There are DNSSEC transactions pending now. Update the state accordingly. */
999 t
->state
= DNS_TRANSACTION_VALIDATING
;
1000 dns_transaction_close_connection(t
);
1001 dns_transaction_stop_timeout(t
);
1006 dns_transaction_process_dnssec(t
);
1010 t
->answer_errno
= -r
;
1011 dns_transaction_complete(t
, DNS_TRANSACTION_ERRNO
);
1014 static int on_dns_packet(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
1015 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
1016 DnsTransaction
*t
= userdata
;
1022 r
= manager_recv(t
->scope
->manager
, fd
, DNS_PROTOCOL_DNS
, &p
);
1023 if (ERRNO_IS_DISCONNECT(-r
)) {
1026 /* UDP connection failure get reported via ICMP and then are possible delivered to us on the next
1027 * recvmsg(). Treat this like a lost packet. */
1029 log_debug_errno(r
, "Connection failure for DNS UDP packet: %m");
1030 assert_se(sd_event_now(t
->scope
->manager
->event
, clock_boottime_or_monotonic(), &usec
) >= 0);
1031 dns_server_packet_lost(t
->server
, IPPROTO_UDP
, t
->current_feature_level
, usec
- t
->start_usec
);
1033 dns_transaction_retry(t
);
1037 dns_transaction_complete(t
, DNS_TRANSACTION_ERRNO
);
1038 t
->answer_errno
= -r
;
1042 r
= dns_packet_validate_reply(p
);
1044 log_debug_errno(r
, "Received invalid DNS packet as response, ignoring: %m");
1048 log_debug("Received inappropriate DNS packet as response, ignoring.");
1052 if (DNS_PACKET_ID(p
) != t
->id
) {
1053 log_debug("Received packet with incorrect transaction ID, ignoring.");
1057 dns_transaction_process_reply(t
, p
);
1061 static int dns_transaction_emit_udp(DnsTransaction
*t
) {
1066 if (t
->scope
->protocol
== DNS_PROTOCOL_DNS
) {
1068 r
= dns_transaction_pick_server(t
);
1072 if (t
->current_feature_level
< DNS_SERVER_FEATURE_LEVEL_UDP
)
1075 if (!dns_server_dnssec_supported(t
->server
) && dns_type_is_dnssec(t
->key
->type
))
1078 if (r
> 0 || t
->dns_udp_fd
< 0) { /* Server changed, or no connection yet. */
1081 dns_transaction_close_connection(t
);
1083 fd
= dns_scope_socket_udp(t
->scope
, t
->server
, 53);
1087 r
= sd_event_add_io(t
->scope
->manager
->event
, &t
->dns_udp_event_source
, fd
, EPOLLIN
, on_dns_packet
, t
);
1093 (void) sd_event_source_set_description(t
->dns_udp_event_source
, "dns-transaction-udp");
1097 r
= dns_server_adjust_opt(t
->server
, t
->sent
, t
->current_feature_level
);
1101 dns_transaction_close_connection(t
);
1103 r
= dns_scope_emit_udp(t
->scope
, t
->dns_udp_fd
, t
->sent
);
1107 dns_transaction_reset_answer(t
);
1112 static int on_transaction_timeout(sd_event_source
*s
, usec_t usec
, void *userdata
) {
1113 DnsTransaction
*t
= userdata
;
1118 if (!t
->initial_jitter_scheduled
|| t
->initial_jitter_elapsed
) {
1119 /* Timeout reached? Increase the timeout for the server used */
1120 switch (t
->scope
->protocol
) {
1122 case DNS_PROTOCOL_DNS
:
1124 dns_server_packet_lost(t
->server
, t
->stream
? IPPROTO_TCP
: IPPROTO_UDP
, t
->current_feature_level
, usec
- t
->start_usec
);
1127 case DNS_PROTOCOL_LLMNR
:
1128 case DNS_PROTOCOL_MDNS
:
1129 dns_scope_packet_lost(t
->scope
, usec
- t
->start_usec
);
1133 assert_not_reached("Invalid DNS protocol.");
1136 if (t
->initial_jitter_scheduled
)
1137 t
->initial_jitter_elapsed
= true;
1140 log_debug("Timeout reached on transaction %" PRIu16
".", t
->id
);
1142 dns_transaction_retry(t
);
1146 static usec_t
transaction_get_resend_timeout(DnsTransaction
*t
) {
1150 switch (t
->scope
->protocol
) {
1152 case DNS_PROTOCOL_DNS
:
1154 return t
->server
->resend_timeout
;
1156 case DNS_PROTOCOL_MDNS
:
1157 assert(t
->n_attempts
> 0);
1158 return (1 << (t
->n_attempts
- 1)) * USEC_PER_SEC
;
1160 case DNS_PROTOCOL_LLMNR
:
1161 return t
->scope
->resend_timeout
;
1164 assert_not_reached("Invalid DNS protocol.");
1168 static int dns_transaction_prepare(DnsTransaction
*t
, usec_t ts
) {
1173 dns_transaction_stop_timeout(t
);
1175 r
= dns_scope_network_good(t
->scope
);
1179 dns_transaction_complete(t
, DNS_TRANSACTION_NETWORK_DOWN
);
1183 if (t
->n_attempts
>= TRANSACTION_ATTEMPTS_MAX(t
->scope
->protocol
)) {
1184 dns_transaction_complete(t
, DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
);
1188 if (t
->scope
->protocol
== DNS_PROTOCOL_LLMNR
&& t
->tried_stream
) {
1189 /* If we already tried via a stream, then we don't
1190 * retry on LLMNR. See RFC 4795, Section 2.7. */
1191 dns_transaction_complete(t
, DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
);
1198 dns_transaction_reset_answer(t
);
1199 dns_transaction_flush_dnssec_transactions(t
);
1201 /* Check the trust anchor. Do so only on classic DNS, since DNSSEC does not apply otherwise. */
1202 if (t
->scope
->protocol
== DNS_PROTOCOL_DNS
) {
1203 r
= dns_trust_anchor_lookup_positive(&t
->scope
->manager
->trust_anchor
, t
->key
, &t
->answer
);
1207 t
->answer_rcode
= DNS_RCODE_SUCCESS
;
1208 t
->answer_source
= DNS_TRANSACTION_TRUST_ANCHOR
;
1209 t
->answer_authenticated
= true;
1210 dns_transaction_complete(t
, DNS_TRANSACTION_SUCCESS
);
1214 if (dns_name_is_root(DNS_RESOURCE_KEY_NAME(t
->key
)) &&
1215 t
->key
->type
== DNS_TYPE_DS
) {
1217 /* Hmm, this is a request for the root DS? A
1218 * DS RR doesn't exist in the root zone, and
1219 * if our trust anchor didn't know it either,
1220 * this means we cannot do any DNSSEC logic
1223 if (t
->scope
->dnssec_mode
== DNSSEC_ALLOW_DOWNGRADE
) {
1224 /* We are in downgrade mode. In this
1225 * case, synthesize an unsigned empty
1226 * response, so that the any lookup
1227 * depending on this one can continue
1228 * assuming there was no DS, and hence
1229 * the root zone was unsigned. */
1231 t
->answer_rcode
= DNS_RCODE_SUCCESS
;
1232 t
->answer_source
= DNS_TRANSACTION_TRUST_ANCHOR
;
1233 t
->answer_authenticated
= false;
1234 dns_transaction_complete(t
, DNS_TRANSACTION_SUCCESS
);
1236 /* If we are not in downgrade mode,
1237 * then fail the lookup, because we
1238 * cannot reasonably answer it. There
1239 * might be DS RRs, but we don't know
1240 * them, and the DNS server won't tell
1241 * them to us (and even if it would,
1242 * we couldn't validate it and trust
1244 dns_transaction_complete(t
, DNS_TRANSACTION_NO_TRUST_ANCHOR
);
1250 /* Check the zone, but only if this transaction is not used
1251 * for probing or verifying a zone item. */
1252 if (set_isempty(t
->notify_zone_items
)) {
1254 r
= dns_zone_lookup(&t
->scope
->zone
, t
->key
, &t
->answer
, NULL
, NULL
);
1258 t
->answer_rcode
= DNS_RCODE_SUCCESS
;
1259 t
->answer_source
= DNS_TRANSACTION_ZONE
;
1260 t
->answer_authenticated
= true;
1261 dns_transaction_complete(t
, DNS_TRANSACTION_SUCCESS
);
1266 /* Check the cache, but only if this transaction is not used
1267 * for probing or verifying a zone item. */
1268 if (set_isempty(t
->notify_zone_items
)) {
1270 /* Before trying the cache, let's make sure we figured out a
1271 * server to use. Should this cause a change of server this
1272 * might flush the cache. */
1273 dns_scope_get_dns_server(t
->scope
);
1275 /* Let's then prune all outdated entries */
1276 dns_cache_prune(&t
->scope
->cache
);
1278 r
= dns_cache_lookup(&t
->scope
->cache
, t
->key
, &t
->answer_rcode
, &t
->answer
, &t
->answer_authenticated
);
1282 t
->answer_source
= DNS_TRANSACTION_CACHE
;
1283 if (t
->answer_rcode
== DNS_RCODE_SUCCESS
)
1284 dns_transaction_complete(t
, DNS_TRANSACTION_SUCCESS
);
1286 dns_transaction_complete(t
, DNS_TRANSACTION_RCODE_FAILURE
);
1294 static int dns_transaction_make_packet_mdns(DnsTransaction
*t
) {
1296 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
1297 bool add_known_answers
= false;
1298 DnsTransaction
*other
;
1304 assert(t
->scope
->protocol
== DNS_PROTOCOL_MDNS
);
1306 /* Discard any previously prepared packet, so we can start over and coalesce again */
1307 t
->sent
= dns_packet_unref(t
->sent
);
1309 r
= dns_packet_new_query(&p
, t
->scope
->protocol
, 0, false);
1313 r
= dns_packet_append_key(p
, t
->key
, NULL
);
1319 if (dns_key_is_shared(t
->key
))
1320 add_known_answers
= true;
1323 * For mDNS, we want to coalesce as many open queries in pending transactions into one single
1324 * query packet on the wire as possible. To achieve that, we iterate through all pending transactions
1325 * in our current scope, and see whether their timing contraints allow them to be sent.
1328 assert_se(sd_event_now(t
->scope
->manager
->event
, clock_boottime_or_monotonic(), &ts
) >= 0);
1330 LIST_FOREACH(transactions_by_scope
, other
, t
->scope
->transactions
) {
1332 /* Skip ourselves */
1336 if (other
->state
!= DNS_TRANSACTION_PENDING
)
1339 if (other
->next_attempt_after
> ts
)
1342 if (qdcount
>= UINT16_MAX
)
1345 r
= dns_packet_append_key(p
, other
->key
, NULL
);
1348 * If we can't stuff more questions into the packet, just give up.
1349 * One of the 'other' transactions will fire later and take care of the rest.
1357 r
= dns_transaction_prepare(other
, ts
);
1361 ts
+= transaction_get_resend_timeout(other
);
1363 r
= sd_event_add_time(
1364 other
->scope
->manager
->event
,
1365 &other
->timeout_event_source
,
1366 clock_boottime_or_monotonic(),
1368 on_transaction_timeout
, other
);
1372 (void) sd_event_source_set_description(t
->timeout_event_source
, "dns-transaction-timeout");
1374 other
->state
= DNS_TRANSACTION_PENDING
;
1375 other
->next_attempt_after
= ts
;
1379 if (dns_key_is_shared(other
->key
))
1380 add_known_answers
= true;
1383 DNS_PACKET_HEADER(p
)->qdcount
= htobe16(qdcount
);
1385 /* Append known answer section if we're asking for any shared record */
1386 if (add_known_answers
) {
1387 r
= dns_cache_export_shared_to_packet(&t
->scope
->cache
, p
);
1398 static int dns_transaction_make_packet(DnsTransaction
*t
) {
1399 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
1404 if (t
->scope
->protocol
== DNS_PROTOCOL_MDNS
)
1405 return dns_transaction_make_packet_mdns(t
);
1410 r
= dns_packet_new_query(&p
, t
->scope
->protocol
, 0, t
->scope
->dnssec_mode
!= DNSSEC_NO
);
1414 r
= dns_scope_good_key(t
->scope
, t
->key
);
1420 r
= dns_packet_append_key(p
, t
->key
, NULL
);
1424 DNS_PACKET_HEADER(p
)->qdcount
= htobe16(1);
1425 DNS_PACKET_HEADER(p
)->id
= t
->id
;
1433 int dns_transaction_go(DnsTransaction
*t
) {
1439 assert_se(sd_event_now(t
->scope
->manager
->event
, clock_boottime_or_monotonic(), &ts
) >= 0);
1441 r
= dns_transaction_prepare(t
, ts
);
1445 log_debug("Excercising transaction %" PRIu16
" for <%s> on scope %s on %s/%s.",
1447 dns_transaction_key_string(t
),
1448 dns_protocol_to_string(t
->scope
->protocol
),
1449 t
->scope
->link
? t
->scope
->link
->name
: "*",
1450 t
->scope
->family
== AF_UNSPEC
? "*" : af_to_name(t
->scope
->family
));
1452 if (!t
->initial_jitter_scheduled
&&
1453 (t
->scope
->protocol
== DNS_PROTOCOL_LLMNR
||
1454 t
->scope
->protocol
== DNS_PROTOCOL_MDNS
)) {
1455 usec_t jitter
, accuracy
;
1457 /* RFC 4795 Section 2.7 suggests all queries should be
1458 * delayed by a random time from 0 to JITTER_INTERVAL. */
1460 t
->initial_jitter_scheduled
= true;
1462 random_bytes(&jitter
, sizeof(jitter
));
1464 switch (t
->scope
->protocol
) {
1466 case DNS_PROTOCOL_LLMNR
:
1467 jitter
%= LLMNR_JITTER_INTERVAL_USEC
;
1468 accuracy
= LLMNR_JITTER_INTERVAL_USEC
;
1471 case DNS_PROTOCOL_MDNS
:
1472 jitter
%= MDNS_JITTER_RANGE_USEC
;
1473 jitter
+= MDNS_JITTER_MIN_USEC
;
1474 accuracy
= MDNS_JITTER_RANGE_USEC
;
1477 assert_not_reached("bad protocol");
1480 r
= sd_event_add_time(
1481 t
->scope
->manager
->event
,
1482 &t
->timeout_event_source
,
1483 clock_boottime_or_monotonic(),
1484 ts
+ jitter
, accuracy
,
1485 on_transaction_timeout
, t
);
1489 (void) sd_event_source_set_description(t
->timeout_event_source
, "dns-transaction-timeout");
1492 t
->next_attempt_after
= ts
;
1493 t
->state
= DNS_TRANSACTION_PENDING
;
1495 log_debug("Delaying %s transaction for " USEC_FMT
"us.", dns_protocol_to_string(t
->scope
->protocol
), jitter
);
1499 /* Otherwise, we need to ask the network */
1500 r
= dns_transaction_make_packet(t
);
1502 /* Not the right request to make on this network?
1503 * (i.e. an A request made on IPv6 or an AAAA request
1504 * made on IPv4, on LLMNR or mDNS.) */
1505 dns_transaction_complete(t
, DNS_TRANSACTION_NO_SERVERS
);
1511 if (t
->scope
->protocol
== DNS_PROTOCOL_LLMNR
&&
1512 (dns_name_endswith(DNS_RESOURCE_KEY_NAME(t
->key
), "in-addr.arpa") > 0 ||
1513 dns_name_endswith(DNS_RESOURCE_KEY_NAME(t
->key
), "ip6.arpa") > 0)) {
1515 /* RFC 4795, Section 2.4. says reverse lookups shall
1516 * always be made via TCP on LLMNR */
1517 r
= dns_transaction_open_tcp(t
);
1519 /* Try via UDP, and if that fails due to large size or lack of
1520 * support try via TCP */
1521 r
= dns_transaction_emit_udp(t
);
1523 log_debug("Sending query via TCP since it is too large.");
1525 log_debug("Sending query via TCP since server doesn't support UDP.");
1526 if (r
== -EMSGSIZE
|| r
== -EAGAIN
)
1527 r
= dns_transaction_open_tcp(t
);
1531 /* No servers to send this to? */
1532 dns_transaction_complete(t
, DNS_TRANSACTION_NO_SERVERS
);
1535 if (r
== -EOPNOTSUPP
) {
1536 /* Tried to ask for DNSSEC RRs, on a server that doesn't do DNSSEC */
1537 dns_transaction_complete(t
, DNS_TRANSACTION_RR_TYPE_UNSUPPORTED
);
1540 if (t
->scope
->protocol
== DNS_PROTOCOL_LLMNR
&& ERRNO_IS_DISCONNECT(-r
)) {
1541 /* On LLMNR, if we cannot connect to a host via TCP when doing revers lookups. This means we cannot
1542 * answer this request with this protocol. */
1543 dns_transaction_complete(t
, DNS_TRANSACTION_NOT_FOUND
);
1547 if (t
->scope
->protocol
!= DNS_PROTOCOL_DNS
)
1550 /* Couldn't send? Try immediately again, with a new server */
1551 dns_scope_next_dns_server(t
->scope
);
1553 return dns_transaction_go(t
);
1556 ts
+= transaction_get_resend_timeout(t
);
1558 r
= sd_event_add_time(
1559 t
->scope
->manager
->event
,
1560 &t
->timeout_event_source
,
1561 clock_boottime_or_monotonic(),
1563 on_transaction_timeout
, t
);
1567 (void) sd_event_source_set_description(t
->timeout_event_source
, "dns-transaction-timeout");
1569 t
->state
= DNS_TRANSACTION_PENDING
;
1570 t
->next_attempt_after
= ts
;
1575 static int dns_transaction_find_cyclic(DnsTransaction
*t
, DnsTransaction
*aux
) {
1583 /* Try to find cyclic dependencies between transaction objects */
1588 SET_FOREACH(n
, aux
->dnssec_transactions
, i
) {
1589 r
= dns_transaction_find_cyclic(t
, n
);
1597 static int dns_transaction_add_dnssec_transaction(DnsTransaction
*t
, DnsResourceKey
*key
, DnsTransaction
**ret
) {
1598 DnsTransaction
*aux
;
1605 aux
= dns_scope_find_transaction(t
->scope
, key
, true);
1607 r
= dns_transaction_new(&aux
, t
->scope
, key
);
1611 if (set_contains(t
->dnssec_transactions
, aux
)) {
1616 r
= dns_transaction_find_cyclic(t
, aux
);
1620 log_debug("Detected potential cyclic dependency, refusing to add transaction %" PRIu16
" (%s) as dependency for %" PRIu16
" (%s).",
1622 strna(dns_transaction_key_string(aux
)),
1624 strna(dns_transaction_key_string(t
)));
1629 r
= set_ensure_allocated(&t
->dnssec_transactions
, NULL
);
1633 r
= set_ensure_allocated(&aux
->notify_transactions
, NULL
);
1637 r
= set_put(t
->dnssec_transactions
, aux
);
1641 r
= set_put(aux
->notify_transactions
, t
);
1643 (void) set_remove(t
->dnssec_transactions
, aux
);
1651 dns_transaction_gc(aux
);
1655 static int dns_transaction_request_dnssec_rr(DnsTransaction
*t
, DnsResourceKey
*key
) {
1656 _cleanup_(dns_answer_unrefp
) DnsAnswer
*a
= NULL
;
1657 DnsTransaction
*aux
;
1663 /* Try to get the data from the trust anchor */
1664 r
= dns_trust_anchor_lookup_positive(&t
->scope
->manager
->trust_anchor
, key
, &a
);
1668 r
= dns_answer_extend(&t
->validated_keys
, a
);
1675 /* This didn't work, ask for it via the network/cache then. */
1676 r
= dns_transaction_add_dnssec_transaction(t
, key
, &aux
);
1677 if (r
== -ELOOP
) /* This would result in a cyclic dependency */
1682 if (aux
->state
== DNS_TRANSACTION_NULL
) {
1683 r
= dns_transaction_go(aux
);
1691 static int dns_transaction_negative_trust_anchor_lookup(DnsTransaction
*t
, const char *name
) {
1696 /* Check whether the specified name is in the the NTA
1697 * database, either in the global one, or the link-local
1700 r
= dns_trust_anchor_lookup_negative(&t
->scope
->manager
->trust_anchor
, name
);
1704 if (!t
->scope
->link
)
1707 return set_contains(t
->scope
->link
->dnssec_negative_trust_anchors
, name
);
1710 static int dns_transaction_has_unsigned_negative_answer(DnsTransaction
*t
) {
1715 /* Checks whether the answer is negative, and lacks NSEC/NSEC3
1716 * RRs to prove it */
1718 r
= dns_transaction_has_positive_answer(t
, NULL
);
1724 /* Is this key explicitly listed as a negative trust anchor?
1725 * If so, it's nothing we need to care about */
1726 r
= dns_transaction_negative_trust_anchor_lookup(t
, DNS_RESOURCE_KEY_NAME(t
->key
));
1732 /* The answer does not contain any RRs that match to the
1733 * question. If so, let's see if there are any NSEC/NSEC3 RRs
1734 * included. If not, the answer is unsigned. */
1736 r
= dns_answer_contains_nsec_or_nsec3(t
->answer
);
1745 static int dns_transaction_is_primary_response(DnsTransaction
*t
, DnsResourceRecord
*rr
) {
1751 /* Check if the specified RR is the "primary" response,
1752 * i.e. either matches the question precisely or is a
1753 * CNAME/DNAME for it. */
1755 r
= dns_resource_key_match_rr(t
->key
, rr
, NULL
);
1759 return dns_resource_key_match_cname_or_dname(t
->key
, rr
->key
, NULL
);
1762 static bool dns_transaction_dnssec_supported(DnsTransaction
*t
) {
1765 /* Checks whether our transaction's DNS server is assumed to be compatible with DNSSEC. Returns false as soon
1766 * as we changed our mind about a server, and now believe it is incompatible with DNSSEC. */
1768 if (t
->scope
->protocol
!= DNS_PROTOCOL_DNS
)
1771 /* If we have picked no server, then we are working from the cache or some other source, and DNSSEC might well
1772 * be supported, hence return true. */
1776 if (t
->current_feature_level
< DNS_SERVER_FEATURE_LEVEL_DO
)
1779 return dns_server_dnssec_supported(t
->server
);
1782 static bool dns_transaction_dnssec_supported_full(DnsTransaction
*t
) {
1788 /* Checks whether our transaction our any of the auxiliary transactions couldn't do DNSSEC. */
1790 if (!dns_transaction_dnssec_supported(t
))
1793 SET_FOREACH(dt
, t
->dnssec_transactions
, i
)
1794 if (!dns_transaction_dnssec_supported(dt
))
1800 int dns_transaction_request_dnssec_keys(DnsTransaction
*t
) {
1801 DnsResourceRecord
*rr
;
1808 * Retrieve all auxiliary RRs for the answer we got, so that
1809 * we can verify signatures or prove that RRs are rightfully
1810 * unsigned. Specifically:
1812 * - For RRSIG we get the matching DNSKEY
1813 * - For DNSKEY we get the matching DS
1814 * - For unsigned SOA/NS we get the matching DS
1815 * - For unsigned CNAME/DNAME/DS we get the parent SOA RR
1816 * - For other unsigned RRs we get the matching SOA RR
1817 * - For SOA/NS/DS queries with no matching response RRs, and no NSEC/NSEC3, the parent's SOA RR
1818 * - For other queries with no matching response RRs, and no NSEC/NSEC3, the SOA RR
1821 if (t
->scope
->dnssec_mode
== DNSSEC_NO
)
1823 if (t
->answer_source
!= DNS_TRANSACTION_NETWORK
)
1824 return 0; /* We only need to validate stuff from the network */
1825 if (!dns_transaction_dnssec_supported(t
))
1826 return 0; /* If we can't do DNSSEC anyway there's no point in geting the auxiliary RRs */
1828 DNS_ANSWER_FOREACH(rr
, t
->answer
) {
1830 if (dns_type_is_pseudo(rr
->key
->type
))
1833 /* If this RR is in the negative trust anchor, we don't need to validate it. */
1834 r
= dns_transaction_negative_trust_anchor_lookup(t
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1840 switch (rr
->key
->type
) {
1842 case DNS_TYPE_RRSIG
: {
1843 /* For each RRSIG we request the matching DNSKEY */
1844 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*dnskey
= NULL
;
1846 /* If this RRSIG is about a DNSKEY RR and the
1847 * signer is the same as the owner, then we
1848 * already have the DNSKEY, and we don't have
1849 * to look for more. */
1850 if (rr
->rrsig
.type_covered
== DNS_TYPE_DNSKEY
) {
1851 r
= dns_name_equal(rr
->rrsig
.signer
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1858 /* If the signer is not a parent of our
1859 * original query, then this is about an
1860 * auxiliary RRset, but not anything we asked
1861 * for. In this case we aren't interested,
1862 * because we don't want to request additional
1863 * RRs for stuff we didn't really ask for, and
1864 * also to avoid request loops, where
1865 * additional RRs from one transaction result
1866 * in another transaction whose additonal RRs
1867 * point back to the original transaction, and
1869 r
= dns_name_endswith(DNS_RESOURCE_KEY_NAME(t
->key
), rr
->rrsig
.signer
);
1875 dnskey
= dns_resource_key_new(rr
->key
->class, DNS_TYPE_DNSKEY
, rr
->rrsig
.signer
);
1879 log_debug("Requesting DNSKEY to validate transaction %" PRIu16
" (%s, RRSIG with key tag: %" PRIu16
").", t
->id
, DNS_RESOURCE_KEY_NAME(rr
->key
), rr
->rrsig
.key_tag
);
1880 r
= dns_transaction_request_dnssec_rr(t
, dnskey
);
1886 case DNS_TYPE_DNSKEY
: {
1887 /* For each DNSKEY we request the matching DS */
1888 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*ds
= NULL
;
1890 /* If the DNSKEY we are looking at is not for
1891 * zone we are interested in, nor any of its
1892 * parents, we aren't interested, and don't
1893 * request it. After all, we don't want to end
1894 * up in request loops, and want to keep
1895 * additional traffic down. */
1897 r
= dns_name_endswith(DNS_RESOURCE_KEY_NAME(t
->key
), DNS_RESOURCE_KEY_NAME(rr
->key
));
1903 ds
= dns_resource_key_new(rr
->key
->class, DNS_TYPE_DS
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1907 log_debug("Requesting DS to validate transaction %" PRIu16
" (%s, DNSKEY with key tag: %" PRIu16
").", t
->id
, DNS_RESOURCE_KEY_NAME(rr
->key
), dnssec_keytag(rr
, false));
1908 r
= dns_transaction_request_dnssec_rr(t
, ds
);
1917 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*ds
= NULL
;
1919 /* For an unsigned SOA or NS, try to acquire
1920 * the matching DS RR, as we are at a zone cut
1921 * then, and whether a DS exists tells us
1922 * whether the zone is signed. Do so only if
1923 * this RR matches our original question,
1926 r
= dns_resource_key_match_rr(t
->key
, rr
, NULL
);
1932 r
= dnssec_has_rrsig(t
->answer
, rr
->key
);
1938 ds
= dns_resource_key_new(rr
->key
->class, DNS_TYPE_DS
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1942 log_debug("Requesting DS to validate transaction %" PRIu16
" (%s, unsigned SOA/NS RRset).", t
->id
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1943 r
= dns_transaction_request_dnssec_rr(t
, ds
);
1951 case DNS_TYPE_CNAME
:
1952 case DNS_TYPE_DNAME
: {
1953 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*soa
= NULL
;
1956 /* CNAMEs and DNAMEs cannot be located at a
1957 * zone apex, hence ask for the parent SOA for
1958 * unsigned CNAME/DNAME RRs, maybe that's the
1959 * apex. But do all that only if this is
1960 * actually a response to our original
1963 * Similar for DS RRs, which are signed when
1964 * the parent SOA is signed. */
1966 r
= dns_transaction_is_primary_response(t
, rr
);
1972 r
= dnssec_has_rrsig(t
->answer
, rr
->key
);
1978 r
= dns_answer_has_dname_for_cname(t
->answer
, rr
);
1984 name
= DNS_RESOURCE_KEY_NAME(rr
->key
);
1985 r
= dns_name_parent(&name
);
1991 soa
= dns_resource_key_new(rr
->key
->class, DNS_TYPE_SOA
, name
);
1995 log_debug("Requesting parent SOA to validate transaction %" PRIu16
" (%s, unsigned CNAME/DNAME/DS RRset).", t
->id
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1996 r
= dns_transaction_request_dnssec_rr(t
, soa
);
2004 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*soa
= NULL
;
2006 /* For other unsigned RRsets (including
2007 * NSEC/NSEC3!), look for proof the zone is
2008 * unsigned, by requesting the SOA RR of the
2009 * zone. However, do so only if they are
2010 * directly relevant to our original
2013 r
= dns_transaction_is_primary_response(t
, rr
);
2019 r
= dnssec_has_rrsig(t
->answer
, rr
->key
);
2025 soa
= dns_resource_key_new(rr
->key
->class, DNS_TYPE_SOA
, DNS_RESOURCE_KEY_NAME(rr
->key
));
2029 log_debug("Requesting SOA to validate transaction %" PRIu16
" (%s, unsigned non-SOA/NS RRset <%s>).", t
->id
, DNS_RESOURCE_KEY_NAME(rr
->key
), dns_resource_record_to_string(rr
));
2030 r
= dns_transaction_request_dnssec_rr(t
, soa
);
2037 /* Above, we requested everything necessary to validate what
2038 * we got. Now, let's request what we need to validate what we
2041 r
= dns_transaction_has_unsigned_negative_answer(t
);
2047 name
= DNS_RESOURCE_KEY_NAME(t
->key
);
2049 /* If this was a SOA or NS request, then this
2050 * indicates that we are not at a zone apex, hence ask
2051 * the parent name instead. If this was a DS request,
2052 * then it's signed when the parent zone is signed,
2053 * hence ask the parent in that case, too. */
2055 if (IN_SET(t
->key
->type
, DNS_TYPE_SOA
, DNS_TYPE_NS
, DNS_TYPE_DS
)) {
2056 r
= dns_name_parent(&name
);
2060 log_debug("Requesting parent SOA to validate transaction %" PRIu16
" (%s, unsigned empty SOA/NS/DS response).", t
->id
, DNS_RESOURCE_KEY_NAME(t
->key
));
2064 log_debug("Requesting SOA to validate transaction %" PRIu16
" (%s, unsigned empty non-SOA/NS/DS response).", t
->id
, DNS_RESOURCE_KEY_NAME(t
->key
));
2067 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*soa
= NULL
;
2069 soa
= dns_resource_key_new(t
->key
->class, DNS_TYPE_SOA
, name
);
2073 r
= dns_transaction_request_dnssec_rr(t
, soa
);
2079 return dns_transaction_dnssec_is_live(t
);
2082 void dns_transaction_notify(DnsTransaction
*t
, DnsTransaction
*source
) {
2086 /* Invoked whenever any of our auxiliary DNSSEC transactions completed its work. If the state is still PENDING,
2087 we are still in the loop that adds further DNSSEC transactions, hence don't check if we are ready yet. If
2088 the state is VALIDATING however, we should check if we are complete now. */
2090 if (t
->state
== DNS_TRANSACTION_VALIDATING
)
2091 dns_transaction_process_dnssec(t
);
2094 static int dns_transaction_validate_dnskey_by_ds(DnsTransaction
*t
) {
2095 DnsResourceRecord
*rr
;
2100 /* Add all DNSKEY RRs from the answer that are validated by DS
2101 * RRs from the list of validated keys to the list of
2102 * validated keys. */
2104 DNS_ANSWER_FOREACH_IFINDEX(rr
, ifindex
, t
->answer
) {
2106 r
= dnssec_verify_dnskey_by_ds_search(rr
, t
->validated_keys
);
2112 /* If so, the DNSKEY is validated too. */
2113 r
= dns_answer_add_extend(&t
->validated_keys
, rr
, ifindex
, DNS_ANSWER_AUTHENTICATED
);
2121 static int dns_transaction_requires_rrsig(DnsTransaction
*t
, DnsResourceRecord
*rr
) {
2127 /* Checks if the RR we are looking for must be signed with an
2128 * RRSIG. This is used for positive responses. */
2130 if (t
->scope
->dnssec_mode
== DNSSEC_NO
)
2133 if (dns_type_is_pseudo(rr
->key
->type
))
2136 r
= dns_transaction_negative_trust_anchor_lookup(t
, DNS_RESOURCE_KEY_NAME(rr
->key
));
2142 switch (rr
->key
->type
) {
2144 case DNS_TYPE_RRSIG
:
2145 /* RRSIGs are the signatures themselves, they need no signing. */
2153 /* For SOA or NS RRs we look for a matching DS transaction */
2155 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
2157 if (dt
->key
->class != rr
->key
->class)
2159 if (dt
->key
->type
!= DNS_TYPE_DS
)
2162 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), DNS_RESOURCE_KEY_NAME(rr
->key
));
2168 /* We found a DS transactions for the SOA/NS
2169 * RRs we are looking at. If it discovered signed DS
2170 * RRs, then we need to be signed, too. */
2172 if (!dt
->answer_authenticated
)
2175 return dns_answer_match_key(dt
->answer
, dt
->key
, NULL
);
2178 /* We found nothing that proves this is safe to leave
2179 * this unauthenticated, hence ask inist on
2180 * authentication. */
2185 case DNS_TYPE_CNAME
:
2186 case DNS_TYPE_DNAME
: {
2187 const char *parent
= NULL
;
2192 * CNAME/DNAME RRs cannot be located at a zone apex, hence look directly for the parent SOA.
2194 * DS RRs are signed if the parent is signed, hence also look at the parent SOA
2197 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
2199 if (dt
->key
->class != rr
->key
->class)
2201 if (dt
->key
->type
!= DNS_TYPE_SOA
)
2205 parent
= DNS_RESOURCE_KEY_NAME(rr
->key
);
2206 r
= dns_name_parent(&parent
);
2210 if (rr
->key
->type
== DNS_TYPE_DS
)
2213 /* A CNAME/DNAME without a parent? That's sooo weird. */
2214 log_debug("Transaction %" PRIu16
" claims CNAME/DNAME at root. Refusing.", t
->id
);
2219 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), parent
);
2225 return t
->answer_authenticated
;
2235 /* Any other kind of RR (including DNSKEY/NSEC/NSEC3). Let's see if our SOA lookup was authenticated */
2237 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
2239 if (dt
->key
->class != rr
->key
->class)
2241 if (dt
->key
->type
!= DNS_TYPE_SOA
)
2244 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), DNS_RESOURCE_KEY_NAME(rr
->key
));
2250 /* We found the transaction that was supposed to find
2251 * the SOA RR for us. It was successful, but found no
2252 * RR for us. This means we are not at a zone cut. In
2253 * this case, we require authentication if the SOA
2254 * lookup was authenticated too. */
2255 return t
->answer_authenticated
;
2262 static int dns_transaction_in_private_tld(DnsTransaction
*t
, const DnsResourceKey
*key
) {
2268 /* If DNSSEC downgrade mode is on, checks whether the
2269 * specified RR is one level below a TLD we have proven not to
2270 * exist. In such a case we assume that this is a private
2271 * domain, and permit it.
2273 * This detects cases like the Fritz!Box router networks. Each
2274 * Fritz!Box router serves a private "fritz.box" zone, in the
2275 * non-existing TLD "box". Requests for the "fritz.box" domain
2276 * are served by the router itself, while requests for the
2277 * "box" domain will result in NXDOMAIN.
2279 * Note that this logic is unable to detect cases where a
2280 * router serves a private DNS zone directly under
2281 * non-existing TLD. In such a case we cannot detect whether
2282 * the TLD is supposed to exist or not, as all requests we
2283 * make for it will be answered by the router's zone, and not
2284 * by the root zone. */
2288 if (t
->scope
->dnssec_mode
!= DNSSEC_ALLOW_DOWNGRADE
)
2289 return false; /* In strict DNSSEC mode what doesn't exist, doesn't exist */
2291 tld
= DNS_RESOURCE_KEY_NAME(key
);
2292 r
= dns_name_parent(&tld
);
2296 return false; /* Already the root domain */
2298 if (!dns_name_is_single_label(tld
))
2301 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
2303 if (dt
->key
->class != key
->class)
2306 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), tld
);
2312 /* We found an auxiliary lookup we did for the TLD. If
2313 * that returned with NXDOMAIN, we know the TLD didn't
2314 * exist, and hence this might be a private zone. */
2316 return dt
->answer_rcode
== DNS_RCODE_NXDOMAIN
;
2322 static int dns_transaction_requires_nsec(DnsTransaction
*t
) {
2330 /* Checks if we need to insist on NSEC/NSEC3 RRs for proving
2331 * this negative reply */
2333 if (t
->scope
->dnssec_mode
== DNSSEC_NO
)
2336 if (dns_type_is_pseudo(t
->key
->type
))
2339 r
= dns_transaction_negative_trust_anchor_lookup(t
, DNS_RESOURCE_KEY_NAME(t
->key
));
2345 r
= dns_transaction_in_private_tld(t
, t
->key
);
2349 /* The lookup is from a TLD that is proven not to
2350 * exist, and we are in downgrade mode, hence ignore
2351 * that fact that we didn't get any NSEC RRs.*/
2353 log_info("Detected a negative query %s in a private DNS zone, permitting unsigned response.", dns_transaction_key_string(t
));
2357 name
= DNS_RESOURCE_KEY_NAME(t
->key
);
2359 if (IN_SET(t
->key
->type
, DNS_TYPE_SOA
, DNS_TYPE_NS
, DNS_TYPE_DS
)) {
2361 /* We got a negative reply for this SOA/NS lookup? If
2362 * so, then we are not at a zone apex, and thus should
2363 * look at the result of the parent SOA lookup.
2365 * We got a negative reply for this DS lookup? DS RRs
2366 * are signed when their parent zone is signed, hence
2367 * also check the parent SOA in this case. */
2369 r
= dns_name_parent(&name
);
2376 /* For all other RRs we check the SOA on the same level to see
2377 * if it's signed. */
2379 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
2381 if (dt
->key
->class != t
->key
->class)
2383 if (dt
->key
->type
!= DNS_TYPE_SOA
)
2386 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), name
);
2392 return dt
->answer_authenticated
;
2395 /* If in doubt, require NSEC/NSEC3 */
2399 static int dns_transaction_dnskey_authenticated(DnsTransaction
*t
, DnsResourceRecord
*rr
) {
2400 DnsResourceRecord
*rrsig
;
2404 /* Checks whether any of the DNSKEYs used for the RRSIGs for
2405 * the specified RRset is authenticated (i.e. has a matching
2408 r
= dns_transaction_negative_trust_anchor_lookup(t
, DNS_RESOURCE_KEY_NAME(rr
->key
));
2414 DNS_ANSWER_FOREACH(rrsig
, t
->answer
) {
2418 r
= dnssec_key_match_rrsig(rr
->key
, rrsig
);
2424 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
2426 if (dt
->key
->class != rr
->key
->class)
2429 if (dt
->key
->type
== DNS_TYPE_DNSKEY
) {
2431 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), rrsig
->rrsig
.signer
);
2437 /* OK, we found an auxiliary DNSKEY
2438 * lookup. If that lookup is
2439 * authenticated, report this. */
2441 if (dt
->answer_authenticated
)
2446 } else if (dt
->key
->type
== DNS_TYPE_DS
) {
2448 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), rrsig
->rrsig
.signer
);
2454 /* OK, we found an auxiliary DS
2455 * lookup. If that lookup is
2456 * authenticated and non-zero, we
2459 if (!dt
->answer_authenticated
)
2462 return dns_answer_match_key(dt
->answer
, dt
->key
, NULL
);
2467 return found
? false : -ENXIO
;
2470 static int dns_transaction_known_signed(DnsTransaction
*t
, DnsResourceRecord
*rr
) {
2474 /* We know that the root domain is signed, hence if it appears
2475 * not to be signed, there's a problem with the DNS server */
2477 return rr
->key
->class == DNS_CLASS_IN
&&
2478 dns_name_is_root(DNS_RESOURCE_KEY_NAME(rr
->key
));
2481 static int dns_transaction_check_revoked_trust_anchors(DnsTransaction
*t
) {
2482 DnsResourceRecord
*rr
;
2487 /* Maybe warn the user that we encountered a revoked DNSKEY
2488 * for a key from our trust anchor. Note that we don't care
2489 * whether the DNSKEY can be authenticated or not. It's
2490 * sufficient if it is self-signed. */
2492 DNS_ANSWER_FOREACH(rr
, t
->answer
) {
2493 r
= dns_trust_anchor_check_revoked(&t
->scope
->manager
->trust_anchor
, rr
, t
->answer
);
2501 static int dns_transaction_invalidate_revoked_keys(DnsTransaction
*t
) {
2507 /* Removes all DNSKEY/DS objects from t->validated_keys that
2508 * our trust anchors database considers revoked. */
2511 DnsResourceRecord
*rr
;
2515 DNS_ANSWER_FOREACH(rr
, t
->validated_keys
) {
2516 r
= dns_trust_anchor_is_revoked(&t
->scope
->manager
->trust_anchor
, rr
);
2520 r
= dns_answer_remove_by_rr(&t
->validated_keys
, rr
);
2534 static int dns_transaction_copy_validated(DnsTransaction
*t
) {
2541 /* Copy all validated RRs from the auxiliary DNSSEC transactions into our set of validated RRs */
2543 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
2545 if (DNS_TRANSACTION_IS_LIVE(dt
->state
))
2548 if (!dt
->answer_authenticated
)
2551 r
= dns_answer_extend(&t
->validated_keys
, dt
->answer
);
2559 int dns_transaction_validate_dnssec(DnsTransaction
*t
) {
2560 _cleanup_(dns_answer_unrefp
) DnsAnswer
*validated
= NULL
;
2562 PHASE_DNSKEY
, /* Phase #1, only validate DNSKEYs */
2563 PHASE_NSEC
, /* Phase #2, only validate NSEC+NSEC3 */
2564 PHASE_ALL
, /* Phase #3, validate everything else */
2566 DnsResourceRecord
*rr
;
2567 DnsAnswerFlags flags
;
2572 /* We have now collected all DS and DNSKEY RRs in
2573 * t->validated_keys, let's see which RRs we can now
2574 * authenticate with that. */
2576 if (t
->scope
->dnssec_mode
== DNSSEC_NO
)
2579 /* Already validated */
2580 if (t
->answer_dnssec_result
!= _DNSSEC_RESULT_INVALID
)
2583 /* Our own stuff needs no validation */
2584 if (IN_SET(t
->answer_source
, DNS_TRANSACTION_ZONE
, DNS_TRANSACTION_TRUST_ANCHOR
)) {
2585 t
->answer_dnssec_result
= DNSSEC_VALIDATED
;
2586 t
->answer_authenticated
= true;
2590 /* Cached stuff is not affected by validation. */
2591 if (t
->answer_source
!= DNS_TRANSACTION_NETWORK
)
2594 if (!dns_transaction_dnssec_supported_full(t
)) {
2595 /* The server does not support DNSSEC, or doesn't augment responses with RRSIGs. */
2596 t
->answer_dnssec_result
= DNSSEC_INCOMPATIBLE_SERVER
;
2597 log_debug("Not validating response for %" PRIu16
", server lacks DNSSEC support.", t
->id
);
2601 log_debug("Validating response from transaction %" PRIu16
" (%s).", t
->id
, dns_transaction_key_string(t
));
2603 /* First, see if this response contains any revoked trust
2604 * anchors we care about */
2605 r
= dns_transaction_check_revoked_trust_anchors(t
);
2609 /* Third, copy all RRs we acquired successfully from auxiliary RRs over. */
2610 r
= dns_transaction_copy_validated(t
);
2614 /* Second, see if there are DNSKEYs we already know a
2615 * validated DS for. */
2616 r
= dns_transaction_validate_dnskey_by_ds(t
);
2620 /* Fourth, remove all DNSKEY and DS RRs again that our trust
2621 * anchor says are revoked. After all we might have marked
2622 * some keys revoked above, but they might still be lingering
2623 * in our validated_keys list. */
2624 r
= dns_transaction_invalidate_revoked_keys(t
);
2628 phase
= PHASE_DNSKEY
;
2630 bool changed
= false, have_nsec
= false;
2632 DNS_ANSWER_FOREACH(rr
, t
->answer
) {
2633 DnsResourceRecord
*rrsig
= NULL
;
2634 DnssecResult result
;
2636 switch (rr
->key
->type
) {
2638 case DNS_TYPE_RRSIG
:
2641 case DNS_TYPE_DNSKEY
:
2642 /* We validate DNSKEYs only in the DNSKEY and ALL phases */
2643 if (phase
== PHASE_NSEC
)
2648 case DNS_TYPE_NSEC3
:
2651 /* We validate NSEC/NSEC3 only in the NSEC and ALL phases */
2652 if (phase
== PHASE_DNSKEY
)
2658 /* We validate all other RRs only in the ALL phases */
2659 if (phase
!= PHASE_ALL
)
2665 r
= dnssec_verify_rrset_search(t
->answer
, rr
->key
, t
->validated_keys
, USEC_INFINITY
, &result
, &rrsig
);
2669 log_debug("Looking at %s: %s", strna(dns_resource_record_to_string(rr
)), dnssec_result_to_string(result
));
2671 if (result
== DNSSEC_VALIDATED
) {
2673 if (rr
->key
->type
== DNS_TYPE_DNSKEY
) {
2674 /* If we just validated a
2675 * DNSKEY RRset, then let's
2676 * add these keys to the set
2677 * of validated keys for this
2680 r
= dns_answer_copy_by_key(&t
->validated_keys
, t
->answer
, rr
->key
, DNS_ANSWER_AUTHENTICATED
);
2684 /* some of the DNSKEYs we just
2685 * added might already have
2686 * been revoked, remove them
2687 * again in that case. */
2688 r
= dns_transaction_invalidate_revoked_keys(t
);
2693 /* Add the validated RRset to the new
2694 * list of validated RRsets, and
2695 * remove it from the unvalidated
2696 * RRsets. We mark the RRset as
2697 * authenticated and cacheable. */
2698 r
= dns_answer_move_by_key(&validated
, &t
->answer
, rr
->key
, DNS_ANSWER_AUTHENTICATED
|DNS_ANSWER_CACHEABLE
);
2702 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_SECURE
, rr
->key
);
2704 /* Exit the loop, we dropped something from the answer, start from the beginning */
2709 /* If we haven't read all DNSKEYs yet a negative result of the validation is irrelevant, as
2710 * there might be more DNSKEYs coming. Similar, if we haven't read all NSEC/NSEC3 RRs yet, we
2711 * cannot do positive wildcard proofs yet, as those require the NSEC/NSEC3 RRs. */
2712 if (phase
!= PHASE_ALL
)
2715 if (result
== DNSSEC_VALIDATED_WILDCARD
) {
2716 bool authenticated
= false;
2719 /* This RRset validated, but as a wildcard. This means we need to prove via NSEC/NSEC3
2720 * that no matching non-wildcard RR exists.*/
2722 /* First step, determine the source of synthesis */
2723 r
= dns_resource_record_source(rrsig
, &source
);
2727 r
= dnssec_test_positive_wildcard(
2729 DNS_RESOURCE_KEY_NAME(rr
->key
),
2731 rrsig
->rrsig
.signer
,
2734 /* Unless the NSEC proof showed that the key really doesn't exist something is off. */
2736 result
= DNSSEC_INVALID
;
2738 r
= dns_answer_move_by_key(&validated
, &t
->answer
, rr
->key
, authenticated
? (DNS_ANSWER_AUTHENTICATED
|DNS_ANSWER_CACHEABLE
) : 0);
2742 manager_dnssec_verdict(t
->scope
->manager
, authenticated
? DNSSEC_SECURE
: DNSSEC_INSECURE
, rr
->key
);
2744 /* Exit the loop, we dropped something from the answer, start from the beginning */
2750 if (result
== DNSSEC_NO_SIGNATURE
) {
2751 r
= dns_transaction_requires_rrsig(t
, rr
);
2755 /* Data does not require signing. In that case, just copy it over,
2756 * but remember that this is by no means authenticated.*/
2757 r
= dns_answer_move_by_key(&validated
, &t
->answer
, rr
->key
, 0);
2761 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INSECURE
, rr
->key
);
2766 r
= dns_transaction_known_signed(t
, rr
);
2770 /* This is an RR we know has to be signed. If it isn't this means
2771 * the server is not attaching RRSIGs, hence complain. */
2773 dns_server_packet_rrsig_missing(t
->server
, t
->current_feature_level
);
2775 if (t
->scope
->dnssec_mode
== DNSSEC_ALLOW_DOWNGRADE
) {
2777 /* Downgrading is OK? If so, just consider the information unsigned */
2779 r
= dns_answer_move_by_key(&validated
, &t
->answer
, rr
->key
, 0);
2783 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INSECURE
, rr
->key
);
2788 /* Otherwise, fail */
2789 t
->answer_dnssec_result
= DNSSEC_INCOMPATIBLE_SERVER
;
2793 r
= dns_transaction_in_private_tld(t
, rr
->key
);
2797 _cleanup_free_
char *s
= NULL
;
2799 /* The data is from a TLD that is proven not to exist, and we are in downgrade
2800 * mode, hence ignore the fact that this was not signed. */
2802 (void) dns_resource_key_to_string(rr
->key
, &s
);
2803 log_info("Detected RRset %s is in a private DNS zone, permitting unsigned RRs.", strna(s
? strstrip(s
) : NULL
));
2805 r
= dns_answer_move_by_key(&validated
, &t
->answer
, rr
->key
, 0);
2809 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INSECURE
, rr
->key
);
2817 DNSSEC_SIGNATURE_EXPIRED
,
2818 DNSSEC_UNSUPPORTED_ALGORITHM
)) {
2820 r
= dns_transaction_dnskey_authenticated(t
, rr
);
2821 if (r
< 0 && r
!= -ENXIO
)
2824 /* The DNSKEY transaction was not authenticated, this means there's
2825 * no DS for this, which means it's OK if no keys are found for this signature. */
2827 r
= dns_answer_move_by_key(&validated
, &t
->answer
, rr
->key
, 0);
2831 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INSECURE
, rr
->key
);
2837 r
= dns_transaction_is_primary_response(t
, rr
);
2842 /* Look for a matching DNAME for this CNAME */
2843 r
= dns_answer_has_dname_for_cname(t
->answer
, rr
);
2847 /* Also look among the stuff we already validated */
2848 r
= dns_answer_has_dname_for_cname(validated
, rr
);
2856 DNSSEC_SIGNATURE_EXPIRED
,
2857 DNSSEC_NO_SIGNATURE
))
2858 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_BOGUS
, rr
->key
);
2859 else /* DNSSEC_MISSING_KEY or DNSSEC_UNSUPPORTED_ALGORITHM */
2860 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INDETERMINATE
, rr
->key
);
2862 /* This is a primary response to our question, and it failed validation. That's
2864 t
->answer_dnssec_result
= result
;
2868 /* This is a primary response, but we do have a DNAME RR in the RR that can replay this
2869 * CNAME, hence rely on that, and we can remove the CNAME in favour of it. */
2872 /* This is just some auxiliary data. Just remove the RRset and continue. */
2873 r
= dns_answer_remove_by_key(&t
->answer
, rr
->key
);
2877 /* Exit the loop, we dropped something from the answer, start from the beginning */
2882 /* Restart the inner loop as long as we managed to achieve something */
2886 if (phase
== PHASE_DNSKEY
&& have_nsec
) {
2887 /* OK, we processed all DNSKEYs, and there are NSEC/NSEC3 RRs, look at those now. */
2892 if (phase
!= PHASE_ALL
) {
2893 /* OK, we processed all DNSKEYs and NSEC/NSEC3 RRs, look at all the rest now. Note that in this
2894 * third phase we start to remove RRs we couldn't validate. */
2903 dns_answer_unref(t
->answer
);
2904 t
->answer
= validated
;
2907 /* At this point the answer only contains validated
2908 * RRsets. Now, let's see if it actually answers the question
2909 * we asked. If so, great! If it doesn't, then see if
2910 * NSEC/NSEC3 can prove this. */
2911 r
= dns_transaction_has_positive_answer(t
, &flags
);
2913 /* Yes, it answers the question! */
2915 if (flags
& DNS_ANSWER_AUTHENTICATED
) {
2916 /* The answer is fully authenticated, yay. */
2917 t
->answer_dnssec_result
= DNSSEC_VALIDATED
;
2918 t
->answer_rcode
= DNS_RCODE_SUCCESS
;
2919 t
->answer_authenticated
= true;
2921 /* The answer is not fully authenticated. */
2922 t
->answer_dnssec_result
= DNSSEC_UNSIGNED
;
2923 t
->answer_authenticated
= false;
2926 } else if (r
== 0) {
2927 DnssecNsecResult nr
;
2928 bool authenticated
= false;
2930 /* Bummer! Let's check NSEC/NSEC3 */
2931 r
= dnssec_nsec_test(t
->answer
, t
->key
, &nr
, &authenticated
, &t
->answer_nsec_ttl
);
2937 case DNSSEC_NSEC_NXDOMAIN
:
2938 /* NSEC proves the domain doesn't exist. Very good. */
2939 log_debug("Proved NXDOMAIN via NSEC/NSEC3 for transaction %u (%s)", t
->id
, dns_transaction_key_string(t
));
2940 t
->answer_dnssec_result
= DNSSEC_VALIDATED
;
2941 t
->answer_rcode
= DNS_RCODE_NXDOMAIN
;
2942 t
->answer_authenticated
= authenticated
;
2944 manager_dnssec_verdict(t
->scope
->manager
, authenticated
? DNSSEC_SECURE
: DNSSEC_INSECURE
, t
->key
);
2947 case DNSSEC_NSEC_NODATA
:
2948 /* NSEC proves that there's no data here, very good. */
2949 log_debug("Proved NODATA via NSEC/NSEC3 for transaction %u (%s)", t
->id
, dns_transaction_key_string(t
));
2950 t
->answer_dnssec_result
= DNSSEC_VALIDATED
;
2951 t
->answer_rcode
= DNS_RCODE_SUCCESS
;
2952 t
->answer_authenticated
= authenticated
;
2954 manager_dnssec_verdict(t
->scope
->manager
, authenticated
? DNSSEC_SECURE
: DNSSEC_INSECURE
, t
->key
);
2957 case DNSSEC_NSEC_OPTOUT
:
2958 /* NSEC3 says the data might not be signed */
2959 log_debug("Data is NSEC3 opt-out via NSEC/NSEC3 for transaction %u (%s)", t
->id
, dns_transaction_key_string(t
));
2960 t
->answer_dnssec_result
= DNSSEC_UNSIGNED
;
2961 t
->answer_authenticated
= false;
2963 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INSECURE
, t
->key
);
2966 case DNSSEC_NSEC_NO_RR
:
2967 /* No NSEC data? Bummer! */
2969 r
= dns_transaction_requires_nsec(t
);
2973 t
->answer_dnssec_result
= DNSSEC_NO_SIGNATURE
;
2974 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_BOGUS
, t
->key
);
2976 t
->answer_dnssec_result
= DNSSEC_UNSIGNED
;
2977 t
->answer_authenticated
= false;
2978 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INSECURE
, t
->key
);
2983 case DNSSEC_NSEC_UNSUPPORTED_ALGORITHM
:
2984 /* We don't know the NSEC3 algorithm used? */
2985 t
->answer_dnssec_result
= DNSSEC_UNSUPPORTED_ALGORITHM
;
2986 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INDETERMINATE
, t
->key
);
2989 case DNSSEC_NSEC_FOUND
:
2990 case DNSSEC_NSEC_CNAME
:
2991 /* NSEC says it needs to be there, but we couldn't find it? Bummer! */
2992 t
->answer_dnssec_result
= DNSSEC_NSEC_MISMATCH
;
2993 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_BOGUS
, t
->key
);
2997 assert_not_reached("Unexpected NSEC result.");
3004 const char *dns_transaction_key_string(DnsTransaction
*t
) {
3007 if (!t
->key_string
) {
3008 if (dns_resource_key_to_string(t
->key
, &t
->key_string
) < 0)
3012 return strstrip(t
->key_string
);
3015 static const char* const dns_transaction_state_table
[_DNS_TRANSACTION_STATE_MAX
] = {
3016 [DNS_TRANSACTION_NULL
] = "null",
3017 [DNS_TRANSACTION_PENDING
] = "pending",
3018 [DNS_TRANSACTION_VALIDATING
] = "validating",
3019 [DNS_TRANSACTION_RCODE_FAILURE
] = "rcode-failure",
3020 [DNS_TRANSACTION_SUCCESS
] = "success",
3021 [DNS_TRANSACTION_NO_SERVERS
] = "no-servers",
3022 [DNS_TRANSACTION_TIMEOUT
] = "timeout",
3023 [DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
] = "attempts-max-reached",
3024 [DNS_TRANSACTION_INVALID_REPLY
] = "invalid-reply",
3025 [DNS_TRANSACTION_ERRNO
] = "errno",
3026 [DNS_TRANSACTION_ABORTED
] = "aborted",
3027 [DNS_TRANSACTION_DNSSEC_FAILED
] = "dnssec-failed",
3028 [DNS_TRANSACTION_NO_TRUST_ANCHOR
] = "no-trust-anchor",
3029 [DNS_TRANSACTION_RR_TYPE_UNSUPPORTED
] = "rr-type-unsupported",
3030 [DNS_TRANSACTION_NETWORK_DOWN
] = "network-down",
3031 [DNS_TRANSACTION_NOT_FOUND
] = "not-found",
3033 DEFINE_STRING_TABLE_LOOKUP(dns_transaction_state
, DnsTransactionState
);
3035 static const char* const dns_transaction_source_table
[_DNS_TRANSACTION_SOURCE_MAX
] = {
3036 [DNS_TRANSACTION_NETWORK
] = "network",
3037 [DNS_TRANSACTION_CACHE
] = "cache",
3038 [DNS_TRANSACTION_ZONE
] = "zone",
3039 [DNS_TRANSACTION_TRUST_ANCHOR
] = "trust-anchor",
3041 DEFINE_STRING_TABLE_LOOKUP(dns_transaction_source
, DnsTransactionSource
);