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"
28 #include "random-util.h"
29 #include "resolved-dns-cache.h"
30 #include "resolved-dns-transaction.h"
31 #include "resolved-llmnr.h"
32 #include "string-table.h"
34 #define TRANSACTIONS_MAX 4096
36 static void dns_transaction_reset_answer(DnsTransaction
*t
) {
39 t
->received
= dns_packet_unref(t
->received
);
40 t
->answer
= dns_answer_unref(t
->answer
);
42 t
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
43 t
->answer_source
= _DNS_TRANSACTION_SOURCE_INVALID
;
44 t
->answer_authenticated
= false;
45 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
;
290 assert(!DNS_TRANSACTION_IS_LIVE(state
));
292 if (state
== DNS_TRANSACTION_DNSSEC_FAILED
)
293 log_struct(LOG_NOTICE
,
294 LOG_MESSAGE_ID(SD_MESSAGE_DNSSEC_FAILURE
),
295 LOG_MESSAGE("DNSSEC validation failed for question %s: %s", dns_transaction_key_string(t
), dnssec_result_to_string(t
->answer_dnssec_result
)),
296 "DNS_TRANSACTION=%" PRIu16
, t
->id
,
297 "DNS_QUESTION=%s", dns_transaction_key_string(t
),
298 "DNSSEC_RESULT=%s", dnssec_result_to_string(t
->answer_dnssec_result
),
301 /* Note that this call might invalidate the query. Callers
302 * should hence not attempt to access the query or transaction
303 * after calling this function. */
305 log_debug("Transaction %" PRIu16
" for <%s> on scope %s on %s/%s now complete with <%s> from %s (%s).",
307 dns_transaction_key_string(t
),
308 dns_protocol_to_string(t
->scope
->protocol
),
309 t
->scope
->link
? t
->scope
->link
->name
: "*",
310 t
->scope
->family
== AF_UNSPEC
? "*" : af_to_name(t
->scope
->family
),
311 dns_transaction_state_to_string(state
),
312 t
->answer_source
< 0 ? "none" : dns_transaction_source_to_string(t
->answer_source
),
313 t
->answer_authenticated
? "authenticated" : "unsigned");
317 dns_transaction_close_connection(t
);
318 dns_transaction_stop_timeout(t
);
320 /* Notify all queries that are interested, but make sure the
321 * transaction isn't freed while we are still looking at it */
324 SET_FOREACH(c
, t
->notify_query_candidates
, i
)
325 dns_query_candidate_notify(c
);
326 SET_FOREACH(z
, t
->notify_zone_items
, i
)
327 dns_zone_item_notify(z
);
329 if (!set_isempty(t
->notify_transactions
)) {
333 /* We need to be careful when notifying other
334 * transactions, as that might destroy other
335 * transactions in our list. Hence, in order to be
336 * able to safely iterate through the list of
337 * transactions, take a GC lock on all of them
338 * first. Then, in a second loop, notify them, but
339 * first unlock that specific transaction. */
341 nt
= newa(DnsTransaction
*, set_size(t
->notify_transactions
));
342 SET_FOREACH(d
, t
->notify_transactions
, i
) {
347 assert(n
== set_size(t
->notify_transactions
));
349 for (j
= 0; j
< n
; j
++) {
350 if (set_contains(t
->notify_transactions
, nt
[j
]))
351 dns_transaction_notify(nt
[j
], t
);
354 dns_transaction_gc(nt
[j
]);
359 dns_transaction_gc(t
);
362 static int dns_transaction_pick_server(DnsTransaction
*t
) {
366 assert(t
->scope
->protocol
== DNS_PROTOCOL_DNS
);
368 server
= dns_scope_get_dns_server(t
->scope
);
372 t
->current_feature_level
= dns_server_possible_feature_level(server
);
374 if (server
== t
->server
)
377 dns_server_unref(t
->server
);
378 t
->server
= dns_server_ref(server
);
383 static void dns_transaction_retry(DnsTransaction
*t
) {
388 log_debug("Retrying transaction %" PRIu16
".", t
->id
);
390 /* Before we try again, switch to a new server. */
391 dns_scope_next_dns_server(t
->scope
);
393 r
= dns_transaction_go(t
);
395 dns_transaction_complete(t
, DNS_TRANSACTION_RESOURCES
);
398 static int dns_transaction_maybe_restart(DnsTransaction
*t
) {
404 if (t
->current_feature_level
<= dns_server_possible_feature_level(t
->server
))
407 /* The server's current feature level is lower than when we sent the original query. We learnt something from
408 the response or possibly an auxiliary DNSSEC response that we didn't know before. We take that as reason to
409 restart the whole transaction. This is a good idea to deal with servers that respond rubbish if we include
410 OPT RR or DO bit. One of these cases is documented here, for example:
411 https://open.nlnetlabs.nl/pipermail/dnssec-trigger/2014-November/000376.html */
413 log_debug("Server feature level is now lower than when we began our transaction. Restarting with new ID.");
414 dns_transaction_shuffle_id(t
);
415 return dns_transaction_go(t
);
418 static int on_stream_complete(DnsStream
*s
, int error
) {
419 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
423 assert(s
->transaction
);
425 /* Copy the data we care about out of the stream before we
428 p
= dns_packet_ref(s
->read_packet
);
430 t
->stream
= dns_stream_free(t
->stream
);
432 if (ERRNO_IS_DISCONNECT(error
)) {
435 log_debug_errno(error
, "Connection failure for DNS TCP stream: %m");
436 assert_se(sd_event_now(t
->scope
->manager
->event
, clock_boottime_or_monotonic(), &usec
) >= 0);
437 dns_server_packet_lost(t
->server
, IPPROTO_TCP
, t
->current_feature_level
, usec
- t
->start_usec
);
439 dns_transaction_retry(t
);
443 dns_transaction_complete(t
, DNS_TRANSACTION_RESOURCES
);
447 if (dns_packet_validate_reply(p
) <= 0) {
448 log_debug("Invalid TCP reply packet.");
449 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
453 dns_scope_check_conflicts(t
->scope
, p
);
456 dns_transaction_process_reply(t
, p
);
459 /* If the response wasn't useful, then complete the transition
460 * now. After all, we are the worst feature set now with TCP
461 * sockets, and there's really no point in retrying. */
462 if (t
->state
== DNS_TRANSACTION_PENDING
)
463 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
465 dns_transaction_gc(t
);
470 static int dns_transaction_open_tcp(DnsTransaction
*t
) {
471 _cleanup_close_
int fd
= -1;
476 dns_transaction_close_connection(t
);
478 switch (t
->scope
->protocol
) {
480 case DNS_PROTOCOL_DNS
:
481 r
= dns_transaction_pick_server(t
);
485 if (!dns_server_dnssec_supported(t
->server
) && dns_type_is_dnssec(t
->key
->type
))
488 r
= dns_server_adjust_opt(t
->server
, t
->sent
, t
->current_feature_level
);
492 fd
= dns_scope_socket_tcp(t
->scope
, AF_UNSPEC
, NULL
, t
->server
, 53);
495 case DNS_PROTOCOL_LLMNR
:
496 /* When we already received a reply to this (but it was truncated), send to its sender address */
498 fd
= dns_scope_socket_tcp(t
->scope
, t
->received
->family
, &t
->received
->sender
, NULL
, t
->received
->sender_port
);
500 union in_addr_union address
;
501 int family
= AF_UNSPEC
;
503 /* Otherwise, try to talk to the owner of a
504 * the IP address, in case this is a reverse
507 r
= dns_name_address(DNS_RESOURCE_KEY_NAME(t
->key
), &family
, &address
);
512 if (family
!= t
->scope
->family
)
515 fd
= dns_scope_socket_tcp(t
->scope
, family
, &address
, NULL
, LLMNR_PORT
);
521 return -EAFNOSUPPORT
;
527 r
= dns_stream_new(t
->scope
->manager
, &t
->stream
, t
->scope
->protocol
, fd
);
532 r
= dns_stream_write_packet(t
->stream
, t
->sent
);
534 t
->stream
= dns_stream_free(t
->stream
);
538 t
->stream
->complete
= on_stream_complete
;
539 t
->stream
->transaction
= t
;
541 /* The interface index is difficult to determine if we are
542 * connecting to the local host, hence fill this in right away
543 * instead of determining it from the socket */
545 t
->stream
->ifindex
= t
->scope
->link
->ifindex
;
547 dns_transaction_reset_answer(t
);
549 t
->tried_stream
= true;
554 static void dns_transaction_cache_answer(DnsTransaction
*t
) {
557 /* For mDNS we cache whenever we get the packet, rather than
558 * in each transaction. */
559 if (!IN_SET(t
->scope
->protocol
, DNS_PROTOCOL_DNS
, DNS_PROTOCOL_LLMNR
))
562 /* We never cache if this packet is from the local host, under
563 * the assumption that a locally running DNS server would
564 * cache this anyway, and probably knows better when to flush
565 * the cache then we could. */
566 if (!DNS_PACKET_SHALL_CACHE(t
->received
))
569 dns_cache_put(&t
->scope
->cache
,
573 t
->answer_authenticated
,
577 &t
->received
->sender
);
580 static bool dns_transaction_dnssec_is_live(DnsTransaction
*t
) {
586 SET_FOREACH(dt
, t
->dnssec_transactions
, i
)
587 if (DNS_TRANSACTION_IS_LIVE(dt
->state
))
593 static int dns_transaction_dnssec_ready(DnsTransaction
*t
) {
599 /* Checks whether the auxiliary DNSSEC transactions of our transaction have completed, or are still
600 * ongoing. Returns 0, if we aren't ready for the DNSSEC validation, positive if we are. */
602 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
606 case DNS_TRANSACTION_NULL
:
607 case DNS_TRANSACTION_PENDING
:
608 case DNS_TRANSACTION_VALIDATING
:
612 case DNS_TRANSACTION_RCODE_FAILURE
:
613 if (dt
->answer_rcode
!= DNS_RCODE_NXDOMAIN
) {
614 log_debug("Auxiliary DNSSEC RR query failed with rcode=%s.", dns_rcode_to_string(dt
->answer_rcode
));
618 /* Fall-through: NXDOMAIN is good enough for us. This is because some DNS servers erronously
619 * return NXDOMAIN for empty non-terminals (Akamai...), and we need to handle that nicely, when
620 * asking for parent SOA or similar RRs to make unsigned proofs. */
622 case DNS_TRANSACTION_SUCCESS
:
626 case DNS_TRANSACTION_DNSSEC_FAILED
:
627 /* We handle DNSSEC failures different from other errors, as we care about the DNSSEC
628 * validationr result */
630 log_debug("Auxiliary DNSSEC RR query failed validation: %s", dnssec_result_to_string(dt
->answer_dnssec_result
));
631 t
->answer_dnssec_result
= dt
->answer_dnssec_result
; /* Copy error code over */
632 dns_transaction_complete(t
, DNS_TRANSACTION_DNSSEC_FAILED
);
637 log_debug("Auxiliary DNSSEC RR query failed with %s", dns_transaction_state_to_string(dt
->state
));
642 /* All is ready, we can go and validate */
646 t
->answer_dnssec_result
= DNSSEC_FAILED_AUXILIARY
;
647 dns_transaction_complete(t
, DNS_TRANSACTION_DNSSEC_FAILED
);
651 static void dns_transaction_process_dnssec(DnsTransaction
*t
) {
656 /* Are there ongoing DNSSEC transactions? If so, let's wait for them. */
657 r
= dns_transaction_dnssec_ready(t
);
659 dns_transaction_complete(t
, DNS_TRANSACTION_RESOURCES
);
662 if (r
== 0) /* We aren't ready yet (or one of our auxiliary transactions failed, and we shouldn't validate now */
665 /* See if we learnt things from the additional DNSSEC transactions, that we didn't know before, and better
666 * restart the lookup immediately. */
667 r
= dns_transaction_maybe_restart(t
);
669 dns_transaction_complete(t
, DNS_TRANSACTION_RESOURCES
);
672 if (r
> 0) /* Transaction got restarted... */
675 /* All our auxiliary DNSSEC transactions are complete now. Try
676 * to validate our RRset now. */
677 r
= dns_transaction_validate_dnssec(t
);
679 dns_transaction_complete(t
, DNS_TRANSACTION_RESOURCES
);
683 if (t
->answer_dnssec_result
== DNSSEC_INCOMPATIBLE_SERVER
&&
684 t
->scope
->dnssec_mode
== DNSSEC_YES
) {
685 /* We are not in automatic downgrade mode, and the
686 * server is bad, refuse operation. */
687 dns_transaction_complete(t
, DNS_TRANSACTION_DNSSEC_FAILED
);
691 if (!IN_SET(t
->answer_dnssec_result
,
692 _DNSSEC_RESULT_INVALID
, /* No DNSSEC validation enabled */
693 DNSSEC_VALIDATED
, /* Answer is signed and validated successfully */
694 DNSSEC_UNSIGNED
, /* Answer is right-fully unsigned */
695 DNSSEC_INCOMPATIBLE_SERVER
)) { /* Server does not do DNSSEC (Yay, we are downgrade attack vulnerable!) */
696 dns_transaction_complete(t
, DNS_TRANSACTION_DNSSEC_FAILED
);
700 dns_transaction_cache_answer(t
);
702 if (t
->answer_rcode
== DNS_RCODE_SUCCESS
)
703 dns_transaction_complete(t
, DNS_TRANSACTION_SUCCESS
);
705 dns_transaction_complete(t
, DNS_TRANSACTION_RCODE_FAILURE
);
708 void dns_transaction_process_reply(DnsTransaction
*t
, DnsPacket
*p
) {
715 assert(t
->scope
->manager
);
717 if (t
->state
!= DNS_TRANSACTION_PENDING
)
720 /* Note that this call might invalidate the query. Callers
721 * should hence not attempt to access the query or transaction
722 * after calling this function. */
724 log_debug("Processing incoming packet on transaction %" PRIu16
".", t
->id
);
726 switch (t
->scope
->protocol
) {
728 case DNS_PROTOCOL_LLMNR
:
729 assert(t
->scope
->link
);
731 /* For LLMNR we will not accept any packets from other
734 if (p
->ifindex
!= t
->scope
->link
->ifindex
)
737 if (p
->family
!= t
->scope
->family
)
740 /* Tentative packets are not full responses but still
741 * useful for identifying uniqueness conflicts during
743 if (DNS_PACKET_LLMNR_T(p
)) {
744 dns_transaction_tentative(t
, p
);
750 case DNS_PROTOCOL_MDNS
:
751 assert(t
->scope
->link
);
753 /* For mDNS we will not accept any packets from other interfaces */
754 if (p
->ifindex
!= t
->scope
->link
->ifindex
)
757 if (p
->family
!= t
->scope
->family
)
762 case DNS_PROTOCOL_DNS
:
763 /* Note that we do not need to verify the
764 * addresses/port numbers of incoming traffic, as we
765 * invoked connect() on our UDP socket in which case
766 * the kernel already does the needed verification for
771 assert_not_reached("Invalid DNS protocol.");
774 if (t
->received
!= p
) {
775 dns_packet_unref(t
->received
);
776 t
->received
= dns_packet_ref(p
);
779 t
->answer_source
= DNS_TRANSACTION_NETWORK
;
781 if (p
->ipproto
== IPPROTO_TCP
) {
782 if (DNS_PACKET_TC(p
)) {
783 /* Truncated via TCP? Somebody must be fucking with us */
784 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
788 if (DNS_PACKET_ID(p
) != t
->id
) {
789 /* Not the reply to our query? Somebody must be fucking with us */
790 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
795 assert_se(sd_event_now(t
->scope
->manager
->event
, clock_boottime_or_monotonic(), &ts
) >= 0);
797 switch (t
->scope
->protocol
) {
799 case DNS_PROTOCOL_DNS
:
802 if (IN_SET(DNS_PACKET_RCODE(p
), DNS_RCODE_FORMERR
, DNS_RCODE_SERVFAIL
, DNS_RCODE_NOTIMP
)) {
804 /* Request failed, immediately try again with reduced features */
805 log_debug("Server returned error: %s", dns_rcode_to_string(DNS_PACKET_RCODE(p
)));
807 dns_server_packet_failed(t
->server
, t
->current_feature_level
);
808 dns_transaction_retry(t
);
810 } else if (DNS_PACKET_TC(p
))
811 dns_server_packet_truncated(t
->server
, t
->current_feature_level
);
815 case DNS_PROTOCOL_LLMNR
:
816 case DNS_PROTOCOL_MDNS
:
817 dns_scope_packet_received(t
->scope
, ts
- t
->start_usec
);
821 assert_not_reached("Invalid DNS protocol.");
824 if (DNS_PACKET_TC(p
)) {
826 /* Truncated packets for mDNS are not allowed. Give up immediately. */
827 if (t
->scope
->protocol
== DNS_PROTOCOL_MDNS
) {
828 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
832 log_debug("Reply truncated, retrying via TCP.");
834 /* Response was truncated, let's try again with good old TCP */
835 r
= dns_transaction_open_tcp(t
);
837 /* No servers found? Damn! */
838 dns_transaction_complete(t
, DNS_TRANSACTION_NO_SERVERS
);
841 if (r
== -EOPNOTSUPP
) {
842 /* Tried to ask for DNSSEC RRs, on a server that doesn't do DNSSEC */
843 dns_transaction_complete(t
, DNS_TRANSACTION_RR_TYPE_UNSUPPORTED
);
847 /* On LLMNR, if we cannot connect to the host,
848 * we immediately give up */
849 if (t
->scope
->protocol
!= DNS_PROTOCOL_DNS
) {
850 dns_transaction_complete(t
, DNS_TRANSACTION_RESOURCES
);
854 /* On DNS, couldn't send? Try immediately again, with a new server */
855 dns_transaction_retry(t
);
861 /* After the superficial checks, actually parse the message. */
862 r
= dns_packet_extract(p
);
864 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
868 /* Report that the OPT RR was missing */
871 dns_server_packet_bad_opt(t
->server
, t
->current_feature_level
);
873 dns_server_packet_received(t
->server
, p
->ipproto
, t
->current_feature_level
, ts
- t
->start_usec
, p
->size
);
876 /* See if we know things we didn't know before that indicate we better restart the lookup immediately. */
877 r
= dns_transaction_maybe_restart(t
);
879 dns_transaction_complete(t
, DNS_TRANSACTION_RESOURCES
);
882 if (r
> 0) /* Transaction got restarted... */
885 if (IN_SET(t
->scope
->protocol
, DNS_PROTOCOL_DNS
, DNS_PROTOCOL_LLMNR
)) {
887 /* Only consider responses with equivalent query section to the request */
888 r
= dns_packet_is_reply_for(p
, t
->key
);
890 dns_transaction_complete(t
, DNS_TRANSACTION_RESOURCES
);
894 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
898 /* Install the answer as answer to the transaction */
899 dns_answer_unref(t
->answer
);
900 t
->answer
= dns_answer_ref(p
->answer
);
901 t
->answer_rcode
= DNS_PACKET_RCODE(p
);
902 t
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
903 t
->answer_authenticated
= false;
905 /* Block GC while starting requests for additional DNSSEC RRs */
907 r
= dns_transaction_request_dnssec_keys(t
);
910 /* Maybe the transaction is ready for GC'ing now? If so, free it and return. */
911 if (!dns_transaction_gc(t
))
914 /* Requesting additional keys might have resulted in
915 * this transaction to fail, since the auxiliary
916 * request failed for some reason. If so, we are not
917 * in pending state anymore, and we should exit
919 if (t
->state
!= DNS_TRANSACTION_PENDING
)
922 dns_transaction_complete(t
, DNS_TRANSACTION_RESOURCES
);
926 /* There are DNSSEC transactions pending now. Update the state accordingly. */
927 t
->state
= DNS_TRANSACTION_VALIDATING
;
928 dns_transaction_close_connection(t
);
929 dns_transaction_stop_timeout(t
);
934 dns_transaction_process_dnssec(t
);
937 static int on_dns_packet(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
938 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
939 DnsTransaction
*t
= userdata
;
945 r
= manager_recv(t
->scope
->manager
, fd
, DNS_PROTOCOL_DNS
, &p
);
946 if (ERRNO_IS_DISCONNECT(-r
)) {
949 /* UDP connection failure get reported via ICMP and then are possible delivered to us on the next
950 * recvmsg(). Treat this like a lost packet. */
952 log_debug_errno(r
, "Connection failure for DNS UDP packet: %m");
953 assert_se(sd_event_now(t
->scope
->manager
->event
, clock_boottime_or_monotonic(), &usec
) >= 0);
954 dns_server_packet_lost(t
->server
, IPPROTO_UDP
, t
->current_feature_level
, usec
- t
->start_usec
);
956 dns_transaction_retry(t
);
960 dns_transaction_complete(t
, DNS_TRANSACTION_RESOURCES
);
964 r
= dns_packet_validate_reply(p
);
966 log_debug_errno(r
, "Received invalid DNS packet as response, ignoring: %m");
970 log_debug("Received inappropriate DNS packet as response, ignoring.");
974 if (DNS_PACKET_ID(p
) != t
->id
) {
975 log_debug("Received packet with incorrect transaction ID, ignoring.");
979 dns_transaction_process_reply(t
, p
);
983 static int dns_transaction_emit_udp(DnsTransaction
*t
) {
988 if (t
->scope
->protocol
== DNS_PROTOCOL_DNS
) {
990 r
= dns_transaction_pick_server(t
);
994 if (t
->current_feature_level
< DNS_SERVER_FEATURE_LEVEL_UDP
)
997 if (!dns_server_dnssec_supported(t
->server
) && dns_type_is_dnssec(t
->key
->type
))
1000 if (r
> 0 || t
->dns_udp_fd
< 0) { /* Server changed, or no connection yet. */
1003 dns_transaction_close_connection(t
);
1005 fd
= dns_scope_socket_udp(t
->scope
, t
->server
, 53);
1009 r
= sd_event_add_io(t
->scope
->manager
->event
, &t
->dns_udp_event_source
, fd
, EPOLLIN
, on_dns_packet
, t
);
1015 (void) sd_event_source_set_description(t
->dns_udp_event_source
, "dns-transaction-udp");
1019 r
= dns_server_adjust_opt(t
->server
, t
->sent
, t
->current_feature_level
);
1023 dns_transaction_close_connection(t
);
1025 r
= dns_scope_emit_udp(t
->scope
, t
->dns_udp_fd
, t
->sent
);
1029 dns_transaction_reset_answer(t
);
1034 static int on_transaction_timeout(sd_event_source
*s
, usec_t usec
, void *userdata
) {
1035 DnsTransaction
*t
= userdata
;
1040 if (!t
->initial_jitter_scheduled
|| t
->initial_jitter_elapsed
) {
1041 /* Timeout reached? Increase the timeout for the server used */
1042 switch (t
->scope
->protocol
) {
1044 case DNS_PROTOCOL_DNS
:
1046 dns_server_packet_lost(t
->server
, t
->stream
? IPPROTO_TCP
: IPPROTO_UDP
, t
->current_feature_level
, usec
- t
->start_usec
);
1049 case DNS_PROTOCOL_LLMNR
:
1050 case DNS_PROTOCOL_MDNS
:
1051 dns_scope_packet_lost(t
->scope
, usec
- t
->start_usec
);
1055 assert_not_reached("Invalid DNS protocol.");
1058 if (t
->initial_jitter_scheduled
)
1059 t
->initial_jitter_elapsed
= true;
1062 log_debug("Timeout reached on transaction %" PRIu16
".", t
->id
);
1064 dns_transaction_retry(t
);
1068 static usec_t
transaction_get_resend_timeout(DnsTransaction
*t
) {
1072 switch (t
->scope
->protocol
) {
1074 case DNS_PROTOCOL_DNS
:
1076 return t
->server
->resend_timeout
;
1078 case DNS_PROTOCOL_MDNS
:
1079 assert(t
->n_attempts
> 0);
1080 return (1 << (t
->n_attempts
- 1)) * USEC_PER_SEC
;
1082 case DNS_PROTOCOL_LLMNR
:
1083 return t
->scope
->resend_timeout
;
1086 assert_not_reached("Invalid DNS protocol.");
1090 static int dns_transaction_prepare(DnsTransaction
*t
, usec_t ts
) {
1095 dns_transaction_stop_timeout(t
);
1097 r
= dns_scope_network_good(t
->scope
);
1101 dns_transaction_complete(t
, DNS_TRANSACTION_NETWORK_DOWN
);
1105 if (t
->n_attempts
>= TRANSACTION_ATTEMPTS_MAX(t
->scope
->protocol
)) {
1106 dns_transaction_complete(t
, DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
);
1110 if (t
->scope
->protocol
== DNS_PROTOCOL_LLMNR
&& t
->tried_stream
) {
1111 /* If we already tried via a stream, then we don't
1112 * retry on LLMNR. See RFC 4795, Section 2.7. */
1113 dns_transaction_complete(t
, DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
);
1120 dns_transaction_reset_answer(t
);
1121 dns_transaction_flush_dnssec_transactions(t
);
1123 /* Check the trust anchor. Do so only on classic DNS, since DNSSEC does not apply otherwise. */
1124 if (t
->scope
->protocol
== DNS_PROTOCOL_DNS
) {
1125 r
= dns_trust_anchor_lookup_positive(&t
->scope
->manager
->trust_anchor
, t
->key
, &t
->answer
);
1129 t
->answer_rcode
= DNS_RCODE_SUCCESS
;
1130 t
->answer_source
= DNS_TRANSACTION_TRUST_ANCHOR
;
1131 t
->answer_authenticated
= true;
1132 dns_transaction_complete(t
, DNS_TRANSACTION_SUCCESS
);
1136 if (dns_name_is_root(DNS_RESOURCE_KEY_NAME(t
->key
)) &&
1137 t
->key
->type
== DNS_TYPE_DS
) {
1139 /* Hmm, this is a request for the root DS? A
1140 * DS RR doesn't exist in the root zone, and
1141 * if our trust anchor didn't know it either,
1142 * this means we cannot do any DNSSEC logic
1145 if (t
->scope
->dnssec_mode
== DNSSEC_ALLOW_DOWNGRADE
) {
1146 /* We are in downgrade mode. In this
1147 * case, synthesize an unsigned empty
1148 * response, so that the any lookup
1149 * depending on this one can continue
1150 * assuming there was no DS, and hence
1151 * the root zone was unsigned. */
1153 t
->answer_rcode
= DNS_RCODE_SUCCESS
;
1154 t
->answer_source
= DNS_TRANSACTION_TRUST_ANCHOR
;
1155 t
->answer_authenticated
= false;
1156 dns_transaction_complete(t
, DNS_TRANSACTION_SUCCESS
);
1158 /* If we are not in downgrade mode,
1159 * then fail the lookup, because we
1160 * cannot reasonably answer it. There
1161 * might be DS RRs, but we don't know
1162 * them, and the DNS server won't tell
1163 * them to us (and even if it would,
1164 * we couldn't validate it and trust
1166 dns_transaction_complete(t
, DNS_TRANSACTION_NO_TRUST_ANCHOR
);
1172 /* Check the zone, but only if this transaction is not used
1173 * for probing or verifying a zone item. */
1174 if (set_isempty(t
->notify_zone_items
)) {
1176 r
= dns_zone_lookup(&t
->scope
->zone
, t
->key
, &t
->answer
, NULL
, NULL
);
1180 t
->answer_rcode
= DNS_RCODE_SUCCESS
;
1181 t
->answer_source
= DNS_TRANSACTION_ZONE
;
1182 t
->answer_authenticated
= true;
1183 dns_transaction_complete(t
, DNS_TRANSACTION_SUCCESS
);
1188 /* Check the cache, but only if this transaction is not used
1189 * for probing or verifying a zone item. */
1190 if (set_isempty(t
->notify_zone_items
)) {
1192 /* Before trying the cache, let's make sure we figured out a
1193 * server to use. Should this cause a change of server this
1194 * might flush the cache. */
1195 dns_scope_get_dns_server(t
->scope
);
1197 /* Let's then prune all outdated entries */
1198 dns_cache_prune(&t
->scope
->cache
);
1200 r
= dns_cache_lookup(&t
->scope
->cache
, t
->key
, &t
->answer_rcode
, &t
->answer
, &t
->answer_authenticated
);
1204 t
->answer_source
= DNS_TRANSACTION_CACHE
;
1205 if (t
->answer_rcode
== DNS_RCODE_SUCCESS
)
1206 dns_transaction_complete(t
, DNS_TRANSACTION_SUCCESS
);
1208 dns_transaction_complete(t
, DNS_TRANSACTION_RCODE_FAILURE
);
1216 static int dns_transaction_make_packet_mdns(DnsTransaction
*t
) {
1218 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
1219 bool add_known_answers
= false;
1220 DnsTransaction
*other
;
1226 assert(t
->scope
->protocol
== DNS_PROTOCOL_MDNS
);
1228 /* Discard any previously prepared packet, so we can start over and coalesce again */
1229 t
->sent
= dns_packet_unref(t
->sent
);
1231 r
= dns_packet_new_query(&p
, t
->scope
->protocol
, 0, false);
1235 r
= dns_packet_append_key(p
, t
->key
, NULL
);
1241 if (dns_key_is_shared(t
->key
))
1242 add_known_answers
= true;
1245 * For mDNS, we want to coalesce as many open queries in pending transactions into one single
1246 * query packet on the wire as possible. To achieve that, we iterate through all pending transactions
1247 * in our current scope, and see whether their timing contraints allow them to be sent.
1250 assert_se(sd_event_now(t
->scope
->manager
->event
, clock_boottime_or_monotonic(), &ts
) >= 0);
1252 LIST_FOREACH(transactions_by_scope
, other
, t
->scope
->transactions
) {
1254 /* Skip ourselves */
1258 if (other
->state
!= DNS_TRANSACTION_PENDING
)
1261 if (other
->next_attempt_after
> ts
)
1264 if (qdcount
>= UINT16_MAX
)
1267 r
= dns_packet_append_key(p
, other
->key
, NULL
);
1270 * If we can't stuff more questions into the packet, just give up.
1271 * One of the 'other' transactions will fire later and take care of the rest.
1279 r
= dns_transaction_prepare(other
, ts
);
1283 ts
+= transaction_get_resend_timeout(other
);
1285 r
= sd_event_add_time(
1286 other
->scope
->manager
->event
,
1287 &other
->timeout_event_source
,
1288 clock_boottime_or_monotonic(),
1290 on_transaction_timeout
, other
);
1294 (void) sd_event_source_set_description(t
->timeout_event_source
, "dns-transaction-timeout");
1296 other
->state
= DNS_TRANSACTION_PENDING
;
1297 other
->next_attempt_after
= ts
;
1301 if (dns_key_is_shared(other
->key
))
1302 add_known_answers
= true;
1305 DNS_PACKET_HEADER(p
)->qdcount
= htobe16(qdcount
);
1307 /* Append known answer section if we're asking for any shared record */
1308 if (add_known_answers
) {
1309 r
= dns_cache_export_shared_to_packet(&t
->scope
->cache
, p
);
1320 static int dns_transaction_make_packet(DnsTransaction
*t
) {
1321 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
1326 if (t
->scope
->protocol
== DNS_PROTOCOL_MDNS
)
1327 return dns_transaction_make_packet_mdns(t
);
1332 r
= dns_packet_new_query(&p
, t
->scope
->protocol
, 0, t
->scope
->dnssec_mode
!= DNSSEC_NO
);
1336 r
= dns_scope_good_key(t
->scope
, t
->key
);
1342 r
= dns_packet_append_key(p
, t
->key
, NULL
);
1346 DNS_PACKET_HEADER(p
)->qdcount
= htobe16(1);
1347 DNS_PACKET_HEADER(p
)->id
= t
->id
;
1355 int dns_transaction_go(DnsTransaction
*t
) {
1361 assert_se(sd_event_now(t
->scope
->manager
->event
, clock_boottime_or_monotonic(), &ts
) >= 0);
1363 r
= dns_transaction_prepare(t
, ts
);
1367 log_debug("Excercising transaction %" PRIu16
" for <%s> on scope %s on %s/%s.",
1369 dns_transaction_key_string(t
),
1370 dns_protocol_to_string(t
->scope
->protocol
),
1371 t
->scope
->link
? t
->scope
->link
->name
: "*",
1372 t
->scope
->family
== AF_UNSPEC
? "*" : af_to_name(t
->scope
->family
));
1374 if (!t
->initial_jitter_scheduled
&&
1375 (t
->scope
->protocol
== DNS_PROTOCOL_LLMNR
||
1376 t
->scope
->protocol
== DNS_PROTOCOL_MDNS
)) {
1377 usec_t jitter
, accuracy
;
1379 /* RFC 4795 Section 2.7 suggests all queries should be
1380 * delayed by a random time from 0 to JITTER_INTERVAL. */
1382 t
->initial_jitter_scheduled
= true;
1384 random_bytes(&jitter
, sizeof(jitter
));
1386 switch (t
->scope
->protocol
) {
1388 case DNS_PROTOCOL_LLMNR
:
1389 jitter
%= LLMNR_JITTER_INTERVAL_USEC
;
1390 accuracy
= LLMNR_JITTER_INTERVAL_USEC
;
1393 case DNS_PROTOCOL_MDNS
:
1394 jitter
%= MDNS_JITTER_RANGE_USEC
;
1395 jitter
+= MDNS_JITTER_MIN_USEC
;
1396 accuracy
= MDNS_JITTER_RANGE_USEC
;
1399 assert_not_reached("bad protocol");
1402 r
= sd_event_add_time(
1403 t
->scope
->manager
->event
,
1404 &t
->timeout_event_source
,
1405 clock_boottime_or_monotonic(),
1406 ts
+ jitter
, accuracy
,
1407 on_transaction_timeout
, t
);
1411 (void) sd_event_source_set_description(t
->timeout_event_source
, "dns-transaction-timeout");
1414 t
->next_attempt_after
= ts
;
1415 t
->state
= DNS_TRANSACTION_PENDING
;
1417 log_debug("Delaying %s transaction for " USEC_FMT
"us.", dns_protocol_to_string(t
->scope
->protocol
), jitter
);
1421 /* Otherwise, we need to ask the network */
1422 r
= dns_transaction_make_packet(t
);
1424 /* Not the right request to make on this network?
1425 * (i.e. an A request made on IPv6 or an AAAA request
1426 * made on IPv4, on LLMNR or mDNS.) */
1427 dns_transaction_complete(t
, DNS_TRANSACTION_NO_SERVERS
);
1433 if (t
->scope
->protocol
== DNS_PROTOCOL_LLMNR
&&
1434 (dns_name_endswith(DNS_RESOURCE_KEY_NAME(t
->key
), "in-addr.arpa") > 0 ||
1435 dns_name_endswith(DNS_RESOURCE_KEY_NAME(t
->key
), "ip6.arpa") > 0)) {
1437 /* RFC 4795, Section 2.4. says reverse lookups shall
1438 * always be made via TCP on LLMNR */
1439 r
= dns_transaction_open_tcp(t
);
1441 /* Try via UDP, and if that fails due to large size or lack of
1442 * support try via TCP */
1443 r
= dns_transaction_emit_udp(t
);
1445 log_debug("Sending query via TCP since it is too large.");
1447 log_debug("Sending query via TCP since server doesn't support UDP.");
1448 if (r
== -EMSGSIZE
|| r
== -EAGAIN
)
1449 r
= dns_transaction_open_tcp(t
);
1453 /* No servers to send this to? */
1454 dns_transaction_complete(t
, DNS_TRANSACTION_NO_SERVERS
);
1457 if (r
== -EOPNOTSUPP
) {
1458 /* Tried to ask for DNSSEC RRs, on a server that doesn't do DNSSEC */
1459 dns_transaction_complete(t
, DNS_TRANSACTION_RR_TYPE_UNSUPPORTED
);
1463 if (t
->scope
->protocol
!= DNS_PROTOCOL_DNS
) {
1464 dns_transaction_complete(t
, DNS_TRANSACTION_RESOURCES
);
1468 /* Couldn't send? Try immediately again, with a new server */
1469 dns_scope_next_dns_server(t
->scope
);
1471 return dns_transaction_go(t
);
1474 ts
+= transaction_get_resend_timeout(t
);
1476 r
= sd_event_add_time(
1477 t
->scope
->manager
->event
,
1478 &t
->timeout_event_source
,
1479 clock_boottime_or_monotonic(),
1481 on_transaction_timeout
, t
);
1485 (void) sd_event_source_set_description(t
->timeout_event_source
, "dns-transaction-timeout");
1487 t
->state
= DNS_TRANSACTION_PENDING
;
1488 t
->next_attempt_after
= ts
;
1493 static int dns_transaction_find_cyclic(DnsTransaction
*t
, DnsTransaction
*aux
) {
1501 /* Try to find cyclic dependencies between transaction objects */
1506 SET_FOREACH(n
, aux
->dnssec_transactions
, i
) {
1507 r
= dns_transaction_find_cyclic(t
, n
);
1515 static int dns_transaction_add_dnssec_transaction(DnsTransaction
*t
, DnsResourceKey
*key
, DnsTransaction
**ret
) {
1516 DnsTransaction
*aux
;
1523 aux
= dns_scope_find_transaction(t
->scope
, key
, true);
1525 r
= dns_transaction_new(&aux
, t
->scope
, key
);
1529 if (set_contains(t
->dnssec_transactions
, aux
)) {
1534 r
= dns_transaction_find_cyclic(t
, aux
);
1538 log_debug("Detected potential cyclic dependency, refusing to add transaction %" PRIu16
" (%s) as dependency for %" PRIu16
" (%s).",
1540 strna(dns_transaction_key_string(aux
)),
1542 strna(dns_transaction_key_string(t
)));
1547 r
= set_ensure_allocated(&t
->dnssec_transactions
, NULL
);
1551 r
= set_ensure_allocated(&aux
->notify_transactions
, NULL
);
1555 r
= set_put(t
->dnssec_transactions
, aux
);
1559 r
= set_put(aux
->notify_transactions
, t
);
1561 (void) set_remove(t
->dnssec_transactions
, aux
);
1569 dns_transaction_gc(aux
);
1573 static int dns_transaction_request_dnssec_rr(DnsTransaction
*t
, DnsResourceKey
*key
) {
1574 _cleanup_(dns_answer_unrefp
) DnsAnswer
*a
= NULL
;
1575 DnsTransaction
*aux
;
1581 /* Try to get the data from the trust anchor */
1582 r
= dns_trust_anchor_lookup_positive(&t
->scope
->manager
->trust_anchor
, key
, &a
);
1586 r
= dns_answer_extend(&t
->validated_keys
, a
);
1593 /* This didn't work, ask for it via the network/cache then. */
1594 r
= dns_transaction_add_dnssec_transaction(t
, key
, &aux
);
1595 if (r
== -ELOOP
) /* This would result in a cyclic dependency */
1600 if (aux
->state
== DNS_TRANSACTION_NULL
) {
1601 r
= dns_transaction_go(aux
);
1609 static int dns_transaction_has_positive_answer(DnsTransaction
*t
, DnsAnswerFlags
*flags
) {
1614 /* Checks whether the answer is positive, i.e. either a direct
1615 * answer to the question, or a CNAME/DNAME for it */
1617 r
= dns_answer_match_key(t
->answer
, t
->key
, flags
);
1621 r
= dns_answer_find_cname_or_dname(t
->answer
, t
->key
, NULL
, flags
);
1628 static int dns_transaction_negative_trust_anchor_lookup(DnsTransaction
*t
, const char *name
) {
1633 /* Check whether the specified name is in the the NTA
1634 * database, either in the global one, or the link-local
1637 r
= dns_trust_anchor_lookup_negative(&t
->scope
->manager
->trust_anchor
, name
);
1641 if (!t
->scope
->link
)
1644 return set_contains(t
->scope
->link
->dnssec_negative_trust_anchors
, name
);
1647 static int dns_transaction_has_unsigned_negative_answer(DnsTransaction
*t
) {
1652 /* Checks whether the answer is negative, and lacks NSEC/NSEC3
1653 * RRs to prove it */
1655 r
= dns_transaction_has_positive_answer(t
, NULL
);
1661 /* Is this key explicitly listed as a negative trust anchor?
1662 * If so, it's nothing we need to care about */
1663 r
= dns_transaction_negative_trust_anchor_lookup(t
, DNS_RESOURCE_KEY_NAME(t
->key
));
1669 /* The answer does not contain any RRs that match to the
1670 * question. If so, let's see if there are any NSEC/NSEC3 RRs
1671 * included. If not, the answer is unsigned. */
1673 r
= dns_answer_contains_nsec_or_nsec3(t
->answer
);
1682 static int dns_transaction_is_primary_response(DnsTransaction
*t
, DnsResourceRecord
*rr
) {
1688 /* Check if the specified RR is the "primary" response,
1689 * i.e. either matches the question precisely or is a
1690 * CNAME/DNAME for it, or is any kind of NSEC/NSEC3 RR */
1692 r
= dns_resource_key_match_rr(t
->key
, rr
, NULL
);
1696 r
= dns_resource_key_match_cname_or_dname(t
->key
, rr
->key
, NULL
);
1700 if (rr
->key
->type
== DNS_TYPE_NSEC3
) {
1703 p
= DNS_RESOURCE_KEY_NAME(rr
->key
);
1704 r
= dns_name_parent(&p
);
1708 r
= dns_name_endswith(DNS_RESOURCE_KEY_NAME(t
->key
), p
);
1716 return rr
->key
->type
== DNS_TYPE_NSEC
;
1719 static bool dns_transaction_dnssec_supported(DnsTransaction
*t
) {
1722 /* Checks whether our transaction's DNS server is assumed to be compatible with DNSSEC. Returns false as soon
1723 * as we changed our mind about a server, and now believe it is incompatible with DNSSEC. */
1725 if (t
->scope
->protocol
!= DNS_PROTOCOL_DNS
)
1728 /* If we have picked no server, then we are working from the cache or some other source, and DNSSEC might well
1729 * be supported, hence return true. */
1733 if (t
->current_feature_level
< DNS_SERVER_FEATURE_LEVEL_DO
)
1736 return dns_server_dnssec_supported(t
->server
);
1739 static bool dns_transaction_dnssec_supported_full(DnsTransaction
*t
) {
1745 /* Checks whether our transaction our any of the auxiliary transactions couldn't do DNSSEC. */
1747 if (!dns_transaction_dnssec_supported(t
))
1750 SET_FOREACH(dt
, t
->dnssec_transactions
, i
)
1751 if (!dns_transaction_dnssec_supported(dt
))
1757 int dns_transaction_request_dnssec_keys(DnsTransaction
*t
) {
1758 DnsResourceRecord
*rr
;
1765 * Retrieve all auxiliary RRs for the answer we got, so that
1766 * we can verify signatures or prove that RRs are rightfully
1767 * unsigned. Specifically:
1769 * - For RRSIG we get the matching DNSKEY
1770 * - For DNSKEY we get the matching DS
1771 * - For unsigned SOA/NS we get the matching DS
1772 * - For unsigned CNAME/DNAME/DS we get the parent SOA RR
1773 * - For other unsigned RRs we get the matching SOA RR
1774 * - For SOA/NS/DS queries with no matching response RRs, and no NSEC/NSEC3, the parent's SOA RR
1775 * - For other queries with no matching response RRs, and no NSEC/NSEC3, the SOA RR
1778 if (t
->scope
->dnssec_mode
== DNSSEC_NO
)
1780 if (t
->answer_source
!= DNS_TRANSACTION_NETWORK
)
1781 return 0; /* We only need to validate stuff from the network */
1782 if (!dns_transaction_dnssec_supported(t
))
1783 return 0; /* If we can't do DNSSEC anyway there's no point in geting the auxiliary RRs */
1785 DNS_ANSWER_FOREACH(rr
, t
->answer
) {
1787 if (dns_type_is_pseudo(rr
->key
->type
))
1790 /* If this RR is in the negative trust anchor, we don't need to validate it. */
1791 r
= dns_transaction_negative_trust_anchor_lookup(t
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1797 switch (rr
->key
->type
) {
1799 case DNS_TYPE_RRSIG
: {
1800 /* For each RRSIG we request the matching DNSKEY */
1801 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*dnskey
= NULL
;
1803 /* If this RRSIG is about a DNSKEY RR and the
1804 * signer is the same as the owner, then we
1805 * already have the DNSKEY, and we don't have
1806 * to look for more. */
1807 if (rr
->rrsig
.type_covered
== DNS_TYPE_DNSKEY
) {
1808 r
= dns_name_equal(rr
->rrsig
.signer
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1815 /* If the signer is not a parent of our
1816 * original query, then this is about an
1817 * auxiliary RRset, but not anything we asked
1818 * for. In this case we aren't interested,
1819 * because we don't want to request additional
1820 * RRs for stuff we didn't really ask for, and
1821 * also to avoid request loops, where
1822 * additional RRs from one transaction result
1823 * in another transaction whose additonal RRs
1824 * point back to the original transaction, and
1826 r
= dns_name_endswith(DNS_RESOURCE_KEY_NAME(t
->key
), rr
->rrsig
.signer
);
1832 dnskey
= dns_resource_key_new(rr
->key
->class, DNS_TYPE_DNSKEY
, rr
->rrsig
.signer
);
1836 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
);
1837 r
= dns_transaction_request_dnssec_rr(t
, dnskey
);
1843 case DNS_TYPE_DNSKEY
: {
1844 /* For each DNSKEY we request the matching DS */
1845 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*ds
= NULL
;
1847 /* If the DNSKEY we are looking at is not for
1848 * zone we are interested in, nor any of its
1849 * parents, we aren't interested, and don't
1850 * request it. After all, we don't want to end
1851 * up in request loops, and want to keep
1852 * additional traffic down. */
1854 r
= dns_name_endswith(DNS_RESOURCE_KEY_NAME(t
->key
), DNS_RESOURCE_KEY_NAME(rr
->key
));
1860 ds
= dns_resource_key_new(rr
->key
->class, DNS_TYPE_DS
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1864 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));
1865 r
= dns_transaction_request_dnssec_rr(t
, ds
);
1874 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*ds
= NULL
;
1876 /* For an unsigned SOA or NS, try to acquire
1877 * the matching DS RR, as we are at a zone cut
1878 * then, and whether a DS exists tells us
1879 * whether the zone is signed. Do so only if
1880 * this RR matches our original question,
1883 r
= dns_resource_key_match_rr(t
->key
, rr
, NULL
);
1889 r
= dnssec_has_rrsig(t
->answer
, rr
->key
);
1895 ds
= dns_resource_key_new(rr
->key
->class, DNS_TYPE_DS
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1899 log_debug("Requesting DS to validate transaction %" PRIu16
" (%s, unsigned SOA/NS RRset).", t
->id
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1900 r
= dns_transaction_request_dnssec_rr(t
, ds
);
1908 case DNS_TYPE_CNAME
:
1909 case DNS_TYPE_DNAME
: {
1910 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*soa
= NULL
;
1913 /* CNAMEs and DNAMEs cannot be located at a
1914 * zone apex, hence ask for the parent SOA for
1915 * unsigned CNAME/DNAME RRs, maybe that's the
1916 * apex. But do all that only if this is
1917 * actually a response to our original
1920 * Similar for DS RRs, which are signed when
1921 * the parent SOA is signed. */
1923 r
= dns_transaction_is_primary_response(t
, rr
);
1929 r
= dnssec_has_rrsig(t
->answer
, rr
->key
);
1935 r
= dns_answer_has_dname_for_cname(t
->answer
, rr
);
1941 name
= DNS_RESOURCE_KEY_NAME(rr
->key
);
1942 r
= dns_name_parent(&name
);
1948 soa
= dns_resource_key_new(rr
->key
->class, DNS_TYPE_SOA
, name
);
1952 log_debug("Requesting parent SOA to validate transaction %" PRIu16
" (%s, unsigned CNAME/DNAME/DS RRset).", t
->id
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1953 r
= dns_transaction_request_dnssec_rr(t
, soa
);
1961 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*soa
= NULL
;
1963 /* For other unsigned RRsets (including
1964 * NSEC/NSEC3!), look for proof the zone is
1965 * unsigned, by requesting the SOA RR of the
1966 * zone. However, do so only if they are
1967 * directly relevant to our original
1970 r
= dns_transaction_is_primary_response(t
, rr
);
1976 r
= dnssec_has_rrsig(t
->answer
, rr
->key
);
1982 soa
= dns_resource_key_new(rr
->key
->class, DNS_TYPE_SOA
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1986 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
));
1987 r
= dns_transaction_request_dnssec_rr(t
, soa
);
1994 /* Above, we requested everything necessary to validate what
1995 * we got. Now, let's request what we need to validate what we
1998 r
= dns_transaction_has_unsigned_negative_answer(t
);
2004 name
= DNS_RESOURCE_KEY_NAME(t
->key
);
2006 /* If this was a SOA or NS request, then this
2007 * indicates that we are not at a zone apex, hence ask
2008 * the parent name instead. If this was a DS request,
2009 * then it's signed when the parent zone is signed,
2010 * hence ask the parent in that case, too. */
2012 if (IN_SET(t
->key
->type
, DNS_TYPE_SOA
, DNS_TYPE_NS
, DNS_TYPE_DS
)) {
2013 r
= dns_name_parent(&name
);
2017 log_debug("Requesting parent SOA to validate transaction %" PRIu16
" (%s, unsigned empty SOA/NS/DS response).", t
->id
, DNS_RESOURCE_KEY_NAME(t
->key
));
2021 log_debug("Requesting SOA to validate transaction %" PRIu16
" (%s, unsigned empty non-SOA/NS/DS response).", t
->id
, DNS_RESOURCE_KEY_NAME(t
->key
));
2024 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*soa
= NULL
;
2026 soa
= dns_resource_key_new(t
->key
->class, DNS_TYPE_SOA
, name
);
2030 r
= dns_transaction_request_dnssec_rr(t
, soa
);
2036 return dns_transaction_dnssec_is_live(t
);
2039 void dns_transaction_notify(DnsTransaction
*t
, DnsTransaction
*source
) {
2043 /* Invoked whenever any of our auxiliary DNSSEC transactions completed its work. If the state is still PENDING,
2044 we are still in the loop that adds further DNSSEC transactions, hence don't check if we are ready yet. If
2045 the state is VALIDATING however, we should check if we are complete now. */
2047 if (t
->state
== DNS_TRANSACTION_VALIDATING
)
2048 dns_transaction_process_dnssec(t
);
2051 static int dns_transaction_validate_dnskey_by_ds(DnsTransaction
*t
) {
2052 DnsResourceRecord
*rr
;
2057 /* Add all DNSKEY RRs from the answer that are validated by DS
2058 * RRs from the list of validated keys to the list of
2059 * validated keys. */
2061 DNS_ANSWER_FOREACH_IFINDEX(rr
, ifindex
, t
->answer
) {
2063 r
= dnssec_verify_dnskey_by_ds_search(rr
, t
->validated_keys
);
2069 /* If so, the DNSKEY is validated too. */
2070 r
= dns_answer_add_extend(&t
->validated_keys
, rr
, ifindex
, DNS_ANSWER_AUTHENTICATED
);
2078 static int dns_transaction_requires_rrsig(DnsTransaction
*t
, DnsResourceRecord
*rr
) {
2084 /* Checks if the RR we are looking for must be signed with an
2085 * RRSIG. This is used for positive responses. */
2087 if (t
->scope
->dnssec_mode
== DNSSEC_NO
)
2090 if (dns_type_is_pseudo(rr
->key
->type
))
2093 r
= dns_transaction_negative_trust_anchor_lookup(t
, DNS_RESOURCE_KEY_NAME(rr
->key
));
2099 switch (rr
->key
->type
) {
2101 case DNS_TYPE_RRSIG
:
2102 /* RRSIGs are the signatures themselves, they need no signing. */
2110 /* For SOA or NS RRs we look for a matching DS transaction */
2112 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
2114 if (dt
->key
->class != rr
->key
->class)
2116 if (dt
->key
->type
!= DNS_TYPE_DS
)
2119 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), DNS_RESOURCE_KEY_NAME(rr
->key
));
2125 /* We found a DS transactions for the SOA/NS
2126 * RRs we are looking at. If it discovered signed DS
2127 * RRs, then we need to be signed, too. */
2129 if (!dt
->answer_authenticated
)
2132 return dns_answer_match_key(dt
->answer
, dt
->key
, NULL
);
2135 /* We found nothing that proves this is safe to leave
2136 * this unauthenticated, hence ask inist on
2137 * authentication. */
2142 case DNS_TYPE_CNAME
:
2143 case DNS_TYPE_DNAME
: {
2144 const char *parent
= NULL
;
2149 * CNAME/DNAME RRs cannot be located at a zone apex, hence look directly for the parent SOA.
2151 * DS RRs are signed if the parent is signed, hence also look at the parent SOA
2154 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
2156 if (dt
->key
->class != rr
->key
->class)
2158 if (dt
->key
->type
!= DNS_TYPE_SOA
)
2162 parent
= DNS_RESOURCE_KEY_NAME(rr
->key
);
2163 r
= dns_name_parent(&parent
);
2167 if (rr
->key
->type
== DNS_TYPE_DS
)
2170 /* A CNAME/DNAME without a parent? That's sooo weird. */
2171 log_debug("Transaction %" PRIu16
" claims CNAME/DNAME at root. Refusing.", t
->id
);
2176 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), parent
);
2182 return t
->answer_authenticated
;
2192 /* Any other kind of RR (including DNSKEY/NSEC/NSEC3). Let's see if our SOA lookup was authenticated */
2194 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
2196 if (dt
->key
->class != rr
->key
->class)
2198 if (dt
->key
->type
!= DNS_TYPE_SOA
)
2201 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), DNS_RESOURCE_KEY_NAME(rr
->key
));
2207 /* We found the transaction that was supposed to find
2208 * the SOA RR for us. It was successful, but found no
2209 * RR for us. This means we are not at a zone cut. In
2210 * this case, we require authentication if the SOA
2211 * lookup was authenticated too. */
2212 return t
->answer_authenticated
;
2219 static int dns_transaction_in_private_tld(DnsTransaction
*t
, const DnsResourceKey
*key
) {
2225 /* If DNSSEC downgrade mode is on, checks whether the
2226 * specified RR is one level below a TLD we have proven not to
2227 * exist. In such a case we assume that this is a private
2228 * domain, and permit it.
2230 * This detects cases like the Fritz!Box router networks. Each
2231 * Fritz!Box router serves a private "fritz.box" zone, in the
2232 * non-existing TLD "box". Requests for the "fritz.box" domain
2233 * are served by the router itself, while requests for the
2234 * "box" domain will result in NXDOMAIN.
2236 * Note that this logic is unable to detect cases where a
2237 * router serves a private DNS zone directly under
2238 * non-existing TLD. In such a case we cannot detect whether
2239 * the TLD is supposed to exist or not, as all requests we
2240 * make for it will be answered by the router's zone, and not
2241 * by the root zone. */
2245 if (t
->scope
->dnssec_mode
!= DNSSEC_ALLOW_DOWNGRADE
)
2246 return false; /* In strict DNSSEC mode what doesn't exist, doesn't exist */
2248 tld
= DNS_RESOURCE_KEY_NAME(key
);
2249 r
= dns_name_parent(&tld
);
2253 return false; /* Already the root domain */
2255 if (!dns_name_is_single_label(tld
))
2258 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
2260 if (dt
->key
->class != key
->class)
2263 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), tld
);
2269 /* We found an auxiliary lookup we did for the TLD. If
2270 * that returned with NXDOMAIN, we know the TLD didn't
2271 * exist, and hence this might be a private zone. */
2273 return dt
->answer_rcode
== DNS_RCODE_NXDOMAIN
;
2279 static int dns_transaction_requires_nsec(DnsTransaction
*t
) {
2287 /* Checks if we need to insist on NSEC/NSEC3 RRs for proving
2288 * this negative reply */
2290 if (t
->scope
->dnssec_mode
== DNSSEC_NO
)
2293 if (dns_type_is_pseudo(t
->key
->type
))
2296 r
= dns_transaction_negative_trust_anchor_lookup(t
, DNS_RESOURCE_KEY_NAME(t
->key
));
2302 r
= dns_transaction_in_private_tld(t
, t
->key
);
2306 /* The lookup is from a TLD that is proven not to
2307 * exist, and we are in downgrade mode, hence ignore
2308 * that fact that we didn't get any NSEC RRs.*/
2310 log_info("Detected a negative query %s in a private DNS zone, permitting unsigned response.", dns_transaction_key_string(t
));
2314 name
= DNS_RESOURCE_KEY_NAME(t
->key
);
2316 if (IN_SET(t
->key
->type
, DNS_TYPE_SOA
, DNS_TYPE_NS
, DNS_TYPE_DS
)) {
2318 /* We got a negative reply for this SOA/NS lookup? If
2319 * so, then we are not at a zone apex, and thus should
2320 * look at the result of the parent SOA lookup.
2322 * We got a negative reply for this DS lookup? DS RRs
2323 * are signed when their parent zone is signed, hence
2324 * also check the parent SOA in this case. */
2326 r
= dns_name_parent(&name
);
2333 /* For all other RRs we check the SOA on the same level to see
2334 * if it's signed. */
2336 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
2338 if (dt
->key
->class != t
->key
->class)
2340 if (dt
->key
->type
!= DNS_TYPE_SOA
)
2343 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), name
);
2349 return dt
->answer_authenticated
;
2352 /* If in doubt, require NSEC/NSEC3 */
2356 static int dns_transaction_dnskey_authenticated(DnsTransaction
*t
, DnsResourceRecord
*rr
) {
2357 DnsResourceRecord
*rrsig
;
2361 /* Checks whether any of the DNSKEYs used for the RRSIGs for
2362 * the specified RRset is authenticated (i.e. has a matching
2365 r
= dns_transaction_negative_trust_anchor_lookup(t
, DNS_RESOURCE_KEY_NAME(rr
->key
));
2371 DNS_ANSWER_FOREACH(rrsig
, t
->answer
) {
2375 r
= dnssec_key_match_rrsig(rr
->key
, rrsig
);
2381 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
2383 if (dt
->key
->class != rr
->key
->class)
2386 if (dt
->key
->type
== DNS_TYPE_DNSKEY
) {
2388 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), rrsig
->rrsig
.signer
);
2394 /* OK, we found an auxiliary DNSKEY
2395 * lookup. If that lookup is
2396 * authenticated, report this. */
2398 if (dt
->answer_authenticated
)
2403 } else if (dt
->key
->type
== DNS_TYPE_DS
) {
2405 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), rrsig
->rrsig
.signer
);
2411 /* OK, we found an auxiliary DS
2412 * lookup. If that lookup is
2413 * authenticated and non-zero, we
2416 if (!dt
->answer_authenticated
)
2419 return dns_answer_match_key(dt
->answer
, dt
->key
, NULL
);
2424 return found
? false : -ENXIO
;
2427 static int dns_transaction_known_signed(DnsTransaction
*t
, DnsResourceRecord
*rr
) {
2431 /* We know that the root domain is signed, hence if it appears
2432 * not to be signed, there's a problem with the DNS server */
2434 return rr
->key
->class == DNS_CLASS_IN
&&
2435 dns_name_is_root(DNS_RESOURCE_KEY_NAME(rr
->key
));
2438 static int dns_transaction_check_revoked_trust_anchors(DnsTransaction
*t
) {
2439 DnsResourceRecord
*rr
;
2444 /* Maybe warn the user that we encountered a revoked DNSKEY
2445 * for a key from our trust anchor. Note that we don't care
2446 * whether the DNSKEY can be authenticated or not. It's
2447 * sufficient if it is self-signed. */
2449 DNS_ANSWER_FOREACH(rr
, t
->answer
) {
2450 r
= dns_trust_anchor_check_revoked(&t
->scope
->manager
->trust_anchor
, rr
, t
->answer
);
2458 static int dns_transaction_invalidate_revoked_keys(DnsTransaction
*t
) {
2464 /* Removes all DNSKEY/DS objects from t->validated_keys that
2465 * our trust anchors database considers revoked. */
2468 DnsResourceRecord
*rr
;
2472 DNS_ANSWER_FOREACH(rr
, t
->validated_keys
) {
2473 r
= dns_trust_anchor_is_revoked(&t
->scope
->manager
->trust_anchor
, rr
);
2477 r
= dns_answer_remove_by_rr(&t
->validated_keys
, rr
);
2491 static int dns_transaction_copy_validated(DnsTransaction
*t
) {
2498 /* Copy all validated RRs from the auxiliary DNSSEC transactions into our set of validated RRs */
2500 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
2502 if (DNS_TRANSACTION_IS_LIVE(dt
->state
))
2505 if (!dt
->answer_authenticated
)
2508 r
= dns_answer_extend(&t
->validated_keys
, dt
->answer
);
2516 int dns_transaction_validate_dnssec(DnsTransaction
*t
) {
2517 _cleanup_(dns_answer_unrefp
) DnsAnswer
*validated
= NULL
;
2519 PHASE_DNSKEY
, /* Phase #1, only validate DNSKEYs */
2520 PHASE_NSEC
, /* Phase #2, only validate NSEC+NSEC3 */
2521 PHASE_ALL
, /* Phase #3, validate everything else */
2523 DnsResourceRecord
*rr
;
2524 DnsAnswerFlags flags
;
2529 /* We have now collected all DS and DNSKEY RRs in
2530 * t->validated_keys, let's see which RRs we can now
2531 * authenticate with that. */
2533 if (t
->scope
->dnssec_mode
== DNSSEC_NO
)
2536 /* Already validated */
2537 if (t
->answer_dnssec_result
!= _DNSSEC_RESULT_INVALID
)
2540 /* Our own stuff needs no validation */
2541 if (IN_SET(t
->answer_source
, DNS_TRANSACTION_ZONE
, DNS_TRANSACTION_TRUST_ANCHOR
)) {
2542 t
->answer_dnssec_result
= DNSSEC_VALIDATED
;
2543 t
->answer_authenticated
= true;
2547 /* Cached stuff is not affected by validation. */
2548 if (t
->answer_source
!= DNS_TRANSACTION_NETWORK
)
2551 if (!dns_transaction_dnssec_supported_full(t
)) {
2552 /* The server does not support DNSSEC, or doesn't augment responses with RRSIGs. */
2553 t
->answer_dnssec_result
= DNSSEC_INCOMPATIBLE_SERVER
;
2554 log_debug("Not validating response, server lacks DNSSEC support.");
2558 log_debug("Validating response from transaction %" PRIu16
" (%s).", t
->id
, dns_transaction_key_string(t
));
2560 /* First, see if this response contains any revoked trust
2561 * anchors we care about */
2562 r
= dns_transaction_check_revoked_trust_anchors(t
);
2566 /* Third, copy all RRs we acquired successfully from auxiliary RRs over. */
2567 r
= dns_transaction_copy_validated(t
);
2571 /* Second, see if there are DNSKEYs we already know a
2572 * validated DS for. */
2573 r
= dns_transaction_validate_dnskey_by_ds(t
);
2577 /* Fourth, remove all DNSKEY and DS RRs again that our trust
2578 * anchor says are revoked. After all we might have marked
2579 * some keys revoked above, but they might still be lingering
2580 * in our validated_keys list. */
2581 r
= dns_transaction_invalidate_revoked_keys(t
);
2585 phase
= PHASE_DNSKEY
;
2587 bool changed
= false, have_nsec
= false;
2589 DNS_ANSWER_FOREACH(rr
, t
->answer
) {
2590 DnsResourceRecord
*rrsig
= NULL
;
2591 DnssecResult result
;
2593 switch (rr
->key
->type
) {
2595 case DNS_TYPE_RRSIG
:
2598 case DNS_TYPE_DNSKEY
:
2599 /* We validate DNSKEYs only in the DNSKEY and ALL phases */
2600 if (phase
== PHASE_NSEC
)
2605 case DNS_TYPE_NSEC3
:
2608 /* We validate NSEC/NSEC3 only in the NSEC and ALL phases */
2609 if (phase
== PHASE_DNSKEY
)
2615 /* We validate all other RRs only in the ALL phases */
2616 if (phase
!= PHASE_ALL
)
2622 r
= dnssec_verify_rrset_search(t
->answer
, rr
->key
, t
->validated_keys
, USEC_INFINITY
, &result
, &rrsig
);
2626 log_debug("Looking at %s: %s", strna(dns_resource_record_to_string(rr
)), dnssec_result_to_string(result
));
2628 if (result
== DNSSEC_VALIDATED
) {
2630 if (rr
->key
->type
== DNS_TYPE_DNSKEY
) {
2631 /* If we just validated a
2632 * DNSKEY RRset, then let's
2633 * add these keys to the set
2634 * of validated keys for this
2637 r
= dns_answer_copy_by_key(&t
->validated_keys
, t
->answer
, rr
->key
, DNS_ANSWER_AUTHENTICATED
);
2641 /* some of the DNSKEYs we just
2642 * added might already have
2643 * been revoked, remove them
2644 * again in that case. */
2645 r
= dns_transaction_invalidate_revoked_keys(t
);
2650 /* Add the validated RRset to the new
2651 * list of validated RRsets, and
2652 * remove it from the unvalidated
2653 * RRsets. We mark the RRset as
2654 * authenticated and cacheable. */
2655 r
= dns_answer_move_by_key(&validated
, &t
->answer
, rr
->key
, DNS_ANSWER_AUTHENTICATED
|DNS_ANSWER_CACHEABLE
);
2659 t
->scope
->manager
->n_dnssec_secure
++;
2661 /* Exit the loop, we dropped something from the answer, start from the beginning */
2666 /* If we haven't read all DNSKEYs yet a negative result of the validation is irrelevant, as
2667 * there might be more DNSKEYs coming. Similar, if we haven't read all NSEC/NSEC3 RRs yet, we
2668 * cannot do positive wildcard proofs yet, as those require the NSEC/NSEC3 RRs. */
2669 if (phase
!= PHASE_ALL
)
2672 if (result
== DNSSEC_VALIDATED_WILDCARD
) {
2673 bool authenticated
= false;
2676 /* This RRset validated, but as a wildcard. This means we need to prove via NSEC/NSEC3
2677 * that no matching non-wildcard RR exists.*/
2679 /* First step, determine the source of synthesis */
2680 r
= dns_resource_record_source(rrsig
, &source
);
2684 r
= dnssec_test_positive_wildcard(
2686 DNS_RESOURCE_KEY_NAME(rr
->key
),
2688 rrsig
->rrsig
.signer
,
2691 /* Unless the NSEC proof showed that the key really doesn't exist something is off. */
2693 result
= DNSSEC_INVALID
;
2695 r
= dns_answer_move_by_key(&validated
, &t
->answer
, rr
->key
, authenticated
? (DNS_ANSWER_AUTHENTICATED
|DNS_ANSWER_CACHEABLE
) : 0);
2700 t
->scope
->manager
->n_dnssec_secure
++;
2702 t
->scope
->manager
->n_dnssec_insecure
++;
2704 /* Exit the loop, we dropped something from the answer, start from the beginning */
2710 if (result
== DNSSEC_NO_SIGNATURE
) {
2711 r
= dns_transaction_requires_rrsig(t
, rr
);
2715 /* Data does not require signing. In that case, just copy it over,
2716 * but remember that this is by no means authenticated.*/
2717 r
= dns_answer_move_by_key(&validated
, &t
->answer
, rr
->key
, 0);
2721 t
->scope
->manager
->n_dnssec_insecure
++;
2726 r
= dns_transaction_known_signed(t
, rr
);
2730 /* This is an RR we know has to be signed. If it isn't this means
2731 * the server is not attaching RRSIGs, hence complain. */
2733 dns_server_packet_rrsig_missing(t
->server
, t
->current_feature_level
);
2735 if (t
->scope
->dnssec_mode
== DNSSEC_ALLOW_DOWNGRADE
) {
2737 /* Downgrading is OK? If so, just consider the information unsigned */
2739 r
= dns_answer_move_by_key(&validated
, &t
->answer
, rr
->key
, 0);
2743 t
->scope
->manager
->n_dnssec_insecure
++;
2748 /* Otherwise, fail */
2749 t
->answer_dnssec_result
= DNSSEC_INCOMPATIBLE_SERVER
;
2753 r
= dns_transaction_in_private_tld(t
, rr
->key
);
2757 _cleanup_free_
char *s
= NULL
;
2759 /* The data is from a TLD that is proven not to exist, and we are in downgrade
2760 * mode, hence ignore the fact that this was not signed. */
2762 (void) dns_resource_key_to_string(rr
->key
, &s
);
2763 log_info("Detected RRset %s is in a private DNS zone, permitting unsigned RRs.", strna(s
? strstrip(s
) : NULL
));
2765 r
= dns_answer_move_by_key(&validated
, &t
->answer
, rr
->key
, 0);
2769 t
->scope
->manager
->n_dnssec_insecure
++;
2777 DNSSEC_SIGNATURE_EXPIRED
,
2778 DNSSEC_UNSUPPORTED_ALGORITHM
)) {
2780 r
= dns_transaction_dnskey_authenticated(t
, rr
);
2781 if (r
< 0 && r
!= -ENXIO
)
2784 /* The DNSKEY transaction was not authenticated, this means there's
2785 * no DS for this, which means it's OK if no keys are found for this signature. */
2787 r
= dns_answer_move_by_key(&validated
, &t
->answer
, rr
->key
, 0);
2791 t
->scope
->manager
->n_dnssec_insecure
++;
2799 DNSSEC_SIGNATURE_EXPIRED
,
2800 DNSSEC_NO_SIGNATURE
))
2801 t
->scope
->manager
->n_dnssec_bogus
++;
2802 else /* DNSSEC_MISSING_KEY or DNSSEC_UNSUPPORTED_ALGORITHM */
2803 t
->scope
->manager
->n_dnssec_indeterminate
++;
2805 r
= dns_transaction_is_primary_response(t
, rr
);
2810 /* Look for a matching DNAME for this CNAME */
2811 r
= dns_answer_has_dname_for_cname(t
->answer
, rr
);
2815 /* Also look among the stuff we already validated */
2816 r
= dns_answer_has_dname_for_cname(validated
, rr
);
2822 /* This is a primary response to our question, and it failed validation. That's
2824 t
->answer_dnssec_result
= result
;
2828 /* This is a primary response, but we do have a DNAME RR in the RR that can replay this
2829 * CNAME, hence rely on that, and we can remove the CNAME in favour of it. */
2832 /* This is just some auxiliary data. Just remove the RRset and continue. */
2833 r
= dns_answer_remove_by_key(&t
->answer
, rr
->key
);
2837 /* Exit the loop, we dropped something from the answer, start from the beginning */
2842 /* Restart the inner loop as long as we managed to achieve something */
2846 if (phase
== PHASE_DNSKEY
&& have_nsec
) {
2847 /* OK, we processed all DNSKEYs, and there are NSEC/NSEC3 RRs, look at those now. */
2852 if (phase
!= PHASE_ALL
) {
2853 /* OK, we processed all DNSKEYs and NSEC/NSEC3 RRs, look at all the rest now. Note that in this
2854 * third phase we start to remove RRs we couldn't validate. */
2863 dns_answer_unref(t
->answer
);
2864 t
->answer
= validated
;
2867 /* At this point the answer only contains validated
2868 * RRsets. Now, let's see if it actually answers the question
2869 * we asked. If so, great! If it doesn't, then see if
2870 * NSEC/NSEC3 can prove this. */
2871 r
= dns_transaction_has_positive_answer(t
, &flags
);
2873 /* Yes, it answers the question! */
2875 if (flags
& DNS_ANSWER_AUTHENTICATED
) {
2876 /* The answer is fully authenticated, yay. */
2877 t
->answer_dnssec_result
= DNSSEC_VALIDATED
;
2878 t
->answer_rcode
= DNS_RCODE_SUCCESS
;
2879 t
->answer_authenticated
= true;
2881 /* The answer is not fully authenticated. */
2882 t
->answer_dnssec_result
= DNSSEC_UNSIGNED
;
2883 t
->answer_authenticated
= false;
2886 } else if (r
== 0) {
2887 DnssecNsecResult nr
;
2888 bool authenticated
= false;
2890 /* Bummer! Let's check NSEC/NSEC3 */
2891 r
= dnssec_nsec_test(t
->answer
, t
->key
, &nr
, &authenticated
, &t
->answer_nsec_ttl
);
2897 case DNSSEC_NSEC_NXDOMAIN
:
2898 /* NSEC proves the domain doesn't exist. Very good. */
2899 log_debug("Proved NXDOMAIN via NSEC/NSEC3 for transaction %u (%s)", t
->id
, dns_transaction_key_string(t
));
2900 t
->answer_dnssec_result
= DNSSEC_VALIDATED
;
2901 t
->answer_rcode
= DNS_RCODE_NXDOMAIN
;
2902 t
->answer_authenticated
= authenticated
;
2905 t
->scope
->manager
->n_dnssec_secure
++;
2907 t
->scope
->manager
->n_dnssec_insecure
++;
2911 case DNSSEC_NSEC_NODATA
:
2912 /* NSEC proves that there's no data here, very good. */
2913 log_debug("Proved NODATA via NSEC/NSEC3 for transaction %u (%s)", t
->id
, dns_transaction_key_string(t
));
2914 t
->answer_dnssec_result
= DNSSEC_VALIDATED
;
2915 t
->answer_rcode
= DNS_RCODE_SUCCESS
;
2916 t
->answer_authenticated
= authenticated
;
2919 t
->scope
->manager
->n_dnssec_secure
++;
2921 t
->scope
->manager
->n_dnssec_insecure
++;
2925 case DNSSEC_NSEC_OPTOUT
:
2926 /* NSEC3 says the data might not be signed */
2927 log_debug("Data is NSEC3 opt-out via NSEC/NSEC3 for transaction %u (%s)", t
->id
, dns_transaction_key_string(t
));
2928 t
->answer_dnssec_result
= DNSSEC_UNSIGNED
;
2929 t
->answer_authenticated
= false;
2931 t
->scope
->manager
->n_dnssec_insecure
++;
2934 case DNSSEC_NSEC_NO_RR
:
2935 /* No NSEC data? Bummer! */
2937 r
= dns_transaction_requires_nsec(t
);
2941 t
->answer_dnssec_result
= DNSSEC_NO_SIGNATURE
;
2942 t
->scope
->manager
->n_dnssec_indeterminate
++;
2944 t
->answer_dnssec_result
= DNSSEC_UNSIGNED
;
2945 t
->answer_authenticated
= false;
2946 t
->scope
->manager
->n_dnssec_insecure
++;
2951 case DNSSEC_NSEC_UNSUPPORTED_ALGORITHM
:
2952 /* We don't know the NSEC3 algorithm used? */
2953 t
->answer_dnssec_result
= DNSSEC_UNSUPPORTED_ALGORITHM
;
2954 t
->scope
->manager
->n_dnssec_indeterminate
++;
2957 case DNSSEC_NSEC_FOUND
:
2958 case DNSSEC_NSEC_CNAME
:
2959 /* NSEC says it needs to be there, but we couldn't find it? Bummer! */
2960 t
->answer_dnssec_result
= DNSSEC_NSEC_MISMATCH
;
2961 t
->scope
->manager
->n_dnssec_bogus
++;
2965 assert_not_reached("Unexpected NSEC result.");
2972 const char *dns_transaction_key_string(DnsTransaction
*t
) {
2975 if (!t
->key_string
) {
2976 if (dns_resource_key_to_string(t
->key
, &t
->key_string
) < 0)
2980 return strstrip(t
->key_string
);
2983 static const char* const dns_transaction_state_table
[_DNS_TRANSACTION_STATE_MAX
] = {
2984 [DNS_TRANSACTION_NULL
] = "null",
2985 [DNS_TRANSACTION_PENDING
] = "pending",
2986 [DNS_TRANSACTION_VALIDATING
] = "validating",
2987 [DNS_TRANSACTION_RCODE_FAILURE
] = "rcode-failure",
2988 [DNS_TRANSACTION_SUCCESS
] = "success",
2989 [DNS_TRANSACTION_NO_SERVERS
] = "no-servers",
2990 [DNS_TRANSACTION_TIMEOUT
] = "timeout",
2991 [DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
] = "attempts-max-reached",
2992 [DNS_TRANSACTION_INVALID_REPLY
] = "invalid-reply",
2993 [DNS_TRANSACTION_RESOURCES
] = "resources",
2994 [DNS_TRANSACTION_ABORTED
] = "aborted",
2995 [DNS_TRANSACTION_DNSSEC_FAILED
] = "dnssec-failed",
2996 [DNS_TRANSACTION_NO_TRUST_ANCHOR
] = "no-trust-anchor",
2997 [DNS_TRANSACTION_RR_TYPE_UNSUPPORTED
] = "rr-type-unsupported",
2998 [DNS_TRANSACTION_NETWORK_DOWN
] = "network-down",
3000 DEFINE_STRING_TABLE_LOOKUP(dns_transaction_state
, DnsTransactionState
);
3002 static const char* const dns_transaction_source_table
[_DNS_TRANSACTION_SOURCE_MAX
] = {
3003 [DNS_TRANSACTION_NETWORK
] = "network",
3004 [DNS_TRANSACTION_CACHE
] = "cache",
3005 [DNS_TRANSACTION_ZONE
] = "zone",
3006 [DNS_TRANSACTION_TRUST_ANCHOR
] = "trust-anchor",
3008 DEFINE_STRING_TABLE_LOOKUP(dns_transaction_source
, DnsTransactionSource
);