]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dns-transaction.c
resolve: really always initialize aux
[thirdparty/systemd.git] / src / resolve / resolved-dns-transaction.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-messages.h"
4
5 #include "af-list.h"
6 #include "alloc-util.h"
7 #include "dns-domain.h"
8 #include "errno-list.h"
9 #include "errno-util.h"
10 #include "fd-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"
18
19 #define TRANSACTIONS_MAX 4096
20 #define TRANSACTION_TCP_TIMEOUT_USEC (10U*USEC_PER_SEC)
21
22 /* After how much time to repeat classic DNS requests */
23 #define DNS_TIMEOUT_USEC (SD_RESOLVED_QUERY_TIMEOUT_USEC / DNS_TRANSACTION_ATTEMPTS_MAX)
24
25 static void dns_transaction_reset_answer(DnsTransaction *t) {
26 assert(t);
27
28 t->received = dns_packet_unref(t->received);
29 t->answer = dns_answer_unref(t->answer);
30 t->answer_rcode = 0;
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;
37 t->answer_errno = 0;
38 }
39
40 static void dns_transaction_flush_dnssec_transactions(DnsTransaction *t) {
41 DnsTransaction *z;
42
43 assert(t);
44
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);
49 }
50 }
51
52 static void dns_transaction_close_connection(
53 DnsTransaction *t,
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,
57 * hence don't. */
58 int r;
59
60 assert(t);
61
62 if (t->stream) {
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);
65
66 /* Remove packet in case it's still in the queue */
67 dns_packet_unref(ordered_set_remove(t->stream->write_queue, t->sent));
68
69 t->stream = dns_stream_unref(t->stream);
70 }
71
72 t->dns_udp_event_source = sd_event_source_disable_unref(t->dns_udp_event_source);
73
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. */
78
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. */
81 if (!t->scope ||
82 !t->scope->manager ||
83 !t->scope->manager->event ||
84 sd_event_get_state(t->scope->manager->event) == SD_EVENT_FINISHED)
85 use_graveyard = false;
86
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);
89 if (r < 0)
90 log_debug_errno(r, "Failed to add UDP socket to graveyard, closing immediately: %m");
91 else
92 TAKE_FD(t->dns_udp_fd);
93 }
94
95 t->dns_udp_fd = safe_close(t->dns_udp_fd);
96 }
97
98 static void dns_transaction_stop_timeout(DnsTransaction *t) {
99 assert(t);
100
101 t->timeout_event_source = sd_event_source_disable_unref(t->timeout_event_source);
102 }
103
104 DnsTransaction* dns_transaction_free(DnsTransaction *t) {
105 DnsQueryCandidate *c;
106 DnsZoneItem *i;
107 DnsTransaction *z;
108
109 if (!t)
110 return NULL;
111
112 log_debug("Freeing transaction %" PRIu16 ".", t->id);
113
114 dns_transaction_close_connection(t, true);
115 dns_transaction_stop_timeout(t);
116
117 dns_packet_unref(t->sent);
118 dns_transaction_reset_answer(t);
119
120 dns_server_unref(t->server);
121
122 if (t->scope) {
123 if (t->key) {
124 DnsTransaction *first;
125
126 first = hashmap_get(t->scope->transactions_by_key, t->key);
127 LIST_REMOVE(transactions_by_key, first, t);
128 if (first)
129 hashmap_replace(t->scope->transactions_by_key, first->key, first);
130 else
131 hashmap_remove(t->scope->transactions_by_key, t->key);
132 }
133
134 LIST_REMOVE(transactions_by_scope, t->scope->transactions, t);
135
136 if (t->id != 0)
137 hashmap_remove(t->scope->manager->dns_transactions, UINT_TO_PTR(t->id));
138 }
139
140 while ((c = set_steal_first(t->notify_query_candidates)))
141 set_remove(c->transactions, t);
142 set_free(t->notify_query_candidates);
143
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);
147
148 while ((i = set_steal_first(t->notify_zone_items)))
149 i->probe_transaction = NULL;
150 set_free(t->notify_zone_items);
151
152 while ((i = set_steal_first(t->notify_zone_items_done)))
153 i->probe_transaction = NULL;
154 set_free(t->notify_zone_items_done);
155
156 while ((z = set_steal_first(t->notify_transactions)))
157 set_remove(z->dnssec_transactions, t);
158 set_free(t->notify_transactions);
159
160 while ((z = set_steal_first(t->notify_transactions_done)))
161 set_remove(z->dnssec_transactions, t);
162 set_free(t->notify_transactions_done);
163
164 dns_transaction_flush_dnssec_transactions(t);
165 set_free(t->dnssec_transactions);
166
167 dns_answer_unref(t->validated_keys);
168 dns_resource_key_unref(t->key);
169 dns_packet_unref(t->bypass);
170
171 return mfree(t);
172 }
173
174 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsTransaction*, dns_transaction_free);
175
176 DnsTransaction* dns_transaction_gc(DnsTransaction *t) {
177 assert(t);
178
179 /* Returns !NULL if we can't gc yet. */
180
181 if (t->block_gc > 0)
182 return t;
183
184 if (t->wait_for_answer && IN_SET(t->state, DNS_TRANSACTION_PENDING, DNS_TRANSACTION_VALIDATING))
185 return t;
186
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);
194
195 return t;
196 }
197
198 static uint16_t pick_new_id(Manager *m) {
199 uint16_t new_id;
200
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. */
203
204 assert_cc(TRANSACTIONS_MAX < 0xFFFF);
205
206 do
207 random_bytes(&new_id, sizeof(new_id));
208 while (new_id == 0 ||
209 hashmap_get(m->dns_transactions, UINT_TO_PTR(new_id)));
210
211 return new_id;
212 }
213
214 static int key_ok(
215 DnsScope *scope,
216 DnsResourceKey *key) {
217
218 /* Don't allow looking up invalid or pseudo RRs */
219 if (!dns_type_is_valid_query(key->type))
220 return -EINVAL;
221 if (dns_type_is_obsolete(key->type))
222 return -EOPNOTSUPP;
223
224 /* We only support the IN class */
225 if (!IN_SET(key->class, DNS_CLASS_IN, DNS_CLASS_ANY))
226 return -EOPNOTSUPP;
227
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))
232 return -EOPNOTSUPP;
233
234 return 0;
235 }
236
237 int dns_transaction_new(
238 DnsTransaction **ret,
239 DnsScope *s,
240 DnsResourceKey *key,
241 DnsPacket *bypass,
242 uint64_t query_flags) {
243
244 _cleanup_(dns_transaction_freep) DnsTransaction *t = NULL;
245 int r;
246
247 assert(ret);
248 assert(s);
249
250 if (key) {
251 assert(!bypass);
252
253 r = key_ok(s, key);
254 if (r < 0)
255 return r;
256 } else {
257 DnsResourceKey *qk;
258 assert(bypass);
259
260 r = dns_packet_validate_query(bypass);
261 if (r < 0)
262 return r;
263
264 DNS_QUESTION_FOREACH(qk, bypass->question) {
265 r = key_ok(s, qk);
266 if (r < 0)
267 return r;
268 }
269 }
270
271 if (hashmap_size(s->manager->dns_transactions) >= TRANSACTIONS_MAX)
272 return -EBUSY;
273
274 r = hashmap_ensure_allocated(&s->manager->dns_transactions, NULL);
275 if (r < 0)
276 return r;
277
278 if (key) {
279 r = hashmap_ensure_allocated(&s->transactions_by_key, &dns_resource_key_hash_ops);
280 if (r < 0)
281 return r;
282 }
283
284 t = new(DnsTransaction, 1);
285 if (!t)
286 return -ENOMEM;
287
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),
300 };
301
302 r = hashmap_put(s->manager->dns_transactions, UINT_TO_PTR(t->id), t);
303 if (r < 0) {
304 t->id = 0;
305 return r;
306 }
307
308 if (t->key) {
309 DnsTransaction *first;
310
311 first = hashmap_get(s->transactions_by_key, t->key);
312 LIST_PREPEND(transactions_by_key, first, t);
313
314 r = hashmap_replace(s->transactions_by_key, first->key, first);
315 if (r < 0) {
316 LIST_REMOVE(transactions_by_key, first, t);
317 return r;
318 }
319 }
320
321 LIST_PREPEND(transactions_by_scope, s->transactions, t);
322 t->scope = s;
323
324 s->manager->n_transactions_total++;
325
326 if (ret)
327 *ret = t;
328
329 TAKE_PTR(t);
330 return 0;
331 }
332
333 static void dns_transaction_shuffle_id(DnsTransaction *t) {
334 uint16_t new_id;
335 assert(t);
336
337 /* Pick a new ID for this transaction. */
338
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);
341
342 log_debug("Transaction %" PRIu16 " is now %" PRIu16 ".", t->id, new_id);
343 t->id = new_id;
344
345 /* Make sure we generate a new packet with the new ID */
346 t->sent = dns_packet_unref(t->sent);
347 }
348
349 static void dns_transaction_tentative(DnsTransaction *t, DnsPacket *p) {
350 char key_str[DNS_RESOURCE_KEY_STRING_MAX];
351 DnsZoneItem *z;
352
353 assert(t);
354 assert(p);
355 assert(t->scope->protocol == DNS_PROTOCOL_LLMNR);
356
357 if (manager_packet_from_local_address(t->scope->manager, p) != 0)
358 return;
359
360 log_debug("Transaction %" PRIu16 " for <%s> on scope %s on %s/%s got tentative packet from %s.",
361 t->id,
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));
367
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.");
372 return;
373 }
374
375 log_debug("We have the lexicographically larger IP address and thus lost in the conflict.");
376
377 t->block_gc++;
378
379 while ((z = set_first(t->notify_zone_items))) {
380 /* First, make sure the zone item drops the reference
381 * to us */
382 dns_zone_item_probe_stop(z);
383
384 /* Secondly, report this as conflict, so that we might
385 * look for a different hostname */
386 dns_zone_item_conflict(z);
387 }
388 t->block_gc--;
389
390 dns_transaction_gc(t);
391 }
392
393 void dns_transaction_complete(DnsTransaction *t, DnsTransactionState state) {
394 DnsQueryCandidate *c;
395 DnsZoneItem *z;
396 DnsTransaction *d;
397 const char *st;
398 char key_str[DNS_RESOURCE_KEY_STRING_MAX];
399
400 assert(t);
401 assert(!DNS_TRANSACTION_IS_LIVE(state));
402
403 if (state == DNS_TRANSACTION_DNSSEC_FAILED) {
404 dns_resource_key_to_string(dns_transaction_key(t), key_str, sizeof key_str);
405
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));
415 }
416
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. */
420
421 if (state == DNS_TRANSACTION_ERRNO)
422 st = errno_to_name(t->answer_errno);
423 else
424 st = dns_transaction_state_to_string(state);
425
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",
428 t->id,
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),
433 st,
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");
438
439 t->state = state;
440
441 dns_transaction_close_connection(t, true);
442 dns_transaction_stop_timeout(t);
443
444 /* Notify all queries that are interested, but make sure the
445 * transaction isn't freed while we are still looking at it */
446 t->block_gc++;
447
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);
451
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);
457
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);
461
462 t->block_gc--;
463 dns_transaction_gc(t);
464 }
465
466 static void dns_transaction_complete_errno(DnsTransaction *t, int error) {
467 assert(t);
468 assert(error != 0);
469
470 t->answer_errno = abs(error);
471 dns_transaction_complete(t, DNS_TRANSACTION_ERRNO);
472 }
473
474 static int dns_transaction_pick_server(DnsTransaction *t) {
475 DnsServer *server;
476
477 assert(t);
478 assert(t->scope->protocol == DNS_PROTOCOL_DNS);
479
480 /* Pick a DNS server and a feature level for it. */
481
482 server = dns_scope_get_dns_server(t->scope);
483 if (!server)
484 return -ESRCH;
485
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;
490
491 t->current_feature_level = dns_server_possible_feature_level(server);
492
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;
497
498 log_debug("Using feature level %s for transaction %u.", dns_server_feature_level_to_string(t->current_feature_level), t->id);
499
500 if (server == t->server)
501 return 0;
502
503 dns_server_unref(t->server);
504 t->server = dns_server_ref(server);
505
506 t->n_picked_servers++;
507
508 log_debug("Using DNS server %s for transaction %u.", strna(dns_server_string_full(t->server)), t->id);
509
510 return 1;
511 }
512
513 static void dns_transaction_retry(DnsTransaction *t, bool next_server) {
514 int r;
515
516 assert(t);
517
518 /* Retries the transaction as it is, possibly on a different server */
519
520 if (next_server && t->scope->protocol == DNS_PROTOCOL_DNS)
521 log_debug("Retrying transaction %" PRIu16 ", after switching servers.", t->id);
522 else
523 log_debug("Retrying transaction %" PRIu16 ".", t->id);
524
525 /* Before we try again, switch to a new server. */
526 if (next_server)
527 dns_scope_next_dns_server(t->scope, t->server);
528
529 r = dns_transaction_go(t);
530 if (r < 0)
531 dns_transaction_complete_errno(t, r);
532 }
533
534 static bool dns_transaction_limited_retry(DnsTransaction *t) {
535 assert(t);
536
537 /* If we haven't tried all different servers yet, let's try again with a different server */
538
539 if (t->n_picked_servers >= dns_scope_get_n_dns_servers(t->scope))
540 return false;
541
542 dns_transaction_retry(t, /* next_server= */ true);
543 return true;
544 }
545
546 static int dns_transaction_maybe_restart(DnsTransaction *t) {
547 int r;
548
549 assert(t);
550
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. */
553
554 if (!t->server)
555 return 0;
556
557 if (t->current_feature_level <= dns_server_possible_feature_level(t->server))
558 return 0;
559
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 */
565
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);
568
569 r = dns_transaction_go(t);
570 if (r < 0)
571 return r;
572
573 return 1;
574 }
575
576 static void on_transaction_stream_error(DnsTransaction *t, int error) {
577 assert(t);
578
579 dns_transaction_close_connection(t, true);
580
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);
586 return;
587 }
588
589 dns_transaction_retry(t, true);
590 return;
591 }
592 if (error != 0)
593 dns_transaction_complete_errno(t, error);
594 }
595
596 static int dns_transaction_on_stream_packet(DnsTransaction *t, DnsStream *s, DnsPacket *p) {
597 bool encrypted;
598
599 assert(t);
600 assert(s);
601 assert(p);
602
603 encrypted = s->encrypted;
604
605 dns_transaction_close_connection(t, true);
606
607 if (dns_packet_validate_reply(p) <= 0) {
608 log_debug("Invalid TCP reply packet.");
609 dns_transaction_complete(t, DNS_TRANSACTION_INVALID_REPLY);
610 return 0;
611 }
612
613 dns_scope_check_conflicts(t->scope, p);
614
615 t->block_gc++;
616 dns_transaction_process_reply(t, p, encrypted);
617 t->block_gc--;
618
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);
624 else
625 dns_transaction_gc(t);
626
627 return 0;
628 }
629
630 static int on_stream_complete(DnsStream *s, int error) {
631 assert(s);
632
633 if (ERRNO_IS_DISCONNECT(error) && s->protocol != DNS_PROTOCOL_LLMNR) {
634 log_debug_errno(error, "Connection failure for DNS TCP stream: %m");
635
636 if (s->transactions) {
637 DnsTransaction *t;
638
639 t = s->transactions;
640 dns_server_packet_lost(t->server, IPPROTO_TCP, t->current_feature_level);
641 }
642 }
643
644 if (error != 0) {
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);
648
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. */
654 DnsTransaction *t;
655 while ((t = s->transactions))
656 on_transaction_stream_error(t, error);
657 }
658
659 return 0;
660 }
661
662 static int on_stream_packet(DnsStream *s, DnsPacket *p) {
663 DnsTransaction *t;
664
665 assert(s);
666 assert(s->manager);
667 assert(p);
668
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);
673
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));
676 return 0;
677 }
678
679 static uint16_t dns_transaction_port(DnsTransaction *t) {
680 assert(t);
681
682 if (t->server->port > 0)
683 return t->server->port;
684
685 return DNS_SERVER_FEATURE_LEVEL_IS_TLS(t->current_feature_level) ? 853 : 53;
686 }
687
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;
693 DnsStreamType type;
694 int r;
695
696 assert(t);
697 assert(t->sent);
698
699 dns_transaction_close_connection(t, true);
700
701 switch (t->scope->protocol) {
702
703 case DNS_PROTOCOL_DNS:
704 r = dns_transaction_pick_server(t);
705 if (r < 0)
706 return r;
707
708 if (manager_server_is_stub(t->scope->manager, t->server))
709 return -ELOOP;
710
711 if (!t->bypass) {
712 if (!dns_server_dnssec_supported(t->server) && dns_type_is_dnssec(dns_transaction_key(t)->type))
713 return -EOPNOTSUPP;
714
715 r = dns_server_adjust_opt(t->server, t->sent, t->current_feature_level);
716 if (r < 0)
717 return r;
718 }
719
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);
722 else
723 fd = dns_scope_socket_tcp(t->scope, AF_UNSPEC, NULL, t->server, dns_transaction_port(t), &sa);
724
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;
732
733 type = DNS_STREAM_LOOKUP;
734 break;
735
736 case DNS_PROTOCOL_LLMNR:
737 /* When we already received a reply to this (but it was truncated), send to its sender address */
738 if (t->received)
739 fd = dns_scope_socket_tcp(t->scope, t->received->family, &t->received->sender, NULL, t->received->sender_port, &sa);
740 else {
741 union in_addr_union address;
742 int family = AF_UNSPEC;
743
744 /* Otherwise, try to talk to the owner of a
745 * the IP address, in case this is a reverse
746 * PTR lookup */
747
748 r = dns_name_address(dns_resource_key_name(dns_transaction_key(t)), &family, &address);
749 if (r < 0)
750 return r;
751 if (r == 0)
752 return -EINVAL;
753 if (family != t->scope->family)
754 return -ESRCH;
755
756 fd = dns_scope_socket_tcp(t->scope, family, &address, NULL, LLMNR_PORT, &sa);
757 }
758
759 type = DNS_STREAM_LLMNR_SEND;
760 break;
761
762 default:
763 return -EAFNOSUPPORT;
764 }
765
766 if (!s) {
767 if (fd < 0)
768 return fd;
769
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);
772 if (r < 0)
773 return r;
774
775 fd = -EBADF;
776
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)) {
780
781 assert(t->server);
782 r = dnstls_stream_connect_tls(s, t->server);
783 if (r < 0)
784 return r;
785 }
786 #endif
787
788 if (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);
792 }
793
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);
798 }
799
800 t->stream = TAKE_PTR(s);
801 LIST_PREPEND(transactions_by_stream, t->stream->transactions, t);
802
803 r = dns_stream_write_packet(t->stream, t->sent);
804 if (r < 0) {
805 dns_transaction_close_connection(t, /* use_graveyard= */ false);
806 return r;
807 }
808
809 dns_transaction_reset_answer(t);
810
811 t->tried_stream = true;
812
813 return 0;
814 }
815
816 static void dns_transaction_cache_answer(DnsTransaction *t) {
817 assert(t);
818
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))
822 return;
823
824 /* Caching disabled? */
825 if (t->scope->manager->enable_cache == DNS_CACHE_MODE_NO)
826 return;
827
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)
830 return;
831
832 /* Packet from localhost? */
833 if (!t->scope->manager->cache_from_localhost &&
834 in_addr_is_localhost(t->received->family, &t->received->sender) != 0)
835 return;
836
837 dns_cache_put(&t->scope->cache,
838 t->scope->manager->enable_cache,
839 t->scope->protocol,
840 dns_transaction_key(t),
841 t->answer_rcode,
842 t->answer,
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,
849 t->answer_nsec_ttl,
850 t->received->family,
851 &t->received->sender,
852 t->scope->manager->stale_retention_usec);
853 }
854
855 static bool dns_transaction_dnssec_is_live(DnsTransaction *t) {
856 DnsTransaction *dt;
857
858 assert(t);
859
860 SET_FOREACH(dt, t->dnssec_transactions)
861 if (DNS_TRANSACTION_IS_LIVE(dt->state))
862 return true;
863
864 return false;
865 }
866
867 static int dns_transaction_dnssec_ready(DnsTransaction *t) {
868 DnsTransaction *dt;
869 int r;
870
871 assert(t);
872
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. */
875
876 SET_FOREACH(dt, t->dnssec_transactions) {
877
878 switch (dt->state) {
879
880 case DNS_TRANSACTION_NULL:
881 case DNS_TRANSACTION_PENDING:
882 case DNS_TRANSACTION_VALIDATING:
883 /* Still ongoing */
884 return 0;
885
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));
889 goto fail;
890 }
891
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. */
896
897 case DNS_TRANSACTION_SUCCESS:
898 /* All good. */
899 break;
900
901 case DNS_TRANSACTION_DNSSEC_FAILED: {
902 DnsAnswer *empty;
903
904 /* We handle DNSSEC failures different from other errors, as we care about the DNSSEC
905 * validation result */
906
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 ? ")" : "");
914
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);
919 if (r < 0)
920 log_oom_debug();
921
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);
925 if (!empty)
926 return -ENOMEM;
927 DNS_ANSWER_REPLACE(t->answer, empty);
928
929 dns_transaction_complete(t, DNS_TRANSACTION_DNSSEC_FAILED);
930 return 0;
931 }
932
933 default:
934 log_debug("Auxiliary DNSSEC RR query failed with %s", dns_transaction_state_to_string(dt->state));
935 goto fail;
936 }
937 }
938
939 /* All is ready, we can go and validate */
940 return 1;
941
942 fail:
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.
946 *
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);
950 if (r < 0)
951 return r;
952 if (r > 0)
953 return 0; /* don't validate just yet, we restarted things */
954
955 t->answer_dnssec_result = DNSSEC_FAILED_AUXILIARY;
956 dns_transaction_complete(t, DNS_TRANSACTION_DNSSEC_FAILED);
957 return 0;
958 }
959
960 static void dns_transaction_process_dnssec(DnsTransaction *t) {
961 int r;
962
963 assert(t);
964
965 /* Are there ongoing DNSSEC transactions? If so, let's wait for them. */
966 r = dns_transaction_dnssec_ready(t);
967 if (r < 0)
968 goto fail;
969 if (r == 0) /* We aren't ready yet (or one of our auxiliary transactions failed, and we shouldn't validate now */
970 return;
971
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);
975 if (r < 0)
976 goto fail;
977 if (r > 0) /* Transaction got restarted... */
978 return;
979
980 /* All our auxiliary DNSSEC transactions are complete now. Try
981 * to validate our RRset now. */
982 r = dns_transaction_validate_dnssec(t);
983 if (r == -EBADMSG) {
984 dns_transaction_complete(t, DNS_TRANSACTION_INVALID_REPLY);
985 return;
986 }
987 if (r < 0)
988 goto fail;
989
990 if (t->answer_dnssec_result == DNSSEC_INCOMPATIBLE_SERVER &&
991 t->scope->dnssec_mode == DNSSEC_YES) {
992
993 /* We are not in automatic downgrade mode, and the server is bad. Let's try a different server, maybe
994 * that works. */
995
996 if (dns_transaction_limited_retry(t))
997 return;
998
999 /* OK, let's give up, apparently all servers we tried didn't work. */
1000 dns_transaction_complete(t, DNS_TRANSACTION_DNSSEC_FAILED);
1001 return;
1002 }
1003
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);
1010 return;
1011 }
1012
1013 if (t->answer_dnssec_result == DNSSEC_INCOMPATIBLE_SERVER)
1014 dns_server_warn_downgrade(t->server);
1015
1016 dns_transaction_cache_answer(t);
1017
1018 if (t->answer_rcode == DNS_RCODE_SUCCESS)
1019 dns_transaction_complete(t, DNS_TRANSACTION_SUCCESS);
1020 else
1021 dns_transaction_complete(t, DNS_TRANSACTION_RCODE_FAILURE);
1022
1023 return;
1024
1025 fail:
1026 dns_transaction_complete_errno(t, r);
1027 }
1028
1029 static int dns_transaction_has_positive_answer(DnsTransaction *t, DnsAnswerFlags *flags) {
1030 int r;
1031
1032 assert(t);
1033
1034 /* Checks whether the answer is positive, i.e. either a direct
1035 * answer to the question, or a CNAME/DNAME for it */
1036
1037 r = dns_answer_match_key(t->answer, dns_transaction_key(t), flags);
1038 if (r != 0)
1039 return r;
1040
1041 r = dns_answer_find_cname_or_dname(t->answer, dns_transaction_key(t), NULL, flags);
1042 if (r != 0)
1043 return r;
1044
1045 return false;
1046 }
1047
1048 static int dns_transaction_fix_rcode(DnsTransaction *t) {
1049 int r;
1050
1051 assert(t);
1052
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.
1062 *
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
1065 * lookup. */
1066
1067 if (t->answer_rcode != DNS_RCODE_NXDOMAIN)
1068 return 0;
1069
1070 r = dns_transaction_has_positive_answer(t, NULL);
1071 if (r <= 0)
1072 return r;
1073
1074 t->answer_rcode = DNS_RCODE_SUCCESS;
1075 return 0;
1076 }
1077
1078 void dns_transaction_process_reply(DnsTransaction *t, DnsPacket *p, bool encrypted) {
1079 bool retry_with_tcp = false;
1080 int r;
1081
1082 assert(t);
1083 assert(p);
1084 assert(t->scope);
1085 assert(t->scope->manager);
1086
1087 if (t->state != DNS_TRANSACTION_PENDING)
1088 return;
1089
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. */
1093
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++;
1096
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. */
1100
1101 log_debug("Processing incoming packet of size %zu on transaction %" PRIu16" (rcode=%s).",
1102 p->size,
1103 t->id, FORMAT_DNS_RCODE(DNS_PACKET_RCODE(p)));
1104
1105 switch (t->scope->protocol) {
1106
1107 case DNS_PROTOCOL_LLMNR:
1108 /* For LLMNR we will not accept any packets from other interfaces */
1109
1110 if (p->ifindex != dns_scope_ifindex(t->scope))
1111 return;
1112
1113 if (p->family != t->scope->family)
1114 return;
1115
1116 /* Tentative packets are not full responses but still
1117 * useful for identifying uniqueness conflicts during
1118 * probing. */
1119 if (DNS_PACKET_LLMNR_T(p)) {
1120 dns_transaction_tentative(t, p);
1121 return;
1122 }
1123
1124 break;
1125
1126 case DNS_PROTOCOL_MDNS:
1127 /* For mDNS we will not accept any packets from other interfaces */
1128
1129 if (p->ifindex != dns_scope_ifindex(t->scope))
1130 return;
1131
1132 if (p->family != t->scope->family)
1133 return;
1134
1135 break;
1136
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
1142 * us. */
1143 break;
1144
1145 default:
1146 assert_not_reached();
1147 }
1148
1149 if (t->received != p)
1150 DNS_PACKET_REPLACE(t->received, dns_packet_ref(p));
1151
1152 t->answer_source = DNS_TRANSACTION_NETWORK;
1153
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);
1158 return;
1159 }
1160
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);
1164 return;
1165 }
1166 }
1167
1168 if (DNS_PACKET_TC(p)) {
1169
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);
1173 return;
1174 }
1175
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;
1179
1180 } else if (t->scope->protocol == DNS_PROTOCOL_DNS &&
1181 DNS_PACKET_IS_FRAGMENTED(p)) {
1182
1183 /* Report the fragment size, so that we downgrade from LARGE to regular EDNS0 if needed */
1184 if (t->server)
1185 dns_server_packet_udp_fragmented(t->server, dns_packet_size_unfragmented(p));
1186
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.) */
1192
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;
1196 }
1197 }
1198
1199 if (retry_with_tcp) {
1200 r = dns_transaction_emit_tcp(t);
1201 if (r == -ESRCH) {
1202 /* No servers found? Damn! */
1203 dns_transaction_complete(t, DNS_TRANSACTION_NO_SERVERS);
1204 return;
1205 }
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);
1209 return;
1210 }
1211 if (r < 0) {
1212 /* On LLMNR, if we cannot connect to the host,
1213 * we immediately give up */
1214 if (t->scope->protocol != DNS_PROTOCOL_DNS)
1215 goto fail;
1216
1217 /* On DNS, couldn't send? Try immediately again, with a new server */
1218 if (dns_transaction_limited_retry(t))
1219 return;
1220
1221 /* No new server to try, give up */
1222 dns_transaction_complete(t, DNS_TRANSACTION_ATTEMPTS_MAX_REACHED);
1223 }
1224
1225 return;
1226 }
1227
1228 /* After the superficial checks, actually parse the message. */
1229 r = dns_packet_extract(p);
1230 if (r < 0) {
1231 if (t->server) {
1232 dns_server_packet_invalid(t->server, t->current_feature_level);
1233
1234 r = dns_transaction_maybe_restart(t);
1235 if (r < 0)
1236 goto fail;
1237 if (r > 0) /* Transaction got restarted... */
1238 return;
1239 }
1240
1241 dns_transaction_complete(t, DNS_TRANSACTION_INVALID_REPLY);
1242 return;
1243 }
1244
1245 switch (t->scope->protocol) {
1246
1247 case DNS_PROTOCOL_DNS: {
1248 assert(t->server);
1249
1250 (void) dns_packet_ede_rcode(p, &t->answer_ede_rcode, &t->answer_ede_msg);
1251
1252 if (!t->bypass &&
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));
1266
1267 t->answer_dnssec_result = DNSSEC_UPSTREAM_FAILURE;
1268 dns_transaction_complete(t, DNS_TRANSACTION_DNSSEC_FAILED);
1269 return;
1270 }
1271
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);
1280 return;
1281 }
1282
1283 /* OK, the query failed, but we still shouldn't degrade the feature set for
1284 * this server. */
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));
1290 break;
1291 }
1292
1293 /* Request failed, immediately try again with reduced features */
1294
1295 if (t->current_feature_level <= DNS_SERVER_FEATURE_LEVEL_UDP) {
1296
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. */
1303
1304 if (dns_transaction_limited_retry(t))
1305 return;
1306
1307 /* Give up, accept the rcode */
1308 log_debug("Server returned error: %s", FORMAT_DNS_RCODE(DNS_PACKET_RCODE(p)));
1309 break;
1310 }
1311
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)));
1323 } else {
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;
1328 break;
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;
1332 break;
1333 default:
1334 t->clamp_feature_level_servfail = t->current_feature_level - 1;
1335 }
1336
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));
1340 }
1341
1342 dns_transaction_retry(t, false /* use the same server */);
1343 return;
1344 }
1345
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));
1351 else
1352 log_debug("Server returned REFUSED, switching servers, and retrying.");
1353
1354 if (dns_transaction_limited_retry(t))
1355 return;
1356
1357 break;
1358 }
1359
1360 if (DNS_PACKET_TC(p))
1361 dns_server_packet_truncated(t->server, t->current_feature_level);
1362
1363 break;
1364 }
1365
1366 case DNS_PROTOCOL_LLMNR:
1367 case DNS_PROTOCOL_MDNS:
1368 dns_scope_packet_received(t->scope, p->timestamp - t->start_usec);
1369 break;
1370
1371 default:
1372 assert_not_reached();
1373 }
1374
1375 if (t->server) {
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 */
1378
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);
1382
1383 /* Report that the OPT RR was missing */
1384 if (!p->opt)
1385 dns_server_packet_bad_opt(t->server, t->current_feature_level);
1386
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);
1390
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));
1395 }
1396
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);
1399 if (r < 0)
1400 goto fail;
1401 if (r > 0) /* Transaction got restarted... */
1402 return;
1403
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));
1409 if (r < 0)
1410 goto fail;
1411 if (r == 0) {
1412 dns_transaction_complete(t, DNS_TRANSACTION_INVALID_REPLY);
1413 return;
1414 }
1415 }
1416
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);
1426
1427 r = dns_transaction_fix_rcode(t);
1428 if (r < 0)
1429 goto fail;
1430
1431 /* Block GC while starting requests for additional DNSSEC RRs */
1432 t->block_gc++;
1433 r = dns_transaction_request_dnssec_keys(t);
1434 t->block_gc--;
1435
1436 /* Maybe the transaction is ready for GC'ing now? If so, free it and return. */
1437 if (!dns_transaction_gc(t))
1438 return;
1439
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
1442 * quickly. */
1443 if (t->state != DNS_TRANSACTION_PENDING)
1444 return;
1445 if (r < 0)
1446 goto fail;
1447 if (r > 0) {
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);
1452 return;
1453 }
1454
1455 dns_transaction_process_dnssec(t);
1456 return;
1457
1458 fail:
1459 dns_transaction_complete_errno(t, r);
1460 }
1461
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);
1465 int r;
1466
1467 assert(t->scope);
1468
1469 r = manager_recv(t->scope->manager, fd, DNS_PROTOCOL_DNS, &p);
1470 if (r < 0) {
1471 if (ERRNO_IS_DISCONNECT(r)) {
1472 usec_t usec;
1473
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. */
1476
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);
1480
1481 dns_transaction_close_connection(t, /* use_graveyard = */ false);
1482
1483 if (dns_transaction_limited_retry(t)) /* Try a different server */
1484 return 0;
1485 }
1486 dns_transaction_complete_errno(t, r);
1487 return 0;
1488 }
1489 if (r == 0)
1490 /* Spurious wakeup without any data */
1491 return 0;
1492
1493 r = dns_packet_validate_reply(p);
1494 if (r < 0) {
1495 log_debug_errno(r, "Received invalid DNS packet as response, ignoring: %m");
1496 return 0;
1497 }
1498 if (r == 0) {
1499 log_debug("Received inappropriate DNS packet as response, ignoring.");
1500 return 0;
1501 }
1502
1503 if (DNS_PACKET_ID(p) != t->id) {
1504 log_debug("Received packet with incorrect transaction ID, ignoring.");
1505 return 0;
1506 }
1507
1508 dns_transaction_process_reply(t, p, false);
1509 return 0;
1510 }
1511
1512 static int dns_transaction_emit_udp(DnsTransaction *t) {
1513 int r;
1514
1515 assert(t);
1516
1517 if (t->scope->protocol == DNS_PROTOCOL_DNS) {
1518
1519 r = dns_transaction_pick_server(t);
1520 if (r < 0)
1521 return r;
1522
1523 if (manager_server_is_stub(t->scope->manager, t->server))
1524 return -ELOOP;
1525
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! */
1528
1529 if (!t->bypass && !dns_server_dnssec_supported(t->server) && dns_type_is_dnssec(dns_transaction_key(t)->type))
1530 return -EOPNOTSUPP;
1531
1532 if (r > 0 || t->dns_udp_fd < 0) { /* Server changed, or no connection yet. */
1533 int fd;
1534
1535 dns_transaction_close_connection(t, true);
1536
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);
1539
1540 fd = dns_scope_socket_udp(t->scope, t->server);
1541 if (fd < 0)
1542 return fd;
1543
1544 r = sd_event_add_io(t->scope->manager->event, &t->dns_udp_event_source, fd, EPOLLIN, on_dns_packet, t);
1545 if (r < 0) {
1546 safe_close(fd);
1547 return r;
1548 }
1549
1550 (void) sd_event_source_set_description(t->dns_udp_event_source, "dns-transaction-udp");
1551 t->dns_udp_fd = fd;
1552 }
1553
1554 if (!t->bypass) {
1555 r = dns_server_adjust_opt(t->server, t->sent, t->current_feature_level);
1556 if (r < 0)
1557 return r;
1558 }
1559 } else
1560 dns_transaction_close_connection(t, true);
1561
1562 r = dns_scope_emit_udp(t->scope, t->dns_udp_fd, t->server ? t->server->family : AF_UNSPEC, t->sent);
1563 if (r < 0)
1564 return r;
1565
1566 dns_transaction_reset_answer(t);
1567
1568 return 0;
1569 }
1570
1571 static int on_transaction_timeout(sd_event_source *s, usec_t usec, void *userdata) {
1572 DnsTransaction *t = ASSERT_PTR(userdata);
1573
1574 assert(s);
1575
1576 t->seen_timeout = true;
1577
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;
1581 } else {
1582 /* Timeout reached? Increase the timeout for the server used */
1583 switch (t->scope->protocol) {
1584
1585 case DNS_PROTOCOL_DNS:
1586 assert(t->server);
1587 dns_server_packet_lost(t->server, t->stream ? IPPROTO_TCP : IPPROTO_UDP, t->current_feature_level);
1588 break;
1589
1590 case DNS_PROTOCOL_LLMNR:
1591 case DNS_PROTOCOL_MDNS:
1592 dns_scope_packet_lost(t->scope, usec - t->start_usec);
1593 break;
1594
1595 default:
1596 assert_not_reached();
1597 }
1598
1599 log_debug("Timeout reached on transaction %" PRIu16 ".", t->id);
1600 }
1601
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
1604 * tried a bunch */
1605 return 0;
1606 }
1607
1608 static int dns_transaction_setup_timeout(
1609 DnsTransaction *t,
1610 usec_t timeout_usec /* relative */,
1611 usec_t next_usec /* CLOCK_BOOTTIME */) {
1612
1613 int r;
1614
1615 assert(t);
1616
1617 dns_transaction_stop_timeout(t);
1618
1619 r = sd_event_add_time_relative(
1620 t->scope->manager->event,
1621 &t->timeout_event_source,
1622 CLOCK_BOOTTIME,
1623 timeout_usec, 0,
1624 on_transaction_timeout, t);
1625 if (r < 0)
1626 return r;
1627
1628 (void) sd_event_source_set_description(t->timeout_event_source, "dns-transaction-timeout");
1629
1630 t->next_attempt_after = next_usec;
1631 t->state = DNS_TRANSACTION_PENDING;
1632 return 0;
1633 }
1634
1635 static usec_t transaction_get_resend_timeout(DnsTransaction *t) {
1636 assert(t);
1637 assert(t->scope);
1638
1639 switch (t->scope->protocol) {
1640
1641 case DNS_PROTOCOL_DNS:
1642
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
1645 * needlessly. */
1646 if (t->stream)
1647 return TRANSACTION_TCP_TIMEOUT_USEC;
1648
1649 return DNS_TIMEOUT_USEC;
1650
1651 case DNS_PROTOCOL_MDNS:
1652 if (t->probing)
1653 return MDNS_PROBING_INTERVAL_USEC;
1654
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;
1658
1659 case DNS_PROTOCOL_LLMNR:
1660 return t->scope->resend_timeout;
1661
1662 default:
1663 assert_not_reached();
1664 }
1665 }
1666
1667 static void dns_transaction_randomize_answer(DnsTransaction *t) {
1668 int r;
1669
1670 assert(t);
1671
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. */
1676
1677 if (t->scope->protocol != DNS_PROTOCOL_DNS)
1678 return;
1679
1680 /* No point in randomizing, if there's just one RR */
1681 if (dns_answer_size(t->answer) <= 1)
1682 return;
1683
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");
1687
1688 dns_answer_randomize(t->answer);
1689 }
1690
1691 static int dns_transaction_prepare(DnsTransaction *t, usec_t ts) {
1692 int r;
1693
1694 assert(t);
1695
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. */
1699
1700 dns_transaction_stop_timeout(t);
1701
1702 if (t->n_attempts == 1 && t->seen_timeout)
1703 t->scope->manager->n_timeouts_total++;
1704
1705 if (!dns_scope_network_good(t->scope)) {
1706 dns_transaction_complete(t, DNS_TRANSACTION_NETWORK_DOWN);
1707 return 0;
1708 }
1709
1710 if (t->n_attempts >= TRANSACTION_ATTEMPTS_MAX(t->scope->protocol)) {
1711 DnsTransactionState result;
1712
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
1715 * the name. */
1716 result = DNS_TRANSACTION_NOT_FOUND;
1717 else
1718 result = DNS_TRANSACTION_ATTEMPTS_MAX_REACHED;
1719
1720 dns_transaction_complete(t, result);
1721 return 0;
1722 }
1723
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);
1728 return 0;
1729 }
1730
1731 t->n_attempts++;
1732 t->start_usec = ts;
1733
1734 dns_transaction_reset_answer(t);
1735 dns_transaction_flush_dnssec_transactions(t);
1736
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);
1741 if (r < 0)
1742 return r;
1743 if (r > 0) {
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);
1748 return 0;
1749 }
1750
1751 if (dns_name_is_root(dns_resource_key_name(dns_transaction_key(t))) &&
1752 dns_transaction_key(t)->type == DNS_TYPE_DS) {
1753
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
1756 * logic anymore. */
1757
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. */
1762
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);
1768 } else
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);
1774
1775 return 0;
1776 }
1777 }
1778
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);
1782 if (r < 0)
1783 return r;
1784 if (r > 0) {
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);
1789 return 0;
1790 }
1791 }
1792
1793 /* Check the cache. */
1794 if (!FLAGS_SET(t->query_flags, SD_RESOLVED_NO_CACHE)) {
1795
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);
1799
1800 /* Let's then prune all outdated entries */
1801 dns_cache_prune(&t->scope->cache);
1802
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;
1810
1811 r = dns_cache_lookup(
1812 &t->scope->cache,
1813 dns_transaction_key(t),
1814 query_flags,
1815 &t->answer_rcode,
1816 &t->answer,
1817 &t->received,
1818 &t->answer_query_flags,
1819 &t->answer_dnssec_result);
1820 if (r < 0)
1821 return r;
1822 if (r > 0) {
1823 dns_transaction_randomize_answer(t);
1824
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
1827 * packet. */
1828 dns_transaction_reset_answer(t);
1829 else {
1830 if (t->n_attempts > 1 && !FLAGS_SET(query_flags, SD_RESOLVED_NO_STALE)) {
1831
1832 if (t->answer_rcode == DNS_RCODE_SUCCESS) {
1833 if (t->seen_timeout)
1834 t->scope->manager->n_timeouts_served_stale_total++;
1835 else
1836 t->scope->manager->n_failure_responses_served_stale_total++;
1837 }
1838
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));
1843 }
1844
1845 t->answer_source = DNS_TRANSACTION_CACHE;
1846 if (t->answer_rcode == DNS_RCODE_SUCCESS)
1847 dns_transaction_complete(t, DNS_TRANSACTION_SUCCESS);
1848 else {
1849 if (t->received)
1850 (void) dns_packet_ede_rcode(t->received, &t->answer_ede_rcode, &t->answer_ede_msg);
1851
1852 dns_transaction_complete(t, DNS_TRANSACTION_RCODE_FAILURE);
1853 }
1854 return 0;
1855 }
1856 }
1857 }
1858
1859 if (FLAGS_SET(t->query_flags, SD_RESOLVED_NO_NETWORK)) {
1860 dns_transaction_complete(t, DNS_TRANSACTION_NO_SOURCE);
1861 return 0;
1862 }
1863
1864 return 1;
1865 }
1866
1867 static int dns_packet_append_zone(DnsPacket *p, DnsTransaction *t, DnsResourceKey *k, unsigned *nscount) {
1868 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
1869 bool tentative;
1870 int r;
1871
1872 assert(p);
1873 assert(t);
1874 assert(k);
1875
1876 if (k->type != DNS_TYPE_ANY)
1877 return 0;
1878
1879 r = dns_zone_lookup(&t->scope->zone, k, t->scope->link->ifindex, &answer, NULL, &tentative);
1880 if (r < 0)
1881 return r;
1882
1883 return dns_packet_append_answer(p, answer, nscount);
1884 }
1885
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;
1890 unsigned qdcount;
1891 usec_t ts;
1892 int r;
1893
1894 assert(t);
1895 assert(t->scope);
1896 assert(t->scope->protocol == DNS_PROTOCOL_MDNS);
1897 assert(ret_packet);
1898 assert(ret_keys);
1899
1900 r = dns_packet_new_query(&p, t->scope->protocol, 0, false);
1901 if (r < 0)
1902 return r;
1903
1904 r = dns_packet_append_key(p, dns_transaction_key(t), 0, NULL);
1905 if (r < 0)
1906 return r;
1907
1908 qdcount = 1;
1909
1910 if (dns_key_is_shared(dns_transaction_key(t)))
1911 add_known_answers = true;
1912
1913 r = dns_packet_append_zone(p, t, dns_transaction_key(t), NULL);
1914 if (r < 0)
1915 return r;
1916
1917 /* Save appended keys */
1918 r = set_ensure_put(&keys, &dns_resource_key_hash_ops, dns_transaction_key(t));
1919 if (r < 0)
1920 return r;
1921
1922 assert_se(sd_event_now(t->scope->manager->event, CLOCK_BOOTTIME, &ts) >= 0);
1923
1924 LIST_FOREACH(transactions_by_scope, other, t->scope->transactions) {
1925
1926 /* Skip ourselves */
1927 if (other == t)
1928 continue;
1929
1930 if (other->state != DNS_TRANSACTION_PENDING)
1931 continue;
1932
1933 if (other->next_attempt_after > ts)
1934 continue;
1935
1936 if (!set_contains(keys, dns_transaction_key(other))) {
1937 size_t saved_packet_size;
1938
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. */
1942 if (r == -EMSGSIZE)
1943 break;
1944 if (r < 0)
1945 return r;
1946
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);
1950 break;
1951 }
1952 if (r < 0)
1953 return r;
1954
1955 r = set_ensure_put(&keys, &dns_resource_key_hash_ops, dns_transaction_key(other));
1956 if (r < 0)
1957 return r;
1958 }
1959
1960 r = dns_transaction_prepare(other, ts);
1961 if (r < 0)
1962 return r;
1963 if (r == 0)
1964 /* In this case, not only this transaction, but multiple transactions may be
1965 * freed. Hence, we need to restart the loop. */
1966 return -EAGAIN;
1967
1968 usec_t timeout = transaction_get_resend_timeout(other);
1969 r = dns_transaction_setup_timeout(other, timeout, usec_add(ts, timeout));
1970 if (r < 0)
1971 return r;
1972
1973 if (dns_key_is_shared(dns_transaction_key(other)))
1974 add_known_answers = true;
1975
1976 qdcount++;
1977 if (qdcount >= UINT16_MAX)
1978 break;
1979 }
1980
1981 DNS_PACKET_HEADER(p)->qdcount = htobe16(qdcount);
1982
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);
1986 if (r < 0)
1987 return r;
1988 }
1989
1990 *ret_packet = TAKE_PTR(p);
1991 *ret_keys = TAKE_PTR(keys);
1992 return add_known_answers;
1993 }
1994
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;
1999 DnsResourceKey *k;
2000 unsigned c;
2001 int r;
2002
2003 assert(t);
2004 assert(t->scope->protocol == DNS_PROTOCOL_MDNS);
2005
2006 /* Discard any previously prepared packet, so we can start over and coalesce again */
2007 t->sent = dns_packet_unref(t->sent);
2008
2009 /* First, create a dummy packet to calculate the number of known answers to be appended in the first packet. */
2010 for (;;) {
2011 r = mdns_make_dummy_packet(t, &dummy, &keys);
2012 if (r == -EAGAIN)
2013 continue;
2014 if (r < 0)
2015 return r;
2016
2017 add_known_answers = r;
2018 break;
2019 }
2020
2021 /* Then, create actual packet. */
2022 r = dns_packet_new_query(&p, t->scope->protocol, 0, false);
2023 if (r < 0)
2024 return r;
2025
2026 /* Questions */
2027 c = 0;
2028 SET_FOREACH(k, keys) {
2029 r = dns_packet_append_key(p, k, 0, NULL);
2030 if (r < 0)
2031 return r;
2032 c++;
2033 }
2034 DNS_PACKET_HEADER(p)->qdcount = htobe16(c);
2035
2036 /* Known answers */
2037 if (add_known_answers) {
2038 usec_t ts;
2039
2040 assert_se(sd_event_now(t->scope->manager->event, CLOCK_BOOTTIME, &ts) >= 0);
2041
2042 r = dns_cache_export_shared_to_packet(&t->scope->cache, p, ts, be16toh(DNS_PACKET_HEADER(dummy)->ancount));
2043 if (r < 0)
2044 return r;
2045 }
2046
2047 /* Authorities */
2048 c = 0;
2049 SET_FOREACH(k, keys) {
2050 r = dns_packet_append_zone(p, t, k, &c);
2051 if (r < 0)
2052 return r;
2053 }
2054 DNS_PACKET_HEADER(p)->nscount = htobe16(c);
2055
2056 t->sent = TAKE_PTR(p);
2057 return 0;
2058 }
2059
2060 static int dns_transaction_make_packet(DnsTransaction *t) {
2061 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
2062 int r;
2063
2064 assert(t);
2065
2066 if (t->scope->protocol == DNS_PROTOCOL_MDNS)
2067 return dns_transaction_make_packet_mdns(t);
2068
2069 if (t->sent)
2070 return 0;
2071
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);
2076 if (r < 0)
2077 return r;
2078 } else {
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);
2084 if (r < 0)
2085 return r;
2086
2087 r = dns_packet_append_key(p, dns_transaction_key(t), 0, NULL);
2088 if (r < 0)
2089 return r;
2090
2091 DNS_PACKET_HEADER(p)->qdcount = htobe16(1);
2092 }
2093
2094 DNS_PACKET_HEADER(p)->id = t->id;
2095
2096 t->sent = TAKE_PTR(p);
2097 return 0;
2098 }
2099
2100 int dns_transaction_go(DnsTransaction *t) {
2101 usec_t ts;
2102 int r;
2103 char key_str[DNS_RESOURCE_KEY_STRING_MAX];
2104
2105 assert(t);
2106
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.
2109 */
2110
2111 assert_se(sd_event_now(t->scope->manager->event, CLOCK_BOOTTIME, &ts) >= 0);
2112
2113 r = dns_transaction_prepare(t, ts);
2114 if (r <= 0)
2115 return r;
2116
2117 log_debug("Firing %s transaction %" PRIu16 " for <%s> scope %s on %s/%s (validate=%s).",
2118 t->bypass ? "bypass" : "regular",
2119 t->id,
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)));
2125
2126 if (!t->initial_jitter_scheduled &&
2127 IN_SET(t->scope->protocol, DNS_PROTOCOL_LLMNR, DNS_PROTOCOL_MDNS)) {
2128 usec_t jitter;
2129
2130 /* RFC 4795 Section 2.7 suggests all LLMNR queries should be delayed by a random time from 0 to
2131 * JITTER_INTERVAL.
2132 * RFC 6762 Section 8.1 suggests initial probe queries should be delayed by a random time from
2133 * 0 to 250ms. */
2134
2135 t->initial_jitter_scheduled = true;
2136 t->n_attempts = 0;
2137
2138 switch (t->scope->protocol) {
2139
2140 case DNS_PROTOCOL_LLMNR:
2141 jitter = random_u64_range(LLMNR_JITTER_INTERVAL_USEC);
2142 break;
2143
2144 case DNS_PROTOCOL_MDNS:
2145 if (t->probing)
2146 jitter = random_u64_range(MDNS_PROBING_INTERVAL_USEC);
2147 else
2148 jitter = 0;
2149 break;
2150 default:
2151 assert_not_reached();
2152 }
2153
2154 r = dns_transaction_setup_timeout(t, jitter, ts);
2155 if (r < 0)
2156 return r;
2157
2158 log_debug("Delaying %s transaction %" PRIu16 " for " USEC_FMT "us.",
2159 dns_protocol_to_string(t->scope->protocol),
2160 t->id,
2161 jitter);
2162 return 1;
2163 }
2164
2165 /* Otherwise, we need to ask the network */
2166 r = dns_transaction_make_packet(t);
2167 if (r < 0)
2168 return r;
2169
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)) {
2173
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);
2177 } else {
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);
2181 if (r == -EMSGSIZE)
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);
2189 }
2190 if (r == -ELOOP) {
2191 if (t->scope->protocol != DNS_PROTOCOL_DNS)
2192 return r;
2193
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.");
2196
2197 dns_scope_next_dns_server(t->scope, t->server);
2198
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);
2202 return 0;
2203 }
2204
2205 return dns_transaction_go(t);
2206 }
2207 if (r == -ESRCH) {
2208 /* No servers to send this to? */
2209 dns_transaction_complete(t, DNS_TRANSACTION_NO_SERVERS);
2210 return 0;
2211 }
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);
2215 return 0;
2216 }
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);
2221 return 0;
2222 }
2223 if (r < 0) {
2224 if (t->scope->protocol != DNS_PROTOCOL_DNS)
2225 return r;
2226
2227 /* Couldn't send? Try immediately again, with a new server */
2228 dns_scope_next_dns_server(t->scope, t->server);
2229
2230 return dns_transaction_go(t);
2231 }
2232
2233 usec_t timeout = transaction_get_resend_timeout(t);
2234 r = dns_transaction_setup_timeout(t, timeout, usec_add(ts, timeout));
2235 if (r < 0)
2236 return r;
2237
2238 return 1;
2239 }
2240
2241 static int dns_transaction_find_cyclic(DnsTransaction *t, DnsTransaction *aux) {
2242 DnsTransaction *n;
2243 int r;
2244
2245 assert(t);
2246 assert(aux);
2247
2248 /* Try to find cyclic dependencies between transaction objects */
2249
2250 if (t == aux)
2251 return 1;
2252
2253 SET_FOREACH(n, aux->dnssec_transactions) {
2254 r = dns_transaction_find_cyclic(t, n);
2255 if (r != 0)
2256 return r;
2257 }
2258
2259 return 0;
2260 }
2261
2262 static int dns_transaction_add_dnssec_transaction(DnsTransaction *t, DnsResourceKey *key, DnsTransaction **ret) {
2263 _cleanup_(dns_transaction_gcp) DnsTransaction *aux = NULL;
2264 int r;
2265
2266 assert(t);
2267 assert(ret);
2268 assert(key);
2269
2270 aux = dns_scope_find_transaction(t->scope, key, t->query_flags);
2271 if (!aux) {
2272 r = dns_transaction_new(&aux, t->scope, key, NULL, t->query_flags);
2273 if (r < 0)
2274 return r;
2275 } else {
2276 if (set_contains(t->dnssec_transactions, aux)) {
2277 *ret = aux;
2278 return 0;
2279 }
2280
2281 r = dns_transaction_find_cyclic(t, aux);
2282 if (r < 0)
2283 return r;
2284 if (r > 0) {
2285 char s[DNS_RESOURCE_KEY_STRING_MAX], saux[DNS_RESOURCE_KEY_STRING_MAX];
2286
2287 return log_debug_errno(SYNTHETIC_ERRNO(ELOOP),
2288 "Potential cyclic dependency, refusing to add transaction %" PRIu16 " (%s) as dependency for %" PRIu16 " (%s).",
2289 aux->id,
2290 dns_resource_key_to_string(dns_transaction_key(t), s, sizeof s),
2291 t->id,
2292 dns_resource_key_to_string(dns_transaction_key(aux), saux, sizeof saux));
2293 }
2294 }
2295
2296 r = set_ensure_allocated(&aux->notify_transactions_done, NULL);
2297 if (r < 0)
2298 return r;
2299
2300 r = set_ensure_put(&t->dnssec_transactions, NULL, aux);
2301 if (r < 0)
2302 return r;
2303
2304 r = set_ensure_put(&aux->notify_transactions, NULL, t);
2305 if (r < 0) {
2306 (void) set_remove(t->dnssec_transactions, aux);
2307 return r;
2308 }
2309
2310 *ret = TAKE_PTR(aux);
2311 return 1;
2312 }
2313
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;
2317 int r;
2318
2319 assert(t);
2320 assert(key);
2321
2322 /* Try to get the data from the trust anchor */
2323 r = dns_trust_anchor_lookup_positive(&t->scope->manager->trust_anchor, key, &a);
2324 if (r < 0)
2325 return r;
2326 if (r > 0) {
2327 r = dns_answer_extend(&t->validated_keys, a);
2328 if (r < 0)
2329 return r;
2330
2331 if (ret)
2332 *ret = NULL;
2333 return 0;
2334 }
2335
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 */
2339 if (ret)
2340 *ret = NULL;
2341 return 0;
2342 }
2343 if (r < 0)
2344 return r;
2345
2346 if (aux->state == DNS_TRANSACTION_NULL) {
2347 r = dns_transaction_go(aux);
2348 if (r < 0)
2349 return r;
2350 }
2351 if (ret)
2352 *ret = aux;
2353
2354 return 1;
2355 }
2356
2357 static int dns_transaction_request_dnssec_rr(DnsTransaction *t, DnsResourceKey *key) {
2358 assert(t);
2359 assert(key);
2360 return dns_transaction_request_dnssec_rr_full(t, key, NULL);
2361 }
2362
2363 static int dns_transaction_negative_trust_anchor_lookup(DnsTransaction *t, const char *name) {
2364 int r;
2365
2366 assert(t);
2367
2368 /* Check whether the specified name is in the NTA
2369 * database, either in the global one, or the link-local
2370 * one. */
2371
2372 r = dns_trust_anchor_lookup_negative(&t->scope->manager->trust_anchor, name);
2373 if (r != 0)
2374 return r;
2375
2376 if (!t->scope->link)
2377 return 0;
2378
2379 return link_negative_trust_anchor_lookup(t->scope->link, name);
2380 }
2381
2382 static int dns_transaction_has_negative_answer(DnsTransaction *t) {
2383 int r;
2384
2385 assert(t);
2386
2387 /* Checks whether the answer is negative, and lacks NSEC/NSEC3
2388 * RRs to prove it */
2389
2390 r = dns_transaction_has_positive_answer(t, NULL);
2391 if (r < 0)
2392 return r;
2393 if (r > 0)
2394 return false;
2395
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)));
2399 if (r < 0)
2400 return r;
2401 return !r;
2402 }
2403
2404 static int dns_transaction_is_primary_response(DnsTransaction *t, DnsResourceRecord *rr) {
2405 int r;
2406
2407 assert(t);
2408 assert(rr);
2409
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. */
2413
2414 r = dns_resource_key_match_rr(dns_transaction_key(t), rr, NULL);
2415 if (r != 0)
2416 return r;
2417
2418 return dns_resource_key_match_cname_or_dname(dns_transaction_key(t), rr->key, NULL);
2419 }
2420
2421 static bool dns_transaction_dnssec_supported(DnsTransaction *t) {
2422 assert(t);
2423
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. */
2426
2427 if (t->scope->protocol != DNS_PROTOCOL_DNS)
2428 return false;
2429
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. */
2432 if (!t->server)
2433 return true;
2434
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. */
2439
2440 return dns_server_dnssec_supported(t->server);
2441 }
2442
2443 static bool dns_transaction_dnssec_supported_full(DnsTransaction *t) {
2444 DnsTransaction *dt;
2445
2446 assert(t);
2447
2448 /* Checks whether our transaction our any of the auxiliary transactions couldn't do DNSSEC. */
2449
2450 if (!dns_transaction_dnssec_supported(t))
2451 return false;
2452
2453 SET_FOREACH(dt, t->dnssec_transactions)
2454 if (!dns_transaction_dnssec_supported(dt))
2455 return false;
2456
2457 return true;
2458 }
2459
2460 int dns_transaction_request_dnssec_keys(DnsTransaction *t) {
2461 DnsResourceRecord *rr;
2462
2463 /* Have we already requested a record that would be sufficient to validate an insecure delegation? */
2464 bool chased_insecure = false;
2465 int r;
2466
2467 assert(t);
2468
2469 /*
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:
2473 *
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
2482 */
2483
2484 if (FLAGS_SET(t->query_flags, SD_RESOLVED_NO_VALIDATE) || t->scope->dnssec_mode == DNSSEC_NO)
2485 return 0;
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 */
2490
2491 DNS_ANSWER_FOREACH(rr, t->answer) {
2492
2493 if (dns_type_is_pseudo(rr->key->type))
2494 continue;
2495
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));
2498 if (r < 0)
2499 return r;
2500 if (r > 0)
2501 continue;
2502
2503 switch (rr->key->type) {
2504
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;
2509
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));
2516 if (r < 0)
2517 return r;
2518 if (r > 0)
2519 continue;
2520 }
2521
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
2532 * we deadlock. */
2533 r = dns_name_endswith(dns_resource_key_name(dns_transaction_key(t)), rr->rrsig.signer);
2534 if (r < 0)
2535 return r;
2536 if (r == 0)
2537 continue;
2538
2539 dnskey = dns_resource_key_new(rr->key->class, DNS_TYPE_DNSKEY, rr->rrsig.signer);
2540 if (!dnskey)
2541 return -ENOMEM;
2542
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);
2546 if (r < 0)
2547 return r;
2548
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
2551 * common case. */
2552 if (aux) {
2553 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *ds =
2554 dns_resource_key_new(rr->key->class, DNS_TYPE_DS, dns_resource_key_name(dnskey));
2555 if (!ds)
2556 return -ENOMEM;
2557 r = dns_transaction_request_dnssec_rr(t, ds);
2558 if (r < 0)
2559 return r;
2560 }
2561 break;
2562 }
2563
2564 case DNS_TYPE_DNSKEY: {
2565 /* For each DNSKEY we request the matching DS */
2566 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *ds = NULL;
2567
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. */
2574
2575 r = dns_name_endswith(dns_resource_key_name(dns_transaction_key(t)), dns_resource_key_name(rr->key));
2576 if (r < 0)
2577 return r;
2578 if (r == 0)
2579 continue;
2580
2581 ds = dns_resource_key_new(rr->key->class, DNS_TYPE_DS, dns_resource_key_name(rr->key));
2582 if (!ds)
2583 return -ENOMEM;
2584
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);
2588 if (r < 0)
2589 return r;
2590
2591 break;
2592 }
2593
2594 case DNS_TYPE_SOA:
2595 case DNS_TYPE_NS: {
2596 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *ds = NULL;
2597
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,
2603 * however. */
2604
2605 r = dns_resource_key_match_rr(dns_transaction_key(t), rr, NULL);
2606 if (r < 0)
2607 return r;
2608 if (r == 0) {
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. */
2612
2613 r = dns_answer_match_key(t->answer, dns_transaction_key(t), NULL);
2614 if (r < 0)
2615 return r;
2616 if (r > 0) /* positive reply, we won't need the SOA and hence don't need to validate
2617 * it. */
2618 continue;
2619
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));
2623 if (r < 0)
2624 return r;
2625 if (r == 0)
2626 continue;
2627 }
2628
2629 r = dnssec_has_rrsig(t->answer, rr->key);
2630 if (r < 0)
2631 return r;
2632 if (r > 0)
2633 continue;
2634
2635 chased_insecure = true;
2636 ds = dns_resource_key_new(rr->key->class, DNS_TYPE_DS, dns_resource_key_name(rr->key));
2637 if (!ds)
2638 return -ENOMEM;
2639
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);
2643 if (r < 0)
2644 return r;
2645
2646 break;
2647 }
2648
2649 case DNS_TYPE_DS:
2650 case DNS_TYPE_CNAME:
2651 case DNS_TYPE_DNAME: {
2652 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *ds = NULL;
2653 const char *name;
2654
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
2660 * question.
2661 *
2662 * Similar for DS RRs, which are signed when
2663 * the parent SOA is signed. */
2664
2665 r = dns_transaction_is_primary_response(t, rr);
2666 if (r < 0)
2667 return r;
2668 if (r == 0)
2669 continue;
2670
2671 r = dnssec_has_rrsig(t->answer, rr->key);
2672 if (r < 0)
2673 return r;
2674 if (r > 0)
2675 continue;
2676
2677 r = dns_answer_has_dname_for_cname(t->answer, rr);
2678 if (r < 0)
2679 return r;
2680 if (r > 0)
2681 continue;
2682
2683 name = dns_resource_key_name(rr->key);
2684 r = dns_name_parent(&name);
2685 if (r < 0)
2686 return r;
2687 if (r == 0)
2688 continue;
2689
2690 ds = dns_resource_key_new(rr->key->class, DNS_TYPE_DS, name);
2691 if (!ds)
2692 return -ENOMEM;
2693
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);
2697 if (r < 0)
2698 return r;
2699
2700 break;
2701 }
2702
2703 default: {
2704 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *ds = NULL;
2705
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
2711 * question. */
2712
2713 r = dns_transaction_is_primary_response(t, rr);
2714 if (r < 0)
2715 return r;
2716 if (r == 0)
2717 continue;
2718
2719 r = dnssec_has_rrsig(t->answer, rr->key);
2720 if (r < 0)
2721 return r;
2722 if (r > 0)
2723 continue;
2724
2725 ds = dns_resource_key_new(rr->key->class, DNS_TYPE_DS, dns_resource_key_name(rr->key));
2726 if (!ds)
2727 return -ENOMEM;
2728
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);
2732 if (r < 0)
2733 return r;
2734 break;
2735 }}
2736 }
2737
2738 /* Above, we requested everything necessary to validate what
2739 * we got. Now, let's request what we need to validate what we
2740 * didn't get... */
2741
2742 r = dns_transaction_has_negative_answer(t);
2743 if (r < 0)
2744 return r;
2745 if (r > 0) {
2746 const char *name = dns_resource_key_name(dns_transaction_key(t));
2747 bool was_signed = dns_answer_contains_nsec_or_nsec3(t->answer);
2748
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
2751 * progress.
2752 *
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. */
2756
2757 if (chased_insecure || was_signed)
2758 /* In this case we already requested what we need above. */
2759 name = NULL;
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)
2764 name = NULL;
2765
2766 if (name) {
2767 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *ds = NULL;
2768
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)));
2772
2773 ds = dns_resource_key_new(dns_transaction_key(t)->class, DNS_TYPE_DS, name);
2774 if (!ds)
2775 return -ENOMEM;
2776
2777 r = dns_transaction_request_dnssec_rr(t, ds);
2778 if (r < 0)
2779 return r;
2780 }
2781 }
2782
2783 return dns_transaction_dnssec_is_live(t);
2784 }
2785
2786 void dns_transaction_notify(DnsTransaction *t, DnsTransaction *source) {
2787 assert(t);
2788 assert(source);
2789
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. */
2793
2794 if (t->state == DNS_TRANSACTION_VALIDATING)
2795 dns_transaction_process_dnssec(t);
2796 }
2797
2798 static int dns_transaction_validate_dnskey_by_ds(DnsTransaction *t) {
2799 DnsAnswerItem *item;
2800 int r;
2801
2802 assert(t);
2803
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. */
2807
2808 DNS_ANSWER_FOREACH_ITEM(item, t->answer) {
2809
2810 r = dnssec_verify_dnskey_by_ds_search(item->rr, t->validated_keys);
2811 if (r < 0)
2812 return r;
2813 if (r == 0)
2814 continue;
2815
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);
2818 if (r < 0)
2819 return r;
2820 }
2821
2822 return 0;
2823 }
2824
2825 static int dns_transaction_requires_rrsig(DnsTransaction *t, DnsResourceRecord *rr) {
2826 int r;
2827
2828 assert(t);
2829 assert(rr);
2830
2831 /* Checks if the RR we are looking for must be signed with an
2832 * RRSIG. This is used for positive responses. */
2833
2834 if (t->scope->dnssec_mode == DNSSEC_NO)
2835 return false;
2836
2837 if (dns_type_is_pseudo(rr->key->type))
2838 return -EINVAL;
2839
2840 r = dns_transaction_negative_trust_anchor_lookup(t, dns_resource_key_name(rr->key));
2841 if (r < 0)
2842 return r;
2843 if (r > 0)
2844 return false;
2845
2846 switch (rr->key->type) {
2847
2848 case DNS_TYPE_RRSIG:
2849 /* RRSIGs are the signatures themselves, they need no signing. */
2850 return false;
2851
2852 case DNS_TYPE_SOA:
2853 case DNS_TYPE_NS: {
2854 DnsTransaction *dt;
2855
2856 /* For SOA or NS RRs we look for a matching DS transaction */
2857 SET_FOREACH(dt, t->dnssec_transactions) {
2858
2859 if (dns_transaction_key(dt)->class != rr->key->class)
2860 continue;
2861 if (dns_transaction_key(dt)->type != DNS_TYPE_DS)
2862 continue;
2863
2864 r = dns_name_endswith(dns_resource_key_name(rr->key), dns_resource_key_name(dns_transaction_key(dt)));
2865 if (r < 0)
2866 return r;
2867 if (r == 0)
2868 continue;
2869
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. */
2873
2874 if (!FLAGS_SET(dt->answer_query_flags, SD_RESOLVED_AUTHENTICATED))
2875 return false;
2876
2877 return dns_answer_match_key(dt->answer, dns_transaction_key(dt), NULL);
2878 }
2879
2880 /* We found nothing that proves this is safe to leave
2881 * this unauthenticated, hence ask inist on
2882 * authentication. */
2883 return true;
2884 }
2885
2886 case DNS_TYPE_DS:
2887 case DNS_TYPE_CNAME:
2888 case DNS_TYPE_DNAME: {
2889 const char *parent = NULL;
2890 DnsTransaction *dt;
2891
2892 /*
2893 * CNAME/DNAME RRs cannot be located at a zone apex, hence look directly for the parent DS.
2894 *
2895 * DS RRs are signed if the parent is signed, hence also look at the parent DS
2896 */
2897
2898 SET_FOREACH(dt, t->dnssec_transactions) {
2899
2900 if (dns_transaction_key(dt)->class != rr->key->class)
2901 continue;
2902 if (dns_transaction_key(dt)->type != DNS_TYPE_DS)
2903 continue;
2904
2905 if (!parent) {
2906 parent = dns_resource_key_name(rr->key);
2907 r = dns_name_parent(&parent);
2908 if (r < 0)
2909 return r;
2910 if (r == 0) {
2911 if (rr->key->type == DNS_TYPE_DS)
2912 return true;
2913
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);
2917 }
2918 }
2919
2920 r = dns_name_endswith(parent, dns_resource_key_name(dns_transaction_key(dt)));
2921 if (r < 0)
2922 return r;
2923 if (r == 0)
2924 continue;
2925
2926 return FLAGS_SET(dt->answer_query_flags, SD_RESOLVED_AUTHENTICATED);
2927 }
2928
2929 return true;
2930 }
2931
2932 default: {
2933 DnsTransaction *dt;
2934
2935 /* Any other kind of RR (including DNSKEY/NSEC/NSEC3). Let's see if our DS lookup was authenticated */
2936
2937 SET_FOREACH(dt, t->dnssec_transactions) {
2938 if (dns_transaction_key(dt)->class != rr->key->class)
2939 continue;
2940 if (dns_transaction_key(dt)->type != DNS_TYPE_DS)
2941 continue;
2942
2943 r = dns_name_endswith(dns_resource_key_name(rr->key), dns_resource_key_name(dns_transaction_key(dt)));
2944 if (r < 0)
2945 return r;
2946 if (r == 0)
2947 continue;
2948
2949 if (!FLAGS_SET(dt->answer_query_flags, SD_RESOLVED_AUTHENTICATED))
2950 return false;
2951
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);
2955 }
2956
2957 return true;
2958 }}
2959 }
2960
2961 static int dns_transaction_in_private_tld(DnsTransaction *t, const DnsResourceKey *key) {
2962 DnsTransaction *dt;
2963 const char *tld;
2964 int r;
2965
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.
2970 *
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.
2976 *
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. */
2983
2984 assert(t);
2985
2986 if (t->scope->dnssec_mode != DNSSEC_ALLOW_DOWNGRADE)
2987 return false; /* In strict DNSSEC mode what doesn't exist, doesn't exist */
2988
2989 tld = dns_resource_key_name(key);
2990 r = dns_name_parent(&tld);
2991 if (r < 0)
2992 return r;
2993 if (r == 0)
2994 return false; /* Already the root domain */
2995
2996 if (!dns_name_is_single_label(tld))
2997 return false;
2998
2999 SET_FOREACH(dt, t->dnssec_transactions) {
3000
3001 if (dns_transaction_key(dt)->class != key->class)
3002 continue;
3003
3004 r = dns_name_equal(dns_resource_key_name(dns_transaction_key(dt)), tld);
3005 if (r < 0)
3006 return r;
3007 if (r == 0)
3008 continue;
3009
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. */
3013
3014 return dt->answer_rcode == DNS_RCODE_NXDOMAIN;
3015 }
3016
3017 return false;
3018 }
3019
3020 static int dns_transaction_requires_nsec(DnsTransaction *t) {
3021 char key_str[DNS_RESOURCE_KEY_STRING_MAX];
3022 DnsTransaction *dt;
3023 const char *name;
3024 int r;
3025
3026 assert(t);
3027
3028 /* Checks if we need to insist on NSEC/NSEC3 RRs for proving
3029 * this negative reply */
3030
3031 if (t->scope->dnssec_mode == DNSSEC_NO)
3032 return false;
3033
3034 if (dns_type_is_pseudo(dns_transaction_key(t)->type))
3035 return -EINVAL;
3036
3037 r = dns_transaction_negative_trust_anchor_lookup(t, dns_resource_key_name(dns_transaction_key(t)));
3038 if (r < 0)
3039 return r;
3040 if (r > 0)
3041 return false;
3042
3043 r = dns_transaction_in_private_tld(t, dns_transaction_key(t));
3044 if (r < 0)
3045 return r;
3046 if (r > 0) {
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. */
3050
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));
3053 return false;
3054 }
3055
3056 name = dns_resource_key_name(dns_transaction_key(t));
3057
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);
3062 if (r < 0)
3063 return r;
3064 if (r == 0)
3065 return true;
3066 }
3067
3068 /* For all other RRs we check the DS on the same level to see
3069 * if it's signed. */
3070
3071 SET_FOREACH(dt, t->dnssec_transactions) {
3072 if (dns_transaction_key(dt)->class != dns_transaction_key(t)->class)
3073 continue;
3074 if (dns_transaction_key(dt)->type != DNS_TYPE_DS)
3075 continue;
3076
3077 r = dns_name_endswith(name, dns_resource_key_name(dns_transaction_key(dt)));
3078 if (r < 0)
3079 return r;
3080 if (r == 0)
3081 continue;
3082
3083 if (!FLAGS_SET(dt->answer_query_flags, SD_RESOLVED_AUTHENTICATED))
3084 return false;
3085
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);
3089 }
3090
3091 /* If in doubt, require NSEC/NSEC3 */
3092 return true;
3093 }
3094
3095 static int dns_transaction_dnskey_authenticated(DnsTransaction *t, DnsResourceRecord *rr) {
3096 DnsResourceRecord *rrsig;
3097 bool found = false;
3098 int r;
3099
3100 /* Checks whether any of the DNSKEYs used for the RRSIGs for
3101 * the specified RRset is authenticated (i.e. has a matching
3102 * DS RR). */
3103
3104 r = dns_transaction_negative_trust_anchor_lookup(t, dns_resource_key_name(rr->key));
3105 if (r < 0)
3106 return r;
3107 if (r > 0)
3108 return false;
3109
3110 DNS_ANSWER_FOREACH(rrsig, t->answer) {
3111 DnsTransaction *dt;
3112
3113 r = dnssec_key_match_rrsig(rr->key, rrsig);
3114 if (r < 0)
3115 return r;
3116 if (r == 0)
3117 continue;
3118
3119 SET_FOREACH(dt, t->dnssec_transactions) {
3120
3121 if (dns_transaction_key(dt)->class != rr->key->class)
3122 continue;
3123
3124 if (dns_transaction_key(dt)->type == DNS_TYPE_DNSKEY) {
3125
3126 r = dns_name_equal(dns_resource_key_name(dns_transaction_key(dt)), rrsig->rrsig.signer);
3127 if (r < 0)
3128 return r;
3129 if (r == 0)
3130 continue;
3131
3132 /* OK, we found an auxiliary DNSKEY lookup. If that lookup is authenticated,
3133 * report this. */
3134
3135 if (FLAGS_SET(dt->answer_query_flags, SD_RESOLVED_AUTHENTICATED))
3136 return true;
3137
3138 found = true;
3139
3140 } else if (dns_transaction_key(dt)->type == DNS_TYPE_DS) {
3141
3142 r = dns_name_equal(dns_resource_key_name(dns_transaction_key(dt)), rrsig->rrsig.signer);
3143 if (r < 0)
3144 return r;
3145 if (r == 0)
3146 continue;
3147
3148 /* OK, we found an auxiliary DS lookup. If that lookup is authenticated and
3149 * non-zero, we won! */
3150
3151 if (!FLAGS_SET(dt->answer_query_flags, SD_RESOLVED_AUTHENTICATED))
3152 return false;
3153
3154 return dns_answer_match_key(dt->answer, dns_transaction_key(dt), NULL);
3155 }
3156 }
3157 }
3158
3159 return found ? false : -ENXIO;
3160 }
3161
3162 static int dns_transaction_known_signed(DnsTransaction *t, DnsResourceRecord *rr) {
3163 assert(t);
3164 assert(rr);
3165
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 */
3168
3169 return rr->key->class == DNS_CLASS_IN &&
3170 dns_name_is_root(dns_resource_key_name(rr->key));
3171 }
3172
3173 static int dns_transaction_check_revoked_trust_anchors(DnsTransaction *t) {
3174 DnsResourceRecord *rr;
3175 int r;
3176
3177 assert(t);
3178
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. */
3183
3184 DNS_ANSWER_FOREACH(rr, t->answer) {
3185 r = dns_trust_anchor_check_revoked(&t->scope->manager->trust_anchor, rr, t->answer);
3186 if (r < 0)
3187 return r;
3188 }
3189
3190 return 0;
3191 }
3192
3193 static int dns_transaction_invalidate_revoked_keys(DnsTransaction *t) {
3194 bool changed;
3195 int r;
3196
3197 assert(t);
3198
3199 /* Removes all DNSKEY/DS objects from t->validated_keys that
3200 * our trust anchors database considers revoked. */
3201
3202 do {
3203 DnsResourceRecord *rr;
3204
3205 changed = false;
3206
3207 DNS_ANSWER_FOREACH(rr, t->validated_keys) {
3208 r = dns_trust_anchor_is_revoked(&t->scope->manager->trust_anchor, rr);
3209 if (r < 0)
3210 return r;
3211 if (r > 0) {
3212 r = dns_answer_remove_by_rr(&t->validated_keys, rr);
3213 if (r < 0)
3214 return r;
3215
3216 assert(r > 0);
3217 changed = true;
3218 break;
3219 }
3220 }
3221 } while (changed);
3222
3223 return 0;
3224 }
3225
3226 static int dns_transaction_copy_validated(DnsTransaction *t) {
3227 DnsTransaction *dt;
3228 int r;
3229
3230 assert(t);
3231
3232 /* Copy all validated RRs from the auxiliary DNSSEC transactions into our set of validated RRs */
3233
3234 SET_FOREACH(dt, t->dnssec_transactions) {
3235
3236 if (DNS_TRANSACTION_IS_LIVE(dt->state))
3237 continue;
3238
3239 if (!FLAGS_SET(dt->answer_query_flags, SD_RESOLVED_AUTHENTICATED))
3240 continue;
3241
3242 r = dns_answer_extend(&t->validated_keys, dt->answer);
3243 if (r < 0)
3244 return r;
3245 }
3246
3247 return 0;
3248 }
3249
3250 typedef enum {
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 */
3254 } Phase;
3255
3256 static int dnssec_validate_records(
3257 DnsTransaction *t,
3258 Phase phase,
3259 bool *have_nsec,
3260 unsigned *nvalidations,
3261 DnsAnswer **validated) {
3262
3263 DnsResourceRecord *rr;
3264 int r;
3265
3266 assert(nvalidations);
3267
3268 /* Returns negative on error, 0 if validation failed, 1 to restart validation, 2 when finished. */
3269
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;
3274
3275 switch (rr->key->type) {
3276 case DNS_TYPE_RRSIG:
3277 continue;
3278
3279 case DNS_TYPE_DNSKEY:
3280 /* We validate DNSKEYs only in the DNSKEY and ALL phases */
3281 if (phase == DNSSEC_PHASE_NSEC)
3282 continue;
3283 break;
3284
3285 case DNS_TYPE_NSEC:
3286 case DNS_TYPE_NSEC3:
3287 *have_nsec = true;
3288
3289 /* We validate NSEC/NSEC3 only in the NSEC and ALL phases */
3290 if (phase == DNSSEC_PHASE_DNSKEY)
3291 continue;
3292 break;
3293
3294 default:
3295 /* We validate all other RRs only in the ALL phases */
3296 if (phase != DNSSEC_PHASE_ALL)
3297 continue;
3298 }
3299
3300 r = dnssec_verify_rrset_search(
3301 t->answer,
3302 rr->key,
3303 t->validated_keys,
3304 USEC_INFINITY,
3305 &result,
3306 &rrsig);
3307 if (r < 0)
3308 return r;
3309 *nvalidations += r;
3310
3311 log_debug("Looking at %s: %s", strna(dns_resource_record_to_string(rr)), dnssec_result_to_string(result));
3312
3313 if (result == DNSSEC_VALIDATED) {
3314 assert(rrsig);
3315
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. */
3319
3320 r = dns_answer_copy_by_key(&t->validated_keys, t->answer, rr->key, DNS_ANSWER_AUTHENTICATED, rrsig);
3321 if (r < 0)
3322 return r;
3323
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);
3327 if (r < 0)
3328 return r;
3329 }
3330
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);
3334 if (r < 0)
3335 return r;
3336
3337 manager_dnssec_verdict(t->scope->manager, DNSSEC_SECURE, rr->key);
3338
3339 /* Exit the loop, we dropped something from the answer, start from the beginning */
3340 return 1;
3341 }
3342
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)
3347 continue;
3348
3349 if (result == DNSSEC_VALIDATED_WILDCARD) {
3350 bool authenticated = false;
3351 const char *source;
3352
3353 assert(rrsig);
3354
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. */
3357
3358 /* First step, determine the source of synthesis */
3359 r = dns_resource_record_source(rrsig, &source);
3360 if (r < 0)
3361 return r;
3362
3363 r = dnssec_test_positive_wildcard(*validated,
3364 dns_resource_key_name(rr->key),
3365 source,
3366 rrsig->rrsig.signer,
3367 &authenticated);
3368
3369 /* Unless the NSEC proof showed that the key really doesn't exist something is off. */
3370 if (r == 0)
3371 result = DNSSEC_INVALID;
3372 else {
3373 r = dns_answer_move_by_key(
3374 validated,
3375 &t->answer,
3376 rr->key,
3377 authenticated ? (DNS_ANSWER_AUTHENTICATED|DNS_ANSWER_CACHEABLE) : 0,
3378 rrsig);
3379 if (r < 0)
3380 return r;
3381
3382 manager_dnssec_verdict(t->scope->manager, authenticated ? DNSSEC_SECURE : DNSSEC_INSECURE, rr->key);
3383
3384 /* Exit the loop, we dropped something from the answer, start from the beginning */
3385 return 1;
3386 }
3387 }
3388
3389 if (result == DNSSEC_NO_SIGNATURE) {
3390 r = dns_transaction_requires_rrsig(t, rr);
3391 if (r < 0)
3392 return r;
3393 if (r == 0) {
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(
3397 validated,
3398 &t->answer,
3399 rr->key,
3400 0,
3401 NULL);
3402 if (r < 0)
3403 return r;
3404
3405 manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, rr->key);
3406 return 1;
3407 }
3408
3409 r = dns_transaction_known_signed(t, rr);
3410 if (r < 0)
3411 return r;
3412 if (r > 0) {
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. */
3415
3416 dns_server_packet_rrsig_missing(t->server, t->current_feature_level);
3417
3418 if (t->scope->dnssec_mode == DNSSEC_ALLOW_DOWNGRADE) {
3419
3420 /* Downgrading is OK? If so, just consider the information unsigned */
3421
3422 r = dns_answer_move_by_key(validated, &t->answer, rr->key, 0, NULL);
3423 if (r < 0)
3424 return r;
3425
3426 manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, rr->key);
3427 return 1;
3428 }
3429
3430 /* Otherwise, fail */
3431 t->answer_dnssec_result = DNSSEC_INCOMPATIBLE_SERVER;
3432 return 0;
3433 }
3434
3435 r = dns_transaction_in_private_tld(t, rr->key);
3436 if (r < 0)
3437 return r;
3438 if (r > 0) {
3439 char s[DNS_RESOURCE_KEY_STRING_MAX];
3440
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. */
3443
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));
3446
3447 r = dns_answer_move_by_key(validated, &t->answer, rr->key, 0, NULL);
3448 if (r < 0)
3449 return r;
3450
3451 manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, rr->key);
3452 return 1;
3453 }
3454 }
3455
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);
3459 if (r < 0)
3460 return r;
3461
3462 manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, rr->key);
3463 return 1;
3464 }
3465
3466 if (IN_SET(result,
3467 DNSSEC_MISSING_KEY,
3468 DNSSEC_SIGNATURE_EXPIRED)) {
3469
3470 r = dns_transaction_dnskey_authenticated(t, rr);
3471 if (r < 0 && r != -ENXIO)
3472 return r;
3473 if (r == 0) {
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. */
3476
3477 r = dns_answer_move_by_key(validated, &t->answer, rr->key, 0, NULL);
3478 if (r < 0)
3479 return r;
3480
3481 manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, rr->key);
3482 return 1;
3483 }
3484 }
3485
3486 r = dns_transaction_is_primary_response(t, rr);
3487 if (r < 0)
3488 return r;
3489 if (r > 0) {
3490 /* Look for a matching DNAME for this CNAME */
3491 r = dns_answer_has_dname_for_cname(t->answer, rr);
3492 if (r < 0)
3493 return r;
3494 if (r == 0) {
3495 /* Also look among the stuff we already validated */
3496 r = dns_answer_has_dname_for_cname(*validated, rr);
3497 if (r < 0)
3498 return r;
3499 }
3500
3501 if (r == 0) {
3502 if (IN_SET(result,
3503 DNSSEC_INVALID,
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);
3510
3511 /* This is a primary response to our question, and it failed validation.
3512 * That's fatal. */
3513 t->answer_dnssec_result = result;
3514 return 0;
3515 }
3516
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. */
3520 }
3521
3522 /* This is just some auxiliary data. Just remove the RRset and continue. */
3523 r = dns_answer_remove_by_key(&t->answer, rr->key);
3524 if (r < 0)
3525 return r;
3526
3527 /* We dropped something from the answer, start from the beginning. */
3528 return 1;
3529 }
3530
3531 return 2; /* Finito. */
3532 }
3533
3534 int dns_transaction_validate_dnssec(DnsTransaction *t) {
3535 _cleanup_(dns_answer_unrefp) DnsAnswer *validated = NULL;
3536 Phase phase;
3537 DnsAnswerFlags flags;
3538 int r;
3539 char key_str[DNS_RESOURCE_KEY_STRING_MAX];
3540
3541 assert(t);
3542
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. */
3545
3546 if (FLAGS_SET(t->query_flags, SD_RESOLVED_NO_VALIDATE) || t->scope->dnssec_mode == DNSSEC_NO)
3547 return 0;
3548
3549 /* Already validated */
3550 if (t->answer_dnssec_result != _DNSSEC_RESULT_INVALID)
3551 return 0;
3552
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);
3557 return 0;
3558 }
3559
3560 /* Cached stuff is not affected by validation. */
3561 if (t->answer_source != DNS_TRANSACTION_NETWORK)
3562 return 0;
3563
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);
3568 return 0;
3569 }
3570
3571 log_debug("Validating response from transaction %" PRIu16 " (%s).",
3572 t->id,
3573 dns_resource_key_to_string(dns_transaction_key(t), key_str, sizeof key_str));
3574
3575 /* First, see if this response contains any revoked trust
3576 * anchors we care about */
3577 r = dns_transaction_check_revoked_trust_anchors(t);
3578 if (r < 0)
3579 return r;
3580
3581 /* Third, copy all RRs we acquired successfully from auxiliary RRs over. */
3582 r = dns_transaction_copy_validated(t);
3583 if (r < 0)
3584 return r;
3585
3586 /* Second, see if there are DNSKEYs we already know a
3587 * validated DS for. */
3588 r = dns_transaction_validate_dnskey_by_ds(t);
3589 if (r < 0)
3590 return r;
3591
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);
3597 if (r < 0)
3598 return r;
3599
3600 phase = DNSSEC_PHASE_DNSKEY;
3601 for (unsigned nvalidations = 0;;) {
3602 bool have_nsec = false;
3603
3604 r = dnssec_validate_records(t, phase, &have_nsec, &nvalidations, &validated);
3605 if (r <= 0) {
3606 DNS_ANSWER_REPLACE(t->answer, TAKE_PTR(validated));
3607 return r;
3608 }
3609
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
3613 * anyway. */
3614 t->answer_dnssec_result = DNSSEC_TOO_MANY_VALIDATIONS;
3615 DNS_ANSWER_REPLACE(t->answer, TAKE_PTR(validated));
3616 return 0;
3617 }
3618
3619 /* Try again as long as we managed to achieve something */
3620 if (r == 1)
3621 continue;
3622
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;
3626 continue;
3627 }
3628
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;
3633 continue;
3634 }
3635
3636 /* We're done */
3637 break;
3638 }
3639
3640 DNS_ANSWER_REPLACE(t->answer, TAKE_PTR(validated));
3641
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);
3647 if (r > 0) {
3648 /* Yes, it answers the question! */
3649
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);
3655 } else {
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);
3659 }
3660
3661 } else if (r == 0) {
3662 DnssecNsecResult nr;
3663 bool authenticated = false;
3664
3665 /* Bummer! Let's check NSEC/NSEC3 */
3666 r = dnssec_nsec_test(t->answer, dns_transaction_key(t), &nr, &authenticated, &t->answer_nsec_ttl);
3667 if (r < 0)
3668 return r;
3669
3670 switch (nr) {
3671
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);
3678
3679 manager_dnssec_verdict(t->scope->manager, authenticated ? DNSSEC_SECURE : DNSSEC_INSECURE, dns_transaction_key(t));
3680 break;
3681
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);
3688
3689 manager_dnssec_verdict(t->scope->manager, authenticated ? DNSSEC_SECURE : DNSSEC_INSECURE, dns_transaction_key(t));
3690 break;
3691
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);
3697
3698 manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, dns_transaction_key(t));
3699 break;
3700
3701 case DNSSEC_NSEC_NO_RR:
3702 /* No NSEC data? Bummer! */
3703
3704 r = dns_transaction_requires_nsec(t);
3705 if (r < 0)
3706 return r;
3707 if (r > 0) {
3708 t->answer_dnssec_result = DNSSEC_NO_SIGNATURE;
3709 manager_dnssec_verdict(t->scope->manager, DNSSEC_BOGUS, dns_transaction_key(t));
3710 } else {
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));
3714 }
3715
3716 break;
3717
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));
3722 break;
3723
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));
3729 break;
3730
3731 default:
3732 assert_not_reached();
3733 }
3734 }
3735
3736 return 1;
3737 }
3738
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",
3758 };
3759 DEFINE_STRING_TABLE_LOOKUP(dns_transaction_state, DnsTransactionState);
3760
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",
3766 };
3767 DEFINE_STRING_TABLE_LOOKUP(dns_transaction_source, DnsTransactionSource);