1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include "sd-messages.h"
6 #include "alloc-util.h"
7 #include "dns-domain.h"
8 #include "errno-list.h"
9 #include "errno-util.h"
11 #include "glyph-util.h"
12 #include "random-util.h"
13 #include "resolved-dns-cache.h"
14 #include "resolved-dns-transaction.h"
15 #include "resolved-dnstls.h"
16 #include "resolved-llmnr.h"
17 #include "string-table.h"
19 #define TRANSACTIONS_MAX 4096
20 #define TRANSACTION_TCP_TIMEOUT_USEC (10U*USEC_PER_SEC)
22 /* After how much time to repeat classic DNS requests */
23 #define DNS_TIMEOUT_USEC (SD_RESOLVED_QUERY_TIMEOUT_USEC / DNS_TRANSACTION_ATTEMPTS_MAX)
25 static void dns_transaction_reset_answer(DnsTransaction
*t
) {
28 t
->received
= dns_packet_unref(t
->received
);
29 t
->answer
= dns_answer_unref(t
->answer
);
31 t
->answer_ede_rcode
= _DNS_EDE_RCODE_INVALID
;
32 t
->answer_ede_msg
= mfree(t
->answer_ede_msg
);
33 t
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
34 t
->answer_source
= _DNS_TRANSACTION_SOURCE_INVALID
;
35 t
->answer_query_flags
= 0;
36 t
->answer_nsec_ttl
= UINT32_MAX
;
40 static void dns_transaction_flush_dnssec_transactions(DnsTransaction
*t
) {
45 while ((z
= set_steal_first(t
->dnssec_transactions
))) {
46 set_remove(z
->notify_transactions
, t
);
47 set_remove(z
->notify_transactions_done
, t
);
48 dns_transaction_gc(z
);
52 static void dns_transaction_close_connection(
54 bool use_graveyard
) { /* Set use_graveyard = false when you know the connection is already
55 * dead, for example because you got a connection error back from the
56 * kernel. In that case there's no point in keeping the fd around,
63 /* Let's detach the stream from our transaction, in case something else keeps a reference to it. */
64 LIST_REMOVE(transactions_by_stream
, t
->stream
->transactions
, t
);
66 /* Remove packet in case it's still in the queue */
67 dns_packet_unref(ordered_set_remove(t
->stream
->write_queue
, t
->sent
));
69 t
->stream
= dns_stream_unref(t
->stream
);
72 t
->dns_udp_event_source
= sd_event_source_disable_unref(t
->dns_udp_event_source
);
74 /* If we have a UDP socket where we sent a packet, but never received one, then add it to the socket
75 * graveyard, instead of closing it right away. That way it will stick around for a moment longer,
76 * and the reply we might still get from the server will be eaten up instead of resulting in an ICMP
77 * port unreachable error message. */
79 /* Skip the graveyard stuff when we're shutting down, since that requires running event loop.
80 * Note that this is also called from dns_transaction_free(). In that case, scope may be NULL. */
83 !t
->scope
->manager
->event
||
84 sd_event_get_state(t
->scope
->manager
->event
) == SD_EVENT_FINISHED
)
85 use_graveyard
= false;
87 if (use_graveyard
&& t
->dns_udp_fd
>= 0 && t
->sent
&& !t
->received
) {
88 r
= manager_add_socket_to_graveyard(t
->scope
->manager
, t
->dns_udp_fd
);
90 log_debug_errno(r
, "Failed to add UDP socket to graveyard, closing immediately: %m");
92 TAKE_FD(t
->dns_udp_fd
);
95 t
->dns_udp_fd
= safe_close(t
->dns_udp_fd
);
98 static void dns_transaction_stop_timeout(DnsTransaction
*t
) {
101 t
->timeout_event_source
= sd_event_source_disable_unref(t
->timeout_event_source
);
104 DnsTransaction
* dns_transaction_free(DnsTransaction
*t
) {
105 DnsQueryCandidate
*c
;
112 log_debug("Freeing transaction %" PRIu16
".", t
->id
);
114 dns_transaction_close_connection(t
, true);
115 dns_transaction_stop_timeout(t
);
117 dns_packet_unref(t
->sent
);
118 dns_transaction_reset_answer(t
);
120 dns_server_unref(t
->server
);
124 DnsTransaction
*first
;
126 first
= hashmap_get(t
->scope
->transactions_by_key
, t
->key
);
127 LIST_REMOVE(transactions_by_key
, first
, t
);
129 hashmap_replace(t
->scope
->transactions_by_key
, first
->key
, first
);
131 hashmap_remove(t
->scope
->transactions_by_key
, t
->key
);
134 LIST_REMOVE(transactions_by_scope
, t
->scope
->transactions
, t
);
137 hashmap_remove(t
->scope
->manager
->dns_transactions
, UINT_TO_PTR(t
->id
));
140 while ((c
= set_steal_first(t
->notify_query_candidates
)))
141 set_remove(c
->transactions
, t
);
142 set_free(t
->notify_query_candidates
);
144 while ((c
= set_steal_first(t
->notify_query_candidates_done
)))
145 set_remove(c
->transactions
, t
);
146 set_free(t
->notify_query_candidates_done
);
148 while ((i
= set_steal_first(t
->notify_zone_items
)))
149 i
->probe_transaction
= NULL
;
150 set_free(t
->notify_zone_items
);
152 while ((i
= set_steal_first(t
->notify_zone_items_done
)))
153 i
->probe_transaction
= NULL
;
154 set_free(t
->notify_zone_items_done
);
156 while ((z
= set_steal_first(t
->notify_transactions
)))
157 set_remove(z
->dnssec_transactions
, t
);
158 set_free(t
->notify_transactions
);
160 while ((z
= set_steal_first(t
->notify_transactions_done
)))
161 set_remove(z
->dnssec_transactions
, t
);
162 set_free(t
->notify_transactions_done
);
164 dns_transaction_flush_dnssec_transactions(t
);
165 set_free(t
->dnssec_transactions
);
167 dns_answer_unref(t
->validated_keys
);
168 dns_resource_key_unref(t
->key
);
169 dns_packet_unref(t
->bypass
);
174 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsTransaction
*, dns_transaction_free
);
176 DnsTransaction
* dns_transaction_gc(DnsTransaction
*t
) {
179 /* Returns !NULL if we can't gc yet. */
184 if (t
->wait_for_answer
&& IN_SET(t
->state
, DNS_TRANSACTION_PENDING
, DNS_TRANSACTION_VALIDATING
))
187 if (set_isempty(t
->notify_query_candidates
) &&
188 set_isempty(t
->notify_query_candidates_done
) &&
189 set_isempty(t
->notify_zone_items
) &&
190 set_isempty(t
->notify_zone_items_done
) &&
191 set_isempty(t
->notify_transactions
) &&
192 set_isempty(t
->notify_transactions_done
))
193 return dns_transaction_free(t
);
198 static uint16_t pick_new_id(Manager
*m
) {
201 /* Find a fresh, unused transaction id. Note that this loop is bounded because there's a limit on the
202 * number of transactions, and it's much lower than the space of IDs. */
204 assert_cc(TRANSACTIONS_MAX
< 0xFFFF);
207 random_bytes(&new_id
, sizeof(new_id
));
208 while (new_id
== 0 ||
209 hashmap_get(m
->dns_transactions
, UINT_TO_PTR(new_id
)));
216 DnsResourceKey
*key
) {
218 /* Don't allow looking up invalid or pseudo RRs */
219 if (!dns_type_is_valid_query(key
->type
))
221 if (dns_type_is_obsolete(key
->type
))
224 /* We only support the IN class */
225 if (!IN_SET(key
->class, DNS_CLASS_IN
, DNS_CLASS_ANY
))
228 /* Don't allows DNSSEC RRs to be looked up via LLMNR/mDNS. They don't really make sense
229 * there, and it speeds up our queries if we refuse this early */
230 if (scope
->protocol
!= DNS_PROTOCOL_DNS
&&
231 dns_type_is_dnssec(key
->type
))
237 int dns_transaction_new(
238 DnsTransaction
**ret
,
242 uint64_t query_flags
) {
244 _cleanup_(dns_transaction_freep
) DnsTransaction
*t
= NULL
;
260 r
= dns_packet_validate_query(bypass
);
264 DNS_QUESTION_FOREACH(qk
, bypass
->question
) {
271 if (hashmap_size(s
->manager
->dns_transactions
) >= TRANSACTIONS_MAX
)
274 r
= hashmap_ensure_allocated(&s
->manager
->dns_transactions
, NULL
);
279 r
= hashmap_ensure_allocated(&s
->transactions_by_key
, &dns_resource_key_hash_ops
);
284 t
= new(DnsTransaction
, 1);
288 *t
= (DnsTransaction
) {
289 .dns_udp_fd
= -EBADF
,
290 .answer_source
= _DNS_TRANSACTION_SOURCE_INVALID
,
291 .answer_dnssec_result
= _DNSSEC_RESULT_INVALID
,
292 .answer_ede_rcode
= _DNS_EDE_RCODE_INVALID
,
293 .answer_nsec_ttl
= UINT32_MAX
,
294 .key
= dns_resource_key_ref(key
),
295 .query_flags
= query_flags
,
296 .bypass
= dns_packet_ref(bypass
),
297 .current_feature_level
= _DNS_SERVER_FEATURE_LEVEL_INVALID
,
298 .clamp_feature_level_servfail
= _DNS_SERVER_FEATURE_LEVEL_INVALID
,
299 .id
= pick_new_id(s
->manager
),
302 r
= hashmap_put(s
->manager
->dns_transactions
, UINT_TO_PTR(t
->id
), t
);
309 DnsTransaction
*first
;
311 first
= hashmap_get(s
->transactions_by_key
, t
->key
);
312 LIST_PREPEND(transactions_by_key
, first
, t
);
314 r
= hashmap_replace(s
->transactions_by_key
, first
->key
, first
);
316 LIST_REMOVE(transactions_by_key
, first
, t
);
321 LIST_PREPEND(transactions_by_scope
, s
->transactions
, t
);
324 s
->manager
->n_transactions_total
++;
333 static void dns_transaction_shuffle_id(DnsTransaction
*t
) {
337 /* Pick a new ID for this transaction. */
339 new_id
= pick_new_id(t
->scope
->manager
);
340 assert_se(hashmap_remove_and_put(t
->scope
->manager
->dns_transactions
, UINT_TO_PTR(t
->id
), UINT_TO_PTR(new_id
), t
) >= 0);
342 log_debug("Transaction %" PRIu16
" is now %" PRIu16
".", t
->id
, new_id
);
345 /* Make sure we generate a new packet with the new ID */
346 t
->sent
= dns_packet_unref(t
->sent
);
349 static void dns_transaction_tentative(DnsTransaction
*t
, DnsPacket
*p
) {
350 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
355 assert(t
->scope
->protocol
== DNS_PROTOCOL_LLMNR
);
357 if (manager_packet_from_local_address(t
->scope
->manager
, p
) != 0)
360 log_debug("Transaction %" PRIu16
" for <%s> on scope %s on %s/%s got tentative packet from %s.",
362 dns_resource_key_to_string(dns_transaction_key(t
), key_str
, sizeof key_str
),
363 dns_protocol_to_string(t
->scope
->protocol
),
364 t
->scope
->link
? t
->scope
->link
->ifname
: "*",
365 af_to_name_short(t
->scope
->family
),
366 IN_ADDR_TO_STRING(p
->family
, &p
->sender
));
368 /* RFC 4795, Section 4.1 says that the peer with the
369 * lexicographically smaller IP address loses */
370 if (memcmp(&p
->sender
, &p
->destination
, FAMILY_ADDRESS_SIZE(p
->family
)) >= 0) {
371 log_debug("Peer has lexicographically larger IP address and thus lost in the conflict.");
375 log_debug("We have the lexicographically larger IP address and thus lost in the conflict.");
379 while ((z
= set_first(t
->notify_zone_items
))) {
380 /* First, make sure the zone item drops the reference
382 dns_zone_item_probe_stop(z
);
384 /* Secondly, report this as conflict, so that we might
385 * look for a different hostname */
386 dns_zone_item_conflict(z
);
390 dns_transaction_gc(t
);
393 void dns_transaction_complete(DnsTransaction
*t
, DnsTransactionState state
) {
394 DnsQueryCandidate
*c
;
398 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
401 assert(!DNS_TRANSACTION_IS_LIVE(state
));
403 if (state
== DNS_TRANSACTION_DNSSEC_FAILED
) {
404 dns_resource_key_to_string(dns_transaction_key(t
), key_str
, sizeof key_str
);
406 log_struct(LOG_NOTICE
,
407 "MESSAGE_ID=" SD_MESSAGE_DNSSEC_FAILURE_STR
,
408 LOG_MESSAGE("DNSSEC validation failed for question %s: %s",
409 key_str
, dnssec_result_to_string(t
->answer_dnssec_result
)),
410 "DNS_TRANSACTION=%" PRIu16
, t
->id
,
411 "DNS_QUESTION=%s", key_str
,
412 "DNSSEC_RESULT=%s", dnssec_result_to_string(t
->answer_dnssec_result
),
413 "DNS_SERVER=%s", strna(dns_server_string_full(t
->server
)),
414 "DNS_SERVER_FEATURE_LEVEL=%s", dns_server_feature_level_to_string(t
->server
->possible_feature_level
));
417 /* Note that this call might invalidate the query. Callers
418 * should hence not attempt to access the query or transaction
419 * after calling this function. */
421 if (state
== DNS_TRANSACTION_ERRNO
)
422 st
= errno_to_name(t
->answer_errno
);
424 st
= dns_transaction_state_to_string(state
);
426 log_debug("%s transaction %" PRIu16
" for <%s> on scope %s on %s/%s now complete with <%s> from %s (%s; %s).",
427 t
->bypass
? "Bypass" : "Regular",
429 dns_resource_key_to_string(dns_transaction_key(t
), key_str
, sizeof key_str
),
430 dns_protocol_to_string(t
->scope
->protocol
),
431 t
->scope
->link
? t
->scope
->link
->ifname
: "*",
432 af_to_name_short(t
->scope
->family
),
434 t
->answer_source
< 0 ? "none" : dns_transaction_source_to_string(t
->answer_source
),
435 FLAGS_SET(t
->query_flags
, SD_RESOLVED_NO_VALIDATE
) ? "not validated" :
436 (FLAGS_SET(t
->answer_query_flags
, SD_RESOLVED_AUTHENTICATED
) ? "authenticated" : "unsigned"),
437 FLAGS_SET(t
->answer_query_flags
, SD_RESOLVED_CONFIDENTIAL
) ? "confidential" : "non-confidential");
441 dns_transaction_close_connection(t
, true);
442 dns_transaction_stop_timeout(t
);
444 /* Notify all queries that are interested, but make sure the
445 * transaction isn't freed while we are still looking at it */
448 SET_FOREACH_MOVE(c
, t
->notify_query_candidates_done
, t
->notify_query_candidates
)
449 dns_query_candidate_notify(c
);
450 SWAP_TWO(t
->notify_query_candidates
, t
->notify_query_candidates_done
);
452 SET_FOREACH_MOVE(z
, t
->notify_zone_items_done
, t
->notify_zone_items
)
453 dns_zone_item_notify(z
);
454 SWAP_TWO(t
->notify_zone_items
, t
->notify_zone_items_done
);
455 if (t
->probing
&& t
->state
== DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
)
456 (void) dns_scope_announce(t
->scope
, false);
458 SET_FOREACH_MOVE(d
, t
->notify_transactions_done
, t
->notify_transactions
)
459 dns_transaction_notify(d
, t
);
460 SWAP_TWO(t
->notify_transactions
, t
->notify_transactions_done
);
463 dns_transaction_gc(t
);
466 static void dns_transaction_complete_errno(DnsTransaction
*t
, int error
) {
470 t
->answer_errno
= abs(error
);
471 dns_transaction_complete(t
, DNS_TRANSACTION_ERRNO
);
474 static int dns_transaction_pick_server(DnsTransaction
*t
) {
478 assert(t
->scope
->protocol
== DNS_PROTOCOL_DNS
);
480 /* Pick a DNS server and a feature level for it. */
482 server
= dns_scope_get_dns_server(t
->scope
);
486 /* If we changed the server invalidate the feature level clamping, as the new server might have completely
487 * different properties. */
488 if (server
!= t
->server
)
489 t
->clamp_feature_level_servfail
= _DNS_SERVER_FEATURE_LEVEL_INVALID
;
491 t
->current_feature_level
= dns_server_possible_feature_level(server
);
493 /* Clamp the feature level if that is requested. */
494 if (t
->clamp_feature_level_servfail
!= _DNS_SERVER_FEATURE_LEVEL_INVALID
&&
495 t
->current_feature_level
> t
->clamp_feature_level_servfail
)
496 t
->current_feature_level
= t
->clamp_feature_level_servfail
;
498 log_debug("Using feature level %s for transaction %u.", dns_server_feature_level_to_string(t
->current_feature_level
), t
->id
);
500 if (server
== t
->server
)
503 dns_server_unref(t
->server
);
504 t
->server
= dns_server_ref(server
);
506 t
->n_picked_servers
++;
508 log_debug("Using DNS server %s for transaction %u.", strna(dns_server_string_full(t
->server
)), t
->id
);
513 static void dns_transaction_retry(DnsTransaction
*t
, bool next_server
) {
518 /* Retries the transaction as it is, possibly on a different server */
520 if (next_server
&& t
->scope
->protocol
== DNS_PROTOCOL_DNS
)
521 log_debug("Retrying transaction %" PRIu16
", after switching servers.", t
->id
);
523 log_debug("Retrying transaction %" PRIu16
".", t
->id
);
525 /* Before we try again, switch to a new server. */
527 dns_scope_next_dns_server(t
->scope
, t
->server
);
529 r
= dns_transaction_go(t
);
531 dns_transaction_complete_errno(t
, r
);
534 static bool dns_transaction_limited_retry(DnsTransaction
*t
) {
537 /* If we haven't tried all different servers yet, let's try again with a different server */
539 if (t
->n_picked_servers
>= dns_scope_get_n_dns_servers(t
->scope
))
542 dns_transaction_retry(t
, /* next_server= */ true);
546 static int dns_transaction_maybe_restart(DnsTransaction
*t
) {
551 /* Restarts the transaction, under a new ID if the feature level of the server changed since we first
552 * tried, without changing DNS server. Returns > 0 if the transaction was restarted, 0 if not. */
557 if (t
->current_feature_level
<= dns_server_possible_feature_level(t
->server
))
560 /* The server's current feature level is lower than when we sent the original query. We learnt something from
561 the response or possibly an auxiliary DNSSEC response that we didn't know before. We take that as reason to
562 restart the whole transaction. This is a good idea to deal with servers that respond rubbish if we include
563 OPT RR or DO bit. One of these cases is documented here, for example:
564 https://open.nlnetlabs.nl/pipermail/dnssec-trigger/2014-November/000376.html */
566 log_debug("Server feature level is now lower than when we began our transaction. Restarting with new ID.");
567 dns_transaction_shuffle_id(t
);
569 r
= dns_transaction_go(t
);
576 static void on_transaction_stream_error(DnsTransaction
*t
, int error
) {
579 dns_transaction_close_connection(t
, true);
581 if (ERRNO_IS_DISCONNECT(error
)) {
582 if (t
->scope
->protocol
== DNS_PROTOCOL_LLMNR
) {
583 /* If the LLMNR/TCP connection failed, the host doesn't support LLMNR, and we cannot answer the
584 * question on this scope. */
585 dns_transaction_complete(t
, DNS_TRANSACTION_NOT_FOUND
);
589 dns_transaction_retry(t
, true);
593 dns_transaction_complete_errno(t
, error
);
596 static int dns_transaction_on_stream_packet(DnsTransaction
*t
, DnsStream
*s
, DnsPacket
*p
) {
603 encrypted
= s
->encrypted
;
605 dns_transaction_close_connection(t
, true);
607 if (dns_packet_validate_reply(p
) <= 0) {
608 log_debug("Invalid TCP reply packet.");
609 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
613 dns_scope_check_conflicts(t
->scope
, p
);
616 dns_transaction_process_reply(t
, p
, encrypted
);
619 /* If the response wasn't useful, then complete the transition
620 * now. After all, we are the worst feature set now with TCP
621 * sockets, and there's really no point in retrying. */
622 if (t
->state
== DNS_TRANSACTION_PENDING
)
623 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
625 dns_transaction_gc(t
);
630 static int on_stream_complete(DnsStream
*s
, int error
) {
633 if (ERRNO_IS_DISCONNECT(error
) && s
->protocol
!= DNS_PROTOCOL_LLMNR
) {
634 log_debug_errno(error
, "Connection failure for DNS TCP stream: %m");
636 if (s
->transactions
) {
640 dns_server_packet_lost(t
->server
, IPPROTO_TCP
, t
->current_feature_level
);
645 /* First, detach the stream from the server. Otherwise, transactions attached to this stream
646 * may be restarted by on_transaction_stream_error() below with this stream. */
647 dns_stream_detach(s
);
649 /* Do not use LIST_FOREACH() here, as
650 * on_transaction_stream_error()
651 * -> dns_transaction_complete_errno()
652 * -> dns_transaction_free()
653 * may free multiple transactions in the list. */
655 while ((t
= s
->transactions
))
656 on_transaction_stream_error(t
, error
);
662 static int on_stream_packet(DnsStream
*s
, DnsPacket
*p
) {
669 t
= hashmap_get(s
->manager
->dns_transactions
, UINT_TO_PTR(DNS_PACKET_ID(p
)));
670 if (t
&& t
->stream
== s
) /* Validate that the stream we got this on actually is the stream the
671 * transaction was using. */
672 return dns_transaction_on_stream_packet(t
, s
, p
);
674 /* Ignore incorrect transaction id as an old transaction can have been canceled. */
675 log_debug("Received unexpected TCP reply packet with id %" PRIu16
", ignoring.", DNS_PACKET_ID(p
));
679 static uint16_t dns_transaction_port(DnsTransaction
*t
) {
682 if (t
->server
->port
> 0)
683 return t
->server
->port
;
685 return DNS_SERVER_FEATURE_LEVEL_IS_TLS(t
->current_feature_level
) ? 853 : 53;
688 static int dns_transaction_emit_tcp(DnsTransaction
*t
) {
689 usec_t stream_timeout_usec
= DNS_STREAM_DEFAULT_TIMEOUT_USEC
;
690 _cleanup_(dns_stream_unrefp
) DnsStream
*s
= NULL
;
691 _cleanup_close_
int fd
= -EBADF
;
692 union sockaddr_union sa
;
699 dns_transaction_close_connection(t
, true);
701 switch (t
->scope
->protocol
) {
703 case DNS_PROTOCOL_DNS
:
704 r
= dns_transaction_pick_server(t
);
708 if (manager_server_is_stub(t
->scope
->manager
, t
->server
))
712 if (!dns_server_dnssec_supported(t
->server
) && dns_type_is_dnssec(dns_transaction_key(t
)->type
))
715 r
= dns_server_adjust_opt(t
->server
, t
->sent
, t
->current_feature_level
);
720 if (t
->server
->stream
&& (DNS_SERVER_FEATURE_LEVEL_IS_TLS(t
->current_feature_level
) == t
->server
->stream
->encrypted
))
721 s
= dns_stream_ref(t
->server
->stream
);
723 fd
= dns_scope_socket_tcp(t
->scope
, AF_UNSPEC
, NULL
, t
->server
, dns_transaction_port(t
), &sa
);
725 /* Lower timeout in DNS-over-TLS opportunistic mode. In environments where DoT is blocked
726 * without ICMP response overly long delays when contacting DoT servers are nasty, in
727 * particular if multiple DNS servers are defined which we try in turn and all are
728 * blocked. Hence, substantially lower the timeout in that case. */
729 if (DNS_SERVER_FEATURE_LEVEL_IS_TLS(t
->current_feature_level
) &&
730 dns_server_get_dns_over_tls_mode(t
->server
) == DNS_OVER_TLS_OPPORTUNISTIC
)
731 stream_timeout_usec
= DNS_STREAM_OPPORTUNISTIC_TLS_TIMEOUT_USEC
;
733 type
= DNS_STREAM_LOOKUP
;
736 case DNS_PROTOCOL_LLMNR
:
737 /* When we already received a reply to this (but it was truncated), send to its sender address */
739 fd
= dns_scope_socket_tcp(t
->scope
, t
->received
->family
, &t
->received
->sender
, NULL
, t
->received
->sender_port
, &sa
);
741 union in_addr_union address
;
742 int family
= AF_UNSPEC
;
744 /* Otherwise, try to talk to the owner of a
745 * the IP address, in case this is a reverse
748 r
= dns_name_address(dns_resource_key_name(dns_transaction_key(t
)), &family
, &address
);
753 if (family
!= t
->scope
->family
)
756 fd
= dns_scope_socket_tcp(t
->scope
, family
, &address
, NULL
, LLMNR_PORT
, &sa
);
759 type
= DNS_STREAM_LLMNR_SEND
;
763 return -EAFNOSUPPORT
;
770 r
= dns_stream_new(t
->scope
->manager
, &s
, type
, t
->scope
->protocol
, fd
, &sa
,
771 on_stream_packet
, on_stream_complete
, stream_timeout_usec
);
777 #if ENABLE_DNS_OVER_TLS
778 if (t
->scope
->protocol
== DNS_PROTOCOL_DNS
&&
779 DNS_SERVER_FEATURE_LEVEL_IS_TLS(t
->current_feature_level
)) {
782 r
= dnstls_stream_connect_tls(s
, t
->server
);
789 dns_server_unref_stream(t
->server
);
790 s
->server
= dns_server_ref(t
->server
);
791 t
->server
->stream
= dns_stream_ref(s
);
794 /* The interface index is difficult to determine if we are
795 * connecting to the local host, hence fill this in right away
796 * instead of determining it from the socket */
797 s
->ifindex
= dns_scope_ifindex(t
->scope
);
800 t
->stream
= TAKE_PTR(s
);
801 LIST_PREPEND(transactions_by_stream
, t
->stream
->transactions
, t
);
803 r
= dns_stream_write_packet(t
->stream
, t
->sent
);
805 dns_transaction_close_connection(t
, /* use_graveyard= */ false);
809 dns_transaction_reset_answer(t
);
811 t
->tried_stream
= true;
816 static void dns_transaction_cache_answer(DnsTransaction
*t
) {
819 /* For mDNS we cache whenever we get the packet, rather than
820 * in each transaction. */
821 if (!IN_SET(t
->scope
->protocol
, DNS_PROTOCOL_DNS
, DNS_PROTOCOL_LLMNR
))
824 /* Caching disabled? */
825 if (t
->scope
->manager
->enable_cache
== DNS_CACHE_MODE_NO
)
828 /* If validation is turned off for this transaction, but DNSSEC is on, then let's not cache this */
829 if (FLAGS_SET(t
->query_flags
, SD_RESOLVED_NO_VALIDATE
) && t
->scope
->dnssec_mode
!= DNSSEC_NO
)
832 /* Packet from localhost? */
833 if (!t
->scope
->manager
->cache_from_localhost
&&
834 in_addr_is_localhost(t
->received
->family
, &t
->received
->sender
) != 0)
837 dns_cache_put(&t
->scope
->cache
,
838 t
->scope
->manager
->enable_cache
,
840 dns_transaction_key(t
),
843 DNS_PACKET_CD(t
->received
) ? t
->received
: NULL
, /* only cache full packets with CD on,
844 * since our use case for caching them
845 * is "bypass" mode which is only
846 * enabled for CD packets. */
847 t
->answer_query_flags
,
848 t
->answer_dnssec_result
,
851 &t
->received
->sender
,
852 t
->scope
->manager
->stale_retention_usec
);
855 static bool dns_transaction_dnssec_is_live(DnsTransaction
*t
) {
860 SET_FOREACH(dt
, t
->dnssec_transactions
)
861 if (DNS_TRANSACTION_IS_LIVE(dt
->state
))
867 static int dns_transaction_dnssec_ready(DnsTransaction
*t
) {
873 /* Checks whether the auxiliary DNSSEC transactions of our transaction have completed, or are still
874 * ongoing. Returns 0, if we aren't ready for the DNSSEC validation, positive if we are. */
876 SET_FOREACH(dt
, t
->dnssec_transactions
) {
880 case DNS_TRANSACTION_NULL
:
881 case DNS_TRANSACTION_PENDING
:
882 case DNS_TRANSACTION_VALIDATING
:
886 case DNS_TRANSACTION_RCODE_FAILURE
:
887 if (!IN_SET(dt
->answer_rcode
, DNS_RCODE_NXDOMAIN
, DNS_RCODE_SERVFAIL
)) {
888 log_debug("Auxiliary DNSSEC RR query failed with rcode=%s.", FORMAT_DNS_RCODE(dt
->answer_rcode
));
892 /* Fall-through: NXDOMAIN/SERVFAIL is good enough for us. This is because some DNS servers
893 * erroneously return NXDOMAIN/SERVFAIL for empty non-terminals (Akamai...) or missing DS
894 * records (Facebook), and we need to handle that nicely, when asking for parent SOA or similar
895 * RRs to make unsigned proofs. */
897 case DNS_TRANSACTION_SUCCESS
:
901 case DNS_TRANSACTION_DNSSEC_FAILED
: {
904 /* We handle DNSSEC failures different from other errors, as we care about the DNSSEC
905 * validation result */
907 log_debug("Auxiliary DNSSEC RR query failed validation: %s%s%s%s%s%s",
908 dnssec_result_to_string(dt
->answer_dnssec_result
),
909 dt
->answer_ede_rcode
>= 0 ? " (" : "",
910 dt
->answer_ede_rcode
>= 0 ? FORMAT_DNS_EDE_RCODE(dt
->answer_ede_rcode
) : "",
911 (dt
->answer_ede_rcode
>= 0 && !isempty(dt
->answer_ede_msg
)) ? ": " : "",
912 dt
->answer_ede_rcode
>= 0 ? strempty(dt
->answer_ede_msg
) : "",
913 dt
->answer_ede_rcode
>= 0 ? ")" : "");
915 /* Copy error code over */
916 t
->answer_dnssec_result
= dt
->answer_dnssec_result
;
917 t
->answer_ede_rcode
= dt
->answer_ede_rcode
;
918 r
= free_and_strdup(&t
->answer_ede_msg
, dt
->answer_ede_msg
);
922 /* The answer would normally be replaced by the validated subset, but at this point
923 * we aren't going to bother validating the rest, so just drop it. */
924 empty
= dns_answer_new(0);
927 DNS_ANSWER_REPLACE(t
->answer
, empty
);
929 dns_transaction_complete(t
, DNS_TRANSACTION_DNSSEC_FAILED
);
934 log_debug("Auxiliary DNSSEC RR query failed with %s", dns_transaction_state_to_string(dt
->state
));
939 /* All is ready, we can go and validate */
943 /* Some auxiliary DNSSEC transaction failed for some reason. Maybe we learned something about the
944 * server due to this failure, and the feature level is now different? Let's see and restart the
945 * transaction if so. If not, let's propagate the auxiliary failure.
947 * This is particularly relevant if an auxiliary request figured out that DNSSEC doesn't work, and we
948 * are in permissive DNSSEC mode, and thus should restart things without DNSSEC magic. */
949 r
= dns_transaction_maybe_restart(t
);
953 return 0; /* don't validate just yet, we restarted things */
955 t
->answer_dnssec_result
= DNSSEC_FAILED_AUXILIARY
;
956 dns_transaction_complete(t
, DNS_TRANSACTION_DNSSEC_FAILED
);
960 static void dns_transaction_process_dnssec(DnsTransaction
*t
) {
965 /* Are there ongoing DNSSEC transactions? If so, let's wait for them. */
966 r
= dns_transaction_dnssec_ready(t
);
969 if (r
== 0) /* We aren't ready yet (or one of our auxiliary transactions failed, and we shouldn't validate now */
972 /* See if we learnt things from the additional DNSSEC transactions, that we didn't know before, and better
973 * restart the lookup immediately. */
974 r
= dns_transaction_maybe_restart(t
);
977 if (r
> 0) /* Transaction got restarted... */
980 /* All our auxiliary DNSSEC transactions are complete now. Try
981 * to validate our RRset now. */
982 r
= dns_transaction_validate_dnssec(t
);
984 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
990 if (t
->answer_dnssec_result
== DNSSEC_INCOMPATIBLE_SERVER
&&
991 t
->scope
->dnssec_mode
== DNSSEC_YES
) {
993 /* We are not in automatic downgrade mode, and the server is bad. Let's try a different server, maybe
996 if (dns_transaction_limited_retry(t
))
999 /* OK, let's give up, apparently all servers we tried didn't work. */
1000 dns_transaction_complete(t
, DNS_TRANSACTION_DNSSEC_FAILED
);
1004 if (!IN_SET(t
->answer_dnssec_result
,
1005 _DNSSEC_RESULT_INVALID
, /* No DNSSEC validation enabled */
1006 DNSSEC_VALIDATED
, /* Answer is signed and validated successfully */
1007 DNSSEC_UNSIGNED
, /* Answer is right-fully unsigned */
1008 DNSSEC_INCOMPATIBLE_SERVER
)) { /* Server does not do DNSSEC (Yay, we are downgrade attack vulnerable!) */
1009 dns_transaction_complete(t
, DNS_TRANSACTION_DNSSEC_FAILED
);
1013 if (t
->answer_dnssec_result
== DNSSEC_INCOMPATIBLE_SERVER
)
1014 dns_server_warn_downgrade(t
->server
);
1016 dns_transaction_cache_answer(t
);
1018 if (t
->answer_rcode
== DNS_RCODE_SUCCESS
)
1019 dns_transaction_complete(t
, DNS_TRANSACTION_SUCCESS
);
1021 dns_transaction_complete(t
, DNS_TRANSACTION_RCODE_FAILURE
);
1026 dns_transaction_complete_errno(t
, r
);
1029 static int dns_transaction_has_positive_answer(DnsTransaction
*t
, DnsAnswerFlags
*flags
) {
1034 /* Checks whether the answer is positive, i.e. either a direct
1035 * answer to the question, or a CNAME/DNAME for it */
1037 r
= dns_answer_match_key(t
->answer
, dns_transaction_key(t
), flags
);
1041 r
= dns_answer_find_cname_or_dname(t
->answer
, dns_transaction_key(t
), NULL
, flags
);
1048 static int dns_transaction_fix_rcode(DnsTransaction
*t
) {
1053 /* Fix up the RCODE to SUCCESS if we get at least one matching RR in a response. Note that this contradicts the
1054 * DNS RFCs a bit. Specifically, RFC 6604 Section 3 clarifies that the RCODE shall say something about a
1055 * CNAME/DNAME chain element coming after the last chain element contained in the message, and not the first
1056 * one included. However, it also indicates that not all DNS servers implement this correctly. Moreover, when
1057 * using DNSSEC we usually only can prove the first element of a CNAME/DNAME chain anyway, hence let's settle
1058 * on always processing the RCODE as referring to the immediate look-up we do, i.e. the first element of a
1059 * CNAME/DNAME chain. This way, we uniformly handle CNAME/DNAME chains, regardless if the DNS server
1060 * incorrectly implements RCODE, whether DNSSEC is in use, or whether the DNS server only supplied us with an
1061 * incomplete CNAME/DNAME chain.
1063 * Or in other words: if we get at least one positive reply in a message we patch NXDOMAIN to become SUCCESS,
1064 * and then rely on the CNAME chasing logic to figure out that there's actually a CNAME error with a new
1067 if (t
->answer_rcode
!= DNS_RCODE_NXDOMAIN
)
1070 r
= dns_transaction_has_positive_answer(t
, NULL
);
1074 t
->answer_rcode
= DNS_RCODE_SUCCESS
;
1078 void dns_transaction_process_reply(DnsTransaction
*t
, DnsPacket
*p
, bool encrypted
) {
1079 bool retry_with_tcp
= false;
1085 assert(t
->scope
->manager
);
1087 if (t
->state
!= DNS_TRANSACTION_PENDING
)
1090 /* Increment the total failure counter only when it is the first attempt at querying and the upstream
1091 * server returns a failure response code. This ensures a more accurate count of the number of queries
1092 * that received a failure response code, as it doesn't consider retries. */
1094 if (t
->n_attempts
== 1 && !IN_SET(DNS_PACKET_RCODE(p
), DNS_RCODE_SUCCESS
, DNS_RCODE_NXDOMAIN
))
1095 t
->scope
->manager
->n_failure_responses_total
++;
1097 /* Note that this call might invalidate the query. Callers
1098 * should hence not attempt to access the query or transaction
1099 * after calling this function. */
1101 log_debug("Processing incoming packet of size %zu on transaction %" PRIu16
" (rcode=%s).",
1103 t
->id
, FORMAT_DNS_RCODE(DNS_PACKET_RCODE(p
)));
1105 switch (t
->scope
->protocol
) {
1107 case DNS_PROTOCOL_LLMNR
:
1108 /* For LLMNR we will not accept any packets from other interfaces */
1110 if (p
->ifindex
!= dns_scope_ifindex(t
->scope
))
1113 if (p
->family
!= t
->scope
->family
)
1116 /* Tentative packets are not full responses but still
1117 * useful for identifying uniqueness conflicts during
1119 if (DNS_PACKET_LLMNR_T(p
)) {
1120 dns_transaction_tentative(t
, p
);
1126 case DNS_PROTOCOL_MDNS
:
1127 /* For mDNS we will not accept any packets from other interfaces */
1129 if (p
->ifindex
!= dns_scope_ifindex(t
->scope
))
1132 if (p
->family
!= t
->scope
->family
)
1137 case DNS_PROTOCOL_DNS
:
1138 /* Note that we do not need to verify the
1139 * addresses/port numbers of incoming traffic, as we
1140 * invoked connect() on our UDP socket in which case
1141 * the kernel already does the needed verification for
1146 assert_not_reached();
1149 if (t
->received
!= p
)
1150 DNS_PACKET_REPLACE(t
->received
, dns_packet_ref(p
));
1152 t
->answer_source
= DNS_TRANSACTION_NETWORK
;
1154 if (p
->ipproto
== IPPROTO_TCP
) {
1155 if (DNS_PACKET_TC(p
)) {
1156 /* Truncated via TCP? Somebody must be fucking with us */
1157 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
1161 if (DNS_PACKET_ID(p
) != t
->id
) {
1162 /* Not the reply to our query? Somebody must be fucking with us */
1163 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
1168 if (DNS_PACKET_TC(p
)) {
1170 /* Truncated packets for mDNS are not allowed. Give up immediately. */
1171 if (t
->scope
->protocol
== DNS_PROTOCOL_MDNS
) {
1172 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
1176 /* Response was truncated, let's try again with good old TCP */
1177 log_debug("Reply truncated, retrying via TCP.");
1178 retry_with_tcp
= true;
1180 } else if (t
->scope
->protocol
== DNS_PROTOCOL_DNS
&&
1181 DNS_PACKET_IS_FRAGMENTED(p
)) {
1183 /* Report the fragment size, so that we downgrade from LARGE to regular EDNS0 if needed */
1185 dns_server_packet_udp_fragmented(t
->server
, dns_packet_size_unfragmented(p
));
1187 if (t
->current_feature_level
> DNS_SERVER_FEATURE_LEVEL_UDP
) {
1188 /* Packet was fragmented. Let's retry with TCP to avoid fragmentation attack
1189 * issues. (We don't do that on the lowest feature level however, since crappy DNS
1190 * servers often do not implement TCP, hence falling back to TCP on fragmentation is
1191 * counter-productive there.) */
1193 log_debug("Reply fragmented, retrying via TCP. (Largest fragment size: %zu; Datagram size: %zu)",
1194 p
->fragsize
, p
->size
);
1195 retry_with_tcp
= true;
1199 if (retry_with_tcp
) {
1200 r
= dns_transaction_emit_tcp(t
);
1202 /* No servers found? Damn! */
1203 dns_transaction_complete(t
, DNS_TRANSACTION_NO_SERVERS
);
1206 if (r
== -EOPNOTSUPP
) {
1207 /* Tried to ask for DNSSEC RRs, on a server that doesn't do DNSSEC */
1208 dns_transaction_complete(t
, DNS_TRANSACTION_RR_TYPE_UNSUPPORTED
);
1212 /* On LLMNR, if we cannot connect to the host,
1213 * we immediately give up */
1214 if (t
->scope
->protocol
!= DNS_PROTOCOL_DNS
)
1217 /* On DNS, couldn't send? Try immediately again, with a new server */
1218 if (dns_transaction_limited_retry(t
))
1221 /* No new server to try, give up */
1222 dns_transaction_complete(t
, DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
);
1228 /* After the superficial checks, actually parse the message. */
1229 r
= dns_packet_extract(p
);
1232 dns_server_packet_invalid(t
->server
, t
->current_feature_level
);
1234 r
= dns_transaction_maybe_restart(t
);
1237 if (r
> 0) /* Transaction got restarted... */
1241 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
1245 switch (t
->scope
->protocol
) {
1247 case DNS_PROTOCOL_DNS
: {
1250 (void) dns_packet_ede_rcode(p
, &t
->answer_ede_rcode
, &t
->answer_ede_msg
);
1253 IN_SET(DNS_PACKET_RCODE(p
), DNS_RCODE_FORMERR
, DNS_RCODE_SERVFAIL
, DNS_RCODE_NOTIMP
)) {
1254 /* If the server has replied with detailed error data, using a degraded feature set
1255 * will likely not help anyone. Examine the detailed error to determine the best
1256 * course of action. */
1257 if (t
->answer_ede_rcode
>= 0 && DNS_PACKET_RCODE(p
) == DNS_RCODE_SERVFAIL
) {
1258 /* These codes are related to DNSSEC configuration errors. If accurate,
1259 * this is the domain operator's problem, and retrying won't help. */
1260 if (dns_ede_rcode_is_dnssec(t
->answer_ede_rcode
)) {
1261 log_debug("Server returned error: %s (%s%s%s). Lookup failed.",
1262 FORMAT_DNS_RCODE(DNS_PACKET_RCODE(p
)),
1263 FORMAT_DNS_EDE_RCODE(t
->answer_ede_rcode
),
1264 isempty(t
->answer_ede_msg
) ? "" : ": ",
1265 strempty(t
->answer_ede_msg
));
1267 t
->answer_dnssec_result
= DNSSEC_UPSTREAM_FAILURE
;
1268 dns_transaction_complete(t
, DNS_TRANSACTION_DNSSEC_FAILED
);
1272 /* These codes probably indicate a transient error. Let's try again. */
1273 if (IN_SET(t
->answer_ede_rcode
, DNS_EDE_RCODE_NOT_READY
, DNS_EDE_RCODE_NET_ERROR
)) {
1274 log_debug("Server returned error: %s (%s%s%s), retrying transaction.",
1275 FORMAT_DNS_RCODE(DNS_PACKET_RCODE(p
)),
1276 FORMAT_DNS_EDE_RCODE(t
->answer_ede_rcode
),
1277 isempty(t
->answer_ede_msg
) ? "" : ": ",
1278 strempty(t
->answer_ede_msg
));
1279 dns_transaction_retry(t
, false);
1283 /* OK, the query failed, but we still shouldn't degrade the feature set for
1285 log_debug("Server returned error: %s (%s%s%s)",
1286 FORMAT_DNS_RCODE(DNS_PACKET_RCODE(p
)),
1287 FORMAT_DNS_EDE_RCODE(t
->answer_ede_rcode
),
1288 isempty(t
->answer_ede_msg
) ? "" : ": ",
1289 strempty(t
->answer_ede_msg
));
1293 /* Request failed, immediately try again with reduced features */
1295 if (t
->current_feature_level
<= DNS_SERVER_FEATURE_LEVEL_UDP
) {
1297 /* This was already at UDP feature level? If so, it doesn't make sense to downgrade
1298 * this transaction anymore, but let's see if it might make sense to send the request
1299 * to a different DNS server instead. If not let's process the response, and accept the
1300 * rcode. Note that we don't retry on TCP, since that's a suitable way to mitigate
1301 * packet loss, but is not going to give us better rcodes should we actually have
1302 * managed to get them already at UDP level. */
1304 if (dns_transaction_limited_retry(t
))
1307 /* Give up, accept the rcode */
1308 log_debug("Server returned error: %s", FORMAT_DNS_RCODE(DNS_PACKET_RCODE(p
)));
1312 /* SERVFAIL can happen for many reasons and may be transient.
1313 * To avoid unnecessary downgrades retry once with the initial level.
1314 * Check for clamp_feature_level_servfail having an invalid value as a sign that this is the
1315 * first attempt to downgrade. If so, clamp to the current value so that the transaction
1316 * is retried without actually downgrading. If the next try also fails we will downgrade by
1317 * hitting the else branch below. */
1318 if (DNS_PACKET_RCODE(p
) == DNS_RCODE_SERVFAIL
&&
1319 t
->clamp_feature_level_servfail
< 0) {
1320 t
->clamp_feature_level_servfail
= t
->current_feature_level
;
1321 log_debug("Server returned error %s, retrying transaction.",
1322 FORMAT_DNS_RCODE(DNS_PACKET_RCODE(p
)));
1324 /* Reduce this feature level by one and try again. */
1325 switch (t
->current_feature_level
) {
1326 case DNS_SERVER_FEATURE_LEVEL_TLS_DO
:
1327 t
->clamp_feature_level_servfail
= DNS_SERVER_FEATURE_LEVEL_TLS_PLAIN
;
1329 case DNS_SERVER_FEATURE_LEVEL_TLS_PLAIN
+ 1:
1330 /* Skip plain TLS when TLS is not supported */
1331 t
->clamp_feature_level_servfail
= DNS_SERVER_FEATURE_LEVEL_TLS_PLAIN
- 1;
1334 t
->clamp_feature_level_servfail
= t
->current_feature_level
- 1;
1337 log_debug("Server returned error %s, retrying transaction with reduced feature level %s.",
1338 FORMAT_DNS_RCODE(DNS_PACKET_RCODE(p
)),
1339 dns_server_feature_level_to_string(t
->clamp_feature_level_servfail
));
1342 dns_transaction_retry(t
, false /* use the same server */);
1346 if (DNS_PACKET_RCODE(p
) == DNS_RCODE_REFUSED
) {
1347 /* This server refused our request? If so, try again, use a different server */
1348 if (t
->answer_ede_rcode
>= 0)
1349 log_debug("Server returned REFUSED (%s), switching servers, and retrying.",
1350 FORMAT_DNS_EDE_RCODE(t
->answer_ede_rcode
));
1352 log_debug("Server returned REFUSED, switching servers, and retrying.");
1354 if (dns_transaction_limited_retry(t
))
1360 if (DNS_PACKET_TC(p
))
1361 dns_server_packet_truncated(t
->server
, t
->current_feature_level
);
1366 case DNS_PROTOCOL_LLMNR
:
1367 case DNS_PROTOCOL_MDNS
:
1368 dns_scope_packet_received(t
->scope
, p
->timestamp
- t
->start_usec
);
1372 assert_not_reached();
1376 /* Report that we successfully received a valid packet with a good rcode after we initially got a bad
1377 * rcode and subsequently downgraded the protocol */
1379 if (IN_SET(DNS_PACKET_RCODE(p
), DNS_RCODE_SUCCESS
, DNS_RCODE_NXDOMAIN
) &&
1380 t
->clamp_feature_level_servfail
!= _DNS_SERVER_FEATURE_LEVEL_INVALID
)
1381 dns_server_packet_rcode_downgrade(t
->server
, t
->clamp_feature_level_servfail
);
1383 /* Report that the OPT RR was missing */
1385 dns_server_packet_bad_opt(t
->server
, t
->current_feature_level
);
1387 /* Report that the server didn't copy our query DO bit from request to response */
1388 if (DNS_PACKET_DO(t
->sent
) && !DNS_PACKET_DO(t
->received
))
1389 dns_server_packet_do_off(t
->server
, t
->current_feature_level
);
1391 /* Report that we successfully received a packet. We keep track of the largest packet
1392 * size/fragment size we got. Which is useful for announcing the EDNS(0) packet size we can
1393 * receive to our server. */
1394 dns_server_packet_received(t
->server
, p
->ipproto
, t
->current_feature_level
, dns_packet_size_unfragmented(p
));
1397 /* See if we know things we didn't know before that indicate we better restart the lookup immediately. */
1398 r
= dns_transaction_maybe_restart(t
);
1401 if (r
> 0) /* Transaction got restarted... */
1404 /* When dealing with protocols other than mDNS only consider responses with equivalent query section
1405 * to the request. For mDNS this check doesn't make sense, because the section 6 of RFC6762 states
1406 * that "Multicast DNS responses MUST NOT contain any questions in the Question Section". */
1407 if (t
->scope
->protocol
!= DNS_PROTOCOL_MDNS
) {
1408 r
= dns_packet_is_reply_for(p
, dns_transaction_key(t
));
1412 dns_transaction_complete(t
, DNS_TRANSACTION_INVALID_REPLY
);
1417 /* Install the answer as answer to the transaction. We ref the answer twice here: the main `answer`
1418 * field is later replaced by the DNSSEC validated subset. The 'answer_auxiliary' field carries the
1419 * original complete record set, including RRSIG and friends. We use this when passing data to
1420 * clients that ask for DNSSEC metadata. */
1421 DNS_ANSWER_REPLACE(t
->answer
, dns_answer_ref(p
->answer
));
1422 t
->answer_rcode
= DNS_PACKET_RCODE(p
);
1423 t
->answer_dnssec_result
= _DNSSEC_RESULT_INVALID
;
1424 SET_FLAG(t
->answer_query_flags
, SD_RESOLVED_AUTHENTICATED
, false);
1425 SET_FLAG(t
->answer_query_flags
, SD_RESOLVED_CONFIDENTIAL
, encrypted
);
1427 r
= dns_transaction_fix_rcode(t
);
1431 /* Block GC while starting requests for additional DNSSEC RRs */
1433 r
= dns_transaction_request_dnssec_keys(t
);
1436 /* Maybe the transaction is ready for GC'ing now? If so, free it and return. */
1437 if (!dns_transaction_gc(t
))
1440 /* Requesting additional keys might have resulted in this transaction to fail, since the auxiliary
1441 * request failed for some reason. If so, we are not in pending state anymore, and we should exit
1443 if (t
->state
!= DNS_TRANSACTION_PENDING
)
1448 /* There are DNSSEC transactions pending now. Update the state accordingly. */
1449 t
->state
= DNS_TRANSACTION_VALIDATING
;
1450 dns_transaction_close_connection(t
, true);
1451 dns_transaction_stop_timeout(t
);
1455 dns_transaction_process_dnssec(t
);
1459 dns_transaction_complete_errno(t
, r
);
1462 static int on_dns_packet(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
1463 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
1464 DnsTransaction
*t
= ASSERT_PTR(userdata
);
1469 r
= manager_recv(t
->scope
->manager
, fd
, DNS_PROTOCOL_DNS
, &p
);
1471 if (ERRNO_IS_DISCONNECT(r
)) {
1474 /* UDP connection failures get reported via ICMP and then are possibly delivered to us on the
1475 * next recvmsg(). Treat this like a lost packet. */
1477 log_debug_errno(r
, "Connection failure for DNS UDP packet: %m");
1478 assert_se(sd_event_now(t
->scope
->manager
->event
, CLOCK_BOOTTIME
, &usec
) >= 0);
1479 dns_server_packet_lost(t
->server
, IPPROTO_UDP
, t
->current_feature_level
);
1481 dns_transaction_close_connection(t
, /* use_graveyard = */ false);
1483 if (dns_transaction_limited_retry(t
)) /* Try a different server */
1486 dns_transaction_complete_errno(t
, r
);
1490 /* Spurious wakeup without any data */
1493 r
= dns_packet_validate_reply(p
);
1495 log_debug_errno(r
, "Received invalid DNS packet as response, ignoring: %m");
1499 log_debug("Received inappropriate DNS packet as response, ignoring.");
1503 if (DNS_PACKET_ID(p
) != t
->id
) {
1504 log_debug("Received packet with incorrect transaction ID, ignoring.");
1508 dns_transaction_process_reply(t
, p
, false);
1512 static int dns_transaction_emit_udp(DnsTransaction
*t
) {
1517 if (t
->scope
->protocol
== DNS_PROTOCOL_DNS
) {
1519 r
= dns_transaction_pick_server(t
);
1523 if (manager_server_is_stub(t
->scope
->manager
, t
->server
))
1526 if (t
->current_feature_level
< DNS_SERVER_FEATURE_LEVEL_UDP
|| DNS_SERVER_FEATURE_LEVEL_IS_TLS(t
->current_feature_level
))
1527 return -EAGAIN
; /* Sorry, can't do UDP, try TCP! */
1529 if (!t
->bypass
&& !dns_server_dnssec_supported(t
->server
) && dns_type_is_dnssec(dns_transaction_key(t
)->type
))
1532 if (r
> 0 || t
->dns_udp_fd
< 0) { /* Server changed, or no connection yet. */
1535 dns_transaction_close_connection(t
, true);
1537 /* Before we allocate a new UDP socket, let's process the graveyard a bit to free some fds */
1538 manager_socket_graveyard_process(t
->scope
->manager
);
1540 fd
= dns_scope_socket_udp(t
->scope
, t
->server
);
1544 r
= sd_event_add_io(t
->scope
->manager
->event
, &t
->dns_udp_event_source
, fd
, EPOLLIN
, on_dns_packet
, t
);
1550 (void) sd_event_source_set_description(t
->dns_udp_event_source
, "dns-transaction-udp");
1555 r
= dns_server_adjust_opt(t
->server
, t
->sent
, t
->current_feature_level
);
1560 dns_transaction_close_connection(t
, true);
1562 r
= dns_scope_emit_udp(t
->scope
, t
->dns_udp_fd
, t
->server
? t
->server
->family
: AF_UNSPEC
, t
->sent
);
1566 dns_transaction_reset_answer(t
);
1571 static int on_transaction_timeout(sd_event_source
*s
, usec_t usec
, void *userdata
) {
1572 DnsTransaction
*t
= ASSERT_PTR(userdata
);
1576 t
->seen_timeout
= true;
1578 if (t
->initial_jitter_scheduled
&& !t
->initial_jitter_elapsed
) {
1579 log_debug("Initial jitter phase for transaction %" PRIu16
" elapsed.", t
->id
);
1580 t
->initial_jitter_elapsed
= true;
1582 /* Timeout reached? Increase the timeout for the server used */
1583 switch (t
->scope
->protocol
) {
1585 case DNS_PROTOCOL_DNS
:
1587 dns_server_packet_lost(t
->server
, t
->stream
? IPPROTO_TCP
: IPPROTO_UDP
, t
->current_feature_level
);
1590 case DNS_PROTOCOL_LLMNR
:
1591 case DNS_PROTOCOL_MDNS
:
1592 dns_scope_packet_lost(t
->scope
, usec
- t
->start_usec
);
1596 assert_not_reached();
1599 log_debug("Timeout reached on transaction %" PRIu16
".", t
->id
);
1602 dns_transaction_retry(t
, /* next_server= */ true); /* try a different server, but given this means
1603 * packet loss, let's do so even if we already
1608 static int dns_transaction_setup_timeout(
1610 usec_t timeout_usec
/* relative */,
1611 usec_t next_usec
/* CLOCK_BOOTTIME */) {
1617 dns_transaction_stop_timeout(t
);
1619 r
= sd_event_add_time_relative(
1620 t
->scope
->manager
->event
,
1621 &t
->timeout_event_source
,
1624 on_transaction_timeout
, t
);
1628 (void) sd_event_source_set_description(t
->timeout_event_source
, "dns-transaction-timeout");
1630 t
->next_attempt_after
= next_usec
;
1631 t
->state
= DNS_TRANSACTION_PENDING
;
1635 static usec_t
transaction_get_resend_timeout(DnsTransaction
*t
) {
1639 switch (t
->scope
->protocol
) {
1641 case DNS_PROTOCOL_DNS
:
1643 /* When we do TCP, grant a much longer timeout, as in this case there's no need for us to quickly
1644 * resend, as the kernel does that anyway for us, and we really don't want to interrupt it in that
1647 return TRANSACTION_TCP_TIMEOUT_USEC
;
1649 return DNS_TIMEOUT_USEC
;
1651 case DNS_PROTOCOL_MDNS
:
1653 return MDNS_PROBING_INTERVAL_USEC
;
1655 /* See RFC 6762 Section 5.1 suggests that timeout should be a few seconds. */
1656 assert(t
->n_attempts
> 0);
1657 return (1 << (t
->n_attempts
- 1)) * USEC_PER_SEC
;
1659 case DNS_PROTOCOL_LLMNR
:
1660 return t
->scope
->resend_timeout
;
1663 assert_not_reached();
1667 static void dns_transaction_randomize_answer(DnsTransaction
*t
) {
1672 /* Randomizes the order of the answer array. This is done for all cached responses, so that we return
1673 * a different order each time. We do this only for DNS traffic, in order to do some minimal, crappy
1674 * load balancing. We don't do this for LLMNR or mDNS, since the order (preferring link-local
1675 * addresses, and such like) might have meaning there, and load balancing is pointless. */
1677 if (t
->scope
->protocol
!= DNS_PROTOCOL_DNS
)
1680 /* No point in randomizing, if there's just one RR */
1681 if (dns_answer_size(t
->answer
) <= 1)
1684 r
= dns_answer_reserve_or_clone(&t
->answer
, 0);
1685 if (r
< 0) /* If this fails, just don't randomize, this is non-essential stuff after all */
1686 return (void) log_debug_errno(r
, "Failed to clone answer record, not randomizing RR order of answer: %m");
1688 dns_answer_randomize(t
->answer
);
1691 static int dns_transaction_prepare(DnsTransaction
*t
, usec_t ts
) {
1696 /* Returns 0 if dns_transaction_complete() has been called. In that case the transaction and query
1697 * candidate objects may have been invalidated and must not be accessed. Returns 1 if the transaction
1698 * has been prepared. */
1700 dns_transaction_stop_timeout(t
);
1702 if (t
->n_attempts
== 1 && t
->seen_timeout
)
1703 t
->scope
->manager
->n_timeouts_total
++;
1705 if (!dns_scope_network_good(t
->scope
)) {
1706 dns_transaction_complete(t
, DNS_TRANSACTION_NETWORK_DOWN
);
1710 if (t
->n_attempts
>= TRANSACTION_ATTEMPTS_MAX(t
->scope
->protocol
)) {
1711 DnsTransactionState result
;
1713 if (t
->scope
->protocol
== DNS_PROTOCOL_LLMNR
)
1714 /* If we didn't find anything on LLMNR, it's not an error, but a failure to resolve
1716 result
= DNS_TRANSACTION_NOT_FOUND
;
1718 result
= DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
;
1720 dns_transaction_complete(t
, result
);
1724 if (t
->scope
->protocol
== DNS_PROTOCOL_LLMNR
&& t
->tried_stream
) {
1725 /* If we already tried via a stream, then we don't
1726 * retry on LLMNR. See RFC 4795, Section 2.7. */
1727 dns_transaction_complete(t
, DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
);
1734 dns_transaction_reset_answer(t
);
1735 dns_transaction_flush_dnssec_transactions(t
);
1737 /* Check the trust anchor. Do so only on classic DNS, since DNSSEC does not apply otherwise. */
1738 if (t
->scope
->protocol
== DNS_PROTOCOL_DNS
&&
1739 !FLAGS_SET(t
->query_flags
, SD_RESOLVED_NO_TRUST_ANCHOR
)) {
1740 r
= dns_trust_anchor_lookup_positive(&t
->scope
->manager
->trust_anchor
, dns_transaction_key(t
), &t
->answer
);
1744 t
->answer_rcode
= DNS_RCODE_SUCCESS
;
1745 t
->answer_source
= DNS_TRANSACTION_TRUST_ANCHOR
;
1746 SET_FLAG(t
->answer_query_flags
, SD_RESOLVED_AUTHENTICATED
|SD_RESOLVED_CONFIDENTIAL
, true);
1747 dns_transaction_complete(t
, DNS_TRANSACTION_SUCCESS
);
1751 if (dns_name_is_root(dns_resource_key_name(dns_transaction_key(t
))) &&
1752 dns_transaction_key(t
)->type
== DNS_TYPE_DS
) {
1754 /* Hmm, this is a request for the root DS? A DS RR doesn't exist in the root zone,
1755 * and if our trust anchor didn't know it either, this means we cannot do any DNSSEC
1758 if (t
->scope
->dnssec_mode
== DNSSEC_ALLOW_DOWNGRADE
) {
1759 /* We are in downgrade mode. In this case, synthesize an unsigned empty
1760 * response, so that the any lookup depending on this one can continue
1761 * assuming there was no DS, and hence the root zone was unsigned. */
1763 t
->answer_rcode
= DNS_RCODE_SUCCESS
;
1764 t
->answer_source
= DNS_TRANSACTION_TRUST_ANCHOR
;
1765 SET_FLAG(t
->answer_query_flags
, SD_RESOLVED_AUTHENTICATED
, false);
1766 SET_FLAG(t
->answer_query_flags
, SD_RESOLVED_CONFIDENTIAL
, true);
1767 dns_transaction_complete(t
, DNS_TRANSACTION_SUCCESS
);
1769 /* If we are not in downgrade mode, then fail the lookup, because we cannot
1770 * reasonably answer it. There might be DS RRs, but we don't know them, and
1771 * the DNS server won't tell them to us (and even if it would, we couldn't
1772 * validate and trust them. */
1773 dns_transaction_complete(t
, DNS_TRANSACTION_NO_TRUST_ANCHOR
);
1779 /* Check the zone. */
1780 if (!FLAGS_SET(t
->query_flags
, SD_RESOLVED_NO_ZONE
)) {
1781 r
= dns_zone_lookup(&t
->scope
->zone
, dns_transaction_key(t
), dns_scope_ifindex(t
->scope
), &t
->answer
, NULL
, NULL
);
1785 t
->answer_rcode
= DNS_RCODE_SUCCESS
;
1786 t
->answer_source
= DNS_TRANSACTION_ZONE
;
1787 SET_FLAG(t
->answer_query_flags
, SD_RESOLVED_AUTHENTICATED
|SD_RESOLVED_CONFIDENTIAL
, true);
1788 dns_transaction_complete(t
, DNS_TRANSACTION_SUCCESS
);
1793 /* Check the cache. */
1794 if (!FLAGS_SET(t
->query_flags
, SD_RESOLVED_NO_CACHE
)) {
1796 /* Before trying the cache, let's make sure we figured out a server to use. Should this cause
1797 * a change of server this might flush the cache. */
1798 (void) dns_scope_get_dns_server(t
->scope
);
1800 /* Let's then prune all outdated entries */
1801 dns_cache_prune(&t
->scope
->cache
);
1803 /* For the initial attempt or when no stale data is requested, disable serve stale
1804 * and answer the question from the cache (honors ttl property).
1805 * On the second attempt, if StaleRetentionSec is greater than zero,
1806 * try to answer the question using stale date (honors until property) */
1807 uint64_t query_flags
= t
->query_flags
;
1808 if (t
->n_attempts
== 1 || t
->scope
->manager
->stale_retention_usec
== 0)
1809 query_flags
|= SD_RESOLVED_NO_STALE
;
1811 r
= dns_cache_lookup(
1813 dns_transaction_key(t
),
1818 &t
->answer_query_flags
,
1819 &t
->answer_dnssec_result
);
1823 dns_transaction_randomize_answer(t
);
1825 if (t
->bypass
&& t
->scope
->protocol
== DNS_PROTOCOL_DNS
&& !t
->received
)
1826 /* When bypass mode is on, do not use cached data unless it came with a full
1828 dns_transaction_reset_answer(t
);
1830 if (t
->n_attempts
> 1 && !FLAGS_SET(query_flags
, SD_RESOLVED_NO_STALE
)) {
1832 if (t
->answer_rcode
== DNS_RCODE_SUCCESS
) {
1833 if (t
->seen_timeout
)
1834 t
->scope
->manager
->n_timeouts_served_stale_total
++;
1836 t
->scope
->manager
->n_failure_responses_served_stale_total
++;
1839 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
1840 log_debug("Serve Stale response rcode=%s for %s",
1841 FORMAT_DNS_RCODE(t
->answer_rcode
),
1842 dns_resource_key_to_string(dns_transaction_key(t
), key_str
, sizeof key_str
));
1845 t
->answer_source
= DNS_TRANSACTION_CACHE
;
1846 if (t
->answer_rcode
== DNS_RCODE_SUCCESS
)
1847 dns_transaction_complete(t
, DNS_TRANSACTION_SUCCESS
);
1850 (void) dns_packet_ede_rcode(t
->received
, &t
->answer_ede_rcode
, &t
->answer_ede_msg
);
1852 dns_transaction_complete(t
, DNS_TRANSACTION_RCODE_FAILURE
);
1859 if (FLAGS_SET(t
->query_flags
, SD_RESOLVED_NO_NETWORK
)) {
1860 dns_transaction_complete(t
, DNS_TRANSACTION_NO_SOURCE
);
1867 static int dns_packet_append_zone(DnsPacket
*p
, DnsTransaction
*t
, DnsResourceKey
*k
, unsigned *nscount
) {
1868 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
1876 if (k
->type
!= DNS_TYPE_ANY
)
1879 r
= dns_zone_lookup(&t
->scope
->zone
, k
, t
->scope
->link
->ifindex
, &answer
, NULL
, &tentative
);
1883 return dns_packet_append_answer(p
, answer
, nscount
);
1886 static int mdns_make_dummy_packet(DnsTransaction
*t
, DnsPacket
**ret_packet
, Set
**ret_keys
) {
1887 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
1888 _cleanup_set_free_ Set
*keys
= NULL
;
1889 bool add_known_answers
= false;
1896 assert(t
->scope
->protocol
== DNS_PROTOCOL_MDNS
);
1900 r
= dns_packet_new_query(&p
, t
->scope
->protocol
, 0, false);
1904 r
= dns_packet_append_key(p
, dns_transaction_key(t
), 0, NULL
);
1910 if (dns_key_is_shared(dns_transaction_key(t
)))
1911 add_known_answers
= true;
1913 r
= dns_packet_append_zone(p
, t
, dns_transaction_key(t
), NULL
);
1917 /* Save appended keys */
1918 r
= set_ensure_put(&keys
, &dns_resource_key_hash_ops
, dns_transaction_key(t
));
1922 assert_se(sd_event_now(t
->scope
->manager
->event
, CLOCK_BOOTTIME
, &ts
) >= 0);
1924 LIST_FOREACH(transactions_by_scope
, other
, t
->scope
->transactions
) {
1926 /* Skip ourselves */
1930 if (other
->state
!= DNS_TRANSACTION_PENDING
)
1933 if (other
->next_attempt_after
> ts
)
1936 if (!set_contains(keys
, dns_transaction_key(other
))) {
1937 size_t saved_packet_size
;
1939 r
= dns_packet_append_key(p
, dns_transaction_key(other
), 0, &saved_packet_size
);
1940 /* If we can't stuff more questions into the packet, just give up.
1941 * One of the 'other' transactions will fire later and take care of the rest. */
1947 r
= dns_packet_append_zone(p
, t
, dns_transaction_key(other
), NULL
);
1948 if (r
== -EMSGSIZE
) {
1949 dns_packet_truncate(p
, saved_packet_size
);
1955 r
= set_ensure_put(&keys
, &dns_resource_key_hash_ops
, dns_transaction_key(other
));
1960 r
= dns_transaction_prepare(other
, ts
);
1964 /* In this case, not only this transaction, but multiple transactions may be
1965 * freed. Hence, we need to restart the loop. */
1968 usec_t timeout
= transaction_get_resend_timeout(other
);
1969 r
= dns_transaction_setup_timeout(other
, timeout
, usec_add(ts
, timeout
));
1973 if (dns_key_is_shared(dns_transaction_key(other
)))
1974 add_known_answers
= true;
1977 if (qdcount
>= UINT16_MAX
)
1981 DNS_PACKET_HEADER(p
)->qdcount
= htobe16(qdcount
);
1983 /* Append known answers section if we're asking for any shared record */
1984 if (add_known_answers
) {
1985 r
= dns_cache_export_shared_to_packet(&t
->scope
->cache
, p
, ts
, 0);
1990 *ret_packet
= TAKE_PTR(p
);
1991 *ret_keys
= TAKE_PTR(keys
);
1992 return add_known_answers
;
1995 static int dns_transaction_make_packet_mdns(DnsTransaction
*t
) {
1996 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
, *dummy
= NULL
;
1997 _cleanup_set_free_ Set
*keys
= NULL
;
1998 bool add_known_answers
;
2004 assert(t
->scope
->protocol
== DNS_PROTOCOL_MDNS
);
2006 /* Discard any previously prepared packet, so we can start over and coalesce again */
2007 t
->sent
= dns_packet_unref(t
->sent
);
2009 /* First, create a dummy packet to calculate the number of known answers to be appended in the first packet. */
2011 r
= mdns_make_dummy_packet(t
, &dummy
, &keys
);
2017 add_known_answers
= r
;
2021 /* Then, create actual packet. */
2022 r
= dns_packet_new_query(&p
, t
->scope
->protocol
, 0, false);
2028 SET_FOREACH(k
, keys
) {
2029 r
= dns_packet_append_key(p
, k
, 0, NULL
);
2034 DNS_PACKET_HEADER(p
)->qdcount
= htobe16(c
);
2037 if (add_known_answers
) {
2040 assert_se(sd_event_now(t
->scope
->manager
->event
, CLOCK_BOOTTIME
, &ts
) >= 0);
2042 r
= dns_cache_export_shared_to_packet(&t
->scope
->cache
, p
, ts
, be16toh(DNS_PACKET_HEADER(dummy
)->ancount
));
2049 SET_FOREACH(k
, keys
) {
2050 r
= dns_packet_append_zone(p
, t
, k
, &c
);
2054 DNS_PACKET_HEADER(p
)->nscount
= htobe16(c
);
2056 t
->sent
= TAKE_PTR(p
);
2060 static int dns_transaction_make_packet(DnsTransaction
*t
) {
2061 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
2066 if (t
->scope
->protocol
== DNS_PROTOCOL_MDNS
)
2067 return dns_transaction_make_packet_mdns(t
);
2072 if (t
->bypass
&& t
->bypass
->protocol
== t
->scope
->protocol
) {
2073 /* If bypass logic is enabled and the protocol if the original packet and our scope match,
2074 * take the original packet, copy it, and patch in our new ID */
2075 r
= dns_packet_dup(&p
, t
->bypass
);
2079 r
= dns_packet_new_query(
2080 &p
, t
->scope
->protocol
,
2081 /* min_alloc_dsize = */ 0,
2082 /* dnssec_cd = */ !FLAGS_SET(t
->query_flags
, SD_RESOLVED_NO_VALIDATE
) &&
2083 t
->scope
->dnssec_mode
!= DNSSEC_NO
);
2087 r
= dns_packet_append_key(p
, dns_transaction_key(t
), 0, NULL
);
2091 DNS_PACKET_HEADER(p
)->qdcount
= htobe16(1);
2094 DNS_PACKET_HEADER(p
)->id
= t
->id
;
2096 t
->sent
= TAKE_PTR(p
);
2100 int dns_transaction_go(DnsTransaction
*t
) {
2103 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
2107 /* Returns > 0 if the transaction is now pending, returns 0 if could be processed immediately and has
2108 * finished now. In the latter case, the transaction and query candidate objects must not be accessed.
2111 assert_se(sd_event_now(t
->scope
->manager
->event
, CLOCK_BOOTTIME
, &ts
) >= 0);
2113 r
= dns_transaction_prepare(t
, ts
);
2117 log_debug("Firing %s transaction %" PRIu16
" for <%s> scope %s on %s/%s (validate=%s).",
2118 t
->bypass
? "bypass" : "regular",
2120 dns_resource_key_to_string(dns_transaction_key(t
), key_str
, sizeof key_str
),
2121 dns_protocol_to_string(t
->scope
->protocol
),
2122 t
->scope
->link
? t
->scope
->link
->ifname
: "*",
2123 af_to_name_short(t
->scope
->family
),
2124 yes_no(!FLAGS_SET(t
->query_flags
, SD_RESOLVED_NO_VALIDATE
)));
2126 if (!t
->initial_jitter_scheduled
&&
2127 IN_SET(t
->scope
->protocol
, DNS_PROTOCOL_LLMNR
, DNS_PROTOCOL_MDNS
)) {
2130 /* RFC 4795 Section 2.7 suggests all LLMNR queries should be delayed by a random time from 0 to
2132 * RFC 6762 Section 8.1 suggests initial probe queries should be delayed by a random time from
2135 t
->initial_jitter_scheduled
= true;
2138 switch (t
->scope
->protocol
) {
2140 case DNS_PROTOCOL_LLMNR
:
2141 jitter
= random_u64_range(LLMNR_JITTER_INTERVAL_USEC
);
2144 case DNS_PROTOCOL_MDNS
:
2146 jitter
= random_u64_range(MDNS_PROBING_INTERVAL_USEC
);
2151 assert_not_reached();
2154 r
= dns_transaction_setup_timeout(t
, jitter
, ts
);
2158 log_debug("Delaying %s transaction %" PRIu16
" for " USEC_FMT
"us.",
2159 dns_protocol_to_string(t
->scope
->protocol
),
2165 /* Otherwise, we need to ask the network */
2166 r
= dns_transaction_make_packet(t
);
2170 if (t
->scope
->protocol
== DNS_PROTOCOL_LLMNR
&&
2171 (dns_name_endswith(dns_resource_key_name(dns_transaction_key(t
)), "in-addr.arpa") > 0 ||
2172 dns_name_endswith(dns_resource_key_name(dns_transaction_key(t
)), "ip6.arpa") > 0)) {
2174 /* RFC 4795, Section 2.4. says reverse lookups shall
2175 * always be made via TCP on LLMNR */
2176 r
= dns_transaction_emit_tcp(t
);
2178 /* Try via UDP, and if that fails due to large size or lack of
2179 * support try via TCP */
2180 r
= dns_transaction_emit_udp(t
);
2182 log_debug("Sending query via TCP since it is too large.");
2183 else if (r
== -EAGAIN
)
2184 log_debug("Sending query via TCP since UDP isn't supported or DNS-over-TLS is selected.");
2185 else if (r
== -EPERM
)
2186 log_debug("Sending query via TCP since UDP is blocked.");
2187 if (IN_SET(r
, -EMSGSIZE
, -EAGAIN
, -EPERM
))
2188 r
= dns_transaction_emit_tcp(t
);
2191 if (t
->scope
->protocol
!= DNS_PROTOCOL_DNS
)
2194 /* One of our own stub listeners */
2195 log_debug_errno(r
, "Detected that specified DNS server is our own extra listener, switching DNS servers.");
2197 dns_scope_next_dns_server(t
->scope
, t
->server
);
2199 if (dns_scope_get_dns_server(t
->scope
) == t
->server
) {
2200 log_debug_errno(r
, "Still pointing to extra listener after switching DNS servers, refusing operation.");
2201 dns_transaction_complete(t
, DNS_TRANSACTION_STUB_LOOP
);
2205 return dns_transaction_go(t
);
2208 /* No servers to send this to? */
2209 dns_transaction_complete(t
, DNS_TRANSACTION_NO_SERVERS
);
2212 if (r
== -EOPNOTSUPP
) {
2213 /* Tried to ask for DNSSEC RRs, on a server that doesn't do DNSSEC */
2214 dns_transaction_complete(t
, DNS_TRANSACTION_RR_TYPE_UNSUPPORTED
);
2217 if (t
->scope
->protocol
== DNS_PROTOCOL_LLMNR
&& ERRNO_IS_NEG_DISCONNECT(r
)) {
2218 /* On LLMNR, if we cannot connect to a host via TCP when doing reverse lookups. This means we cannot
2219 * answer this request with this protocol. */
2220 dns_transaction_complete(t
, DNS_TRANSACTION_NOT_FOUND
);
2224 if (t
->scope
->protocol
!= DNS_PROTOCOL_DNS
)
2227 /* Couldn't send? Try immediately again, with a new server */
2228 dns_scope_next_dns_server(t
->scope
, t
->server
);
2230 return dns_transaction_go(t
);
2233 usec_t timeout
= transaction_get_resend_timeout(t
);
2234 r
= dns_transaction_setup_timeout(t
, timeout
, usec_add(ts
, timeout
));
2241 static int dns_transaction_find_cyclic(DnsTransaction
*t
, DnsTransaction
*aux
) {
2248 /* Try to find cyclic dependencies between transaction objects */
2253 SET_FOREACH(n
, aux
->dnssec_transactions
) {
2254 r
= dns_transaction_find_cyclic(t
, n
);
2262 static int dns_transaction_add_dnssec_transaction(DnsTransaction
*t
, DnsResourceKey
*key
, DnsTransaction
**ret
) {
2263 _cleanup_(dns_transaction_gcp
) DnsTransaction
*aux
= NULL
;
2270 aux
= dns_scope_find_transaction(t
->scope
, key
, t
->query_flags
);
2272 r
= dns_transaction_new(&aux
, t
->scope
, key
, NULL
, t
->query_flags
);
2276 if (set_contains(t
->dnssec_transactions
, aux
)) {
2281 r
= dns_transaction_find_cyclic(t
, aux
);
2285 char s
[DNS_RESOURCE_KEY_STRING_MAX
], saux
[DNS_RESOURCE_KEY_STRING_MAX
];
2287 return log_debug_errno(SYNTHETIC_ERRNO(ELOOP
),
2288 "Potential cyclic dependency, refusing to add transaction %" PRIu16
" (%s) as dependency for %" PRIu16
" (%s).",
2290 dns_resource_key_to_string(dns_transaction_key(t
), s
, sizeof s
),
2292 dns_resource_key_to_string(dns_transaction_key(aux
), saux
, sizeof saux
));
2296 r
= set_ensure_allocated(&aux
->notify_transactions_done
, NULL
);
2300 r
= set_ensure_put(&t
->dnssec_transactions
, NULL
, aux
);
2304 r
= set_ensure_put(&aux
->notify_transactions
, NULL
, t
);
2306 (void) set_remove(t
->dnssec_transactions
, aux
);
2310 *ret
= TAKE_PTR(aux
);
2314 static int dns_transaction_request_dnssec_rr_full(DnsTransaction
*t
, DnsResourceKey
*key
, DnsTransaction
**ret
) {
2315 _cleanup_(dns_answer_unrefp
) DnsAnswer
*a
= NULL
;
2316 DnsTransaction
*aux
;
2322 /* Try to get the data from the trust anchor */
2323 r
= dns_trust_anchor_lookup_positive(&t
->scope
->manager
->trust_anchor
, key
, &a
);
2327 r
= dns_answer_extend(&t
->validated_keys
, a
);
2336 /* This didn't work, ask for it via the network/cache then. */
2337 r
= dns_transaction_add_dnssec_transaction(t
, key
, &aux
);
2338 if (r
== -ELOOP
) { /* This would result in a cyclic dependency */
2346 if (aux
->state
== DNS_TRANSACTION_NULL
) {
2347 r
= dns_transaction_go(aux
);
2357 static int dns_transaction_request_dnssec_rr(DnsTransaction
*t
, DnsResourceKey
*key
) {
2360 return dns_transaction_request_dnssec_rr_full(t
, key
, NULL
);
2363 static int dns_transaction_negative_trust_anchor_lookup(DnsTransaction
*t
, const char *name
) {
2368 /* Check whether the specified name is in the NTA
2369 * database, either in the global one, or the link-local
2372 r
= dns_trust_anchor_lookup_negative(&t
->scope
->manager
->trust_anchor
, name
);
2376 if (!t
->scope
->link
)
2379 return link_negative_trust_anchor_lookup(t
->scope
->link
, name
);
2382 static int dns_transaction_has_negative_answer(DnsTransaction
*t
) {
2387 /* Checks whether the answer is negative, and lacks NSEC/NSEC3
2388 * RRs to prove it */
2390 r
= dns_transaction_has_positive_answer(t
, NULL
);
2396 /* Is this key explicitly listed as a negative trust anchor?
2397 * If so, it's nothing we need to care about */
2398 r
= dns_transaction_negative_trust_anchor_lookup(t
, dns_resource_key_name(dns_transaction_key(t
)));
2404 static int dns_transaction_is_primary_response(DnsTransaction
*t
, DnsResourceRecord
*rr
) {
2410 /* Check if the specified RR is the "primary" response,
2411 * i.e. either matches the question precisely or is a
2412 * CNAME/DNAME for it. */
2414 r
= dns_resource_key_match_rr(dns_transaction_key(t
), rr
, NULL
);
2418 return dns_resource_key_match_cname_or_dname(dns_transaction_key(t
), rr
->key
, NULL
);
2421 static bool dns_transaction_dnssec_supported(DnsTransaction
*t
) {
2424 /* Checks whether our transaction's DNS server is assumed to be compatible with DNSSEC. Returns false as soon
2425 * as we changed our mind about a server, and now believe it is incompatible with DNSSEC. */
2427 if (t
->scope
->protocol
!= DNS_PROTOCOL_DNS
)
2430 /* If we have picked no server, then we are working from the cache or some other source, and DNSSEC might well
2431 * be supported, hence return true. */
2435 /* Note that we do not check the feature level actually used for the transaction but instead the feature level
2436 * the server is known to support currently, as the transaction feature level might be lower than what the
2437 * server actually supports, since we might have downgraded this transaction's feature level because we got a
2438 * SERVFAIL earlier and wanted to check whether downgrading fixes it. */
2440 return dns_server_dnssec_supported(t
->server
);
2443 static bool dns_transaction_dnssec_supported_full(DnsTransaction
*t
) {
2448 /* Checks whether our transaction our any of the auxiliary transactions couldn't do DNSSEC. */
2450 if (!dns_transaction_dnssec_supported(t
))
2453 SET_FOREACH(dt
, t
->dnssec_transactions
)
2454 if (!dns_transaction_dnssec_supported(dt
))
2460 int dns_transaction_request_dnssec_keys(DnsTransaction
*t
) {
2461 DnsResourceRecord
*rr
;
2463 /* Have we already requested a record that would be sufficient to validate an insecure delegation? */
2464 bool chased_insecure
= false;
2470 * Retrieve all auxiliary RRs for the answer we got, so that
2471 * we can verify signatures or prove that RRs are rightfully
2472 * unsigned. Specifically:
2474 * - For RRSIG we get the matching DNSKEY
2475 * - For DNSKEY we get the matching DS
2476 * - For unsigned SOA/NS we get the matching DS
2477 * - For unsigned CNAME/DNAME/DS we get the parent DS RR
2478 * - For other unsigned RRs we get the matching DS RR
2479 * - For SOA/NS queries with no matching response RR, and no NSEC/NSEC3, the DS RR
2480 * - For DS queries with no matching response RRs, and no NSEC/NSEC3, the parent's DS RR
2481 * - For other queries with no matching response RRs, and no NSEC/NSEC3, the DS RR
2484 if (FLAGS_SET(t
->query_flags
, SD_RESOLVED_NO_VALIDATE
) || t
->scope
->dnssec_mode
== DNSSEC_NO
)
2486 if (t
->answer_source
!= DNS_TRANSACTION_NETWORK
)
2487 return 0; /* We only need to validate stuff from the network */
2488 if (!dns_transaction_dnssec_supported(t
))
2489 return 0; /* If we can't do DNSSEC anyway there's no point in getting the auxiliary RRs */
2491 DNS_ANSWER_FOREACH(rr
, t
->answer
) {
2493 if (dns_type_is_pseudo(rr
->key
->type
))
2496 /* If this RR is in the negative trust anchor, we don't need to validate it. */
2497 r
= dns_transaction_negative_trust_anchor_lookup(t
, dns_resource_key_name(rr
->key
));
2503 switch (rr
->key
->type
) {
2505 case DNS_TYPE_RRSIG
: {
2506 /* For each RRSIG we request the matching DNSKEY */
2507 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*dnskey
= NULL
;
2508 DnsTransaction
*aux
;
2510 /* If this RRSIG is about a DNSKEY RR and the
2511 * signer is the same as the owner, then we
2512 * already have the DNSKEY, and we don't have
2513 * to look for more. */
2514 if (rr
->rrsig
.type_covered
== DNS_TYPE_DNSKEY
) {
2515 r
= dns_name_equal(rr
->rrsig
.signer
, dns_resource_key_name(rr
->key
));
2522 /* If the signer is not a parent of our
2523 * original query, then this is about an
2524 * auxiliary RRset, but not anything we asked
2525 * for. In this case we aren't interested,
2526 * because we don't want to request additional
2527 * RRs for stuff we didn't really ask for, and
2528 * also to avoid request loops, where
2529 * additional RRs from one transaction result
2530 * in another transaction whose additional RRs
2531 * point back to the original transaction, and
2533 r
= dns_name_endswith(dns_resource_key_name(dns_transaction_key(t
)), rr
->rrsig
.signer
);
2539 dnskey
= dns_resource_key_new(rr
->key
->class, DNS_TYPE_DNSKEY
, rr
->rrsig
.signer
);
2543 log_debug("Requesting DNSKEY to validate transaction %" PRIu16
" (%s, RRSIG with key tag: %" PRIu16
").",
2544 t
->id
, dns_resource_key_name(rr
->key
), rr
->rrsig
.key_tag
);
2545 r
= dns_transaction_request_dnssec_rr_full(t
, dnskey
, &aux
);
2549 /* If we are requesting a DNSKEY, we can anticipate that we will want the matching DS
2550 * in the near future. Let's request it in advance so we don't have to wait in the
2553 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*ds
=
2554 dns_resource_key_new(rr
->key
->class, DNS_TYPE_DS
, dns_resource_key_name(dnskey
));
2557 r
= dns_transaction_request_dnssec_rr(t
, ds
);
2564 case DNS_TYPE_DNSKEY
: {
2565 /* For each DNSKEY we request the matching DS */
2566 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*ds
= NULL
;
2568 /* If the DNSKEY we are looking at is not for
2569 * zone we are interested in, nor any of its
2570 * parents, we aren't interested, and don't
2571 * request it. After all, we don't want to end
2572 * up in request loops, and want to keep
2573 * additional traffic down. */
2575 r
= dns_name_endswith(dns_resource_key_name(dns_transaction_key(t
)), dns_resource_key_name(rr
->key
));
2581 ds
= dns_resource_key_new(rr
->key
->class, DNS_TYPE_DS
, dns_resource_key_name(rr
->key
));
2585 log_debug("Requesting DS to validate transaction %" PRIu16
" (%s, DNSKEY with key tag: %" PRIu16
").",
2586 t
->id
, dns_resource_key_name(rr
->key
), dnssec_keytag(rr
, false));
2587 r
= dns_transaction_request_dnssec_rr(t
, ds
);
2596 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*ds
= NULL
;
2598 /* For an unsigned SOA or NS, try to acquire
2599 * the matching DS RR, as we are at a zone cut
2600 * then, and whether a DS exists tells us
2601 * whether the zone is signed. Do so only if
2602 * this RR matches our original question,
2605 r
= dns_resource_key_match_rr(dns_transaction_key(t
), rr
, NULL
);
2609 /* Hmm, so this SOA RR doesn't match our original question. In this case, maybe this is
2610 * a negative reply, and we need the SOA RR's TTL in order to cache a negative entry?
2611 * If so, we need to validate it, too. */
2613 r
= dns_answer_match_key(t
->answer
, dns_transaction_key(t
), NULL
);
2616 if (r
> 0) /* positive reply, we won't need the SOA and hence don't need to validate
2620 /* Only bother with this if the SOA/NS RR we are looking at is actually a parent of
2621 * what we are looking for, otherwise there's no value in it for us. */
2622 r
= dns_name_endswith(dns_resource_key_name(dns_transaction_key(t
)), dns_resource_key_name(rr
->key
));
2629 r
= dnssec_has_rrsig(t
->answer
, rr
->key
);
2635 chased_insecure
= true;
2636 ds
= dns_resource_key_new(rr
->key
->class, DNS_TYPE_DS
, dns_resource_key_name(rr
->key
));
2640 log_debug("Requesting DS to validate transaction %" PRIu16
" (%s, unsigned SOA/NS RRset).",
2641 t
->id
, dns_resource_key_name(rr
->key
));
2642 r
= dns_transaction_request_dnssec_rr(t
, ds
);
2650 case DNS_TYPE_CNAME
:
2651 case DNS_TYPE_DNAME
: {
2652 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*ds
= NULL
;
2655 /* CNAMEs and DNAMEs cannot be located at a
2656 * zone apex, hence ask for the parent DS for
2657 * unsigned CNAME/DNAME RRs, maybe that's the
2658 * apex. But do all that only if this is
2659 * actually a response to our original
2662 * Similar for DS RRs, which are signed when
2663 * the parent SOA is signed. */
2665 r
= dns_transaction_is_primary_response(t
, rr
);
2671 r
= dnssec_has_rrsig(t
->answer
, rr
->key
);
2677 r
= dns_answer_has_dname_for_cname(t
->answer
, rr
);
2683 name
= dns_resource_key_name(rr
->key
);
2684 r
= dns_name_parent(&name
);
2690 ds
= dns_resource_key_new(rr
->key
->class, DNS_TYPE_DS
, name
);
2694 log_debug("Requesting parent DS to validate transaction %" PRIu16
" (%s, unsigned CNAME/DNAME/DS RRset).",
2695 t
->id
, dns_resource_key_name(rr
->key
));
2696 r
= dns_transaction_request_dnssec_rr(t
, ds
);
2704 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*ds
= NULL
;
2706 /* For other unsigned RRsets (including
2707 * NSEC/NSEC3!), look for proof the zone is
2708 * unsigned, by requesting the DS RR of the
2709 * zone. However, do so only if they are
2710 * directly relevant to our original
2713 r
= dns_transaction_is_primary_response(t
, rr
);
2719 r
= dnssec_has_rrsig(t
->answer
, rr
->key
);
2725 ds
= dns_resource_key_new(rr
->key
->class, DNS_TYPE_DS
, dns_resource_key_name(rr
->key
));
2729 log_debug("Requesting DS to validate transaction %" PRIu16
" (%s, unsigned non-SOA/NS RRset <%s>).",
2730 t
->id
, dns_resource_key_name(rr
->key
), dns_resource_record_to_string(rr
));
2731 r
= dns_transaction_request_dnssec_rr(t
, ds
);
2738 /* Above, we requested everything necessary to validate what
2739 * we got. Now, let's request what we need to validate what we
2742 r
= dns_transaction_has_negative_answer(t
);
2746 const char *name
= dns_resource_key_name(dns_transaction_key(t
));
2747 bool was_signed
= dns_answer_contains_nsec_or_nsec3(t
->answer
);
2749 /* If the response is empty, seek the DS for this name, just in case we're at a zone cut
2750 * already, unless we just requested the DS, in which case we have to ask the parent to make
2753 * If this was an SOA or NS request, we could also skip to the parent, but in real world
2754 * setups there are too many broken DNS servers (Hello, incapdns.net!) where non-terminal
2755 * zones return NXDOMAIN even though they have further children. */
2757 if (chased_insecure
|| was_signed
)
2758 /* In this case we already requested what we need above. */
2760 else if (dns_transaction_key(t
)->type
== DNS_TYPE_DS
)
2761 /* If the DS response is empty, we'll walk up the dns labels requesting DS until we
2762 * find a referral to the SOA or hit it anyway and get a positive DS response. */
2763 if (dns_name_parent(&name
) <= 0)
2767 _cleanup_(dns_resource_key_unrefp
) DnsResourceKey
*ds
= NULL
;
2769 log_debug("Requesting DS (%s %s) to validate transaction %" PRIu16
" (%s empty response).",
2770 special_glyph(SPECIAL_GLYPH_ARROW_RIGHT
), name
, t
->id
,
2771 dns_resource_key_name(dns_transaction_key(t
)));
2773 ds
= dns_resource_key_new(dns_transaction_key(t
)->class, DNS_TYPE_DS
, name
);
2777 r
= dns_transaction_request_dnssec_rr(t
, ds
);
2783 return dns_transaction_dnssec_is_live(t
);
2786 void dns_transaction_notify(DnsTransaction
*t
, DnsTransaction
*source
) {
2790 /* Invoked whenever any of our auxiliary DNSSEC transactions completed its work. If the state is still PENDING,
2791 we are still in the loop that adds further DNSSEC transactions, hence don't check if we are ready yet. If
2792 the state is VALIDATING however, we should check if we are complete now. */
2794 if (t
->state
== DNS_TRANSACTION_VALIDATING
)
2795 dns_transaction_process_dnssec(t
);
2798 static int dns_transaction_validate_dnskey_by_ds(DnsTransaction
*t
) {
2799 DnsAnswerItem
*item
;
2804 /* Add all DNSKEY RRs from the answer that are validated by DS
2805 * RRs from the list of validated keys to the list of
2806 * validated keys. */
2808 DNS_ANSWER_FOREACH_ITEM(item
, t
->answer
) {
2810 r
= dnssec_verify_dnskey_by_ds_search(item
->rr
, t
->validated_keys
);
2816 /* If so, the DNSKEY is validated too. */
2817 r
= dns_answer_add_extend(&t
->validated_keys
, item
->rr
, item
->ifindex
, item
->flags
|DNS_ANSWER_AUTHENTICATED
, item
->rrsig
);
2825 static int dns_transaction_requires_rrsig(DnsTransaction
*t
, DnsResourceRecord
*rr
) {
2831 /* Checks if the RR we are looking for must be signed with an
2832 * RRSIG. This is used for positive responses. */
2834 if (t
->scope
->dnssec_mode
== DNSSEC_NO
)
2837 if (dns_type_is_pseudo(rr
->key
->type
))
2840 r
= dns_transaction_negative_trust_anchor_lookup(t
, dns_resource_key_name(rr
->key
));
2846 switch (rr
->key
->type
) {
2848 case DNS_TYPE_RRSIG
:
2849 /* RRSIGs are the signatures themselves, they need no signing. */
2856 /* For SOA or NS RRs we look for a matching DS transaction */
2857 SET_FOREACH(dt
, t
->dnssec_transactions
) {
2859 if (dns_transaction_key(dt
)->class != rr
->key
->class)
2861 if (dns_transaction_key(dt
)->type
!= DNS_TYPE_DS
)
2864 r
= dns_name_endswith(dns_resource_key_name(rr
->key
), dns_resource_key_name(dns_transaction_key(dt
)));
2870 /* We found a DS transactions for the SOA/NS
2871 * RRs we are looking at. If it discovered signed DS
2872 * RRs, then we need to be signed, too. */
2874 if (!FLAGS_SET(dt
->answer_query_flags
, SD_RESOLVED_AUTHENTICATED
))
2877 return dns_answer_match_key(dt
->answer
, dns_transaction_key(dt
), NULL
);
2880 /* We found nothing that proves this is safe to leave
2881 * this unauthenticated, hence ask inist on
2882 * authentication. */
2887 case DNS_TYPE_CNAME
:
2888 case DNS_TYPE_DNAME
: {
2889 const char *parent
= NULL
;
2893 * CNAME/DNAME RRs cannot be located at a zone apex, hence look directly for the parent DS.
2895 * DS RRs are signed if the parent is signed, hence also look at the parent DS
2898 SET_FOREACH(dt
, t
->dnssec_transactions
) {
2900 if (dns_transaction_key(dt
)->class != rr
->key
->class)
2902 if (dns_transaction_key(dt
)->type
!= DNS_TYPE_DS
)
2906 parent
= dns_resource_key_name(rr
->key
);
2907 r
= dns_name_parent(&parent
);
2911 if (rr
->key
->type
== DNS_TYPE_DS
)
2914 /* A CNAME/DNAME without a parent? That's sooo weird. */
2915 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG
),
2916 "Transaction %" PRIu16
" claims CNAME/DNAME at root. Refusing.", t
->id
);
2920 r
= dns_name_endswith(parent
, dns_resource_key_name(dns_transaction_key(dt
)));
2926 return FLAGS_SET(dt
->answer_query_flags
, SD_RESOLVED_AUTHENTICATED
);
2935 /* Any other kind of RR (including DNSKEY/NSEC/NSEC3). Let's see if our DS lookup was authenticated */
2937 SET_FOREACH(dt
, t
->dnssec_transactions
) {
2938 if (dns_transaction_key(dt
)->class != rr
->key
->class)
2940 if (dns_transaction_key(dt
)->type
!= DNS_TYPE_DS
)
2943 r
= dns_name_endswith(dns_resource_key_name(rr
->key
), dns_resource_key_name(dns_transaction_key(dt
)));
2949 if (!FLAGS_SET(dt
->answer_query_flags
, SD_RESOLVED_AUTHENTICATED
))
2952 /* We expect this to be signed when the DS record exists, and don't expect it to be
2953 * signed when the DS record is proven not to exist. */
2954 return dns_answer_match_key(dt
->answer
, dns_transaction_key(dt
), NULL
);
2961 static int dns_transaction_in_private_tld(DnsTransaction
*t
, const DnsResourceKey
*key
) {
2966 /* If DNSSEC downgrade mode is on, checks whether the
2967 * specified RR is one level below a TLD we have proven not to
2968 * exist. In such a case we assume that this is a private
2969 * domain, and permit it.
2971 * This detects cases like the Fritz!Box router networks. Each
2972 * Fritz!Box router serves a private "fritz.box" zone, in the
2973 * non-existing TLD "box". Requests for the "fritz.box" domain
2974 * are served by the router itself, while requests for the
2975 * "box" domain will result in NXDOMAIN.
2977 * Note that this logic is unable to detect cases where a
2978 * router serves a private DNS zone directly under
2979 * non-existing TLD. In such a case we cannot detect whether
2980 * the TLD is supposed to exist or not, as all requests we
2981 * make for it will be answered by the router's zone, and not
2982 * by the root zone. */
2986 if (t
->scope
->dnssec_mode
!= DNSSEC_ALLOW_DOWNGRADE
)
2987 return false; /* In strict DNSSEC mode what doesn't exist, doesn't exist */
2989 tld
= dns_resource_key_name(key
);
2990 r
= dns_name_parent(&tld
);
2994 return false; /* Already the root domain */
2996 if (!dns_name_is_single_label(tld
))
2999 SET_FOREACH(dt
, t
->dnssec_transactions
) {
3001 if (dns_transaction_key(dt
)->class != key
->class)
3004 r
= dns_name_equal(dns_resource_key_name(dns_transaction_key(dt
)), tld
);
3010 /* We found an auxiliary lookup we did for the TLD. If
3011 * that returned with NXDOMAIN, we know the TLD didn't
3012 * exist, and hence this might be a private zone. */
3014 return dt
->answer_rcode
== DNS_RCODE_NXDOMAIN
;
3020 static int dns_transaction_requires_nsec(DnsTransaction
*t
) {
3021 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
3028 /* Checks if we need to insist on NSEC/NSEC3 RRs for proving
3029 * this negative reply */
3031 if (t
->scope
->dnssec_mode
== DNSSEC_NO
)
3034 if (dns_type_is_pseudo(dns_transaction_key(t
)->type
))
3037 r
= dns_transaction_negative_trust_anchor_lookup(t
, dns_resource_key_name(dns_transaction_key(t
)));
3043 r
= dns_transaction_in_private_tld(t
, dns_transaction_key(t
));
3047 /* The lookup is from a TLD that is proven not to
3048 * exist, and we are in downgrade mode, hence ignore
3049 * that fact that we didn't get any NSEC RRs. */
3051 log_info("Detected a negative query %s in a private DNS zone, permitting unsigned response.",
3052 dns_resource_key_to_string(dns_transaction_key(t
), key_str
, sizeof key_str
));
3056 name
= dns_resource_key_name(dns_transaction_key(t
));
3058 if (IN_SET(dns_transaction_key(t
)->type
, DNS_TYPE_DS
, DNS_TYPE_CNAME
, DNS_TYPE_DNAME
)) {
3059 /* We got a negative reply for this DS/CNAME/DNAME lookup? Check the parent in this case to
3060 * see if this answer should have been signed. */
3061 r
= dns_name_parent(&name
);
3068 /* For all other RRs we check the DS on the same level to see
3069 * if it's signed. */
3071 SET_FOREACH(dt
, t
->dnssec_transactions
) {
3072 if (dns_transaction_key(dt
)->class != dns_transaction_key(t
)->class)
3074 if (dns_transaction_key(dt
)->type
!= DNS_TYPE_DS
)
3077 r
= dns_name_endswith(name
, dns_resource_key_name(dns_transaction_key(dt
)));
3083 if (!FLAGS_SET(dt
->answer_query_flags
, SD_RESOLVED_AUTHENTICATED
))
3086 /* We expect this to be signed when the DS record exists, and don't expect it to be signed
3087 * when the DS record is proven not to exist. */
3088 return dns_answer_match_key(dt
->answer
, dns_transaction_key(dt
), NULL
);
3091 /* If in doubt, require NSEC/NSEC3 */
3095 static int dns_transaction_dnskey_authenticated(DnsTransaction
*t
, DnsResourceRecord
*rr
) {
3096 DnsResourceRecord
*rrsig
;
3100 /* Checks whether any of the DNSKEYs used for the RRSIGs for
3101 * the specified RRset is authenticated (i.e. has a matching
3104 r
= dns_transaction_negative_trust_anchor_lookup(t
, dns_resource_key_name(rr
->key
));
3110 DNS_ANSWER_FOREACH(rrsig
, t
->answer
) {
3113 r
= dnssec_key_match_rrsig(rr
->key
, rrsig
);
3119 SET_FOREACH(dt
, t
->dnssec_transactions
) {
3121 if (dns_transaction_key(dt
)->class != rr
->key
->class)
3124 if (dns_transaction_key(dt
)->type
== DNS_TYPE_DNSKEY
) {
3126 r
= dns_name_equal(dns_resource_key_name(dns_transaction_key(dt
)), rrsig
->rrsig
.signer
);
3132 /* OK, we found an auxiliary DNSKEY lookup. If that lookup is authenticated,
3135 if (FLAGS_SET(dt
->answer_query_flags
, SD_RESOLVED_AUTHENTICATED
))
3140 } else if (dns_transaction_key(dt
)->type
== DNS_TYPE_DS
) {
3142 r
= dns_name_equal(dns_resource_key_name(dns_transaction_key(dt
)), rrsig
->rrsig
.signer
);
3148 /* OK, we found an auxiliary DS lookup. If that lookup is authenticated and
3149 * non-zero, we won! */
3151 if (!FLAGS_SET(dt
->answer_query_flags
, SD_RESOLVED_AUTHENTICATED
))
3154 return dns_answer_match_key(dt
->answer
, dns_transaction_key(dt
), NULL
);
3159 return found
? false : -ENXIO
;
3162 static int dns_transaction_known_signed(DnsTransaction
*t
, DnsResourceRecord
*rr
) {
3166 /* We know that the root domain is signed, hence if it appears
3167 * not to be signed, there's a problem with the DNS server */
3169 return rr
->key
->class == DNS_CLASS_IN
&&
3170 dns_name_is_root(dns_resource_key_name(rr
->key
));
3173 static int dns_transaction_check_revoked_trust_anchors(DnsTransaction
*t
) {
3174 DnsResourceRecord
*rr
;
3179 /* Maybe warn the user that we encountered a revoked DNSKEY
3180 * for a key from our trust anchor. Note that we don't care
3181 * whether the DNSKEY can be authenticated or not. It's
3182 * sufficient if it is self-signed. */
3184 DNS_ANSWER_FOREACH(rr
, t
->answer
) {
3185 r
= dns_trust_anchor_check_revoked(&t
->scope
->manager
->trust_anchor
, rr
, t
->answer
);
3193 static int dns_transaction_invalidate_revoked_keys(DnsTransaction
*t
) {
3199 /* Removes all DNSKEY/DS objects from t->validated_keys that
3200 * our trust anchors database considers revoked. */
3203 DnsResourceRecord
*rr
;
3207 DNS_ANSWER_FOREACH(rr
, t
->validated_keys
) {
3208 r
= dns_trust_anchor_is_revoked(&t
->scope
->manager
->trust_anchor
, rr
);
3212 r
= dns_answer_remove_by_rr(&t
->validated_keys
, rr
);
3226 static int dns_transaction_copy_validated(DnsTransaction
*t
) {
3232 /* Copy all validated RRs from the auxiliary DNSSEC transactions into our set of validated RRs */
3234 SET_FOREACH(dt
, t
->dnssec_transactions
) {
3236 if (DNS_TRANSACTION_IS_LIVE(dt
->state
))
3239 if (!FLAGS_SET(dt
->answer_query_flags
, SD_RESOLVED_AUTHENTICATED
))
3242 r
= dns_answer_extend(&t
->validated_keys
, dt
->answer
);
3251 DNSSEC_PHASE_DNSKEY
, /* Phase #1, only validate DNSKEYs */
3252 DNSSEC_PHASE_NSEC
, /* Phase #2, only validate NSEC+NSEC3 */
3253 DNSSEC_PHASE_ALL
, /* Phase #3, validate everything else */
3256 static int dnssec_validate_records(
3260 unsigned *nvalidations
,
3261 DnsAnswer
**validated
) {
3263 DnsResourceRecord
*rr
;
3266 assert(nvalidations
);
3268 /* Returns negative on error, 0 if validation failed, 1 to restart validation, 2 when finished. */
3270 DNS_ANSWER_FOREACH(rr
, t
->answer
) {
3271 _unused_
_cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr_ref
= dns_resource_record_ref(rr
);
3272 DnsResourceRecord
*rrsig
= NULL
;
3273 DnssecResult result
;
3275 switch (rr
->key
->type
) {
3276 case DNS_TYPE_RRSIG
:
3279 case DNS_TYPE_DNSKEY
:
3280 /* We validate DNSKEYs only in the DNSKEY and ALL phases */
3281 if (phase
== DNSSEC_PHASE_NSEC
)
3286 case DNS_TYPE_NSEC3
:
3289 /* We validate NSEC/NSEC3 only in the NSEC and ALL phases */
3290 if (phase
== DNSSEC_PHASE_DNSKEY
)
3295 /* We validate all other RRs only in the ALL phases */
3296 if (phase
!= DNSSEC_PHASE_ALL
)
3300 r
= dnssec_verify_rrset_search(
3311 log_debug("Looking at %s: %s", strna(dns_resource_record_to_string(rr
)), dnssec_result_to_string(result
));
3313 if (result
== DNSSEC_VALIDATED
) {
3316 if (rr
->key
->type
== DNS_TYPE_DNSKEY
) {
3317 /* If we just validated a DNSKEY RRset, then let's add these keys to
3318 * the set of validated keys for this transaction. */
3320 r
= dns_answer_copy_by_key(&t
->validated_keys
, t
->answer
, rr
->key
, DNS_ANSWER_AUTHENTICATED
, rrsig
);
3324 /* Some of the DNSKEYs we just added might already have been revoked,
3325 * remove them again in that case. */
3326 r
= dns_transaction_invalidate_revoked_keys(t
);
3331 /* Add the validated RRset to the new list of validated RRsets, and remove it from
3332 * the unvalidated RRsets. We mark the RRset as authenticated and cacheable. */
3333 r
= dns_answer_move_by_key(validated
, &t
->answer
, rr
->key
, DNS_ANSWER_AUTHENTICATED
|DNS_ANSWER_CACHEABLE
, rrsig
);
3337 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_SECURE
, rr
->key
);
3339 /* Exit the loop, we dropped something from the answer, start from the beginning */
3343 /* If we haven't read all DNSKEYs yet a negative result of the validation is irrelevant, as
3344 * there might be more DNSKEYs coming. Similar, if we haven't read all NSEC/NSEC3 RRs yet,
3345 * we cannot do positive wildcard proofs yet, as those require the NSEC/NSEC3 RRs. */
3346 if (phase
!= DNSSEC_PHASE_ALL
)
3349 if (result
== DNSSEC_VALIDATED_WILDCARD
) {
3350 bool authenticated
= false;
3355 /* This RRset validated, but as a wildcard. This means we need
3356 * to prove via NSEC/NSEC3 that no matching non-wildcard RR exists. */
3358 /* First step, determine the source of synthesis */
3359 r
= dns_resource_record_source(rrsig
, &source
);
3363 r
= dnssec_test_positive_wildcard(*validated
,
3364 dns_resource_key_name(rr
->key
),
3366 rrsig
->rrsig
.signer
,
3369 /* Unless the NSEC proof showed that the key really doesn't exist something is off. */
3371 result
= DNSSEC_INVALID
;
3373 r
= dns_answer_move_by_key(
3377 authenticated
? (DNS_ANSWER_AUTHENTICATED
|DNS_ANSWER_CACHEABLE
) : 0,
3382 manager_dnssec_verdict(t
->scope
->manager
, authenticated
? DNSSEC_SECURE
: DNSSEC_INSECURE
, rr
->key
);
3384 /* Exit the loop, we dropped something from the answer, start from the beginning */
3389 if (result
== DNSSEC_NO_SIGNATURE
) {
3390 r
= dns_transaction_requires_rrsig(t
, rr
);
3394 /* Data does not require signing. In that case, just copy it over,
3395 * but remember that this is by no means authenticated. */
3396 r
= dns_answer_move_by_key(
3405 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INSECURE
, rr
->key
);
3409 r
= dns_transaction_known_signed(t
, rr
);
3413 /* This is an RR we know has to be signed. If it isn't this means
3414 * the server is not attaching RRSIGs, hence complain. */
3416 dns_server_packet_rrsig_missing(t
->server
, t
->current_feature_level
);
3418 if (t
->scope
->dnssec_mode
== DNSSEC_ALLOW_DOWNGRADE
) {
3420 /* Downgrading is OK? If so, just consider the information unsigned */
3422 r
= dns_answer_move_by_key(validated
, &t
->answer
, rr
->key
, 0, NULL
);
3426 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INSECURE
, rr
->key
);
3430 /* Otherwise, fail */
3431 t
->answer_dnssec_result
= DNSSEC_INCOMPATIBLE_SERVER
;
3435 r
= dns_transaction_in_private_tld(t
, rr
->key
);
3439 char s
[DNS_RESOURCE_KEY_STRING_MAX
];
3441 /* The data is from a TLD that is proven not to exist, and we are in downgrade
3442 * mode, hence ignore the fact that this was not signed. */
3444 log_info("Detected RRset %s is in a private DNS zone, permitting unsigned RRs.",
3445 dns_resource_key_to_string(rr
->key
, s
, sizeof s
));
3447 r
= dns_answer_move_by_key(validated
, &t
->answer
, rr
->key
, 0, NULL
);
3451 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INSECURE
, rr
->key
);
3456 /* https://datatracker.ietf.org/doc/html/rfc6840#section-5.2 */
3457 if (result
== DNSSEC_UNSUPPORTED_ALGORITHM
) {
3458 r
= dns_answer_move_by_key(validated
, &t
->answer
, rr
->key
, 0, NULL
);
3462 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INSECURE
, rr
->key
);
3468 DNSSEC_SIGNATURE_EXPIRED
)) {
3470 r
= dns_transaction_dnskey_authenticated(t
, rr
);
3471 if (r
< 0 && r
!= -ENXIO
)
3474 /* The DNSKEY transaction was not authenticated, this means there's
3475 * no DS for this, which means it's OK if no keys are found for this signature. */
3477 r
= dns_answer_move_by_key(validated
, &t
->answer
, rr
->key
, 0, NULL
);
3481 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INSECURE
, rr
->key
);
3486 r
= dns_transaction_is_primary_response(t
, rr
);
3490 /* Look for a matching DNAME for this CNAME */
3491 r
= dns_answer_has_dname_for_cname(t
->answer
, rr
);
3495 /* Also look among the stuff we already validated */
3496 r
= dns_answer_has_dname_for_cname(*validated
, rr
);
3504 DNSSEC_SIGNATURE_EXPIRED
,
3505 DNSSEC_NO_SIGNATURE
))
3506 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_BOGUS
, rr
->key
);
3507 else /* DNSSEC_MISSING_KEY, DNSSEC_UNSUPPORTED_ALGORITHM,
3508 or DNSSEC_TOO_MANY_VALIDATIONS */
3509 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INDETERMINATE
, rr
->key
);
3511 /* This is a primary response to our question, and it failed validation.
3513 t
->answer_dnssec_result
= result
;
3517 /* This is a primary response, but we do have a DNAME RR
3518 * in the RR that can replay this CNAME, hence rely on
3519 * that, and we can remove the CNAME in favour of it. */
3522 /* This is just some auxiliary data. Just remove the RRset and continue. */
3523 r
= dns_answer_remove_by_key(&t
->answer
, rr
->key
);
3527 /* We dropped something from the answer, start from the beginning. */
3531 return 2; /* Finito. */
3534 int dns_transaction_validate_dnssec(DnsTransaction
*t
) {
3535 _cleanup_(dns_answer_unrefp
) DnsAnswer
*validated
= NULL
;
3537 DnsAnswerFlags flags
;
3539 char key_str
[DNS_RESOURCE_KEY_STRING_MAX
];
3543 /* We have now collected all DS and DNSKEY RRs in t->validated_keys, let's see which RRs we can now
3544 * authenticate with that. */
3546 if (FLAGS_SET(t
->query_flags
, SD_RESOLVED_NO_VALIDATE
) || t
->scope
->dnssec_mode
== DNSSEC_NO
)
3549 /* Already validated */
3550 if (t
->answer_dnssec_result
!= _DNSSEC_RESULT_INVALID
)
3553 /* Our own stuff needs no validation */
3554 if (IN_SET(t
->answer_source
, DNS_TRANSACTION_ZONE
, DNS_TRANSACTION_TRUST_ANCHOR
)) {
3555 t
->answer_dnssec_result
= DNSSEC_VALIDATED
;
3556 SET_FLAG(t
->answer_query_flags
, SD_RESOLVED_AUTHENTICATED
, true);
3560 /* Cached stuff is not affected by validation. */
3561 if (t
->answer_source
!= DNS_TRANSACTION_NETWORK
)
3564 if (!dns_transaction_dnssec_supported_full(t
)) {
3565 /* The server does not support DNSSEC, or doesn't augment responses with RRSIGs. */
3566 t
->answer_dnssec_result
= DNSSEC_INCOMPATIBLE_SERVER
;
3567 log_debug("Not validating response for %" PRIu16
", used server feature level does not support DNSSEC.", t
->id
);
3571 log_debug("Validating response from transaction %" PRIu16
" (%s).",
3573 dns_resource_key_to_string(dns_transaction_key(t
), key_str
, sizeof key_str
));
3575 /* First, see if this response contains any revoked trust
3576 * anchors we care about */
3577 r
= dns_transaction_check_revoked_trust_anchors(t
);
3581 /* Third, copy all RRs we acquired successfully from auxiliary RRs over. */
3582 r
= dns_transaction_copy_validated(t
);
3586 /* Second, see if there are DNSKEYs we already know a
3587 * validated DS for. */
3588 r
= dns_transaction_validate_dnskey_by_ds(t
);
3592 /* Fourth, remove all DNSKEY and DS RRs again that our trust
3593 * anchor says are revoked. After all we might have marked
3594 * some keys revoked above, but they might still be lingering
3595 * in our validated_keys list. */
3596 r
= dns_transaction_invalidate_revoked_keys(t
);
3600 phase
= DNSSEC_PHASE_DNSKEY
;
3601 for (unsigned nvalidations
= 0;;) {
3602 bool have_nsec
= false;
3604 r
= dnssec_validate_records(t
, phase
, &have_nsec
, &nvalidations
, &validated
);
3606 DNS_ANSWER_REPLACE(t
->answer
, TAKE_PTR(validated
));
3610 if (nvalidations
> DNSSEC_VALIDATION_MAX
) {
3611 /* This reply requires an onerous number of signature validations to verify. Let's
3612 * not waste our time trying, as this shouldn't happen for well-behaved domains
3614 t
->answer_dnssec_result
= DNSSEC_TOO_MANY_VALIDATIONS
;
3615 DNS_ANSWER_REPLACE(t
->answer
, TAKE_PTR(validated
));
3619 /* Try again as long as we managed to achieve something */
3623 if (phase
== DNSSEC_PHASE_DNSKEY
&& have_nsec
) {
3624 /* OK, we processed all DNSKEYs, and there are NSEC/NSEC3 RRs, look at those now. */
3625 phase
= DNSSEC_PHASE_NSEC
;
3629 if (phase
!= DNSSEC_PHASE_ALL
) {
3630 /* OK, we processed all DNSKEYs and NSEC/NSEC3 RRs, look at all the rest now.
3631 * Note that in this third phase we start to remove RRs we couldn't validate. */
3632 phase
= DNSSEC_PHASE_ALL
;
3640 DNS_ANSWER_REPLACE(t
->answer
, TAKE_PTR(validated
));
3642 /* At this point the answer only contains validated
3643 * RRsets. Now, let's see if it actually answers the question
3644 * we asked. If so, great! If it doesn't, then see if
3645 * NSEC/NSEC3 can prove this. */
3646 r
= dns_transaction_has_positive_answer(t
, &flags
);
3648 /* Yes, it answers the question! */
3650 if (flags
& DNS_ANSWER_AUTHENTICATED
) {
3651 /* The answer is fully authenticated, yay. */
3652 t
->answer_dnssec_result
= DNSSEC_VALIDATED
;
3653 t
->answer_rcode
= DNS_RCODE_SUCCESS
;
3654 SET_FLAG(t
->answer_query_flags
, SD_RESOLVED_AUTHENTICATED
, true);
3656 /* The answer is not fully authenticated. */
3657 t
->answer_dnssec_result
= DNSSEC_UNSIGNED
;
3658 SET_FLAG(t
->answer_query_flags
, SD_RESOLVED_AUTHENTICATED
, false);
3661 } else if (r
== 0) {
3662 DnssecNsecResult nr
;
3663 bool authenticated
= false;
3665 /* Bummer! Let's check NSEC/NSEC3 */
3666 r
= dnssec_nsec_test(t
->answer
, dns_transaction_key(t
), &nr
, &authenticated
, &t
->answer_nsec_ttl
);
3672 case DNSSEC_NSEC_NXDOMAIN
:
3673 /* NSEC proves the domain doesn't exist. Very good. */
3674 log_debug("Proved NXDOMAIN via NSEC/NSEC3 for transaction %u (%s)", t
->id
, key_str
);
3675 t
->answer_dnssec_result
= DNSSEC_VALIDATED
;
3676 t
->answer_rcode
= DNS_RCODE_NXDOMAIN
;
3677 SET_FLAG(t
->answer_query_flags
, SD_RESOLVED_AUTHENTICATED
, authenticated
);
3679 manager_dnssec_verdict(t
->scope
->manager
, authenticated
? DNSSEC_SECURE
: DNSSEC_INSECURE
, dns_transaction_key(t
));
3682 case DNSSEC_NSEC_NODATA
:
3683 /* NSEC proves that there's no data here, very good. */
3684 log_debug("Proved NODATA via NSEC/NSEC3 for transaction %u (%s)", t
->id
, key_str
);
3685 t
->answer_dnssec_result
= DNSSEC_VALIDATED
;
3686 t
->answer_rcode
= DNS_RCODE_SUCCESS
;
3687 SET_FLAG(t
->answer_query_flags
, SD_RESOLVED_AUTHENTICATED
, authenticated
);
3689 manager_dnssec_verdict(t
->scope
->manager
, authenticated
? DNSSEC_SECURE
: DNSSEC_INSECURE
, dns_transaction_key(t
));
3692 case DNSSEC_NSEC_OPTOUT
:
3693 /* NSEC3 says the data might not be signed */
3694 log_debug("Data is NSEC3 opt-out via NSEC/NSEC3 for transaction %u (%s)", t
->id
, key_str
);
3695 t
->answer_dnssec_result
= DNSSEC_UNSIGNED
;
3696 SET_FLAG(t
->answer_query_flags
, SD_RESOLVED_AUTHENTICATED
, false);
3698 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INSECURE
, dns_transaction_key(t
));
3701 case DNSSEC_NSEC_NO_RR
:
3702 /* No NSEC data? Bummer! */
3704 r
= dns_transaction_requires_nsec(t
);
3708 t
->answer_dnssec_result
= DNSSEC_NO_SIGNATURE
;
3709 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_BOGUS
, dns_transaction_key(t
));
3711 t
->answer_dnssec_result
= DNSSEC_UNSIGNED
;
3712 SET_FLAG(t
->answer_query_flags
, SD_RESOLVED_AUTHENTICATED
, false);
3713 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INSECURE
, dns_transaction_key(t
));
3718 case DNSSEC_NSEC_UNSUPPORTED_ALGORITHM
:
3719 /* We don't know the NSEC3 algorithm used? */
3720 t
->answer_dnssec_result
= DNSSEC_UNSUPPORTED_ALGORITHM
;
3721 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_INDETERMINATE
, dns_transaction_key(t
));
3724 case DNSSEC_NSEC_FOUND
:
3725 case DNSSEC_NSEC_CNAME
:
3726 /* NSEC says it needs to be there, but we couldn't find it? Bummer! */
3727 t
->answer_dnssec_result
= DNSSEC_NSEC_MISMATCH
;
3728 manager_dnssec_verdict(t
->scope
->manager
, DNSSEC_BOGUS
, dns_transaction_key(t
));
3732 assert_not_reached();
3739 static const char* const dns_transaction_state_table
[_DNS_TRANSACTION_STATE_MAX
] = {
3740 [DNS_TRANSACTION_NULL
] = "null",
3741 [DNS_TRANSACTION_PENDING
] = "pending",
3742 [DNS_TRANSACTION_VALIDATING
] = "validating",
3743 [DNS_TRANSACTION_RCODE_FAILURE
] = "rcode-failure",
3744 [DNS_TRANSACTION_SUCCESS
] = "success",
3745 [DNS_TRANSACTION_NO_SERVERS
] = "no-servers",
3746 [DNS_TRANSACTION_TIMEOUT
] = "timeout",
3747 [DNS_TRANSACTION_ATTEMPTS_MAX_REACHED
] = "attempts-max-reached",
3748 [DNS_TRANSACTION_INVALID_REPLY
] = "invalid-reply",
3749 [DNS_TRANSACTION_ERRNO
] = "errno",
3750 [DNS_TRANSACTION_ABORTED
] = "aborted",
3751 [DNS_TRANSACTION_DNSSEC_FAILED
] = "dnssec-failed",
3752 [DNS_TRANSACTION_NO_TRUST_ANCHOR
] = "no-trust-anchor",
3753 [DNS_TRANSACTION_RR_TYPE_UNSUPPORTED
] = "rr-type-unsupported",
3754 [DNS_TRANSACTION_NETWORK_DOWN
] = "network-down",
3755 [DNS_TRANSACTION_NOT_FOUND
] = "not-found",
3756 [DNS_TRANSACTION_NO_SOURCE
] = "no-source",
3757 [DNS_TRANSACTION_STUB_LOOP
] = "stub-loop",
3759 DEFINE_STRING_TABLE_LOOKUP(dns_transaction_state
, DnsTransactionState
);
3761 static const char* const dns_transaction_source_table
[_DNS_TRANSACTION_SOURCE_MAX
] = {
3762 [DNS_TRANSACTION_NETWORK
] = "network",
3763 [DNS_TRANSACTION_CACHE
] = "cache",
3764 [DNS_TRANSACTION_ZONE
] = "zone",
3765 [DNS_TRANSACTION_TRUST_ANCHOR
] = "trust-anchor",
3767 DEFINE_STRING_TABLE_LOOKUP(dns_transaction_source
, DnsTransactionSource
);