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