2 This file is part of systemd.
4 Copyright 2014 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include <sd-messages.h>
23 #include "alloc-util.h"
24 #include "dns-domain.h"
25 #include "errno-list.h"
27 #include "random-util.h"
28 #include "resolved-dns-cache.h"
29 #include "resolved-dns-transaction.h"
30 #include "resolved-llmnr.h"
31 #include "string-table.h"
33 #define TRANSACTIONS_MAX 4096
35 static void dns_transaction_reset_answer(DnsTransaction
*t
) {
38 t
->received
= dns_packet_unref(t
->received
);
39 t
->answer
= dns_answer_unref(t
->answer
);
41 t
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
42 t
->answer_source
= _DNS_TRANSACTION_SOURCE_INVALID
;
43 t
->answer_authenticated
= false;
44 t
->answer_nsec_ttl
= (uint32_t) -1;
48 static void dns_transaction_flush_dnssec_transactions(DnsTransaction
*t
) {
53 while ((z
= set_steal_first(t
->dnssec_transactions
))) {
54 set_remove(z
->notify_transactions
, t
);
55 dns_transaction_gc(z
);
59 static void dns_transaction_close_connection(DnsTransaction
*t
) {
62 t
->stream
= dns_stream_free(t
->stream
);
63 t
->dns_udp_event_source
= sd_event_source_unref(t
->dns_udp_event_source
);
64 t
->dns_udp_fd
= safe_close(t
->dns_udp_fd
);
67 static void dns_transaction_stop_timeout(DnsTransaction
*t
) {
70 t
->timeout_event_source
= sd_event_source_unref(t
->timeout_event_source
);
73 DnsTransaction
* dns_transaction_free(DnsTransaction
*t
) {
81 log_debug("Freeing transaction %" PRIu16
".", t
->id
);
83 dns_transaction_close_connection(t
);
84 dns_transaction_stop_timeout(t
);
86 dns_packet_unref(t
->sent
);
87 dns_transaction_reset_answer(t
);
89 dns_server_unref(t
->server
);
92 hashmap_remove_value(t
->scope
->transactions_by_key
, t
->key
, t
);
93 LIST_REMOVE(transactions_by_scope
, t
->scope
->transactions
, t
);
96 hashmap_remove(t
->scope
->manager
->dns_transactions
, UINT_TO_PTR(t
->id
));
99 while ((c
= set_steal_first(t
->notify_query_candidates
)))
100 set_remove(c
->transactions
, t
);
101 set_free(t
->notify_query_candidates
);
103 while ((i
= set_steal_first(t
->notify_zone_items
)))
104 i
->probe_transaction
= NULL
;
105 set_free(t
->notify_zone_items
);
107 while ((z
= set_steal_first(t
->notify_transactions
)))
108 set_remove(z
->dnssec_transactions
, t
);
109 set_free(t
->notify_transactions
);
111 dns_transaction_flush_dnssec_transactions(t
);
112 set_free(t
->dnssec_transactions
);
114 dns_answer_unref(t
->validated_keys
);
115 dns_resource_key_unref(t
->key
);
122 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsTransaction
*, dns_transaction_free
);
124 bool dns_transaction_gc(DnsTransaction
*t
) {
130 if (set_isempty(t
->notify_query_candidates
) &&
131 set_isempty(t
->notify_zone_items
) &&
132 set_isempty(t
->notify_transactions
)) {
133 dns_transaction_free(t
);
140 static uint16_t pick_new_id(Manager
*m
) {
143 /* Find a fresh, unused transaction id. Note that this loop is bounded because there's a limit on the number of
144 * transactions, and it's much lower than the space of IDs. */
146 assert_cc(TRANSACTIONS_MAX
< 0xFFFF);
149 random_bytes(&new_id
, sizeof(new_id
));
150 while (new_id
== 0 ||
151 hashmap_get(m
->dns_transactions
, UINT_TO_PTR(new_id
)));
156 int dns_transaction_new(DnsTransaction
**ret
, DnsScope
*s
, DnsResourceKey
*key
) {
157 _cleanup_(dns_transaction_freep
) DnsTransaction
*t
= NULL
;
164 /* Don't allow looking up invalid or pseudo RRs */
165 if (!dns_type_is_valid_query(key
->type
))
167 if (dns_type_is_obsolete(key
->type
))
170 /* We only support the IN class */
171 if (key
->class != DNS_CLASS_IN
&& key
->class != DNS_CLASS_ANY
)
174 if (hashmap_size(s
->manager
->dns_transactions
) >= TRANSACTIONS_MAX
)
177 r
= hashmap_ensure_allocated(&s
->manager
->dns_transactions
, NULL
);
181 r
= hashmap_ensure_allocated(&s
->transactions_by_key
, &dns_resource_key_hash_ops
);
185 t
= new0(DnsTransaction
, 1);
190 t
->answer_source
= _DNS_TRANSACTION_SOURCE_INVALID
;
191 t
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
192 t
->answer_nsec_ttl
= (uint32_t) -1;
193 t
->key
= dns_resource_key_ref(key
);
194 t
->current_feature_level
= _DNS_SERVER_FEATURE_LEVEL_INVALID
;
196 t
->id
= pick_new_id(s
->manager
);
198 r
= hashmap_put(s
->manager
->dns_transactions
, UINT_TO_PTR(t
->id
), t
);
204 r
= hashmap_replace(s
->transactions_by_key
, t
->key
, t
);
206 hashmap_remove(s
->manager
->dns_transactions
, UINT_TO_PTR(t
->id
));
210 LIST_PREPEND(transactions_by_scope
, s
->transactions
, t
);
213 s
->manager
->n_transactions_total
++;
223 static void dns_transaction_shuffle_id(DnsTransaction
*t
) {
227 /* Pick a new ID for this transaction. */
229 new_id
= pick_new_id(t
->scope
->manager
);
230 assert_se(hashmap_remove_and_put(t
->scope
->manager
->dns_transactions
, UINT_TO_PTR(t
->id
), UINT_TO_PTR(new_id
), t
) >= 0);
232 log_debug("Transaction %" PRIu16
" is now %" PRIu16
".", t
->id
, new_id
);
235 /* Make sure we generate a new packet with the new ID */
236 t
->sent
= dns_packet_unref(t
->sent
);
239 static void dns_transaction_tentative(DnsTransaction
*t
, DnsPacket
*p
) {
240 _cleanup_free_
char *pretty
= NULL
;
246 if (manager_our_packet(t
->scope
->manager
, p
) != 0)
249 in_addr_to_string(p
->family
, &p
->sender
, &pretty
);
251 log_debug("Transaction %" PRIu16
" for <%s> on scope %s on %s/%s got tentative packet from %s.",
253 dns_transaction_key_string(t
),
254 dns_protocol_to_string(t
->scope
->protocol
),
255 t
->scope
->link
? t
->scope
->link
->name
: "*",
256 t
->scope
->family
== AF_UNSPEC
? "*" : af_to_name(t
->scope
->family
),
259 /* RFC 4795, Section 4.1 says that the peer with the
260 * lexicographically smaller IP address loses */
261 if (memcmp(&p
->sender
, &p
->destination
, FAMILY_ADDRESS_SIZE(p
->family
)) >= 0) {
262 log_debug("Peer has lexicographically larger IP address and thus lost in the conflict.");
266 log_debug("We have the lexicographically larger IP address and thus lost in the conflict.");
269 while ((z
= set_first(t
->notify_zone_items
))) {
270 /* First, make sure the zone item drops the reference
272 dns_zone_item_probe_stop(z
);
274 /* Secondly, report this as conflict, so that we might
275 * look for a different hostname */
276 dns_zone_item_conflict(z
);
280 dns_transaction_gc(t
);
283 void dns_transaction_complete(DnsTransaction
*t
, DnsTransactionState state
) {
284 DnsQueryCandidate
*c
;
291 assert(!DNS_TRANSACTION_IS_LIVE(state
));
293 if (state
== DNS_TRANSACTION_DNSSEC_FAILED
)
294 log_struct(LOG_NOTICE
,
295 LOG_MESSAGE_ID(SD_MESSAGE_DNSSEC_FAILURE
),
296 LOG_MESSAGE("DNSSEC validation failed for question %s: %s", dns_transaction_key_string(t
), dnssec_result_to_string(t
->answer_dnssec_result
)),
297 "DNS_TRANSACTION=%" PRIu16
, t
->id
,
298 "DNS_QUESTION=%s", dns_transaction_key_string(t
),
299 "DNSSEC_RESULT=%s", dnssec_result_to_string(t
->answer_dnssec_result
),
300 "DNS_SERVER=%s", dns_server_string(t
->server
),
301 "DNS_SERVER_FEATURE_LEVEL=%s", dns_server_feature_level_to_string(t
->server
->possible_feature_level
),
304 /* Note that this call might invalidate the query. Callers
305 * should hence not attempt to access the query or transaction
306 * after calling this function. */
308 if (state
== DNS_TRANSACTION_ERRNO
)
309 st
= errno_to_name(t
->answer_errno
);
311 st
= dns_transaction_state_to_string(state
);
313 log_debug("Transaction %" PRIu16
" for <%s> on scope %s on %s/%s now complete with <%s> from %s (%s).",
315 dns_transaction_key_string(t
),
316 dns_protocol_to_string(t
->scope
->protocol
),
317 t
->scope
->link
? t
->scope
->link
->name
: "*",
318 t
->scope
->family
== AF_UNSPEC
? "*" : af_to_name(t
->scope
->family
),
320 t
->answer_source
< 0 ? "none" : dns_transaction_source_to_string(t
->answer_source
),
321 t
->answer_authenticated
? "authenticated" : "unsigned");
325 dns_transaction_close_connection(t
);
326 dns_transaction_stop_timeout(t
);
328 /* Notify all queries that are interested, but make sure the
329 * transaction isn't freed while we are still looking at it */
332 SET_FOREACH(c
, t
->notify_query_candidates
, i
)
333 dns_query_candidate_notify(c
);
334 SET_FOREACH(z
, t
->notify_zone_items
, i
)
335 dns_zone_item_notify(z
);
337 if (!set_isempty(t
->notify_transactions
)) {
341 /* We need to be careful when notifying other
342 * transactions, as that might destroy other
343 * transactions in our list. Hence, in order to be
344 * able to safely iterate through the list of
345 * transactions, take a GC lock on all of them
346 * first. Then, in a second loop, notify them, but
347 * first unlock that specific transaction. */
349 nt
= newa(DnsTransaction
*, set_size(t
->notify_transactions
));
350 SET_FOREACH(d
, t
->notify_transactions
, i
) {
355 assert(n
== set_size(t
->notify_transactions
));
357 for (j
= 0; j
< n
; j
++) {
358 if (set_contains(t
->notify_transactions
, nt
[j
]))
359 dns_transaction_notify(nt
[j
], t
);
362 dns_transaction_gc(nt
[j
]);
367 dns_transaction_gc(t
);
370 static int dns_transaction_pick_server(DnsTransaction
*t
) {
374 assert(t
->scope
->protocol
== DNS_PROTOCOL_DNS
);
376 server
= dns_scope_get_dns_server(t
->scope
);
380 t
->current_feature_level
= dns_server_possible_feature_level(server
);
382 if (server
== t
->server
)
385 dns_server_unref(t
->server
);
386 t
->server
= dns_server_ref(server
);
391 static void dns_transaction_retry(DnsTransaction
*t
) {
396 log_debug("Retrying transaction %" PRIu16
".", t
->id
);
398 /* Before we try again, switch to a new server. */
399 dns_scope_next_dns_server(t
->scope
);
401 r
= dns_transaction_go(t
);
403 t
->answer_errno
= -r
;
404 dns_transaction_complete(t
, DNS_TRANSACTION_ERRNO
);
408 static int dns_transaction_maybe_restart(DnsTransaction
*t
) {
414 if (t
->current_feature_level
<= dns_server_possible_feature_level(t
->server
))
417 /* The server's current feature level is lower than when we sent the original query. We learnt something from
418 the response or possibly an auxiliary DNSSEC response that we didn't know before. We take that as reason to
419 restart the whole transaction. This is a good idea to deal with servers that respond rubbish if we include
420 OPT RR or DO bit. One of these cases is documented here, for example:
421 https://open.nlnetlabs.nl/pipermail/dnssec-trigger/2014-November/000376.html */
423 log_debug("Server feature level is now lower than when we began our transaction. Restarting with new ID.");
424 dns_transaction_shuffle_id(t
);
425 return dns_transaction_go(t
);
428 static int on_stream_complete(DnsStream
*s
, int error
) {
429 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
433 assert(s
->transaction
);
435 /* Copy the data we care about out of the stream before we
438 p
= dns_packet_ref(s
->read_packet
);
440 t
->stream
= dns_stream_free(t
->stream
);
442 if (ERRNO_IS_DISCONNECT(error
)) {
445 if (t
->scope
->protocol
== DNS_PROTOCOL_LLMNR
) {
446 /* If the LLMNR/TCP connection failed, the host doesn't support LLMNR, and we cannot answer the
447 * question on this scope. */
448 dns_transaction_complete(t
, DNS_TRANSACTION_NOT_FOUND
);
452 log_debug_errno(error
, "Connection failure for DNS TCP stream: %m");
453 assert_se(sd_event_now(t
->scope
->manager
->event
, clock_boottime_or_monotonic(), &usec
) >= 0);
454 dns_server_packet_lost(t
->server
, IPPROTO_TCP
, t
->current_feature_level
, usec
- t
->start_usec
);
456 dns_transaction_retry(t
);
460 t
->answer_errno
= error
;
461 dns_transaction_complete(t
, DNS_TRANSACTION_ERRNO
);
465 if (dns_packet_validate_reply(p
) <= 0) {
466 log_debug("Invalid TCP reply packet.");
467 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
471 dns_scope_check_conflicts(t
->scope
, p
);
474 dns_transaction_process_reply(t
, p
);
477 /* If the response wasn't useful, then complete the transition
478 * now. After all, we are the worst feature set now with TCP
479 * sockets, and there's really no point in retrying. */
480 if (t
->state
== DNS_TRANSACTION_PENDING
)
481 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
483 dns_transaction_gc(t
);
488 static int dns_transaction_open_tcp(DnsTransaction
*t
) {
489 _cleanup_close_
int fd
= -1;
494 dns_transaction_close_connection(t
);
496 switch (t
->scope
->protocol
) {
498 case DNS_PROTOCOL_DNS
:
499 r
= dns_transaction_pick_server(t
);
503 if (!dns_server_dnssec_supported(t
->server
) && dns_type_is_dnssec(t
->key
->type
))
506 r
= dns_server_adjust_opt(t
->server
, t
->sent
, t
->current_feature_level
);
510 fd
= dns_scope_socket_tcp(t
->scope
, AF_UNSPEC
, NULL
, t
->server
, 53);
513 case DNS_PROTOCOL_LLMNR
:
514 /* When we already received a reply to this (but it was truncated), send to its sender address */
516 fd
= dns_scope_socket_tcp(t
->scope
, t
->received
->family
, &t
->received
->sender
, NULL
, t
->received
->sender_port
);
518 union in_addr_union address
;
519 int family
= AF_UNSPEC
;
521 /* Otherwise, try to talk to the owner of a
522 * the IP address, in case this is a reverse
525 r
= dns_name_address(DNS_RESOURCE_KEY_NAME(t
->key
), &family
, &address
);
530 if (family
!= t
->scope
->family
)
533 fd
= dns_scope_socket_tcp(t
->scope
, family
, &address
, NULL
, LLMNR_PORT
);
539 return -EAFNOSUPPORT
;
545 r
= dns_stream_new(t
->scope
->manager
, &t
->stream
, t
->scope
->protocol
, fd
);
550 r
= dns_stream_write_packet(t
->stream
, t
->sent
);
552 t
->stream
= dns_stream_free(t
->stream
);
556 t
->stream
->complete
= on_stream_complete
;
557 t
->stream
->transaction
= t
;
559 /* The interface index is difficult to determine if we are
560 * connecting to the local host, hence fill this in right away
561 * instead of determining it from the socket */
563 t
->stream
->ifindex
= t
->scope
->link
->ifindex
;
565 dns_transaction_reset_answer(t
);
567 t
->tried_stream
= true;
572 static void dns_transaction_cache_answer(DnsTransaction
*t
) {
575 /* For mDNS we cache whenever we get the packet, rather than
576 * in each transaction. */
577 if (!IN_SET(t
->scope
->protocol
, DNS_PROTOCOL_DNS
, DNS_PROTOCOL_LLMNR
))
580 /* We never cache if this packet is from the local host, under
581 * the assumption that a locally running DNS server would
582 * cache this anyway, and probably knows better when to flush
583 * the cache then we could. */
584 if (!DNS_PACKET_SHALL_CACHE(t
->received
))
587 dns_cache_put(&t
->scope
->cache
,
591 t
->answer_authenticated
,
595 &t
->received
->sender
);
598 static bool dns_transaction_dnssec_is_live(DnsTransaction
*t
) {
604 SET_FOREACH(dt
, t
->dnssec_transactions
, i
)
605 if (DNS_TRANSACTION_IS_LIVE(dt
->state
))
611 static int dns_transaction_dnssec_ready(DnsTransaction
*t
) {
617 /* Checks whether the auxiliary DNSSEC transactions of our transaction have completed, or are still
618 * ongoing. Returns 0, if we aren't ready for the DNSSEC validation, positive if we are. */
620 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
624 case DNS_TRANSACTION_NULL
:
625 case DNS_TRANSACTION_PENDING
:
626 case DNS_TRANSACTION_VALIDATING
:
630 case DNS_TRANSACTION_RCODE_FAILURE
:
631 if (dt
->answer_rcode
!= DNS_RCODE_NXDOMAIN
) {
632 log_debug("Auxiliary DNSSEC RR query failed with rcode=%s.", dns_rcode_to_string(dt
->answer_rcode
));
636 /* Fall-through: NXDOMAIN is good enough for us. This is because some DNS servers erronously
637 * return NXDOMAIN for empty non-terminals (Akamai...), and we need to handle that nicely, when
638 * asking for parent SOA or similar RRs to make unsigned proofs. */
640 case DNS_TRANSACTION_SUCCESS
:
644 case DNS_TRANSACTION_DNSSEC_FAILED
:
645 /* We handle DNSSEC failures different from other errors, as we care about the DNSSEC
646 * validationr result */
648 log_debug("Auxiliary DNSSEC RR query failed validation: %s", dnssec_result_to_string(dt
->answer_dnssec_result
));
649 t
->answer_dnssec_result
= dt
->answer_dnssec_result
; /* Copy error code over */
650 dns_transaction_complete(t
, DNS_TRANSACTION_DNSSEC_FAILED
);
655 log_debug("Auxiliary DNSSEC RR query failed with %s", dns_transaction_state_to_string(dt
->state
));
660 /* All is ready, we can go and validate */
664 t
->answer_dnssec_result
= DNSSEC_FAILED_AUXILIARY
;
665 dns_transaction_complete(t
, DNS_TRANSACTION_DNSSEC_FAILED
);
669 static void dns_transaction_process_dnssec(DnsTransaction
*t
) {
674 /* Are there ongoing DNSSEC transactions? If so, let's wait for them. */
675 r
= dns_transaction_dnssec_ready(t
);
678 if (r
== 0) /* We aren't ready yet (or one of our auxiliary transactions failed, and we shouldn't validate now */
681 /* See if we learnt things from the additional DNSSEC transactions, that we didn't know before, and better
682 * restart the lookup immediately. */
683 r
= dns_transaction_maybe_restart(t
);
686 if (r
> 0) /* Transaction got restarted... */
689 /* All our auxiliary DNSSEC transactions are complete now. Try
690 * to validate our RRset now. */
691 r
= dns_transaction_validate_dnssec(t
);
693 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
699 if (t
->answer_dnssec_result
== DNSSEC_INCOMPATIBLE_SERVER
&&
700 t
->scope
->dnssec_mode
== DNSSEC_YES
) {
701 /* We are not in automatic downgrade mode, and the
702 * server is bad, refuse operation. */
703 dns_transaction_complete(t
, DNS_TRANSACTION_DNSSEC_FAILED
);
707 if (!IN_SET(t
->answer_dnssec_result
,
708 _DNSSEC_RESULT_INVALID
, /* No DNSSEC validation enabled */
709 DNSSEC_VALIDATED
, /* Answer is signed and validated successfully */
710 DNSSEC_UNSIGNED
, /* Answer is right-fully unsigned */
711 DNSSEC_INCOMPATIBLE_SERVER
)) { /* Server does not do DNSSEC (Yay, we are downgrade attack vulnerable!) */
712 dns_transaction_complete(t
, DNS_TRANSACTION_DNSSEC_FAILED
);
716 if (t
->answer_dnssec_result
== DNSSEC_INCOMPATIBLE_SERVER
)
717 dns_server_warn_downgrade(t
->server
);
719 dns_transaction_cache_answer(t
);
721 if (t
->answer_rcode
== DNS_RCODE_SUCCESS
)
722 dns_transaction_complete(t
, DNS_TRANSACTION_SUCCESS
);
724 dns_transaction_complete(t
, DNS_TRANSACTION_RCODE_FAILURE
);
729 t
->answer_errno
= -r
;
730 dns_transaction_complete(t
, DNS_TRANSACTION_ERRNO
);
733 static int dns_transaction_has_positive_answer(DnsTransaction
*t
, DnsAnswerFlags
*flags
) {
738 /* Checks whether the answer is positive, i.e. either a direct
739 * answer to the question, or a CNAME/DNAME for it */
741 r
= dns_answer_match_key(t
->answer
, t
->key
, flags
);
745 r
= dns_answer_find_cname_or_dname(t
->answer
, t
->key
, NULL
, flags
);
752 static int dns_transaction_fix_rcode(DnsTransaction
*t
) {
757 /* Fix up the RCODE to SUCCESS if we get at least one matching RR in a response. Note that this contradicts the
758 * DNS RFCs a bit. Specifically, RFC 6604 Section 3 clarifies that the RCODE shall say something about a
759 * CNAME/DNAME chain element coming after the last chain element contained in the message, and not the first
760 * one included. However, it also indicates that not all DNS servers implement this correctly. Moreover, when
761 * using DNSSEC we usually only can prove the first element of a CNAME/DNAME chain anyway, hence let's settle
762 * on always processing the RCODE as referring to the immediate look-up we do, i.e. the first element of a
763 * CNAME/DNAME chain. This way, we uniformly handle CNAME/DNAME chains, regardless if the DNS server
764 * incorrectly implements RCODE, whether DNSSEC is in use, or whether the DNS server only supplied us with an
765 * incomplete CNAME/DNAME chain.
767 * Or in other words: if we get at least one positive reply in a message we patch NXDOMAIN to become SUCCESS,
768 * and then rely on the CNAME chasing logic to figure out that there's actually a CNAME error with a new
771 if (t
->answer_rcode
!= DNS_RCODE_NXDOMAIN
)
774 r
= dns_transaction_has_positive_answer(t
, NULL
);
778 t
->answer_rcode
= DNS_RCODE_SUCCESS
;
782 void dns_transaction_process_reply(DnsTransaction
*t
, DnsPacket
*p
) {
789 assert(t
->scope
->manager
);
791 if (t
->state
!= DNS_TRANSACTION_PENDING
)
794 /* Note that this call might invalidate the query. Callers
795 * should hence not attempt to access the query or transaction
796 * after calling this function. */
798 log_debug("Processing incoming packet on transaction %" PRIu16
".", t
->id
);
800 switch (t
->scope
->protocol
) {
802 case DNS_PROTOCOL_LLMNR
:
803 assert(t
->scope
->link
);
805 /* For LLMNR we will not accept any packets from other
808 if (p
->ifindex
!= t
->scope
->link
->ifindex
)
811 if (p
->family
!= t
->scope
->family
)
814 /* Tentative packets are not full responses but still
815 * useful for identifying uniqueness conflicts during
817 if (DNS_PACKET_LLMNR_T(p
)) {
818 dns_transaction_tentative(t
, p
);
824 case DNS_PROTOCOL_MDNS
:
825 assert(t
->scope
->link
);
827 /* For mDNS we will not accept any packets from other interfaces */
828 if (p
->ifindex
!= t
->scope
->link
->ifindex
)
831 if (p
->family
!= t
->scope
->family
)
836 case DNS_PROTOCOL_DNS
:
837 /* Note that we do not need to verify the
838 * addresses/port numbers of incoming traffic, as we
839 * invoked connect() on our UDP socket in which case
840 * the kernel already does the needed verification for
845 assert_not_reached("Invalid DNS protocol.");
848 if (t
->received
!= p
) {
849 dns_packet_unref(t
->received
);
850 t
->received
= dns_packet_ref(p
);
853 t
->answer_source
= DNS_TRANSACTION_NETWORK
;
855 if (p
->ipproto
== IPPROTO_TCP
) {
856 if (DNS_PACKET_TC(p
)) {
857 /* Truncated via TCP? Somebody must be fucking with us */
858 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
862 if (DNS_PACKET_ID(p
) != t
->id
) {
863 /* Not the reply to our query? Somebody must be fucking with us */
864 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
869 assert_se(sd_event_now(t
->scope
->manager
->event
, clock_boottime_or_monotonic(), &ts
) >= 0);
871 switch (t
->scope
->protocol
) {
873 case DNS_PROTOCOL_DNS
:
876 if (IN_SET(DNS_PACKET_RCODE(p
), DNS_RCODE_FORMERR
, DNS_RCODE_SERVFAIL
, DNS_RCODE_NOTIMP
)) {
878 /* Request failed, immediately try again with reduced features */
879 log_debug("Server returned error: %s", dns_rcode_to_string(DNS_PACKET_RCODE(p
)));
881 dns_server_packet_failed(t
->server
, t
->current_feature_level
);
882 dns_transaction_retry(t
);
884 } else if (DNS_PACKET_TC(p
))
885 dns_server_packet_truncated(t
->server
, t
->current_feature_level
);
889 case DNS_PROTOCOL_LLMNR
:
890 case DNS_PROTOCOL_MDNS
:
891 dns_scope_packet_received(t
->scope
, ts
- t
->start_usec
);
895 assert_not_reached("Invalid DNS protocol.");
898 if (DNS_PACKET_TC(p
)) {
900 /* Truncated packets for mDNS are not allowed. Give up immediately. */
901 if (t
->scope
->protocol
== DNS_PROTOCOL_MDNS
) {
902 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
906 log_debug("Reply truncated, retrying via TCP.");
908 /* Response was truncated, let's try again with good old TCP */
909 r
= dns_transaction_open_tcp(t
);
911 /* No servers found? Damn! */
912 dns_transaction_complete(t
, DNS_TRANSACTION_NO_SERVERS
);
915 if (r
== -EOPNOTSUPP
) {
916 /* Tried to ask for DNSSEC RRs, on a server that doesn't do DNSSEC */
917 dns_transaction_complete(t
, DNS_TRANSACTION_RR_TYPE_UNSUPPORTED
);
921 /* On LLMNR, if we cannot connect to the host,
922 * we immediately give up */
923 if (t
->scope
->protocol
!= DNS_PROTOCOL_DNS
)
926 /* On DNS, couldn't send? Try immediately again, with a new server */
927 dns_transaction_retry(t
);
933 /* After the superficial checks, actually parse the message. */
934 r
= dns_packet_extract(p
);
936 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
940 /* Report that the OPT RR was missing */
943 dns_server_packet_bad_opt(t
->server
, t
->current_feature_level
);
945 dns_server_packet_received(t
->server
, p
->ipproto
, t
->current_feature_level
, ts
- t
->start_usec
, p
->size
);
948 /* See if we know things we didn't know before that indicate we better restart the lookup immediately. */
949 r
= dns_transaction_maybe_restart(t
);
952 if (r
> 0) /* Transaction got restarted... */
955 if (IN_SET(t
->scope
->protocol
, DNS_PROTOCOL_DNS
, DNS_PROTOCOL_LLMNR
)) {
957 /* Only consider responses with equivalent query section to the request */
958 r
= dns_packet_is_reply_for(p
, t
->key
);
962 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
966 /* Install the answer as answer to the transaction */
967 dns_answer_unref(t
->answer
);
968 t
->answer
= dns_answer_ref(p
->answer
);
969 t
->answer_rcode
= DNS_PACKET_RCODE(p
);
970 t
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
971 t
->answer_authenticated
= false;
973 r
= dns_transaction_fix_rcode(t
);
977 /* Block GC while starting requests for additional DNSSEC RRs */
979 r
= dns_transaction_request_dnssec_keys(t
);
982 /* Maybe the transaction is ready for GC'ing now? If so, free it and return. */
983 if (!dns_transaction_gc(t
))
986 /* Requesting additional keys might have resulted in
987 * this transaction to fail, since the auxiliary
988 * request failed for some reason. If so, we are not
989 * in pending state anymore, and we should exit
991 if (t
->state
!= DNS_TRANSACTION_PENDING
)
996 /* There are DNSSEC transactions pending now. Update the state accordingly. */
997 t
->state
= DNS_TRANSACTION_VALIDATING
;
998 dns_transaction_close_connection(t
);
999 dns_transaction_stop_timeout(t
);
1004 dns_transaction_process_dnssec(t
);
1008 t
->answer_errno
= -r
;
1009 dns_transaction_complete(t
, DNS_TRANSACTION_ERRNO
);
1012 static int on_dns_packet(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
1013 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
1014 DnsTransaction
*t
= userdata
;
1020 r
= manager_recv(t
->scope
->manager
, fd
, DNS_PROTOCOL_DNS
, &p
);
1021 if (ERRNO_IS_DISCONNECT(-r
)) {
1024 /* UDP connection failure get reported via ICMP and then are possible delivered to us on the next
1025 * recvmsg(). Treat this like a lost packet. */
1027 log_debug_errno(r
, "Connection failure for DNS UDP packet: %m");
1028 assert_se(sd_event_now(t
->scope
->manager
->event
, clock_boottime_or_monotonic(), &usec
) >= 0);
1029 dns_server_packet_lost(t
->server
, IPPROTO_UDP
, t
->current_feature_level
, usec
- t
->start_usec
);
1031 dns_transaction_retry(t
);
1035 dns_transaction_complete(t
, DNS_TRANSACTION_ERRNO
);
1036 t
->answer_errno
= -r
;
1040 r
= dns_packet_validate_reply(p
);
1042 log_debug_errno(r
, "Received invalid DNS packet as response, ignoring: %m");
1046 log_debug("Received inappropriate DNS packet as response, ignoring.");
1050 if (DNS_PACKET_ID(p
) != t
->id
) {
1051 log_debug("Received packet with incorrect transaction ID, ignoring.");
1055 dns_transaction_process_reply(t
, p
);
1059 static int dns_transaction_emit_udp(DnsTransaction
*t
) {
1064 if (t
->scope
->protocol
== DNS_PROTOCOL_DNS
) {
1066 r
= dns_transaction_pick_server(t
);
1070 if (t
->current_feature_level
< DNS_SERVER_FEATURE_LEVEL_UDP
)
1073 if (!dns_server_dnssec_supported(t
->server
) && dns_type_is_dnssec(t
->key
->type
))
1076 if (r
> 0 || t
->dns_udp_fd
< 0) { /* Server changed, or no connection yet. */
1079 dns_transaction_close_connection(t
);
1081 fd
= dns_scope_socket_udp(t
->scope
, t
->server
, 53);
1085 r
= sd_event_add_io(t
->scope
->manager
->event
, &t
->dns_udp_event_source
, fd
, EPOLLIN
, on_dns_packet
, t
);
1091 (void) sd_event_source_set_description(t
->dns_udp_event_source
, "dns-transaction-udp");
1095 r
= dns_server_adjust_opt(t
->server
, t
->sent
, t
->current_feature_level
);
1099 dns_transaction_close_connection(t
);
1101 r
= dns_scope_emit_udp(t
->scope
, t
->dns_udp_fd
, t
->sent
);
1105 dns_transaction_reset_answer(t
);
1110 static int on_transaction_timeout(sd_event_source
*s
, usec_t usec
, void *userdata
) {
1111 DnsTransaction
*t
= userdata
;
1116 if (!t
->initial_jitter_scheduled
|| t
->initial_jitter_elapsed
) {
1117 /* Timeout reached? Increase the timeout for the server used */
1118 switch (t
->scope
->protocol
) {
1120 case DNS_PROTOCOL_DNS
:
1122 dns_server_packet_lost(t
->server
, t
->stream
? IPPROTO_TCP
: IPPROTO_UDP
, t
->current_feature_level
, usec
- t
->start_usec
);
1125 case DNS_PROTOCOL_LLMNR
:
1126 case DNS_PROTOCOL_MDNS
:
1127 dns_scope_packet_lost(t
->scope
, usec
- t
->start_usec
);
1131 assert_not_reached("Invalid DNS protocol.");
1134 if (t
->initial_jitter_scheduled
)
1135 t
->initial_jitter_elapsed
= true;
1138 log_debug("Timeout reached on transaction %" PRIu16
".", t
->id
);
1140 dns_transaction_retry(t
);
1144 static usec_t
transaction_get_resend_timeout(DnsTransaction
*t
) {
1148 switch (t
->scope
->protocol
) {
1150 case DNS_PROTOCOL_DNS
:
1152 return t
->server
->resend_timeout
;
1154 case DNS_PROTOCOL_MDNS
:
1155 assert(t
->n_attempts
> 0);
1156 return (1 << (t
->n_attempts
- 1)) * USEC_PER_SEC
;
1158 case DNS_PROTOCOL_LLMNR
:
1159 return t
->scope
->resend_timeout
;
1162 assert_not_reached("Invalid DNS protocol.");
1166 static int dns_transaction_prepare(DnsTransaction
*t
, usec_t ts
) {
1171 dns_transaction_stop_timeout(t
);
1173 r
= dns_scope_network_good(t
->scope
);
1177 dns_transaction_complete(t
, DNS_TRANSACTION_NETWORK_DOWN
);
1181 if (t
->n_attempts
>= TRANSACTION_ATTEMPTS_MAX(t
->scope
->protocol
)) {
1182 dns_transaction_complete(t
, DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
);
1186 if (t
->scope
->protocol
== DNS_PROTOCOL_LLMNR
&& t
->tried_stream
) {
1187 /* If we already tried via a stream, then we don't
1188 * retry on LLMNR. See RFC 4795, Section 2.7. */
1189 dns_transaction_complete(t
, DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
);
1196 dns_transaction_reset_answer(t
);
1197 dns_transaction_flush_dnssec_transactions(t
);
1199 /* Check the trust anchor. Do so only on classic DNS, since DNSSEC does not apply otherwise. */
1200 if (t
->scope
->protocol
== DNS_PROTOCOL_DNS
) {
1201 r
= dns_trust_anchor_lookup_positive(&t
->scope
->manager
->trust_anchor
, t
->key
, &t
->answer
);
1205 t
->answer_rcode
= DNS_RCODE_SUCCESS
;
1206 t
->answer_source
= DNS_TRANSACTION_TRUST_ANCHOR
;
1207 t
->answer_authenticated
= true;
1208 dns_transaction_complete(t
, DNS_TRANSACTION_SUCCESS
);
1212 if (dns_name_is_root(DNS_RESOURCE_KEY_NAME(t
->key
)) &&
1213 t
->key
->type
== DNS_TYPE_DS
) {
1215 /* Hmm, this is a request for the root DS? A
1216 * DS RR doesn't exist in the root zone, and
1217 * if our trust anchor didn't know it either,
1218 * this means we cannot do any DNSSEC logic
1221 if (t
->scope
->dnssec_mode
== DNSSEC_ALLOW_DOWNGRADE
) {
1222 /* We are in downgrade mode. In this
1223 * case, synthesize an unsigned empty
1224 * response, so that the any lookup
1225 * depending on this one can continue
1226 * assuming there was no DS, and hence
1227 * the root zone was unsigned. */
1229 t
->answer_rcode
= DNS_RCODE_SUCCESS
;
1230 t
->answer_source
= DNS_TRANSACTION_TRUST_ANCHOR
;
1231 t
->answer_authenticated
= false;
1232 dns_transaction_complete(t
, DNS_TRANSACTION_SUCCESS
);
1234 /* If we are not in downgrade mode,
1235 * then fail the lookup, because we
1236 * cannot reasonably answer it. There
1237 * might be DS RRs, but we don't know
1238 * them, and the DNS server won't tell
1239 * them to us (and even if it would,
1240 * we couldn't validate it and trust
1242 dns_transaction_complete(t
, DNS_TRANSACTION_NO_TRUST_ANCHOR
);
1248 /* Check the zone, but only if this transaction is not used
1249 * for probing or verifying a zone item. */
1250 if (set_isempty(t
->notify_zone_items
)) {
1252 r
= dns_zone_lookup(&t
->scope
->zone
, t
->key
, &t
->answer
, NULL
, NULL
);
1256 t
->answer_rcode
= DNS_RCODE_SUCCESS
;
1257 t
->answer_source
= DNS_TRANSACTION_ZONE
;
1258 t
->answer_authenticated
= true;
1259 dns_transaction_complete(t
, DNS_TRANSACTION_SUCCESS
);
1264 /* Check the cache, but only if this transaction is not used
1265 * for probing or verifying a zone item. */
1266 if (set_isempty(t
->notify_zone_items
)) {
1268 /* Before trying the cache, let's make sure we figured out a
1269 * server to use. Should this cause a change of server this
1270 * might flush the cache. */
1271 dns_scope_get_dns_server(t
->scope
);
1273 /* Let's then prune all outdated entries */
1274 dns_cache_prune(&t
->scope
->cache
);
1276 r
= dns_cache_lookup(&t
->scope
->cache
, t
->key
, &t
->answer_rcode
, &t
->answer
, &t
->answer_authenticated
);
1280 t
->answer_source
= DNS_TRANSACTION_CACHE
;
1281 if (t
->answer_rcode
== DNS_RCODE_SUCCESS
)
1282 dns_transaction_complete(t
, DNS_TRANSACTION_SUCCESS
);
1284 dns_transaction_complete(t
, DNS_TRANSACTION_RCODE_FAILURE
);
1292 static int dns_transaction_make_packet_mdns(DnsTransaction
*t
) {
1294 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
1295 bool add_known_answers
= false;
1296 DnsTransaction
*other
;
1302 assert(t
->scope
->protocol
== DNS_PROTOCOL_MDNS
);
1304 /* Discard any previously prepared packet, so we can start over and coalesce again */
1305 t
->sent
= dns_packet_unref(t
->sent
);
1307 r
= dns_packet_new_query(&p
, t
->scope
->protocol
, 0, false);
1311 r
= dns_packet_append_key(p
, t
->key
, NULL
);
1317 if (dns_key_is_shared(t
->key
))
1318 add_known_answers
= true;
1321 * For mDNS, we want to coalesce as many open queries in pending transactions into one single
1322 * query packet on the wire as possible. To achieve that, we iterate through all pending transactions
1323 * in our current scope, and see whether their timing contraints allow them to be sent.
1326 assert_se(sd_event_now(t
->scope
->manager
->event
, clock_boottime_or_monotonic(), &ts
) >= 0);
1328 LIST_FOREACH(transactions_by_scope
, other
, t
->scope
->transactions
) {
1330 /* Skip ourselves */
1334 if (other
->state
!= DNS_TRANSACTION_PENDING
)
1337 if (other
->next_attempt_after
> ts
)
1340 if (qdcount
>= UINT16_MAX
)
1343 r
= dns_packet_append_key(p
, other
->key
, NULL
);
1346 * If we can't stuff more questions into the packet, just give up.
1347 * One of the 'other' transactions will fire later and take care of the rest.
1355 r
= dns_transaction_prepare(other
, ts
);
1359 ts
+= transaction_get_resend_timeout(other
);
1361 r
= sd_event_add_time(
1362 other
->scope
->manager
->event
,
1363 &other
->timeout_event_source
,
1364 clock_boottime_or_monotonic(),
1366 on_transaction_timeout
, other
);
1370 (void) sd_event_source_set_description(t
->timeout_event_source
, "dns-transaction-timeout");
1372 other
->state
= DNS_TRANSACTION_PENDING
;
1373 other
->next_attempt_after
= ts
;
1377 if (dns_key_is_shared(other
->key
))
1378 add_known_answers
= true;
1381 DNS_PACKET_HEADER(p
)->qdcount
= htobe16(qdcount
);
1383 /* Append known answer section if we're asking for any shared record */
1384 if (add_known_answers
) {
1385 r
= dns_cache_export_shared_to_packet(&t
->scope
->cache
, p
);
1396 static int dns_transaction_make_packet(DnsTransaction
*t
) {
1397 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
1402 if (t
->scope
->protocol
== DNS_PROTOCOL_MDNS
)
1403 return dns_transaction_make_packet_mdns(t
);
1408 r
= dns_packet_new_query(&p
, t
->scope
->protocol
, 0, t
->scope
->dnssec_mode
!= DNSSEC_NO
);
1412 r
= dns_packet_append_key(p
, t
->key
, NULL
);
1416 DNS_PACKET_HEADER(p
)->qdcount
= htobe16(1);
1417 DNS_PACKET_HEADER(p
)->id
= t
->id
;
1425 int dns_transaction_go(DnsTransaction
*t
) {
1431 assert_se(sd_event_now(t
->scope
->manager
->event
, clock_boottime_or_monotonic(), &ts
) >= 0);
1433 r
= dns_transaction_prepare(t
, ts
);
1437 log_debug("Excercising transaction %" PRIu16
" for <%s> on scope %s on %s/%s.",
1439 dns_transaction_key_string(t
),
1440 dns_protocol_to_string(t
->scope
->protocol
),
1441 t
->scope
->link
? t
->scope
->link
->name
: "*",
1442 t
->scope
->family
== AF_UNSPEC
? "*" : af_to_name(t
->scope
->family
));
1444 if (!t
->initial_jitter_scheduled
&&
1445 (t
->scope
->protocol
== DNS_PROTOCOL_LLMNR
||
1446 t
->scope
->protocol
== DNS_PROTOCOL_MDNS
)) {
1447 usec_t jitter
, accuracy
;
1449 /* RFC 4795 Section 2.7 suggests all queries should be
1450 * delayed by a random time from 0 to JITTER_INTERVAL. */
1452 t
->initial_jitter_scheduled
= true;
1454 random_bytes(&jitter
, sizeof(jitter
));
1456 switch (t
->scope
->protocol
) {
1458 case DNS_PROTOCOL_LLMNR
:
1459 jitter
%= LLMNR_JITTER_INTERVAL_USEC
;
1460 accuracy
= LLMNR_JITTER_INTERVAL_USEC
;
1463 case DNS_PROTOCOL_MDNS
:
1464 jitter
%= MDNS_JITTER_RANGE_USEC
;
1465 jitter
+= MDNS_JITTER_MIN_USEC
;
1466 accuracy
= MDNS_JITTER_RANGE_USEC
;
1469 assert_not_reached("bad protocol");
1472 r
= sd_event_add_time(
1473 t
->scope
->manager
->event
,
1474 &t
->timeout_event_source
,
1475 clock_boottime_or_monotonic(),
1476 ts
+ jitter
, accuracy
,
1477 on_transaction_timeout
, t
);
1481 (void) sd_event_source_set_description(t
->timeout_event_source
, "dns-transaction-timeout");
1484 t
->next_attempt_after
= ts
;
1485 t
->state
= DNS_TRANSACTION_PENDING
;
1487 log_debug("Delaying %s transaction for " USEC_FMT
"us.", dns_protocol_to_string(t
->scope
->protocol
), jitter
);
1491 /* Otherwise, we need to ask the network */
1492 r
= dns_transaction_make_packet(t
);
1496 if (t
->scope
->protocol
== DNS_PROTOCOL_LLMNR
&&
1497 (dns_name_endswith(DNS_RESOURCE_KEY_NAME(t
->key
), "in-addr.arpa") > 0 ||
1498 dns_name_endswith(DNS_RESOURCE_KEY_NAME(t
->key
), "ip6.arpa") > 0)) {
1500 /* RFC 4795, Section 2.4. says reverse lookups shall
1501 * always be made via TCP on LLMNR */
1502 r
= dns_transaction_open_tcp(t
);
1504 /* Try via UDP, and if that fails due to large size or lack of
1505 * support try via TCP */
1506 r
= dns_transaction_emit_udp(t
);
1508 log_debug("Sending query via TCP since it is too large.");
1510 log_debug("Sending query via TCP since server doesn't support UDP.");
1511 if (r
== -EMSGSIZE
|| r
== -EAGAIN
)
1512 r
= dns_transaction_open_tcp(t
);
1516 /* No servers to send this to? */
1517 dns_transaction_complete(t
, DNS_TRANSACTION_NO_SERVERS
);
1520 if (r
== -EOPNOTSUPP
) {
1521 /* Tried to ask for DNSSEC RRs, on a server that doesn't do DNSSEC */
1522 dns_transaction_complete(t
, DNS_TRANSACTION_RR_TYPE_UNSUPPORTED
);
1525 if (t
->scope
->protocol
== DNS_PROTOCOL_LLMNR
&& ERRNO_IS_DISCONNECT(-r
)) {
1526 /* On LLMNR, if we cannot connect to a host via TCP when doing reverse lookups. This means we cannot
1527 * answer this request with this protocol. */
1528 dns_transaction_complete(t
, DNS_TRANSACTION_NOT_FOUND
);
1532 if (t
->scope
->protocol
!= DNS_PROTOCOL_DNS
)
1535 /* Couldn't send? Try immediately again, with a new server */
1536 dns_scope_next_dns_server(t
->scope
);
1538 return dns_transaction_go(t
);
1541 ts
+= transaction_get_resend_timeout(t
);
1543 r
= sd_event_add_time(
1544 t
->scope
->manager
->event
,
1545 &t
->timeout_event_source
,
1546 clock_boottime_or_monotonic(),
1548 on_transaction_timeout
, t
);
1552 (void) sd_event_source_set_description(t
->timeout_event_source
, "dns-transaction-timeout");
1554 t
->state
= DNS_TRANSACTION_PENDING
;
1555 t
->next_attempt_after
= ts
;
1560 static int dns_transaction_find_cyclic(DnsTransaction
*t
, DnsTransaction
*aux
) {
1568 /* Try to find cyclic dependencies between transaction objects */
1573 SET_FOREACH(n
, aux
->dnssec_transactions
, i
) {
1574 r
= dns_transaction_find_cyclic(t
, n
);
1582 static int dns_transaction_add_dnssec_transaction(DnsTransaction
*t
, DnsResourceKey
*key
, DnsTransaction
**ret
) {
1583 DnsTransaction
*aux
;
1590 aux
= dns_scope_find_transaction(t
->scope
, key
, true);
1592 r
= dns_transaction_new(&aux
, t
->scope
, key
);
1596 if (set_contains(t
->dnssec_transactions
, aux
)) {
1601 r
= dns_transaction_find_cyclic(t
, aux
);
1605 log_debug("Detected potential cyclic dependency, refusing to add transaction %" PRIu16
" (%s) as dependency for %" PRIu16
" (%s).",
1607 strna(dns_transaction_key_string(aux
)),
1609 strna(dns_transaction_key_string(t
)));
1614 r
= set_ensure_allocated(&t
->dnssec_transactions
, NULL
);
1618 r
= set_ensure_allocated(&aux
->notify_transactions
, NULL
);
1622 r
= set_put(t
->dnssec_transactions
, aux
);
1626 r
= set_put(aux
->notify_transactions
, t
);
1628 (void) set_remove(t
->dnssec_transactions
, aux
);
1636 dns_transaction_gc(aux
);
1640 static int dns_transaction_request_dnssec_rr(DnsTransaction
*t
, DnsResourceKey
*key
) {
1641 _cleanup_(dns_answer_unrefp
) DnsAnswer
*a
= NULL
;
1642 DnsTransaction
*aux
;
1648 /* Try to get the data from the trust anchor */
1649 r
= dns_trust_anchor_lookup_positive(&t
->scope
->manager
->trust_anchor
, key
, &a
);
1653 r
= dns_answer_extend(&t
->validated_keys
, a
);
1660 /* This didn't work, ask for it via the network/cache then. */
1661 r
= dns_transaction_add_dnssec_transaction(t
, key
, &aux
);
1662 if (r
== -ELOOP
) /* This would result in a cyclic dependency */
1667 if (aux
->state
== DNS_TRANSACTION_NULL
) {
1668 r
= dns_transaction_go(aux
);
1676 static int dns_transaction_negative_trust_anchor_lookup(DnsTransaction
*t
, const char *name
) {
1681 /* Check whether the specified name is in the the NTA
1682 * database, either in the global one, or the link-local
1685 r
= dns_trust_anchor_lookup_negative(&t
->scope
->manager
->trust_anchor
, name
);
1689 if (!t
->scope
->link
)
1692 return set_contains(t
->scope
->link
->dnssec_negative_trust_anchors
, name
);
1695 static int dns_transaction_has_unsigned_negative_answer(DnsTransaction
*t
) {
1700 /* Checks whether the answer is negative, and lacks NSEC/NSEC3
1701 * RRs to prove it */
1703 r
= dns_transaction_has_positive_answer(t
, NULL
);
1709 /* Is this key explicitly listed as a negative trust anchor?
1710 * If so, it's nothing we need to care about */
1711 r
= dns_transaction_negative_trust_anchor_lookup(t
, DNS_RESOURCE_KEY_NAME(t
->key
));
1717 /* The answer does not contain any RRs that match to the
1718 * question. If so, let's see if there are any NSEC/NSEC3 RRs
1719 * included. If not, the answer is unsigned. */
1721 r
= dns_answer_contains_nsec_or_nsec3(t
->answer
);
1730 static int dns_transaction_is_primary_response(DnsTransaction
*t
, DnsResourceRecord
*rr
) {
1736 /* Check if the specified RR is the "primary" response,
1737 * i.e. either matches the question precisely or is a
1738 * CNAME/DNAME for it. */
1740 r
= dns_resource_key_match_rr(t
->key
, rr
, NULL
);
1744 return dns_resource_key_match_cname_or_dname(t
->key
, rr
->key
, NULL
);
1747 static bool dns_transaction_dnssec_supported(DnsTransaction
*t
) {
1750 /* Checks whether our transaction's DNS server is assumed to be compatible with DNSSEC. Returns false as soon
1751 * as we changed our mind about a server, and now believe it is incompatible with DNSSEC. */
1753 if (t
->scope
->protocol
!= DNS_PROTOCOL_DNS
)
1756 /* If we have picked no server, then we are working from the cache or some other source, and DNSSEC might well
1757 * be supported, hence return true. */
1761 if (t
->current_feature_level
< DNS_SERVER_FEATURE_LEVEL_DO
)
1764 return dns_server_dnssec_supported(t
->server
);
1767 static bool dns_transaction_dnssec_supported_full(DnsTransaction
*t
) {
1773 /* Checks whether our transaction our any of the auxiliary transactions couldn't do DNSSEC. */
1775 if (!dns_transaction_dnssec_supported(t
))
1778 SET_FOREACH(dt
, t
->dnssec_transactions
, i
)
1779 if (!dns_transaction_dnssec_supported(dt
))
1785 int dns_transaction_request_dnssec_keys(DnsTransaction
*t
) {
1786 DnsResourceRecord
*rr
;
1793 * Retrieve all auxiliary RRs for the answer we got, so that
1794 * we can verify signatures or prove that RRs are rightfully
1795 * unsigned. Specifically:
1797 * - For RRSIG we get the matching DNSKEY
1798 * - For DNSKEY we get the matching DS
1799 * - For unsigned SOA/NS we get the matching DS
1800 * - For unsigned CNAME/DNAME/DS we get the parent SOA RR
1801 * - For other unsigned RRs we get the matching SOA RR
1802 * - For SOA/NS/DS queries with no matching response RRs, and no NSEC/NSEC3, the parent's SOA RR
1803 * - For other queries with no matching response RRs, and no NSEC/NSEC3, the SOA RR
1806 if (t
->scope
->dnssec_mode
== DNSSEC_NO
)
1808 if (t
->answer_source
!= DNS_TRANSACTION_NETWORK
)
1809 return 0; /* We only need to validate stuff from the network */
1810 if (!dns_transaction_dnssec_supported(t
))
1811 return 0; /* If we can't do DNSSEC anyway there's no point in geting the auxiliary RRs */
1813 DNS_ANSWER_FOREACH(rr
, t
->answer
) {
1815 if (dns_type_is_pseudo(rr
->key
->type
))
1818 /* If this RR is in the negative trust anchor, we don't need to validate it. */
1819 r
= dns_transaction_negative_trust_anchor_lookup(t
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1825 switch (rr
->key
->type
) {
1827 case DNS_TYPE_RRSIG
: {
1828 /* For each RRSIG we request the matching DNSKEY */
1829 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*dnskey
= NULL
;
1831 /* If this RRSIG is about a DNSKEY RR and the
1832 * signer is the same as the owner, then we
1833 * already have the DNSKEY, and we don't have
1834 * to look for more. */
1835 if (rr
->rrsig
.type_covered
== DNS_TYPE_DNSKEY
) {
1836 r
= dns_name_equal(rr
->rrsig
.signer
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1843 /* If the signer is not a parent of our
1844 * original query, then this is about an
1845 * auxiliary RRset, but not anything we asked
1846 * for. In this case we aren't interested,
1847 * because we don't want to request additional
1848 * RRs for stuff we didn't really ask for, and
1849 * also to avoid request loops, where
1850 * additional RRs from one transaction result
1851 * in another transaction whose additonal RRs
1852 * point back to the original transaction, and
1854 r
= dns_name_endswith(DNS_RESOURCE_KEY_NAME(t
->key
), rr
->rrsig
.signer
);
1860 dnskey
= dns_resource_key_new(rr
->key
->class, DNS_TYPE_DNSKEY
, rr
->rrsig
.signer
);
1864 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
);
1865 r
= dns_transaction_request_dnssec_rr(t
, dnskey
);
1871 case DNS_TYPE_DNSKEY
: {
1872 /* For each DNSKEY we request the matching DS */
1873 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*ds
= NULL
;
1875 /* If the DNSKEY we are looking at is not for
1876 * zone we are interested in, nor any of its
1877 * parents, we aren't interested, and don't
1878 * request it. After all, we don't want to end
1879 * up in request loops, and want to keep
1880 * additional traffic down. */
1882 r
= dns_name_endswith(DNS_RESOURCE_KEY_NAME(t
->key
), DNS_RESOURCE_KEY_NAME(rr
->key
));
1888 ds
= dns_resource_key_new(rr
->key
->class, DNS_TYPE_DS
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1892 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));
1893 r
= dns_transaction_request_dnssec_rr(t
, ds
);
1902 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*ds
= NULL
;
1904 /* For an unsigned SOA or NS, try to acquire
1905 * the matching DS RR, as we are at a zone cut
1906 * then, and whether a DS exists tells us
1907 * whether the zone is signed. Do so only if
1908 * this RR matches our original question,
1911 r
= dns_resource_key_match_rr(t
->key
, rr
, NULL
);
1917 r
= dnssec_has_rrsig(t
->answer
, rr
->key
);
1923 ds
= dns_resource_key_new(rr
->key
->class, DNS_TYPE_DS
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1927 log_debug("Requesting DS to validate transaction %" PRIu16
" (%s, unsigned SOA/NS RRset).", t
->id
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1928 r
= dns_transaction_request_dnssec_rr(t
, ds
);
1936 case DNS_TYPE_CNAME
:
1937 case DNS_TYPE_DNAME
: {
1938 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*soa
= NULL
;
1941 /* CNAMEs and DNAMEs cannot be located at a
1942 * zone apex, hence ask for the parent SOA for
1943 * unsigned CNAME/DNAME RRs, maybe that's the
1944 * apex. But do all that only if this is
1945 * actually a response to our original
1948 * Similar for DS RRs, which are signed when
1949 * the parent SOA is signed. */
1951 r
= dns_transaction_is_primary_response(t
, rr
);
1957 r
= dnssec_has_rrsig(t
->answer
, rr
->key
);
1963 r
= dns_answer_has_dname_for_cname(t
->answer
, rr
);
1969 name
= DNS_RESOURCE_KEY_NAME(rr
->key
);
1970 r
= dns_name_parent(&name
);
1976 soa
= dns_resource_key_new(rr
->key
->class, DNS_TYPE_SOA
, name
);
1980 log_debug("Requesting parent SOA to validate transaction %" PRIu16
" (%s, unsigned CNAME/DNAME/DS RRset).", t
->id
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1981 r
= dns_transaction_request_dnssec_rr(t
, soa
);
1989 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*soa
= NULL
;
1991 /* For other unsigned RRsets (including
1992 * NSEC/NSEC3!), look for proof the zone is
1993 * unsigned, by requesting the SOA RR of the
1994 * zone. However, do so only if they are
1995 * directly relevant to our original
1998 r
= dns_transaction_is_primary_response(t
, rr
);
2004 r
= dnssec_has_rrsig(t
->answer
, rr
->key
);
2010 soa
= dns_resource_key_new(rr
->key
->class, DNS_TYPE_SOA
, DNS_RESOURCE_KEY_NAME(rr
->key
));
2014 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
));
2015 r
= dns_transaction_request_dnssec_rr(t
, soa
);
2022 /* Above, we requested everything necessary to validate what
2023 * we got. Now, let's request what we need to validate what we
2026 r
= dns_transaction_has_unsigned_negative_answer(t
);
2032 name
= DNS_RESOURCE_KEY_NAME(t
->key
);
2034 /* If this was a SOA or NS request, then this
2035 * indicates that we are not at a zone apex, hence ask
2036 * the parent name instead. If this was a DS request,
2037 * then it's signed when the parent zone is signed,
2038 * hence ask the parent in that case, too. */
2040 if (IN_SET(t
->key
->type
, DNS_TYPE_SOA
, DNS_TYPE_NS
, DNS_TYPE_DS
)) {
2041 r
= dns_name_parent(&name
);
2045 log_debug("Requesting parent SOA to validate transaction %" PRIu16
" (%s, unsigned empty SOA/NS/DS response).", t
->id
, DNS_RESOURCE_KEY_NAME(t
->key
));
2049 log_debug("Requesting SOA to validate transaction %" PRIu16
" (%s, unsigned empty non-SOA/NS/DS response).", t
->id
, DNS_RESOURCE_KEY_NAME(t
->key
));
2052 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*soa
= NULL
;
2054 soa
= dns_resource_key_new(t
->key
->class, DNS_TYPE_SOA
, name
);
2058 r
= dns_transaction_request_dnssec_rr(t
, soa
);
2064 return dns_transaction_dnssec_is_live(t
);
2067 void dns_transaction_notify(DnsTransaction
*t
, DnsTransaction
*source
) {
2071 /* Invoked whenever any of our auxiliary DNSSEC transactions completed its work. If the state is still PENDING,
2072 we are still in the loop that adds further DNSSEC transactions, hence don't check if we are ready yet. If
2073 the state is VALIDATING however, we should check if we are complete now. */
2075 if (t
->state
== DNS_TRANSACTION_VALIDATING
)
2076 dns_transaction_process_dnssec(t
);
2079 static int dns_transaction_validate_dnskey_by_ds(DnsTransaction
*t
) {
2080 DnsResourceRecord
*rr
;
2085 /* Add all DNSKEY RRs from the answer that are validated by DS
2086 * RRs from the list of validated keys to the list of
2087 * validated keys. */
2089 DNS_ANSWER_FOREACH_IFINDEX(rr
, ifindex
, t
->answer
) {
2091 r
= dnssec_verify_dnskey_by_ds_search(rr
, t
->validated_keys
);
2097 /* If so, the DNSKEY is validated too. */
2098 r
= dns_answer_add_extend(&t
->validated_keys
, rr
, ifindex
, DNS_ANSWER_AUTHENTICATED
);
2106 static int dns_transaction_requires_rrsig(DnsTransaction
*t
, DnsResourceRecord
*rr
) {
2112 /* Checks if the RR we are looking for must be signed with an
2113 * RRSIG. This is used for positive responses. */
2115 if (t
->scope
->dnssec_mode
== DNSSEC_NO
)
2118 if (dns_type_is_pseudo(rr
->key
->type
))
2121 r
= dns_transaction_negative_trust_anchor_lookup(t
, DNS_RESOURCE_KEY_NAME(rr
->key
));
2127 switch (rr
->key
->type
) {
2129 case DNS_TYPE_RRSIG
:
2130 /* RRSIGs are the signatures themselves, they need no signing. */
2138 /* For SOA or NS RRs we look for a matching DS transaction */
2140 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
2142 if (dt
->key
->class != rr
->key
->class)
2144 if (dt
->key
->type
!= DNS_TYPE_DS
)
2147 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), DNS_RESOURCE_KEY_NAME(rr
->key
));
2153 /* We found a DS transactions for the SOA/NS
2154 * RRs we are looking at. If it discovered signed DS
2155 * RRs, then we need to be signed, too. */
2157 if (!dt
->answer_authenticated
)
2160 return dns_answer_match_key(dt
->answer
, dt
->key
, NULL
);
2163 /* We found nothing that proves this is safe to leave
2164 * this unauthenticated, hence ask inist on
2165 * authentication. */
2170 case DNS_TYPE_CNAME
:
2171 case DNS_TYPE_DNAME
: {
2172 const char *parent
= NULL
;
2177 * CNAME/DNAME RRs cannot be located at a zone apex, hence look directly for the parent SOA.
2179 * DS RRs are signed if the parent is signed, hence also look at the parent SOA
2182 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
2184 if (dt
->key
->class != rr
->key
->class)
2186 if (dt
->key
->type
!= DNS_TYPE_SOA
)
2190 parent
= DNS_RESOURCE_KEY_NAME(rr
->key
);
2191 r
= dns_name_parent(&parent
);
2195 if (rr
->key
->type
== DNS_TYPE_DS
)
2198 /* A CNAME/DNAME without a parent? That's sooo weird. */
2199 log_debug("Transaction %" PRIu16
" claims CNAME/DNAME at root. Refusing.", t
->id
);
2204 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), parent
);
2210 return t
->answer_authenticated
;
2220 /* Any other kind of RR (including DNSKEY/NSEC/NSEC3). Let's see if our SOA lookup was authenticated */
2222 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
2224 if (dt
->key
->class != rr
->key
->class)
2226 if (dt
->key
->type
!= DNS_TYPE_SOA
)
2229 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), DNS_RESOURCE_KEY_NAME(rr
->key
));
2235 /* We found the transaction that was supposed to find
2236 * the SOA RR for us. It was successful, but found no
2237 * RR for us. This means we are not at a zone cut. In
2238 * this case, we require authentication if the SOA
2239 * lookup was authenticated too. */
2240 return t
->answer_authenticated
;
2247 static int dns_transaction_in_private_tld(DnsTransaction
*t
, const DnsResourceKey
*key
) {
2253 /* If DNSSEC downgrade mode is on, checks whether the
2254 * specified RR is one level below a TLD we have proven not to
2255 * exist. In such a case we assume that this is a private
2256 * domain, and permit it.
2258 * This detects cases like the Fritz!Box router networks. Each
2259 * Fritz!Box router serves a private "fritz.box" zone, in the
2260 * non-existing TLD "box". Requests for the "fritz.box" domain
2261 * are served by the router itself, while requests for the
2262 * "box" domain will result in NXDOMAIN.
2264 * Note that this logic is unable to detect cases where a
2265 * router serves a private DNS zone directly under
2266 * non-existing TLD. In such a case we cannot detect whether
2267 * the TLD is supposed to exist or not, as all requests we
2268 * make for it will be answered by the router's zone, and not
2269 * by the root zone. */
2273 if (t
->scope
->dnssec_mode
!= DNSSEC_ALLOW_DOWNGRADE
)
2274 return false; /* In strict DNSSEC mode what doesn't exist, doesn't exist */
2276 tld
= DNS_RESOURCE_KEY_NAME(key
);
2277 r
= dns_name_parent(&tld
);
2281 return false; /* Already the root domain */
2283 if (!dns_name_is_single_label(tld
))
2286 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
2288 if (dt
->key
->class != key
->class)
2291 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), tld
);
2297 /* We found an auxiliary lookup we did for the TLD. If
2298 * that returned with NXDOMAIN, we know the TLD didn't
2299 * exist, and hence this might be a private zone. */
2301 return dt
->answer_rcode
== DNS_RCODE_NXDOMAIN
;
2307 static int dns_transaction_requires_nsec(DnsTransaction
*t
) {
2315 /* Checks if we need to insist on NSEC/NSEC3 RRs for proving
2316 * this negative reply */
2318 if (t
->scope
->dnssec_mode
== DNSSEC_NO
)
2321 if (dns_type_is_pseudo(t
->key
->type
))
2324 r
= dns_transaction_negative_trust_anchor_lookup(t
, DNS_RESOURCE_KEY_NAME(t
->key
));
2330 r
= dns_transaction_in_private_tld(t
, t
->key
);
2334 /* The lookup is from a TLD that is proven not to
2335 * exist, and we are in downgrade mode, hence ignore
2336 * that fact that we didn't get any NSEC RRs.*/
2338 log_info("Detected a negative query %s in a private DNS zone, permitting unsigned response.", dns_transaction_key_string(t
));
2342 name
= DNS_RESOURCE_KEY_NAME(t
->key
);
2344 if (IN_SET(t
->key
->type
, DNS_TYPE_SOA
, DNS_TYPE_NS
, DNS_TYPE_DS
)) {
2346 /* We got a negative reply for this SOA/NS lookup? If
2347 * so, then we are not at a zone apex, and thus should
2348 * look at the result of the parent SOA lookup.
2350 * We got a negative reply for this DS lookup? DS RRs
2351 * are signed when their parent zone is signed, hence
2352 * also check the parent SOA in this case. */
2354 r
= dns_name_parent(&name
);
2361 /* For all other RRs we check the SOA on the same level to see
2362 * if it's signed. */
2364 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
2366 if (dt
->key
->class != t
->key
->class)
2368 if (dt
->key
->type
!= DNS_TYPE_SOA
)
2371 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), name
);
2377 return dt
->answer_authenticated
;
2380 /* If in doubt, require NSEC/NSEC3 */
2384 static int dns_transaction_dnskey_authenticated(DnsTransaction
*t
, DnsResourceRecord
*rr
) {
2385 DnsResourceRecord
*rrsig
;
2389 /* Checks whether any of the DNSKEYs used for the RRSIGs for
2390 * the specified RRset is authenticated (i.e. has a matching
2393 r
= dns_transaction_negative_trust_anchor_lookup(t
, DNS_RESOURCE_KEY_NAME(rr
->key
));
2399 DNS_ANSWER_FOREACH(rrsig
, t
->answer
) {
2403 r
= dnssec_key_match_rrsig(rr
->key
, rrsig
);
2409 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
2411 if (dt
->key
->class != rr
->key
->class)
2414 if (dt
->key
->type
== DNS_TYPE_DNSKEY
) {
2416 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), rrsig
->rrsig
.signer
);
2422 /* OK, we found an auxiliary DNSKEY
2423 * lookup. If that lookup is
2424 * authenticated, report this. */
2426 if (dt
->answer_authenticated
)
2431 } else if (dt
->key
->type
== DNS_TYPE_DS
) {
2433 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), rrsig
->rrsig
.signer
);
2439 /* OK, we found an auxiliary DS
2440 * lookup. If that lookup is
2441 * authenticated and non-zero, we
2444 if (!dt
->answer_authenticated
)
2447 return dns_answer_match_key(dt
->answer
, dt
->key
, NULL
);
2452 return found
? false : -ENXIO
;
2455 static int dns_transaction_known_signed(DnsTransaction
*t
, DnsResourceRecord
*rr
) {
2459 /* We know that the root domain is signed, hence if it appears
2460 * not to be signed, there's a problem with the DNS server */
2462 return rr
->key
->class == DNS_CLASS_IN
&&
2463 dns_name_is_root(DNS_RESOURCE_KEY_NAME(rr
->key
));
2466 static int dns_transaction_check_revoked_trust_anchors(DnsTransaction
*t
) {
2467 DnsResourceRecord
*rr
;
2472 /* Maybe warn the user that we encountered a revoked DNSKEY
2473 * for a key from our trust anchor. Note that we don't care
2474 * whether the DNSKEY can be authenticated or not. It's
2475 * sufficient if it is self-signed. */
2477 DNS_ANSWER_FOREACH(rr
, t
->answer
) {
2478 r
= dns_trust_anchor_check_revoked(&t
->scope
->manager
->trust_anchor
, rr
, t
->answer
);
2486 static int dns_transaction_invalidate_revoked_keys(DnsTransaction
*t
) {
2492 /* Removes all DNSKEY/DS objects from t->validated_keys that
2493 * our trust anchors database considers revoked. */
2496 DnsResourceRecord
*rr
;
2500 DNS_ANSWER_FOREACH(rr
, t
->validated_keys
) {
2501 r
= dns_trust_anchor_is_revoked(&t
->scope
->manager
->trust_anchor
, rr
);
2505 r
= dns_answer_remove_by_rr(&t
->validated_keys
, rr
);
2519 static int dns_transaction_copy_validated(DnsTransaction
*t
) {
2526 /* Copy all validated RRs from the auxiliary DNSSEC transactions into our set of validated RRs */
2528 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
2530 if (DNS_TRANSACTION_IS_LIVE(dt
->state
))
2533 if (!dt
->answer_authenticated
)
2536 r
= dns_answer_extend(&t
->validated_keys
, dt
->answer
);
2544 int dns_transaction_validate_dnssec(DnsTransaction
*t
) {
2545 _cleanup_(dns_answer_unrefp
) DnsAnswer
*validated
= NULL
;
2547 PHASE_DNSKEY
, /* Phase #1, only validate DNSKEYs */
2548 PHASE_NSEC
, /* Phase #2, only validate NSEC+NSEC3 */
2549 PHASE_ALL
, /* Phase #3, validate everything else */
2551 DnsResourceRecord
*rr
;
2552 DnsAnswerFlags flags
;
2557 /* We have now collected all DS and DNSKEY RRs in
2558 * t->validated_keys, let's see which RRs we can now
2559 * authenticate with that. */
2561 if (t
->scope
->dnssec_mode
== DNSSEC_NO
)
2564 /* Already validated */
2565 if (t
->answer_dnssec_result
!= _DNSSEC_RESULT_INVALID
)
2568 /* Our own stuff needs no validation */
2569 if (IN_SET(t
->answer_source
, DNS_TRANSACTION_ZONE
, DNS_TRANSACTION_TRUST_ANCHOR
)) {
2570 t
->answer_dnssec_result
= DNSSEC_VALIDATED
;
2571 t
->answer_authenticated
= true;
2575 /* Cached stuff is not affected by validation. */
2576 if (t
->answer_source
!= DNS_TRANSACTION_NETWORK
)
2579 if (!dns_transaction_dnssec_supported_full(t
)) {
2580 /* The server does not support DNSSEC, or doesn't augment responses with RRSIGs. */
2581 t
->answer_dnssec_result
= DNSSEC_INCOMPATIBLE_SERVER
;
2582 log_debug("Not validating response for %" PRIu16
", server lacks DNSSEC support.", t
->id
);
2586 log_debug("Validating response from transaction %" PRIu16
" (%s).", t
->id
, dns_transaction_key_string(t
));
2588 /* First, see if this response contains any revoked trust
2589 * anchors we care about */
2590 r
= dns_transaction_check_revoked_trust_anchors(t
);
2594 /* Third, copy all RRs we acquired successfully from auxiliary RRs over. */
2595 r
= dns_transaction_copy_validated(t
);
2599 /* Second, see if there are DNSKEYs we already know a
2600 * validated DS for. */
2601 r
= dns_transaction_validate_dnskey_by_ds(t
);
2605 /* Fourth, remove all DNSKEY and DS RRs again that our trust
2606 * anchor says are revoked. After all we might have marked
2607 * some keys revoked above, but they might still be lingering
2608 * in our validated_keys list. */
2609 r
= dns_transaction_invalidate_revoked_keys(t
);
2613 phase
= PHASE_DNSKEY
;
2615 bool changed
= false, have_nsec
= false;
2617 DNS_ANSWER_FOREACH(rr
, t
->answer
) {
2618 DnsResourceRecord
*rrsig
= NULL
;
2619 DnssecResult result
;
2621 switch (rr
->key
->type
) {
2623 case DNS_TYPE_RRSIG
:
2626 case DNS_TYPE_DNSKEY
:
2627 /* We validate DNSKEYs only in the DNSKEY and ALL phases */
2628 if (phase
== PHASE_NSEC
)
2633 case DNS_TYPE_NSEC3
:
2636 /* We validate NSEC/NSEC3 only in the NSEC and ALL phases */
2637 if (phase
== PHASE_DNSKEY
)
2643 /* We validate all other RRs only in the ALL phases */
2644 if (phase
!= PHASE_ALL
)
2650 r
= dnssec_verify_rrset_search(t
->answer
, rr
->key
, t
->validated_keys
, USEC_INFINITY
, &result
, &rrsig
);
2654 log_debug("Looking at %s: %s", strna(dns_resource_record_to_string(rr
)), dnssec_result_to_string(result
));
2656 if (result
== DNSSEC_VALIDATED
) {
2658 if (rr
->key
->type
== DNS_TYPE_DNSKEY
) {
2659 /* If we just validated a
2660 * DNSKEY RRset, then let's
2661 * add these keys to the set
2662 * of validated keys for this
2665 r
= dns_answer_copy_by_key(&t
->validated_keys
, t
->answer
, rr
->key
, DNS_ANSWER_AUTHENTICATED
);
2669 /* some of the DNSKEYs we just
2670 * added might already have
2671 * been revoked, remove them
2672 * again in that case. */
2673 r
= dns_transaction_invalidate_revoked_keys(t
);
2678 /* Add the validated RRset to the new
2679 * list of validated RRsets, and
2680 * remove it from the unvalidated
2681 * RRsets. We mark the RRset as
2682 * authenticated and cacheable. */
2683 r
= dns_answer_move_by_key(&validated
, &t
->answer
, rr
->key
, DNS_ANSWER_AUTHENTICATED
|DNS_ANSWER_CACHEABLE
);
2687 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_SECURE
, rr
->key
);
2689 /* Exit the loop, we dropped something from the answer, start from the beginning */
2694 /* If we haven't read all DNSKEYs yet a negative result of the validation is irrelevant, as
2695 * there might be more DNSKEYs coming. Similar, if we haven't read all NSEC/NSEC3 RRs yet, we
2696 * cannot do positive wildcard proofs yet, as those require the NSEC/NSEC3 RRs. */
2697 if (phase
!= PHASE_ALL
)
2700 if (result
== DNSSEC_VALIDATED_WILDCARD
) {
2701 bool authenticated
= false;
2704 /* This RRset validated, but as a wildcard. This means we need to prove via NSEC/NSEC3
2705 * that no matching non-wildcard RR exists.*/
2707 /* First step, determine the source of synthesis */
2708 r
= dns_resource_record_source(rrsig
, &source
);
2712 r
= dnssec_test_positive_wildcard(
2714 DNS_RESOURCE_KEY_NAME(rr
->key
),
2716 rrsig
->rrsig
.signer
,
2719 /* Unless the NSEC proof showed that the key really doesn't exist something is off. */
2721 result
= DNSSEC_INVALID
;
2723 r
= dns_answer_move_by_key(&validated
, &t
->answer
, rr
->key
, authenticated
? (DNS_ANSWER_AUTHENTICATED
|DNS_ANSWER_CACHEABLE
) : 0);
2727 manager_dnssec_verdict(t
->scope
->manager
, authenticated
? DNSSEC_SECURE
: DNSSEC_INSECURE
, rr
->key
);
2729 /* Exit the loop, we dropped something from the answer, start from the beginning */
2735 if (result
== DNSSEC_NO_SIGNATURE
) {
2736 r
= dns_transaction_requires_rrsig(t
, rr
);
2740 /* Data does not require signing. In that case, just copy it over,
2741 * but remember that this is by no means authenticated.*/
2742 r
= dns_answer_move_by_key(&validated
, &t
->answer
, rr
->key
, 0);
2746 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INSECURE
, rr
->key
);
2751 r
= dns_transaction_known_signed(t
, rr
);
2755 /* This is an RR we know has to be signed. If it isn't this means
2756 * the server is not attaching RRSIGs, hence complain. */
2758 dns_server_packet_rrsig_missing(t
->server
, t
->current_feature_level
);
2760 if (t
->scope
->dnssec_mode
== DNSSEC_ALLOW_DOWNGRADE
) {
2762 /* Downgrading is OK? If so, just consider the information unsigned */
2764 r
= dns_answer_move_by_key(&validated
, &t
->answer
, rr
->key
, 0);
2768 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INSECURE
, rr
->key
);
2773 /* Otherwise, fail */
2774 t
->answer_dnssec_result
= DNSSEC_INCOMPATIBLE_SERVER
;
2778 r
= dns_transaction_in_private_tld(t
, rr
->key
);
2782 _cleanup_free_
char *s
= NULL
;
2784 /* The data is from a TLD that is proven not to exist, and we are in downgrade
2785 * mode, hence ignore the fact that this was not signed. */
2787 (void) dns_resource_key_to_string(rr
->key
, &s
);
2788 log_info("Detected RRset %s is in a private DNS zone, permitting unsigned RRs.", strna(s
? strstrip(s
) : NULL
));
2790 r
= dns_answer_move_by_key(&validated
, &t
->answer
, rr
->key
, 0);
2794 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INSECURE
, rr
->key
);
2802 DNSSEC_SIGNATURE_EXPIRED
,
2803 DNSSEC_UNSUPPORTED_ALGORITHM
)) {
2805 r
= dns_transaction_dnskey_authenticated(t
, rr
);
2806 if (r
< 0 && r
!= -ENXIO
)
2809 /* The DNSKEY transaction was not authenticated, this means there's
2810 * no DS for this, which means it's OK if no keys are found for this signature. */
2812 r
= dns_answer_move_by_key(&validated
, &t
->answer
, rr
->key
, 0);
2816 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INSECURE
, rr
->key
);
2822 r
= dns_transaction_is_primary_response(t
, rr
);
2827 /* Look for a matching DNAME for this CNAME */
2828 r
= dns_answer_has_dname_for_cname(t
->answer
, rr
);
2832 /* Also look among the stuff we already validated */
2833 r
= dns_answer_has_dname_for_cname(validated
, rr
);
2841 DNSSEC_SIGNATURE_EXPIRED
,
2842 DNSSEC_NO_SIGNATURE
))
2843 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_BOGUS
, rr
->key
);
2844 else /* DNSSEC_MISSING_KEY or DNSSEC_UNSUPPORTED_ALGORITHM */
2845 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INDETERMINATE
, rr
->key
);
2847 /* This is a primary response to our question, and it failed validation. That's
2849 t
->answer_dnssec_result
= result
;
2853 /* This is a primary response, but we do have a DNAME RR in the RR that can replay this
2854 * CNAME, hence rely on that, and we can remove the CNAME in favour of it. */
2857 /* This is just some auxiliary data. Just remove the RRset and continue. */
2858 r
= dns_answer_remove_by_key(&t
->answer
, rr
->key
);
2862 /* Exit the loop, we dropped something from the answer, start from the beginning */
2867 /* Restart the inner loop as long as we managed to achieve something */
2871 if (phase
== PHASE_DNSKEY
&& have_nsec
) {
2872 /* OK, we processed all DNSKEYs, and there are NSEC/NSEC3 RRs, look at those now. */
2877 if (phase
!= PHASE_ALL
) {
2878 /* OK, we processed all DNSKEYs and NSEC/NSEC3 RRs, look at all the rest now. Note that in this
2879 * third phase we start to remove RRs we couldn't validate. */
2888 dns_answer_unref(t
->answer
);
2889 t
->answer
= validated
;
2892 /* At this point the answer only contains validated
2893 * RRsets. Now, let's see if it actually answers the question
2894 * we asked. If so, great! If it doesn't, then see if
2895 * NSEC/NSEC3 can prove this. */
2896 r
= dns_transaction_has_positive_answer(t
, &flags
);
2898 /* Yes, it answers the question! */
2900 if (flags
& DNS_ANSWER_AUTHENTICATED
) {
2901 /* The answer is fully authenticated, yay. */
2902 t
->answer_dnssec_result
= DNSSEC_VALIDATED
;
2903 t
->answer_rcode
= DNS_RCODE_SUCCESS
;
2904 t
->answer_authenticated
= true;
2906 /* The answer is not fully authenticated. */
2907 t
->answer_dnssec_result
= DNSSEC_UNSIGNED
;
2908 t
->answer_authenticated
= false;
2911 } else if (r
== 0) {
2912 DnssecNsecResult nr
;
2913 bool authenticated
= false;
2915 /* Bummer! Let's check NSEC/NSEC3 */
2916 r
= dnssec_nsec_test(t
->answer
, t
->key
, &nr
, &authenticated
, &t
->answer_nsec_ttl
);
2922 case DNSSEC_NSEC_NXDOMAIN
:
2923 /* NSEC proves the domain doesn't exist. Very good. */
2924 log_debug("Proved NXDOMAIN via NSEC/NSEC3 for transaction %u (%s)", t
->id
, dns_transaction_key_string(t
));
2925 t
->answer_dnssec_result
= DNSSEC_VALIDATED
;
2926 t
->answer_rcode
= DNS_RCODE_NXDOMAIN
;
2927 t
->answer_authenticated
= authenticated
;
2929 manager_dnssec_verdict(t
->scope
->manager
, authenticated
? DNSSEC_SECURE
: DNSSEC_INSECURE
, t
->key
);
2932 case DNSSEC_NSEC_NODATA
:
2933 /* NSEC proves that there's no data here, very good. */
2934 log_debug("Proved NODATA via NSEC/NSEC3 for transaction %u (%s)", t
->id
, dns_transaction_key_string(t
));
2935 t
->answer_dnssec_result
= DNSSEC_VALIDATED
;
2936 t
->answer_rcode
= DNS_RCODE_SUCCESS
;
2937 t
->answer_authenticated
= authenticated
;
2939 manager_dnssec_verdict(t
->scope
->manager
, authenticated
? DNSSEC_SECURE
: DNSSEC_INSECURE
, t
->key
);
2942 case DNSSEC_NSEC_OPTOUT
:
2943 /* NSEC3 says the data might not be signed */
2944 log_debug("Data is NSEC3 opt-out via NSEC/NSEC3 for transaction %u (%s)", t
->id
, dns_transaction_key_string(t
));
2945 t
->answer_dnssec_result
= DNSSEC_UNSIGNED
;
2946 t
->answer_authenticated
= false;
2948 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INSECURE
, t
->key
);
2951 case DNSSEC_NSEC_NO_RR
:
2952 /* No NSEC data? Bummer! */
2954 r
= dns_transaction_requires_nsec(t
);
2958 t
->answer_dnssec_result
= DNSSEC_NO_SIGNATURE
;
2959 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_BOGUS
, t
->key
);
2961 t
->answer_dnssec_result
= DNSSEC_UNSIGNED
;
2962 t
->answer_authenticated
= false;
2963 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INSECURE
, t
->key
);
2968 case DNSSEC_NSEC_UNSUPPORTED_ALGORITHM
:
2969 /* We don't know the NSEC3 algorithm used? */
2970 t
->answer_dnssec_result
= DNSSEC_UNSUPPORTED_ALGORITHM
;
2971 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INDETERMINATE
, t
->key
);
2974 case DNSSEC_NSEC_FOUND
:
2975 case DNSSEC_NSEC_CNAME
:
2976 /* NSEC says it needs to be there, but we couldn't find it? Bummer! */
2977 t
->answer_dnssec_result
= DNSSEC_NSEC_MISMATCH
;
2978 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_BOGUS
, t
->key
);
2982 assert_not_reached("Unexpected NSEC result.");
2989 const char *dns_transaction_key_string(DnsTransaction
*t
) {
2992 if (!t
->key_string
) {
2993 if (dns_resource_key_to_string(t
->key
, &t
->key_string
) < 0)
2997 return strstrip(t
->key_string
);
3000 static const char* const dns_transaction_state_table
[_DNS_TRANSACTION_STATE_MAX
] = {
3001 [DNS_TRANSACTION_NULL
] = "null",
3002 [DNS_TRANSACTION_PENDING
] = "pending",
3003 [DNS_TRANSACTION_VALIDATING
] = "validating",
3004 [DNS_TRANSACTION_RCODE_FAILURE
] = "rcode-failure",
3005 [DNS_TRANSACTION_SUCCESS
] = "success",
3006 [DNS_TRANSACTION_NO_SERVERS
] = "no-servers",
3007 [DNS_TRANSACTION_TIMEOUT
] = "timeout",
3008 [DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
] = "attempts-max-reached",
3009 [DNS_TRANSACTION_INVALID_REPLY
] = "invalid-reply",
3010 [DNS_TRANSACTION_ERRNO
] = "errno",
3011 [DNS_TRANSACTION_ABORTED
] = "aborted",
3012 [DNS_TRANSACTION_DNSSEC_FAILED
] = "dnssec-failed",
3013 [DNS_TRANSACTION_NO_TRUST_ANCHOR
] = "no-trust-anchor",
3014 [DNS_TRANSACTION_RR_TYPE_UNSUPPORTED
] = "rr-type-unsupported",
3015 [DNS_TRANSACTION_NETWORK_DOWN
] = "network-down",
3016 [DNS_TRANSACTION_NOT_FOUND
] = "not-found",
3018 DEFINE_STRING_TABLE_LOOKUP(dns_transaction_state
, DnsTransactionState
);
3020 static const char* const dns_transaction_source_table
[_DNS_TRANSACTION_SOURCE_MAX
] = {
3021 [DNS_TRANSACTION_NETWORK
] = "network",
3022 [DNS_TRANSACTION_CACHE
] = "cache",
3023 [DNS_TRANSACTION_ZONE
] = "zone",
3024 [DNS_TRANSACTION_TRUST_ANCHOR
] = "trust-anchor",
3026 DEFINE_STRING_TABLE_LOOKUP(dns_transaction_source
, DnsTransactionSource
);