]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dns-transaction.c
resolved: wait to gc transactions if they might still give an answer
[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 /* We handle DNSSEC failures different from other errors, as we care about the DNSSEC
903 * validation result */
904
905 log_debug("Auxiliary DNSSEC RR query failed validation: %s%s%s%s%s%s",
906 dnssec_result_to_string(dt->answer_dnssec_result),
907 dt->answer_ede_rcode >= 0 ? " (" : "",
908 dt->answer_ede_rcode >= 0 ? FORMAT_DNS_EDE_RCODE(dt->answer_ede_rcode) : "",
909 (dt->answer_ede_rcode >= 0 && !isempty(dt->answer_ede_msg)) ? ": " : "",
910 dt->answer_ede_rcode >= 0 ? strempty(dt->answer_ede_msg) : "",
911 dt->answer_ede_rcode >= 0 ? ")" : "");
912
913 /* Copy error code over */
914 t->answer_dnssec_result = dt->answer_dnssec_result;
915 t->answer_ede_rcode = dt->answer_ede_rcode;
916 r = free_and_strdup(&t->answer_ede_msg, dt->answer_ede_msg);
917 if (r < 0)
918 log_oom_debug();
919
920 dns_transaction_complete(t, DNS_TRANSACTION_DNSSEC_FAILED);
921 return 0;
922
923 default:
924 log_debug("Auxiliary DNSSEC RR query failed with %s", dns_transaction_state_to_string(dt->state));
925 goto fail;
926 }
927 }
928
929 /* All is ready, we can go and validate */
930 return 1;
931
932 fail:
933 /* Some auxiliary DNSSEC transaction failed for some reason. Maybe we learned something about the
934 * server due to this failure, and the feature level is now different? Let's see and restart the
935 * transaction if so. If not, let's propagate the auxiliary failure.
936 *
937 * This is particularly relevant if an auxiliary request figured out that DNSSEC doesn't work, and we
938 * are in permissive DNSSEC mode, and thus should restart things without DNSSEC magic. */
939 r = dns_transaction_maybe_restart(t);
940 if (r < 0)
941 return r;
942 if (r > 0)
943 return 0; /* don't validate just yet, we restarted things */
944
945 t->answer_dnssec_result = DNSSEC_FAILED_AUXILIARY;
946 dns_transaction_complete(t, DNS_TRANSACTION_DNSSEC_FAILED);
947 return 0;
948 }
949
950 static void dns_transaction_process_dnssec(DnsTransaction *t) {
951 int r;
952
953 assert(t);
954
955 /* Are there ongoing DNSSEC transactions? If so, let's wait for them. */
956 r = dns_transaction_dnssec_ready(t);
957 if (r < 0)
958 goto fail;
959 if (r == 0) /* We aren't ready yet (or one of our auxiliary transactions failed, and we shouldn't validate now */
960 return;
961
962 /* See if we learnt things from the additional DNSSEC transactions, that we didn't know before, and better
963 * restart the lookup immediately. */
964 r = dns_transaction_maybe_restart(t);
965 if (r < 0)
966 goto fail;
967 if (r > 0) /* Transaction got restarted... */
968 return;
969
970 /* All our auxiliary DNSSEC transactions are complete now. Try
971 * to validate our RRset now. */
972 r = dns_transaction_validate_dnssec(t);
973 if (r == -EBADMSG) {
974 dns_transaction_complete(t, DNS_TRANSACTION_INVALID_REPLY);
975 return;
976 }
977 if (r < 0)
978 goto fail;
979
980 if (t->answer_dnssec_result == DNSSEC_INCOMPATIBLE_SERVER &&
981 t->scope->dnssec_mode == DNSSEC_YES) {
982
983 /* We are not in automatic downgrade mode, and the server is bad. Let's try a different server, maybe
984 * that works. */
985
986 if (dns_transaction_limited_retry(t))
987 return;
988
989 /* OK, let's give up, apparently all servers we tried didn't work. */
990 dns_transaction_complete(t, DNS_TRANSACTION_DNSSEC_FAILED);
991 return;
992 }
993
994 if (!IN_SET(t->answer_dnssec_result,
995 _DNSSEC_RESULT_INVALID, /* No DNSSEC validation enabled */
996 DNSSEC_VALIDATED, /* Answer is signed and validated successfully */
997 DNSSEC_UNSIGNED, /* Answer is right-fully unsigned */
998 DNSSEC_INCOMPATIBLE_SERVER)) { /* Server does not do DNSSEC (Yay, we are downgrade attack vulnerable!) */
999 dns_transaction_complete(t, DNS_TRANSACTION_DNSSEC_FAILED);
1000 return;
1001 }
1002
1003 if (t->answer_dnssec_result == DNSSEC_INCOMPATIBLE_SERVER)
1004 dns_server_warn_downgrade(t->server);
1005
1006 dns_transaction_cache_answer(t);
1007
1008 if (t->answer_rcode == DNS_RCODE_SUCCESS)
1009 dns_transaction_complete(t, DNS_TRANSACTION_SUCCESS);
1010 else
1011 dns_transaction_complete(t, DNS_TRANSACTION_RCODE_FAILURE);
1012
1013 return;
1014
1015 fail:
1016 dns_transaction_complete_errno(t, r);
1017 }
1018
1019 static int dns_transaction_has_positive_answer(DnsTransaction *t, DnsAnswerFlags *flags) {
1020 int r;
1021
1022 assert(t);
1023
1024 /* Checks whether the answer is positive, i.e. either a direct
1025 * answer to the question, or a CNAME/DNAME for it */
1026
1027 r = dns_answer_match_key(t->answer, dns_transaction_key(t), flags);
1028 if (r != 0)
1029 return r;
1030
1031 r = dns_answer_find_cname_or_dname(t->answer, dns_transaction_key(t), NULL, flags);
1032 if (r != 0)
1033 return r;
1034
1035 return false;
1036 }
1037
1038 static int dns_transaction_fix_rcode(DnsTransaction *t) {
1039 int r;
1040
1041 assert(t);
1042
1043 /* Fix up the RCODE to SUCCESS if we get at least one matching RR in a response. Note that this contradicts the
1044 * DNS RFCs a bit. Specifically, RFC 6604 Section 3 clarifies that the RCODE shall say something about a
1045 * CNAME/DNAME chain element coming after the last chain element contained in the message, and not the first
1046 * one included. However, it also indicates that not all DNS servers implement this correctly. Moreover, when
1047 * using DNSSEC we usually only can prove the first element of a CNAME/DNAME chain anyway, hence let's settle
1048 * on always processing the RCODE as referring to the immediate look-up we do, i.e. the first element of a
1049 * CNAME/DNAME chain. This way, we uniformly handle CNAME/DNAME chains, regardless if the DNS server
1050 * incorrectly implements RCODE, whether DNSSEC is in use, or whether the DNS server only supplied us with an
1051 * incomplete CNAME/DNAME chain.
1052 *
1053 * Or in other words: if we get at least one positive reply in a message we patch NXDOMAIN to become SUCCESS,
1054 * and then rely on the CNAME chasing logic to figure out that there's actually a CNAME error with a new
1055 * lookup. */
1056
1057 if (t->answer_rcode != DNS_RCODE_NXDOMAIN)
1058 return 0;
1059
1060 r = dns_transaction_has_positive_answer(t, NULL);
1061 if (r <= 0)
1062 return r;
1063
1064 t->answer_rcode = DNS_RCODE_SUCCESS;
1065 return 0;
1066 }
1067
1068 void dns_transaction_process_reply(DnsTransaction *t, DnsPacket *p, bool encrypted) {
1069 bool retry_with_tcp = false;
1070 int r;
1071
1072 assert(t);
1073 assert(p);
1074 assert(t->scope);
1075 assert(t->scope->manager);
1076
1077 if (t->state != DNS_TRANSACTION_PENDING)
1078 return;
1079
1080 /* Increment the total failure counter only when it is the first attempt at querying and the upstream
1081 * server returns a failure response code. This ensures a more accurate count of the number of queries
1082 * that received a failure response code, as it doesn't consider retries. */
1083
1084 if (t->n_attempts == 1 && !IN_SET(DNS_PACKET_RCODE(p), DNS_RCODE_SUCCESS, DNS_RCODE_NXDOMAIN))
1085 t->scope->manager->n_failure_responses_total++;
1086
1087 /* Note that this call might invalidate the query. Callers
1088 * should hence not attempt to access the query or transaction
1089 * after calling this function. */
1090
1091 log_debug("Processing incoming packet of size %zu on transaction %" PRIu16" (rcode=%s).",
1092 p->size,
1093 t->id, FORMAT_DNS_RCODE(DNS_PACKET_RCODE(p)));
1094
1095 switch (t->scope->protocol) {
1096
1097 case DNS_PROTOCOL_LLMNR:
1098 /* For LLMNR we will not accept any packets from other interfaces */
1099
1100 if (p->ifindex != dns_scope_ifindex(t->scope))
1101 return;
1102
1103 if (p->family != t->scope->family)
1104 return;
1105
1106 /* Tentative packets are not full responses but still
1107 * useful for identifying uniqueness conflicts during
1108 * probing. */
1109 if (DNS_PACKET_LLMNR_T(p)) {
1110 dns_transaction_tentative(t, p);
1111 return;
1112 }
1113
1114 break;
1115
1116 case DNS_PROTOCOL_MDNS:
1117 /* For mDNS we will not accept any packets from other interfaces */
1118
1119 if (p->ifindex != dns_scope_ifindex(t->scope))
1120 return;
1121
1122 if (p->family != t->scope->family)
1123 return;
1124
1125 break;
1126
1127 case DNS_PROTOCOL_DNS:
1128 /* Note that we do not need to verify the
1129 * addresses/port numbers of incoming traffic, as we
1130 * invoked connect() on our UDP socket in which case
1131 * the kernel already does the needed verification for
1132 * us. */
1133 break;
1134
1135 default:
1136 assert_not_reached();
1137 }
1138
1139 if (t->received != p)
1140 DNS_PACKET_REPLACE(t->received, dns_packet_ref(p));
1141
1142 t->answer_source = DNS_TRANSACTION_NETWORK;
1143
1144 if (p->ipproto == IPPROTO_TCP) {
1145 if (DNS_PACKET_TC(p)) {
1146 /* Truncated via TCP? Somebody must be fucking with us */
1147 dns_transaction_complete(t, DNS_TRANSACTION_INVALID_REPLY);
1148 return;
1149 }
1150
1151 if (DNS_PACKET_ID(p) != t->id) {
1152 /* Not the reply to our query? Somebody must be fucking with us */
1153 dns_transaction_complete(t, DNS_TRANSACTION_INVALID_REPLY);
1154 return;
1155 }
1156 }
1157
1158 if (DNS_PACKET_TC(p)) {
1159
1160 /* Truncated packets for mDNS are not allowed. Give up immediately. */
1161 if (t->scope->protocol == DNS_PROTOCOL_MDNS) {
1162 dns_transaction_complete(t, DNS_TRANSACTION_INVALID_REPLY);
1163 return;
1164 }
1165
1166 /* Response was truncated, let's try again with good old TCP */
1167 log_debug("Reply truncated, retrying via TCP.");
1168 retry_with_tcp = true;
1169
1170 } else if (t->scope->protocol == DNS_PROTOCOL_DNS &&
1171 DNS_PACKET_IS_FRAGMENTED(p)) {
1172
1173 /* Report the fragment size, so that we downgrade from LARGE to regular EDNS0 if needed */
1174 if (t->server)
1175 dns_server_packet_udp_fragmented(t->server, dns_packet_size_unfragmented(p));
1176
1177 if (t->current_feature_level > DNS_SERVER_FEATURE_LEVEL_UDP) {
1178 /* Packet was fragmented. Let's retry with TCP to avoid fragmentation attack
1179 * issues. (We don't do that on the lowest feature level however, since crappy DNS
1180 * servers often do not implement TCP, hence falling back to TCP on fragmentation is
1181 * counter-productive there.) */
1182
1183 log_debug("Reply fragmented, retrying via TCP. (Largest fragment size: %zu; Datagram size: %zu)",
1184 p->fragsize, p->size);
1185 retry_with_tcp = true;
1186 }
1187 }
1188
1189 if (retry_with_tcp) {
1190 r = dns_transaction_emit_tcp(t);
1191 if (r == -ESRCH) {
1192 /* No servers found? Damn! */
1193 dns_transaction_complete(t, DNS_TRANSACTION_NO_SERVERS);
1194 return;
1195 }
1196 if (r == -EOPNOTSUPP) {
1197 /* Tried to ask for DNSSEC RRs, on a server that doesn't do DNSSEC */
1198 dns_transaction_complete(t, DNS_TRANSACTION_RR_TYPE_UNSUPPORTED);
1199 return;
1200 }
1201 if (r < 0) {
1202 /* On LLMNR, if we cannot connect to the host,
1203 * we immediately give up */
1204 if (t->scope->protocol != DNS_PROTOCOL_DNS)
1205 goto fail;
1206
1207 /* On DNS, couldn't send? Try immediately again, with a new server */
1208 if (dns_transaction_limited_retry(t))
1209 return;
1210
1211 /* No new server to try, give up */
1212 dns_transaction_complete(t, DNS_TRANSACTION_ATTEMPTS_MAX_REACHED);
1213 }
1214
1215 return;
1216 }
1217
1218 /* After the superficial checks, actually parse the message. */
1219 r = dns_packet_extract(p);
1220 if (r < 0) {
1221 if (t->server) {
1222 dns_server_packet_invalid(t->server, t->current_feature_level);
1223
1224 r = dns_transaction_maybe_restart(t);
1225 if (r < 0)
1226 goto fail;
1227 if (r > 0) /* Transaction got restarted... */
1228 return;
1229 }
1230
1231 dns_transaction_complete(t, DNS_TRANSACTION_INVALID_REPLY);
1232 return;
1233 }
1234
1235 switch (t->scope->protocol) {
1236
1237 case DNS_PROTOCOL_DNS: {
1238 assert(t->server);
1239
1240 (void) dns_packet_ede_rcode(p, &t->answer_ede_rcode, &t->answer_ede_msg);
1241
1242 if (!t->bypass &&
1243 IN_SET(DNS_PACKET_RCODE(p), DNS_RCODE_FORMERR, DNS_RCODE_SERVFAIL, DNS_RCODE_NOTIMP)) {
1244 /* If the server has replied with detailed error data, using a degraded feature set
1245 * will likely not help anyone. Examine the detailed error to determine the best
1246 * course of action. */
1247 if (t->answer_ede_rcode >= 0 && DNS_PACKET_RCODE(p) == DNS_RCODE_SERVFAIL) {
1248 /* These codes are related to DNSSEC configuration errors. If accurate,
1249 * this is the domain operator's problem, and retrying won't help. */
1250 if (dns_ede_rcode_is_dnssec(t->answer_ede_rcode)) {
1251 log_debug("Server returned error: %s (%s%s%s). Lookup failed.",
1252 FORMAT_DNS_RCODE(DNS_PACKET_RCODE(p)),
1253 FORMAT_DNS_EDE_RCODE(t->answer_ede_rcode),
1254 isempty(t->answer_ede_msg) ? "" : ": ",
1255 strempty(t->answer_ede_msg));
1256
1257 t->answer_dnssec_result = DNSSEC_UPSTREAM_FAILURE;
1258 dns_transaction_complete(t, DNS_TRANSACTION_DNSSEC_FAILED);
1259 return;
1260 }
1261
1262 /* These codes probably indicate a transient error. Let's try again. */
1263 if (IN_SET(t->answer_ede_rcode, DNS_EDE_RCODE_NOT_READY, DNS_EDE_RCODE_NET_ERROR)) {
1264 log_debug("Server returned error: %s (%s%s%s), retrying transaction.",
1265 FORMAT_DNS_RCODE(DNS_PACKET_RCODE(p)),
1266 FORMAT_DNS_EDE_RCODE(t->answer_ede_rcode),
1267 isempty(t->answer_ede_msg) ? "" : ": ",
1268 strempty(t->answer_ede_msg));
1269 dns_transaction_retry(t, false);
1270 return;
1271 }
1272
1273 /* OK, the query failed, but we still shouldn't degrade the feature set for
1274 * this server. */
1275 log_debug("Server returned error: %s (%s%s%s)",
1276 FORMAT_DNS_RCODE(DNS_PACKET_RCODE(p)),
1277 FORMAT_DNS_EDE_RCODE(t->answer_ede_rcode),
1278 isempty(t->answer_ede_msg) ? "" : ": ",
1279 strempty(t->answer_ede_msg));
1280 break;
1281 }
1282
1283 /* Request failed, immediately try again with reduced features */
1284
1285 if (t->current_feature_level <= DNS_SERVER_FEATURE_LEVEL_UDP) {
1286
1287 /* This was already at UDP feature level? If so, it doesn't make sense to downgrade
1288 * this transaction anymore, but let's see if it might make sense to send the request
1289 * to a different DNS server instead. If not let's process the response, and accept the
1290 * rcode. Note that we don't retry on TCP, since that's a suitable way to mitigate
1291 * packet loss, but is not going to give us better rcodes should we actually have
1292 * managed to get them already at UDP level. */
1293
1294 if (dns_transaction_limited_retry(t))
1295 return;
1296
1297 /* Give up, accept the rcode */
1298 log_debug("Server returned error: %s", FORMAT_DNS_RCODE(DNS_PACKET_RCODE(p)));
1299 break;
1300 }
1301
1302 /* SERVFAIL can happen for many reasons and may be transient.
1303 * To avoid unnecessary downgrades retry once with the initial level.
1304 * Check for clamp_feature_level_servfail having an invalid value as a sign that this is the
1305 * first attempt to downgrade. If so, clamp to the current value so that the transaction
1306 * is retried without actually downgrading. If the next try also fails we will downgrade by
1307 * hitting the else branch below. */
1308 if (DNS_PACKET_RCODE(p) == DNS_RCODE_SERVFAIL &&
1309 t->clamp_feature_level_servfail < 0) {
1310 t->clamp_feature_level_servfail = t->current_feature_level;
1311 log_debug("Server returned error %s, retrying transaction.",
1312 FORMAT_DNS_RCODE(DNS_PACKET_RCODE(p)));
1313 } else {
1314 /* Reduce this feature level by one and try again. */
1315 switch (t->current_feature_level) {
1316 case DNS_SERVER_FEATURE_LEVEL_TLS_DO:
1317 t->clamp_feature_level_servfail = DNS_SERVER_FEATURE_LEVEL_TLS_PLAIN;
1318 break;
1319 case DNS_SERVER_FEATURE_LEVEL_TLS_PLAIN + 1:
1320 /* Skip plain TLS when TLS is not supported */
1321 t->clamp_feature_level_servfail = DNS_SERVER_FEATURE_LEVEL_TLS_PLAIN - 1;
1322 break;
1323 default:
1324 t->clamp_feature_level_servfail = t->current_feature_level - 1;
1325 }
1326
1327 log_debug("Server returned error %s, retrying transaction with reduced feature level %s.",
1328 FORMAT_DNS_RCODE(DNS_PACKET_RCODE(p)),
1329 dns_server_feature_level_to_string(t->clamp_feature_level_servfail));
1330 }
1331
1332 dns_transaction_retry(t, false /* use the same server */);
1333 return;
1334 }
1335
1336 if (DNS_PACKET_RCODE(p) == DNS_RCODE_REFUSED) {
1337 /* This server refused our request? If so, try again, use a different server */
1338 if (t->answer_ede_rcode >= 0)
1339 log_debug("Server returned REFUSED (%s), switching servers, and retrying.",
1340 FORMAT_DNS_EDE_RCODE(t->answer_ede_rcode));
1341 else
1342 log_debug("Server returned REFUSED, switching servers, and retrying.");
1343
1344 if (dns_transaction_limited_retry(t))
1345 return;
1346
1347 break;
1348 }
1349
1350 if (DNS_PACKET_TC(p))
1351 dns_server_packet_truncated(t->server, t->current_feature_level);
1352
1353 break;
1354 }
1355
1356 case DNS_PROTOCOL_LLMNR:
1357 case DNS_PROTOCOL_MDNS:
1358 dns_scope_packet_received(t->scope, p->timestamp - t->start_usec);
1359 break;
1360
1361 default:
1362 assert_not_reached();
1363 }
1364
1365 if (t->server) {
1366 /* Report that we successfully received a valid packet with a good rcode after we initially got a bad
1367 * rcode and subsequently downgraded the protocol */
1368
1369 if (IN_SET(DNS_PACKET_RCODE(p), DNS_RCODE_SUCCESS, DNS_RCODE_NXDOMAIN) &&
1370 t->clamp_feature_level_servfail != _DNS_SERVER_FEATURE_LEVEL_INVALID)
1371 dns_server_packet_rcode_downgrade(t->server, t->clamp_feature_level_servfail);
1372
1373 /* Report that the OPT RR was missing */
1374 if (!p->opt)
1375 dns_server_packet_bad_opt(t->server, t->current_feature_level);
1376
1377 /* Report that the server didn't copy our query DO bit from request to response */
1378 if (DNS_PACKET_DO(t->sent) && !DNS_PACKET_DO(t->received))
1379 dns_server_packet_do_off(t->server, t->current_feature_level);
1380
1381 /* Report that we successfully received a packet. We keep track of the largest packet
1382 * size/fragment size we got. Which is useful for announcing the EDNS(0) packet size we can
1383 * receive to our server. */
1384 dns_server_packet_received(t->server, p->ipproto, t->current_feature_level, dns_packet_size_unfragmented(p));
1385 }
1386
1387 /* See if we know things we didn't know before that indicate we better restart the lookup immediately. */
1388 r = dns_transaction_maybe_restart(t);
1389 if (r < 0)
1390 goto fail;
1391 if (r > 0) /* Transaction got restarted... */
1392 return;
1393
1394 /* When dealing with protocols other than mDNS only consider responses with equivalent query section
1395 * to the request. For mDNS this check doesn't make sense, because the section 6 of RFC6762 states
1396 * that "Multicast DNS responses MUST NOT contain any questions in the Question Section". */
1397 if (t->scope->protocol != DNS_PROTOCOL_MDNS) {
1398 r = dns_packet_is_reply_for(p, dns_transaction_key(t));
1399 if (r < 0)
1400 goto fail;
1401 if (r == 0) {
1402 dns_transaction_complete(t, DNS_TRANSACTION_INVALID_REPLY);
1403 return;
1404 }
1405 }
1406
1407 /* Install the answer as answer to the transaction. We ref the answer twice here: the main `answer`
1408 * field is later replaced by the DNSSEC validated subset. The 'answer_auxiliary' field carries the
1409 * original complete record set, including RRSIG and friends. We use this when passing data to
1410 * clients that ask for DNSSEC metadata. */
1411 DNS_ANSWER_REPLACE(t->answer, dns_answer_ref(p->answer));
1412 t->answer_rcode = DNS_PACKET_RCODE(p);
1413 t->answer_dnssec_result = _DNSSEC_RESULT_INVALID;
1414 SET_FLAG(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED, false);
1415 SET_FLAG(t->answer_query_flags, SD_RESOLVED_CONFIDENTIAL, encrypted);
1416
1417 r = dns_transaction_fix_rcode(t);
1418 if (r < 0)
1419 goto fail;
1420
1421 /* Block GC while starting requests for additional DNSSEC RRs */
1422 t->block_gc++;
1423 r = dns_transaction_request_dnssec_keys(t);
1424 t->block_gc--;
1425
1426 /* Maybe the transaction is ready for GC'ing now? If so, free it and return. */
1427 if (!dns_transaction_gc(t))
1428 return;
1429
1430 /* Requesting additional keys might have resulted in this transaction to fail, since the auxiliary
1431 * request failed for some reason. If so, we are not in pending state anymore, and we should exit
1432 * quickly. */
1433 if (t->state != DNS_TRANSACTION_PENDING)
1434 return;
1435 if (r < 0)
1436 goto fail;
1437 if (r > 0) {
1438 /* There are DNSSEC transactions pending now. Update the state accordingly. */
1439 t->state = DNS_TRANSACTION_VALIDATING;
1440 dns_transaction_close_connection(t, true);
1441 dns_transaction_stop_timeout(t);
1442 return;
1443 }
1444
1445 dns_transaction_process_dnssec(t);
1446 return;
1447
1448 fail:
1449 dns_transaction_complete_errno(t, r);
1450 }
1451
1452 static int on_dns_packet(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
1453 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
1454 DnsTransaction *t = ASSERT_PTR(userdata);
1455 int r;
1456
1457 assert(t->scope);
1458
1459 r = manager_recv(t->scope->manager, fd, DNS_PROTOCOL_DNS, &p);
1460 if (r < 0) {
1461 if (ERRNO_IS_DISCONNECT(r)) {
1462 usec_t usec;
1463
1464 /* UDP connection failures get reported via ICMP and then are possibly delivered to us on the
1465 * next recvmsg(). Treat this like a lost packet. */
1466
1467 log_debug_errno(r, "Connection failure for DNS UDP packet: %m");
1468 assert_se(sd_event_now(t->scope->manager->event, CLOCK_BOOTTIME, &usec) >= 0);
1469 dns_server_packet_lost(t->server, IPPROTO_UDP, t->current_feature_level);
1470
1471 dns_transaction_close_connection(t, /* use_graveyard = */ false);
1472
1473 if (dns_transaction_limited_retry(t)) /* Try a different server */
1474 return 0;
1475 }
1476 dns_transaction_complete_errno(t, r);
1477 return 0;
1478 }
1479 if (r == 0)
1480 /* Spurious wakeup without any data */
1481 return 0;
1482
1483 r = dns_packet_validate_reply(p);
1484 if (r < 0) {
1485 log_debug_errno(r, "Received invalid DNS packet as response, ignoring: %m");
1486 return 0;
1487 }
1488 if (r == 0) {
1489 log_debug("Received inappropriate DNS packet as response, ignoring.");
1490 return 0;
1491 }
1492
1493 if (DNS_PACKET_ID(p) != t->id) {
1494 log_debug("Received packet with incorrect transaction ID, ignoring.");
1495 return 0;
1496 }
1497
1498 dns_transaction_process_reply(t, p, false);
1499 return 0;
1500 }
1501
1502 static int dns_transaction_emit_udp(DnsTransaction *t) {
1503 int r;
1504
1505 assert(t);
1506
1507 if (t->scope->protocol == DNS_PROTOCOL_DNS) {
1508
1509 r = dns_transaction_pick_server(t);
1510 if (r < 0)
1511 return r;
1512
1513 if (manager_server_is_stub(t->scope->manager, t->server))
1514 return -ELOOP;
1515
1516 if (t->current_feature_level < DNS_SERVER_FEATURE_LEVEL_UDP || DNS_SERVER_FEATURE_LEVEL_IS_TLS(t->current_feature_level))
1517 return -EAGAIN; /* Sorry, can't do UDP, try TCP! */
1518
1519 if (!t->bypass && !dns_server_dnssec_supported(t->server) && dns_type_is_dnssec(dns_transaction_key(t)->type))
1520 return -EOPNOTSUPP;
1521
1522 if (r > 0 || t->dns_udp_fd < 0) { /* Server changed, or no connection yet. */
1523 int fd;
1524
1525 dns_transaction_close_connection(t, true);
1526
1527 /* Before we allocate a new UDP socket, let's process the graveyard a bit to free some fds */
1528 manager_socket_graveyard_process(t->scope->manager);
1529
1530 fd = dns_scope_socket_udp(t->scope, t->server);
1531 if (fd < 0)
1532 return fd;
1533
1534 r = sd_event_add_io(t->scope->manager->event, &t->dns_udp_event_source, fd, EPOLLIN, on_dns_packet, t);
1535 if (r < 0) {
1536 safe_close(fd);
1537 return r;
1538 }
1539
1540 (void) sd_event_source_set_description(t->dns_udp_event_source, "dns-transaction-udp");
1541 t->dns_udp_fd = fd;
1542 }
1543
1544 if (!t->bypass) {
1545 r = dns_server_adjust_opt(t->server, t->sent, t->current_feature_level);
1546 if (r < 0)
1547 return r;
1548 }
1549 } else
1550 dns_transaction_close_connection(t, true);
1551
1552 r = dns_scope_emit_udp(t->scope, t->dns_udp_fd, t->server ? t->server->family : AF_UNSPEC, t->sent);
1553 if (r < 0)
1554 return r;
1555
1556 dns_transaction_reset_answer(t);
1557
1558 return 0;
1559 }
1560
1561 static int on_transaction_timeout(sd_event_source *s, usec_t usec, void *userdata) {
1562 DnsTransaction *t = ASSERT_PTR(userdata);
1563
1564 assert(s);
1565
1566 t->seen_timeout = true;
1567
1568 if (t->initial_jitter_scheduled && !t->initial_jitter_elapsed) {
1569 log_debug("Initial jitter phase for transaction %" PRIu16 " elapsed.", t->id);
1570 t->initial_jitter_elapsed = true;
1571 } else {
1572 /* Timeout reached? Increase the timeout for the server used */
1573 switch (t->scope->protocol) {
1574
1575 case DNS_PROTOCOL_DNS:
1576 assert(t->server);
1577 dns_server_packet_lost(t->server, t->stream ? IPPROTO_TCP : IPPROTO_UDP, t->current_feature_level);
1578 break;
1579
1580 case DNS_PROTOCOL_LLMNR:
1581 case DNS_PROTOCOL_MDNS:
1582 dns_scope_packet_lost(t->scope, usec - t->start_usec);
1583 break;
1584
1585 default:
1586 assert_not_reached();
1587 }
1588
1589 log_debug("Timeout reached on transaction %" PRIu16 ".", t->id);
1590 }
1591
1592 dns_transaction_retry(t, /* next_server= */ true); /* try a different server, but given this means
1593 * packet loss, let's do so even if we already
1594 * tried a bunch */
1595 return 0;
1596 }
1597
1598 static int dns_transaction_setup_timeout(
1599 DnsTransaction *t,
1600 usec_t timeout_usec /* relative */,
1601 usec_t next_usec /* CLOCK_BOOTTIME */) {
1602
1603 int r;
1604
1605 assert(t);
1606
1607 dns_transaction_stop_timeout(t);
1608
1609 r = sd_event_add_time_relative(
1610 t->scope->manager->event,
1611 &t->timeout_event_source,
1612 CLOCK_BOOTTIME,
1613 timeout_usec, 0,
1614 on_transaction_timeout, t);
1615 if (r < 0)
1616 return r;
1617
1618 (void) sd_event_source_set_description(t->timeout_event_source, "dns-transaction-timeout");
1619
1620 t->next_attempt_after = next_usec;
1621 t->state = DNS_TRANSACTION_PENDING;
1622 return 0;
1623 }
1624
1625 static usec_t transaction_get_resend_timeout(DnsTransaction *t) {
1626 assert(t);
1627 assert(t->scope);
1628
1629 switch (t->scope->protocol) {
1630
1631 case DNS_PROTOCOL_DNS:
1632
1633 /* When we do TCP, grant a much longer timeout, as in this case there's no need for us to quickly
1634 * resend, as the kernel does that anyway for us, and we really don't want to interrupt it in that
1635 * needlessly. */
1636 if (t->stream)
1637 return TRANSACTION_TCP_TIMEOUT_USEC;
1638
1639 return DNS_TIMEOUT_USEC;
1640
1641 case DNS_PROTOCOL_MDNS:
1642 if (t->probing)
1643 return MDNS_PROBING_INTERVAL_USEC;
1644
1645 /* See RFC 6762 Section 5.1 suggests that timeout should be a few seconds. */
1646 assert(t->n_attempts > 0);
1647 return (1 << (t->n_attempts - 1)) * USEC_PER_SEC;
1648
1649 case DNS_PROTOCOL_LLMNR:
1650 return t->scope->resend_timeout;
1651
1652 default:
1653 assert_not_reached();
1654 }
1655 }
1656
1657 static void dns_transaction_randomize_answer(DnsTransaction *t) {
1658 int r;
1659
1660 assert(t);
1661
1662 /* Randomizes the order of the answer array. This is done for all cached responses, so that we return
1663 * a different order each time. We do this only for DNS traffic, in order to do some minimal, crappy
1664 * load balancing. We don't do this for LLMNR or mDNS, since the order (preferring link-local
1665 * addresses, and such like) might have meaning there, and load balancing is pointless. */
1666
1667 if (t->scope->protocol != DNS_PROTOCOL_DNS)
1668 return;
1669
1670 /* No point in randomizing, if there's just one RR */
1671 if (dns_answer_size(t->answer) <= 1)
1672 return;
1673
1674 r = dns_answer_reserve_or_clone(&t->answer, 0);
1675 if (r < 0) /* If this fails, just don't randomize, this is non-essential stuff after all */
1676 return (void) log_debug_errno(r, "Failed to clone answer record, not randomizing RR order of answer: %m");
1677
1678 dns_answer_randomize(t->answer);
1679 }
1680
1681 static int dns_transaction_prepare(DnsTransaction *t, usec_t ts) {
1682 int r;
1683
1684 assert(t);
1685
1686 /* Returns 0 if dns_transaction_complete() has been called. In that case the transaction and query
1687 * candidate objects may have been invalidated and must not be accessed. Returns 1 if the transaction
1688 * has been prepared. */
1689
1690 dns_transaction_stop_timeout(t);
1691
1692 if (t->n_attempts == 1 && t->seen_timeout)
1693 t->scope->manager->n_timeouts_total++;
1694
1695 if (!dns_scope_network_good(t->scope)) {
1696 dns_transaction_complete(t, DNS_TRANSACTION_NETWORK_DOWN);
1697 return 0;
1698 }
1699
1700 if (t->n_attempts >= TRANSACTION_ATTEMPTS_MAX(t->scope->protocol)) {
1701 DnsTransactionState result;
1702
1703 if (t->scope->protocol == DNS_PROTOCOL_LLMNR)
1704 /* If we didn't find anything on LLMNR, it's not an error, but a failure to resolve
1705 * the name. */
1706 result = DNS_TRANSACTION_NOT_FOUND;
1707 else
1708 result = DNS_TRANSACTION_ATTEMPTS_MAX_REACHED;
1709
1710 dns_transaction_complete(t, result);
1711 return 0;
1712 }
1713
1714 if (t->scope->protocol == DNS_PROTOCOL_LLMNR && t->tried_stream) {
1715 /* If we already tried via a stream, then we don't
1716 * retry on LLMNR. See RFC 4795, Section 2.7. */
1717 dns_transaction_complete(t, DNS_TRANSACTION_ATTEMPTS_MAX_REACHED);
1718 return 0;
1719 }
1720
1721 t->n_attempts++;
1722 t->start_usec = ts;
1723
1724 dns_transaction_reset_answer(t);
1725 dns_transaction_flush_dnssec_transactions(t);
1726
1727 /* Check the trust anchor. Do so only on classic DNS, since DNSSEC does not apply otherwise. */
1728 if (t->scope->protocol == DNS_PROTOCOL_DNS &&
1729 !FLAGS_SET(t->query_flags, SD_RESOLVED_NO_TRUST_ANCHOR)) {
1730 r = dns_trust_anchor_lookup_positive(&t->scope->manager->trust_anchor, dns_transaction_key(t), &t->answer);
1731 if (r < 0)
1732 return r;
1733 if (r > 0) {
1734 t->answer_rcode = DNS_RCODE_SUCCESS;
1735 t->answer_source = DNS_TRANSACTION_TRUST_ANCHOR;
1736 SET_FLAG(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED|SD_RESOLVED_CONFIDENTIAL, true);
1737 dns_transaction_complete(t, DNS_TRANSACTION_SUCCESS);
1738 return 0;
1739 }
1740
1741 if (dns_name_is_root(dns_resource_key_name(dns_transaction_key(t))) &&
1742 dns_transaction_key(t)->type == DNS_TYPE_DS) {
1743
1744 /* Hmm, this is a request for the root DS? A DS RR doesn't exist in the root zone,
1745 * and if our trust anchor didn't know it either, this means we cannot do any DNSSEC
1746 * logic anymore. */
1747
1748 if (t->scope->dnssec_mode == DNSSEC_ALLOW_DOWNGRADE) {
1749 /* We are in downgrade mode. In this case, synthesize an unsigned empty
1750 * response, so that the any lookup depending on this one can continue
1751 * assuming there was no DS, and hence the root zone was unsigned. */
1752
1753 t->answer_rcode = DNS_RCODE_SUCCESS;
1754 t->answer_source = DNS_TRANSACTION_TRUST_ANCHOR;
1755 SET_FLAG(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED, false);
1756 SET_FLAG(t->answer_query_flags, SD_RESOLVED_CONFIDENTIAL, true);
1757 dns_transaction_complete(t, DNS_TRANSACTION_SUCCESS);
1758 } else
1759 /* If we are not in downgrade mode, then fail the lookup, because we cannot
1760 * reasonably answer it. There might be DS RRs, but we don't know them, and
1761 * the DNS server won't tell them to us (and even if it would, we couldn't
1762 * validate and trust them. */
1763 dns_transaction_complete(t, DNS_TRANSACTION_NO_TRUST_ANCHOR);
1764
1765 return 0;
1766 }
1767 }
1768
1769 /* Check the zone. */
1770 if (!FLAGS_SET(t->query_flags, SD_RESOLVED_NO_ZONE)) {
1771 r = dns_zone_lookup(&t->scope->zone, dns_transaction_key(t), dns_scope_ifindex(t->scope), &t->answer, NULL, NULL);
1772 if (r < 0)
1773 return r;
1774 if (r > 0) {
1775 t->answer_rcode = DNS_RCODE_SUCCESS;
1776 t->answer_source = DNS_TRANSACTION_ZONE;
1777 SET_FLAG(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED|SD_RESOLVED_CONFIDENTIAL, true);
1778 dns_transaction_complete(t, DNS_TRANSACTION_SUCCESS);
1779 return 0;
1780 }
1781 }
1782
1783 /* Check the cache. */
1784 if (!FLAGS_SET(t->query_flags, SD_RESOLVED_NO_CACHE)) {
1785
1786 /* Before trying the cache, let's make sure we figured out a server to use. Should this cause
1787 * a change of server this might flush the cache. */
1788 (void) dns_scope_get_dns_server(t->scope);
1789
1790 /* Let's then prune all outdated entries */
1791 dns_cache_prune(&t->scope->cache);
1792
1793 /* For the initial attempt or when no stale data is requested, disable serve stale
1794 * and answer the question from the cache (honors ttl property).
1795 * On the second attempt, if StaleRetentionSec is greater than zero,
1796 * try to answer the question using stale date (honors until property) */
1797 uint64_t query_flags = t->query_flags;
1798 if (t->n_attempts == 1 || t->scope->manager->stale_retention_usec == 0)
1799 query_flags |= SD_RESOLVED_NO_STALE;
1800
1801 r = dns_cache_lookup(
1802 &t->scope->cache,
1803 dns_transaction_key(t),
1804 query_flags,
1805 &t->answer_rcode,
1806 &t->answer,
1807 &t->received,
1808 &t->answer_query_flags,
1809 &t->answer_dnssec_result);
1810 if (r < 0)
1811 return r;
1812 if (r > 0) {
1813 dns_transaction_randomize_answer(t);
1814
1815 if (t->bypass && t->scope->protocol == DNS_PROTOCOL_DNS && !t->received)
1816 /* When bypass mode is on, do not use cached data unless it came with a full
1817 * packet. */
1818 dns_transaction_reset_answer(t);
1819 else {
1820 if (t->n_attempts > 1 && !FLAGS_SET(query_flags, SD_RESOLVED_NO_STALE)) {
1821
1822 if (t->answer_rcode == DNS_RCODE_SUCCESS) {
1823 if (t->seen_timeout)
1824 t->scope->manager->n_timeouts_served_stale_total++;
1825 else
1826 t->scope->manager->n_failure_responses_served_stale_total++;
1827 }
1828
1829 char key_str[DNS_RESOURCE_KEY_STRING_MAX];
1830 log_debug("Serve Stale response rcode=%s for %s",
1831 FORMAT_DNS_RCODE(t->answer_rcode),
1832 dns_resource_key_to_string(dns_transaction_key(t), key_str, sizeof key_str));
1833 }
1834
1835 t->answer_source = DNS_TRANSACTION_CACHE;
1836 if (t->answer_rcode == DNS_RCODE_SUCCESS)
1837 dns_transaction_complete(t, DNS_TRANSACTION_SUCCESS);
1838 else {
1839 if (t->received)
1840 (void) dns_packet_ede_rcode(t->received, &t->answer_ede_rcode, &t->answer_ede_msg);
1841
1842 dns_transaction_complete(t, DNS_TRANSACTION_RCODE_FAILURE);
1843 }
1844 return 0;
1845 }
1846 }
1847 }
1848
1849 if (FLAGS_SET(t->query_flags, SD_RESOLVED_NO_NETWORK)) {
1850 dns_transaction_complete(t, DNS_TRANSACTION_NO_SOURCE);
1851 return 0;
1852 }
1853
1854 return 1;
1855 }
1856
1857 static int dns_packet_append_zone(DnsPacket *p, DnsTransaction *t, DnsResourceKey *k, unsigned *nscount) {
1858 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
1859 bool tentative;
1860 int r;
1861
1862 assert(p);
1863 assert(t);
1864 assert(k);
1865
1866 if (k->type != DNS_TYPE_ANY)
1867 return 0;
1868
1869 r = dns_zone_lookup(&t->scope->zone, k, t->scope->link->ifindex, &answer, NULL, &tentative);
1870 if (r < 0)
1871 return r;
1872
1873 return dns_packet_append_answer(p, answer, nscount);
1874 }
1875
1876 static int mdns_make_dummy_packet(DnsTransaction *t, DnsPacket **ret_packet, Set **ret_keys) {
1877 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
1878 _cleanup_set_free_ Set *keys = NULL;
1879 bool add_known_answers = false;
1880 unsigned qdcount;
1881 usec_t ts;
1882 int r;
1883
1884 assert(t);
1885 assert(t->scope);
1886 assert(t->scope->protocol == DNS_PROTOCOL_MDNS);
1887 assert(ret_packet);
1888 assert(ret_keys);
1889
1890 r = dns_packet_new_query(&p, t->scope->protocol, 0, false);
1891 if (r < 0)
1892 return r;
1893
1894 r = dns_packet_append_key(p, dns_transaction_key(t), 0, NULL);
1895 if (r < 0)
1896 return r;
1897
1898 qdcount = 1;
1899
1900 if (dns_key_is_shared(dns_transaction_key(t)))
1901 add_known_answers = true;
1902
1903 r = dns_packet_append_zone(p, t, dns_transaction_key(t), NULL);
1904 if (r < 0)
1905 return r;
1906
1907 /* Save appended keys */
1908 r = set_ensure_put(&keys, &dns_resource_key_hash_ops, dns_transaction_key(t));
1909 if (r < 0)
1910 return r;
1911
1912 assert_se(sd_event_now(t->scope->manager->event, CLOCK_BOOTTIME, &ts) >= 0);
1913
1914 LIST_FOREACH(transactions_by_scope, other, t->scope->transactions) {
1915
1916 /* Skip ourselves */
1917 if (other == t)
1918 continue;
1919
1920 if (other->state != DNS_TRANSACTION_PENDING)
1921 continue;
1922
1923 if (other->next_attempt_after > ts)
1924 continue;
1925
1926 if (!set_contains(keys, dns_transaction_key(other))) {
1927 size_t saved_packet_size;
1928
1929 r = dns_packet_append_key(p, dns_transaction_key(other), 0, &saved_packet_size);
1930 /* If we can't stuff more questions into the packet, just give up.
1931 * One of the 'other' transactions will fire later and take care of the rest. */
1932 if (r == -EMSGSIZE)
1933 break;
1934 if (r < 0)
1935 return r;
1936
1937 r = dns_packet_append_zone(p, t, dns_transaction_key(other), NULL);
1938 if (r == -EMSGSIZE) {
1939 dns_packet_truncate(p, saved_packet_size);
1940 break;
1941 }
1942 if (r < 0)
1943 return r;
1944
1945 r = set_ensure_put(&keys, &dns_resource_key_hash_ops, dns_transaction_key(other));
1946 if (r < 0)
1947 return r;
1948 }
1949
1950 r = dns_transaction_prepare(other, ts);
1951 if (r < 0)
1952 return r;
1953 if (r == 0)
1954 /* In this case, not only this transaction, but multiple transactions may be
1955 * freed. Hence, we need to restart the loop. */
1956 return -EAGAIN;
1957
1958 usec_t timeout = transaction_get_resend_timeout(other);
1959 r = dns_transaction_setup_timeout(other, timeout, usec_add(ts, timeout));
1960 if (r < 0)
1961 return r;
1962
1963 if (dns_key_is_shared(dns_transaction_key(other)))
1964 add_known_answers = true;
1965
1966 qdcount++;
1967 if (qdcount >= UINT16_MAX)
1968 break;
1969 }
1970
1971 DNS_PACKET_HEADER(p)->qdcount = htobe16(qdcount);
1972
1973 /* Append known answers section if we're asking for any shared record */
1974 if (add_known_answers) {
1975 r = dns_cache_export_shared_to_packet(&t->scope->cache, p, ts, 0);
1976 if (r < 0)
1977 return r;
1978 }
1979
1980 *ret_packet = TAKE_PTR(p);
1981 *ret_keys = TAKE_PTR(keys);
1982 return add_known_answers;
1983 }
1984
1985 static int dns_transaction_make_packet_mdns(DnsTransaction *t) {
1986 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL, *dummy = NULL;
1987 _cleanup_set_free_ Set *keys = NULL;
1988 bool add_known_answers;
1989 DnsResourceKey *k;
1990 unsigned c;
1991 int r;
1992
1993 assert(t);
1994 assert(t->scope->protocol == DNS_PROTOCOL_MDNS);
1995
1996 /* Discard any previously prepared packet, so we can start over and coalesce again */
1997 t->sent = dns_packet_unref(t->sent);
1998
1999 /* First, create a dummy packet to calculate the number of known answers to be appended in the first packet. */
2000 for (;;) {
2001 r = mdns_make_dummy_packet(t, &dummy, &keys);
2002 if (r == -EAGAIN)
2003 continue;
2004 if (r < 0)
2005 return r;
2006
2007 add_known_answers = r;
2008 break;
2009 }
2010
2011 /* Then, create actual packet. */
2012 r = dns_packet_new_query(&p, t->scope->protocol, 0, false);
2013 if (r < 0)
2014 return r;
2015
2016 /* Questions */
2017 c = 0;
2018 SET_FOREACH(k, keys) {
2019 r = dns_packet_append_key(p, k, 0, NULL);
2020 if (r < 0)
2021 return r;
2022 c++;
2023 }
2024 DNS_PACKET_HEADER(p)->qdcount = htobe16(c);
2025
2026 /* Known answers */
2027 if (add_known_answers) {
2028 usec_t ts;
2029
2030 assert_se(sd_event_now(t->scope->manager->event, CLOCK_BOOTTIME, &ts) >= 0);
2031
2032 r = dns_cache_export_shared_to_packet(&t->scope->cache, p, ts, be16toh(DNS_PACKET_HEADER(dummy)->ancount));
2033 if (r < 0)
2034 return r;
2035 }
2036
2037 /* Authorities */
2038 c = 0;
2039 SET_FOREACH(k, keys) {
2040 r = dns_packet_append_zone(p, t, k, &c);
2041 if (r < 0)
2042 return r;
2043 }
2044 DNS_PACKET_HEADER(p)->nscount = htobe16(c);
2045
2046 t->sent = TAKE_PTR(p);
2047 return 0;
2048 }
2049
2050 static int dns_transaction_make_packet(DnsTransaction *t) {
2051 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
2052 int r;
2053
2054 assert(t);
2055
2056 if (t->scope->protocol == DNS_PROTOCOL_MDNS)
2057 return dns_transaction_make_packet_mdns(t);
2058
2059 if (t->sent)
2060 return 0;
2061
2062 if (t->bypass && t->bypass->protocol == t->scope->protocol) {
2063 /* If bypass logic is enabled and the protocol if the original packet and our scope match,
2064 * take the original packet, copy it, and patch in our new ID */
2065 r = dns_packet_dup(&p, t->bypass);
2066 if (r < 0)
2067 return r;
2068 } else {
2069 r = dns_packet_new_query(
2070 &p, t->scope->protocol,
2071 /* min_alloc_dsize = */ 0,
2072 /* dnssec_cd = */ !FLAGS_SET(t->query_flags, SD_RESOLVED_NO_VALIDATE) &&
2073 t->scope->dnssec_mode != DNSSEC_NO);
2074 if (r < 0)
2075 return r;
2076
2077 r = dns_packet_append_key(p, dns_transaction_key(t), 0, NULL);
2078 if (r < 0)
2079 return r;
2080
2081 DNS_PACKET_HEADER(p)->qdcount = htobe16(1);
2082 }
2083
2084 DNS_PACKET_HEADER(p)->id = t->id;
2085
2086 t->sent = TAKE_PTR(p);
2087 return 0;
2088 }
2089
2090 int dns_transaction_go(DnsTransaction *t) {
2091 usec_t ts;
2092 int r;
2093 char key_str[DNS_RESOURCE_KEY_STRING_MAX];
2094
2095 assert(t);
2096
2097 /* Returns > 0 if the transaction is now pending, returns 0 if could be processed immediately and has
2098 * finished now. In the latter case, the transaction and query candidate objects must not be accessed.
2099 */
2100
2101 assert_se(sd_event_now(t->scope->manager->event, CLOCK_BOOTTIME, &ts) >= 0);
2102
2103 r = dns_transaction_prepare(t, ts);
2104 if (r <= 0)
2105 return r;
2106
2107 log_debug("Firing %s transaction %" PRIu16 " for <%s> scope %s on %s/%s (validate=%s).",
2108 t->bypass ? "bypass" : "regular",
2109 t->id,
2110 dns_resource_key_to_string(dns_transaction_key(t), key_str, sizeof key_str),
2111 dns_protocol_to_string(t->scope->protocol),
2112 t->scope->link ? t->scope->link->ifname : "*",
2113 af_to_name_short(t->scope->family),
2114 yes_no(!FLAGS_SET(t->query_flags, SD_RESOLVED_NO_VALIDATE)));
2115
2116 if (!t->initial_jitter_scheduled &&
2117 IN_SET(t->scope->protocol, DNS_PROTOCOL_LLMNR, DNS_PROTOCOL_MDNS)) {
2118 usec_t jitter;
2119
2120 /* RFC 4795 Section 2.7 suggests all LLMNR queries should be delayed by a random time from 0 to
2121 * JITTER_INTERVAL.
2122 * RFC 6762 Section 8.1 suggests initial probe queries should be delayed by a random time from
2123 * 0 to 250ms. */
2124
2125 t->initial_jitter_scheduled = true;
2126 t->n_attempts = 0;
2127
2128 switch (t->scope->protocol) {
2129
2130 case DNS_PROTOCOL_LLMNR:
2131 jitter = random_u64_range(LLMNR_JITTER_INTERVAL_USEC);
2132 break;
2133
2134 case DNS_PROTOCOL_MDNS:
2135 if (t->probing)
2136 jitter = random_u64_range(MDNS_PROBING_INTERVAL_USEC);
2137 else
2138 jitter = 0;
2139 break;
2140 default:
2141 assert_not_reached();
2142 }
2143
2144 r = dns_transaction_setup_timeout(t, jitter, ts);
2145 if (r < 0)
2146 return r;
2147
2148 log_debug("Delaying %s transaction %" PRIu16 " for " USEC_FMT "us.",
2149 dns_protocol_to_string(t->scope->protocol),
2150 t->id,
2151 jitter);
2152 return 1;
2153 }
2154
2155 /* Otherwise, we need to ask the network */
2156 r = dns_transaction_make_packet(t);
2157 if (r < 0)
2158 return r;
2159
2160 if (t->scope->protocol == DNS_PROTOCOL_LLMNR &&
2161 (dns_name_endswith(dns_resource_key_name(dns_transaction_key(t)), "in-addr.arpa") > 0 ||
2162 dns_name_endswith(dns_resource_key_name(dns_transaction_key(t)), "ip6.arpa") > 0)) {
2163
2164 /* RFC 4795, Section 2.4. says reverse lookups shall
2165 * always be made via TCP on LLMNR */
2166 r = dns_transaction_emit_tcp(t);
2167 } else {
2168 /* Try via UDP, and if that fails due to large size or lack of
2169 * support try via TCP */
2170 r = dns_transaction_emit_udp(t);
2171 if (r == -EMSGSIZE)
2172 log_debug("Sending query via TCP since it is too large.");
2173 else if (r == -EAGAIN)
2174 log_debug("Sending query via TCP since UDP isn't supported or DNS-over-TLS is selected.");
2175 else if (r == -EPERM)
2176 log_debug("Sending query via TCP since UDP is blocked.");
2177 if (IN_SET(r, -EMSGSIZE, -EAGAIN, -EPERM))
2178 r = dns_transaction_emit_tcp(t);
2179 }
2180 if (r == -ELOOP) {
2181 if (t->scope->protocol != DNS_PROTOCOL_DNS)
2182 return r;
2183
2184 /* One of our own stub listeners */
2185 log_debug_errno(r, "Detected that specified DNS server is our own extra listener, switching DNS servers.");
2186
2187 dns_scope_next_dns_server(t->scope, t->server);
2188
2189 if (dns_scope_get_dns_server(t->scope) == t->server) {
2190 log_debug_errno(r, "Still pointing to extra listener after switching DNS servers, refusing operation.");
2191 dns_transaction_complete(t, DNS_TRANSACTION_STUB_LOOP);
2192 return 0;
2193 }
2194
2195 return dns_transaction_go(t);
2196 }
2197 if (r == -ESRCH) {
2198 /* No servers to send this to? */
2199 dns_transaction_complete(t, DNS_TRANSACTION_NO_SERVERS);
2200 return 0;
2201 }
2202 if (r == -EOPNOTSUPP) {
2203 /* Tried to ask for DNSSEC RRs, on a server that doesn't do DNSSEC */
2204 dns_transaction_complete(t, DNS_TRANSACTION_RR_TYPE_UNSUPPORTED);
2205 return 0;
2206 }
2207 if (t->scope->protocol == DNS_PROTOCOL_LLMNR && ERRNO_IS_NEG_DISCONNECT(r)) {
2208 /* On LLMNR, if we cannot connect to a host via TCP when doing reverse lookups. This means we cannot
2209 * answer this request with this protocol. */
2210 dns_transaction_complete(t, DNS_TRANSACTION_NOT_FOUND);
2211 return 0;
2212 }
2213 if (r < 0) {
2214 if (t->scope->protocol != DNS_PROTOCOL_DNS)
2215 return r;
2216
2217 /* Couldn't send? Try immediately again, with a new server */
2218 dns_scope_next_dns_server(t->scope, t->server);
2219
2220 return dns_transaction_go(t);
2221 }
2222
2223 usec_t timeout = transaction_get_resend_timeout(t);
2224 r = dns_transaction_setup_timeout(t, timeout, usec_add(ts, timeout));
2225 if (r < 0)
2226 return r;
2227
2228 return 1;
2229 }
2230
2231 static int dns_transaction_find_cyclic(DnsTransaction *t, DnsTransaction *aux) {
2232 DnsTransaction *n;
2233 int r;
2234
2235 assert(t);
2236 assert(aux);
2237
2238 /* Try to find cyclic dependencies between transaction objects */
2239
2240 if (t == aux)
2241 return 1;
2242
2243 SET_FOREACH(n, aux->dnssec_transactions) {
2244 r = dns_transaction_find_cyclic(t, n);
2245 if (r != 0)
2246 return r;
2247 }
2248
2249 return 0;
2250 }
2251
2252 static int dns_transaction_add_dnssec_transaction(DnsTransaction *t, DnsResourceKey *key, DnsTransaction **ret) {
2253 _cleanup_(dns_transaction_gcp) DnsTransaction *aux = NULL;
2254 int r;
2255
2256 assert(t);
2257 assert(ret);
2258 assert(key);
2259
2260 aux = dns_scope_find_transaction(t->scope, key, t->query_flags);
2261 if (!aux) {
2262 r = dns_transaction_new(&aux, t->scope, key, NULL, t->query_flags);
2263 if (r < 0)
2264 return r;
2265 } else {
2266 if (set_contains(t->dnssec_transactions, aux)) {
2267 *ret = aux;
2268 return 0;
2269 }
2270
2271 r = dns_transaction_find_cyclic(t, aux);
2272 if (r < 0)
2273 return r;
2274 if (r > 0) {
2275 char s[DNS_RESOURCE_KEY_STRING_MAX], saux[DNS_RESOURCE_KEY_STRING_MAX];
2276
2277 return log_debug_errno(SYNTHETIC_ERRNO(ELOOP),
2278 "Potential cyclic dependency, refusing to add transaction %" PRIu16 " (%s) as dependency for %" PRIu16 " (%s).",
2279 aux->id,
2280 dns_resource_key_to_string(dns_transaction_key(t), s, sizeof s),
2281 t->id,
2282 dns_resource_key_to_string(dns_transaction_key(aux), saux, sizeof saux));
2283 }
2284 }
2285
2286 r = set_ensure_allocated(&aux->notify_transactions_done, NULL);
2287 if (r < 0)
2288 return r;
2289
2290 r = set_ensure_put(&t->dnssec_transactions, NULL, aux);
2291 if (r < 0)
2292 return r;
2293
2294 r = set_ensure_put(&aux->notify_transactions, NULL, t);
2295 if (r < 0) {
2296 (void) set_remove(t->dnssec_transactions, aux);
2297 return r;
2298 }
2299
2300 *ret = TAKE_PTR(aux);
2301 return 1;
2302 }
2303
2304 static int dns_transaction_request_dnssec_rr(DnsTransaction *t, DnsResourceKey *key) {
2305 _cleanup_(dns_answer_unrefp) DnsAnswer *a = NULL;
2306 DnsTransaction *aux;
2307 int r;
2308
2309 assert(t);
2310 assert(key);
2311
2312 /* Try to get the data from the trust anchor */
2313 r = dns_trust_anchor_lookup_positive(&t->scope->manager->trust_anchor, key, &a);
2314 if (r < 0)
2315 return r;
2316 if (r > 0) {
2317 r = dns_answer_extend(&t->validated_keys, a);
2318 if (r < 0)
2319 return r;
2320
2321 return 0;
2322 }
2323
2324 /* This didn't work, ask for it via the network/cache then. */
2325 r = dns_transaction_add_dnssec_transaction(t, key, &aux);
2326 if (r == -ELOOP) /* This would result in a cyclic dependency */
2327 return 0;
2328 if (r < 0)
2329 return r;
2330
2331 if (aux->state == DNS_TRANSACTION_NULL) {
2332 r = dns_transaction_go(aux);
2333 if (r < 0)
2334 return r;
2335 }
2336
2337 return 1;
2338 }
2339
2340 static int dns_transaction_negative_trust_anchor_lookup(DnsTransaction *t, const char *name) {
2341 int r;
2342
2343 assert(t);
2344
2345 /* Check whether the specified name is in the NTA
2346 * database, either in the global one, or the link-local
2347 * one. */
2348
2349 r = dns_trust_anchor_lookup_negative(&t->scope->manager->trust_anchor, name);
2350 if (r != 0)
2351 return r;
2352
2353 if (!t->scope->link)
2354 return 0;
2355
2356 return link_negative_trust_anchor_lookup(t->scope->link, name);
2357 }
2358
2359 static int dns_transaction_has_negative_answer(DnsTransaction *t) {
2360 int r;
2361
2362 assert(t);
2363
2364 /* Checks whether the answer is negative, and lacks NSEC/NSEC3
2365 * RRs to prove it */
2366
2367 r = dns_transaction_has_positive_answer(t, NULL);
2368 if (r < 0)
2369 return r;
2370 if (r > 0)
2371 return false;
2372
2373 /* Is this key explicitly listed as a negative trust anchor?
2374 * If so, it's nothing we need to care about */
2375 r = dns_transaction_negative_trust_anchor_lookup(t, dns_resource_key_name(dns_transaction_key(t)));
2376 if (r < 0)
2377 return r;
2378 return !r;
2379 }
2380
2381 static int dns_transaction_is_primary_response(DnsTransaction *t, DnsResourceRecord *rr) {
2382 int r;
2383
2384 assert(t);
2385 assert(rr);
2386
2387 /* Check if the specified RR is the "primary" response,
2388 * i.e. either matches the question precisely or is a
2389 * CNAME/DNAME for it. */
2390
2391 r = dns_resource_key_match_rr(dns_transaction_key(t), rr, NULL);
2392 if (r != 0)
2393 return r;
2394
2395 return dns_resource_key_match_cname_or_dname(dns_transaction_key(t), rr->key, NULL);
2396 }
2397
2398 static bool dns_transaction_dnssec_supported(DnsTransaction *t) {
2399 assert(t);
2400
2401 /* Checks whether our transaction's DNS server is assumed to be compatible with DNSSEC. Returns false as soon
2402 * as we changed our mind about a server, and now believe it is incompatible with DNSSEC. */
2403
2404 if (t->scope->protocol != DNS_PROTOCOL_DNS)
2405 return false;
2406
2407 /* If we have picked no server, then we are working from the cache or some other source, and DNSSEC might well
2408 * be supported, hence return true. */
2409 if (!t->server)
2410 return true;
2411
2412 /* Note that we do not check the feature level actually used for the transaction but instead the feature level
2413 * the server is known to support currently, as the transaction feature level might be lower than what the
2414 * server actually supports, since we might have downgraded this transaction's feature level because we got a
2415 * SERVFAIL earlier and wanted to check whether downgrading fixes it. */
2416
2417 return dns_server_dnssec_supported(t->server);
2418 }
2419
2420 static bool dns_transaction_dnssec_supported_full(DnsTransaction *t) {
2421 DnsTransaction *dt;
2422
2423 assert(t);
2424
2425 /* Checks whether our transaction our any of the auxiliary transactions couldn't do DNSSEC. */
2426
2427 if (!dns_transaction_dnssec_supported(t))
2428 return false;
2429
2430 SET_FOREACH(dt, t->dnssec_transactions)
2431 if (!dns_transaction_dnssec_supported(dt))
2432 return false;
2433
2434 return true;
2435 }
2436
2437 int dns_transaction_request_dnssec_keys(DnsTransaction *t) {
2438 DnsResourceRecord *rr;
2439
2440 int r;
2441
2442 assert(t);
2443
2444 /*
2445 * Retrieve all auxiliary RRs for the answer we got, so that
2446 * we can verify signatures or prove that RRs are rightfully
2447 * unsigned. Specifically:
2448 *
2449 * - For RRSIG we get the matching DNSKEY
2450 * - For DNSKEY we get the matching DS
2451 * - For unsigned SOA/NS we get the matching DS
2452 * - For unsigned CNAME/DNAME/DS we get the parent SOA RR
2453 * - For other unsigned RRs we get the matching SOA RR
2454 * - For SOA/NS queries with no matching response RR, and no NSEC/NSEC3, the DS RR
2455 * - For DS queries with no matching response RRs, and no NSEC/NSEC3, the parent's SOA RR
2456 * - For other queries with no matching response RRs, and no NSEC/NSEC3, the SOA RR
2457 */
2458
2459 if (FLAGS_SET(t->query_flags, SD_RESOLVED_NO_VALIDATE) || t->scope->dnssec_mode == DNSSEC_NO)
2460 return 0;
2461 if (t->answer_source != DNS_TRANSACTION_NETWORK)
2462 return 0; /* We only need to validate stuff from the network */
2463 if (!dns_transaction_dnssec_supported(t))
2464 return 0; /* If we can't do DNSSEC anyway there's no point in getting the auxiliary RRs */
2465
2466 DNS_ANSWER_FOREACH(rr, t->answer) {
2467
2468 if (dns_type_is_pseudo(rr->key->type))
2469 continue;
2470
2471 /* If this RR is in the negative trust anchor, we don't need to validate it. */
2472 r = dns_transaction_negative_trust_anchor_lookup(t, dns_resource_key_name(rr->key));
2473 if (r < 0)
2474 return r;
2475 if (r > 0)
2476 continue;
2477
2478 switch (rr->key->type) {
2479
2480 case DNS_TYPE_RRSIG: {
2481 /* For each RRSIG we request the matching DNSKEY */
2482 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *dnskey = NULL;
2483
2484 /* If this RRSIG is about a DNSKEY RR and the
2485 * signer is the same as the owner, then we
2486 * already have the DNSKEY, and we don't have
2487 * to look for more. */
2488 if (rr->rrsig.type_covered == DNS_TYPE_DNSKEY) {
2489 r = dns_name_equal(rr->rrsig.signer, dns_resource_key_name(rr->key));
2490 if (r < 0)
2491 return r;
2492 if (r > 0)
2493 continue;
2494 }
2495
2496 /* If the signer is not a parent of our
2497 * original query, then this is about an
2498 * auxiliary RRset, but not anything we asked
2499 * for. In this case we aren't interested,
2500 * because we don't want to request additional
2501 * RRs for stuff we didn't really ask for, and
2502 * also to avoid request loops, where
2503 * additional RRs from one transaction result
2504 * in another transaction whose additional RRs
2505 * point back to the original transaction, and
2506 * we deadlock. */
2507 r = dns_name_endswith(dns_resource_key_name(dns_transaction_key(t)), rr->rrsig.signer);
2508 if (r < 0)
2509 return r;
2510 if (r == 0)
2511 continue;
2512
2513 dnskey = dns_resource_key_new(rr->key->class, DNS_TYPE_DNSKEY, rr->rrsig.signer);
2514 if (!dnskey)
2515 return -ENOMEM;
2516
2517 log_debug("Requesting DNSKEY to validate transaction %" PRIu16" (%s, RRSIG with key tag: %" PRIu16 ").",
2518 t->id, dns_resource_key_name(rr->key), rr->rrsig.key_tag);
2519 r = dns_transaction_request_dnssec_rr(t, dnskey);
2520 if (r < 0)
2521 return r;
2522 break;
2523 }
2524
2525 case DNS_TYPE_DNSKEY: {
2526 /* For each DNSKEY we request the matching DS */
2527 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *ds = NULL;
2528
2529 /* If the DNSKEY we are looking at is not for
2530 * zone we are interested in, nor any of its
2531 * parents, we aren't interested, and don't
2532 * request it. After all, we don't want to end
2533 * up in request loops, and want to keep
2534 * additional traffic down. */
2535
2536 r = dns_name_endswith(dns_resource_key_name(dns_transaction_key(t)), dns_resource_key_name(rr->key));
2537 if (r < 0)
2538 return r;
2539 if (r == 0)
2540 continue;
2541
2542 ds = dns_resource_key_new(rr->key->class, DNS_TYPE_DS, dns_resource_key_name(rr->key));
2543 if (!ds)
2544 return -ENOMEM;
2545
2546 log_debug("Requesting DS to validate transaction %" PRIu16" (%s, DNSKEY with key tag: %" PRIu16 ").",
2547 t->id, dns_resource_key_name(rr->key), dnssec_keytag(rr, false));
2548 r = dns_transaction_request_dnssec_rr(t, ds);
2549 if (r < 0)
2550 return r;
2551
2552 break;
2553 }
2554
2555 case DNS_TYPE_SOA:
2556 case DNS_TYPE_NS: {
2557 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *ds = NULL;
2558
2559 /* For an unsigned SOA or NS, try to acquire
2560 * the matching DS RR, as we are at a zone cut
2561 * then, and whether a DS exists tells us
2562 * whether the zone is signed. Do so only if
2563 * this RR matches our original question,
2564 * however. */
2565
2566 r = dns_resource_key_match_rr(dns_transaction_key(t), rr, NULL);
2567 if (r < 0)
2568 return r;
2569 if (r == 0) {
2570 /* Hmm, so this SOA RR doesn't match our original question. In this case, maybe this is
2571 * a negative reply, and we need the SOA RR's TTL in order to cache a negative entry?
2572 * If so, we need to validate it, too. */
2573
2574 r = dns_answer_match_key(t->answer, dns_transaction_key(t), NULL);
2575 if (r < 0)
2576 return r;
2577 if (r > 0) /* positive reply, we won't need the SOA and hence don't need to validate
2578 * it. */
2579 continue;
2580
2581 /* Only bother with this if the SOA/NS RR we are looking at is actually a parent of
2582 * what we are looking for, otherwise there's no value in it for us. */
2583 r = dns_name_endswith(dns_resource_key_name(dns_transaction_key(t)), dns_resource_key_name(rr->key));
2584 if (r < 0)
2585 return r;
2586 if (r == 0)
2587 continue;
2588 }
2589
2590 r = dnssec_has_rrsig(t->answer, rr->key);
2591 if (r < 0)
2592 return r;
2593 if (r > 0)
2594 continue;
2595
2596 ds = dns_resource_key_new(rr->key->class, DNS_TYPE_DS, dns_resource_key_name(rr->key));
2597 if (!ds)
2598 return -ENOMEM;
2599
2600 log_debug("Requesting DS to validate transaction %" PRIu16 " (%s, unsigned SOA/NS RRset).",
2601 t->id, dns_resource_key_name(rr->key));
2602 r = dns_transaction_request_dnssec_rr(t, ds);
2603 if (r < 0)
2604 return r;
2605
2606 break;
2607 }
2608
2609 case DNS_TYPE_DS:
2610 case DNS_TYPE_CNAME:
2611 case DNS_TYPE_DNAME: {
2612 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *soa = NULL;
2613 const char *name;
2614
2615 /* CNAMEs and DNAMEs cannot be located at a
2616 * zone apex, hence ask for the parent SOA for
2617 * unsigned CNAME/DNAME RRs, maybe that's the
2618 * apex. But do all that only if this is
2619 * actually a response to our original
2620 * question.
2621 *
2622 * Similar for DS RRs, which are signed when
2623 * the parent SOA is signed. */
2624
2625 r = dns_transaction_is_primary_response(t, rr);
2626 if (r < 0)
2627 return r;
2628 if (r == 0)
2629 continue;
2630
2631 r = dnssec_has_rrsig(t->answer, rr->key);
2632 if (r < 0)
2633 return r;
2634 if (r > 0)
2635 continue;
2636
2637 r = dns_answer_has_dname_for_cname(t->answer, rr);
2638 if (r < 0)
2639 return r;
2640 if (r > 0)
2641 continue;
2642
2643 name = dns_resource_key_name(rr->key);
2644 r = dns_name_parent(&name);
2645 if (r < 0)
2646 return r;
2647 if (r == 0)
2648 continue;
2649
2650 soa = dns_resource_key_new(rr->key->class, DNS_TYPE_SOA, name);
2651 if (!soa)
2652 return -ENOMEM;
2653
2654 log_debug("Requesting parent SOA to validate transaction %" PRIu16 " (%s, unsigned CNAME/DNAME/DS RRset).",
2655 t->id, dns_resource_key_name(rr->key));
2656 r = dns_transaction_request_dnssec_rr(t, soa);
2657 if (r < 0)
2658 return r;
2659
2660 break;
2661 }
2662
2663 default: {
2664 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *soa = NULL;
2665
2666 /* For other unsigned RRsets (including
2667 * NSEC/NSEC3!), look for proof the zone is
2668 * unsigned, by requesting the SOA RR of the
2669 * zone. However, do so only if they are
2670 * directly relevant to our original
2671 * question. */
2672
2673 r = dns_transaction_is_primary_response(t, rr);
2674 if (r < 0)
2675 return r;
2676 if (r == 0)
2677 continue;
2678
2679 r = dnssec_has_rrsig(t->answer, rr->key);
2680 if (r < 0)
2681 return r;
2682 if (r > 0)
2683 continue;
2684
2685 soa = dns_resource_key_new(rr->key->class, DNS_TYPE_SOA, dns_resource_key_name(rr->key));
2686 if (!soa)
2687 return -ENOMEM;
2688
2689 log_debug("Requesting SOA to validate transaction %" PRIu16 " (%s, unsigned non-SOA/NS RRset <%s>).",
2690 t->id, dns_resource_key_name(rr->key), dns_resource_record_to_string(rr));
2691 r = dns_transaction_request_dnssec_rr(t, soa);
2692 if (r < 0)
2693 return r;
2694 break;
2695 }}
2696 }
2697
2698 /* Above, we requested everything necessary to validate what
2699 * we got. Now, let's request what we need to validate what we
2700 * didn't get... */
2701
2702 r = dns_transaction_has_negative_answer(t);
2703 if (r < 0)
2704 return r;
2705 if (r > 0) {
2706 const char *name, *signed_status;
2707 uint16_t type = 0;
2708
2709 name = dns_resource_key_name(dns_transaction_key(t));
2710 signed_status = dns_answer_contains_nsec_or_nsec3(t->answer) ? "signed" : "unsigned";
2711
2712 /* If this was a SOA or NS request, then check if there's a DS RR for the same domain. Note that this
2713 * could also be used as indication that we are not at a zone apex, but in real world setups there are
2714 * too many broken DNS servers (Hello, incapdns.net!) where non-terminal zones return NXDOMAIN even
2715 * though they have further children. If this was a DS request, then it's signed when the parent zone
2716 * is signed, hence ask the parent SOA in that case. If this was any other RR then ask for the SOA RR,
2717 * to see if that is signed. */
2718
2719 if (dns_transaction_key(t)->type == DNS_TYPE_DS) {
2720 r = dns_name_parent(&name);
2721 if (r > 0) {
2722 type = DNS_TYPE_SOA;
2723 log_debug("Requesting parent SOA (%s %s) to validate transaction %" PRIu16 " (%s, %s empty DS response).",
2724 special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), name, t->id,
2725 dns_resource_key_name(dns_transaction_key(t)), signed_status);
2726 } else
2727 name = NULL;
2728
2729 } else if (IN_SET(dns_transaction_key(t)->type, DNS_TYPE_SOA, DNS_TYPE_NS)) {
2730
2731 type = DNS_TYPE_DS;
2732 log_debug("Requesting DS (%s %s) to validate transaction %" PRIu16 " (%s, %s empty SOA/NS response).",
2733 special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), name, t->id, name, signed_status);
2734
2735 } else {
2736 type = DNS_TYPE_SOA;
2737 log_debug("Requesting SOA (%s %s) to validate transaction %" PRIu16 " (%s, %s empty non-SOA/NS/DS response).",
2738 special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), name, t->id, name, signed_status);
2739 }
2740
2741 if (name) {
2742 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *soa = NULL;
2743
2744 soa = dns_resource_key_new(dns_transaction_key(t)->class, type, name);
2745 if (!soa)
2746 return -ENOMEM;
2747
2748 r = dns_transaction_request_dnssec_rr(t, soa);
2749 if (r < 0)
2750 return r;
2751 }
2752 }
2753
2754 return dns_transaction_dnssec_is_live(t);
2755 }
2756
2757 void dns_transaction_notify(DnsTransaction *t, DnsTransaction *source) {
2758 assert(t);
2759 assert(source);
2760
2761 /* Invoked whenever any of our auxiliary DNSSEC transactions completed its work. If the state is still PENDING,
2762 we are still in the loop that adds further DNSSEC transactions, hence don't check if we are ready yet. If
2763 the state is VALIDATING however, we should check if we are complete now. */
2764
2765 if (t->state == DNS_TRANSACTION_VALIDATING)
2766 dns_transaction_process_dnssec(t);
2767 }
2768
2769 static int dns_transaction_validate_dnskey_by_ds(DnsTransaction *t) {
2770 DnsAnswerItem *item;
2771 int r;
2772
2773 assert(t);
2774
2775 /* Add all DNSKEY RRs from the answer that are validated by DS
2776 * RRs from the list of validated keys to the list of
2777 * validated keys. */
2778
2779 DNS_ANSWER_FOREACH_ITEM(item, t->answer) {
2780
2781 r = dnssec_verify_dnskey_by_ds_search(item->rr, t->validated_keys);
2782 if (r < 0)
2783 return r;
2784 if (r == 0)
2785 continue;
2786
2787 /* If so, the DNSKEY is validated too. */
2788 r = dns_answer_add_extend(&t->validated_keys, item->rr, item->ifindex, item->flags|DNS_ANSWER_AUTHENTICATED, item->rrsig);
2789 if (r < 0)
2790 return r;
2791 }
2792
2793 return 0;
2794 }
2795
2796 static int dns_transaction_requires_rrsig(DnsTransaction *t, DnsResourceRecord *rr) {
2797 int r;
2798
2799 assert(t);
2800 assert(rr);
2801
2802 /* Checks if the RR we are looking for must be signed with an
2803 * RRSIG. This is used for positive responses. */
2804
2805 if (t->scope->dnssec_mode == DNSSEC_NO)
2806 return false;
2807
2808 if (dns_type_is_pseudo(rr->key->type))
2809 return -EINVAL;
2810
2811 r = dns_transaction_negative_trust_anchor_lookup(t, dns_resource_key_name(rr->key));
2812 if (r < 0)
2813 return r;
2814 if (r > 0)
2815 return false;
2816
2817 switch (rr->key->type) {
2818
2819 case DNS_TYPE_RRSIG:
2820 /* RRSIGs are the signatures themselves, they need no signing. */
2821 return false;
2822
2823 case DNS_TYPE_SOA:
2824 case DNS_TYPE_NS: {
2825 DnsTransaction *dt;
2826
2827 /* For SOA or NS RRs we look for a matching DS transaction */
2828
2829 SET_FOREACH(dt, t->dnssec_transactions) {
2830
2831 if (dns_transaction_key(dt)->class != rr->key->class)
2832 continue;
2833 if (dns_transaction_key(dt)->type != DNS_TYPE_DS)
2834 continue;
2835
2836 r = dns_name_equal(dns_resource_key_name(dns_transaction_key(dt)), dns_resource_key_name(rr->key));
2837 if (r < 0)
2838 return r;
2839 if (r == 0)
2840 continue;
2841
2842 /* We found a DS transactions for the SOA/NS
2843 * RRs we are looking at. If it discovered signed DS
2844 * RRs, then we need to be signed, too. */
2845
2846 if (!FLAGS_SET(dt->answer_query_flags, SD_RESOLVED_AUTHENTICATED))
2847 return false;
2848
2849 return dns_answer_match_key(dt->answer, dns_transaction_key(dt), NULL);
2850 }
2851
2852 /* We found nothing that proves this is safe to leave
2853 * this unauthenticated, hence ask inist on
2854 * authentication. */
2855 return true;
2856 }
2857
2858 case DNS_TYPE_DS:
2859 case DNS_TYPE_CNAME:
2860 case DNS_TYPE_DNAME: {
2861 const char *parent = NULL;
2862 DnsTransaction *dt;
2863
2864 /*
2865 * CNAME/DNAME RRs cannot be located at a zone apex, hence look directly for the parent SOA.
2866 *
2867 * DS RRs are signed if the parent is signed, hence also look at the parent SOA
2868 */
2869
2870 SET_FOREACH(dt, t->dnssec_transactions) {
2871
2872 if (dns_transaction_key(dt)->class != rr->key->class)
2873 continue;
2874 if (dns_transaction_key(dt)->type != DNS_TYPE_SOA)
2875 continue;
2876
2877 if (!parent) {
2878 parent = dns_resource_key_name(rr->key);
2879 r = dns_name_parent(&parent);
2880 if (r < 0)
2881 return r;
2882 if (r == 0) {
2883 if (rr->key->type == DNS_TYPE_DS)
2884 return true;
2885
2886 /* A CNAME/DNAME without a parent? That's sooo weird. */
2887 return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG),
2888 "Transaction %" PRIu16 " claims CNAME/DNAME at root. Refusing.", t->id);
2889 }
2890 }
2891
2892 r = dns_name_equal(dns_resource_key_name(dns_transaction_key(dt)), parent);
2893 if (r < 0)
2894 return r;
2895 if (r == 0)
2896 continue;
2897
2898 return FLAGS_SET(dt->answer_query_flags, SD_RESOLVED_AUTHENTICATED);
2899 }
2900
2901 return true;
2902 }
2903
2904 default: {
2905 DnsTransaction *dt;
2906
2907 /* Any other kind of RR (including DNSKEY/NSEC/NSEC3). Let's see if our SOA lookup was authenticated */
2908
2909 SET_FOREACH(dt, t->dnssec_transactions) {
2910
2911 if (dns_transaction_key(dt)->class != rr->key->class)
2912 continue;
2913 if (dns_transaction_key(dt)->type != DNS_TYPE_SOA)
2914 continue;
2915
2916 r = dns_name_equal(dns_resource_key_name(dns_transaction_key(dt)), dns_resource_key_name(rr->key));
2917 if (r < 0)
2918 return r;
2919 if (r == 0)
2920 continue;
2921
2922 /* We found the transaction that was supposed to find the SOA RR for us. It was
2923 * successful, but found no RR for us. This means we are not at a zone cut. In this
2924 * case, we require authentication if the SOA lookup was authenticated too. */
2925 return FLAGS_SET(dt->answer_query_flags, SD_RESOLVED_AUTHENTICATED);
2926 }
2927
2928 return true;
2929 }}
2930 }
2931
2932 static int dns_transaction_in_private_tld(DnsTransaction *t, const DnsResourceKey *key) {
2933 DnsTransaction *dt;
2934 const char *tld;
2935 int r;
2936
2937 /* If DNSSEC downgrade mode is on, checks whether the
2938 * specified RR is one level below a TLD we have proven not to
2939 * exist. In such a case we assume that this is a private
2940 * domain, and permit it.
2941 *
2942 * This detects cases like the Fritz!Box router networks. Each
2943 * Fritz!Box router serves a private "fritz.box" zone, in the
2944 * non-existing TLD "box". Requests for the "fritz.box" domain
2945 * are served by the router itself, while requests for the
2946 * "box" domain will result in NXDOMAIN.
2947 *
2948 * Note that this logic is unable to detect cases where a
2949 * router serves a private DNS zone directly under
2950 * non-existing TLD. In such a case we cannot detect whether
2951 * the TLD is supposed to exist or not, as all requests we
2952 * make for it will be answered by the router's zone, and not
2953 * by the root zone. */
2954
2955 assert(t);
2956
2957 if (t->scope->dnssec_mode != DNSSEC_ALLOW_DOWNGRADE)
2958 return false; /* In strict DNSSEC mode what doesn't exist, doesn't exist */
2959
2960 tld = dns_resource_key_name(key);
2961 r = dns_name_parent(&tld);
2962 if (r < 0)
2963 return r;
2964 if (r == 0)
2965 return false; /* Already the root domain */
2966
2967 if (!dns_name_is_single_label(tld))
2968 return false;
2969
2970 SET_FOREACH(dt, t->dnssec_transactions) {
2971
2972 if (dns_transaction_key(dt)->class != key->class)
2973 continue;
2974
2975 r = dns_name_equal(dns_resource_key_name(dns_transaction_key(dt)), tld);
2976 if (r < 0)
2977 return r;
2978 if (r == 0)
2979 continue;
2980
2981 /* We found an auxiliary lookup we did for the TLD. If
2982 * that returned with NXDOMAIN, we know the TLD didn't
2983 * exist, and hence this might be a private zone. */
2984
2985 return dt->answer_rcode == DNS_RCODE_NXDOMAIN;
2986 }
2987
2988 return false;
2989 }
2990
2991 static int dns_transaction_requires_nsec(DnsTransaction *t) {
2992 char key_str[DNS_RESOURCE_KEY_STRING_MAX];
2993 DnsTransaction *dt;
2994 const char *name;
2995 uint16_t type = 0;
2996 int r;
2997
2998 assert(t);
2999
3000 /* Checks if we need to insist on NSEC/NSEC3 RRs for proving
3001 * this negative reply */
3002
3003 if (t->scope->dnssec_mode == DNSSEC_NO)
3004 return false;
3005
3006 if (dns_type_is_pseudo(dns_transaction_key(t)->type))
3007 return -EINVAL;
3008
3009 r = dns_transaction_negative_trust_anchor_lookup(t, dns_resource_key_name(dns_transaction_key(t)));
3010 if (r < 0)
3011 return r;
3012 if (r > 0)
3013 return false;
3014
3015 r = dns_transaction_in_private_tld(t, dns_transaction_key(t));
3016 if (r < 0)
3017 return r;
3018 if (r > 0) {
3019 /* The lookup is from a TLD that is proven not to
3020 * exist, and we are in downgrade mode, hence ignore
3021 * that fact that we didn't get any NSEC RRs. */
3022
3023 log_info("Detected a negative query %s in a private DNS zone, permitting unsigned response.",
3024 dns_resource_key_to_string(dns_transaction_key(t), key_str, sizeof key_str));
3025 return false;
3026 }
3027
3028 name = dns_resource_key_name(dns_transaction_key(t));
3029
3030 if (dns_transaction_key(t)->type == DNS_TYPE_DS) {
3031
3032 /* We got a negative reply for this DS lookup? DS RRs are signed when their parent zone is signed,
3033 * hence check the parent SOA in this case. */
3034
3035 r = dns_name_parent(&name);
3036 if (r < 0)
3037 return r;
3038 if (r == 0)
3039 return true;
3040
3041 type = DNS_TYPE_SOA;
3042
3043 } else if (IN_SET(dns_transaction_key(t)->type, DNS_TYPE_SOA, DNS_TYPE_NS))
3044 /* We got a negative reply for this SOA/NS lookup? If so, check if there's a DS RR for this */
3045 type = DNS_TYPE_DS;
3046 else
3047 /* For all other negative replies, check for the SOA lookup */
3048 type = DNS_TYPE_SOA;
3049
3050 /* For all other RRs we check the SOA on the same level to see
3051 * if it's signed. */
3052
3053 SET_FOREACH(dt, t->dnssec_transactions) {
3054
3055 if (dns_transaction_key(dt)->class != dns_transaction_key(t)->class)
3056 continue;
3057 if (dns_transaction_key(dt)->type != type)
3058 continue;
3059
3060 r = dns_name_equal(dns_resource_key_name(dns_transaction_key(dt)), name);
3061 if (r < 0)
3062 return r;
3063 if (r == 0)
3064 continue;
3065
3066 return FLAGS_SET(dt->answer_query_flags, SD_RESOLVED_AUTHENTICATED);
3067 }
3068
3069 /* If in doubt, require NSEC/NSEC3 */
3070 return true;
3071 }
3072
3073 static int dns_transaction_dnskey_authenticated(DnsTransaction *t, DnsResourceRecord *rr) {
3074 DnsResourceRecord *rrsig;
3075 bool found = false;
3076 int r;
3077
3078 /* Checks whether any of the DNSKEYs used for the RRSIGs for
3079 * the specified RRset is authenticated (i.e. has a matching
3080 * DS RR). */
3081
3082 r = dns_transaction_negative_trust_anchor_lookup(t, dns_resource_key_name(rr->key));
3083 if (r < 0)
3084 return r;
3085 if (r > 0)
3086 return false;
3087
3088 DNS_ANSWER_FOREACH(rrsig, t->answer) {
3089 DnsTransaction *dt;
3090
3091 r = dnssec_key_match_rrsig(rr->key, rrsig);
3092 if (r < 0)
3093 return r;
3094 if (r == 0)
3095 continue;
3096
3097 SET_FOREACH(dt, t->dnssec_transactions) {
3098
3099 if (dns_transaction_key(dt)->class != rr->key->class)
3100 continue;
3101
3102 if (dns_transaction_key(dt)->type == DNS_TYPE_DNSKEY) {
3103
3104 r = dns_name_equal(dns_resource_key_name(dns_transaction_key(dt)), rrsig->rrsig.signer);
3105 if (r < 0)
3106 return r;
3107 if (r == 0)
3108 continue;
3109
3110 /* OK, we found an auxiliary DNSKEY lookup. If that lookup is authenticated,
3111 * report this. */
3112
3113 if (FLAGS_SET(dt->answer_query_flags, SD_RESOLVED_AUTHENTICATED))
3114 return true;
3115
3116 found = true;
3117
3118 } else if (dns_transaction_key(dt)->type == DNS_TYPE_DS) {
3119
3120 r = dns_name_equal(dns_resource_key_name(dns_transaction_key(dt)), rrsig->rrsig.signer);
3121 if (r < 0)
3122 return r;
3123 if (r == 0)
3124 continue;
3125
3126 /* OK, we found an auxiliary DS lookup. If that lookup is authenticated and
3127 * non-zero, we won! */
3128
3129 if (!FLAGS_SET(dt->answer_query_flags, SD_RESOLVED_AUTHENTICATED))
3130 return false;
3131
3132 return dns_answer_match_key(dt->answer, dns_transaction_key(dt), NULL);
3133 }
3134 }
3135 }
3136
3137 return found ? false : -ENXIO;
3138 }
3139
3140 static int dns_transaction_known_signed(DnsTransaction *t, DnsResourceRecord *rr) {
3141 assert(t);
3142 assert(rr);
3143
3144 /* We know that the root domain is signed, hence if it appears
3145 * not to be signed, there's a problem with the DNS server */
3146
3147 return rr->key->class == DNS_CLASS_IN &&
3148 dns_name_is_root(dns_resource_key_name(rr->key));
3149 }
3150
3151 static int dns_transaction_check_revoked_trust_anchors(DnsTransaction *t) {
3152 DnsResourceRecord *rr;
3153 int r;
3154
3155 assert(t);
3156
3157 /* Maybe warn the user that we encountered a revoked DNSKEY
3158 * for a key from our trust anchor. Note that we don't care
3159 * whether the DNSKEY can be authenticated or not. It's
3160 * sufficient if it is self-signed. */
3161
3162 DNS_ANSWER_FOREACH(rr, t->answer) {
3163 r = dns_trust_anchor_check_revoked(&t->scope->manager->trust_anchor, rr, t->answer);
3164 if (r < 0)
3165 return r;
3166 }
3167
3168 return 0;
3169 }
3170
3171 static int dns_transaction_invalidate_revoked_keys(DnsTransaction *t) {
3172 bool changed;
3173 int r;
3174
3175 assert(t);
3176
3177 /* Removes all DNSKEY/DS objects from t->validated_keys that
3178 * our trust anchors database considers revoked. */
3179
3180 do {
3181 DnsResourceRecord *rr;
3182
3183 changed = false;
3184
3185 DNS_ANSWER_FOREACH(rr, t->validated_keys) {
3186 r = dns_trust_anchor_is_revoked(&t->scope->manager->trust_anchor, rr);
3187 if (r < 0)
3188 return r;
3189 if (r > 0) {
3190 r = dns_answer_remove_by_rr(&t->validated_keys, rr);
3191 if (r < 0)
3192 return r;
3193
3194 assert(r > 0);
3195 changed = true;
3196 break;
3197 }
3198 }
3199 } while (changed);
3200
3201 return 0;
3202 }
3203
3204 static int dns_transaction_copy_validated(DnsTransaction *t) {
3205 DnsTransaction *dt;
3206 int r;
3207
3208 assert(t);
3209
3210 /* Copy all validated RRs from the auxiliary DNSSEC transactions into our set of validated RRs */
3211
3212 SET_FOREACH(dt, t->dnssec_transactions) {
3213
3214 if (DNS_TRANSACTION_IS_LIVE(dt->state))
3215 continue;
3216
3217 if (!FLAGS_SET(dt->answer_query_flags, SD_RESOLVED_AUTHENTICATED))
3218 continue;
3219
3220 r = dns_answer_extend(&t->validated_keys, dt->answer);
3221 if (r < 0)
3222 return r;
3223 }
3224
3225 return 0;
3226 }
3227
3228 typedef enum {
3229 DNSSEC_PHASE_DNSKEY, /* Phase #1, only validate DNSKEYs */
3230 DNSSEC_PHASE_NSEC, /* Phase #2, only validate NSEC+NSEC3 */
3231 DNSSEC_PHASE_ALL, /* Phase #3, validate everything else */
3232 } Phase;
3233
3234 static int dnssec_validate_records(
3235 DnsTransaction *t,
3236 Phase phase,
3237 bool *have_nsec,
3238 unsigned *nvalidations,
3239 DnsAnswer **validated) {
3240
3241 DnsResourceRecord *rr;
3242 int r;
3243
3244 assert(nvalidations);
3245
3246 /* Returns negative on error, 0 if validation failed, 1 to restart validation, 2 when finished. */
3247
3248 DNS_ANSWER_FOREACH(rr, t->answer) {
3249 _unused_ _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr_ref = dns_resource_record_ref(rr);
3250 DnsResourceRecord *rrsig = NULL;
3251 DnssecResult result;
3252
3253 switch (rr->key->type) {
3254 case DNS_TYPE_RRSIG:
3255 continue;
3256
3257 case DNS_TYPE_DNSKEY:
3258 /* We validate DNSKEYs only in the DNSKEY and ALL phases */
3259 if (phase == DNSSEC_PHASE_NSEC)
3260 continue;
3261 break;
3262
3263 case DNS_TYPE_NSEC:
3264 case DNS_TYPE_NSEC3:
3265 *have_nsec = true;
3266
3267 /* We validate NSEC/NSEC3 only in the NSEC and ALL phases */
3268 if (phase == DNSSEC_PHASE_DNSKEY)
3269 continue;
3270 break;
3271
3272 default:
3273 /* We validate all other RRs only in the ALL phases */
3274 if (phase != DNSSEC_PHASE_ALL)
3275 continue;
3276 }
3277
3278 r = dnssec_verify_rrset_search(
3279 t->answer,
3280 rr->key,
3281 t->validated_keys,
3282 USEC_INFINITY,
3283 &result,
3284 &rrsig);
3285 if (r < 0)
3286 return r;
3287 *nvalidations += r;
3288
3289 log_debug("Looking at %s: %s", strna(dns_resource_record_to_string(rr)), dnssec_result_to_string(result));
3290
3291 if (result == DNSSEC_VALIDATED) {
3292 assert(rrsig);
3293
3294 if (rr->key->type == DNS_TYPE_DNSKEY) {
3295 /* If we just validated a DNSKEY RRset, then let's add these keys to
3296 * the set of validated keys for this transaction. */
3297
3298 r = dns_answer_copy_by_key(&t->validated_keys, t->answer, rr->key, DNS_ANSWER_AUTHENTICATED, rrsig);
3299 if (r < 0)
3300 return r;
3301
3302 /* Some of the DNSKEYs we just added might already have been revoked,
3303 * remove them again in that case. */
3304 r = dns_transaction_invalidate_revoked_keys(t);
3305 if (r < 0)
3306 return r;
3307 }
3308
3309 /* Add the validated RRset to the new list of validated RRsets, and remove it from
3310 * the unvalidated RRsets. We mark the RRset as authenticated and cacheable. */
3311 r = dns_answer_move_by_key(validated, &t->answer, rr->key, DNS_ANSWER_AUTHENTICATED|DNS_ANSWER_CACHEABLE, rrsig);
3312 if (r < 0)
3313 return r;
3314
3315 manager_dnssec_verdict(t->scope->manager, DNSSEC_SECURE, rr->key);
3316
3317 /* Exit the loop, we dropped something from the answer, start from the beginning */
3318 return 1;
3319 }
3320
3321 /* If we haven't read all DNSKEYs yet a negative result of the validation is irrelevant, as
3322 * there might be more DNSKEYs coming. Similar, if we haven't read all NSEC/NSEC3 RRs yet,
3323 * we cannot do positive wildcard proofs yet, as those require the NSEC/NSEC3 RRs. */
3324 if (phase != DNSSEC_PHASE_ALL)
3325 continue;
3326
3327 if (result == DNSSEC_VALIDATED_WILDCARD) {
3328 bool authenticated = false;
3329 const char *source;
3330
3331 assert(rrsig);
3332
3333 /* This RRset validated, but as a wildcard. This means we need
3334 * to prove via NSEC/NSEC3 that no matching non-wildcard RR exists. */
3335
3336 /* First step, determine the source of synthesis */
3337 r = dns_resource_record_source(rrsig, &source);
3338 if (r < 0)
3339 return r;
3340
3341 r = dnssec_test_positive_wildcard(*validated,
3342 dns_resource_key_name(rr->key),
3343 source,
3344 rrsig->rrsig.signer,
3345 &authenticated);
3346
3347 /* Unless the NSEC proof showed that the key really doesn't exist something is off. */
3348 if (r == 0)
3349 result = DNSSEC_INVALID;
3350 else {
3351 r = dns_answer_move_by_key(
3352 validated,
3353 &t->answer,
3354 rr->key,
3355 authenticated ? (DNS_ANSWER_AUTHENTICATED|DNS_ANSWER_CACHEABLE) : 0,
3356 rrsig);
3357 if (r < 0)
3358 return r;
3359
3360 manager_dnssec_verdict(t->scope->manager, authenticated ? DNSSEC_SECURE : DNSSEC_INSECURE, rr->key);
3361
3362 /* Exit the loop, we dropped something from the answer, start from the beginning */
3363 return 1;
3364 }
3365 }
3366
3367 if (result == DNSSEC_NO_SIGNATURE) {
3368 r = dns_transaction_requires_rrsig(t, rr);
3369 if (r < 0)
3370 return r;
3371 if (r == 0) {
3372 /* Data does not require signing. In that case, just copy it over,
3373 * but remember that this is by no means authenticated. */
3374 r = dns_answer_move_by_key(
3375 validated,
3376 &t->answer,
3377 rr->key,
3378 0,
3379 NULL);
3380 if (r < 0)
3381 return r;
3382
3383 manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, rr->key);
3384 return 1;
3385 }
3386
3387 r = dns_transaction_known_signed(t, rr);
3388 if (r < 0)
3389 return r;
3390 if (r > 0) {
3391 /* This is an RR we know has to be signed. If it isn't this means
3392 * the server is not attaching RRSIGs, hence complain. */
3393
3394 dns_server_packet_rrsig_missing(t->server, t->current_feature_level);
3395
3396 if (t->scope->dnssec_mode == DNSSEC_ALLOW_DOWNGRADE) {
3397
3398 /* Downgrading is OK? If so, just consider the information unsigned */
3399
3400 r = dns_answer_move_by_key(validated, &t->answer, rr->key, 0, NULL);
3401 if (r < 0)
3402 return r;
3403
3404 manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, rr->key);
3405 return 1;
3406 }
3407
3408 /* Otherwise, fail */
3409 t->answer_dnssec_result = DNSSEC_INCOMPATIBLE_SERVER;
3410 return 0;
3411 }
3412
3413 r = dns_transaction_in_private_tld(t, rr->key);
3414 if (r < 0)
3415 return r;
3416 if (r > 0) {
3417 char s[DNS_RESOURCE_KEY_STRING_MAX];
3418
3419 /* The data is from a TLD that is proven not to exist, and we are in downgrade
3420 * mode, hence ignore the fact that this was not signed. */
3421
3422 log_info("Detected RRset %s is in a private DNS zone, permitting unsigned RRs.",
3423 dns_resource_key_to_string(rr->key, s, sizeof s));
3424
3425 r = dns_answer_move_by_key(validated, &t->answer, rr->key, 0, NULL);
3426 if (r < 0)
3427 return r;
3428
3429 manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, rr->key);
3430 return 1;
3431 }
3432 }
3433
3434 /* https://datatracker.ietf.org/doc/html/rfc6840#section-5.2 */
3435 if (result == DNSSEC_UNSUPPORTED_ALGORITHM) {
3436 r = dns_answer_move_by_key(validated, &t->answer, rr->key, 0, NULL);
3437 if (r < 0)
3438 return r;
3439
3440 manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, rr->key);
3441 return 1;
3442 }
3443
3444 if (IN_SET(result,
3445 DNSSEC_MISSING_KEY,
3446 DNSSEC_SIGNATURE_EXPIRED)) {
3447
3448 r = dns_transaction_dnskey_authenticated(t, rr);
3449 if (r < 0 && r != -ENXIO)
3450 return r;
3451 if (r == 0) {
3452 /* The DNSKEY transaction was not authenticated, this means there's
3453 * no DS for this, which means it's OK if no keys are found for this signature. */
3454
3455 r = dns_answer_move_by_key(validated, &t->answer, rr->key, 0, NULL);
3456 if (r < 0)
3457 return r;
3458
3459 manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, rr->key);
3460 return 1;
3461 }
3462 }
3463
3464 r = dns_transaction_is_primary_response(t, rr);
3465 if (r < 0)
3466 return r;
3467 if (r > 0) {
3468 /* Look for a matching DNAME for this CNAME */
3469 r = dns_answer_has_dname_for_cname(t->answer, rr);
3470 if (r < 0)
3471 return r;
3472 if (r == 0) {
3473 /* Also look among the stuff we already validated */
3474 r = dns_answer_has_dname_for_cname(*validated, rr);
3475 if (r < 0)
3476 return r;
3477 }
3478
3479 if (r == 0) {
3480 if (IN_SET(result,
3481 DNSSEC_INVALID,
3482 DNSSEC_SIGNATURE_EXPIRED,
3483 DNSSEC_NO_SIGNATURE))
3484 manager_dnssec_verdict(t->scope->manager, DNSSEC_BOGUS, rr->key);
3485 else /* DNSSEC_MISSING_KEY, DNSSEC_UNSUPPORTED_ALGORITHM,
3486 or DNSSEC_TOO_MANY_VALIDATIONS */
3487 manager_dnssec_verdict(t->scope->manager, DNSSEC_INDETERMINATE, rr->key);
3488
3489 /* This is a primary response to our question, and it failed validation.
3490 * That's fatal. */
3491 t->answer_dnssec_result = result;
3492 return 0;
3493 }
3494
3495 /* This is a primary response, but we do have a DNAME RR
3496 * in the RR that can replay this CNAME, hence rely on
3497 * that, and we can remove the CNAME in favour of it. */
3498 }
3499
3500 /* This is just some auxiliary data. Just remove the RRset and continue. */
3501 r = dns_answer_remove_by_key(&t->answer, rr->key);
3502 if (r < 0)
3503 return r;
3504
3505 /* We dropped something from the answer, start from the beginning. */
3506 return 1;
3507 }
3508
3509 return 2; /* Finito. */
3510 }
3511
3512 int dns_transaction_validate_dnssec(DnsTransaction *t) {
3513 _cleanup_(dns_answer_unrefp) DnsAnswer *validated = NULL;
3514 Phase phase;
3515 DnsAnswerFlags flags;
3516 int r;
3517 char key_str[DNS_RESOURCE_KEY_STRING_MAX];
3518
3519 assert(t);
3520
3521 /* We have now collected all DS and DNSKEY RRs in t->validated_keys, let's see which RRs we can now
3522 * authenticate with that. */
3523
3524 if (FLAGS_SET(t->query_flags, SD_RESOLVED_NO_VALIDATE) || t->scope->dnssec_mode == DNSSEC_NO)
3525 return 0;
3526
3527 /* Already validated */
3528 if (t->answer_dnssec_result != _DNSSEC_RESULT_INVALID)
3529 return 0;
3530
3531 /* Our own stuff needs no validation */
3532 if (IN_SET(t->answer_source, DNS_TRANSACTION_ZONE, DNS_TRANSACTION_TRUST_ANCHOR)) {
3533 t->answer_dnssec_result = DNSSEC_VALIDATED;
3534 SET_FLAG(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED, true);
3535 return 0;
3536 }
3537
3538 /* Cached stuff is not affected by validation. */
3539 if (t->answer_source != DNS_TRANSACTION_NETWORK)
3540 return 0;
3541
3542 if (!dns_transaction_dnssec_supported_full(t)) {
3543 /* The server does not support DNSSEC, or doesn't augment responses with RRSIGs. */
3544 t->answer_dnssec_result = DNSSEC_INCOMPATIBLE_SERVER;
3545 log_debug("Not validating response for %" PRIu16 ", used server feature level does not support DNSSEC.", t->id);
3546 return 0;
3547 }
3548
3549 log_debug("Validating response from transaction %" PRIu16 " (%s).",
3550 t->id,
3551 dns_resource_key_to_string(dns_transaction_key(t), key_str, sizeof key_str));
3552
3553 /* First, see if this response contains any revoked trust
3554 * anchors we care about */
3555 r = dns_transaction_check_revoked_trust_anchors(t);
3556 if (r < 0)
3557 return r;
3558
3559 /* Third, copy all RRs we acquired successfully from auxiliary RRs over. */
3560 r = dns_transaction_copy_validated(t);
3561 if (r < 0)
3562 return r;
3563
3564 /* Second, see if there are DNSKEYs we already know a
3565 * validated DS for. */
3566 r = dns_transaction_validate_dnskey_by_ds(t);
3567 if (r < 0)
3568 return r;
3569
3570 /* Fourth, remove all DNSKEY and DS RRs again that our trust
3571 * anchor says are revoked. After all we might have marked
3572 * some keys revoked above, but they might still be lingering
3573 * in our validated_keys list. */
3574 r = dns_transaction_invalidate_revoked_keys(t);
3575 if (r < 0)
3576 return r;
3577
3578 phase = DNSSEC_PHASE_DNSKEY;
3579 for (unsigned nvalidations = 0;;) {
3580 bool have_nsec = false;
3581
3582 r = dnssec_validate_records(t, phase, &have_nsec, &nvalidations, &validated);
3583 if (r <= 0)
3584 return r;
3585
3586 if (nvalidations > DNSSEC_VALIDATION_MAX) {
3587 /* This reply requires an onerous number of signature validations to verify. Let's
3588 * not waste our time trying, as this shouldn't happen for well-behaved domains
3589 * anyway. */
3590 t->answer_dnssec_result = DNSSEC_TOO_MANY_VALIDATIONS;
3591 return 0;
3592 }
3593
3594 /* Try again as long as we managed to achieve something */
3595 if (r == 1)
3596 continue;
3597
3598 if (phase == DNSSEC_PHASE_DNSKEY && have_nsec) {
3599 /* OK, we processed all DNSKEYs, and there are NSEC/NSEC3 RRs, look at those now. */
3600 phase = DNSSEC_PHASE_NSEC;
3601 continue;
3602 }
3603
3604 if (phase != DNSSEC_PHASE_ALL) {
3605 /* OK, we processed all DNSKEYs and NSEC/NSEC3 RRs, look at all the rest now.
3606 * Note that in this third phase we start to remove RRs we couldn't validate. */
3607 phase = DNSSEC_PHASE_ALL;
3608 continue;
3609 }
3610
3611 /* We're done */
3612 break;
3613 }
3614
3615 DNS_ANSWER_REPLACE(t->answer, TAKE_PTR(validated));
3616
3617 /* At this point the answer only contains validated
3618 * RRsets. Now, let's see if it actually answers the question
3619 * we asked. If so, great! If it doesn't, then see if
3620 * NSEC/NSEC3 can prove this. */
3621 r = dns_transaction_has_positive_answer(t, &flags);
3622 if (r > 0) {
3623 /* Yes, it answers the question! */
3624
3625 if (flags & DNS_ANSWER_AUTHENTICATED) {
3626 /* The answer is fully authenticated, yay. */
3627 t->answer_dnssec_result = DNSSEC_VALIDATED;
3628 t->answer_rcode = DNS_RCODE_SUCCESS;
3629 SET_FLAG(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED, true);
3630 } else {
3631 /* The answer is not fully authenticated. */
3632 t->answer_dnssec_result = DNSSEC_UNSIGNED;
3633 SET_FLAG(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED, false);
3634 }
3635
3636 } else if (r == 0) {
3637 DnssecNsecResult nr;
3638 bool authenticated = false;
3639
3640 /* Bummer! Let's check NSEC/NSEC3 */
3641 r = dnssec_nsec_test(t->answer, dns_transaction_key(t), &nr, &authenticated, &t->answer_nsec_ttl);
3642 if (r < 0)
3643 return r;
3644
3645 switch (nr) {
3646
3647 case DNSSEC_NSEC_NXDOMAIN:
3648 /* NSEC proves the domain doesn't exist. Very good. */
3649 log_debug("Proved NXDOMAIN via NSEC/NSEC3 for transaction %u (%s)", t->id, key_str);
3650 t->answer_dnssec_result = DNSSEC_VALIDATED;
3651 t->answer_rcode = DNS_RCODE_NXDOMAIN;
3652 SET_FLAG(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED, authenticated);
3653
3654 manager_dnssec_verdict(t->scope->manager, authenticated ? DNSSEC_SECURE : DNSSEC_INSECURE, dns_transaction_key(t));
3655 break;
3656
3657 case DNSSEC_NSEC_NODATA:
3658 /* NSEC proves that there's no data here, very good. */
3659 log_debug("Proved NODATA via NSEC/NSEC3 for transaction %u (%s)", t->id, key_str);
3660 t->answer_dnssec_result = DNSSEC_VALIDATED;
3661 t->answer_rcode = DNS_RCODE_SUCCESS;
3662 SET_FLAG(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED, authenticated);
3663
3664 manager_dnssec_verdict(t->scope->manager, authenticated ? DNSSEC_SECURE : DNSSEC_INSECURE, dns_transaction_key(t));
3665 break;
3666
3667 case DNSSEC_NSEC_OPTOUT:
3668 /* NSEC3 says the data might not be signed */
3669 log_debug("Data is NSEC3 opt-out via NSEC/NSEC3 for transaction %u (%s)", t->id, key_str);
3670 t->answer_dnssec_result = DNSSEC_UNSIGNED;
3671 SET_FLAG(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED, false);
3672
3673 manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, dns_transaction_key(t));
3674 break;
3675
3676 case DNSSEC_NSEC_NO_RR:
3677 /* No NSEC data? Bummer! */
3678
3679 r = dns_transaction_requires_nsec(t);
3680 if (r < 0)
3681 return r;
3682 if (r > 0) {
3683 t->answer_dnssec_result = DNSSEC_NO_SIGNATURE;
3684 manager_dnssec_verdict(t->scope->manager, DNSSEC_BOGUS, dns_transaction_key(t));
3685 } else {
3686 t->answer_dnssec_result = DNSSEC_UNSIGNED;
3687 SET_FLAG(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED, false);
3688 manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, dns_transaction_key(t));
3689 }
3690
3691 break;
3692
3693 case DNSSEC_NSEC_UNSUPPORTED_ALGORITHM:
3694 /* We don't know the NSEC3 algorithm used? */
3695 t->answer_dnssec_result = DNSSEC_UNSUPPORTED_ALGORITHM;
3696 manager_dnssec_verdict(t->scope->manager, DNSSEC_INDETERMINATE, dns_transaction_key(t));
3697 break;
3698
3699 case DNSSEC_NSEC_FOUND:
3700 case DNSSEC_NSEC_CNAME:
3701 /* NSEC says it needs to be there, but we couldn't find it? Bummer! */
3702 t->answer_dnssec_result = DNSSEC_NSEC_MISMATCH;
3703 manager_dnssec_verdict(t->scope->manager, DNSSEC_BOGUS, dns_transaction_key(t));
3704 break;
3705
3706 default:
3707 assert_not_reached();
3708 }
3709 }
3710
3711 return 1;
3712 }
3713
3714 static const char* const dns_transaction_state_table[_DNS_TRANSACTION_STATE_MAX] = {
3715 [DNS_TRANSACTION_NULL] = "null",
3716 [DNS_TRANSACTION_PENDING] = "pending",
3717 [DNS_TRANSACTION_VALIDATING] = "validating",
3718 [DNS_TRANSACTION_RCODE_FAILURE] = "rcode-failure",
3719 [DNS_TRANSACTION_SUCCESS] = "success",
3720 [DNS_TRANSACTION_NO_SERVERS] = "no-servers",
3721 [DNS_TRANSACTION_TIMEOUT] = "timeout",
3722 [DNS_TRANSACTION_ATTEMPTS_MAX_REACHED] = "attempts-max-reached",
3723 [DNS_TRANSACTION_INVALID_REPLY] = "invalid-reply",
3724 [DNS_TRANSACTION_ERRNO] = "errno",
3725 [DNS_TRANSACTION_ABORTED] = "aborted",
3726 [DNS_TRANSACTION_DNSSEC_FAILED] = "dnssec-failed",
3727 [DNS_TRANSACTION_NO_TRUST_ANCHOR] = "no-trust-anchor",
3728 [DNS_TRANSACTION_RR_TYPE_UNSUPPORTED] = "rr-type-unsupported",
3729 [DNS_TRANSACTION_NETWORK_DOWN] = "network-down",
3730 [DNS_TRANSACTION_NOT_FOUND] = "not-found",
3731 [DNS_TRANSACTION_NO_SOURCE] = "no-source",
3732 [DNS_TRANSACTION_STUB_LOOP] = "stub-loop",
3733 };
3734 DEFINE_STRING_TABLE_LOOKUP(dns_transaction_state, DnsTransactionState);
3735
3736 static const char* const dns_transaction_source_table[_DNS_TRANSACTION_SOURCE_MAX] = {
3737 [DNS_TRANSACTION_NETWORK] = "network",
3738 [DNS_TRANSACTION_CACHE] = "cache",
3739 [DNS_TRANSACTION_ZONE] = "zone",
3740 [DNS_TRANSACTION_TRUST_ANCHOR] = "trust-anchor",
3741 };
3742 DEFINE_STRING_TABLE_LOOKUP(dns_transaction_source, DnsTransactionSource);