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 static void dns_transaction_reset_answer(DnsTransaction
*t
) {
37 t
->received
= dns_packet_unref(t
->received
);
38 t
->answer
= dns_answer_unref(t
->answer
);
40 t
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
41 t
->answer_source
= _DNS_TRANSACTION_SOURCE_INVALID
;
42 t
->answer_authenticated
= false;
43 t
->answer_nsec_ttl
= (uint32_t) -1;
46 static void dns_transaction_close_connection(DnsTransaction
*t
) {
49 t
->stream
= dns_stream_free(t
->stream
);
50 t
->dns_udp_event_source
= sd_event_source_unref(t
->dns_udp_event_source
);
51 t
->dns_udp_fd
= safe_close(t
->dns_udp_fd
);
54 static void dns_transaction_stop_timeout(DnsTransaction
*t
) {
57 t
->timeout_event_source
= sd_event_source_unref(t
->timeout_event_source
);
60 DnsTransaction
* dns_transaction_free(DnsTransaction
*t
) {
68 log_debug("Freeing transaction %" PRIu16
".", t
->id
);
70 dns_transaction_close_connection(t
);
71 dns_transaction_stop_timeout(t
);
73 dns_packet_unref(t
->sent
);
74 dns_transaction_reset_answer(t
);
76 dns_server_unref(t
->server
);
79 hashmap_remove_value(t
->scope
->transactions_by_key
, t
->key
, t
);
80 LIST_REMOVE(transactions_by_scope
, t
->scope
->transactions
, t
);
83 hashmap_remove(t
->scope
->manager
->dns_transactions
, UINT_TO_PTR(t
->id
));
86 while ((c
= set_steal_first(t
->notify_query_candidates
)))
87 set_remove(c
->transactions
, t
);
88 set_free(t
->notify_query_candidates
);
90 while ((i
= set_steal_first(t
->notify_zone_items
)))
91 i
->probe_transaction
= NULL
;
92 set_free(t
->notify_zone_items
);
94 while ((z
= set_steal_first(t
->notify_transactions
)))
95 set_remove(z
->dnssec_transactions
, t
);
96 set_free(t
->notify_transactions
);
98 while ((z
= set_steal_first(t
->dnssec_transactions
))) {
99 set_remove(z
->notify_transactions
, t
);
100 dns_transaction_gc(z
);
102 set_free(t
->dnssec_transactions
);
104 dns_answer_unref(t
->validated_keys
);
105 dns_resource_key_unref(t
->key
);
112 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsTransaction
*, dns_transaction_free
);
114 bool dns_transaction_gc(DnsTransaction
*t
) {
120 if (set_isempty(t
->notify_query_candidates
) &&
121 set_isempty(t
->notify_zone_items
) &&
122 set_isempty(t
->notify_transactions
)) {
123 dns_transaction_free(t
);
130 int dns_transaction_new(DnsTransaction
**ret
, DnsScope
*s
, DnsResourceKey
*key
) {
131 _cleanup_(dns_transaction_freep
) DnsTransaction
*t
= NULL
;
138 /* Don't allow looking up invalid or pseudo RRs */
139 if (!dns_type_is_valid_query(key
->type
))
142 /* We only support the IN class */
143 if (key
->class != DNS_CLASS_IN
&& key
->class != DNS_CLASS_ANY
)
146 r
= hashmap_ensure_allocated(&s
->manager
->dns_transactions
, NULL
);
150 r
= hashmap_ensure_allocated(&s
->transactions_by_key
, &dns_resource_key_hash_ops
);
154 t
= new0(DnsTransaction
, 1);
159 t
->answer_source
= _DNS_TRANSACTION_SOURCE_INVALID
;
160 t
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
161 t
->answer_nsec_ttl
= (uint32_t) -1;
162 t
->key
= dns_resource_key_ref(key
);
164 /* Find a fresh, unused transaction id */
166 random_bytes(&t
->id
, sizeof(t
->id
));
168 hashmap_get(s
->manager
->dns_transactions
, UINT_TO_PTR(t
->id
)));
170 r
= hashmap_put(s
->manager
->dns_transactions
, UINT_TO_PTR(t
->id
), t
);
176 r
= hashmap_replace(s
->transactions_by_key
, t
->key
, t
);
178 hashmap_remove(s
->manager
->dns_transactions
, UINT_TO_PTR(t
->id
));
182 LIST_PREPEND(transactions_by_scope
, s
->transactions
, t
);
185 s
->manager
->n_transactions_total
++;
195 static void dns_transaction_tentative(DnsTransaction
*t
, DnsPacket
*p
) {
196 _cleanup_free_
char *pretty
= NULL
;
202 if (manager_our_packet(t
->scope
->manager
, p
) != 0)
205 in_addr_to_string(p
->family
, &p
->sender
, &pretty
);
207 log_debug("Transaction %" PRIu16
" for <%s> on scope %s on %s/%s got tentative packet from %s.",
209 dns_transaction_key_string(t
),
210 dns_protocol_to_string(t
->scope
->protocol
),
211 t
->scope
->link
? t
->scope
->link
->name
: "*",
212 t
->scope
->family
== AF_UNSPEC
? "*" : af_to_name(t
->scope
->family
),
215 /* RFC 4795, Section 4.1 says that the peer with the
216 * lexicographically smaller IP address loses */
217 if (memcmp(&p
->sender
, &p
->destination
, FAMILY_ADDRESS_SIZE(p
->family
)) >= 0) {
218 log_debug("Peer has lexicographically larger IP address and thus lost in the conflict.");
222 log_debug("We have the lexicographically larger IP address and thus lost in the conflict.");
225 while ((z
= set_first(t
->notify_zone_items
))) {
226 /* First, make sure the zone item drops the reference
228 dns_zone_item_probe_stop(z
);
230 /* Secondly, report this as conflict, so that we might
231 * look for a different hostname */
232 dns_zone_item_conflict(z
);
236 dns_transaction_gc(t
);
239 void dns_transaction_complete(DnsTransaction
*t
, DnsTransactionState state
) {
240 DnsQueryCandidate
*c
;
246 assert(!DNS_TRANSACTION_IS_LIVE(state
));
248 if (state
== DNS_TRANSACTION_DNSSEC_FAILED
)
249 log_struct(LOG_NOTICE
,
250 LOG_MESSAGE_ID(SD_MESSAGE_DNSSEC_FAILURE
),
251 LOG_MESSAGE("DNSSEC validation failed for question %s: %s", dns_transaction_key_string(t
), dnssec_result_to_string(t
->answer_dnssec_result
)),
252 "DNS_TRANSACTION=%" PRIu16
, t
->id
,
253 "DNS_QUESTION=%s", dns_transaction_key_string(t
),
254 "DNSSEC_RESULT=%s", dnssec_result_to_string(t
->answer_dnssec_result
),
257 /* Note that this call might invalidate the query. Callers
258 * should hence not attempt to access the query or transaction
259 * after calling this function. */
261 log_debug("Transaction %" PRIu16
" for <%s> on scope %s on %s/%s now complete with <%s> from %s (%s).",
263 dns_transaction_key_string(t
),
264 dns_protocol_to_string(t
->scope
->protocol
),
265 t
->scope
->link
? t
->scope
->link
->name
: "*",
266 t
->scope
->family
== AF_UNSPEC
? "*" : af_to_name(t
->scope
->family
),
267 dns_transaction_state_to_string(state
),
268 t
->answer_source
< 0 ? "none" : dns_transaction_source_to_string(t
->answer_source
),
269 t
->answer_authenticated
? "authenticated" : "unsigned");
273 dns_transaction_close_connection(t
);
274 dns_transaction_stop_timeout(t
);
276 /* Notify all queries that are interested, but make sure the
277 * transaction isn't freed while we are still looking at it */
280 SET_FOREACH(c
, t
->notify_query_candidates
, i
)
281 dns_query_candidate_notify(c
);
282 SET_FOREACH(z
, t
->notify_zone_items
, i
)
283 dns_zone_item_notify(z
);
285 if (!set_isempty(t
->notify_transactions
)) {
289 /* We need to be careful when notifying other
290 * transactions, as that might destroy other
291 * transactions in our list. Hence, in order to be
292 * able to safely iterate through the list of
293 * transactions, take a GC lock on all of them
294 * first. Then, in a second loop, notify them, but
295 * first unlock that specific transaction. */
297 nt
= newa(DnsTransaction
*, set_size(t
->notify_transactions
));
298 SET_FOREACH(d
, t
->notify_transactions
, i
) {
303 assert(n
== set_size(t
->notify_transactions
));
305 for (j
= 0; j
< n
; j
++) {
306 if (set_contains(t
->notify_transactions
, nt
[j
]))
307 dns_transaction_notify(nt
[j
], t
);
310 dns_transaction_gc(nt
[j
]);
315 dns_transaction_gc(t
);
318 static int dns_transaction_pick_server(DnsTransaction
*t
) {
322 assert(t
->scope
->protocol
== DNS_PROTOCOL_DNS
);
324 server
= dns_scope_get_dns_server(t
->scope
);
328 t
->current_features
= dns_server_possible_feature_level(server
);
330 if (server
== t
->server
)
333 dns_server_unref(t
->server
);
334 t
->server
= dns_server_ref(server
);
339 static void dns_transaction_retry(DnsTransaction
*t
) {
344 log_debug("Retrying transaction %" PRIu16
".", t
->id
);
346 /* Before we try again, switch to a new server. */
347 dns_scope_next_dns_server(t
->scope
);
349 r
= dns_transaction_go(t
);
351 dns_transaction_complete(t
, DNS_TRANSACTION_RESOURCES
);
354 static int on_stream_complete(DnsStream
*s
, int error
) {
355 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
359 assert(s
->transaction
);
361 /* Copy the data we care about out of the stream before we
364 p
= dns_packet_ref(s
->read_packet
);
366 t
->stream
= dns_stream_free(t
->stream
);
368 if (ERRNO_IS_DISCONNECT(error
)) {
371 log_debug_errno(error
, "Connection failure for DNS TCP stream, treating as lost packet: %m");
372 assert_se(sd_event_now(t
->scope
->manager
->event
, clock_boottime_or_monotonic(), &usec
) >= 0);
373 dns_server_packet_lost(t
->server
, t
->current_features
, usec
- t
->start_usec
);
375 dns_transaction_retry(t
);
379 dns_transaction_complete(t
, DNS_TRANSACTION_RESOURCES
);
383 if (dns_packet_validate_reply(p
) <= 0) {
384 log_debug("Invalid TCP reply packet.");
385 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
389 dns_scope_check_conflicts(t
->scope
, p
);
392 dns_transaction_process_reply(t
, p
);
395 /* If the response wasn't useful, then complete the transition
396 * now. After all, we are the worst feature set now with TCP
397 * sockets, and there's really no point in retrying. */
398 if (t
->state
== DNS_TRANSACTION_PENDING
)
399 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
401 dns_transaction_gc(t
);
406 static int dns_transaction_open_tcp(DnsTransaction
*t
) {
407 _cleanup_close_
int fd
= -1;
412 dns_transaction_close_connection(t
);
414 switch (t
->scope
->protocol
) {
416 case DNS_PROTOCOL_DNS
:
417 r
= dns_transaction_pick_server(t
);
421 r
= dns_server_adjust_opt(t
->server
, t
->sent
, t
->current_features
);
425 fd
= dns_scope_socket_tcp(t
->scope
, AF_UNSPEC
, NULL
, t
->server
, 53);
428 case DNS_PROTOCOL_LLMNR
:
429 /* When we already received a reply to this (but it was truncated), send to its sender address */
431 fd
= dns_scope_socket_tcp(t
->scope
, t
->received
->family
, &t
->received
->sender
, NULL
, t
->received
->sender_port
);
433 union in_addr_union address
;
434 int family
= AF_UNSPEC
;
436 /* Otherwise, try to talk to the owner of a
437 * the IP address, in case this is a reverse
440 r
= dns_name_address(DNS_RESOURCE_KEY_NAME(t
->key
), &family
, &address
);
445 if (family
!= t
->scope
->family
)
448 fd
= dns_scope_socket_tcp(t
->scope
, family
, &address
, NULL
, LLMNR_PORT
);
454 return -EAFNOSUPPORT
;
460 r
= dns_stream_new(t
->scope
->manager
, &t
->stream
, t
->scope
->protocol
, fd
);
465 r
= dns_stream_write_packet(t
->stream
, t
->sent
);
467 t
->stream
= dns_stream_free(t
->stream
);
471 t
->stream
->complete
= on_stream_complete
;
472 t
->stream
->transaction
= t
;
474 /* The interface index is difficult to determine if we are
475 * connecting to the local host, hence fill this in right away
476 * instead of determining it from the socket */
478 t
->stream
->ifindex
= t
->scope
->link
->ifindex
;
480 dns_transaction_reset_answer(t
);
482 t
->tried_stream
= true;
487 static void dns_transaction_cache_answer(DnsTransaction
*t
) {
490 /* For mDNS we cache whenever we get the packet, rather than
491 * in each transaction. */
492 if (!IN_SET(t
->scope
->protocol
, DNS_PROTOCOL_DNS
, DNS_PROTOCOL_LLMNR
))
495 /* We never cache if this packet is from the local host, under
496 * the assumption that a locally running DNS server would
497 * cache this anyway, and probably knows better when to flush
498 * the cache then we could. */
499 if (!DNS_PACKET_SHALL_CACHE(t
->received
))
502 dns_cache_put(&t
->scope
->cache
,
506 t
->answer_authenticated
,
510 &t
->received
->sender
);
513 static bool dns_transaction_dnssec_is_live(DnsTransaction
*t
) {
519 SET_FOREACH(dt
, t
->dnssec_transactions
, i
)
520 if (DNS_TRANSACTION_IS_LIVE(dt
->state
))
526 static void dns_transaction_process_dnssec(DnsTransaction
*t
) {
531 /* Are there ongoing DNSSEC transactions? If so, let's wait for them. */
532 if (dns_transaction_dnssec_is_live(t
))
535 /* All our auxiliary DNSSEC transactions are complete now. Try
536 * to validate our RRset now. */
537 r
= dns_transaction_validate_dnssec(t
);
539 dns_transaction_complete(t
, DNS_TRANSACTION_RESOURCES
);
543 if (t
->answer_dnssec_result
== DNSSEC_INCOMPATIBLE_SERVER
&&
544 t
->scope
->dnssec_mode
== DNSSEC_YES
) {
545 /* We are not in automatic downgrade mode, and the
546 * server is bad, refuse operation. */
547 dns_transaction_complete(t
, DNS_TRANSACTION_DNSSEC_FAILED
);
551 if (!IN_SET(t
->answer_dnssec_result
,
552 _DNSSEC_RESULT_INVALID
, /* No DNSSEC validation enabled */
553 DNSSEC_VALIDATED
, /* Answer is signed and validated successfully */
554 DNSSEC_UNSIGNED
, /* Answer is right-fully unsigned */
555 DNSSEC_INCOMPATIBLE_SERVER
)) { /* Server does not do DNSSEC (Yay, we are downgrade attack vulnerable!) */
556 dns_transaction_complete(t
, DNS_TRANSACTION_DNSSEC_FAILED
);
560 dns_transaction_cache_answer(t
);
562 if (t
->answer_rcode
== DNS_RCODE_SUCCESS
)
563 dns_transaction_complete(t
, DNS_TRANSACTION_SUCCESS
);
565 dns_transaction_complete(t
, DNS_TRANSACTION_RCODE_FAILURE
);
568 void dns_transaction_process_reply(DnsTransaction
*t
, DnsPacket
*p
) {
575 assert(t
->scope
->manager
);
577 if (t
->state
!= DNS_TRANSACTION_PENDING
)
580 /* Note that this call might invalidate the query. Callers
581 * should hence not attempt to access the query or transaction
582 * after calling this function. */
584 log_debug("Processing incoming packet on transaction %" PRIu16
".", t
->id
);
586 switch (t
->scope
->protocol
) {
588 case DNS_PROTOCOL_LLMNR
:
589 assert(t
->scope
->link
);
591 /* For LLMNR we will not accept any packets from other
594 if (p
->ifindex
!= t
->scope
->link
->ifindex
)
597 if (p
->family
!= t
->scope
->family
)
600 /* Tentative packets are not full responses but still
601 * useful for identifying uniqueness conflicts during
603 if (DNS_PACKET_LLMNR_T(p
)) {
604 dns_transaction_tentative(t
, p
);
610 case DNS_PROTOCOL_MDNS
:
611 assert(t
->scope
->link
);
613 /* For mDNS we will not accept any packets from other interfaces */
614 if (p
->ifindex
!= t
->scope
->link
->ifindex
)
617 if (p
->family
!= t
->scope
->family
)
622 case DNS_PROTOCOL_DNS
:
623 /* Note that we do not need to verify the
624 * addresses/port numbers of incoming traffic, as we
625 * invoked connect() on our UDP socket in which case
626 * the kernel already does the needed verification for
631 assert_not_reached("Invalid DNS protocol.");
634 if (t
->received
!= p
) {
635 dns_packet_unref(t
->received
);
636 t
->received
= dns_packet_ref(p
);
639 t
->answer_source
= DNS_TRANSACTION_NETWORK
;
641 if (p
->ipproto
== IPPROTO_TCP
) {
642 if (DNS_PACKET_TC(p
)) {
643 /* Truncated via TCP? Somebody must be fucking with us */
644 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
648 if (DNS_PACKET_ID(p
) != t
->id
) {
649 /* Not the reply to our query? Somebody must be fucking with us */
650 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
655 assert_se(sd_event_now(t
->scope
->manager
->event
, clock_boottime_or_monotonic(), &ts
) >= 0);
657 switch (t
->scope
->protocol
) {
659 case DNS_PROTOCOL_DNS
:
662 if (IN_SET(DNS_PACKET_RCODE(p
), DNS_RCODE_FORMERR
, DNS_RCODE_SERVFAIL
, DNS_RCODE_NOTIMP
)) {
664 /* Request failed, immediately try again with reduced features */
665 log_debug("Server returned error: %s", dns_rcode_to_string(DNS_PACKET_RCODE(p
)));
667 dns_server_packet_failed(t
->server
, t
->current_features
);
668 dns_transaction_retry(t
);
671 dns_server_packet_received(t
->server
, t
->current_features
, ts
- t
->start_usec
, p
->size
);
675 case DNS_PROTOCOL_LLMNR
:
676 case DNS_PROTOCOL_MDNS
:
677 dns_scope_packet_received(t
->scope
, ts
- t
->start_usec
);
681 assert_not_reached("Invalid DNS protocol.");
684 if (DNS_PACKET_TC(p
)) {
686 /* Truncated packets for mDNS are not allowed. Give up immediately. */
687 if (t
->scope
->protocol
== DNS_PROTOCOL_MDNS
) {
688 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
692 /* Response was truncated, let's try again with good old TCP */
693 r
= dns_transaction_open_tcp(t
);
695 /* No servers found? Damn! */
696 dns_transaction_complete(t
, DNS_TRANSACTION_NO_SERVERS
);
700 /* On LLMNR, if we cannot connect to the host,
701 * we immediately give up */
702 if (t
->scope
->protocol
== DNS_PROTOCOL_LLMNR
) {
703 dns_transaction_complete(t
, DNS_TRANSACTION_RESOURCES
);
707 /* On DNS, couldn't send? Try immediately again, with a new server */
708 dns_transaction_retry(t
);
714 /* Parse message, if it isn't parsed yet. */
715 r
= dns_packet_extract(p
);
717 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
721 if (IN_SET(t
->scope
->protocol
, DNS_PROTOCOL_DNS
, DNS_PROTOCOL_LLMNR
)) {
723 /* Only consider responses with equivalent query section to the request */
724 r
= dns_packet_is_reply_for(p
, t
->key
);
726 dns_transaction_complete(t
, DNS_TRANSACTION_RESOURCES
);
730 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
734 /* Install the answer as answer to the transaction */
735 dns_answer_unref(t
->answer
);
736 t
->answer
= dns_answer_ref(p
->answer
);
737 t
->answer_rcode
= DNS_PACKET_RCODE(p
);
738 t
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
739 t
->answer_authenticated
= false;
741 /* Block GC while starting requests for additional DNSSEC RRs */
743 r
= dns_transaction_request_dnssec_keys(t
);
746 /* Maybe the transaction is ready for GC'ing now? If so, free it and return. */
747 if (!dns_transaction_gc(t
))
750 /* Requesting additional keys might have resulted in
751 * this transaction to fail, since the auxiliary
752 * request failed for some reason. If so, we are not
753 * in pending state anymore, and we should exit
755 if (t
->state
!= DNS_TRANSACTION_PENDING
)
758 dns_transaction_complete(t
, DNS_TRANSACTION_RESOURCES
);
762 /* There are DNSSEC transactions pending now. Update the state accordingly. */
763 t
->state
= DNS_TRANSACTION_VALIDATING
;
764 dns_transaction_close_connection(t
);
765 dns_transaction_stop_timeout(t
);
770 dns_transaction_process_dnssec(t
);
773 static int on_dns_packet(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
774 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
775 DnsTransaction
*t
= userdata
;
781 r
= manager_recv(t
->scope
->manager
, fd
, DNS_PROTOCOL_DNS
, &p
);
782 if (ERRNO_IS_DISCONNECT(-r
)) {
785 /* UDP connection failure get reported via ICMP and then are possible delivered to us on the next
786 * recvmsg(). Treat this like a lost packet. */
788 log_debug_errno(r
, "Connection failure for DNS UDP packet, treating as lost packet: %m");
789 assert_se(sd_event_now(t
->scope
->manager
->event
, clock_boottime_or_monotonic(), &usec
) >= 0);
790 dns_server_packet_lost(t
->server
, t
->current_features
, usec
- t
->start_usec
);
792 dns_transaction_retry(t
);
796 dns_transaction_complete(t
, DNS_TRANSACTION_RESOURCES
);
800 r
= dns_packet_validate_reply(p
);
802 log_debug_errno(r
, "Received invalid DNS packet as response, ignoring: %m");
806 log_debug("Received inappropriate DNS packet as response, ignoring: %m");
810 if (DNS_PACKET_ID(p
) != t
->id
) {
811 log_debug("Received packet with incorrect transaction ID, ignoring: %m");
815 dns_transaction_process_reply(t
, p
);
819 static int dns_transaction_emit_udp(DnsTransaction
*t
) {
824 if (t
->scope
->protocol
== DNS_PROTOCOL_DNS
) {
826 r
= dns_transaction_pick_server(t
);
830 if (t
->current_features
< DNS_SERVER_FEATURE_LEVEL_UDP
)
833 if (r
> 0 || t
->dns_udp_fd
< 0) { /* Server changed, or no connection yet. */
836 dns_transaction_close_connection(t
);
838 fd
= dns_scope_socket_udp(t
->scope
, t
->server
, 53);
842 r
= sd_event_add_io(t
->scope
->manager
->event
, &t
->dns_udp_event_source
, fd
, EPOLLIN
, on_dns_packet
, t
);
848 (void) sd_event_source_set_description(t
->dns_udp_event_source
, "dns-transaction-udp");
852 r
= dns_server_adjust_opt(t
->server
, t
->sent
, t
->current_features
);
856 dns_transaction_close_connection(t
);
858 r
= dns_scope_emit_udp(t
->scope
, t
->dns_udp_fd
, t
->sent
);
862 dns_transaction_reset_answer(t
);
867 static int on_transaction_timeout(sd_event_source
*s
, usec_t usec
, void *userdata
) {
868 DnsTransaction
*t
= userdata
;
873 if (!t
->initial_jitter_scheduled
|| t
->initial_jitter_elapsed
) {
874 /* Timeout reached? Increase the timeout for the server used */
875 switch (t
->scope
->protocol
) {
877 case DNS_PROTOCOL_DNS
:
879 dns_server_packet_lost(t
->server
, t
->current_features
, usec
- t
->start_usec
);
882 case DNS_PROTOCOL_LLMNR
:
883 case DNS_PROTOCOL_MDNS
:
884 dns_scope_packet_lost(t
->scope
, usec
- t
->start_usec
);
888 assert_not_reached("Invalid DNS protocol.");
891 if (t
->initial_jitter_scheduled
)
892 t
->initial_jitter_elapsed
= true;
895 log_debug("Timeout reached on transaction %" PRIu16
".", t
->id
);
897 dns_transaction_retry(t
);
901 static usec_t
transaction_get_resend_timeout(DnsTransaction
*t
) {
905 switch (t
->scope
->protocol
) {
907 case DNS_PROTOCOL_DNS
:
909 return t
->server
->resend_timeout
;
911 case DNS_PROTOCOL_MDNS
:
912 assert(t
->n_attempts
> 0);
913 return (1 << (t
->n_attempts
- 1)) * USEC_PER_SEC
;
915 case DNS_PROTOCOL_LLMNR
:
916 return t
->scope
->resend_timeout
;
919 assert_not_reached("Invalid DNS protocol.");
923 static int dns_transaction_prepare(DnsTransaction
*t
, usec_t ts
) {
928 dns_transaction_stop_timeout(t
);
930 if (t
->n_attempts
>= TRANSACTION_ATTEMPTS_MAX(t
->scope
->protocol
)) {
931 dns_transaction_complete(t
, DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
);
935 if (t
->scope
->protocol
== DNS_PROTOCOL_LLMNR
&& t
->tried_stream
) {
936 /* If we already tried via a stream, then we don't
937 * retry on LLMNR. See RFC 4795, Section 2.7. */
938 dns_transaction_complete(t
, DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
);
945 dns_transaction_reset_answer(t
);
947 /* Check the trust anchor. Do so only on classic DNS, since DNSSEC does not apply otherwise. */
948 if (t
->scope
->protocol
== DNS_PROTOCOL_DNS
) {
949 r
= dns_trust_anchor_lookup_positive(&t
->scope
->manager
->trust_anchor
, t
->key
, &t
->answer
);
953 t
->answer_rcode
= DNS_RCODE_SUCCESS
;
954 t
->answer_source
= DNS_TRANSACTION_TRUST_ANCHOR
;
955 t
->answer_authenticated
= true;
956 dns_transaction_complete(t
, DNS_TRANSACTION_SUCCESS
);
960 if (dns_name_is_root(DNS_RESOURCE_KEY_NAME(t
->key
)) &&
961 t
->key
->type
== DNS_TYPE_DS
) {
963 /* Hmm, this is a request for the root DS? A
964 * DS RR doesn't exist in the root zone, and
965 * if our trust anchor didn't know it either,
966 * this means we cannot do any DNSSEC logic
969 if (t
->scope
->dnssec_mode
== DNSSEC_ALLOW_DOWNGRADE
) {
970 /* We are in downgrade mode. In this
971 * case, synthesize an unsigned empty
972 * response, so that the any lookup
973 * depending on this one can continue
974 * assuming there was no DS, and hence
975 * the root zone was unsigned. */
977 t
->answer_rcode
= DNS_RCODE_SUCCESS
;
978 t
->answer_source
= DNS_TRANSACTION_TRUST_ANCHOR
;
979 t
->answer_authenticated
= false;
980 dns_transaction_complete(t
, DNS_TRANSACTION_SUCCESS
);
982 /* If we are not in downgrade mode,
983 * then fail the lookup, because we
984 * cannot reasonably answer it. There
985 * might be DS RRs, but we don't know
986 * them, and the DNS server won't tell
987 * them to us (and even if it would,
988 * we couldn't validate it and trust
990 dns_transaction_complete(t
, DNS_TRANSACTION_NO_TRUST_ANCHOR
);
996 /* Check the zone, but only if this transaction is not used
997 * for probing or verifying a zone item. */
998 if (set_isempty(t
->notify_zone_items
)) {
1000 r
= dns_zone_lookup(&t
->scope
->zone
, t
->key
, &t
->answer
, NULL
, NULL
);
1004 t
->answer_rcode
= DNS_RCODE_SUCCESS
;
1005 t
->answer_source
= DNS_TRANSACTION_ZONE
;
1006 t
->answer_authenticated
= true;
1007 dns_transaction_complete(t
, DNS_TRANSACTION_SUCCESS
);
1012 /* Check the cache, but only if this transaction is not used
1013 * for probing or verifying a zone item. */
1014 if (set_isempty(t
->notify_zone_items
)) {
1016 /* Before trying the cache, let's make sure we figured out a
1017 * server to use. Should this cause a change of server this
1018 * might flush the cache. */
1019 dns_scope_get_dns_server(t
->scope
);
1021 /* Let's then prune all outdated entries */
1022 dns_cache_prune(&t
->scope
->cache
);
1024 r
= dns_cache_lookup(&t
->scope
->cache
, t
->key
, &t
->answer_rcode
, &t
->answer
, &t
->answer_authenticated
);
1028 t
->answer_source
= DNS_TRANSACTION_CACHE
;
1029 if (t
->answer_rcode
== DNS_RCODE_SUCCESS
)
1030 dns_transaction_complete(t
, DNS_TRANSACTION_SUCCESS
);
1032 dns_transaction_complete(t
, DNS_TRANSACTION_RCODE_FAILURE
);
1040 static int dns_transaction_make_packet_mdns(DnsTransaction
*t
) {
1042 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
1043 bool add_known_answers
= false;
1044 DnsTransaction
*other
;
1050 assert(t
->scope
->protocol
== DNS_PROTOCOL_MDNS
);
1052 /* Discard any previously prepared packet, so we can start over and coalesce again */
1053 t
->sent
= dns_packet_unref(t
->sent
);
1055 r
= dns_packet_new_query(&p
, t
->scope
->protocol
, 0, false);
1059 r
= dns_packet_append_key(p
, t
->key
, NULL
);
1065 if (dns_key_is_shared(t
->key
))
1066 add_known_answers
= true;
1069 * For mDNS, we want to coalesce as many open queries in pending transactions into one single
1070 * query packet on the wire as possible. To achieve that, we iterate through all pending transactions
1071 * in our current scope, and see whether their timing contraints allow them to be sent.
1074 assert_se(sd_event_now(t
->scope
->manager
->event
, clock_boottime_or_monotonic(), &ts
) >= 0);
1076 LIST_FOREACH(transactions_by_scope
, other
, t
->scope
->transactions
) {
1078 /* Skip ourselves */
1082 if (other
->state
!= DNS_TRANSACTION_PENDING
)
1085 if (other
->next_attempt_after
> ts
)
1088 if (qdcount
>= UINT16_MAX
)
1091 r
= dns_packet_append_key(p
, other
->key
, NULL
);
1094 * If we can't stuff more questions into the packet, just give up.
1095 * One of the 'other' transactions will fire later and take care of the rest.
1103 r
= dns_transaction_prepare(other
, ts
);
1107 ts
+= transaction_get_resend_timeout(other
);
1109 r
= sd_event_add_time(
1110 other
->scope
->manager
->event
,
1111 &other
->timeout_event_source
,
1112 clock_boottime_or_monotonic(),
1114 on_transaction_timeout
, other
);
1118 (void) sd_event_source_set_description(t
->timeout_event_source
, "dns-transaction-timeout");
1120 other
->state
= DNS_TRANSACTION_PENDING
;
1121 other
->next_attempt_after
= ts
;
1125 if (dns_key_is_shared(other
->key
))
1126 add_known_answers
= true;
1129 DNS_PACKET_HEADER(p
)->qdcount
= htobe16(qdcount
);
1131 /* Append known answer section if we're asking for any shared record */
1132 if (add_known_answers
) {
1133 r
= dns_cache_export_shared_to_packet(&t
->scope
->cache
, p
);
1144 static int dns_transaction_make_packet(DnsTransaction
*t
) {
1145 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
1150 if (t
->scope
->protocol
== DNS_PROTOCOL_MDNS
)
1151 return dns_transaction_make_packet_mdns(t
);
1156 r
= dns_packet_new_query(&p
, t
->scope
->protocol
, 0, t
->scope
->dnssec_mode
!= DNSSEC_NO
);
1160 r
= dns_scope_good_key(t
->scope
, t
->key
);
1166 r
= dns_packet_append_key(p
, t
->key
, NULL
);
1170 DNS_PACKET_HEADER(p
)->qdcount
= htobe16(1);
1171 DNS_PACKET_HEADER(p
)->id
= t
->id
;
1179 int dns_transaction_go(DnsTransaction
*t
) {
1185 assert_se(sd_event_now(t
->scope
->manager
->event
, clock_boottime_or_monotonic(), &ts
) >= 0);
1187 r
= dns_transaction_prepare(t
, ts
);
1191 log_debug("Excercising transaction %" PRIu16
" for <%s> on scope %s on %s/%s.",
1193 dns_transaction_key_string(t
),
1194 dns_protocol_to_string(t
->scope
->protocol
),
1195 t
->scope
->link
? t
->scope
->link
->name
: "*",
1196 t
->scope
->family
== AF_UNSPEC
? "*" : af_to_name(t
->scope
->family
));
1198 if (!t
->initial_jitter_scheduled
&&
1199 (t
->scope
->protocol
== DNS_PROTOCOL_LLMNR
||
1200 t
->scope
->protocol
== DNS_PROTOCOL_MDNS
)) {
1201 usec_t jitter
, accuracy
;
1203 /* RFC 4795 Section 2.7 suggests all queries should be
1204 * delayed by a random time from 0 to JITTER_INTERVAL. */
1206 t
->initial_jitter_scheduled
= true;
1208 random_bytes(&jitter
, sizeof(jitter
));
1210 switch (t
->scope
->protocol
) {
1212 case DNS_PROTOCOL_LLMNR
:
1213 jitter
%= LLMNR_JITTER_INTERVAL_USEC
;
1214 accuracy
= LLMNR_JITTER_INTERVAL_USEC
;
1217 case DNS_PROTOCOL_MDNS
:
1218 jitter
%= MDNS_JITTER_RANGE_USEC
;
1219 jitter
+= MDNS_JITTER_MIN_USEC
;
1220 accuracy
= MDNS_JITTER_RANGE_USEC
;
1223 assert_not_reached("bad protocol");
1226 r
= sd_event_add_time(
1227 t
->scope
->manager
->event
,
1228 &t
->timeout_event_source
,
1229 clock_boottime_or_monotonic(),
1230 ts
+ jitter
, accuracy
,
1231 on_transaction_timeout
, t
);
1235 (void) sd_event_source_set_description(t
->timeout_event_source
, "dns-transaction-timeout");
1238 t
->next_attempt_after
= ts
;
1239 t
->state
= DNS_TRANSACTION_PENDING
;
1241 log_debug("Delaying %s transaction for " USEC_FMT
"us.", dns_protocol_to_string(t
->scope
->protocol
), jitter
);
1245 /* Otherwise, we need to ask the network */
1246 r
= dns_transaction_make_packet(t
);
1248 /* Not the right request to make on this network?
1249 * (i.e. an A request made on IPv6 or an AAAA request
1250 * made on IPv4, on LLMNR or mDNS.) */
1251 dns_transaction_complete(t
, DNS_TRANSACTION_NO_SERVERS
);
1257 if (t
->scope
->protocol
== DNS_PROTOCOL_LLMNR
&&
1258 (dns_name_endswith(DNS_RESOURCE_KEY_NAME(t
->key
), "in-addr.arpa") > 0 ||
1259 dns_name_endswith(DNS_RESOURCE_KEY_NAME(t
->key
), "ip6.arpa") > 0)) {
1261 /* RFC 4795, Section 2.4. says reverse lookups shall
1262 * always be made via TCP on LLMNR */
1263 r
= dns_transaction_open_tcp(t
);
1265 /* Try via UDP, and if that fails due to large size or lack of
1266 * support try via TCP */
1267 r
= dns_transaction_emit_udp(t
);
1268 if (r
== -EMSGSIZE
|| r
== -EAGAIN
)
1269 r
= dns_transaction_open_tcp(t
);
1273 /* No servers to send this to? */
1274 dns_transaction_complete(t
, DNS_TRANSACTION_NO_SERVERS
);
1277 if (t
->scope
->protocol
!= DNS_PROTOCOL_DNS
) {
1278 dns_transaction_complete(t
, DNS_TRANSACTION_RESOURCES
);
1282 /* Couldn't send? Try immediately again, with a new server */
1283 dns_scope_next_dns_server(t
->scope
);
1285 return dns_transaction_go(t
);
1288 ts
+= transaction_get_resend_timeout(t
);
1290 r
= sd_event_add_time(
1291 t
->scope
->manager
->event
,
1292 &t
->timeout_event_source
,
1293 clock_boottime_or_monotonic(),
1295 on_transaction_timeout
, t
);
1299 (void) sd_event_source_set_description(t
->timeout_event_source
, "dns-transaction-timeout");
1301 t
->state
= DNS_TRANSACTION_PENDING
;
1302 t
->next_attempt_after
= ts
;
1307 static int dns_transaction_find_cyclic(DnsTransaction
*t
, DnsTransaction
*aux
) {
1315 /* Try to find cyclic dependencies between transaction objects */
1320 SET_FOREACH(n
, aux
->dnssec_transactions
, i
) {
1321 r
= dns_transaction_find_cyclic(t
, n
);
1329 static int dns_transaction_add_dnssec_transaction(DnsTransaction
*t
, DnsResourceKey
*key
, DnsTransaction
**ret
) {
1330 DnsTransaction
*aux
;
1337 aux
= dns_scope_find_transaction(t
->scope
, key
, true);
1339 r
= dns_transaction_new(&aux
, t
->scope
, key
);
1343 if (set_contains(t
->dnssec_transactions
, aux
)) {
1348 r
= dns_transaction_find_cyclic(t
, aux
);
1352 log_debug("Detected potential cyclic dependency, refusing to add transaction %" PRIu16
" (%s) as dependency for %" PRIu16
" (%s).",
1354 strna(dns_transaction_key_string(aux
)),
1356 strna(dns_transaction_key_string(t
)));
1361 r
= set_ensure_allocated(&t
->dnssec_transactions
, NULL
);
1365 r
= set_ensure_allocated(&aux
->notify_transactions
, NULL
);
1369 r
= set_put(t
->dnssec_transactions
, aux
);
1373 r
= set_put(aux
->notify_transactions
, t
);
1375 (void) set_remove(t
->dnssec_transactions
, aux
);
1383 dns_transaction_gc(aux
);
1387 static int dns_transaction_request_dnssec_rr(DnsTransaction
*t
, DnsResourceKey
*key
) {
1388 _cleanup_(dns_answer_unrefp
) DnsAnswer
*a
= NULL
;
1389 DnsTransaction
*aux
;
1395 /* Try to get the data from the trust anchor */
1396 r
= dns_trust_anchor_lookup_positive(&t
->scope
->manager
->trust_anchor
, key
, &a
);
1400 r
= dns_answer_extend(&t
->validated_keys
, a
);
1407 /* This didn't work, ask for it via the network/cache then. */
1408 r
= dns_transaction_add_dnssec_transaction(t
, key
, &aux
);
1409 if (r
== -ELOOP
) /* This would result in a cyclic dependency */
1414 if (aux
->state
== DNS_TRANSACTION_NULL
) {
1415 r
= dns_transaction_go(aux
);
1423 static int dns_transaction_has_positive_answer(DnsTransaction
*t
, DnsAnswerFlags
*flags
) {
1428 /* Checks whether the answer is positive, i.e. either a direct
1429 * answer to the question, or a CNAME/DNAME for it */
1431 r
= dns_answer_match_key(t
->answer
, t
->key
, flags
);
1435 r
= dns_answer_find_cname_or_dname(t
->answer
, t
->key
, NULL
, flags
);
1442 static int dns_transaction_negative_trust_anchor_lookup(DnsTransaction
*t
, const char *name
) {
1447 /* Check whether the specified name is in the the NTA
1448 * database, either in the global one, or the link-local
1451 r
= dns_trust_anchor_lookup_negative(&t
->scope
->manager
->trust_anchor
, name
);
1455 if (!t
->scope
->link
)
1458 return set_contains(t
->scope
->link
->dnssec_negative_trust_anchors
, name
);
1461 static int dns_transaction_has_unsigned_negative_answer(DnsTransaction
*t
) {
1466 /* Checks whether the answer is negative, and lacks NSEC/NSEC3
1467 * RRs to prove it */
1469 r
= dns_transaction_has_positive_answer(t
, NULL
);
1475 /* Is this key explicitly listed as a negative trust anchor?
1476 * If so, it's nothing we need to care about */
1477 r
= dns_transaction_negative_trust_anchor_lookup(t
, DNS_RESOURCE_KEY_NAME(t
->key
));
1483 /* The answer does not contain any RRs that match to the
1484 * question. If so, let's see if there are any NSEC/NSEC3 RRs
1485 * included. If not, the answer is unsigned. */
1487 r
= dns_answer_contains_nsec_or_nsec3(t
->answer
);
1496 static int dns_transaction_is_primary_response(DnsTransaction
*t
, DnsResourceRecord
*rr
) {
1502 /* Check if the specified RR is the "primary" response,
1503 * i.e. either matches the question precisely or is a
1504 * CNAME/DNAME for it, or is any kind of NSEC/NSEC3 RR */
1506 r
= dns_resource_key_match_rr(t
->key
, rr
, NULL
);
1510 r
= dns_resource_key_match_cname_or_dname(t
->key
, rr
->key
, NULL
);
1514 if (rr
->key
->type
== DNS_TYPE_NSEC3
) {
1517 p
= DNS_RESOURCE_KEY_NAME(rr
->key
);
1518 r
= dns_name_parent(&p
);
1522 r
= dns_name_endswith(DNS_RESOURCE_KEY_NAME(t
->key
), p
);
1530 return rr
->key
->type
== DNS_TYPE_NSEC
;
1533 int dns_transaction_request_dnssec_keys(DnsTransaction
*t
) {
1534 DnsResourceRecord
*rr
;
1541 * Retrieve all auxiliary RRs for the answer we got, so that
1542 * we can verify signatures or prove that RRs are rightfully
1543 * unsigned. Specifically:
1545 * - For RRSIG we get the matching DNSKEY
1546 * - For DNSKEY we get the matching DS
1547 * - For unsigned SOA/NS we get the matching DS
1548 * - For unsigned CNAME/DNAME/DS we get the parent SOA RR
1549 * - For other unsigned RRs we get the matching SOA RR
1550 * - For SOA/NS/DS queries with no matching response RRs, and no NSEC/NSEC3, the parent's SOA RR
1551 * - For other queries with no matching response RRs, and no NSEC/NSEC3, the SOA RR
1554 if (t
->scope
->dnssec_mode
== DNSSEC_NO
)
1557 if (t
->current_features
< DNS_SERVER_FEATURE_LEVEL_DO
)
1558 return 0; /* Server doesn't do DNSSEC, there's no point in requesting any RRs then. */
1559 if (t
->server
&& t
->server
->rrsig_missing
)
1560 return 0; /* Server handles DNSSEC requests, but isn't augmenting responses with RRSIGs. No point in trying DNSSEC then. */
1562 DNS_ANSWER_FOREACH(rr
, t
->answer
) {
1564 if (dns_type_is_pseudo(rr
->key
->type
))
1567 /* If this RR is in the negative trust anchor, we don't need to validate it. */
1568 r
= dns_transaction_negative_trust_anchor_lookup(t
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1574 switch (rr
->key
->type
) {
1576 case DNS_TYPE_RRSIG
: {
1577 /* For each RRSIG we request the matching DNSKEY */
1578 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*dnskey
= NULL
;
1580 /* If this RRSIG is about a DNSKEY RR and the
1581 * signer is the same as the owner, then we
1582 * already have the DNSKEY, and we don't have
1583 * to look for more. */
1584 if (rr
->rrsig
.type_covered
== DNS_TYPE_DNSKEY
) {
1585 r
= dns_name_equal(rr
->rrsig
.signer
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1592 /* If the signer is not a parent of our
1593 * original query, then this is about an
1594 * auxiliary RRset, but not anything we asked
1595 * for. In this case we aren't interested,
1596 * because we don't want to request additional
1597 * RRs for stuff we didn't really ask for, and
1598 * also to avoid request loops, where
1599 * additional RRs from one transaction result
1600 * in another transaction whose additonal RRs
1601 * point back to the original transaction, and
1603 r
= dns_name_endswith(DNS_RESOURCE_KEY_NAME(t
->key
), rr
->rrsig
.signer
);
1609 dnskey
= dns_resource_key_new(rr
->key
->class, DNS_TYPE_DNSKEY
, rr
->rrsig
.signer
);
1613 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
);
1614 r
= dns_transaction_request_dnssec_rr(t
, dnskey
);
1620 case DNS_TYPE_DNSKEY
: {
1621 /* For each DNSKEY we request the matching DS */
1622 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*ds
= NULL
;
1624 /* If the DNSKEY we are looking at is not for
1625 * zone we are interested in, nor any of its
1626 * parents, we aren't interested, and don't
1627 * request it. After all, we don't want to end
1628 * up in request loops, and want to keep
1629 * additional traffic down. */
1631 r
= dns_name_endswith(DNS_RESOURCE_KEY_NAME(t
->key
), DNS_RESOURCE_KEY_NAME(rr
->key
));
1637 ds
= dns_resource_key_new(rr
->key
->class, DNS_TYPE_DS
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1641 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));
1642 r
= dns_transaction_request_dnssec_rr(t
, ds
);
1651 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*ds
= NULL
;
1653 /* For an unsigned SOA or NS, try to acquire
1654 * the matching DS RR, as we are at a zone cut
1655 * then, and whether a DS exists tells us
1656 * whether the zone is signed. Do so only if
1657 * this RR matches our original question,
1660 r
= dns_resource_key_match_rr(t
->key
, rr
, NULL
);
1666 r
= dnssec_has_rrsig(t
->answer
, rr
->key
);
1672 ds
= dns_resource_key_new(rr
->key
->class, DNS_TYPE_DS
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1676 log_debug("Requesting DS to validate transaction %" PRIu16
" (%s, unsigned SOA/NS RRset).", t
->id
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1677 r
= dns_transaction_request_dnssec_rr(t
, ds
);
1685 case DNS_TYPE_CNAME
:
1686 case DNS_TYPE_DNAME
: {
1687 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*soa
= NULL
;
1690 /* CNAMEs and DNAMEs cannot be located at a
1691 * zone apex, hence ask for the parent SOA for
1692 * unsigned CNAME/DNAME RRs, maybe that's the
1693 * apex. But do all that only if this is
1694 * actually a response to our original
1697 * Similar for DS RRs, which are signed when
1698 * the parent SOA is signed. */
1700 r
= dns_transaction_is_primary_response(t
, rr
);
1706 r
= dnssec_has_rrsig(t
->answer
, rr
->key
);
1712 name
= DNS_RESOURCE_KEY_NAME(rr
->key
);
1713 r
= dns_name_parent(&name
);
1719 soa
= dns_resource_key_new(rr
->key
->class, DNS_TYPE_SOA
, name
);
1723 log_debug("Requesting parent SOA to validate transaction %" PRIu16
" (%s, unsigned CNAME/DNAME/DS RRset).", t
->id
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1724 r
= dns_transaction_request_dnssec_rr(t
, soa
);
1732 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*soa
= NULL
;
1734 /* For other unsigned RRsets (including
1735 * NSEC/NSEC3!), look for proof the zone is
1736 * unsigned, by requesting the SOA RR of the
1737 * zone. However, do so only if they are
1738 * directly relevant to our original
1741 r
= dns_transaction_is_primary_response(t
, rr
);
1747 r
= dnssec_has_rrsig(t
->answer
, rr
->key
);
1753 soa
= dns_resource_key_new(rr
->key
->class, DNS_TYPE_SOA
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1757 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
));
1758 r
= dns_transaction_request_dnssec_rr(t
, soa
);
1765 /* Above, we requested everything necessary to validate what
1766 * we got. Now, let's request what we need to validate what we
1769 r
= dns_transaction_has_unsigned_negative_answer(t
);
1775 name
= DNS_RESOURCE_KEY_NAME(t
->key
);
1777 /* If this was a SOA or NS request, then this
1778 * indicates that we are not at a zone apex, hence ask
1779 * the parent name instead. If this was a DS request,
1780 * then it's signed when the parent zone is signed,
1781 * hence ask the parent in that case, too. */
1783 if (IN_SET(t
->key
->type
, DNS_TYPE_SOA
, DNS_TYPE_NS
, DNS_TYPE_DS
)) {
1784 r
= dns_name_parent(&name
);
1788 log_debug("Requesting parent SOA to validate transaction %" PRIu16
" (%s, unsigned empty SOA/NS/DS response).", t
->id
, DNS_RESOURCE_KEY_NAME(t
->key
));
1792 log_debug("Requesting SOA to validate transaction %" PRIu16
" (%s, unsigned empty non-SOA/NS/DS response).", t
->id
, DNS_RESOURCE_KEY_NAME(t
->key
));
1795 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*soa
= NULL
;
1797 soa
= dns_resource_key_new(t
->key
->class, DNS_TYPE_SOA
, name
);
1801 r
= dns_transaction_request_dnssec_rr(t
, soa
);
1807 return dns_transaction_dnssec_is_live(t
);
1810 void dns_transaction_notify(DnsTransaction
*t
, DnsTransaction
*source
) {
1816 if (!IN_SET(t
->state
, DNS_TRANSACTION_PENDING
, DNS_TRANSACTION_VALIDATING
))
1819 /* Invoked whenever any of our auxiliary DNSSEC transactions
1820 completed its work. We copy any RRs from that transaction
1821 over into our list of validated keys -- but only if the
1822 answer is authenticated.
1824 Note that we fail our transaction if the auxiliary
1825 transaction failed, except on NXDOMAIN. This is because
1826 some broken DNS servers (Akamai...) will return NXDOMAIN
1827 for empty non-terminals. */
1829 switch (source
->state
) {
1831 case DNS_TRANSACTION_DNSSEC_FAILED
:
1833 log_debug("Auxiliary DNSSEC RR query failed validation: %s", dnssec_result_to_string(source
->answer_dnssec_result
));
1834 t
->answer_dnssec_result
= source
->answer_dnssec_result
; /* Copy error code over */
1835 dns_transaction_complete(t
, DNS_TRANSACTION_DNSSEC_FAILED
);
1838 case DNS_TRANSACTION_RCODE_FAILURE
:
1840 if (source
->answer_rcode
!= DNS_RCODE_NXDOMAIN
) {
1841 log_debug("Auxiliary DNSSEC RR query failed with rcode=%i.", source
->answer_rcode
);
1845 /* fall-through: NXDOMAIN is good enough for us */
1847 case DNS_TRANSACTION_SUCCESS
:
1848 if (source
->answer_authenticated
) {
1849 r
= dns_answer_extend(&t
->validated_keys
, source
->answer
);
1851 log_error_errno(r
, "Failed to merge validated DNSSEC key data: %m");
1856 /* If the state is still PENDING, we are still in the loop
1857 * that adds further DNSSEC transactions, hence don't check if
1858 * we are ready yet. If the state is VALIDATING however, we
1859 * should check if we are complete now. */
1860 if (t
->state
== DNS_TRANSACTION_VALIDATING
)
1861 dns_transaction_process_dnssec(t
);
1865 log_debug("Auxiliary DNSSEC RR query failed with %s", dns_transaction_state_to_string(source
->state
));
1872 t
->answer_dnssec_result
= DNSSEC_FAILED_AUXILIARY
;
1873 dns_transaction_complete(t
, DNS_TRANSACTION_DNSSEC_FAILED
);
1876 static int dns_transaction_validate_dnskey_by_ds(DnsTransaction
*t
) {
1877 DnsResourceRecord
*rr
;
1882 /* Add all DNSKEY RRs from the answer that are validated by DS
1883 * RRs from the list of validated keys to the list of
1884 * validated keys. */
1886 DNS_ANSWER_FOREACH_IFINDEX(rr
, ifindex
, t
->answer
) {
1888 r
= dnssec_verify_dnskey_search(rr
, t
->validated_keys
);
1894 /* If so, the DNSKEY is validated too. */
1895 r
= dns_answer_add_extend(&t
->validated_keys
, rr
, ifindex
, DNS_ANSWER_AUTHENTICATED
);
1903 static int dns_transaction_requires_rrsig(DnsTransaction
*t
, DnsResourceRecord
*rr
) {
1909 /* Checks if the RR we are looking for must be signed with an
1910 * RRSIG. This is used for positive responses. */
1912 if (t
->scope
->dnssec_mode
== DNSSEC_NO
)
1915 if (dns_type_is_pseudo(rr
->key
->type
))
1918 r
= dns_transaction_negative_trust_anchor_lookup(t
, DNS_RESOURCE_KEY_NAME(rr
->key
));
1924 switch (rr
->key
->type
) {
1926 case DNS_TYPE_RRSIG
:
1927 /* RRSIGs are the signatures themselves, they need no signing. */
1935 /* For SOA or NS RRs we look for a matching DS transaction */
1937 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
1939 if (dt
->key
->class != rr
->key
->class)
1941 if (dt
->key
->type
!= DNS_TYPE_DS
)
1944 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), DNS_RESOURCE_KEY_NAME(rr
->key
));
1950 /* We found a DS transactions for the SOA/NS
1951 * RRs we are looking at. If it discovered signed DS
1952 * RRs, then we need to be signed, too. */
1954 if (!dt
->answer_authenticated
)
1957 return dns_answer_match_key(dt
->answer
, dt
->key
, NULL
);
1960 /* We found nothing that proves this is safe to leave
1961 * this unauthenticated, hence ask inist on
1962 * authentication. */
1967 case DNS_TYPE_CNAME
:
1968 case DNS_TYPE_DNAME
: {
1969 const char *parent
= NULL
;
1974 * CNAME/DNAME RRs cannot be located at a zone apex, hence look directly for the parent SOA.
1976 * DS RRs are signed if the parent is signed, hence also look at the parent SOA
1979 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
1981 if (dt
->key
->class != rr
->key
->class)
1983 if (dt
->key
->type
!= DNS_TYPE_SOA
)
1987 parent
= DNS_RESOURCE_KEY_NAME(rr
->key
);
1988 r
= dns_name_parent(&parent
);
1992 if (rr
->key
->type
== DNS_TYPE_DS
)
1995 /* A CNAME/DNAME without a parent? That's sooo weird. */
1996 log_debug("Transaction %" PRIu16
" claims CNAME/DNAME at root. Refusing.", t
->id
);
2001 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), parent
);
2007 return t
->answer_authenticated
;
2017 /* Any other kind of RR (including DNSKEY/NSEC/NSEC3). Let's see if our SOA lookup was authenticated */
2019 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
2021 if (dt
->key
->class != rr
->key
->class)
2023 if (dt
->key
->type
!= DNS_TYPE_SOA
)
2026 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), DNS_RESOURCE_KEY_NAME(rr
->key
));
2032 /* We found the transaction that was supposed to find
2033 * the SOA RR for us. It was successful, but found no
2034 * RR for us. This means we are not at a zone cut. In
2035 * this case, we require authentication if the SOA
2036 * lookup was authenticated too. */
2037 return t
->answer_authenticated
;
2044 static int dns_transaction_in_private_tld(DnsTransaction
*t
, const DnsResourceKey
*key
) {
2050 /* If DNSSEC downgrade mode is on, checks whether the
2051 * specified RR is one level below a TLD we have proven not to
2052 * exist. In such a case we assume that this is a private
2053 * domain, and permit it.
2055 * This detects cases like the Fritz!Box router networks. Each
2056 * Fritz!Box router serves a private "fritz.box" zone, in the
2057 * non-existing TLD "box". Requests for the "fritz.box" domain
2058 * are served by the router itself, while requests for the
2059 * "box" domain will result in NXDOMAIN.
2061 * Note that this logic is unable to detect cases where a
2062 * router serves a private DNS zone directly under
2063 * non-existing TLD. In such a case we cannot detect whether
2064 * the TLD is supposed to exist or not, as all requests we
2065 * make for it will be answered by the router's zone, and not
2066 * by the root zone. */
2070 if (t
->scope
->dnssec_mode
!= DNSSEC_ALLOW_DOWNGRADE
)
2071 return false; /* In strict DNSSEC mode what doesn't exist, doesn't exist */
2073 tld
= DNS_RESOURCE_KEY_NAME(key
);
2074 r
= dns_name_parent(&tld
);
2078 return false; /* Already the root domain */
2080 if (!dns_name_is_single_label(tld
))
2083 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
2085 if (dt
->key
->class != key
->class)
2088 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), tld
);
2094 /* We found an auxiliary lookup we did for the TLD. If
2095 * that returned with NXDOMAIN, we know the TLD didn't
2096 * exist, and hence this might be a private zone. */
2098 return dt
->answer_rcode
== DNS_RCODE_NXDOMAIN
;
2104 static int dns_transaction_requires_nsec(DnsTransaction
*t
) {
2112 /* Checks if we need to insist on NSEC/NSEC3 RRs for proving
2113 * this negative reply */
2115 if (t
->scope
->dnssec_mode
== DNSSEC_NO
)
2118 if (dns_type_is_pseudo(t
->key
->type
))
2121 r
= dns_transaction_negative_trust_anchor_lookup(t
, DNS_RESOURCE_KEY_NAME(t
->key
));
2127 r
= dns_transaction_in_private_tld(t
, t
->key
);
2131 /* The lookup is from a TLD that is proven not to
2132 * exist, and we are in downgrade mode, hence ignore
2133 * that fact that we didn't get any NSEC RRs.*/
2135 log_info("Detected a negative query %s in a private DNS zone, permitting unsigned response.", dns_transaction_key_string(t
));
2139 name
= DNS_RESOURCE_KEY_NAME(t
->key
);
2141 if (IN_SET(t
->key
->type
, DNS_TYPE_SOA
, DNS_TYPE_NS
, DNS_TYPE_DS
)) {
2143 /* We got a negative reply for this SOA/NS lookup? If
2144 * so, then we are not at a zone apex, and thus should
2145 * look at the result of the parent SOA lookup.
2147 * We got a negative reply for this DS lookup? DS RRs
2148 * are signed when their parent zone is signed, hence
2149 * also check the parent SOA in this case. */
2151 r
= dns_name_parent(&name
);
2158 /* For all other RRs we check the SOA on the same level to see
2159 * if it's signed. */
2161 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
2163 if (dt
->key
->class != t
->key
->class)
2165 if (dt
->key
->type
!= DNS_TYPE_SOA
)
2168 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), name
);
2174 return dt
->answer_authenticated
;
2177 /* If in doubt, require NSEC/NSEC3 */
2181 static int dns_transaction_dnskey_authenticated(DnsTransaction
*t
, DnsResourceRecord
*rr
) {
2182 DnsResourceRecord
*rrsig
;
2186 /* Checks whether any of the DNSKEYs used for the RRSIGs for
2187 * the specified RRset is authenticated (i.e. has a matching
2190 r
= dns_transaction_negative_trust_anchor_lookup(t
, DNS_RESOURCE_KEY_NAME(rr
->key
));
2196 DNS_ANSWER_FOREACH(rrsig
, t
->answer
) {
2200 r
= dnssec_key_match_rrsig(rr
->key
, rrsig
);
2206 SET_FOREACH(dt
, t
->dnssec_transactions
, i
) {
2208 if (dt
->key
->class != rr
->key
->class)
2211 if (dt
->key
->type
== DNS_TYPE_DNSKEY
) {
2213 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), rrsig
->rrsig
.signer
);
2219 /* OK, we found an auxiliary DNSKEY
2220 * lookup. If that lookup is
2221 * authenticated, report this. */
2223 if (dt
->answer_authenticated
)
2228 } else if (dt
->key
->type
== DNS_TYPE_DS
) {
2230 r
= dns_name_equal(DNS_RESOURCE_KEY_NAME(dt
->key
), rrsig
->rrsig
.signer
);
2236 /* OK, we found an auxiliary DS
2237 * lookup. If that lookup is
2238 * authenticated and non-zero, we
2241 if (!dt
->answer_authenticated
)
2244 return dns_answer_match_key(dt
->answer
, dt
->key
, NULL
);
2249 return found
? false : -ENXIO
;
2252 static int dns_transaction_known_signed(DnsTransaction
*t
, DnsResourceRecord
*rr
) {
2256 /* We know that the root domain is signed, hence if it appears
2257 * not to be signed, there's a problem with the DNS server */
2259 return rr
->key
->class == DNS_CLASS_IN
&&
2260 dns_name_is_root(DNS_RESOURCE_KEY_NAME(rr
->key
));
2263 static int dns_transaction_check_revoked_trust_anchors(DnsTransaction
*t
) {
2264 DnsResourceRecord
*rr
;
2269 /* Maybe warn the user that we encountered a revoked DNSKEY
2270 * for a key from our trust anchor. Note that we don't care
2271 * whether the DNSKEY can be authenticated or not. It's
2272 * sufficient if it is self-signed. */
2274 DNS_ANSWER_FOREACH(rr
, t
->answer
) {
2275 r
= dns_trust_anchor_check_revoked(&t
->scope
->manager
->trust_anchor
, rr
, t
->answer
);
2283 static int dns_transaction_invalidate_revoked_keys(DnsTransaction
*t
) {
2289 /* Removes all DNSKEY/DS objects from t->validated_keys that
2290 * our trust anchors database considers revoked. */
2293 DnsResourceRecord
*rr
;
2297 DNS_ANSWER_FOREACH(rr
, t
->validated_keys
) {
2298 r
= dns_trust_anchor_is_revoked(&t
->scope
->manager
->trust_anchor
, rr
);
2302 r
= dns_answer_remove_by_rr(&t
->validated_keys
, rr
);
2316 int dns_transaction_validate_dnssec(DnsTransaction
*t
) {
2317 _cleanup_(dns_answer_unrefp
) DnsAnswer
*validated
= NULL
;
2319 PHASE_DNSKEY
, /* Phase #1, only validate DNSKEYs */
2320 PHASE_NSEC
, /* Phase #2, only validate NSEC+NSEC3 */
2321 PHASE_ALL
, /* Phase #3, validate everything else */
2323 DnsResourceRecord
*rr
;
2324 DnsAnswerFlags flags
;
2329 /* We have now collected all DS and DNSKEY RRs in
2330 * t->validated_keys, let's see which RRs we can now
2331 * authenticate with that. */
2333 if (t
->scope
->dnssec_mode
== DNSSEC_NO
)
2336 /* Already validated */
2337 if (t
->answer_dnssec_result
!= _DNSSEC_RESULT_INVALID
)
2340 /* Our own stuff needs no validation */
2341 if (IN_SET(t
->answer_source
, DNS_TRANSACTION_ZONE
, DNS_TRANSACTION_TRUST_ANCHOR
)) {
2342 t
->answer_dnssec_result
= DNSSEC_VALIDATED
;
2343 t
->answer_authenticated
= true;
2347 /* Cached stuff is not affected by validation. */
2348 if (t
->answer_source
!= DNS_TRANSACTION_NETWORK
)
2351 if (t
->current_features
< DNS_SERVER_FEATURE_LEVEL_DO
||
2352 (t
->server
&& t
->server
->rrsig_missing
)) {
2353 /* The server does not support DNSSEC, or doesn't augment responses with RRSIGs. */
2354 t
->answer_dnssec_result
= DNSSEC_INCOMPATIBLE_SERVER
;
2358 log_debug("Validating response from transaction %" PRIu16
" (%s).", t
->id
, dns_transaction_key_string(t
));
2360 /* First, see if this response contains any revoked trust
2361 * anchors we care about */
2362 r
= dns_transaction_check_revoked_trust_anchors(t
);
2366 /* Second, see if there are DNSKEYs we already know a
2367 * validated DS for. */
2368 r
= dns_transaction_validate_dnskey_by_ds(t
);
2372 /* Third, remove all DNSKEY and DS RRs again that our trust
2373 * anchor says are revoked. After all we might have marked
2374 * some keys revoked above, but they might still be lingering
2375 * in our validated_keys list. */
2376 r
= dns_transaction_invalidate_revoked_keys(t
);
2380 phase
= PHASE_DNSKEY
;
2382 bool changed
= false, have_nsec
= false;
2384 DNS_ANSWER_FOREACH(rr
, t
->answer
) {
2385 DnsResourceRecord
*rrsig
= NULL
;
2386 DnssecResult result
;
2388 switch (rr
->key
->type
) {
2390 case DNS_TYPE_RRSIG
:
2393 case DNS_TYPE_DNSKEY
:
2394 /* We validate DNSKEYs only in the DNSKEY and ALL phases */
2395 if (phase
== PHASE_NSEC
)
2400 case DNS_TYPE_NSEC3
:
2403 /* We validate NSEC/NSEC3 only in the NSEC and ALL phases */
2404 if (phase
== PHASE_DNSKEY
)
2410 /* We validate all other RRs only in the ALL phases */
2411 if (phase
!= PHASE_ALL
)
2417 r
= dnssec_verify_rrset_search(t
->answer
, rr
->key
, t
->validated_keys
, USEC_INFINITY
, &result
, &rrsig
);
2421 log_debug("Looking at %s: %s", strna(dns_resource_record_to_string(rr
)), dnssec_result_to_string(result
));
2423 if (result
== DNSSEC_VALIDATED
) {
2425 if (rr
->key
->type
== DNS_TYPE_DNSKEY
) {
2426 /* If we just validated a
2427 * DNSKEY RRset, then let's
2428 * add these keys to the set
2429 * of validated keys for this
2432 r
= dns_answer_copy_by_key(&t
->validated_keys
, t
->answer
, rr
->key
, DNS_ANSWER_AUTHENTICATED
);
2436 /* some of the DNSKEYs we just
2437 * added might already have
2438 * been revoked, remove them
2439 * again in that case. */
2440 r
= dns_transaction_invalidate_revoked_keys(t
);
2445 /* Add the validated RRset to the new
2446 * list of validated RRsets, and
2447 * remove it from the unvalidated
2448 * RRsets. We mark the RRset as
2449 * authenticated and cacheable. */
2450 r
= dns_answer_move_by_key(&validated
, &t
->answer
, rr
->key
, DNS_ANSWER_AUTHENTICATED
|DNS_ANSWER_CACHEABLE
);
2454 t
->scope
->manager
->n_dnssec_secure
++;
2456 /* Exit the loop, we dropped something from the answer, start from the beginning */
2461 /* If we haven't read all DNSKEYs yet a negative result of the validation is irrelevant, as
2462 * there might be more DNSKEYs coming. Similar, if we haven't read all NSEC/NSEC3 RRs yet, we
2463 * cannot do positive wildcard proofs yet, as those require the NSEC/NSEC3 RRs. */
2464 if (phase
!= PHASE_ALL
)
2467 if (result
== DNSSEC_VALIDATED_WILDCARD
) {
2468 bool authenticated
= false;
2471 /* This RRset validated, but as a wildcard. This means we need to proof via NSEC/NSEC3
2472 * that no matching non-wildcard RR exists.
2474 * See RFC 5155, Section 8.8 and RFC 4035, Section 5.3.4*/
2476 r
= dns_name_suffix(DNS_RESOURCE_KEY_NAME(rr
->key
), rrsig
->rrsig
.labels
, &suffix
);
2482 r
= dns_name_parent(&suffix
);
2488 r
= dnssec_nsec_test_between(validated
, DNS_RESOURCE_KEY_NAME(rr
->key
), suffix
, &authenticated
);
2492 /* Unless the NSEC proof showed that the key really doesn't exist something is off. */
2493 if (r
== 0 || !authenticated
)
2494 result
= DNSSEC_INVALID
;
2496 r
= dns_answer_move_by_key(&validated
, &t
->answer
, rr
->key
, DNS_ANSWER_AUTHENTICATED
|DNS_ANSWER_CACHEABLE
);
2500 t
->scope
->manager
->n_dnssec_secure
++;
2502 /* Exit the loop, we dropped something from the answer, start from the beginning */
2507 if (result
== DNSSEC_NO_SIGNATURE
) {
2508 r
= dns_transaction_requires_rrsig(t
, rr
);
2512 /* Data does not require signing. In that case, just copy it over,
2513 * but remember that this is by no means authenticated.*/
2514 r
= dns_answer_move_by_key(&validated
, &t
->answer
, rr
->key
, 0);
2518 t
->scope
->manager
->n_dnssec_insecure
++;
2523 r
= dns_transaction_known_signed(t
, rr
);
2527 /* This is an RR we know has to be signed. If it isn't this means
2528 * the server is not attaching RRSIGs, hence complain. */
2530 dns_server_packet_rrsig_missing(t
->server
);
2532 if (t
->scope
->dnssec_mode
== DNSSEC_ALLOW_DOWNGRADE
) {
2534 /* Downgrading is OK? If so, just consider the information unsigned */
2536 r
= dns_answer_move_by_key(&validated
, &t
->answer
, rr
->key
, 0);
2540 t
->scope
->manager
->n_dnssec_insecure
++;
2545 /* Otherwise, fail */
2546 t
->answer_dnssec_result
= DNSSEC_INCOMPATIBLE_SERVER
;
2550 r
= dns_transaction_in_private_tld(t
, rr
->key
);
2554 _cleanup_free_
char *s
= NULL
;
2556 /* The data is from a TLD that is proven not to exist, and we are in downgrade
2557 * mode, hence ignore the fact that this was not signed. */
2559 (void) dns_resource_key_to_string(rr
->key
, &s
);
2560 log_info("Detected RRset %s is in a private DNS zone, permitting unsigned RRs.", strna(s
? strstrip(s
) : NULL
));
2562 r
= dns_answer_move_by_key(&validated
, &t
->answer
, rr
->key
, 0);
2566 t
->scope
->manager
->n_dnssec_insecure
++;
2574 DNSSEC_SIGNATURE_EXPIRED
,
2575 DNSSEC_UNSUPPORTED_ALGORITHM
)) {
2577 r
= dns_transaction_dnskey_authenticated(t
, rr
);
2578 if (r
< 0 && r
!= -ENXIO
)
2581 /* The DNSKEY transaction was not authenticated, this means there's
2582 * no DS for this, which means it's OK if no keys are found for this signature. */
2584 r
= dns_answer_move_by_key(&validated
, &t
->answer
, rr
->key
, 0);
2588 t
->scope
->manager
->n_dnssec_insecure
++;
2596 DNSSEC_SIGNATURE_EXPIRED
,
2597 DNSSEC_NO_SIGNATURE
))
2598 t
->scope
->manager
->n_dnssec_bogus
++;
2599 else /* DNSSEC_MISSING_KEY or DNSSEC_UNSUPPORTED_ALGORITHM */
2600 t
->scope
->manager
->n_dnssec_indeterminate
++;
2602 r
= dns_transaction_is_primary_response(t
, rr
);
2606 /* This is a primary response
2607 * to our question, and it
2608 * failed validation. That's
2610 t
->answer_dnssec_result
= result
;
2614 /* This is just some auxiliary
2615 * data. Just remove the RRset and
2617 r
= dns_answer_remove_by_key(&t
->answer
, rr
->key
);
2621 /* Exit the loop, we dropped something from the answer, start from the beginning */
2626 /* Restart the inner loop as long as we managed to achieve something */
2630 if (phase
== PHASE_DNSKEY
&& have_nsec
) {
2631 /* OK, we processed all DNSKEYs, and there are NSEC/NSEC3 RRs, look at those now. */
2636 if (phase
!= PHASE_ALL
) {
2637 /* OK, we processed all DNSKEYs and NSEC/NSEC3 RRs, look at all the rest now. Note that in this
2638 * third phase we start to remove RRs we couldn't validate. */
2647 dns_answer_unref(t
->answer
);
2648 t
->answer
= validated
;
2651 /* At this point the answer only contains validated
2652 * RRsets. Now, let's see if it actually answers the question
2653 * we asked. If so, great! If it doesn't, then see if
2654 * NSEC/NSEC3 can prove this. */
2655 r
= dns_transaction_has_positive_answer(t
, &flags
);
2657 /* Yes, it answers the question! */
2659 if (flags
& DNS_ANSWER_AUTHENTICATED
) {
2660 /* The answer is fully authenticated, yay. */
2661 t
->answer_dnssec_result
= DNSSEC_VALIDATED
;
2662 t
->answer_rcode
= DNS_RCODE_SUCCESS
;
2663 t
->answer_authenticated
= true;
2665 /* The answer is not fully authenticated. */
2666 t
->answer_dnssec_result
= DNSSEC_UNSIGNED
;
2667 t
->answer_authenticated
= false;
2670 } else if (r
== 0) {
2671 DnssecNsecResult nr
;
2672 bool authenticated
= false;
2674 /* Bummer! Let's check NSEC/NSEC3 */
2675 r
= dnssec_nsec_test(t
->answer
, t
->key
, &nr
, &authenticated
, &t
->answer_nsec_ttl
);
2681 case DNSSEC_NSEC_NXDOMAIN
:
2682 /* NSEC proves the domain doesn't exist. Very good. */
2683 log_debug("Proved NXDOMAIN via NSEC/NSEC3 for transaction %u (%s)", t
->id
, dns_transaction_key_string(t
));
2684 t
->answer_dnssec_result
= DNSSEC_VALIDATED
;
2685 t
->answer_rcode
= DNS_RCODE_NXDOMAIN
;
2686 t
->answer_authenticated
= authenticated
;
2689 case DNSSEC_NSEC_NODATA
:
2690 /* NSEC proves that there's no data here, very good. */
2691 log_debug("Proved NODATA via NSEC/NSEC3 for transaction %u (%s)", t
->id
, dns_transaction_key_string(t
));
2692 t
->answer_dnssec_result
= DNSSEC_VALIDATED
;
2693 t
->answer_rcode
= DNS_RCODE_SUCCESS
;
2694 t
->answer_authenticated
= authenticated
;
2697 case DNSSEC_NSEC_OPTOUT
:
2698 /* NSEC3 says the data might not be signed */
2699 log_debug("Data is NSEC3 opt-out via NSEC/NSEC3 for transaction %u (%s)", t
->id
, dns_transaction_key_string(t
));
2700 t
->answer_dnssec_result
= DNSSEC_UNSIGNED
;
2701 t
->answer_authenticated
= false;
2704 case DNSSEC_NSEC_NO_RR
:
2705 /* No NSEC data? Bummer! */
2707 r
= dns_transaction_requires_nsec(t
);
2711 t
->answer_dnssec_result
= DNSSEC_NO_SIGNATURE
;
2713 t
->answer_dnssec_result
= DNSSEC_UNSIGNED
;
2714 t
->answer_authenticated
= false;
2719 case DNSSEC_NSEC_UNSUPPORTED_ALGORITHM
:
2720 /* We don't know the NSEC3 algorithm used? */
2721 t
->answer_dnssec_result
= DNSSEC_UNSUPPORTED_ALGORITHM
;
2724 case DNSSEC_NSEC_FOUND
:
2725 case DNSSEC_NSEC_CNAME
:
2726 /* NSEC says it needs to be there, but we couldn't find it? Bummer! */
2727 t
->answer_dnssec_result
= DNSSEC_NSEC_MISMATCH
;
2731 assert_not_reached("Unexpected NSEC result.");
2738 const char *dns_transaction_key_string(DnsTransaction
*t
) {
2741 if (!t
->key_string
) {
2742 if (dns_resource_key_to_string(t
->key
, &t
->key_string
) < 0)
2746 return strstrip(t
->key_string
);
2749 static const char* const dns_transaction_state_table
[_DNS_TRANSACTION_STATE_MAX
] = {
2750 [DNS_TRANSACTION_NULL
] = "null",
2751 [DNS_TRANSACTION_PENDING
] = "pending",
2752 [DNS_TRANSACTION_VALIDATING
] = "validating",
2753 [DNS_TRANSACTION_RCODE_FAILURE
] = "rcode-failure",
2754 [DNS_TRANSACTION_SUCCESS
] = "success",
2755 [DNS_TRANSACTION_NO_SERVERS
] = "no-servers",
2756 [DNS_TRANSACTION_TIMEOUT
] = "timeout",
2757 [DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
] = "attempts-max-reached",
2758 [DNS_TRANSACTION_INVALID_REPLY
] = "invalid-reply",
2759 [DNS_TRANSACTION_RESOURCES
] = "resources",
2760 [DNS_TRANSACTION_ABORTED
] = "aborted",
2761 [DNS_TRANSACTION_DNSSEC_FAILED
] = "dnssec-failed",
2762 [DNS_TRANSACTION_NO_TRUST_ANCHOR
] = "no-trust-anchor",
2764 DEFINE_STRING_TABLE_LOOKUP(dns_transaction_state
, DnsTransactionState
);
2766 static const char* const dns_transaction_source_table
[_DNS_TRANSACTION_SOURCE_MAX
] = {
2767 [DNS_TRANSACTION_NETWORK
] = "network",
2768 [DNS_TRANSACTION_CACHE
] = "cache",
2769 [DNS_TRANSACTION_ZONE
] = "zone",
2770 [DNS_TRANSACTION_TRUST_ANCHOR
] = "trust-anchor",
2772 DEFINE_STRING_TABLE_LOOKUP(dns_transaction_source
, DnsTransactionSource
);