]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dns-transaction.c
tree-wide: remove Emacs lines from all files
[thirdparty/systemd.git] / src / resolve / resolved-dns-transaction.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2014 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <sd-messages.h>
21
22 #include "af-list.h"
23 #include "alloc-util.h"
24 #include "dns-domain.h"
25 #include "errno-list.h"
26 #include "fd-util.h"
27 #include "random-util.h"
28 #include "resolved-dns-cache.h"
29 #include "resolved-dns-transaction.h"
30 #include "resolved-llmnr.h"
31 #include "string-table.h"
32
33 #define TRANSACTIONS_MAX 4096
34
35 static void dns_transaction_reset_answer(DnsTransaction *t) {
36 assert(t);
37
38 t->received = dns_packet_unref(t->received);
39 t->answer = dns_answer_unref(t->answer);
40 t->answer_rcode = 0;
41 t->answer_dnssec_result = _DNSSEC_RESULT_INVALID;
42 t->answer_source = _DNS_TRANSACTION_SOURCE_INVALID;
43 t->answer_authenticated = false;
44 t->answer_nsec_ttl = (uint32_t) -1;
45 t->answer_errno = 0;
46 }
47
48 static void dns_transaction_flush_dnssec_transactions(DnsTransaction *t) {
49 DnsTransaction *z;
50
51 assert(t);
52
53 while ((z = set_steal_first(t->dnssec_transactions))) {
54 set_remove(z->notify_transactions, t);
55 dns_transaction_gc(z);
56 }
57 }
58
59 static void dns_transaction_close_connection(DnsTransaction *t) {
60 assert(t);
61
62 t->stream = dns_stream_free(t->stream);
63 t->dns_udp_event_source = sd_event_source_unref(t->dns_udp_event_source);
64 t->dns_udp_fd = safe_close(t->dns_udp_fd);
65 }
66
67 static void dns_transaction_stop_timeout(DnsTransaction *t) {
68 assert(t);
69
70 t->timeout_event_source = sd_event_source_unref(t->timeout_event_source);
71 }
72
73 DnsTransaction* dns_transaction_free(DnsTransaction *t) {
74 DnsQueryCandidate *c;
75 DnsZoneItem *i;
76 DnsTransaction *z;
77
78 if (!t)
79 return NULL;
80
81 log_debug("Freeing transaction %" PRIu16 ".", t->id);
82
83 dns_transaction_close_connection(t);
84 dns_transaction_stop_timeout(t);
85
86 dns_packet_unref(t->sent);
87 dns_transaction_reset_answer(t);
88
89 dns_server_unref(t->server);
90
91 if (t->scope) {
92 hashmap_remove_value(t->scope->transactions_by_key, t->key, t);
93 LIST_REMOVE(transactions_by_scope, t->scope->transactions, t);
94
95 if (t->id != 0)
96 hashmap_remove(t->scope->manager->dns_transactions, UINT_TO_PTR(t->id));
97 }
98
99 while ((c = set_steal_first(t->notify_query_candidates)))
100 set_remove(c->transactions, t);
101 set_free(t->notify_query_candidates);
102
103 while ((i = set_steal_first(t->notify_zone_items)))
104 i->probe_transaction = NULL;
105 set_free(t->notify_zone_items);
106
107 while ((z = set_steal_first(t->notify_transactions)))
108 set_remove(z->dnssec_transactions, t);
109 set_free(t->notify_transactions);
110
111 dns_transaction_flush_dnssec_transactions(t);
112 set_free(t->dnssec_transactions);
113
114 dns_answer_unref(t->validated_keys);
115 dns_resource_key_unref(t->key);
116 free(t->key_string);
117
118 free(t);
119 return NULL;
120 }
121
122 DEFINE_TRIVIAL_CLEANUP_FUNC(DnsTransaction*, dns_transaction_free);
123
124 bool dns_transaction_gc(DnsTransaction *t) {
125 assert(t);
126
127 if (t->block_gc > 0)
128 return true;
129
130 if (set_isempty(t->notify_query_candidates) &&
131 set_isempty(t->notify_zone_items) &&
132 set_isempty(t->notify_transactions)) {
133 dns_transaction_free(t);
134 return false;
135 }
136
137 return true;
138 }
139
140 static uint16_t pick_new_id(Manager *m) {
141 uint16_t new_id;
142
143 /* Find a fresh, unused transaction id. Note that this loop is bounded because there's a limit on the number of
144 * transactions, and it's much lower than the space of IDs. */
145
146 assert_cc(TRANSACTIONS_MAX < 0xFFFF);
147
148 do
149 random_bytes(&new_id, sizeof(new_id));
150 while (new_id == 0 ||
151 hashmap_get(m->dns_transactions, UINT_TO_PTR(new_id)));
152
153 return new_id;
154 }
155
156 int dns_transaction_new(DnsTransaction **ret, DnsScope *s, DnsResourceKey *key) {
157 _cleanup_(dns_transaction_freep) DnsTransaction *t = NULL;
158 int r;
159
160 assert(ret);
161 assert(s);
162 assert(key);
163
164 /* Don't allow looking up invalid or pseudo RRs */
165 if (!dns_type_is_valid_query(key->type))
166 return -EINVAL;
167 if (dns_type_is_obsolete(key->type))
168 return -EOPNOTSUPP;
169
170 /* We only support the IN class */
171 if (key->class != DNS_CLASS_IN && key->class != DNS_CLASS_ANY)
172 return -EOPNOTSUPP;
173
174 if (hashmap_size(s->manager->dns_transactions) >= TRANSACTIONS_MAX)
175 return -EBUSY;
176
177 r = hashmap_ensure_allocated(&s->manager->dns_transactions, NULL);
178 if (r < 0)
179 return r;
180
181 r = hashmap_ensure_allocated(&s->transactions_by_key, &dns_resource_key_hash_ops);
182 if (r < 0)
183 return r;
184
185 t = new0(DnsTransaction, 1);
186 if (!t)
187 return -ENOMEM;
188
189 t->dns_udp_fd = -1;
190 t->answer_source = _DNS_TRANSACTION_SOURCE_INVALID;
191 t->answer_dnssec_result = _DNSSEC_RESULT_INVALID;
192 t->answer_nsec_ttl = (uint32_t) -1;
193 t->key = dns_resource_key_ref(key);
194 t->current_feature_level = _DNS_SERVER_FEATURE_LEVEL_INVALID;
195
196 t->id = pick_new_id(s->manager);
197
198 r = hashmap_put(s->manager->dns_transactions, UINT_TO_PTR(t->id), t);
199 if (r < 0) {
200 t->id = 0;
201 return r;
202 }
203
204 r = hashmap_replace(s->transactions_by_key, t->key, t);
205 if (r < 0) {
206 hashmap_remove(s->manager->dns_transactions, UINT_TO_PTR(t->id));
207 return r;
208 }
209
210 LIST_PREPEND(transactions_by_scope, s->transactions, t);
211 t->scope = s;
212
213 s->manager->n_transactions_total ++;
214
215 if (ret)
216 *ret = t;
217
218 t = NULL;
219
220 return 0;
221 }
222
223 static void dns_transaction_shuffle_id(DnsTransaction *t) {
224 uint16_t new_id;
225 assert(t);
226
227 /* Pick a new ID for this transaction. */
228
229 new_id = pick_new_id(t->scope->manager);
230 assert_se(hashmap_remove_and_put(t->scope->manager->dns_transactions, UINT_TO_PTR(t->id), UINT_TO_PTR(new_id), t) >= 0);
231
232 log_debug("Transaction %" PRIu16 " is now %" PRIu16 ".", t->id, new_id);
233 t->id = new_id;
234
235 /* Make sure we generate a new packet with the new ID */
236 t->sent = dns_packet_unref(t->sent);
237 }
238
239 static void dns_transaction_tentative(DnsTransaction *t, DnsPacket *p) {
240 _cleanup_free_ char *pretty = NULL;
241 DnsZoneItem *z;
242
243 assert(t);
244 assert(p);
245
246 if (manager_our_packet(t->scope->manager, p) != 0)
247 return;
248
249 in_addr_to_string(p->family, &p->sender, &pretty);
250
251 log_debug("Transaction %" PRIu16 " for <%s> on scope %s on %s/%s got tentative packet from %s.",
252 t->id,
253 dns_transaction_key_string(t),
254 dns_protocol_to_string(t->scope->protocol),
255 t->scope->link ? t->scope->link->name : "*",
256 t->scope->family == AF_UNSPEC ? "*" : af_to_name(t->scope->family),
257 pretty);
258
259 /* RFC 4795, Section 4.1 says that the peer with the
260 * lexicographically smaller IP address loses */
261 if (memcmp(&p->sender, &p->destination, FAMILY_ADDRESS_SIZE(p->family)) >= 0) {
262 log_debug("Peer has lexicographically larger IP address and thus lost in the conflict.");
263 return;
264 }
265
266 log_debug("We have the lexicographically larger IP address and thus lost in the conflict.");
267
268 t->block_gc++;
269 while ((z = set_first(t->notify_zone_items))) {
270 /* First, make sure the zone item drops the reference
271 * to us */
272 dns_zone_item_probe_stop(z);
273
274 /* Secondly, report this as conflict, so that we might
275 * look for a different hostname */
276 dns_zone_item_conflict(z);
277 }
278 t->block_gc--;
279
280 dns_transaction_gc(t);
281 }
282
283 void dns_transaction_complete(DnsTransaction *t, DnsTransactionState state) {
284 DnsQueryCandidate *c;
285 DnsZoneItem *z;
286 DnsTransaction *d;
287 Iterator i;
288 const char *st;
289
290 assert(t);
291 assert(!DNS_TRANSACTION_IS_LIVE(state));
292
293 if (state == DNS_TRANSACTION_DNSSEC_FAILED)
294 log_struct(LOG_NOTICE,
295 LOG_MESSAGE_ID(SD_MESSAGE_DNSSEC_FAILURE),
296 LOG_MESSAGE("DNSSEC validation failed for question %s: %s", dns_transaction_key_string(t), dnssec_result_to_string(t->answer_dnssec_result)),
297 "DNS_TRANSACTION=%" PRIu16, t->id,
298 "DNS_QUESTION=%s", dns_transaction_key_string(t),
299 "DNSSEC_RESULT=%s", dnssec_result_to_string(t->answer_dnssec_result),
300 "DNS_SERVER=%s", dns_server_string(t->server),
301 "DNS_SERVER_FEATURE_LEVEL=%s", dns_server_feature_level_to_string(t->server->possible_feature_level),
302 NULL);
303
304 /* Note that this call might invalidate the query. Callers
305 * should hence not attempt to access the query or transaction
306 * after calling this function. */
307
308 if (state == DNS_TRANSACTION_ERRNO)
309 st = errno_to_name(t->answer_errno);
310 else
311 st = dns_transaction_state_to_string(state);
312
313 log_debug("Transaction %" PRIu16 " for <%s> on scope %s on %s/%s now complete with <%s> from %s (%s).",
314 t->id,
315 dns_transaction_key_string(t),
316 dns_protocol_to_string(t->scope->protocol),
317 t->scope->link ? t->scope->link->name : "*",
318 t->scope->family == AF_UNSPEC ? "*" : af_to_name(t->scope->family),
319 st,
320 t->answer_source < 0 ? "none" : dns_transaction_source_to_string(t->answer_source),
321 t->answer_authenticated ? "authenticated" : "unsigned");
322
323 t->state = state;
324
325 dns_transaction_close_connection(t);
326 dns_transaction_stop_timeout(t);
327
328 /* Notify all queries that are interested, but make sure the
329 * transaction isn't freed while we are still looking at it */
330 t->block_gc++;
331
332 SET_FOREACH(c, t->notify_query_candidates, i)
333 dns_query_candidate_notify(c);
334 SET_FOREACH(z, t->notify_zone_items, i)
335 dns_zone_item_notify(z);
336
337 if (!set_isempty(t->notify_transactions)) {
338 DnsTransaction **nt;
339 unsigned j, n = 0;
340
341 /* We need to be careful when notifying other
342 * transactions, as that might destroy other
343 * transactions in our list. Hence, in order to be
344 * able to safely iterate through the list of
345 * transactions, take a GC lock on all of them
346 * first. Then, in a second loop, notify them, but
347 * first unlock that specific transaction. */
348
349 nt = newa(DnsTransaction*, set_size(t->notify_transactions));
350 SET_FOREACH(d, t->notify_transactions, i) {
351 nt[n++] = d;
352 d->block_gc++;
353 }
354
355 assert(n == set_size(t->notify_transactions));
356
357 for (j = 0; j < n; j++) {
358 if (set_contains(t->notify_transactions, nt[j]))
359 dns_transaction_notify(nt[j], t);
360
361 nt[j]->block_gc--;
362 dns_transaction_gc(nt[j]);
363 }
364 }
365
366 t->block_gc--;
367 dns_transaction_gc(t);
368 }
369
370 static int dns_transaction_pick_server(DnsTransaction *t) {
371 DnsServer *server;
372
373 assert(t);
374 assert(t->scope->protocol == DNS_PROTOCOL_DNS);
375
376 server = dns_scope_get_dns_server(t->scope);
377 if (!server)
378 return -ESRCH;
379
380 t->current_feature_level = dns_server_possible_feature_level(server);
381
382 if (server == t->server)
383 return 0;
384
385 dns_server_unref(t->server);
386 t->server = dns_server_ref(server);
387
388 return 1;
389 }
390
391 static void dns_transaction_retry(DnsTransaction *t) {
392 int r;
393
394 assert(t);
395
396 log_debug("Retrying transaction %" PRIu16 ".", t->id);
397
398 /* Before we try again, switch to a new server. */
399 dns_scope_next_dns_server(t->scope);
400
401 r = dns_transaction_go(t);
402 if (r < 0) {
403 t->answer_errno = -r;
404 dns_transaction_complete(t, DNS_TRANSACTION_ERRNO);
405 }
406 }
407
408 static int dns_transaction_maybe_restart(DnsTransaction *t) {
409 assert(t);
410
411 if (!t->server)
412 return 0;
413
414 if (t->current_feature_level <= dns_server_possible_feature_level(t->server))
415 return 0;
416
417 /* The server's current feature level is lower than when we sent the original query. We learnt something from
418 the response or possibly an auxiliary DNSSEC response that we didn't know before. We take that as reason to
419 restart the whole transaction. This is a good idea to deal with servers that respond rubbish if we include
420 OPT RR or DO bit. One of these cases is documented here, for example:
421 https://open.nlnetlabs.nl/pipermail/dnssec-trigger/2014-November/000376.html */
422
423 log_debug("Server feature level is now lower than when we began our transaction. Restarting with new ID.");
424 dns_transaction_shuffle_id(t);
425 return dns_transaction_go(t);
426 }
427
428 static int on_stream_complete(DnsStream *s, int error) {
429 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
430 DnsTransaction *t;
431
432 assert(s);
433 assert(s->transaction);
434
435 /* Copy the data we care about out of the stream before we
436 * destroy it. */
437 t = s->transaction;
438 p = dns_packet_ref(s->read_packet);
439
440 t->stream = dns_stream_free(t->stream);
441
442 if (ERRNO_IS_DISCONNECT(error)) {
443 usec_t usec;
444
445 if (t->scope->protocol == DNS_PROTOCOL_LLMNR) {
446 /* If the LLMNR/TCP connection failed, the host doesn't support LLMNR, and we cannot answer the
447 * question on this scope. */
448 dns_transaction_complete(t, DNS_TRANSACTION_NOT_FOUND);
449 return 0;
450 }
451
452 log_debug_errno(error, "Connection failure for DNS TCP stream: %m");
453 assert_se(sd_event_now(t->scope->manager->event, clock_boottime_or_monotonic(), &usec) >= 0);
454 dns_server_packet_lost(t->server, IPPROTO_TCP, t->current_feature_level, usec - t->start_usec);
455
456 dns_transaction_retry(t);
457 return 0;
458 }
459 if (error != 0) {
460 t->answer_errno = error;
461 dns_transaction_complete(t, DNS_TRANSACTION_ERRNO);
462 return 0;
463 }
464
465 if (dns_packet_validate_reply(p) <= 0) {
466 log_debug("Invalid TCP reply packet.");
467 dns_transaction_complete(t, DNS_TRANSACTION_INVALID_REPLY);
468 return 0;
469 }
470
471 dns_scope_check_conflicts(t->scope, p);
472
473 t->block_gc++;
474 dns_transaction_process_reply(t, p);
475 t->block_gc--;
476
477 /* If the response wasn't useful, then complete the transition
478 * now. After all, we are the worst feature set now with TCP
479 * sockets, and there's really no point in retrying. */
480 if (t->state == DNS_TRANSACTION_PENDING)
481 dns_transaction_complete(t, DNS_TRANSACTION_INVALID_REPLY);
482 else
483 dns_transaction_gc(t);
484
485 return 0;
486 }
487
488 static int dns_transaction_open_tcp(DnsTransaction *t) {
489 _cleanup_close_ int fd = -1;
490 int r;
491
492 assert(t);
493
494 dns_transaction_close_connection(t);
495
496 switch (t->scope->protocol) {
497
498 case DNS_PROTOCOL_DNS:
499 r = dns_transaction_pick_server(t);
500 if (r < 0)
501 return r;
502
503 if (!dns_server_dnssec_supported(t->server) && dns_type_is_dnssec(t->key->type))
504 return -EOPNOTSUPP;
505
506 r = dns_server_adjust_opt(t->server, t->sent, t->current_feature_level);
507 if (r < 0)
508 return r;
509
510 fd = dns_scope_socket_tcp(t->scope, AF_UNSPEC, NULL, t->server, 53);
511 break;
512
513 case DNS_PROTOCOL_LLMNR:
514 /* When we already received a reply to this (but it was truncated), send to its sender address */
515 if (t->received)
516 fd = dns_scope_socket_tcp(t->scope, t->received->family, &t->received->sender, NULL, t->received->sender_port);
517 else {
518 union in_addr_union address;
519 int family = AF_UNSPEC;
520
521 /* Otherwise, try to talk to the owner of a
522 * the IP address, in case this is a reverse
523 * PTR lookup */
524
525 r = dns_name_address(DNS_RESOURCE_KEY_NAME(t->key), &family, &address);
526 if (r < 0)
527 return r;
528 if (r == 0)
529 return -EINVAL;
530 if (family != t->scope->family)
531 return -ESRCH;
532
533 fd = dns_scope_socket_tcp(t->scope, family, &address, NULL, LLMNR_PORT);
534 }
535
536 break;
537
538 default:
539 return -EAFNOSUPPORT;
540 }
541
542 if (fd < 0)
543 return fd;
544
545 r = dns_stream_new(t->scope->manager, &t->stream, t->scope->protocol, fd);
546 if (r < 0)
547 return r;
548 fd = -1;
549
550 r = dns_stream_write_packet(t->stream, t->sent);
551 if (r < 0) {
552 t->stream = dns_stream_free(t->stream);
553 return r;
554 }
555
556 t->stream->complete = on_stream_complete;
557 t->stream->transaction = t;
558
559 /* The interface index is difficult to determine if we are
560 * connecting to the local host, hence fill this in right away
561 * instead of determining it from the socket */
562 if (t->scope->link)
563 t->stream->ifindex = t->scope->link->ifindex;
564
565 dns_transaction_reset_answer(t);
566
567 t->tried_stream = true;
568
569 return 0;
570 }
571
572 static void dns_transaction_cache_answer(DnsTransaction *t) {
573 assert(t);
574
575 /* For mDNS we cache whenever we get the packet, rather than
576 * in each transaction. */
577 if (!IN_SET(t->scope->protocol, DNS_PROTOCOL_DNS, DNS_PROTOCOL_LLMNR))
578 return;
579
580 /* We never cache if this packet is from the local host, under
581 * the assumption that a locally running DNS server would
582 * cache this anyway, and probably knows better when to flush
583 * the cache then we could. */
584 if (!DNS_PACKET_SHALL_CACHE(t->received))
585 return;
586
587 dns_cache_put(&t->scope->cache,
588 t->key,
589 t->answer_rcode,
590 t->answer,
591 t->answer_authenticated,
592 t->answer_nsec_ttl,
593 0,
594 t->received->family,
595 &t->received->sender);
596 }
597
598 static bool dns_transaction_dnssec_is_live(DnsTransaction *t) {
599 DnsTransaction *dt;
600 Iterator i;
601
602 assert(t);
603
604 SET_FOREACH(dt, t->dnssec_transactions, i)
605 if (DNS_TRANSACTION_IS_LIVE(dt->state))
606 return true;
607
608 return false;
609 }
610
611 static int dns_transaction_dnssec_ready(DnsTransaction *t) {
612 DnsTransaction *dt;
613 Iterator i;
614
615 assert(t);
616
617 /* Checks whether the auxiliary DNSSEC transactions of our transaction have completed, or are still
618 * ongoing. Returns 0, if we aren't ready for the DNSSEC validation, positive if we are. */
619
620 SET_FOREACH(dt, t->dnssec_transactions, i) {
621
622 switch (dt->state) {
623
624 case DNS_TRANSACTION_NULL:
625 case DNS_TRANSACTION_PENDING:
626 case DNS_TRANSACTION_VALIDATING:
627 /* Still ongoing */
628 return 0;
629
630 case DNS_TRANSACTION_RCODE_FAILURE:
631 if (dt->answer_rcode != DNS_RCODE_NXDOMAIN) {
632 log_debug("Auxiliary DNSSEC RR query failed with rcode=%s.", dns_rcode_to_string(dt->answer_rcode));
633 goto fail;
634 }
635
636 /* Fall-through: NXDOMAIN is good enough for us. This is because some DNS servers erronously
637 * return NXDOMAIN for empty non-terminals (Akamai...), and we need to handle that nicely, when
638 * asking for parent SOA or similar RRs to make unsigned proofs. */
639
640 case DNS_TRANSACTION_SUCCESS:
641 /* All good. */
642 break;
643
644 case DNS_TRANSACTION_DNSSEC_FAILED:
645 /* We handle DNSSEC failures different from other errors, as we care about the DNSSEC
646 * validationr result */
647
648 log_debug("Auxiliary DNSSEC RR query failed validation: %s", dnssec_result_to_string(dt->answer_dnssec_result));
649 t->answer_dnssec_result = dt->answer_dnssec_result; /* Copy error code over */
650 dns_transaction_complete(t, DNS_TRANSACTION_DNSSEC_FAILED);
651 return 0;
652
653
654 default:
655 log_debug("Auxiliary DNSSEC RR query failed with %s", dns_transaction_state_to_string(dt->state));
656 goto fail;
657 }
658 }
659
660 /* All is ready, we can go and validate */
661 return 1;
662
663 fail:
664 t->answer_dnssec_result = DNSSEC_FAILED_AUXILIARY;
665 dns_transaction_complete(t, DNS_TRANSACTION_DNSSEC_FAILED);
666 return 0;
667 }
668
669 static void dns_transaction_process_dnssec(DnsTransaction *t) {
670 int r;
671
672 assert(t);
673
674 /* Are there ongoing DNSSEC transactions? If so, let's wait for them. */
675 r = dns_transaction_dnssec_ready(t);
676 if (r < 0)
677 goto fail;
678 if (r == 0) /* We aren't ready yet (or one of our auxiliary transactions failed, and we shouldn't validate now */
679 return;
680
681 /* See if we learnt things from the additional DNSSEC transactions, that we didn't know before, and better
682 * restart the lookup immediately. */
683 r = dns_transaction_maybe_restart(t);
684 if (r < 0)
685 goto fail;
686 if (r > 0) /* Transaction got restarted... */
687 return;
688
689 /* All our auxiliary DNSSEC transactions are complete now. Try
690 * to validate our RRset now. */
691 r = dns_transaction_validate_dnssec(t);
692 if (r == -EBADMSG) {
693 dns_transaction_complete(t, DNS_TRANSACTION_INVALID_REPLY);
694 return;
695 }
696 if (r < 0)
697 goto fail;
698
699 if (t->answer_dnssec_result == DNSSEC_INCOMPATIBLE_SERVER &&
700 t->scope->dnssec_mode == DNSSEC_YES) {
701 /* We are not in automatic downgrade mode, and the
702 * server is bad, refuse operation. */
703 dns_transaction_complete(t, DNS_TRANSACTION_DNSSEC_FAILED);
704 return;
705 }
706
707 if (!IN_SET(t->answer_dnssec_result,
708 _DNSSEC_RESULT_INVALID, /* No DNSSEC validation enabled */
709 DNSSEC_VALIDATED, /* Answer is signed and validated successfully */
710 DNSSEC_UNSIGNED, /* Answer is right-fully unsigned */
711 DNSSEC_INCOMPATIBLE_SERVER)) { /* Server does not do DNSSEC (Yay, we are downgrade attack vulnerable!) */
712 dns_transaction_complete(t, DNS_TRANSACTION_DNSSEC_FAILED);
713 return;
714 }
715
716 if (t->answer_dnssec_result == DNSSEC_INCOMPATIBLE_SERVER)
717 dns_server_warn_downgrade(t->server);
718
719 dns_transaction_cache_answer(t);
720
721 if (t->answer_rcode == DNS_RCODE_SUCCESS)
722 dns_transaction_complete(t, DNS_TRANSACTION_SUCCESS);
723 else
724 dns_transaction_complete(t, DNS_TRANSACTION_RCODE_FAILURE);
725
726 return;
727
728 fail:
729 t->answer_errno = -r;
730 dns_transaction_complete(t, DNS_TRANSACTION_ERRNO);
731 }
732
733 static int dns_transaction_has_positive_answer(DnsTransaction *t, DnsAnswerFlags *flags) {
734 int r;
735
736 assert(t);
737
738 /* Checks whether the answer is positive, i.e. either a direct
739 * answer to the question, or a CNAME/DNAME for it */
740
741 r = dns_answer_match_key(t->answer, t->key, flags);
742 if (r != 0)
743 return r;
744
745 r = dns_answer_find_cname_or_dname(t->answer, t->key, NULL, flags);
746 if (r != 0)
747 return r;
748
749 return false;
750 }
751
752 static int dns_transaction_fix_rcode(DnsTransaction *t) {
753 int r;
754
755 assert(t);
756
757 /* Fix up the RCODE to SUCCESS if we get at least one matching RR in a response. Note that this contradicts the
758 * DNS RFCs a bit. Specifically, RFC 6604 Section 3 clarifies that the RCODE shall say something about a
759 * CNAME/DNAME chain element coming after the last chain element contained in the message, and not the first
760 * one included. However, it also indicates that not all DNS servers implement this correctly. Moreover, when
761 * using DNSSEC we usually only can prove the first element of a CNAME/DNAME chain anyway, hence let's settle
762 * on always processing the RCODE as referring to the immediate look-up we do, i.e. the first element of a
763 * CNAME/DNAME chain. This way, we uniformly handle CNAME/DNAME chains, regardless if the DNS server
764 * incorrectly implements RCODE, whether DNSSEC is in use, or whether the DNS server only supplied us with an
765 * incomplete CNAME/DNAME chain.
766 *
767 * Or in other words: if we get at least one positive reply in a message we patch NXDOMAIN to become SUCCESS,
768 * and then rely on the CNAME chasing logic to figure out that there's actually a CNAME error with a new
769 * lookup. */
770
771 if (t->answer_rcode != DNS_RCODE_NXDOMAIN)
772 return 0;
773
774 r = dns_transaction_has_positive_answer(t, NULL);
775 if (r <= 0)
776 return r;
777
778 t->answer_rcode = DNS_RCODE_SUCCESS;
779 return 0;
780 }
781
782 void dns_transaction_process_reply(DnsTransaction *t, DnsPacket *p) {
783 usec_t ts;
784 int r;
785
786 assert(t);
787 assert(p);
788 assert(t->scope);
789 assert(t->scope->manager);
790
791 if (t->state != DNS_TRANSACTION_PENDING)
792 return;
793
794 /* Note that this call might invalidate the query. Callers
795 * should hence not attempt to access the query or transaction
796 * after calling this function. */
797
798 log_debug("Processing incoming packet on transaction %" PRIu16".", t->id);
799
800 switch (t->scope->protocol) {
801
802 case DNS_PROTOCOL_LLMNR:
803 assert(t->scope->link);
804
805 /* For LLMNR we will not accept any packets from other
806 * interfaces */
807
808 if (p->ifindex != t->scope->link->ifindex)
809 return;
810
811 if (p->family != t->scope->family)
812 return;
813
814 /* Tentative packets are not full responses but still
815 * useful for identifying uniqueness conflicts during
816 * probing. */
817 if (DNS_PACKET_LLMNR_T(p)) {
818 dns_transaction_tentative(t, p);
819 return;
820 }
821
822 break;
823
824 case DNS_PROTOCOL_MDNS:
825 assert(t->scope->link);
826
827 /* For mDNS we will not accept any packets from other interfaces */
828 if (p->ifindex != t->scope->link->ifindex)
829 return;
830
831 if (p->family != t->scope->family)
832 return;
833
834 break;
835
836 case DNS_PROTOCOL_DNS:
837 /* Note that we do not need to verify the
838 * addresses/port numbers of incoming traffic, as we
839 * invoked connect() on our UDP socket in which case
840 * the kernel already does the needed verification for
841 * us. */
842 break;
843
844 default:
845 assert_not_reached("Invalid DNS protocol.");
846 }
847
848 if (t->received != p) {
849 dns_packet_unref(t->received);
850 t->received = dns_packet_ref(p);
851 }
852
853 t->answer_source = DNS_TRANSACTION_NETWORK;
854
855 if (p->ipproto == IPPROTO_TCP) {
856 if (DNS_PACKET_TC(p)) {
857 /* Truncated via TCP? Somebody must be fucking with us */
858 dns_transaction_complete(t, DNS_TRANSACTION_INVALID_REPLY);
859 return;
860 }
861
862 if (DNS_PACKET_ID(p) != t->id) {
863 /* Not the reply to our query? Somebody must be fucking with us */
864 dns_transaction_complete(t, DNS_TRANSACTION_INVALID_REPLY);
865 return;
866 }
867 }
868
869 assert_se(sd_event_now(t->scope->manager->event, clock_boottime_or_monotonic(), &ts) >= 0);
870
871 switch (t->scope->protocol) {
872
873 case DNS_PROTOCOL_DNS:
874 assert(t->server);
875
876 if (IN_SET(DNS_PACKET_RCODE(p), DNS_RCODE_FORMERR, DNS_RCODE_SERVFAIL, DNS_RCODE_NOTIMP)) {
877
878 /* Request failed, immediately try again with reduced features */
879 log_debug("Server returned error: %s", dns_rcode_to_string(DNS_PACKET_RCODE(p)));
880
881 dns_server_packet_failed(t->server, t->current_feature_level);
882 dns_transaction_retry(t);
883 return;
884 } else if (DNS_PACKET_TC(p))
885 dns_server_packet_truncated(t->server, t->current_feature_level);
886
887 break;
888
889 case DNS_PROTOCOL_LLMNR:
890 case DNS_PROTOCOL_MDNS:
891 dns_scope_packet_received(t->scope, ts - t->start_usec);
892 break;
893
894 default:
895 assert_not_reached("Invalid DNS protocol.");
896 }
897
898 if (DNS_PACKET_TC(p)) {
899
900 /* Truncated packets for mDNS are not allowed. Give up immediately. */
901 if (t->scope->protocol == DNS_PROTOCOL_MDNS) {
902 dns_transaction_complete(t, DNS_TRANSACTION_INVALID_REPLY);
903 return;
904 }
905
906 log_debug("Reply truncated, retrying via TCP.");
907
908 /* Response was truncated, let's try again with good old TCP */
909 r = dns_transaction_open_tcp(t);
910 if (r == -ESRCH) {
911 /* No servers found? Damn! */
912 dns_transaction_complete(t, DNS_TRANSACTION_NO_SERVERS);
913 return;
914 }
915 if (r == -EOPNOTSUPP) {
916 /* Tried to ask for DNSSEC RRs, on a server that doesn't do DNSSEC */
917 dns_transaction_complete(t, DNS_TRANSACTION_RR_TYPE_UNSUPPORTED);
918 return;
919 }
920 if (r < 0) {
921 /* On LLMNR, if we cannot connect to the host,
922 * we immediately give up */
923 if (t->scope->protocol != DNS_PROTOCOL_DNS)
924 goto fail;
925
926 /* On DNS, couldn't send? Try immediately again, with a new server */
927 dns_transaction_retry(t);
928 }
929
930 return;
931 }
932
933 /* After the superficial checks, actually parse the message. */
934 r = dns_packet_extract(p);
935 if (r < 0) {
936 dns_transaction_complete(t, DNS_TRANSACTION_INVALID_REPLY);
937 return;
938 }
939
940 /* Report that the OPT RR was missing */
941 if (t->server) {
942 if (!p->opt)
943 dns_server_packet_bad_opt(t->server, t->current_feature_level);
944
945 dns_server_packet_received(t->server, p->ipproto, t->current_feature_level, ts - t->start_usec, p->size);
946 }
947
948 /* See if we know things we didn't know before that indicate we better restart the lookup immediately. */
949 r = dns_transaction_maybe_restart(t);
950 if (r < 0)
951 goto fail;
952 if (r > 0) /* Transaction got restarted... */
953 return;
954
955 if (IN_SET(t->scope->protocol, DNS_PROTOCOL_DNS, DNS_PROTOCOL_LLMNR)) {
956
957 /* Only consider responses with equivalent query section to the request */
958 r = dns_packet_is_reply_for(p, t->key);
959 if (r < 0)
960 goto fail;
961 if (r == 0) {
962 dns_transaction_complete(t, DNS_TRANSACTION_INVALID_REPLY);
963 return;
964 }
965
966 /* Install the answer as answer to the transaction */
967 dns_answer_unref(t->answer);
968 t->answer = dns_answer_ref(p->answer);
969 t->answer_rcode = DNS_PACKET_RCODE(p);
970 t->answer_dnssec_result = _DNSSEC_RESULT_INVALID;
971 t->answer_authenticated = false;
972
973 r = dns_transaction_fix_rcode(t);
974 if (r < 0)
975 goto fail;
976
977 /* Block GC while starting requests for additional DNSSEC RRs */
978 t->block_gc++;
979 r = dns_transaction_request_dnssec_keys(t);
980 t->block_gc--;
981
982 /* Maybe the transaction is ready for GC'ing now? If so, free it and return. */
983 if (!dns_transaction_gc(t))
984 return;
985
986 /* Requesting additional keys might have resulted in
987 * this transaction to fail, since the auxiliary
988 * request failed for some reason. If so, we are not
989 * in pending state anymore, and we should exit
990 * quickly. */
991 if (t->state != DNS_TRANSACTION_PENDING)
992 return;
993 if (r < 0)
994 goto fail;
995 if (r > 0) {
996 /* There are DNSSEC transactions pending now. Update the state accordingly. */
997 t->state = DNS_TRANSACTION_VALIDATING;
998 dns_transaction_close_connection(t);
999 dns_transaction_stop_timeout(t);
1000 return;
1001 }
1002 }
1003
1004 dns_transaction_process_dnssec(t);
1005 return;
1006
1007 fail:
1008 t->answer_errno = -r;
1009 dns_transaction_complete(t, DNS_TRANSACTION_ERRNO);
1010 }
1011
1012 static int on_dns_packet(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
1013 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
1014 DnsTransaction *t = userdata;
1015 int r;
1016
1017 assert(t);
1018 assert(t->scope);
1019
1020 r = manager_recv(t->scope->manager, fd, DNS_PROTOCOL_DNS, &p);
1021 if (ERRNO_IS_DISCONNECT(-r)) {
1022 usec_t usec;
1023
1024 /* UDP connection failure get reported via ICMP and then are possible delivered to us on the next
1025 * recvmsg(). Treat this like a lost packet. */
1026
1027 log_debug_errno(r, "Connection failure for DNS UDP packet: %m");
1028 assert_se(sd_event_now(t->scope->manager->event, clock_boottime_or_monotonic(), &usec) >= 0);
1029 dns_server_packet_lost(t->server, IPPROTO_UDP, t->current_feature_level, usec - t->start_usec);
1030
1031 dns_transaction_retry(t);
1032 return 0;
1033 }
1034 if (r < 0) {
1035 dns_transaction_complete(t, DNS_TRANSACTION_ERRNO);
1036 t->answer_errno = -r;
1037 return 0;
1038 }
1039
1040 r = dns_packet_validate_reply(p);
1041 if (r < 0) {
1042 log_debug_errno(r, "Received invalid DNS packet as response, ignoring: %m");
1043 return 0;
1044 }
1045 if (r == 0) {
1046 log_debug("Received inappropriate DNS packet as response, ignoring.");
1047 return 0;
1048 }
1049
1050 if (DNS_PACKET_ID(p) != t->id) {
1051 log_debug("Received packet with incorrect transaction ID, ignoring.");
1052 return 0;
1053 }
1054
1055 dns_transaction_process_reply(t, p);
1056 return 0;
1057 }
1058
1059 static int dns_transaction_emit_udp(DnsTransaction *t) {
1060 int r;
1061
1062 assert(t);
1063
1064 if (t->scope->protocol == DNS_PROTOCOL_DNS) {
1065
1066 r = dns_transaction_pick_server(t);
1067 if (r < 0)
1068 return r;
1069
1070 if (t->current_feature_level < DNS_SERVER_FEATURE_LEVEL_UDP)
1071 return -EAGAIN;
1072
1073 if (!dns_server_dnssec_supported(t->server) && dns_type_is_dnssec(t->key->type))
1074 return -EOPNOTSUPP;
1075
1076 if (r > 0 || t->dns_udp_fd < 0) { /* Server changed, or no connection yet. */
1077 int fd;
1078
1079 dns_transaction_close_connection(t);
1080
1081 fd = dns_scope_socket_udp(t->scope, t->server, 53);
1082 if (fd < 0)
1083 return fd;
1084
1085 r = sd_event_add_io(t->scope->manager->event, &t->dns_udp_event_source, fd, EPOLLIN, on_dns_packet, t);
1086 if (r < 0) {
1087 safe_close(fd);
1088 return r;
1089 }
1090
1091 (void) sd_event_source_set_description(t->dns_udp_event_source, "dns-transaction-udp");
1092 t->dns_udp_fd = fd;
1093 }
1094
1095 r = dns_server_adjust_opt(t->server, t->sent, t->current_feature_level);
1096 if (r < 0)
1097 return r;
1098 } else
1099 dns_transaction_close_connection(t);
1100
1101 r = dns_scope_emit_udp(t->scope, t->dns_udp_fd, t->sent);
1102 if (r < 0)
1103 return r;
1104
1105 dns_transaction_reset_answer(t);
1106
1107 return 0;
1108 }
1109
1110 static int on_transaction_timeout(sd_event_source *s, usec_t usec, void *userdata) {
1111 DnsTransaction *t = userdata;
1112
1113 assert(s);
1114 assert(t);
1115
1116 if (!t->initial_jitter_scheduled || t->initial_jitter_elapsed) {
1117 /* Timeout reached? Increase the timeout for the server used */
1118 switch (t->scope->protocol) {
1119
1120 case DNS_PROTOCOL_DNS:
1121 assert(t->server);
1122 dns_server_packet_lost(t->server, t->stream ? IPPROTO_TCP : IPPROTO_UDP, t->current_feature_level, usec - t->start_usec);
1123 break;
1124
1125 case DNS_PROTOCOL_LLMNR:
1126 case DNS_PROTOCOL_MDNS:
1127 dns_scope_packet_lost(t->scope, usec - t->start_usec);
1128 break;
1129
1130 default:
1131 assert_not_reached("Invalid DNS protocol.");
1132 }
1133
1134 if (t->initial_jitter_scheduled)
1135 t->initial_jitter_elapsed = true;
1136 }
1137
1138 log_debug("Timeout reached on transaction %" PRIu16 ".", t->id);
1139
1140 dns_transaction_retry(t);
1141 return 0;
1142 }
1143
1144 static usec_t transaction_get_resend_timeout(DnsTransaction *t) {
1145 assert(t);
1146 assert(t->scope);
1147
1148 switch (t->scope->protocol) {
1149
1150 case DNS_PROTOCOL_DNS:
1151 assert(t->server);
1152 return t->server->resend_timeout;
1153
1154 case DNS_PROTOCOL_MDNS:
1155 assert(t->n_attempts > 0);
1156 return (1 << (t->n_attempts - 1)) * USEC_PER_SEC;
1157
1158 case DNS_PROTOCOL_LLMNR:
1159 return t->scope->resend_timeout;
1160
1161 default:
1162 assert_not_reached("Invalid DNS protocol.");
1163 }
1164 }
1165
1166 static int dns_transaction_prepare(DnsTransaction *t, usec_t ts) {
1167 int r;
1168
1169 assert(t);
1170
1171 dns_transaction_stop_timeout(t);
1172
1173 r = dns_scope_network_good(t->scope);
1174 if (r < 0)
1175 return r;
1176 if (r == 0) {
1177 dns_transaction_complete(t, DNS_TRANSACTION_NETWORK_DOWN);
1178 return 0;
1179 }
1180
1181 if (t->n_attempts >= TRANSACTION_ATTEMPTS_MAX(t->scope->protocol)) {
1182 dns_transaction_complete(t, DNS_TRANSACTION_ATTEMPTS_MAX_REACHED);
1183 return 0;
1184 }
1185
1186 if (t->scope->protocol == DNS_PROTOCOL_LLMNR && t->tried_stream) {
1187 /* If we already tried via a stream, then we don't
1188 * retry on LLMNR. See RFC 4795, Section 2.7. */
1189 dns_transaction_complete(t, DNS_TRANSACTION_ATTEMPTS_MAX_REACHED);
1190 return 0;
1191 }
1192
1193 t->n_attempts++;
1194 t->start_usec = ts;
1195
1196 dns_transaction_reset_answer(t);
1197 dns_transaction_flush_dnssec_transactions(t);
1198
1199 /* Check the trust anchor. Do so only on classic DNS, since DNSSEC does not apply otherwise. */
1200 if (t->scope->protocol == DNS_PROTOCOL_DNS) {
1201 r = dns_trust_anchor_lookup_positive(&t->scope->manager->trust_anchor, t->key, &t->answer);
1202 if (r < 0)
1203 return r;
1204 if (r > 0) {
1205 t->answer_rcode = DNS_RCODE_SUCCESS;
1206 t->answer_source = DNS_TRANSACTION_TRUST_ANCHOR;
1207 t->answer_authenticated = true;
1208 dns_transaction_complete(t, DNS_TRANSACTION_SUCCESS);
1209 return 0;
1210 }
1211
1212 if (dns_name_is_root(DNS_RESOURCE_KEY_NAME(t->key)) &&
1213 t->key->type == DNS_TYPE_DS) {
1214
1215 /* Hmm, this is a request for the root DS? A
1216 * DS RR doesn't exist in the root zone, and
1217 * if our trust anchor didn't know it either,
1218 * this means we cannot do any DNSSEC logic
1219 * anymore. */
1220
1221 if (t->scope->dnssec_mode == DNSSEC_ALLOW_DOWNGRADE) {
1222 /* We are in downgrade mode. In this
1223 * case, synthesize an unsigned empty
1224 * response, so that the any lookup
1225 * depending on this one can continue
1226 * assuming there was no DS, and hence
1227 * the root zone was unsigned. */
1228
1229 t->answer_rcode = DNS_RCODE_SUCCESS;
1230 t->answer_source = DNS_TRANSACTION_TRUST_ANCHOR;
1231 t->answer_authenticated = false;
1232 dns_transaction_complete(t, DNS_TRANSACTION_SUCCESS);
1233 } else
1234 /* If we are not in downgrade mode,
1235 * then fail the lookup, because we
1236 * cannot reasonably answer it. There
1237 * might be DS RRs, but we don't know
1238 * them, and the DNS server won't tell
1239 * them to us (and even if it would,
1240 * we couldn't validate it and trust
1241 * it). */
1242 dns_transaction_complete(t, DNS_TRANSACTION_NO_TRUST_ANCHOR);
1243
1244 return 0;
1245 }
1246 }
1247
1248 /* Check the zone, but only if this transaction is not used
1249 * for probing or verifying a zone item. */
1250 if (set_isempty(t->notify_zone_items)) {
1251
1252 r = dns_zone_lookup(&t->scope->zone, t->key, &t->answer, NULL, NULL);
1253 if (r < 0)
1254 return r;
1255 if (r > 0) {
1256 t->answer_rcode = DNS_RCODE_SUCCESS;
1257 t->answer_source = DNS_TRANSACTION_ZONE;
1258 t->answer_authenticated = true;
1259 dns_transaction_complete(t, DNS_TRANSACTION_SUCCESS);
1260 return 0;
1261 }
1262 }
1263
1264 /* Check the cache, but only if this transaction is not used
1265 * for probing or verifying a zone item. */
1266 if (set_isempty(t->notify_zone_items)) {
1267
1268 /* Before trying the cache, let's make sure we figured out a
1269 * server to use. Should this cause a change of server this
1270 * might flush the cache. */
1271 dns_scope_get_dns_server(t->scope);
1272
1273 /* Let's then prune all outdated entries */
1274 dns_cache_prune(&t->scope->cache);
1275
1276 r = dns_cache_lookup(&t->scope->cache, t->key, &t->answer_rcode, &t->answer, &t->answer_authenticated);
1277 if (r < 0)
1278 return r;
1279 if (r > 0) {
1280 t->answer_source = DNS_TRANSACTION_CACHE;
1281 if (t->answer_rcode == DNS_RCODE_SUCCESS)
1282 dns_transaction_complete(t, DNS_TRANSACTION_SUCCESS);
1283 else
1284 dns_transaction_complete(t, DNS_TRANSACTION_RCODE_FAILURE);
1285 return 0;
1286 }
1287 }
1288
1289 return 1;
1290 }
1291
1292 static int dns_transaction_make_packet_mdns(DnsTransaction *t) {
1293
1294 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
1295 bool add_known_answers = false;
1296 DnsTransaction *other;
1297 unsigned qdcount;
1298 usec_t ts;
1299 int r;
1300
1301 assert(t);
1302 assert(t->scope->protocol == DNS_PROTOCOL_MDNS);
1303
1304 /* Discard any previously prepared packet, so we can start over and coalesce again */
1305 t->sent = dns_packet_unref(t->sent);
1306
1307 r = dns_packet_new_query(&p, t->scope->protocol, 0, false);
1308 if (r < 0)
1309 return r;
1310
1311 r = dns_packet_append_key(p, t->key, NULL);
1312 if (r < 0)
1313 return r;
1314
1315 qdcount = 1;
1316
1317 if (dns_key_is_shared(t->key))
1318 add_known_answers = true;
1319
1320 /*
1321 * For mDNS, we want to coalesce as many open queries in pending transactions into one single
1322 * query packet on the wire as possible. To achieve that, we iterate through all pending transactions
1323 * in our current scope, and see whether their timing contraints allow them to be sent.
1324 */
1325
1326 assert_se(sd_event_now(t->scope->manager->event, clock_boottime_or_monotonic(), &ts) >= 0);
1327
1328 LIST_FOREACH(transactions_by_scope, other, t->scope->transactions) {
1329
1330 /* Skip ourselves */
1331 if (other == t)
1332 continue;
1333
1334 if (other->state != DNS_TRANSACTION_PENDING)
1335 continue;
1336
1337 if (other->next_attempt_after > ts)
1338 continue;
1339
1340 if (qdcount >= UINT16_MAX)
1341 break;
1342
1343 r = dns_packet_append_key(p, other->key, NULL);
1344
1345 /*
1346 * If we can't stuff more questions into the packet, just give up.
1347 * One of the 'other' transactions will fire later and take care of the rest.
1348 */
1349 if (r == -EMSGSIZE)
1350 break;
1351
1352 if (r < 0)
1353 return r;
1354
1355 r = dns_transaction_prepare(other, ts);
1356 if (r <= 0)
1357 continue;
1358
1359 ts += transaction_get_resend_timeout(other);
1360
1361 r = sd_event_add_time(
1362 other->scope->manager->event,
1363 &other->timeout_event_source,
1364 clock_boottime_or_monotonic(),
1365 ts, 0,
1366 on_transaction_timeout, other);
1367 if (r < 0)
1368 return r;
1369
1370 (void) sd_event_source_set_description(t->timeout_event_source, "dns-transaction-timeout");
1371
1372 other->state = DNS_TRANSACTION_PENDING;
1373 other->next_attempt_after = ts;
1374
1375 qdcount ++;
1376
1377 if (dns_key_is_shared(other->key))
1378 add_known_answers = true;
1379 }
1380
1381 DNS_PACKET_HEADER(p)->qdcount = htobe16(qdcount);
1382
1383 /* Append known answer section if we're asking for any shared record */
1384 if (add_known_answers) {
1385 r = dns_cache_export_shared_to_packet(&t->scope->cache, p);
1386 if (r < 0)
1387 return r;
1388 }
1389
1390 t->sent = p;
1391 p = NULL;
1392
1393 return 0;
1394 }
1395
1396 static int dns_transaction_make_packet(DnsTransaction *t) {
1397 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
1398 int r;
1399
1400 assert(t);
1401
1402 if (t->scope->protocol == DNS_PROTOCOL_MDNS)
1403 return dns_transaction_make_packet_mdns(t);
1404
1405 if (t->sent)
1406 return 0;
1407
1408 r = dns_packet_new_query(&p, t->scope->protocol, 0, t->scope->dnssec_mode != DNSSEC_NO);
1409 if (r < 0)
1410 return r;
1411
1412 r = dns_packet_append_key(p, t->key, NULL);
1413 if (r < 0)
1414 return r;
1415
1416 DNS_PACKET_HEADER(p)->qdcount = htobe16(1);
1417 DNS_PACKET_HEADER(p)->id = t->id;
1418
1419 t->sent = p;
1420 p = NULL;
1421
1422 return 0;
1423 }
1424
1425 int dns_transaction_go(DnsTransaction *t) {
1426 usec_t ts;
1427 int r;
1428
1429 assert(t);
1430
1431 assert_se(sd_event_now(t->scope->manager->event, clock_boottime_or_monotonic(), &ts) >= 0);
1432
1433 r = dns_transaction_prepare(t, ts);
1434 if (r <= 0)
1435 return r;
1436
1437 log_debug("Excercising transaction %" PRIu16 " for <%s> on scope %s on %s/%s.",
1438 t->id,
1439 dns_transaction_key_string(t),
1440 dns_protocol_to_string(t->scope->protocol),
1441 t->scope->link ? t->scope->link->name : "*",
1442 t->scope->family == AF_UNSPEC ? "*" : af_to_name(t->scope->family));
1443
1444 if (!t->initial_jitter_scheduled &&
1445 (t->scope->protocol == DNS_PROTOCOL_LLMNR ||
1446 t->scope->protocol == DNS_PROTOCOL_MDNS)) {
1447 usec_t jitter, accuracy;
1448
1449 /* RFC 4795 Section 2.7 suggests all queries should be
1450 * delayed by a random time from 0 to JITTER_INTERVAL. */
1451
1452 t->initial_jitter_scheduled = true;
1453
1454 random_bytes(&jitter, sizeof(jitter));
1455
1456 switch (t->scope->protocol) {
1457
1458 case DNS_PROTOCOL_LLMNR:
1459 jitter %= LLMNR_JITTER_INTERVAL_USEC;
1460 accuracy = LLMNR_JITTER_INTERVAL_USEC;
1461 break;
1462
1463 case DNS_PROTOCOL_MDNS:
1464 jitter %= MDNS_JITTER_RANGE_USEC;
1465 jitter += MDNS_JITTER_MIN_USEC;
1466 accuracy = MDNS_JITTER_RANGE_USEC;
1467 break;
1468 default:
1469 assert_not_reached("bad protocol");
1470 }
1471
1472 r = sd_event_add_time(
1473 t->scope->manager->event,
1474 &t->timeout_event_source,
1475 clock_boottime_or_monotonic(),
1476 ts + jitter, accuracy,
1477 on_transaction_timeout, t);
1478 if (r < 0)
1479 return r;
1480
1481 (void) sd_event_source_set_description(t->timeout_event_source, "dns-transaction-timeout");
1482
1483 t->n_attempts = 0;
1484 t->next_attempt_after = ts;
1485 t->state = DNS_TRANSACTION_PENDING;
1486
1487 log_debug("Delaying %s transaction for " USEC_FMT "us.", dns_protocol_to_string(t->scope->protocol), jitter);
1488 return 0;
1489 }
1490
1491 /* Otherwise, we need to ask the network */
1492 r = dns_transaction_make_packet(t);
1493 if (r < 0)
1494 return r;
1495
1496 if (t->scope->protocol == DNS_PROTOCOL_LLMNR &&
1497 (dns_name_endswith(DNS_RESOURCE_KEY_NAME(t->key), "in-addr.arpa") > 0 ||
1498 dns_name_endswith(DNS_RESOURCE_KEY_NAME(t->key), "ip6.arpa") > 0)) {
1499
1500 /* RFC 4795, Section 2.4. says reverse lookups shall
1501 * always be made via TCP on LLMNR */
1502 r = dns_transaction_open_tcp(t);
1503 } else {
1504 /* Try via UDP, and if that fails due to large size or lack of
1505 * support try via TCP */
1506 r = dns_transaction_emit_udp(t);
1507 if (r == -EMSGSIZE)
1508 log_debug("Sending query via TCP since it is too large.");
1509 if (r == -EAGAIN)
1510 log_debug("Sending query via TCP since server doesn't support UDP.");
1511 if (r == -EMSGSIZE || r == -EAGAIN)
1512 r = dns_transaction_open_tcp(t);
1513 }
1514
1515 if (r == -ESRCH) {
1516 /* No servers to send this to? */
1517 dns_transaction_complete(t, DNS_TRANSACTION_NO_SERVERS);
1518 return 0;
1519 }
1520 if (r == -EOPNOTSUPP) {
1521 /* Tried to ask for DNSSEC RRs, on a server that doesn't do DNSSEC */
1522 dns_transaction_complete(t, DNS_TRANSACTION_RR_TYPE_UNSUPPORTED);
1523 return 0;
1524 }
1525 if (t->scope->protocol == DNS_PROTOCOL_LLMNR && ERRNO_IS_DISCONNECT(-r)) {
1526 /* On LLMNR, if we cannot connect to a host via TCP when doing reverse lookups. This means we cannot
1527 * answer this request with this protocol. */
1528 dns_transaction_complete(t, DNS_TRANSACTION_NOT_FOUND);
1529 return 0;
1530 }
1531 if (r < 0) {
1532 if (t->scope->protocol != DNS_PROTOCOL_DNS)
1533 return r;
1534
1535 /* Couldn't send? Try immediately again, with a new server */
1536 dns_scope_next_dns_server(t->scope);
1537
1538 return dns_transaction_go(t);
1539 }
1540
1541 ts += transaction_get_resend_timeout(t);
1542
1543 r = sd_event_add_time(
1544 t->scope->manager->event,
1545 &t->timeout_event_source,
1546 clock_boottime_or_monotonic(),
1547 ts, 0,
1548 on_transaction_timeout, t);
1549 if (r < 0)
1550 return r;
1551
1552 (void) sd_event_source_set_description(t->timeout_event_source, "dns-transaction-timeout");
1553
1554 t->state = DNS_TRANSACTION_PENDING;
1555 t->next_attempt_after = ts;
1556
1557 return 1;
1558 }
1559
1560 static int dns_transaction_find_cyclic(DnsTransaction *t, DnsTransaction *aux) {
1561 DnsTransaction *n;
1562 Iterator i;
1563 int r;
1564
1565 assert(t);
1566 assert(aux);
1567
1568 /* Try to find cyclic dependencies between transaction objects */
1569
1570 if (t == aux)
1571 return 1;
1572
1573 SET_FOREACH(n, aux->dnssec_transactions, i) {
1574 r = dns_transaction_find_cyclic(t, n);
1575 if (r != 0)
1576 return r;
1577 }
1578
1579 return 0;
1580 }
1581
1582 static int dns_transaction_add_dnssec_transaction(DnsTransaction *t, DnsResourceKey *key, DnsTransaction **ret) {
1583 DnsTransaction *aux;
1584 int r;
1585
1586 assert(t);
1587 assert(ret);
1588 assert(key);
1589
1590 aux = dns_scope_find_transaction(t->scope, key, true);
1591 if (!aux) {
1592 r = dns_transaction_new(&aux, t->scope, key);
1593 if (r < 0)
1594 return r;
1595 } else {
1596 if (set_contains(t->dnssec_transactions, aux)) {
1597 *ret = aux;
1598 return 0;
1599 }
1600
1601 r = dns_transaction_find_cyclic(t, aux);
1602 if (r < 0)
1603 return r;
1604 if (r > 0) {
1605 log_debug("Detected potential cyclic dependency, refusing to add transaction %" PRIu16 " (%s) as dependency for %" PRIu16 " (%s).",
1606 aux->id,
1607 strna(dns_transaction_key_string(aux)),
1608 t->id,
1609 strna(dns_transaction_key_string(t)));
1610 return -ELOOP;
1611 }
1612 }
1613
1614 r = set_ensure_allocated(&t->dnssec_transactions, NULL);
1615 if (r < 0)
1616 goto gc;
1617
1618 r = set_ensure_allocated(&aux->notify_transactions, NULL);
1619 if (r < 0)
1620 goto gc;
1621
1622 r = set_put(t->dnssec_transactions, aux);
1623 if (r < 0)
1624 goto gc;
1625
1626 r = set_put(aux->notify_transactions, t);
1627 if (r < 0) {
1628 (void) set_remove(t->dnssec_transactions, aux);
1629 goto gc;
1630 }
1631
1632 *ret = aux;
1633 return 1;
1634
1635 gc:
1636 dns_transaction_gc(aux);
1637 return r;
1638 }
1639
1640 static int dns_transaction_request_dnssec_rr(DnsTransaction *t, DnsResourceKey *key) {
1641 _cleanup_(dns_answer_unrefp) DnsAnswer *a = NULL;
1642 DnsTransaction *aux;
1643 int r;
1644
1645 assert(t);
1646 assert(key);
1647
1648 /* Try to get the data from the trust anchor */
1649 r = dns_trust_anchor_lookup_positive(&t->scope->manager->trust_anchor, key, &a);
1650 if (r < 0)
1651 return r;
1652 if (r > 0) {
1653 r = dns_answer_extend(&t->validated_keys, a);
1654 if (r < 0)
1655 return r;
1656
1657 return 0;
1658 }
1659
1660 /* This didn't work, ask for it via the network/cache then. */
1661 r = dns_transaction_add_dnssec_transaction(t, key, &aux);
1662 if (r == -ELOOP) /* This would result in a cyclic dependency */
1663 return 0;
1664 if (r < 0)
1665 return r;
1666
1667 if (aux->state == DNS_TRANSACTION_NULL) {
1668 r = dns_transaction_go(aux);
1669 if (r < 0)
1670 return r;
1671 }
1672
1673 return 1;
1674 }
1675
1676 static int dns_transaction_negative_trust_anchor_lookup(DnsTransaction *t, const char *name) {
1677 int r;
1678
1679 assert(t);
1680
1681 /* Check whether the specified name is in the the NTA
1682 * database, either in the global one, or the link-local
1683 * one. */
1684
1685 r = dns_trust_anchor_lookup_negative(&t->scope->manager->trust_anchor, name);
1686 if (r != 0)
1687 return r;
1688
1689 if (!t->scope->link)
1690 return 0;
1691
1692 return set_contains(t->scope->link->dnssec_negative_trust_anchors, name);
1693 }
1694
1695 static int dns_transaction_has_unsigned_negative_answer(DnsTransaction *t) {
1696 int r;
1697
1698 assert(t);
1699
1700 /* Checks whether the answer is negative, and lacks NSEC/NSEC3
1701 * RRs to prove it */
1702
1703 r = dns_transaction_has_positive_answer(t, NULL);
1704 if (r < 0)
1705 return r;
1706 if (r > 0)
1707 return false;
1708
1709 /* Is this key explicitly listed as a negative trust anchor?
1710 * If so, it's nothing we need to care about */
1711 r = dns_transaction_negative_trust_anchor_lookup(t, DNS_RESOURCE_KEY_NAME(t->key));
1712 if (r < 0)
1713 return r;
1714 if (r > 0)
1715 return false;
1716
1717 /* The answer does not contain any RRs that match to the
1718 * question. If so, let's see if there are any NSEC/NSEC3 RRs
1719 * included. If not, the answer is unsigned. */
1720
1721 r = dns_answer_contains_nsec_or_nsec3(t->answer);
1722 if (r < 0)
1723 return r;
1724 if (r > 0)
1725 return false;
1726
1727 return true;
1728 }
1729
1730 static int dns_transaction_is_primary_response(DnsTransaction *t, DnsResourceRecord *rr) {
1731 int r;
1732
1733 assert(t);
1734 assert(rr);
1735
1736 /* Check if the specified RR is the "primary" response,
1737 * i.e. either matches the question precisely or is a
1738 * CNAME/DNAME for it. */
1739
1740 r = dns_resource_key_match_rr(t->key, rr, NULL);
1741 if (r != 0)
1742 return r;
1743
1744 return dns_resource_key_match_cname_or_dname(t->key, rr->key, NULL);
1745 }
1746
1747 static bool dns_transaction_dnssec_supported(DnsTransaction *t) {
1748 assert(t);
1749
1750 /* Checks whether our transaction's DNS server is assumed to be compatible with DNSSEC. Returns false as soon
1751 * as we changed our mind about a server, and now believe it is incompatible with DNSSEC. */
1752
1753 if (t->scope->protocol != DNS_PROTOCOL_DNS)
1754 return false;
1755
1756 /* If we have picked no server, then we are working from the cache or some other source, and DNSSEC might well
1757 * be supported, hence return true. */
1758 if (!t->server)
1759 return true;
1760
1761 if (t->current_feature_level < DNS_SERVER_FEATURE_LEVEL_DO)
1762 return false;
1763
1764 return dns_server_dnssec_supported(t->server);
1765 }
1766
1767 static bool dns_transaction_dnssec_supported_full(DnsTransaction *t) {
1768 DnsTransaction *dt;
1769 Iterator i;
1770
1771 assert(t);
1772
1773 /* Checks whether our transaction our any of the auxiliary transactions couldn't do DNSSEC. */
1774
1775 if (!dns_transaction_dnssec_supported(t))
1776 return false;
1777
1778 SET_FOREACH(dt, t->dnssec_transactions, i)
1779 if (!dns_transaction_dnssec_supported(dt))
1780 return false;
1781
1782 return true;
1783 }
1784
1785 int dns_transaction_request_dnssec_keys(DnsTransaction *t) {
1786 DnsResourceRecord *rr;
1787
1788 int r;
1789
1790 assert(t);
1791
1792 /*
1793 * Retrieve all auxiliary RRs for the answer we got, so that
1794 * we can verify signatures or prove that RRs are rightfully
1795 * unsigned. Specifically:
1796 *
1797 * - For RRSIG we get the matching DNSKEY
1798 * - For DNSKEY we get the matching DS
1799 * - For unsigned SOA/NS we get the matching DS
1800 * - For unsigned CNAME/DNAME/DS we get the parent SOA RR
1801 * - For other unsigned RRs we get the matching SOA RR
1802 * - For SOA/NS/DS queries with no matching response RRs, and no NSEC/NSEC3, the parent's SOA RR
1803 * - For other queries with no matching response RRs, and no NSEC/NSEC3, the SOA RR
1804 */
1805
1806 if (t->scope->dnssec_mode == DNSSEC_NO)
1807 return 0;
1808 if (t->answer_source != DNS_TRANSACTION_NETWORK)
1809 return 0; /* We only need to validate stuff from the network */
1810 if (!dns_transaction_dnssec_supported(t))
1811 return 0; /* If we can't do DNSSEC anyway there's no point in geting the auxiliary RRs */
1812
1813 DNS_ANSWER_FOREACH(rr, t->answer) {
1814
1815 if (dns_type_is_pseudo(rr->key->type))
1816 continue;
1817
1818 /* If this RR is in the negative trust anchor, we don't need to validate it. */
1819 r = dns_transaction_negative_trust_anchor_lookup(t, DNS_RESOURCE_KEY_NAME(rr->key));
1820 if (r < 0)
1821 return r;
1822 if (r > 0)
1823 continue;
1824
1825 switch (rr->key->type) {
1826
1827 case DNS_TYPE_RRSIG: {
1828 /* For each RRSIG we request the matching DNSKEY */
1829 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *dnskey = NULL;
1830
1831 /* If this RRSIG is about a DNSKEY RR and the
1832 * signer is the same as the owner, then we
1833 * already have the DNSKEY, and we don't have
1834 * to look for more. */
1835 if (rr->rrsig.type_covered == DNS_TYPE_DNSKEY) {
1836 r = dns_name_equal(rr->rrsig.signer, DNS_RESOURCE_KEY_NAME(rr->key));
1837 if (r < 0)
1838 return r;
1839 if (r > 0)
1840 continue;
1841 }
1842
1843 /* If the signer is not a parent of our
1844 * original query, then this is about an
1845 * auxiliary RRset, but not anything we asked
1846 * for. In this case we aren't interested,
1847 * because we don't want to request additional
1848 * RRs for stuff we didn't really ask for, and
1849 * also to avoid request loops, where
1850 * additional RRs from one transaction result
1851 * in another transaction whose additonal RRs
1852 * point back to the original transaction, and
1853 * we deadlock. */
1854 r = dns_name_endswith(DNS_RESOURCE_KEY_NAME(t->key), rr->rrsig.signer);
1855 if (r < 0)
1856 return r;
1857 if (r == 0)
1858 continue;
1859
1860 dnskey = dns_resource_key_new(rr->key->class, DNS_TYPE_DNSKEY, rr->rrsig.signer);
1861 if (!dnskey)
1862 return -ENOMEM;
1863
1864 log_debug("Requesting DNSKEY to validate transaction %" PRIu16" (%s, RRSIG with key tag: %" PRIu16 ").", t->id, DNS_RESOURCE_KEY_NAME(rr->key), rr->rrsig.key_tag);
1865 r = dns_transaction_request_dnssec_rr(t, dnskey);
1866 if (r < 0)
1867 return r;
1868 break;
1869 }
1870
1871 case DNS_TYPE_DNSKEY: {
1872 /* For each DNSKEY we request the matching DS */
1873 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *ds = NULL;
1874
1875 /* If the DNSKEY we are looking at is not for
1876 * zone we are interested in, nor any of its
1877 * parents, we aren't interested, and don't
1878 * request it. After all, we don't want to end
1879 * up in request loops, and want to keep
1880 * additional traffic down. */
1881
1882 r = dns_name_endswith(DNS_RESOURCE_KEY_NAME(t->key), DNS_RESOURCE_KEY_NAME(rr->key));
1883 if (r < 0)
1884 return r;
1885 if (r == 0)
1886 continue;
1887
1888 ds = dns_resource_key_new(rr->key->class, DNS_TYPE_DS, DNS_RESOURCE_KEY_NAME(rr->key));
1889 if (!ds)
1890 return -ENOMEM;
1891
1892 log_debug("Requesting DS to validate transaction %" PRIu16" (%s, DNSKEY with key tag: %" PRIu16 ").", t->id, DNS_RESOURCE_KEY_NAME(rr->key), dnssec_keytag(rr, false));
1893 r = dns_transaction_request_dnssec_rr(t, ds);
1894 if (r < 0)
1895 return r;
1896
1897 break;
1898 }
1899
1900 case DNS_TYPE_SOA:
1901 case DNS_TYPE_NS: {
1902 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *ds = NULL;
1903
1904 /* For an unsigned SOA or NS, try to acquire
1905 * the matching DS RR, as we are at a zone cut
1906 * then, and whether a DS exists tells us
1907 * whether the zone is signed. Do so only if
1908 * this RR matches our original question,
1909 * however. */
1910
1911 r = dns_resource_key_match_rr(t->key, rr, NULL);
1912 if (r < 0)
1913 return r;
1914 if (r == 0)
1915 continue;
1916
1917 r = dnssec_has_rrsig(t->answer, rr->key);
1918 if (r < 0)
1919 return r;
1920 if (r > 0)
1921 continue;
1922
1923 ds = dns_resource_key_new(rr->key->class, DNS_TYPE_DS, DNS_RESOURCE_KEY_NAME(rr->key));
1924 if (!ds)
1925 return -ENOMEM;
1926
1927 log_debug("Requesting DS to validate transaction %" PRIu16 " (%s, unsigned SOA/NS RRset).", t->id, DNS_RESOURCE_KEY_NAME(rr->key));
1928 r = dns_transaction_request_dnssec_rr(t, ds);
1929 if (r < 0)
1930 return r;
1931
1932 break;
1933 }
1934
1935 case DNS_TYPE_DS:
1936 case DNS_TYPE_CNAME:
1937 case DNS_TYPE_DNAME: {
1938 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *soa = NULL;
1939 const char *name;
1940
1941 /* CNAMEs and DNAMEs cannot be located at a
1942 * zone apex, hence ask for the parent SOA for
1943 * unsigned CNAME/DNAME RRs, maybe that's the
1944 * apex. But do all that only if this is
1945 * actually a response to our original
1946 * question.
1947 *
1948 * Similar for DS RRs, which are signed when
1949 * the parent SOA is signed. */
1950
1951 r = dns_transaction_is_primary_response(t, rr);
1952 if (r < 0)
1953 return r;
1954 if (r == 0)
1955 continue;
1956
1957 r = dnssec_has_rrsig(t->answer, rr->key);
1958 if (r < 0)
1959 return r;
1960 if (r > 0)
1961 continue;
1962
1963 r = dns_answer_has_dname_for_cname(t->answer, rr);
1964 if (r < 0)
1965 return r;
1966 if (r > 0)
1967 continue;
1968
1969 name = DNS_RESOURCE_KEY_NAME(rr->key);
1970 r = dns_name_parent(&name);
1971 if (r < 0)
1972 return r;
1973 if (r == 0)
1974 continue;
1975
1976 soa = dns_resource_key_new(rr->key->class, DNS_TYPE_SOA, name);
1977 if (!soa)
1978 return -ENOMEM;
1979
1980 log_debug("Requesting parent SOA to validate transaction %" PRIu16 " (%s, unsigned CNAME/DNAME/DS RRset).", t->id, DNS_RESOURCE_KEY_NAME(rr->key));
1981 r = dns_transaction_request_dnssec_rr(t, soa);
1982 if (r < 0)
1983 return r;
1984
1985 break;
1986 }
1987
1988 default: {
1989 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *soa = NULL;
1990
1991 /* For other unsigned RRsets (including
1992 * NSEC/NSEC3!), look for proof the zone is
1993 * unsigned, by requesting the SOA RR of the
1994 * zone. However, do so only if they are
1995 * directly relevant to our original
1996 * question. */
1997
1998 r = dns_transaction_is_primary_response(t, rr);
1999 if (r < 0)
2000 return r;
2001 if (r == 0)
2002 continue;
2003
2004 r = dnssec_has_rrsig(t->answer, rr->key);
2005 if (r < 0)
2006 return r;
2007 if (r > 0)
2008 continue;
2009
2010 soa = dns_resource_key_new(rr->key->class, DNS_TYPE_SOA, DNS_RESOURCE_KEY_NAME(rr->key));
2011 if (!soa)
2012 return -ENOMEM;
2013
2014 log_debug("Requesting SOA to validate transaction %" PRIu16 " (%s, unsigned non-SOA/NS RRset <%s>).", t->id, DNS_RESOURCE_KEY_NAME(rr->key), dns_resource_record_to_string(rr));
2015 r = dns_transaction_request_dnssec_rr(t, soa);
2016 if (r < 0)
2017 return r;
2018 break;
2019 }}
2020 }
2021
2022 /* Above, we requested everything necessary to validate what
2023 * we got. Now, let's request what we need to validate what we
2024 * didn't get... */
2025
2026 r = dns_transaction_has_unsigned_negative_answer(t);
2027 if (r < 0)
2028 return r;
2029 if (r > 0) {
2030 const char *name;
2031
2032 name = DNS_RESOURCE_KEY_NAME(t->key);
2033
2034 /* If this was a SOA or NS request, then this
2035 * indicates that we are not at a zone apex, hence ask
2036 * the parent name instead. If this was a DS request,
2037 * then it's signed when the parent zone is signed,
2038 * hence ask the parent in that case, too. */
2039
2040 if (IN_SET(t->key->type, DNS_TYPE_SOA, DNS_TYPE_NS, DNS_TYPE_DS)) {
2041 r = dns_name_parent(&name);
2042 if (r < 0)
2043 return r;
2044 if (r > 0)
2045 log_debug("Requesting parent SOA to validate transaction %" PRIu16 " (%s, unsigned empty SOA/NS/DS response).", t->id, DNS_RESOURCE_KEY_NAME(t->key));
2046 else
2047 name = NULL;
2048 } else
2049 log_debug("Requesting SOA to validate transaction %" PRIu16 " (%s, unsigned empty non-SOA/NS/DS response).", t->id, DNS_RESOURCE_KEY_NAME(t->key));
2050
2051 if (name) {
2052 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *soa = NULL;
2053
2054 soa = dns_resource_key_new(t->key->class, DNS_TYPE_SOA, name);
2055 if (!soa)
2056 return -ENOMEM;
2057
2058 r = dns_transaction_request_dnssec_rr(t, soa);
2059 if (r < 0)
2060 return r;
2061 }
2062 }
2063
2064 return dns_transaction_dnssec_is_live(t);
2065 }
2066
2067 void dns_transaction_notify(DnsTransaction *t, DnsTransaction *source) {
2068 assert(t);
2069 assert(source);
2070
2071 /* Invoked whenever any of our auxiliary DNSSEC transactions completed its work. If the state is still PENDING,
2072 we are still in the loop that adds further DNSSEC transactions, hence don't check if we are ready yet. If
2073 the state is VALIDATING however, we should check if we are complete now. */
2074
2075 if (t->state == DNS_TRANSACTION_VALIDATING)
2076 dns_transaction_process_dnssec(t);
2077 }
2078
2079 static int dns_transaction_validate_dnskey_by_ds(DnsTransaction *t) {
2080 DnsResourceRecord *rr;
2081 int ifindex, r;
2082
2083 assert(t);
2084
2085 /* Add all DNSKEY RRs from the answer that are validated by DS
2086 * RRs from the list of validated keys to the list of
2087 * validated keys. */
2088
2089 DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, t->answer) {
2090
2091 r = dnssec_verify_dnskey_by_ds_search(rr, t->validated_keys);
2092 if (r < 0)
2093 return r;
2094 if (r == 0)
2095 continue;
2096
2097 /* If so, the DNSKEY is validated too. */
2098 r = dns_answer_add_extend(&t->validated_keys, rr, ifindex, DNS_ANSWER_AUTHENTICATED);
2099 if (r < 0)
2100 return r;
2101 }
2102
2103 return 0;
2104 }
2105
2106 static int dns_transaction_requires_rrsig(DnsTransaction *t, DnsResourceRecord *rr) {
2107 int r;
2108
2109 assert(t);
2110 assert(rr);
2111
2112 /* Checks if the RR we are looking for must be signed with an
2113 * RRSIG. This is used for positive responses. */
2114
2115 if (t->scope->dnssec_mode == DNSSEC_NO)
2116 return false;
2117
2118 if (dns_type_is_pseudo(rr->key->type))
2119 return -EINVAL;
2120
2121 r = dns_transaction_negative_trust_anchor_lookup(t, DNS_RESOURCE_KEY_NAME(rr->key));
2122 if (r < 0)
2123 return r;
2124 if (r > 0)
2125 return false;
2126
2127 switch (rr->key->type) {
2128
2129 case DNS_TYPE_RRSIG:
2130 /* RRSIGs are the signatures themselves, they need no signing. */
2131 return false;
2132
2133 case DNS_TYPE_SOA:
2134 case DNS_TYPE_NS: {
2135 DnsTransaction *dt;
2136 Iterator i;
2137
2138 /* For SOA or NS RRs we look for a matching DS transaction */
2139
2140 SET_FOREACH(dt, t->dnssec_transactions, i) {
2141
2142 if (dt->key->class != rr->key->class)
2143 continue;
2144 if (dt->key->type != DNS_TYPE_DS)
2145 continue;
2146
2147 r = dns_name_equal(DNS_RESOURCE_KEY_NAME(dt->key), DNS_RESOURCE_KEY_NAME(rr->key));
2148 if (r < 0)
2149 return r;
2150 if (r == 0)
2151 continue;
2152
2153 /* We found a DS transactions for the SOA/NS
2154 * RRs we are looking at. If it discovered signed DS
2155 * RRs, then we need to be signed, too. */
2156
2157 if (!dt->answer_authenticated)
2158 return false;
2159
2160 return dns_answer_match_key(dt->answer, dt->key, NULL);
2161 }
2162
2163 /* We found nothing that proves this is safe to leave
2164 * this unauthenticated, hence ask inist on
2165 * authentication. */
2166 return true;
2167 }
2168
2169 case DNS_TYPE_DS:
2170 case DNS_TYPE_CNAME:
2171 case DNS_TYPE_DNAME: {
2172 const char *parent = NULL;
2173 DnsTransaction *dt;
2174 Iterator i;
2175
2176 /*
2177 * CNAME/DNAME RRs cannot be located at a zone apex, hence look directly for the parent SOA.
2178 *
2179 * DS RRs are signed if the parent is signed, hence also look at the parent SOA
2180 */
2181
2182 SET_FOREACH(dt, t->dnssec_transactions, i) {
2183
2184 if (dt->key->class != rr->key->class)
2185 continue;
2186 if (dt->key->type != DNS_TYPE_SOA)
2187 continue;
2188
2189 if (!parent) {
2190 parent = DNS_RESOURCE_KEY_NAME(rr->key);
2191 r = dns_name_parent(&parent);
2192 if (r < 0)
2193 return r;
2194 if (r == 0) {
2195 if (rr->key->type == DNS_TYPE_DS)
2196 return true;
2197
2198 /* A CNAME/DNAME without a parent? That's sooo weird. */
2199 log_debug("Transaction %" PRIu16 " claims CNAME/DNAME at root. Refusing.", t->id);
2200 return -EBADMSG;
2201 }
2202 }
2203
2204 r = dns_name_equal(DNS_RESOURCE_KEY_NAME(dt->key), parent);
2205 if (r < 0)
2206 return r;
2207 if (r == 0)
2208 continue;
2209
2210 return t->answer_authenticated;
2211 }
2212
2213 return true;
2214 }
2215
2216 default: {
2217 DnsTransaction *dt;
2218 Iterator i;
2219
2220 /* Any other kind of RR (including DNSKEY/NSEC/NSEC3). Let's see if our SOA lookup was authenticated */
2221
2222 SET_FOREACH(dt, t->dnssec_transactions, i) {
2223
2224 if (dt->key->class != rr->key->class)
2225 continue;
2226 if (dt->key->type != DNS_TYPE_SOA)
2227 continue;
2228
2229 r = dns_name_equal(DNS_RESOURCE_KEY_NAME(dt->key), DNS_RESOURCE_KEY_NAME(rr->key));
2230 if (r < 0)
2231 return r;
2232 if (r == 0)
2233 continue;
2234
2235 /* We found the transaction that was supposed to find
2236 * the SOA RR for us. It was successful, but found no
2237 * RR for us. This means we are not at a zone cut. In
2238 * this case, we require authentication if the SOA
2239 * lookup was authenticated too. */
2240 return t->answer_authenticated;
2241 }
2242
2243 return true;
2244 }}
2245 }
2246
2247 static int dns_transaction_in_private_tld(DnsTransaction *t, const DnsResourceKey *key) {
2248 DnsTransaction *dt;
2249 const char *tld;
2250 Iterator i;
2251 int r;
2252
2253 /* If DNSSEC downgrade mode is on, checks whether the
2254 * specified RR is one level below a TLD we have proven not to
2255 * exist. In such a case we assume that this is a private
2256 * domain, and permit it.
2257 *
2258 * This detects cases like the Fritz!Box router networks. Each
2259 * Fritz!Box router serves a private "fritz.box" zone, in the
2260 * non-existing TLD "box". Requests for the "fritz.box" domain
2261 * are served by the router itself, while requests for the
2262 * "box" domain will result in NXDOMAIN.
2263 *
2264 * Note that this logic is unable to detect cases where a
2265 * router serves a private DNS zone directly under
2266 * non-existing TLD. In such a case we cannot detect whether
2267 * the TLD is supposed to exist or not, as all requests we
2268 * make for it will be answered by the router's zone, and not
2269 * by the root zone. */
2270
2271 assert(t);
2272
2273 if (t->scope->dnssec_mode != DNSSEC_ALLOW_DOWNGRADE)
2274 return false; /* In strict DNSSEC mode what doesn't exist, doesn't exist */
2275
2276 tld = DNS_RESOURCE_KEY_NAME(key);
2277 r = dns_name_parent(&tld);
2278 if (r < 0)
2279 return r;
2280 if (r == 0)
2281 return false; /* Already the root domain */
2282
2283 if (!dns_name_is_single_label(tld))
2284 return false;
2285
2286 SET_FOREACH(dt, t->dnssec_transactions, i) {
2287
2288 if (dt->key->class != key->class)
2289 continue;
2290
2291 r = dns_name_equal(DNS_RESOURCE_KEY_NAME(dt->key), tld);
2292 if (r < 0)
2293 return r;
2294 if (r == 0)
2295 continue;
2296
2297 /* We found an auxiliary lookup we did for the TLD. If
2298 * that returned with NXDOMAIN, we know the TLD didn't
2299 * exist, and hence this might be a private zone. */
2300
2301 return dt->answer_rcode == DNS_RCODE_NXDOMAIN;
2302 }
2303
2304 return false;
2305 }
2306
2307 static int dns_transaction_requires_nsec(DnsTransaction *t) {
2308 DnsTransaction *dt;
2309 const char *name;
2310 Iterator i;
2311 int r;
2312
2313 assert(t);
2314
2315 /* Checks if we need to insist on NSEC/NSEC3 RRs for proving
2316 * this negative reply */
2317
2318 if (t->scope->dnssec_mode == DNSSEC_NO)
2319 return false;
2320
2321 if (dns_type_is_pseudo(t->key->type))
2322 return -EINVAL;
2323
2324 r = dns_transaction_negative_trust_anchor_lookup(t, DNS_RESOURCE_KEY_NAME(t->key));
2325 if (r < 0)
2326 return r;
2327 if (r > 0)
2328 return false;
2329
2330 r = dns_transaction_in_private_tld(t, t->key);
2331 if (r < 0)
2332 return r;
2333 if (r > 0) {
2334 /* The lookup is from a TLD that is proven not to
2335 * exist, and we are in downgrade mode, hence ignore
2336 * that fact that we didn't get any NSEC RRs.*/
2337
2338 log_info("Detected a negative query %s in a private DNS zone, permitting unsigned response.", dns_transaction_key_string(t));
2339 return false;
2340 }
2341
2342 name = DNS_RESOURCE_KEY_NAME(t->key);
2343
2344 if (IN_SET(t->key->type, DNS_TYPE_SOA, DNS_TYPE_NS, DNS_TYPE_DS)) {
2345
2346 /* We got a negative reply for this SOA/NS lookup? If
2347 * so, then we are not at a zone apex, and thus should
2348 * look at the result of the parent SOA lookup.
2349 *
2350 * We got a negative reply for this DS lookup? DS RRs
2351 * are signed when their parent zone is signed, hence
2352 * also check the parent SOA in this case. */
2353
2354 r = dns_name_parent(&name);
2355 if (r < 0)
2356 return r;
2357 if (r == 0)
2358 return true;
2359 }
2360
2361 /* For all other RRs we check the SOA on the same level to see
2362 * if it's signed. */
2363
2364 SET_FOREACH(dt, t->dnssec_transactions, i) {
2365
2366 if (dt->key->class != t->key->class)
2367 continue;
2368 if (dt->key->type != DNS_TYPE_SOA)
2369 continue;
2370
2371 r = dns_name_equal(DNS_RESOURCE_KEY_NAME(dt->key), name);
2372 if (r < 0)
2373 return r;
2374 if (r == 0)
2375 continue;
2376
2377 return dt->answer_authenticated;
2378 }
2379
2380 /* If in doubt, require NSEC/NSEC3 */
2381 return true;
2382 }
2383
2384 static int dns_transaction_dnskey_authenticated(DnsTransaction *t, DnsResourceRecord *rr) {
2385 DnsResourceRecord *rrsig;
2386 bool found = false;
2387 int r;
2388
2389 /* Checks whether any of the DNSKEYs used for the RRSIGs for
2390 * the specified RRset is authenticated (i.e. has a matching
2391 * DS RR). */
2392
2393 r = dns_transaction_negative_trust_anchor_lookup(t, DNS_RESOURCE_KEY_NAME(rr->key));
2394 if (r < 0)
2395 return r;
2396 if (r > 0)
2397 return false;
2398
2399 DNS_ANSWER_FOREACH(rrsig, t->answer) {
2400 DnsTransaction *dt;
2401 Iterator i;
2402
2403 r = dnssec_key_match_rrsig(rr->key, rrsig);
2404 if (r < 0)
2405 return r;
2406 if (r == 0)
2407 continue;
2408
2409 SET_FOREACH(dt, t->dnssec_transactions, i) {
2410
2411 if (dt->key->class != rr->key->class)
2412 continue;
2413
2414 if (dt->key->type == DNS_TYPE_DNSKEY) {
2415
2416 r = dns_name_equal(DNS_RESOURCE_KEY_NAME(dt->key), rrsig->rrsig.signer);
2417 if (r < 0)
2418 return r;
2419 if (r == 0)
2420 continue;
2421
2422 /* OK, we found an auxiliary DNSKEY
2423 * lookup. If that lookup is
2424 * authenticated, report this. */
2425
2426 if (dt->answer_authenticated)
2427 return true;
2428
2429 found = true;
2430
2431 } else if (dt->key->type == DNS_TYPE_DS) {
2432
2433 r = dns_name_equal(DNS_RESOURCE_KEY_NAME(dt->key), rrsig->rrsig.signer);
2434 if (r < 0)
2435 return r;
2436 if (r == 0)
2437 continue;
2438
2439 /* OK, we found an auxiliary DS
2440 * lookup. If that lookup is
2441 * authenticated and non-zero, we
2442 * won! */
2443
2444 if (!dt->answer_authenticated)
2445 return false;
2446
2447 return dns_answer_match_key(dt->answer, dt->key, NULL);
2448 }
2449 }
2450 }
2451
2452 return found ? false : -ENXIO;
2453 }
2454
2455 static int dns_transaction_known_signed(DnsTransaction *t, DnsResourceRecord *rr) {
2456 assert(t);
2457 assert(rr);
2458
2459 /* We know that the root domain is signed, hence if it appears
2460 * not to be signed, there's a problem with the DNS server */
2461
2462 return rr->key->class == DNS_CLASS_IN &&
2463 dns_name_is_root(DNS_RESOURCE_KEY_NAME(rr->key));
2464 }
2465
2466 static int dns_transaction_check_revoked_trust_anchors(DnsTransaction *t) {
2467 DnsResourceRecord *rr;
2468 int r;
2469
2470 assert(t);
2471
2472 /* Maybe warn the user that we encountered a revoked DNSKEY
2473 * for a key from our trust anchor. Note that we don't care
2474 * whether the DNSKEY can be authenticated or not. It's
2475 * sufficient if it is self-signed. */
2476
2477 DNS_ANSWER_FOREACH(rr, t->answer) {
2478 r = dns_trust_anchor_check_revoked(&t->scope->manager->trust_anchor, rr, t->answer);
2479 if (r < 0)
2480 return r;
2481 }
2482
2483 return 0;
2484 }
2485
2486 static int dns_transaction_invalidate_revoked_keys(DnsTransaction *t) {
2487 bool changed;
2488 int r;
2489
2490 assert(t);
2491
2492 /* Removes all DNSKEY/DS objects from t->validated_keys that
2493 * our trust anchors database considers revoked. */
2494
2495 do {
2496 DnsResourceRecord *rr;
2497
2498 changed = false;
2499
2500 DNS_ANSWER_FOREACH(rr, t->validated_keys) {
2501 r = dns_trust_anchor_is_revoked(&t->scope->manager->trust_anchor, rr);
2502 if (r < 0)
2503 return r;
2504 if (r > 0) {
2505 r = dns_answer_remove_by_rr(&t->validated_keys, rr);
2506 if (r < 0)
2507 return r;
2508
2509 assert(r > 0);
2510 changed = true;
2511 break;
2512 }
2513 }
2514 } while (changed);
2515
2516 return 0;
2517 }
2518
2519 static int dns_transaction_copy_validated(DnsTransaction *t) {
2520 DnsTransaction *dt;
2521 Iterator i;
2522 int r;
2523
2524 assert(t);
2525
2526 /* Copy all validated RRs from the auxiliary DNSSEC transactions into our set of validated RRs */
2527
2528 SET_FOREACH(dt, t->dnssec_transactions, i) {
2529
2530 if (DNS_TRANSACTION_IS_LIVE(dt->state))
2531 continue;
2532
2533 if (!dt->answer_authenticated)
2534 continue;
2535
2536 r = dns_answer_extend(&t->validated_keys, dt->answer);
2537 if (r < 0)
2538 return r;
2539 }
2540
2541 return 0;
2542 }
2543
2544 int dns_transaction_validate_dnssec(DnsTransaction *t) {
2545 _cleanup_(dns_answer_unrefp) DnsAnswer *validated = NULL;
2546 enum {
2547 PHASE_DNSKEY, /* Phase #1, only validate DNSKEYs */
2548 PHASE_NSEC, /* Phase #2, only validate NSEC+NSEC3 */
2549 PHASE_ALL, /* Phase #3, validate everything else */
2550 } phase;
2551 DnsResourceRecord *rr;
2552 DnsAnswerFlags flags;
2553 int r;
2554
2555 assert(t);
2556
2557 /* We have now collected all DS and DNSKEY RRs in
2558 * t->validated_keys, let's see which RRs we can now
2559 * authenticate with that. */
2560
2561 if (t->scope->dnssec_mode == DNSSEC_NO)
2562 return 0;
2563
2564 /* Already validated */
2565 if (t->answer_dnssec_result != _DNSSEC_RESULT_INVALID)
2566 return 0;
2567
2568 /* Our own stuff needs no validation */
2569 if (IN_SET(t->answer_source, DNS_TRANSACTION_ZONE, DNS_TRANSACTION_TRUST_ANCHOR)) {
2570 t->answer_dnssec_result = DNSSEC_VALIDATED;
2571 t->answer_authenticated = true;
2572 return 0;
2573 }
2574
2575 /* Cached stuff is not affected by validation. */
2576 if (t->answer_source != DNS_TRANSACTION_NETWORK)
2577 return 0;
2578
2579 if (!dns_transaction_dnssec_supported_full(t)) {
2580 /* The server does not support DNSSEC, or doesn't augment responses with RRSIGs. */
2581 t->answer_dnssec_result = DNSSEC_INCOMPATIBLE_SERVER;
2582 log_debug("Not validating response for %" PRIu16 ", server lacks DNSSEC support.", t->id);
2583 return 0;
2584 }
2585
2586 log_debug("Validating response from transaction %" PRIu16 " (%s).", t->id, dns_transaction_key_string(t));
2587
2588 /* First, see if this response contains any revoked trust
2589 * anchors we care about */
2590 r = dns_transaction_check_revoked_trust_anchors(t);
2591 if (r < 0)
2592 return r;
2593
2594 /* Third, copy all RRs we acquired successfully from auxiliary RRs over. */
2595 r = dns_transaction_copy_validated(t);
2596 if (r < 0)
2597 return r;
2598
2599 /* Second, see if there are DNSKEYs we already know a
2600 * validated DS for. */
2601 r = dns_transaction_validate_dnskey_by_ds(t);
2602 if (r < 0)
2603 return r;
2604
2605 /* Fourth, remove all DNSKEY and DS RRs again that our trust
2606 * anchor says are revoked. After all we might have marked
2607 * some keys revoked above, but they might still be lingering
2608 * in our validated_keys list. */
2609 r = dns_transaction_invalidate_revoked_keys(t);
2610 if (r < 0)
2611 return r;
2612
2613 phase = PHASE_DNSKEY;
2614 for (;;) {
2615 bool changed = false, have_nsec = false;
2616
2617 DNS_ANSWER_FOREACH(rr, t->answer) {
2618 DnsResourceRecord *rrsig = NULL;
2619 DnssecResult result;
2620
2621 switch (rr->key->type) {
2622
2623 case DNS_TYPE_RRSIG:
2624 continue;
2625
2626 case DNS_TYPE_DNSKEY:
2627 /* We validate DNSKEYs only in the DNSKEY and ALL phases */
2628 if (phase == PHASE_NSEC)
2629 continue;
2630 break;
2631
2632 case DNS_TYPE_NSEC:
2633 case DNS_TYPE_NSEC3:
2634 have_nsec = true;
2635
2636 /* We validate NSEC/NSEC3 only in the NSEC and ALL phases */
2637 if (phase == PHASE_DNSKEY)
2638 continue;
2639
2640 break;
2641
2642 default:
2643 /* We validate all other RRs only in the ALL phases */
2644 if (phase != PHASE_ALL)
2645 continue;
2646
2647 break;
2648 }
2649
2650 r = dnssec_verify_rrset_search(t->answer, rr->key, t->validated_keys, USEC_INFINITY, &result, &rrsig);
2651 if (r < 0)
2652 return r;
2653
2654 log_debug("Looking at %s: %s", strna(dns_resource_record_to_string(rr)), dnssec_result_to_string(result));
2655
2656 if (result == DNSSEC_VALIDATED) {
2657
2658 if (rr->key->type == DNS_TYPE_DNSKEY) {
2659 /* If we just validated a
2660 * DNSKEY RRset, then let's
2661 * add these keys to the set
2662 * of validated keys for this
2663 * transaction. */
2664
2665 r = dns_answer_copy_by_key(&t->validated_keys, t->answer, rr->key, DNS_ANSWER_AUTHENTICATED);
2666 if (r < 0)
2667 return r;
2668
2669 /* some of the DNSKEYs we just
2670 * added might already have
2671 * been revoked, remove them
2672 * again in that case. */
2673 r = dns_transaction_invalidate_revoked_keys(t);
2674 if (r < 0)
2675 return r;
2676 }
2677
2678 /* Add the validated RRset to the new
2679 * list of validated RRsets, and
2680 * remove it from the unvalidated
2681 * RRsets. We mark the RRset as
2682 * authenticated and cacheable. */
2683 r = dns_answer_move_by_key(&validated, &t->answer, rr->key, DNS_ANSWER_AUTHENTICATED|DNS_ANSWER_CACHEABLE);
2684 if (r < 0)
2685 return r;
2686
2687 manager_dnssec_verdict(t->scope->manager, DNSSEC_SECURE, rr->key);
2688
2689 /* Exit the loop, we dropped something from the answer, start from the beginning */
2690 changed = true;
2691 break;
2692 }
2693
2694 /* If we haven't read all DNSKEYs yet a negative result of the validation is irrelevant, as
2695 * there might be more DNSKEYs coming. Similar, if we haven't read all NSEC/NSEC3 RRs yet, we
2696 * cannot do positive wildcard proofs yet, as those require the NSEC/NSEC3 RRs. */
2697 if (phase != PHASE_ALL)
2698 continue;
2699
2700 if (result == DNSSEC_VALIDATED_WILDCARD) {
2701 bool authenticated = false;
2702 const char *source;
2703
2704 /* This RRset validated, but as a wildcard. This means we need to prove via NSEC/NSEC3
2705 * that no matching non-wildcard RR exists.*/
2706
2707 /* First step, determine the source of synthesis */
2708 r = dns_resource_record_source(rrsig, &source);
2709 if (r < 0)
2710 return r;
2711
2712 r = dnssec_test_positive_wildcard(
2713 validated,
2714 DNS_RESOURCE_KEY_NAME(rr->key),
2715 source,
2716 rrsig->rrsig.signer,
2717 &authenticated);
2718
2719 /* Unless the NSEC proof showed that the key really doesn't exist something is off. */
2720 if (r == 0)
2721 result = DNSSEC_INVALID;
2722 else {
2723 r = dns_answer_move_by_key(&validated, &t->answer, rr->key, authenticated ? (DNS_ANSWER_AUTHENTICATED|DNS_ANSWER_CACHEABLE) : 0);
2724 if (r < 0)
2725 return r;
2726
2727 manager_dnssec_verdict(t->scope->manager, authenticated ? DNSSEC_SECURE : DNSSEC_INSECURE, rr->key);
2728
2729 /* Exit the loop, we dropped something from the answer, start from the beginning */
2730 changed = true;
2731 break;
2732 }
2733 }
2734
2735 if (result == DNSSEC_NO_SIGNATURE) {
2736 r = dns_transaction_requires_rrsig(t, rr);
2737 if (r < 0)
2738 return r;
2739 if (r == 0) {
2740 /* Data does not require signing. In that case, just copy it over,
2741 * but remember that this is by no means authenticated.*/
2742 r = dns_answer_move_by_key(&validated, &t->answer, rr->key, 0);
2743 if (r < 0)
2744 return r;
2745
2746 manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, rr->key);
2747 changed = true;
2748 break;
2749 }
2750
2751 r = dns_transaction_known_signed(t, rr);
2752 if (r < 0)
2753 return r;
2754 if (r > 0) {
2755 /* This is an RR we know has to be signed. If it isn't this means
2756 * the server is not attaching RRSIGs, hence complain. */
2757
2758 dns_server_packet_rrsig_missing(t->server, t->current_feature_level);
2759
2760 if (t->scope->dnssec_mode == DNSSEC_ALLOW_DOWNGRADE) {
2761
2762 /* Downgrading is OK? If so, just consider the information unsigned */
2763
2764 r = dns_answer_move_by_key(&validated, &t->answer, rr->key, 0);
2765 if (r < 0)
2766 return r;
2767
2768 manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, rr->key);
2769 changed = true;
2770 break;
2771 }
2772
2773 /* Otherwise, fail */
2774 t->answer_dnssec_result = DNSSEC_INCOMPATIBLE_SERVER;
2775 return 0;
2776 }
2777
2778 r = dns_transaction_in_private_tld(t, rr->key);
2779 if (r < 0)
2780 return r;
2781 if (r > 0) {
2782 _cleanup_free_ char *s = NULL;
2783
2784 /* The data is from a TLD that is proven not to exist, and we are in downgrade
2785 * mode, hence ignore the fact that this was not signed. */
2786
2787 (void) dns_resource_key_to_string(rr->key, &s);
2788 log_info("Detected RRset %s is in a private DNS zone, permitting unsigned RRs.", strna(s ? strstrip(s) : NULL));
2789
2790 r = dns_answer_move_by_key(&validated, &t->answer, rr->key, 0);
2791 if (r < 0)
2792 return r;
2793
2794 manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, rr->key);
2795 changed = true;
2796 break;
2797 }
2798 }
2799
2800 if (IN_SET(result,
2801 DNSSEC_MISSING_KEY,
2802 DNSSEC_SIGNATURE_EXPIRED,
2803 DNSSEC_UNSUPPORTED_ALGORITHM)) {
2804
2805 r = dns_transaction_dnskey_authenticated(t, rr);
2806 if (r < 0 && r != -ENXIO)
2807 return r;
2808 if (r == 0) {
2809 /* The DNSKEY transaction was not authenticated, this means there's
2810 * no DS for this, which means it's OK if no keys are found for this signature. */
2811
2812 r = dns_answer_move_by_key(&validated, &t->answer, rr->key, 0);
2813 if (r < 0)
2814 return r;
2815
2816 manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, rr->key);
2817 changed = true;
2818 break;
2819 }
2820 }
2821
2822 r = dns_transaction_is_primary_response(t, rr);
2823 if (r < 0)
2824 return r;
2825 if (r > 0) {
2826
2827 /* Look for a matching DNAME for this CNAME */
2828 r = dns_answer_has_dname_for_cname(t->answer, rr);
2829 if (r < 0)
2830 return r;
2831 if (r == 0) {
2832 /* Also look among the stuff we already validated */
2833 r = dns_answer_has_dname_for_cname(validated, rr);
2834 if (r < 0)
2835 return r;
2836 }
2837
2838 if (r == 0) {
2839 if (IN_SET(result,
2840 DNSSEC_INVALID,
2841 DNSSEC_SIGNATURE_EXPIRED,
2842 DNSSEC_NO_SIGNATURE))
2843 manager_dnssec_verdict(t->scope->manager, DNSSEC_BOGUS, rr->key);
2844 else /* DNSSEC_MISSING_KEY or DNSSEC_UNSUPPORTED_ALGORITHM */
2845 manager_dnssec_verdict(t->scope->manager, DNSSEC_INDETERMINATE, rr->key);
2846
2847 /* This is a primary response to our question, and it failed validation. That's
2848 * fatal. */
2849 t->answer_dnssec_result = result;
2850 return 0;
2851 }
2852
2853 /* This is a primary response, but we do have a DNAME RR in the RR that can replay this
2854 * CNAME, hence rely on that, and we can remove the CNAME in favour of it. */
2855 }
2856
2857 /* This is just some auxiliary data. Just remove the RRset and continue. */
2858 r = dns_answer_remove_by_key(&t->answer, rr->key);
2859 if (r < 0)
2860 return r;
2861
2862 /* Exit the loop, we dropped something from the answer, start from the beginning */
2863 changed = true;
2864 break;
2865 }
2866
2867 /* Restart the inner loop as long as we managed to achieve something */
2868 if (changed)
2869 continue;
2870
2871 if (phase == PHASE_DNSKEY && have_nsec) {
2872 /* OK, we processed all DNSKEYs, and there are NSEC/NSEC3 RRs, look at those now. */
2873 phase = PHASE_NSEC;
2874 continue;
2875 }
2876
2877 if (phase != PHASE_ALL) {
2878 /* OK, we processed all DNSKEYs and NSEC/NSEC3 RRs, look at all the rest now. Note that in this
2879 * third phase we start to remove RRs we couldn't validate. */
2880 phase = PHASE_ALL;
2881 continue;
2882 }
2883
2884 /* We're done */
2885 break;
2886 }
2887
2888 dns_answer_unref(t->answer);
2889 t->answer = validated;
2890 validated = NULL;
2891
2892 /* At this point the answer only contains validated
2893 * RRsets. Now, let's see if it actually answers the question
2894 * we asked. If so, great! If it doesn't, then see if
2895 * NSEC/NSEC3 can prove this. */
2896 r = dns_transaction_has_positive_answer(t, &flags);
2897 if (r > 0) {
2898 /* Yes, it answers the question! */
2899
2900 if (flags & DNS_ANSWER_AUTHENTICATED) {
2901 /* The answer is fully authenticated, yay. */
2902 t->answer_dnssec_result = DNSSEC_VALIDATED;
2903 t->answer_rcode = DNS_RCODE_SUCCESS;
2904 t->answer_authenticated = true;
2905 } else {
2906 /* The answer is not fully authenticated. */
2907 t->answer_dnssec_result = DNSSEC_UNSIGNED;
2908 t->answer_authenticated = false;
2909 }
2910
2911 } else if (r == 0) {
2912 DnssecNsecResult nr;
2913 bool authenticated = false;
2914
2915 /* Bummer! Let's check NSEC/NSEC3 */
2916 r = dnssec_nsec_test(t->answer, t->key, &nr, &authenticated, &t->answer_nsec_ttl);
2917 if (r < 0)
2918 return r;
2919
2920 switch (nr) {
2921
2922 case DNSSEC_NSEC_NXDOMAIN:
2923 /* NSEC proves the domain doesn't exist. Very good. */
2924 log_debug("Proved NXDOMAIN via NSEC/NSEC3 for transaction %u (%s)", t->id, dns_transaction_key_string(t));
2925 t->answer_dnssec_result = DNSSEC_VALIDATED;
2926 t->answer_rcode = DNS_RCODE_NXDOMAIN;
2927 t->answer_authenticated = authenticated;
2928
2929 manager_dnssec_verdict(t->scope->manager, authenticated ? DNSSEC_SECURE : DNSSEC_INSECURE, t->key);
2930 break;
2931
2932 case DNSSEC_NSEC_NODATA:
2933 /* NSEC proves that there's no data here, very good. */
2934 log_debug("Proved NODATA via NSEC/NSEC3 for transaction %u (%s)", t->id, dns_transaction_key_string(t));
2935 t->answer_dnssec_result = DNSSEC_VALIDATED;
2936 t->answer_rcode = DNS_RCODE_SUCCESS;
2937 t->answer_authenticated = authenticated;
2938
2939 manager_dnssec_verdict(t->scope->manager, authenticated ? DNSSEC_SECURE : DNSSEC_INSECURE, t->key);
2940 break;
2941
2942 case DNSSEC_NSEC_OPTOUT:
2943 /* NSEC3 says the data might not be signed */
2944 log_debug("Data is NSEC3 opt-out via NSEC/NSEC3 for transaction %u (%s)", t->id, dns_transaction_key_string(t));
2945 t->answer_dnssec_result = DNSSEC_UNSIGNED;
2946 t->answer_authenticated = false;
2947
2948 manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, t->key);
2949 break;
2950
2951 case DNSSEC_NSEC_NO_RR:
2952 /* No NSEC data? Bummer! */
2953
2954 r = dns_transaction_requires_nsec(t);
2955 if (r < 0)
2956 return r;
2957 if (r > 0) {
2958 t->answer_dnssec_result = DNSSEC_NO_SIGNATURE;
2959 manager_dnssec_verdict(t->scope->manager, DNSSEC_BOGUS, t->key);
2960 } else {
2961 t->answer_dnssec_result = DNSSEC_UNSIGNED;
2962 t->answer_authenticated = false;
2963 manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, t->key);
2964 }
2965
2966 break;
2967
2968 case DNSSEC_NSEC_UNSUPPORTED_ALGORITHM:
2969 /* We don't know the NSEC3 algorithm used? */
2970 t->answer_dnssec_result = DNSSEC_UNSUPPORTED_ALGORITHM;
2971 manager_dnssec_verdict(t->scope->manager, DNSSEC_INDETERMINATE, t->key);
2972 break;
2973
2974 case DNSSEC_NSEC_FOUND:
2975 case DNSSEC_NSEC_CNAME:
2976 /* NSEC says it needs to be there, but we couldn't find it? Bummer! */
2977 t->answer_dnssec_result = DNSSEC_NSEC_MISMATCH;
2978 manager_dnssec_verdict(t->scope->manager, DNSSEC_BOGUS, t->key);
2979 break;
2980
2981 default:
2982 assert_not_reached("Unexpected NSEC result.");
2983 }
2984 }
2985
2986 return 1;
2987 }
2988
2989 const char *dns_transaction_key_string(DnsTransaction *t) {
2990 assert(t);
2991
2992 if (!t->key_string) {
2993 if (dns_resource_key_to_string(t->key, &t->key_string) < 0)
2994 return "n/a";
2995 }
2996
2997 return strstrip(t->key_string);
2998 }
2999
3000 static const char* const dns_transaction_state_table[_DNS_TRANSACTION_STATE_MAX] = {
3001 [DNS_TRANSACTION_NULL] = "null",
3002 [DNS_TRANSACTION_PENDING] = "pending",
3003 [DNS_TRANSACTION_VALIDATING] = "validating",
3004 [DNS_TRANSACTION_RCODE_FAILURE] = "rcode-failure",
3005 [DNS_TRANSACTION_SUCCESS] = "success",
3006 [DNS_TRANSACTION_NO_SERVERS] = "no-servers",
3007 [DNS_TRANSACTION_TIMEOUT] = "timeout",
3008 [DNS_TRANSACTION_ATTEMPTS_MAX_REACHED] = "attempts-max-reached",
3009 [DNS_TRANSACTION_INVALID_REPLY] = "invalid-reply",
3010 [DNS_TRANSACTION_ERRNO] = "errno",
3011 [DNS_TRANSACTION_ABORTED] = "aborted",
3012 [DNS_TRANSACTION_DNSSEC_FAILED] = "dnssec-failed",
3013 [DNS_TRANSACTION_NO_TRUST_ANCHOR] = "no-trust-anchor",
3014 [DNS_TRANSACTION_RR_TYPE_UNSUPPORTED] = "rr-type-unsupported",
3015 [DNS_TRANSACTION_NETWORK_DOWN] = "network-down",
3016 [DNS_TRANSACTION_NOT_FOUND] = "not-found",
3017 };
3018 DEFINE_STRING_TABLE_LOOKUP(dns_transaction_state, DnsTransactionState);
3019
3020 static const char* const dns_transaction_source_table[_DNS_TRANSACTION_SOURCE_MAX] = {
3021 [DNS_TRANSACTION_NETWORK] = "network",
3022 [DNS_TRANSACTION_CACHE] = "cache",
3023 [DNS_TRANSACTION_ZONE] = "zone",
3024 [DNS_TRANSACTION_TRUST_ANCHOR] = "trust-anchor",
3025 };
3026 DEFINE_STRING_TABLE_LOOKUP(dns_transaction_source, DnsTransactionSource);