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