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_packet_append_key(p
, t
->key
, NULL
);
1418 DNS_PACKET_HEADER(p
)->qdcount
= htobe16(1);
1419 DNS_PACKET_HEADER(p
)->id
= t
->id
;
1427 int dns_transaction_go(DnsTransaction
*t
) {
1433 assert_se(sd_event_now(t
->scope
->manager
->event
, clock_boottime_or_monotonic(), &ts
) >= 0);
1435 r
= dns_transaction_prepare(t
, ts
);
1439 log_debug("Excercising transaction %" PRIu16
" for <%s> on scope %s on %s/%s.",
1441 dns_transaction_key_string(t
),
1442 dns_protocol_to_string(t
->scope
->protocol
),
1443 t
->scope
->link
? t
->scope
->link
->name
: "*",
1444 t
->scope
->family
== AF_UNSPEC
? "*" : af_to_name(t
->scope
->family
));
1446 if (!t
->initial_jitter_scheduled
&&
1447 (t
->scope
->protocol
== DNS_PROTOCOL_LLMNR
||
1448 t
->scope
->protocol
== DNS_PROTOCOL_MDNS
)) {
1449 usec_t jitter
, accuracy
;
1451 /* RFC 4795 Section 2.7 suggests all queries should be
1452 * delayed by a random time from 0 to JITTER_INTERVAL. */
1454 t
->initial_jitter_scheduled
= true;
1456 random_bytes(&jitter
, sizeof(jitter
));
1458 switch (t
->scope
->protocol
) {
1460 case DNS_PROTOCOL_LLMNR
:
1461 jitter
%= LLMNR_JITTER_INTERVAL_USEC
;
1462 accuracy
= LLMNR_JITTER_INTERVAL_USEC
;
1465 case DNS_PROTOCOL_MDNS
:
1466 jitter
%= MDNS_JITTER_RANGE_USEC
;
1467 jitter
+= MDNS_JITTER_MIN_USEC
;
1468 accuracy
= MDNS_JITTER_RANGE_USEC
;
1471 assert_not_reached("bad protocol");
1474 r
= sd_event_add_time(
1475 t
->scope
->manager
->event
,
1476 &t
->timeout_event_source
,
1477 clock_boottime_or_monotonic(),
1478 ts
+ jitter
, accuracy
,
1479 on_transaction_timeout
, t
);
1483 (void) sd_event_source_set_description(t
->timeout_event_source
, "dns-transaction-timeout");
1486 t
->next_attempt_after
= ts
;
1487 t
->state
= DNS_TRANSACTION_PENDING
;
1489 log_debug("Delaying %s transaction for " USEC_FMT
"us.", dns_protocol_to_string(t
->scope
->protocol
), jitter
);
1493 /* Otherwise, we need to ask the network */
1494 r
= dns_transaction_make_packet(t
);
1498 if (t
->scope
->protocol
== DNS_PROTOCOL_LLMNR
&&
1499 (dns_name_endswith(DNS_RESOURCE_KEY_NAME(t
->key
), "in-addr.arpa") > 0 ||
1500 dns_name_endswith(DNS_RESOURCE_KEY_NAME(t
->key
), "ip6.arpa") > 0)) {
1502 /* RFC 4795, Section 2.4. says reverse lookups shall
1503 * always be made via TCP on LLMNR */
1504 r
= dns_transaction_open_tcp(t
);
1506 /* Try via UDP, and if that fails due to large size or lack of
1507 * support try via TCP */
1508 r
= dns_transaction_emit_udp(t
);
1510 log_debug("Sending query via TCP since it is too large.");
1512 log_debug("Sending query via TCP since server doesn't support UDP.");
1513 if (r
== -EMSGSIZE
|| r
== -EAGAIN
)
1514 r
= dns_transaction_open_tcp(t
);
1518 /* No servers to send this to? */
1519 dns_transaction_complete(t
, DNS_TRANSACTION_NO_SERVERS
);
1522 if (r
== -EOPNOTSUPP
) {
1523 /* Tried to ask for DNSSEC RRs, on a server that doesn't do DNSSEC */
1524 dns_transaction_complete(t
, DNS_TRANSACTION_RR_TYPE_UNSUPPORTED
);
1527 if (t
->scope
->protocol
== DNS_PROTOCOL_LLMNR
&& ERRNO_IS_DISCONNECT(-r
)) {
1528 /* On LLMNR, if we cannot connect to a host via TCP when doing reverse lookups. This means we cannot
1529 * answer this request with this protocol. */
1530 dns_transaction_complete(t
, DNS_TRANSACTION_NOT_FOUND
);
1534 if (t
->scope
->protocol
!= DNS_PROTOCOL_DNS
)
1537 /* Couldn't send? Try immediately again, with a new server */
1538 dns_scope_next_dns_server(t
->scope
);
1540 return dns_transaction_go(t
);
1543 ts
+= transaction_get_resend_timeout(t
);
1545 r
= sd_event_add_time(
1546 t
->scope
->manager
->event
,
1547 &t
->timeout_event_source
,
1548 clock_boottime_or_monotonic(),
1550 on_transaction_timeout
, t
);
1554 (void) sd_event_source_set_description(t
->timeout_event_source
, "dns-transaction-timeout");
1556 t
->state
= DNS_TRANSACTION_PENDING
;
1557 t
->next_attempt_after
= ts
;
1562 static int dns_transaction_find_cyclic(DnsTransaction
*t
, DnsTransaction
*aux
) {
1570 /* Try to find cyclic dependencies between transaction objects */
1575 SET_FOREACH(n
, aux
->dnssec_transactions
, i
) {
1576 r
= dns_transaction_find_cyclic(t
, n
);
1584 static int dns_transaction_add_dnssec_transaction(DnsTransaction
*t
, DnsResourceKey
*key
, DnsTransaction
**ret
) {
1585 DnsTransaction
*aux
;
1592 aux
= dns_scope_find_transaction(t
->scope
, key
, true);
1594 r
= dns_transaction_new(&aux
, t
->scope
, key
);
1598 if (set_contains(t
->dnssec_transactions
, aux
)) {
1603 r
= dns_transaction_find_cyclic(t
, aux
);
1607 log_debug("Detected potential cyclic dependency, refusing to add transaction %" PRIu16
" (%s) as dependency for %" PRIu16
" (%s).",
1609 strna(dns_transaction_key_string(aux
)),
1611 strna(dns_transaction_key_string(t
)));
1616 r
= set_ensure_allocated(&t
->dnssec_transactions
, NULL
);
1620 r
= set_ensure_allocated(&aux
->notify_transactions
, NULL
);
1624 r
= set_put(t
->dnssec_transactions
, aux
);
1628 r
= set_put(aux
->notify_transactions
, t
);
1630 (void) set_remove(t
->dnssec_transactions
, aux
);
1638 dns_transaction_gc(aux
);
1642 static int dns_transaction_request_dnssec_rr(DnsTransaction
*t
, DnsResourceKey
*key
) {
1643 _cleanup_(dns_answer_unrefp
) DnsAnswer
*a
= NULL
;
1644 DnsTransaction
*aux
;
1650 /* Try to get the data from the trust anchor */
1651 r
= dns_trust_anchor_lookup_positive(&t
->scope
->manager
->trust_anchor
, key
, &a
);
1655 r
= dns_answer_extend(&t
->validated_keys
, a
);
1662 /* This didn't work, ask for it via the network/cache then. */
1663 r
= dns_transaction_add_dnssec_transaction(t
, key
, &aux
);
1664 if (r
== -ELOOP
) /* This would result in a cyclic dependency */
1669 if (aux
->state
== DNS_TRANSACTION_NULL
) {
1670 r
= dns_transaction_go(aux
);
1678 static int dns_transaction_negative_trust_anchor_lookup(DnsTransaction
*t
, const char *name
) {
1683 /* Check whether the specified name is in the the NTA
1684 * database, either in the global one, or the link-local
1687 r
= dns_trust_anchor_lookup_negative(&t
->scope
->manager
->trust_anchor
, name
);
1691 if (!t
->scope
->link
)
1694 return set_contains(t
->scope
->link
->dnssec_negative_trust_anchors
, name
);
1697 static int dns_transaction_has_unsigned_negative_answer(DnsTransaction
*t
) {
1702 /* Checks whether the answer is negative, and lacks NSEC/NSEC3
1703 * RRs to prove it */
1705 r
= dns_transaction_has_positive_answer(t
, NULL
);
1711 /* Is this key explicitly listed as a negative trust anchor?
1712 * If so, it's nothing we need to care about */
1713 r
= dns_transaction_negative_trust_anchor_lookup(t
, DNS_RESOURCE_KEY_NAME(t
->key
));
1719 /* The answer does not contain any RRs that match to the
1720 * question. If so, let's see if there are any NSEC/NSEC3 RRs
1721 * included. If not, the answer is unsigned. */
1723 r
= dns_answer_contains_nsec_or_nsec3(t
->answer
);
1732 static int dns_transaction_is_primary_response(DnsTransaction
*t
, DnsResourceRecord
*rr
) {
1738 /* Check if the specified RR is the "primary" response,
1739 * i.e. either matches the question precisely or is a
1740 * CNAME/DNAME for it. */
1742 r
= dns_resource_key_match_rr(t
->key
, rr
, NULL
);
1746 return dns_resource_key_match_cname_or_dname(t
->key
, rr
->key
, NULL
);
1749 static bool dns_transaction_dnssec_supported(DnsTransaction
*t
) {
1752 /* Checks whether our transaction's DNS server is assumed to be compatible with DNSSEC. Returns false as soon
1753 * as we changed our mind about a server, and now believe it is incompatible with DNSSEC. */
1755 if (t
->scope
->protocol
!= DNS_PROTOCOL_DNS
)
1758 /* If we have picked no server, then we are working from the cache or some other source, and DNSSEC might well
1759 * be supported, hence return true. */
1763 if (t
->current_feature_level
< DNS_SERVER_FEATURE_LEVEL_DO
)
1766 return dns_server_dnssec_supported(t
->server
);
1769 static bool dns_transaction_dnssec_supported_full(DnsTransaction
*t
) {
1775 /* Checks whether our transaction our any of the auxiliary transactions couldn't do DNSSEC. */
1777 if (!dns_transaction_dnssec_supported(t
))
1780 SET_FOREACH(dt
, t
->dnssec_transactions
, i
)
1781 if (!dns_transaction_dnssec_supported(dt
))
1787 int dns_transaction_request_dnssec_keys(DnsTransaction
*t
) {
1788 DnsResourceRecord
*rr
;
1795 * Retrieve all auxiliary RRs for the answer we got, so that
1796 * we can verify signatures or prove that RRs are rightfully
1797 * unsigned. Specifically:
1799 * - For RRSIG we get the matching DNSKEY
1800 * - For DNSKEY we get the matching DS
1801 * - For unsigned SOA/NS we get the matching DS
1802 * - For unsigned CNAME/DNAME/DS we get the parent SOA RR
1803 * - For other unsigned RRs we get the matching SOA RR
1804 * - For SOA/NS/DS queries with no matching response RRs, and no NSEC/NSEC3, the parent's SOA RR
1805 * - For other queries with no matching response RRs, and no NSEC/NSEC3, the SOA RR
1808 if (t
->scope
->dnssec_mode
== DNSSEC_NO
)
1810 if (t
->answer_source
!= DNS_TRANSACTION_NETWORK
)
1811 return 0; /* We only need to validate stuff from the network */
1812 if (!dns_transaction_dnssec_supported(t
))
1813 return 0; /* If we can't do DNSSEC anyway there's no point in geting the auxiliary RRs */
1815 DNS_ANSWER_FOREACH(rr
, t
->answer
) {
1817 if (dns_type_is_pseudo(rr
->key
->type
))
1820 /* If this RR is in the negative trust anchor, we don't need to validate it. */
1821 r
= dns_transaction_negative_trust_anchor_lookup(t
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1827 switch (rr
->key
->type
) {
1829 case DNS_TYPE_RRSIG
: {
1830 /* For each RRSIG we request the matching DNSKEY */
1831 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*dnskey
= NULL
;
1833 /* If this RRSIG is about a DNSKEY RR and the
1834 * signer is the same as the owner, then we
1835 * already have the DNSKEY, and we don't have
1836 * to look for more. */
1837 if (rr
->rrsig
.type_covered
== DNS_TYPE_DNSKEY
) {
1838 r
= dns_name_equal(rr
->rrsig
.signer
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1845 /* If the signer is not a parent of our
1846 * original query, then this is about an
1847 * auxiliary RRset, but not anything we asked
1848 * for. In this case we aren't interested,
1849 * because we don't want to request additional
1850 * RRs for stuff we didn't really ask for, and
1851 * also to avoid request loops, where
1852 * additional RRs from one transaction result
1853 * in another transaction whose additonal RRs
1854 * point back to the original transaction, and
1856 r
= dns_name_endswith(DNS_RESOURCE_KEY_NAME(t
->key
), rr
->rrsig
.signer
);
1862 dnskey
= dns_resource_key_new(rr
->key
->class, DNS_TYPE_DNSKEY
, rr
->rrsig
.signer
);
1866 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
);
1867 r
= dns_transaction_request_dnssec_rr(t
, dnskey
);
1873 case DNS_TYPE_DNSKEY
: {
1874 /* For each DNSKEY we request the matching DS */
1875 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*ds
= NULL
;
1877 /* If the DNSKEY we are looking at is not for
1878 * zone we are interested in, nor any of its
1879 * parents, we aren't interested, and don't
1880 * request it. After all, we don't want to end
1881 * up in request loops, and want to keep
1882 * additional traffic down. */
1884 r
= dns_name_endswith(DNS_RESOURCE_KEY_NAME(t
->key
), DNS_RESOURCE_KEY_NAME(rr
->key
));
1890 ds
= dns_resource_key_new(rr
->key
->class, DNS_TYPE_DS
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1894 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));
1895 r
= dns_transaction_request_dnssec_rr(t
, ds
);
1904 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*ds
= NULL
;
1906 /* For an unsigned SOA or NS, try to acquire
1907 * the matching DS RR, as we are at a zone cut
1908 * then, and whether a DS exists tells us
1909 * whether the zone is signed. Do so only if
1910 * this RR matches our original question,
1913 r
= dns_resource_key_match_rr(t
->key
, rr
, NULL
);
1919 r
= dnssec_has_rrsig(t
->answer
, rr
->key
);
1925 ds
= dns_resource_key_new(rr
->key
->class, DNS_TYPE_DS
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1929 log_debug("Requesting DS to validate transaction %" PRIu16
" (%s, unsigned SOA/NS RRset).", t
->id
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1930 r
= dns_transaction_request_dnssec_rr(t
, ds
);
1938 case DNS_TYPE_CNAME
:
1939 case DNS_TYPE_DNAME
: {
1940 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*soa
= NULL
;
1943 /* CNAMEs and DNAMEs cannot be located at a
1944 * zone apex, hence ask for the parent SOA for
1945 * unsigned CNAME/DNAME RRs, maybe that's the
1946 * apex. But do all that only if this is
1947 * actually a response to our original
1950 * Similar for DS RRs, which are signed when
1951 * the parent SOA is signed. */
1953 r
= dns_transaction_is_primary_response(t
, rr
);
1959 r
= dnssec_has_rrsig(t
->answer
, rr
->key
);
1965 r
= dns_answer_has_dname_for_cname(t
->answer
, rr
);
1971 name
= DNS_RESOURCE_KEY_NAME(rr
->key
);
1972 r
= dns_name_parent(&name
);
1978 soa
= dns_resource_key_new(rr
->key
->class, DNS_TYPE_SOA
, name
);
1982 log_debug("Requesting parent SOA to validate transaction %" PRIu16
" (%s, unsigned CNAME/DNAME/DS RRset).", t
->id
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1983 r
= dns_transaction_request_dnssec_rr(t
, soa
);
1991 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*soa
= NULL
;
1993 /* For other unsigned RRsets (including
1994 * NSEC/NSEC3!), look for proof the zone is
1995 * unsigned, by requesting the SOA RR of the
1996 * zone. However, do so only if they are
1997 * directly relevant to our original
2000 r
= dns_transaction_is_primary_response(t
, rr
);
2006 r
= dnssec_has_rrsig(t
->answer
, rr
->key
);
2012 soa
= dns_resource_key_new(rr
->key
->class, DNS_TYPE_SOA
, DNS_RESOURCE_KEY_NAME(rr
->key
));
2016 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
));
2017 r
= dns_transaction_request_dnssec_rr(t
, soa
);
2024 /* Above, we requested everything necessary to validate what
2025 * we got. Now, let's request what we need to validate what we
2028 r
= dns_transaction_has_unsigned_negative_answer(t
);
2034 name
= DNS_RESOURCE_KEY_NAME(t
->key
);
2036 /* If this was a SOA or NS request, then this
2037 * indicates that we are not at a zone apex, hence ask
2038 * the parent name instead. If this was a DS request,
2039 * then it's signed when the parent zone is signed,
2040 * hence ask the parent in that case, too. */
2042 if (IN_SET(t
->key
->type
, DNS_TYPE_SOA
, DNS_TYPE_NS
, DNS_TYPE_DS
)) {
2043 r
= dns_name_parent(&name
);
2047 log_debug("Requesting parent SOA to validate transaction %" PRIu16
" (%s, unsigned empty SOA/NS/DS response).", t
->id
, DNS_RESOURCE_KEY_NAME(t
->key
));
2051 log_debug("Requesting SOA to validate transaction %" PRIu16
" (%s, unsigned empty non-SOA/NS/DS response).", t
->id
, DNS_RESOURCE_KEY_NAME(t
->key
));
2054 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*soa
= NULL
;
2056 soa
= dns_resource_key_new(t
->key
->class, DNS_TYPE_SOA
, name
);
2060 r
= dns_transaction_request_dnssec_rr(t
, soa
);
2066 return dns_transaction_dnssec_is_live(t
);
2069 void dns_transaction_notify(DnsTransaction
*t
, DnsTransaction
*source
) {
2073 /* Invoked whenever any of our auxiliary DNSSEC transactions completed its work. If the state is still PENDING,
2074 we are still in the loop that adds further DNSSEC transactions, hence don't check if we are ready yet. If
2075 the state is VALIDATING however, we should check if we are complete now. */
2077 if (t
->state
== DNS_TRANSACTION_VALIDATING
)
2078 dns_transaction_process_dnssec(t
);
2081 static int dns_transaction_validate_dnskey_by_ds(DnsTransaction
*t
) {
2082 DnsResourceRecord
*rr
;
2087 /* Add all DNSKEY RRs from the answer that are validated by DS
2088 * RRs from the list of validated keys to the list of
2089 * validated keys. */
2091 DNS_ANSWER_FOREACH_IFINDEX(rr
, ifindex
, t
->answer
) {
2093 r
= dnssec_verify_dnskey_by_ds_search(rr
, t
->validated_keys
);
2099 /* If so, the DNSKEY is validated too. */
2100 r
= dns_answer_add_extend(&t
->validated_keys
, rr
, ifindex
, DNS_ANSWER_AUTHENTICATED
);
2108 static int dns_transaction_requires_rrsig(DnsTransaction
*t
, DnsResourceRecord
*rr
) {
2114 /* Checks if the RR we are looking for must be signed with an
2115 * RRSIG. This is used for positive responses. */
2117 if (t
->scope
->dnssec_mode
== DNSSEC_NO
)
2120 if (dns_type_is_pseudo(rr
->key
->type
))
2123 r
= dns_transaction_negative_trust_anchor_lookup(t
, DNS_RESOURCE_KEY_NAME(rr
->key
));
2129 switch (rr
->key
->type
) {
2131 case DNS_TYPE_RRSIG
:
2132 /* RRSIGs are the signatures themselves, they need no signing. */
2140 /* For SOA or NS RRs we look for a matching DS transaction */
2142 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
2144 if (dt
->key
->class != rr
->key
->class)
2146 if (dt
->key
->type
!= DNS_TYPE_DS
)
2149 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), DNS_RESOURCE_KEY_NAME(rr
->key
));
2155 /* We found a DS transactions for the SOA/NS
2156 * RRs we are looking at. If it discovered signed DS
2157 * RRs, then we need to be signed, too. */
2159 if (!dt
->answer_authenticated
)
2162 return dns_answer_match_key(dt
->answer
, dt
->key
, NULL
);
2165 /* We found nothing that proves this is safe to leave
2166 * this unauthenticated, hence ask inist on
2167 * authentication. */
2172 case DNS_TYPE_CNAME
:
2173 case DNS_TYPE_DNAME
: {
2174 const char *parent
= NULL
;
2179 * CNAME/DNAME RRs cannot be located at a zone apex, hence look directly for the parent SOA.
2181 * DS RRs are signed if the parent is signed, hence also look at the parent SOA
2184 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
2186 if (dt
->key
->class != rr
->key
->class)
2188 if (dt
->key
->type
!= DNS_TYPE_SOA
)
2192 parent
= DNS_RESOURCE_KEY_NAME(rr
->key
);
2193 r
= dns_name_parent(&parent
);
2197 if (rr
->key
->type
== DNS_TYPE_DS
)
2200 /* A CNAME/DNAME without a parent? That's sooo weird. */
2201 log_debug("Transaction %" PRIu16
" claims CNAME/DNAME at root. Refusing.", t
->id
);
2206 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), parent
);
2212 return t
->answer_authenticated
;
2222 /* Any other kind of RR (including DNSKEY/NSEC/NSEC3). Let's see if our SOA lookup was authenticated */
2224 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
2226 if (dt
->key
->class != rr
->key
->class)
2228 if (dt
->key
->type
!= DNS_TYPE_SOA
)
2231 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), DNS_RESOURCE_KEY_NAME(rr
->key
));
2237 /* We found the transaction that was supposed to find
2238 * the SOA RR for us. It was successful, but found no
2239 * RR for us. This means we are not at a zone cut. In
2240 * this case, we require authentication if the SOA
2241 * lookup was authenticated too. */
2242 return t
->answer_authenticated
;
2249 static int dns_transaction_in_private_tld(DnsTransaction
*t
, const DnsResourceKey
*key
) {
2255 /* If DNSSEC downgrade mode is on, checks whether the
2256 * specified RR is one level below a TLD we have proven not to
2257 * exist. In such a case we assume that this is a private
2258 * domain, and permit it.
2260 * This detects cases like the Fritz!Box router networks. Each
2261 * Fritz!Box router serves a private "fritz.box" zone, in the
2262 * non-existing TLD "box". Requests for the "fritz.box" domain
2263 * are served by the router itself, while requests for the
2264 * "box" domain will result in NXDOMAIN.
2266 * Note that this logic is unable to detect cases where a
2267 * router serves a private DNS zone directly under
2268 * non-existing TLD. In such a case we cannot detect whether
2269 * the TLD is supposed to exist or not, as all requests we
2270 * make for it will be answered by the router's zone, and not
2271 * by the root zone. */
2275 if (t
->scope
->dnssec_mode
!= DNSSEC_ALLOW_DOWNGRADE
)
2276 return false; /* In strict DNSSEC mode what doesn't exist, doesn't exist */
2278 tld
= DNS_RESOURCE_KEY_NAME(key
);
2279 r
= dns_name_parent(&tld
);
2283 return false; /* Already the root domain */
2285 if (!dns_name_is_single_label(tld
))
2288 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
2290 if (dt
->key
->class != key
->class)
2293 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), tld
);
2299 /* We found an auxiliary lookup we did for the TLD. If
2300 * that returned with NXDOMAIN, we know the TLD didn't
2301 * exist, and hence this might be a private zone. */
2303 return dt
->answer_rcode
== DNS_RCODE_NXDOMAIN
;
2309 static int dns_transaction_requires_nsec(DnsTransaction
*t
) {
2317 /* Checks if we need to insist on NSEC/NSEC3 RRs for proving
2318 * this negative reply */
2320 if (t
->scope
->dnssec_mode
== DNSSEC_NO
)
2323 if (dns_type_is_pseudo(t
->key
->type
))
2326 r
= dns_transaction_negative_trust_anchor_lookup(t
, DNS_RESOURCE_KEY_NAME(t
->key
));
2332 r
= dns_transaction_in_private_tld(t
, t
->key
);
2336 /* The lookup is from a TLD that is proven not to
2337 * exist, and we are in downgrade mode, hence ignore
2338 * that fact that we didn't get any NSEC RRs.*/
2340 log_info("Detected a negative query %s in a private DNS zone, permitting unsigned response.", dns_transaction_key_string(t
));
2344 name
= DNS_RESOURCE_KEY_NAME(t
->key
);
2346 if (IN_SET(t
->key
->type
, DNS_TYPE_SOA
, DNS_TYPE_NS
, DNS_TYPE_DS
)) {
2348 /* We got a negative reply for this SOA/NS lookup? If
2349 * so, then we are not at a zone apex, and thus should
2350 * look at the result of the parent SOA lookup.
2352 * We got a negative reply for this DS lookup? DS RRs
2353 * are signed when their parent zone is signed, hence
2354 * also check the parent SOA in this case. */
2356 r
= dns_name_parent(&name
);
2363 /* For all other RRs we check the SOA on the same level to see
2364 * if it's signed. */
2366 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
2368 if (dt
->key
->class != t
->key
->class)
2370 if (dt
->key
->type
!= DNS_TYPE_SOA
)
2373 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), name
);
2379 return dt
->answer_authenticated
;
2382 /* If in doubt, require NSEC/NSEC3 */
2386 static int dns_transaction_dnskey_authenticated(DnsTransaction
*t
, DnsResourceRecord
*rr
) {
2387 DnsResourceRecord
*rrsig
;
2391 /* Checks whether any of the DNSKEYs used for the RRSIGs for
2392 * the specified RRset is authenticated (i.e. has a matching
2395 r
= dns_transaction_negative_trust_anchor_lookup(t
, DNS_RESOURCE_KEY_NAME(rr
->key
));
2401 DNS_ANSWER_FOREACH(rrsig
, t
->answer
) {
2405 r
= dnssec_key_match_rrsig(rr
->key
, rrsig
);
2411 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
2413 if (dt
->key
->class != rr
->key
->class)
2416 if (dt
->key
->type
== DNS_TYPE_DNSKEY
) {
2418 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), rrsig
->rrsig
.signer
);
2424 /* OK, we found an auxiliary DNSKEY
2425 * lookup. If that lookup is
2426 * authenticated, report this. */
2428 if (dt
->answer_authenticated
)
2433 } else if (dt
->key
->type
== DNS_TYPE_DS
) {
2435 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), rrsig
->rrsig
.signer
);
2441 /* OK, we found an auxiliary DS
2442 * lookup. If that lookup is
2443 * authenticated and non-zero, we
2446 if (!dt
->answer_authenticated
)
2449 return dns_answer_match_key(dt
->answer
, dt
->key
, NULL
);
2454 return found
? false : -ENXIO
;
2457 static int dns_transaction_known_signed(DnsTransaction
*t
, DnsResourceRecord
*rr
) {
2461 /* We know that the root domain is signed, hence if it appears
2462 * not to be signed, there's a problem with the DNS server */
2464 return rr
->key
->class == DNS_CLASS_IN
&&
2465 dns_name_is_root(DNS_RESOURCE_KEY_NAME(rr
->key
));
2468 static int dns_transaction_check_revoked_trust_anchors(DnsTransaction
*t
) {
2469 DnsResourceRecord
*rr
;
2474 /* Maybe warn the user that we encountered a revoked DNSKEY
2475 * for a key from our trust anchor. Note that we don't care
2476 * whether the DNSKEY can be authenticated or not. It's
2477 * sufficient if it is self-signed. */
2479 DNS_ANSWER_FOREACH(rr
, t
->answer
) {
2480 r
= dns_trust_anchor_check_revoked(&t
->scope
->manager
->trust_anchor
, rr
, t
->answer
);
2488 static int dns_transaction_invalidate_revoked_keys(DnsTransaction
*t
) {
2494 /* Removes all DNSKEY/DS objects from t->validated_keys that
2495 * our trust anchors database considers revoked. */
2498 DnsResourceRecord
*rr
;
2502 DNS_ANSWER_FOREACH(rr
, t
->validated_keys
) {
2503 r
= dns_trust_anchor_is_revoked(&t
->scope
->manager
->trust_anchor
, rr
);
2507 r
= dns_answer_remove_by_rr(&t
->validated_keys
, rr
);
2521 static int dns_transaction_copy_validated(DnsTransaction
*t
) {
2528 /* Copy all validated RRs from the auxiliary DNSSEC transactions into our set of validated RRs */
2530 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
2532 if (DNS_TRANSACTION_IS_LIVE(dt
->state
))
2535 if (!dt
->answer_authenticated
)
2538 r
= dns_answer_extend(&t
->validated_keys
, dt
->answer
);
2546 int dns_transaction_validate_dnssec(DnsTransaction
*t
) {
2547 _cleanup_(dns_answer_unrefp
) DnsAnswer
*validated
= NULL
;
2549 PHASE_DNSKEY
, /* Phase #1, only validate DNSKEYs */
2550 PHASE_NSEC
, /* Phase #2, only validate NSEC+NSEC3 */
2551 PHASE_ALL
, /* Phase #3, validate everything else */
2553 DnsResourceRecord
*rr
;
2554 DnsAnswerFlags flags
;
2559 /* We have now collected all DS and DNSKEY RRs in
2560 * t->validated_keys, let's see which RRs we can now
2561 * authenticate with that. */
2563 if (t
->scope
->dnssec_mode
== DNSSEC_NO
)
2566 /* Already validated */
2567 if (t
->answer_dnssec_result
!= _DNSSEC_RESULT_INVALID
)
2570 /* Our own stuff needs no validation */
2571 if (IN_SET(t
->answer_source
, DNS_TRANSACTION_ZONE
, DNS_TRANSACTION_TRUST_ANCHOR
)) {
2572 t
->answer_dnssec_result
= DNSSEC_VALIDATED
;
2573 t
->answer_authenticated
= true;
2577 /* Cached stuff is not affected by validation. */
2578 if (t
->answer_source
!= DNS_TRANSACTION_NETWORK
)
2581 if (!dns_transaction_dnssec_supported_full(t
)) {
2582 /* The server does not support DNSSEC, or doesn't augment responses with RRSIGs. */
2583 t
->answer_dnssec_result
= DNSSEC_INCOMPATIBLE_SERVER
;
2584 log_debug("Not validating response for %" PRIu16
", server lacks DNSSEC support.", t
->id
);
2588 log_debug("Validating response from transaction %" PRIu16
" (%s).", t
->id
, dns_transaction_key_string(t
));
2590 /* First, see if this response contains any revoked trust
2591 * anchors we care about */
2592 r
= dns_transaction_check_revoked_trust_anchors(t
);
2596 /* Third, copy all RRs we acquired successfully from auxiliary RRs over. */
2597 r
= dns_transaction_copy_validated(t
);
2601 /* Second, see if there are DNSKEYs we already know a
2602 * validated DS for. */
2603 r
= dns_transaction_validate_dnskey_by_ds(t
);
2607 /* Fourth, remove all DNSKEY and DS RRs again that our trust
2608 * anchor says are revoked. After all we might have marked
2609 * some keys revoked above, but they might still be lingering
2610 * in our validated_keys list. */
2611 r
= dns_transaction_invalidate_revoked_keys(t
);
2615 phase
= PHASE_DNSKEY
;
2617 bool changed
= false, have_nsec
= false;
2619 DNS_ANSWER_FOREACH(rr
, t
->answer
) {
2620 DnsResourceRecord
*rrsig
= NULL
;
2621 DnssecResult result
;
2623 switch (rr
->key
->type
) {
2625 case DNS_TYPE_RRSIG
:
2628 case DNS_TYPE_DNSKEY
:
2629 /* We validate DNSKEYs only in the DNSKEY and ALL phases */
2630 if (phase
== PHASE_NSEC
)
2635 case DNS_TYPE_NSEC3
:
2638 /* We validate NSEC/NSEC3 only in the NSEC and ALL phases */
2639 if (phase
== PHASE_DNSKEY
)
2645 /* We validate all other RRs only in the ALL phases */
2646 if (phase
!= PHASE_ALL
)
2652 r
= dnssec_verify_rrset_search(t
->answer
, rr
->key
, t
->validated_keys
, USEC_INFINITY
, &result
, &rrsig
);
2656 log_debug("Looking at %s: %s", strna(dns_resource_record_to_string(rr
)), dnssec_result_to_string(result
));
2658 if (result
== DNSSEC_VALIDATED
) {
2660 if (rr
->key
->type
== DNS_TYPE_DNSKEY
) {
2661 /* If we just validated a
2662 * DNSKEY RRset, then let's
2663 * add these keys to the set
2664 * of validated keys for this
2667 r
= dns_answer_copy_by_key(&t
->validated_keys
, t
->answer
, rr
->key
, DNS_ANSWER_AUTHENTICATED
);
2671 /* some of the DNSKEYs we just
2672 * added might already have
2673 * been revoked, remove them
2674 * again in that case. */
2675 r
= dns_transaction_invalidate_revoked_keys(t
);
2680 /* Add the validated RRset to the new
2681 * list of validated RRsets, and
2682 * remove it from the unvalidated
2683 * RRsets. We mark the RRset as
2684 * authenticated and cacheable. */
2685 r
= dns_answer_move_by_key(&validated
, &t
->answer
, rr
->key
, DNS_ANSWER_AUTHENTICATED
|DNS_ANSWER_CACHEABLE
);
2689 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_SECURE
, rr
->key
);
2691 /* Exit the loop, we dropped something from the answer, start from the beginning */
2696 /* If we haven't read all DNSKEYs yet a negative result of the validation is irrelevant, as
2697 * there might be more DNSKEYs coming. Similar, if we haven't read all NSEC/NSEC3 RRs yet, we
2698 * cannot do positive wildcard proofs yet, as those require the NSEC/NSEC3 RRs. */
2699 if (phase
!= PHASE_ALL
)
2702 if (result
== DNSSEC_VALIDATED_WILDCARD
) {
2703 bool authenticated
= false;
2706 /* This RRset validated, but as a wildcard. This means we need to prove via NSEC/NSEC3
2707 * that no matching non-wildcard RR exists.*/
2709 /* First step, determine the source of synthesis */
2710 r
= dns_resource_record_source(rrsig
, &source
);
2714 r
= dnssec_test_positive_wildcard(
2716 DNS_RESOURCE_KEY_NAME(rr
->key
),
2718 rrsig
->rrsig
.signer
,
2721 /* Unless the NSEC proof showed that the key really doesn't exist something is off. */
2723 result
= DNSSEC_INVALID
;
2725 r
= dns_answer_move_by_key(&validated
, &t
->answer
, rr
->key
, authenticated
? (DNS_ANSWER_AUTHENTICATED
|DNS_ANSWER_CACHEABLE
) : 0);
2729 manager_dnssec_verdict(t
->scope
->manager
, authenticated
? DNSSEC_SECURE
: DNSSEC_INSECURE
, rr
->key
);
2731 /* Exit the loop, we dropped something from the answer, start from the beginning */
2737 if (result
== DNSSEC_NO_SIGNATURE
) {
2738 r
= dns_transaction_requires_rrsig(t
, rr
);
2742 /* Data does not require signing. In that case, just copy it over,
2743 * but remember that this is by no means authenticated.*/
2744 r
= dns_answer_move_by_key(&validated
, &t
->answer
, rr
->key
, 0);
2748 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INSECURE
, rr
->key
);
2753 r
= dns_transaction_known_signed(t
, rr
);
2757 /* This is an RR we know has to be signed. If it isn't this means
2758 * the server is not attaching RRSIGs, hence complain. */
2760 dns_server_packet_rrsig_missing(t
->server
, t
->current_feature_level
);
2762 if (t
->scope
->dnssec_mode
== DNSSEC_ALLOW_DOWNGRADE
) {
2764 /* Downgrading is OK? If so, just consider the information unsigned */
2766 r
= dns_answer_move_by_key(&validated
, &t
->answer
, rr
->key
, 0);
2770 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INSECURE
, rr
->key
);
2775 /* Otherwise, fail */
2776 t
->answer_dnssec_result
= DNSSEC_INCOMPATIBLE_SERVER
;
2780 r
= dns_transaction_in_private_tld(t
, rr
->key
);
2784 _cleanup_free_
char *s
= NULL
;
2786 /* The data is from a TLD that is proven not to exist, and we are in downgrade
2787 * mode, hence ignore the fact that this was not signed. */
2789 (void) dns_resource_key_to_string(rr
->key
, &s
);
2790 log_info("Detected RRset %s is in a private DNS zone, permitting unsigned RRs.", strna(s
? strstrip(s
) : NULL
));
2792 r
= dns_answer_move_by_key(&validated
, &t
->answer
, rr
->key
, 0);
2796 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INSECURE
, rr
->key
);
2804 DNSSEC_SIGNATURE_EXPIRED
,
2805 DNSSEC_UNSUPPORTED_ALGORITHM
)) {
2807 r
= dns_transaction_dnskey_authenticated(t
, rr
);
2808 if (r
< 0 && r
!= -ENXIO
)
2811 /* The DNSKEY transaction was not authenticated, this means there's
2812 * no DS for this, which means it's OK if no keys are found for this signature. */
2814 r
= dns_answer_move_by_key(&validated
, &t
->answer
, rr
->key
, 0);
2818 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INSECURE
, rr
->key
);
2824 r
= dns_transaction_is_primary_response(t
, rr
);
2829 /* Look for a matching DNAME for this CNAME */
2830 r
= dns_answer_has_dname_for_cname(t
->answer
, rr
);
2834 /* Also look among the stuff we already validated */
2835 r
= dns_answer_has_dname_for_cname(validated
, rr
);
2843 DNSSEC_SIGNATURE_EXPIRED
,
2844 DNSSEC_NO_SIGNATURE
))
2845 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_BOGUS
, rr
->key
);
2846 else /* DNSSEC_MISSING_KEY or DNSSEC_UNSUPPORTED_ALGORITHM */
2847 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INDETERMINATE
, rr
->key
);
2849 /* This is a primary response to our question, and it failed validation. That's
2851 t
->answer_dnssec_result
= result
;
2855 /* This is a primary response, but we do have a DNAME RR in the RR that can replay this
2856 * CNAME, hence rely on that, and we can remove the CNAME in favour of it. */
2859 /* This is just some auxiliary data. Just remove the RRset and continue. */
2860 r
= dns_answer_remove_by_key(&t
->answer
, rr
->key
);
2864 /* Exit the loop, we dropped something from the answer, start from the beginning */
2869 /* Restart the inner loop as long as we managed to achieve something */
2873 if (phase
== PHASE_DNSKEY
&& have_nsec
) {
2874 /* OK, we processed all DNSKEYs, and there are NSEC/NSEC3 RRs, look at those now. */
2879 if (phase
!= PHASE_ALL
) {
2880 /* OK, we processed all DNSKEYs and NSEC/NSEC3 RRs, look at all the rest now. Note that in this
2881 * third phase we start to remove RRs we couldn't validate. */
2890 dns_answer_unref(t
->answer
);
2891 t
->answer
= validated
;
2894 /* At this point the answer only contains validated
2895 * RRsets. Now, let's see if it actually answers the question
2896 * we asked. If so, great! If it doesn't, then see if
2897 * NSEC/NSEC3 can prove this. */
2898 r
= dns_transaction_has_positive_answer(t
, &flags
);
2900 /* Yes, it answers the question! */
2902 if (flags
& DNS_ANSWER_AUTHENTICATED
) {
2903 /* The answer is fully authenticated, yay. */
2904 t
->answer_dnssec_result
= DNSSEC_VALIDATED
;
2905 t
->answer_rcode
= DNS_RCODE_SUCCESS
;
2906 t
->answer_authenticated
= true;
2908 /* The answer is not fully authenticated. */
2909 t
->answer_dnssec_result
= DNSSEC_UNSIGNED
;
2910 t
->answer_authenticated
= false;
2913 } else if (r
== 0) {
2914 DnssecNsecResult nr
;
2915 bool authenticated
= false;
2917 /* Bummer! Let's check NSEC/NSEC3 */
2918 r
= dnssec_nsec_test(t
->answer
, t
->key
, &nr
, &authenticated
, &t
->answer_nsec_ttl
);
2924 case DNSSEC_NSEC_NXDOMAIN
:
2925 /* NSEC proves the domain doesn't exist. Very good. */
2926 log_debug("Proved NXDOMAIN via NSEC/NSEC3 for transaction %u (%s)", t
->id
, dns_transaction_key_string(t
));
2927 t
->answer_dnssec_result
= DNSSEC_VALIDATED
;
2928 t
->answer_rcode
= DNS_RCODE_NXDOMAIN
;
2929 t
->answer_authenticated
= authenticated
;
2931 manager_dnssec_verdict(t
->scope
->manager
, authenticated
? DNSSEC_SECURE
: DNSSEC_INSECURE
, t
->key
);
2934 case DNSSEC_NSEC_NODATA
:
2935 /* NSEC proves that there's no data here, very good. */
2936 log_debug("Proved NODATA via NSEC/NSEC3 for transaction %u (%s)", t
->id
, dns_transaction_key_string(t
));
2937 t
->answer_dnssec_result
= DNSSEC_VALIDATED
;
2938 t
->answer_rcode
= DNS_RCODE_SUCCESS
;
2939 t
->answer_authenticated
= authenticated
;
2941 manager_dnssec_verdict(t
->scope
->manager
, authenticated
? DNSSEC_SECURE
: DNSSEC_INSECURE
, t
->key
);
2944 case DNSSEC_NSEC_OPTOUT
:
2945 /* NSEC3 says the data might not be signed */
2946 log_debug("Data is NSEC3 opt-out via NSEC/NSEC3 for transaction %u (%s)", t
->id
, dns_transaction_key_string(t
));
2947 t
->answer_dnssec_result
= DNSSEC_UNSIGNED
;
2948 t
->answer_authenticated
= false;
2950 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INSECURE
, t
->key
);
2953 case DNSSEC_NSEC_NO_RR
:
2954 /* No NSEC data? Bummer! */
2956 r
= dns_transaction_requires_nsec(t
);
2960 t
->answer_dnssec_result
= DNSSEC_NO_SIGNATURE
;
2961 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_BOGUS
, t
->key
);
2963 t
->answer_dnssec_result
= DNSSEC_UNSIGNED
;
2964 t
->answer_authenticated
= false;
2965 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INSECURE
, t
->key
);
2970 case DNSSEC_NSEC_UNSUPPORTED_ALGORITHM
:
2971 /* We don't know the NSEC3 algorithm used? */
2972 t
->answer_dnssec_result
= DNSSEC_UNSUPPORTED_ALGORITHM
;
2973 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INDETERMINATE
, t
->key
);
2976 case DNSSEC_NSEC_FOUND
:
2977 case DNSSEC_NSEC_CNAME
:
2978 /* NSEC says it needs to be there, but we couldn't find it? Bummer! */
2979 t
->answer_dnssec_result
= DNSSEC_NSEC_MISMATCH
;
2980 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_BOGUS
, t
->key
);
2984 assert_not_reached("Unexpected NSEC result.");
2991 const char *dns_transaction_key_string(DnsTransaction
*t
) {
2994 if (!t
->key_string
) {
2995 if (dns_resource_key_to_string(t
->key
, &t
->key_string
) < 0)
2999 return strstrip(t
->key_string
);
3002 static const char* const dns_transaction_state_table
[_DNS_TRANSACTION_STATE_MAX
] = {
3003 [DNS_TRANSACTION_NULL
] = "null",
3004 [DNS_TRANSACTION_PENDING
] = "pending",
3005 [DNS_TRANSACTION_VALIDATING
] = "validating",
3006 [DNS_TRANSACTION_RCODE_FAILURE
] = "rcode-failure",
3007 [DNS_TRANSACTION_SUCCESS
] = "success",
3008 [DNS_TRANSACTION_NO_SERVERS
] = "no-servers",
3009 [DNS_TRANSACTION_TIMEOUT
] = "timeout",
3010 [DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
] = "attempts-max-reached",
3011 [DNS_TRANSACTION_INVALID_REPLY
] = "invalid-reply",
3012 [DNS_TRANSACTION_ERRNO
] = "errno",
3013 [DNS_TRANSACTION_ABORTED
] = "aborted",
3014 [DNS_TRANSACTION_DNSSEC_FAILED
] = "dnssec-failed",
3015 [DNS_TRANSACTION_NO_TRUST_ANCHOR
] = "no-trust-anchor",
3016 [DNS_TRANSACTION_RR_TYPE_UNSUPPORTED
] = "rr-type-unsupported",
3017 [DNS_TRANSACTION_NETWORK_DOWN
] = "network-down",
3018 [DNS_TRANSACTION_NOT_FOUND
] = "not-found",
3020 DEFINE_STRING_TABLE_LOOKUP(dns_transaction_state
, DnsTransactionState
);
3022 static const char* const dns_transaction_source_table
[_DNS_TRANSACTION_SOURCE_MAX
] = {
3023 [DNS_TRANSACTION_NETWORK
] = "network",
3024 [DNS_TRANSACTION_CACHE
] = "cache",
3025 [DNS_TRANSACTION_ZONE
] = "zone",
3026 [DNS_TRANSACTION_TRUST_ANCHOR
] = "trust-anchor",
3028 DEFINE_STRING_TABLE_LOOKUP(dns_transaction_source
, DnsTransactionSource
);