]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
ec2c5e43 | 2 | |
62cc1c55 | 3 | #include "sd-messages.h" |
beef6a5f | 4 | |
ec2c5e43 | 5 | #include "af-list.h" |
b5efdb8a | 6 | #include "alloc-util.h" |
f52e61da | 7 | #include "dns-domain.h" |
7cc6ed7b | 8 | #include "errno-list.h" |
c3fecddf | 9 | #include "errno-util.h" |
3ffd4af2 | 10 | #include "fd-util.h" |
e2341b6b | 11 | #include "glyph-util.h" |
3ffd4af2 | 12 | #include "random-util.h" |
7778dfff | 13 | #include "resolved-dns-cache.h" |
3ffd4af2 | 14 | #include "resolved-dns-transaction.h" |
6016fcb0 | 15 | #include "resolved-dnstls.h" |
aedf00a2 | 16 | #include "resolved-llmnr.h" |
6016fcb0 | 17 | #include "string-table.h" |
5d67a7ae | 18 | |
b214dc0f | 19 | #define TRANSACTIONS_MAX 4096 |
dc349f5f | 20 | #define TRANSACTION_TCP_TIMEOUT_USEC (10U*USEC_PER_SEC) |
b214dc0f | 21 | |
dbc4661a MCO |
22 | /* After how much time to repeat classic DNS requests */ |
23 | #define DNS_TIMEOUT_USEC (SD_RESOLVED_QUERY_TIMEOUT_USEC / DNS_TRANSACTION_ATTEMPTS_MAX) | |
24 | ||
c61d2b44 LP |
25 | static void dns_transaction_reset_answer(DnsTransaction *t) { |
26 | assert(t); | |
27 | ||
28 | t->received = dns_packet_unref(t->received); | |
29 | t->answer = dns_answer_unref(t->answer); | |
30 | t->answer_rcode = 0; | |
41398e87 YW |
31 | t->answer_ede_rcode = _DNS_EDE_RCODE_INVALID; |
32 | t->answer_ede_msg = mfree(t->answer_ede_msg); | |
c61d2b44 LP |
33 | t->answer_dnssec_result = _DNSSEC_RESULT_INVALID; |
34 | t->answer_source = _DNS_TRANSACTION_SOURCE_INVALID; | |
6f055e43 | 35 | t->answer_query_flags = 0; |
f5fbe71d | 36 | t->answer_nsec_ttl = UINT32_MAX; |
7cc6ed7b | 37 | t->answer_errno = 0; |
c61d2b44 LP |
38 | } |
39 | ||
c5b4f861 LP |
40 | static void dns_transaction_flush_dnssec_transactions(DnsTransaction *t) { |
41 | DnsTransaction *z; | |
42 | ||
43 | assert(t); | |
44 | ||
45 | while ((z = set_steal_first(t->dnssec_transactions))) { | |
46 | set_remove(z->notify_transactions, t); | |
35aa04e9 | 47 | set_remove(z->notify_transactions_done, t); |
c5b4f861 LP |
48 | dns_transaction_gc(z); |
49 | } | |
50 | } | |
51 | ||
80710ade LP |
52 | static void dns_transaction_close_connection( |
53 | DnsTransaction *t, | |
54 | bool use_graveyard) { /* Set use_graveyard = false when you know the connection is already | |
55 | * dead, for example because you got a connection error back from the | |
56 | * kernel. In that case there's no point in keeping the fd around, | |
57 | * hence don't. */ | |
58 | int r; | |
59 | ||
f32f0e57 LP |
60 | assert(t); |
61 | ||
b30bf55d LP |
62 | if (t->stream) { |
63 | /* Let's detach the stream from our transaction, in case something else keeps a reference to it. */ | |
98767d75 IT |
64 | LIST_REMOVE(transactions_by_stream, t->stream->transactions, t); |
65 | ||
66 | /* Remove packet in case it's still in the queue */ | |
67 | dns_packet_unref(ordered_set_remove(t->stream->write_queue, t->sent)); | |
68 | ||
b30bf55d LP |
69 | t->stream = dns_stream_unref(t->stream); |
70 | } | |
71 | ||
97935302 | 72 | t->dns_udp_event_source = sd_event_source_disable_unref(t->dns_udp_event_source); |
80710ade | 73 | |
5bc9ea07 | 74 | /* If we have a UDP socket where we sent a packet, but never received one, then add it to the socket |
80710ade LP |
75 | * graveyard, instead of closing it right away. That way it will stick around for a moment longer, |
76 | * and the reply we might still get from the server will be eaten up instead of resulting in an ICMP | |
77 | * port unreachable error message. */ | |
78 | ||
73780aa8 YW |
79 | /* Skip the graveyard stuff when we're shutting down, since that requires running event loop. |
80 | * Note that this is also called from dns_transaction_free(). In that case, scope may be NULL. */ | |
81 | if (!t->scope || | |
82 | !t->scope->manager || | |
83 | !t->scope->manager->event || | |
84 | sd_event_get_state(t->scope->manager->event) == SD_EVENT_FINISHED) | |
ac1b7b9e FS |
85 | use_graveyard = false; |
86 | ||
80710ade LP |
87 | if (use_graveyard && t->dns_udp_fd >= 0 && t->sent && !t->received) { |
88 | r = manager_add_socket_to_graveyard(t->scope->manager, t->dns_udp_fd); | |
89 | if (r < 0) | |
90 | log_debug_errno(r, "Failed to add UDP socket to graveyard, closing immediately: %m"); | |
91 | else | |
92 | TAKE_FD(t->dns_udp_fd); | |
93 | } | |
94 | ||
f32f0e57 LP |
95 | t->dns_udp_fd = safe_close(t->dns_udp_fd); |
96 | } | |
97 | ||
f535705a | 98 | static void dns_transaction_stop_timeout(DnsTransaction *t) { |
97cc656c LP |
99 | assert(t); |
100 | ||
97935302 | 101 | t->timeout_event_source = sd_event_source_disable_unref(t->timeout_event_source); |
97cc656c LP |
102 | } |
103 | ||
ec2c5e43 | 104 | DnsTransaction* dns_transaction_free(DnsTransaction *t) { |
801ad6a6 | 105 | DnsQueryCandidate *c; |
ec2c5e43 | 106 | DnsZoneItem *i; |
547973de | 107 | DnsTransaction *z; |
ec2c5e43 LP |
108 | |
109 | if (!t) | |
110 | return NULL; | |
111 | ||
51e399bc LP |
112 | log_debug("Freeing transaction %" PRIu16 ".", t->id); |
113 | ||
80710ade | 114 | dns_transaction_close_connection(t, true); |
f535705a | 115 | dns_transaction_stop_timeout(t); |
ec2c5e43 | 116 | |
ec2c5e43 | 117 | dns_packet_unref(t->sent); |
c61d2b44 | 118 | dns_transaction_reset_answer(t); |
ec2c5e43 | 119 | |
8300ba21 | 120 | dns_server_unref(t->server); |
ec2c5e43 LP |
121 | |
122 | if (t->scope) { | |
775ae354 LP |
123 | if (t->key) { |
124 | DnsTransaction *first; | |
125 | ||
126 | first = hashmap_get(t->scope->transactions_by_key, t->key); | |
127 | LIST_REMOVE(transactions_by_key, first, t); | |
128 | if (first) | |
129 | hashmap_replace(t->scope->transactions_by_key, first->key, first); | |
130 | else | |
131 | hashmap_remove(t->scope->transactions_by_key, t->key); | |
132 | } | |
ec2c5e43 | 133 | |
775ae354 | 134 | LIST_REMOVE(transactions_by_scope, t->scope->transactions, t); |
ec2c5e43 | 135 | |
013668db LP |
136 | if (t->id != 0) |
137 | hashmap_remove(t->scope->manager->dns_transactions, UINT_TO_PTR(t->id)); | |
138 | } | |
775ae354 | 139 | |
547973de | 140 | while ((c = set_steal_first(t->notify_query_candidates))) |
801ad6a6 | 141 | set_remove(c->transactions, t); |
547973de | 142 | set_free(t->notify_query_candidates); |
801ad6a6 | 143 | |
35aa04e9 LP |
144 | while ((c = set_steal_first(t->notify_query_candidates_done))) |
145 | set_remove(c->transactions, t); | |
146 | set_free(t->notify_query_candidates_done); | |
147 | ||
547973de | 148 | while ((i = set_steal_first(t->notify_zone_items))) |
ec2c5e43 | 149 | i->probe_transaction = NULL; |
547973de LP |
150 | set_free(t->notify_zone_items); |
151 | ||
35aa04e9 LP |
152 | while ((i = set_steal_first(t->notify_zone_items_done))) |
153 | i->probe_transaction = NULL; | |
154 | set_free(t->notify_zone_items_done); | |
155 | ||
547973de LP |
156 | while ((z = set_steal_first(t->notify_transactions))) |
157 | set_remove(z->dnssec_transactions, t); | |
158 | set_free(t->notify_transactions); | |
159 | ||
35aa04e9 LP |
160 | while ((z = set_steal_first(t->notify_transactions_done))) |
161 | set_remove(z->dnssec_transactions, t); | |
162 | set_free(t->notify_transactions_done); | |
163 | ||
c5b4f861 | 164 | dns_transaction_flush_dnssec_transactions(t); |
547973de LP |
165 | set_free(t->dnssec_transactions); |
166 | ||
167 | dns_answer_unref(t->validated_keys); | |
97cc656c | 168 | dns_resource_key_unref(t->key); |
775ae354 | 169 | dns_packet_unref(t->bypass); |
97cc656c | 170 | |
6b430fdb | 171 | return mfree(t); |
ec2c5e43 LP |
172 | } |
173 | ||
174 | DEFINE_TRIVIAL_CLEANUP_FUNC(DnsTransaction*, dns_transaction_free); | |
175 | ||
1f388301 | 176 | DnsTransaction* dns_transaction_gc(DnsTransaction *t) { |
ec2c5e43 LP |
177 | assert(t); |
178 | ||
1f388301 ZJS |
179 | /* Returns !NULL if we can't gc yet. */ |
180 | ||
ec2c5e43 | 181 | if (t->block_gc > 0) |
1f388301 | 182 | return t; |
ec2c5e43 | 183 | |
ce880172 RP |
184 | if (t->wait_for_answer && IN_SET(t->state, DNS_TRANSACTION_PENDING, DNS_TRANSACTION_VALIDATING)) |
185 | return t; | |
186 | ||
547973de | 187 | if (set_isempty(t->notify_query_candidates) && |
35aa04e9 | 188 | set_isempty(t->notify_query_candidates_done) && |
547973de | 189 | set_isempty(t->notify_zone_items) && |
35aa04e9 LP |
190 | set_isempty(t->notify_zone_items_done) && |
191 | set_isempty(t->notify_transactions) && | |
1f388301 ZJS |
192 | set_isempty(t->notify_transactions_done)) |
193 | return dns_transaction_free(t); | |
51e399bc | 194 | |
1f388301 | 195 | return t; |
ec2c5e43 LP |
196 | } |
197 | ||
4dd15077 LP |
198 | static uint16_t pick_new_id(Manager *m) { |
199 | uint16_t new_id; | |
200 | ||
4ea8b443 ZJS |
201 | /* Find a fresh, unused transaction id. Note that this loop is bounded because there's a limit on the |
202 | * number of transactions, and it's much lower than the space of IDs. */ | |
4dd15077 LP |
203 | |
204 | assert_cc(TRANSACTIONS_MAX < 0xFFFF); | |
205 | ||
206 | do | |
207 | random_bytes(&new_id, sizeof(new_id)); | |
208 | while (new_id == 0 || | |
209 | hashmap_get(m->dns_transactions, UINT_TO_PTR(new_id))); | |
210 | ||
211 | return new_id; | |
212 | } | |
213 | ||
775ae354 LP |
214 | static int key_ok( |
215 | DnsScope *scope, | |
216 | DnsResourceKey *key) { | |
ec2c5e43 | 217 | |
9eae2bf3 | 218 | /* Don't allow looking up invalid or pseudo RRs */ |
c463eb78 | 219 | if (!dns_type_is_valid_query(key->type)) |
9eae2bf3 | 220 | return -EINVAL; |
d0129ddb LP |
221 | if (dns_type_is_obsolete(key->type)) |
222 | return -EOPNOTSUPP; | |
9eae2bf3 LP |
223 | |
224 | /* We only support the IN class */ | |
4c701096 | 225 | if (!IN_SET(key->class, DNS_CLASS_IN, DNS_CLASS_ANY)) |
9eae2bf3 LP |
226 | return -EOPNOTSUPP; |
227 | ||
775ae354 LP |
228 | /* Don't allows DNSSEC RRs to be looked up via LLMNR/mDNS. They don't really make sense |
229 | * there, and it speeds up our queries if we refuse this early */ | |
230 | if (scope->protocol != DNS_PROTOCOL_DNS && | |
231 | dns_type_is_dnssec(key->type)) | |
232 | return -EOPNOTSUPP; | |
233 | ||
234 | return 0; | |
235 | } | |
236 | ||
237 | int dns_transaction_new( | |
238 | DnsTransaction **ret, | |
239 | DnsScope *s, | |
240 | DnsResourceKey *key, | |
241 | DnsPacket *bypass, | |
242 | uint64_t query_flags) { | |
243 | ||
244 | _cleanup_(dns_transaction_freep) DnsTransaction *t = NULL; | |
245 | int r; | |
246 | ||
247 | assert(ret); | |
248 | assert(s); | |
249 | ||
250 | if (key) { | |
251 | assert(!bypass); | |
252 | ||
253 | r = key_ok(s, key); | |
254 | if (r < 0) | |
255 | return r; | |
256 | } else { | |
257 | DnsResourceKey *qk; | |
258 | assert(bypass); | |
259 | ||
260 | r = dns_packet_validate_query(bypass); | |
261 | if (r < 0) | |
262 | return r; | |
263 | ||
264 | DNS_QUESTION_FOREACH(qk, bypass->question) { | |
265 | r = key_ok(s, qk); | |
266 | if (r < 0) | |
267 | return r; | |
268 | } | |
269 | } | |
270 | ||
b214dc0f LP |
271 | if (hashmap_size(s->manager->dns_transactions) >= TRANSACTIONS_MAX) |
272 | return -EBUSY; | |
273 | ||
d5099efc | 274 | r = hashmap_ensure_allocated(&s->manager->dns_transactions, NULL); |
ec2c5e43 LP |
275 | if (r < 0) |
276 | return r; | |
277 | ||
775ae354 LP |
278 | if (key) { |
279 | r = hashmap_ensure_allocated(&s->transactions_by_key, &dns_resource_key_hash_ops); | |
280 | if (r < 0) | |
281 | return r; | |
282 | } | |
da0c630e | 283 | |
1ed31408 | 284 | t = new(DnsTransaction, 1); |
ec2c5e43 LP |
285 | if (!t) |
286 | return -ENOMEM; | |
287 | ||
1ed31408 | 288 | *t = (DnsTransaction) { |
254d1313 | 289 | .dns_udp_fd = -EBADF, |
1ed31408 LP |
290 | .answer_source = _DNS_TRANSACTION_SOURCE_INVALID, |
291 | .answer_dnssec_result = _DNSSEC_RESULT_INVALID, | |
9ca133e9 | 292 | .answer_ede_rcode = _DNS_EDE_RCODE_INVALID, |
f5fbe71d | 293 | .answer_nsec_ttl = UINT32_MAX, |
1ed31408 | 294 | .key = dns_resource_key_ref(key), |
775ae354 LP |
295 | .query_flags = query_flags, |
296 | .bypass = dns_packet_ref(bypass), | |
1ed31408 | 297 | .current_feature_level = _DNS_SERVER_FEATURE_LEVEL_INVALID, |
1ed4e584 | 298 | .clamp_feature_level_servfail = _DNS_SERVER_FEATURE_LEVEL_INVALID, |
1ed31408 LP |
299 | .id = pick_new_id(s->manager), |
300 | }; | |
ec2c5e43 LP |
301 | |
302 | r = hashmap_put(s->manager->dns_transactions, UINT_TO_PTR(t->id), t); | |
303 | if (r < 0) { | |
304 | t->id = 0; | |
305 | return r; | |
306 | } | |
307 | ||
775ae354 LP |
308 | if (t->key) { |
309 | DnsTransaction *first; | |
310 | ||
311 | first = hashmap_get(s->transactions_by_key, t->key); | |
312 | LIST_PREPEND(transactions_by_key, first, t); | |
313 | ||
314 | r = hashmap_replace(s->transactions_by_key, first->key, first); | |
315 | if (r < 0) { | |
316 | LIST_REMOVE(transactions_by_key, first, t); | |
317 | return r; | |
318 | } | |
da0c630e LP |
319 | } |
320 | ||
f9ebb22a | 321 | LIST_PREPEND(transactions_by_scope, s->transactions, t); |
ec2c5e43 LP |
322 | t->scope = s; |
323 | ||
313cefa1 | 324 | s->manager->n_transactions_total++; |
a150ff5e | 325 | |
ec2c5e43 LP |
326 | if (ret) |
327 | *ret = t; | |
328 | ||
775ae354 | 329 | TAKE_PTR(t); |
ec2c5e43 LP |
330 | return 0; |
331 | } | |
332 | ||
4dd15077 LP |
333 | static void dns_transaction_shuffle_id(DnsTransaction *t) { |
334 | uint16_t new_id; | |
335 | assert(t); | |
336 | ||
337 | /* Pick a new ID for this transaction. */ | |
338 | ||
339 | new_id = pick_new_id(t->scope->manager); | |
340 | assert_se(hashmap_remove_and_put(t->scope->manager->dns_transactions, UINT_TO_PTR(t->id), UINT_TO_PTR(new_id), t) >= 0); | |
341 | ||
342 | log_debug("Transaction %" PRIu16 " is now %" PRIu16 ".", t->id, new_id); | |
343 | t->id = new_id; | |
344 | ||
345 | /* Make sure we generate a new packet with the new ID */ | |
346 | t->sent = dns_packet_unref(t->sent); | |
347 | } | |
348 | ||
ec2c5e43 | 349 | static void dns_transaction_tentative(DnsTransaction *t, DnsPacket *p) { |
202b76ae | 350 | char key_str[DNS_RESOURCE_KEY_STRING_MAX]; |
ec2c5e43 | 351 | DnsZoneItem *z; |
ec2c5e43 LP |
352 | |
353 | assert(t); | |
354 | assert(p); | |
94378145 | 355 | assert(t->scope->protocol == DNS_PROTOCOL_LLMNR); |
ec2c5e43 | 356 | |
94378145 | 357 | if (manager_packet_from_local_address(t->scope->manager, p) != 0) |
ec2c5e43 LP |
358 | return; |
359 | ||
a5784c49 LP |
360 | log_debug("Transaction %" PRIu16 " for <%s> on scope %s on %s/%s got tentative packet from %s.", |
361 | t->id, | |
42df9532 | 362 | dns_resource_key_to_string(dns_transaction_key(t), key_str, sizeof key_str), |
ec2c5e43 | 363 | dns_protocol_to_string(t->scope->protocol), |
6ff79f76 | 364 | t->scope->link ? t->scope->link->ifname : "*", |
202b76ae | 365 | af_to_name_short(t->scope->family), |
84dbb3fd | 366 | IN_ADDR_TO_STRING(p->family, &p->sender)); |
ec2c5e43 | 367 | |
a4076574 LP |
368 | /* RFC 4795, Section 4.1 says that the peer with the |
369 | * lexicographically smaller IP address loses */ | |
4d91eec4 LP |
370 | if (memcmp(&p->sender, &p->destination, FAMILY_ADDRESS_SIZE(p->family)) >= 0) { |
371 | log_debug("Peer has lexicographically larger IP address and thus lost in the conflict."); | |
a4076574 LP |
372 | return; |
373 | } | |
374 | ||
4d91eec4 | 375 | log_debug("We have the lexicographically larger IP address and thus lost in the conflict."); |
a4076574 | 376 | |
ec2c5e43 | 377 | t->block_gc++; |
35aa04e9 | 378 | |
547973de | 379 | while ((z = set_first(t->notify_zone_items))) { |
3ef64445 LP |
380 | /* First, make sure the zone item drops the reference |
381 | * to us */ | |
382 | dns_zone_item_probe_stop(z); | |
383 | ||
384 | /* Secondly, report this as conflict, so that we might | |
385 | * look for a different hostname */ | |
ec2c5e43 | 386 | dns_zone_item_conflict(z); |
3ef64445 | 387 | } |
ec2c5e43 LP |
388 | t->block_gc--; |
389 | ||
390 | dns_transaction_gc(t); | |
391 | } | |
392 | ||
393 | void dns_transaction_complete(DnsTransaction *t, DnsTransactionState state) { | |
801ad6a6 | 394 | DnsQueryCandidate *c; |
ec2c5e43 | 395 | DnsZoneItem *z; |
547973de | 396 | DnsTransaction *d; |
7cc6ed7b | 397 | const char *st; |
202b76ae | 398 | char key_str[DNS_RESOURCE_KEY_STRING_MAX]; |
ec2c5e43 LP |
399 | |
400 | assert(t); | |
547973de | 401 | assert(!DNS_TRANSACTION_IS_LIVE(state)); |
e56187ca | 402 | |
202b76ae | 403 | if (state == DNS_TRANSACTION_DNSSEC_FAILED) { |
42df9532 | 404 | dns_resource_key_to_string(dns_transaction_key(t), key_str, sizeof key_str); |
202b76ae | 405 | |
f61dfddb | 406 | log_struct(LOG_NOTICE, |
2b044526 | 407 | "MESSAGE_ID=" SD_MESSAGE_DNSSEC_FAILURE_STR, |
92663a5e ZJS |
408 | LOG_MESSAGE("DNSSEC validation failed for question %s: %s", |
409 | key_str, dnssec_result_to_string(t->answer_dnssec_result)), | |
f61dfddb | 410 | "DNS_TRANSACTION=%" PRIu16, t->id, |
202b76ae | 411 | "DNS_QUESTION=%s", key_str, |
f61dfddb | 412 | "DNSSEC_RESULT=%s", dnssec_result_to_string(t->answer_dnssec_result), |
8aa5afd2 | 413 | "DNS_SERVER=%s", strna(dns_server_string_full(t->server)), |
a1230ff9 | 414 | "DNS_SERVER_FEATURE_LEVEL=%s", dns_server_feature_level_to_string(t->server->possible_feature_level)); |
202b76ae | 415 | } |
f61dfddb | 416 | |
ec2c5e43 LP |
417 | /* Note that this call might invalidate the query. Callers |
418 | * should hence not attempt to access the query or transaction | |
419 | * after calling this function. */ | |
420 | ||
7cc6ed7b LP |
421 | if (state == DNS_TRANSACTION_ERRNO) |
422 | st = errno_to_name(t->answer_errno); | |
423 | else | |
424 | st = dns_transaction_state_to_string(state); | |
425 | ||
43fc4baa | 426 | log_debug("%s transaction %" PRIu16 " for <%s> on scope %s on %s/%s now complete with <%s> from %s (%s; %s).", |
775ae354 | 427 | t->bypass ? "Bypass" : "Regular", |
a5784c49 | 428 | t->id, |
42df9532 | 429 | dns_resource_key_to_string(dns_transaction_key(t), key_str, sizeof key_str), |
ec2c5e43 | 430 | dns_protocol_to_string(t->scope->protocol), |
6ff79f76 | 431 | t->scope->link ? t->scope->link->ifname : "*", |
202b76ae | 432 | af_to_name_short(t->scope->family), |
7cc6ed7b | 433 | st, |
a5784c49 | 434 | t->answer_source < 0 ? "none" : dns_transaction_source_to_string(t->answer_source), |
775ae354 | 435 | FLAGS_SET(t->query_flags, SD_RESOLVED_NO_VALIDATE) ? "not validated" : |
43fc4baa LP |
436 | (FLAGS_SET(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED) ? "authenticated" : "unsigned"), |
437 | FLAGS_SET(t->answer_query_flags, SD_RESOLVED_CONFIDENTIAL) ? "confidential" : "non-confidential"); | |
ec2c5e43 LP |
438 | |
439 | t->state = state; | |
440 | ||
80710ade | 441 | dns_transaction_close_connection(t, true); |
f535705a | 442 | dns_transaction_stop_timeout(t); |
ec2c5e43 LP |
443 | |
444 | /* Notify all queries that are interested, but make sure the | |
445 | * transaction isn't freed while we are still looking at it */ | |
446 | t->block_gc++; | |
f7014757 | 447 | |
35aa04e9 | 448 | SET_FOREACH_MOVE(c, t->notify_query_candidates_done, t->notify_query_candidates) |
547973de | 449 | dns_query_candidate_notify(c); |
35aa04e9 | 450 | SWAP_TWO(t->notify_query_candidates, t->notify_query_candidates_done); |
ec2c5e43 | 451 | |
35aa04e9 LP |
452 | SET_FOREACH_MOVE(z, t->notify_zone_items_done, t->notify_zone_items) |
453 | dns_zone_item_notify(z); | |
454 | SWAP_TWO(t->notify_zone_items, t->notify_zone_items_done); | |
8d67e72c | 455 | if (t->probing && t->state == DNS_TRANSACTION_ATTEMPTS_MAX_REACHED) |
1a63fc54 | 456 | (void) dns_scope_announce(t->scope, false); |
f7014757 | 457 | |
35aa04e9 LP |
458 | SET_FOREACH_MOVE(d, t->notify_transactions_done, t->notify_transactions) |
459 | dns_transaction_notify(d, t); | |
460 | SWAP_TWO(t->notify_transactions, t->notify_transactions_done); | |
f7014757 LP |
461 | |
462 | t->block_gc--; | |
ec2c5e43 LP |
463 | dns_transaction_gc(t); |
464 | } | |
465 | ||
fd8a3017 LP |
466 | static void dns_transaction_complete_errno(DnsTransaction *t, int error) { |
467 | assert(t); | |
468 | assert(error != 0); | |
469 | ||
470 | t->answer_errno = abs(error); | |
471 | dns_transaction_complete(t, DNS_TRANSACTION_ERRNO); | |
472 | } | |
473 | ||
519ef046 LP |
474 | static int dns_transaction_pick_server(DnsTransaction *t) { |
475 | DnsServer *server; | |
476 | ||
477 | assert(t); | |
478 | assert(t->scope->protocol == DNS_PROTOCOL_DNS); | |
479 | ||
d001e0a3 LP |
480 | /* Pick a DNS server and a feature level for it. */ |
481 | ||
519ef046 LP |
482 | server = dns_scope_get_dns_server(t->scope); |
483 | if (!server) | |
484 | return -ESRCH; | |
485 | ||
d001e0a3 LP |
486 | /* If we changed the server invalidate the feature level clamping, as the new server might have completely |
487 | * different properties. */ | |
4aa37ad3 | 488 | if (server != t->server) |
1ed4e584 | 489 | t->clamp_feature_level_servfail = _DNS_SERVER_FEATURE_LEVEL_INVALID; |
d001e0a3 | 490 | |
274b8748 | 491 | t->current_feature_level = dns_server_possible_feature_level(server); |
519ef046 | 492 | |
d001e0a3 | 493 | /* Clamp the feature level if that is requested. */ |
1ed4e584 LP |
494 | if (t->clamp_feature_level_servfail != _DNS_SERVER_FEATURE_LEVEL_INVALID && |
495 | t->current_feature_level > t->clamp_feature_level_servfail) | |
496 | t->current_feature_level = t->clamp_feature_level_servfail; | |
d001e0a3 LP |
497 | |
498 | log_debug("Using feature level %s for transaction %u.", dns_server_feature_level_to_string(t->current_feature_level), t->id); | |
499 | ||
519ef046 LP |
500 | if (server == t->server) |
501 | return 0; | |
502 | ||
503 | dns_server_unref(t->server); | |
504 | t->server = dns_server_ref(server); | |
505 | ||
b3a9d980 | 506 | t->n_picked_servers++; |
44db02d0 | 507 | |
8aa5afd2 | 508 | log_debug("Using DNS server %s for transaction %u.", strna(dns_server_string_full(t->server)), t->id); |
d001e0a3 | 509 | |
519ef046 LP |
510 | return 1; |
511 | } | |
512 | ||
d001e0a3 | 513 | static void dns_transaction_retry(DnsTransaction *t, bool next_server) { |
8d10d620 LP |
514 | int r; |
515 | ||
516 | assert(t); | |
517 | ||
7ef863a7 LP |
518 | /* Retries the transaction as it is, possibly on a different server */ |
519 | ||
ca55fb88 | 520 | if (next_server && t->scope->protocol == DNS_PROTOCOL_DNS) |
7ef863a7 LP |
521 | log_debug("Retrying transaction %" PRIu16 ", after switching servers.", t->id); |
522 | else | |
523 | log_debug("Retrying transaction %" PRIu16 ".", t->id); | |
8d10d620 LP |
524 | |
525 | /* Before we try again, switch to a new server. */ | |
d001e0a3 | 526 | if (next_server) |
5e8bc852 | 527 | dns_scope_next_dns_server(t->scope, t->server); |
8d10d620 LP |
528 | |
529 | r = dns_transaction_go(t); | |
fd8a3017 LP |
530 | if (r < 0) |
531 | dns_transaction_complete_errno(t, r); | |
8d10d620 LP |
532 | } |
533 | ||
9147b591 LP |
534 | static bool dns_transaction_limited_retry(DnsTransaction *t) { |
535 | assert(t); | |
536 | ||
537 | /* If we haven't tried all different servers yet, let's try again with a different server */ | |
538 | ||
539 | if (t->n_picked_servers >= dns_scope_get_n_dns_servers(t->scope)) | |
540 | return false; | |
541 | ||
542 | dns_transaction_retry(t, /* next_server= */ true); | |
543 | return true; | |
544 | } | |
545 | ||
c02cf2f4 | 546 | static int dns_transaction_maybe_restart(DnsTransaction *t) { |
5278bbfe LP |
547 | int r; |
548 | ||
c02cf2f4 LP |
549 | assert(t); |
550 | ||
9147b591 LP |
551 | /* Restarts the transaction, under a new ID if the feature level of the server changed since we first |
552 | * tried, without changing DNS server. Returns > 0 if the transaction was restarted, 0 if not. */ | |
5278bbfe | 553 | |
c02cf2f4 LP |
554 | if (!t->server) |
555 | return 0; | |
556 | ||
557 | if (t->current_feature_level <= dns_server_possible_feature_level(t->server)) | |
558 | return 0; | |
559 | ||
560 | /* The server's current feature level is lower than when we sent the original query. We learnt something from | |
561 | the response or possibly an auxiliary DNSSEC response that we didn't know before. We take that as reason to | |
562 | restart the whole transaction. This is a good idea to deal with servers that respond rubbish if we include | |
563 | OPT RR or DO bit. One of these cases is documented here, for example: | |
564 | https://open.nlnetlabs.nl/pipermail/dnssec-trigger/2014-November/000376.html */ | |
565 | ||
4dd15077 LP |
566 | log_debug("Server feature level is now lower than when we began our transaction. Restarting with new ID."); |
567 | dns_transaction_shuffle_id(t); | |
5278bbfe LP |
568 | |
569 | r = dns_transaction_go(t); | |
570 | if (r < 0) | |
571 | return r; | |
572 | ||
573 | return 1; | |
c02cf2f4 LP |
574 | } |
575 | ||
98767d75 IT |
576 | static void on_transaction_stream_error(DnsTransaction *t, int error) { |
577 | assert(t); | |
ec2c5e43 | 578 | |
80710ade | 579 | dns_transaction_close_connection(t, true); |
ec2c5e43 | 580 | |
a1a3f73a | 581 | if (ERRNO_IS_DISCONNECT(error)) { |
0791110f LP |
582 | if (t->scope->protocol == DNS_PROTOCOL_LLMNR) { |
583 | /* If the LLMNR/TCP connection failed, the host doesn't support LLMNR, and we cannot answer the | |
584 | * question on this scope. */ | |
585 | dns_transaction_complete(t, DNS_TRANSACTION_NOT_FOUND); | |
daab72ea | 586 | return; |
0791110f LP |
587 | } |
588 | ||
d001e0a3 | 589 | dns_transaction_retry(t, true); |
daab72ea | 590 | return; |
ac720200 | 591 | } |
fd8a3017 LP |
592 | if (error != 0) |
593 | dns_transaction_complete_errno(t, error); | |
98767d75 IT |
594 | } |
595 | ||
43fc4baa LP |
596 | static int dns_transaction_on_stream_packet(DnsTransaction *t, DnsStream *s, DnsPacket *p) { |
597 | bool encrypted; | |
598 | ||
98767d75 | 599 | assert(t); |
43fc4baa | 600 | assert(s); |
98767d75 IT |
601 | assert(p); |
602 | ||
43fc4baa LP |
603 | encrypted = s->encrypted; |
604 | ||
80710ade | 605 | dns_transaction_close_connection(t, true); |
ec2c5e43 | 606 | |
a4076574 | 607 | if (dns_packet_validate_reply(p) <= 0) { |
a20b9592 | 608 | log_debug("Invalid TCP reply packet."); |
a4076574 LP |
609 | dns_transaction_complete(t, DNS_TRANSACTION_INVALID_REPLY); |
610 | return 0; | |
611 | } | |
612 | ||
613 | dns_scope_check_conflicts(t->scope, p); | |
614 | ||
ec2c5e43 | 615 | t->block_gc++; |
43fc4baa | 616 | dns_transaction_process_reply(t, p, encrypted); |
ec2c5e43 LP |
617 | t->block_gc--; |
618 | ||
519ef046 LP |
619 | /* If the response wasn't useful, then complete the transition |
620 | * now. After all, we are the worst feature set now with TCP | |
621 | * sockets, and there's really no point in retrying. */ | |
ec2c5e43 LP |
622 | if (t->state == DNS_TRANSACTION_PENDING) |
623 | dns_transaction_complete(t, DNS_TRANSACTION_INVALID_REPLY); | |
598f44bd LP |
624 | else |
625 | dns_transaction_gc(t); | |
ec2c5e43 LP |
626 | |
627 | return 0; | |
628 | } | |
629 | ||
98767d75 | 630 | static int on_stream_complete(DnsStream *s, int error) { |
7172e4ee | 631 | assert(s); |
98767d75 IT |
632 | |
633 | if (ERRNO_IS_DISCONNECT(error) && s->protocol != DNS_PROTOCOL_LLMNR) { | |
98767d75 IT |
634 | log_debug_errno(error, "Connection failure for DNS TCP stream: %m"); |
635 | ||
636 | if (s->transactions) { | |
97d5d905 LP |
637 | DnsTransaction *t; |
638 | ||
98767d75 | 639 | t = s->transactions; |
3da3cdd5 | 640 | dns_server_packet_lost(t->server, IPPROTO_TCP, t->current_feature_level); |
98767d75 IT |
641 | } |
642 | } | |
643 | ||
3db1e6a8 YW |
644 | if (error != 0) { |
645 | /* First, detach the stream from the server. Otherwise, transactions attached to this stream | |
646 | * may be restarted by on_transaction_stream_error() below with this stream. */ | |
647 | dns_stream_detach(s); | |
648 | ||
649 | /* Do not use LIST_FOREACH() here, as | |
650 | * on_transaction_stream_error() | |
651 | * -> dns_transaction_complete_errno() | |
652 | * -> dns_transaction_free() | |
653 | * may free multiple transactions in the list. */ | |
654 | DnsTransaction *t; | |
655 | while ((t = s->transactions)) | |
98767d75 | 656 | on_transaction_stream_error(t, error); |
3db1e6a8 | 657 | } |
98767d75 | 658 | |
97d5d905 | 659 | return 0; |
98767d75 IT |
660 | } |
661 | ||
624f907e | 662 | static int on_stream_packet(DnsStream *s, DnsPacket *p) { |
98767d75 IT |
663 | DnsTransaction *t; |
664 | ||
aa337a5e | 665 | assert(s); |
624f907e YW |
666 | assert(s->manager); |
667 | assert(p); | |
98767d75 IT |
668 | |
669 | t = hashmap_get(s->manager->dns_transactions, UINT_TO_PTR(DNS_PACKET_ID(p))); | |
b52eac20 LP |
670 | if (t && t->stream == s) /* Validate that the stream we got this on actually is the stream the |
671 | * transaction was using. */ | |
43fc4baa | 672 | return dns_transaction_on_stream_packet(t, s, p); |
98767d75 | 673 | |
8227cfa1 | 674 | /* Ignore incorrect transaction id as an old transaction can have been canceled. */ |
30f9e0bf | 675 | log_debug("Received unexpected TCP reply packet with id %" PRIu16 ", ignoring.", DNS_PACKET_ID(p)); |
aa337a5e | 676 | return 0; |
98767d75 IT |
677 | } |
678 | ||
da9de738 | 679 | static uint16_t dns_transaction_port(DnsTransaction *t) { |
775ae354 LP |
680 | assert(t); |
681 | ||
da9de738 YW |
682 | if (t->server->port > 0) |
683 | return t->server->port; | |
775ae354 | 684 | |
da9de738 | 685 | return DNS_SERVER_FEATURE_LEVEL_IS_TLS(t->current_feature_level) ? 853 : 53; |
ec962fba LP |
686 | } |
687 | ||
98767d75 | 688 | static int dns_transaction_emit_tcp(DnsTransaction *t) { |
e1158539 | 689 | usec_t stream_timeout_usec = DNS_STREAM_DEFAULT_TIMEOUT_USEC; |
98767d75 | 690 | _cleanup_(dns_stream_unrefp) DnsStream *s = NULL; |
254d1313 | 691 | _cleanup_close_ int fd = -EBADF; |
91ccab1e | 692 | union sockaddr_union sa; |
652ba568 | 693 | DnsStreamType type; |
ec2c5e43 LP |
694 | int r; |
695 | ||
696 | assert(t); | |
775ae354 | 697 | assert(t->sent); |
ec2c5e43 | 698 | |
80710ade | 699 | dns_transaction_close_connection(t, true); |
ec2c5e43 | 700 | |
106784eb | 701 | switch (t->scope->protocol) { |
519ef046 | 702 | |
106784eb | 703 | case DNS_PROTOCOL_DNS: |
519ef046 LP |
704 | r = dns_transaction_pick_server(t); |
705 | if (r < 0) | |
706 | return r; | |
707 | ||
49ef064c LP |
708 | if (manager_server_is_stub(t->scope->manager, t->server)) |
709 | return -ELOOP; | |
710 | ||
775ae354 LP |
711 | if (!t->bypass) { |
712 | if (!dns_server_dnssec_supported(t->server) && dns_type_is_dnssec(dns_transaction_key(t)->type)) | |
713 | return -EOPNOTSUPP; | |
91adc4db | 714 | |
775ae354 LP |
715 | r = dns_server_adjust_opt(t->server, t->sent, t->current_feature_level); |
716 | if (r < 0) | |
717 | return r; | |
718 | } | |
519ef046 | 719 | |
5d67a7ae | 720 | if (t->server->stream && (DNS_SERVER_FEATURE_LEVEL_IS_TLS(t->current_feature_level) == t->server->stream->encrypted)) |
98767d75 IT |
721 | s = dns_stream_ref(t->server->stream); |
722 | else | |
da9de738 | 723 | fd = dns_scope_socket_tcp(t->scope, AF_UNSPEC, NULL, t->server, dns_transaction_port(t), &sa); |
98767d75 | 724 | |
e1158539 LP |
725 | /* Lower timeout in DNS-over-TLS opportunistic mode. In environments where DoT is blocked |
726 | * without ICMP response overly long delays when contacting DoT servers are nasty, in | |
727 | * particular if multiple DNS servers are defined which we try in turn and all are | |
728 | * blocked. Hence, substantially lower the timeout in that case. */ | |
729 | if (DNS_SERVER_FEATURE_LEVEL_IS_TLS(t->current_feature_level) && | |
730 | dns_server_get_dns_over_tls_mode(t->server) == DNS_OVER_TLS_OPPORTUNISTIC) | |
731 | stream_timeout_usec = DNS_STREAM_OPPORTUNISTIC_TLS_TIMEOUT_USEC; | |
732 | ||
652ba568 | 733 | type = DNS_STREAM_LOOKUP; |
106784eb | 734 | break; |
ec2c5e43 | 735 | |
106784eb | 736 | case DNS_PROTOCOL_LLMNR: |
a8f6397f | 737 | /* When we already received a reply to this (but it was truncated), send to its sender address */ |
ec2c5e43 | 738 | if (t->received) |
91ccab1e | 739 | fd = dns_scope_socket_tcp(t->scope, t->received->family, &t->received->sender, NULL, t->received->sender_port, &sa); |
ec2c5e43 LP |
740 | else { |
741 | union in_addr_union address; | |
a7f7d1bd | 742 | int family = AF_UNSPEC; |
ec2c5e43 LP |
743 | |
744 | /* Otherwise, try to talk to the owner of a | |
745 | * the IP address, in case this is a reverse | |
746 | * PTR lookup */ | |
f52e61da | 747 | |
42df9532 | 748 | r = dns_name_address(dns_resource_key_name(dns_transaction_key(t)), &family, &address); |
ec2c5e43 LP |
749 | if (r < 0) |
750 | return r; | |
751 | if (r == 0) | |
752 | return -EINVAL; | |
9e08a6e0 | 753 | if (family != t->scope->family) |
9318cdd3 | 754 | return -ESRCH; |
ec2c5e43 | 755 | |
91ccab1e | 756 | fd = dns_scope_socket_tcp(t->scope, family, &address, NULL, LLMNR_PORT, &sa); |
ec2c5e43 | 757 | } |
106784eb | 758 | |
652ba568 | 759 | type = DNS_STREAM_LLMNR_SEND; |
106784eb DM |
760 | break; |
761 | ||
762 | default: | |
ec2c5e43 | 763 | return -EAFNOSUPPORT; |
106784eb | 764 | } |
ec2c5e43 | 765 | |
98767d75 IT |
766 | if (!s) { |
767 | if (fd < 0) | |
768 | return fd; | |
ec2c5e43 | 769 | |
18230451 YW |
770 | r = dns_stream_new(t->scope->manager, &s, type, t->scope->protocol, fd, &sa, |
771 | on_stream_packet, on_stream_complete, stream_timeout_usec); | |
98767d75 IT |
772 | if (r < 0) |
773 | return r; | |
774 | ||
254d1313 | 775 | fd = -EBADF; |
98767d75 | 776 | |
56ddbf10 | 777 | #if ENABLE_DNS_OVER_TLS |
199dda9c LP |
778 | if (t->scope->protocol == DNS_PROTOCOL_DNS && |
779 | DNS_SERVER_FEATURE_LEVEL_IS_TLS(t->current_feature_level)) { | |
780 | ||
b02a7e1a | 781 | assert(t->server); |
6016fcb0 | 782 | r = dnstls_stream_connect_tls(s, t->server); |
5d67a7ae IT |
783 | if (r < 0) |
784 | return r; | |
785 | } | |
786 | #endif | |
787 | ||
19feb28f | 788 | if (t->server) { |
904dcaf9 | 789 | dns_server_unref_stream(t->server); |
19feb28f | 790 | s->server = dns_server_ref(t->server); |
8227cfa1 | 791 | t->server->stream = dns_stream_ref(s); |
19feb28f IT |
792 | } |
793 | ||
98767d75 IT |
794 | /* The interface index is difficult to determine if we are |
795 | * connecting to the local host, hence fill this in right away | |
796 | * instead of determining it from the socket */ | |
797 | s->ifindex = dns_scope_ifindex(t->scope); | |
798 | } | |
799 | ||
800 | t->stream = TAKE_PTR(s); | |
801 | LIST_PREPEND(transactions_by_stream, t->stream->transactions, t); | |
ec2c5e43 LP |
802 | |
803 | r = dns_stream_write_packet(t->stream, t->sent); | |
804 | if (r < 0) { | |
80710ade | 805 | dns_transaction_close_connection(t, /* use_graveyard= */ false); |
ec2c5e43 LP |
806 | return r; |
807 | } | |
808 | ||
519ef046 LP |
809 | dns_transaction_reset_answer(t); |
810 | ||
cbe4216d LP |
811 | t->tried_stream = true; |
812 | ||
ec2c5e43 LP |
813 | return 0; |
814 | } | |
815 | ||
547973de | 816 | static void dns_transaction_cache_answer(DnsTransaction *t) { |
547973de LP |
817 | assert(t); |
818 | ||
819 | /* For mDNS we cache whenever we get the packet, rather than | |
820 | * in each transaction. */ | |
821 | if (!IN_SET(t->scope->protocol, DNS_PROTOCOL_DNS, DNS_PROTOCOL_LLMNR)) | |
822 | return; | |
823 | ||
ceeddf79 | 824 | /* Caching disabled? */ |
37d7a7d9 | 825 | if (t->scope->manager->enable_cache == DNS_CACHE_MODE_NO) |
ceeddf79 MP |
826 | return; |
827 | ||
775ae354 LP |
828 | /* If validation is turned off for this transaction, but DNSSEC is on, then let's not cache this */ |
829 | if (FLAGS_SET(t->query_flags, SD_RESOLVED_NO_VALIDATE) && t->scope->dnssec_mode != DNSSEC_NO) | |
830 | return; | |
831 | ||
6d8325f6 PS |
832 | /* Packet from localhost? */ |
833 | if (!t->scope->manager->cache_from_localhost && | |
834 | in_addr_is_localhost(t->received->family, &t->received->sender) != 0) | |
547973de LP |
835 | return; |
836 | ||
547973de | 837 | dns_cache_put(&t->scope->cache, |
37d7a7d9 | 838 | t->scope->manager->enable_cache, |
a78049fc | 839 | t->scope->protocol, |
42df9532 | 840 | dns_transaction_key(t), |
547973de LP |
841 | t->answer_rcode, |
842 | t->answer, | |
775ae354 | 843 | DNS_PACKET_CD(t->received) ? t->received : NULL, /* only cache full packets with CD on, |
7227dd81 | 844 | * since our use case for caching them |
775ae354 LP |
845 | * is "bypass" mode which is only |
846 | * enabled for CD packets. */ | |
6f055e43 | 847 | t->answer_query_flags, |
775ae354 | 848 | t->answer_dnssec_result, |
d3760be0 | 849 | t->answer_nsec_ttl, |
547973de | 850 | t->received->family, |
5ed91481 KV |
851 | &t->received->sender, |
852 | t->scope->manager->stale_retention_usec); | |
547973de LP |
853 | } |
854 | ||
105e1512 LP |
855 | static bool dns_transaction_dnssec_is_live(DnsTransaction *t) { |
856 | DnsTransaction *dt; | |
105e1512 LP |
857 | |
858 | assert(t); | |
859 | ||
90e74a66 | 860 | SET_FOREACH(dt, t->dnssec_transactions) |
105e1512 LP |
861 | if (DNS_TRANSACTION_IS_LIVE(dt->state)) |
862 | return true; | |
863 | ||
864 | return false; | |
865 | } | |
866 | ||
942eb2e7 LP |
867 | static int dns_transaction_dnssec_ready(DnsTransaction *t) { |
868 | DnsTransaction *dt; | |
e30a62bf | 869 | int r; |
942eb2e7 LP |
870 | |
871 | assert(t); | |
872 | ||
873 | /* Checks whether the auxiliary DNSSEC transactions of our transaction have completed, or are still | |
874 | * ongoing. Returns 0, if we aren't ready for the DNSSEC validation, positive if we are. */ | |
875 | ||
90e74a66 | 876 | SET_FOREACH(dt, t->dnssec_transactions) { |
942eb2e7 LP |
877 | |
878 | switch (dt->state) { | |
879 | ||
880 | case DNS_TRANSACTION_NULL: | |
881 | case DNS_TRANSACTION_PENDING: | |
882 | case DNS_TRANSACTION_VALIDATING: | |
883 | /* Still ongoing */ | |
884 | return 0; | |
885 | ||
886 | case DNS_TRANSACTION_RCODE_FAILURE: | |
b3c6b00a | 887 | if (!IN_SET(dt->answer_rcode, DNS_RCODE_NXDOMAIN, DNS_RCODE_SERVFAIL)) { |
0d609349 | 888 | log_debug("Auxiliary DNSSEC RR query failed with rcode=%s.", FORMAT_DNS_RCODE(dt->answer_rcode)); |
942eb2e7 LP |
889 | goto fail; |
890 | } | |
891 | ||
b3c6b00a | 892 | /* Fall-through: NXDOMAIN/SERVFAIL is good enough for us. This is because some DNS servers |
5238e957 | 893 | * erroneously return NXDOMAIN/SERVFAIL for empty non-terminals (Akamai...) or missing DS |
b3c6b00a LP |
894 | * records (Facebook), and we need to handle that nicely, when asking for parent SOA or similar |
895 | * RRs to make unsigned proofs. */ | |
942eb2e7 LP |
896 | |
897 | case DNS_TRANSACTION_SUCCESS: | |
898 | /* All good. */ | |
899 | break; | |
900 | ||
3a768db4 | 901 | case DNS_TRANSACTION_DNSSEC_FAILED: |
942eb2e7 | 902 | /* We handle DNSSEC failures different from other errors, as we care about the DNSSEC |
e30a62bf | 903 | * validation result */ |
942eb2e7 | 904 | |
a72cf22d YW |
905 | log_debug("Auxiliary DNSSEC RR query failed validation: %s%s%s%s%s%s", |
906 | dnssec_result_to_string(dt->answer_dnssec_result), | |
907 | dt->answer_ede_rcode >= 0 ? " (" : "", | |
908 | dt->answer_ede_rcode >= 0 ? FORMAT_DNS_EDE_RCODE(dt->answer_ede_rcode) : "", | |
909 | (dt->answer_ede_rcode >= 0 && !isempty(dt->answer_ede_msg)) ? ": " : "", | |
910 | dt->answer_ede_rcode >= 0 ? strempty(dt->answer_ede_msg) : "", | |
911 | dt->answer_ede_rcode >= 0 ? ")" : ""); | |
912 | ||
913 | /* Copy error code over */ | |
914 | t->answer_dnssec_result = dt->answer_dnssec_result; | |
915 | t->answer_ede_rcode = dt->answer_ede_rcode; | |
916 | r = free_and_strdup(&t->answer_ede_msg, dt->answer_ede_msg); | |
917 | if (r < 0) | |
918 | log_oom_debug(); | |
919 | ||
0dfea62e RP |
920 | /* The answer would normally be replaced by the validated subset, but at this point |
921 | * we aren't going to bother validating the rest, so just drop it. */ | |
3a768db4 | 922 | t->answer = dns_answer_unref(t->answer); |
0dfea62e | 923 | |
942eb2e7 LP |
924 | dns_transaction_complete(t, DNS_TRANSACTION_DNSSEC_FAILED); |
925 | return 0; | |
926 | ||
942eb2e7 LP |
927 | default: |
928 | log_debug("Auxiliary DNSSEC RR query failed with %s", dns_transaction_state_to_string(dt->state)); | |
929 | goto fail; | |
930 | } | |
931 | } | |
932 | ||
933 | /* All is ready, we can go and validate */ | |
934 | return 1; | |
935 | ||
936 | fail: | |
e30a62bf LP |
937 | /* Some auxiliary DNSSEC transaction failed for some reason. Maybe we learned something about the |
938 | * server due to this failure, and the feature level is now different? Let's see and restart the | |
939 | * transaction if so. If not, let's propagate the auxiliary failure. | |
940 | * | |
941 | * This is particularly relevant if an auxiliary request figured out that DNSSEC doesn't work, and we | |
942 | * are in permissive DNSSEC mode, and thus should restart things without DNSSEC magic. */ | |
943 | r = dns_transaction_maybe_restart(t); | |
944 | if (r < 0) | |
945 | return r; | |
946 | if (r > 0) | |
947 | return 0; /* don't validate just yet, we restarted things */ | |
948 | ||
942eb2e7 LP |
949 | t->answer_dnssec_result = DNSSEC_FAILED_AUXILIARY; |
950 | dns_transaction_complete(t, DNS_TRANSACTION_DNSSEC_FAILED); | |
951 | return 0; | |
952 | } | |
953 | ||
547973de LP |
954 | static void dns_transaction_process_dnssec(DnsTransaction *t) { |
955 | int r; | |
956 | ||
957 | assert(t); | |
958 | ||
959 | /* Are there ongoing DNSSEC transactions? If so, let's wait for them. */ | |
942eb2e7 | 960 | r = dns_transaction_dnssec_ready(t); |
7cc6ed7b LP |
961 | if (r < 0) |
962 | goto fail; | |
942eb2e7 | 963 | if (r == 0) /* We aren't ready yet (or one of our auxiliary transactions failed, and we shouldn't validate now */ |
547973de LP |
964 | return; |
965 | ||
c02cf2f4 LP |
966 | /* See if we learnt things from the additional DNSSEC transactions, that we didn't know before, and better |
967 | * restart the lookup immediately. */ | |
968 | r = dns_transaction_maybe_restart(t); | |
7cc6ed7b LP |
969 | if (r < 0) |
970 | goto fail; | |
c02cf2f4 LP |
971 | if (r > 0) /* Transaction got restarted... */ |
972 | return; | |
973 | ||
547973de LP |
974 | /* All our auxiliary DNSSEC transactions are complete now. Try |
975 | * to validate our RRset now. */ | |
976 | r = dns_transaction_validate_dnssec(t); | |
fcfaff12 LP |
977 | if (r == -EBADMSG) { |
978 | dns_transaction_complete(t, DNS_TRANSACTION_INVALID_REPLY); | |
979 | return; | |
980 | } | |
7cc6ed7b LP |
981 | if (r < 0) |
982 | goto fail; | |
547973de | 983 | |
b652d4a2 LP |
984 | if (t->answer_dnssec_result == DNSSEC_INCOMPATIBLE_SERVER && |
985 | t->scope->dnssec_mode == DNSSEC_YES) { | |
e82b1132 LP |
986 | |
987 | /* We are not in automatic downgrade mode, and the server is bad. Let's try a different server, maybe | |
988 | * that works. */ | |
989 | ||
9147b591 | 990 | if (dns_transaction_limited_retry(t)) |
e82b1132 | 991 | return; |
e82b1132 LP |
992 | |
993 | /* OK, let's give up, apparently all servers we tried didn't work. */ | |
b652d4a2 LP |
994 | dns_transaction_complete(t, DNS_TRANSACTION_DNSSEC_FAILED); |
995 | return; | |
996 | } | |
997 | ||
019036a4 | 998 | if (!IN_SET(t->answer_dnssec_result, |
b652d4a2 LP |
999 | _DNSSEC_RESULT_INVALID, /* No DNSSEC validation enabled */ |
1000 | DNSSEC_VALIDATED, /* Answer is signed and validated successfully */ | |
1001 | DNSSEC_UNSIGNED, /* Answer is right-fully unsigned */ | |
1002 | DNSSEC_INCOMPATIBLE_SERVER)) { /* Server does not do DNSSEC (Yay, we are downgrade attack vulnerable!) */ | |
547973de LP |
1003 | dns_transaction_complete(t, DNS_TRANSACTION_DNSSEC_FAILED); |
1004 | return; | |
1005 | } | |
1006 | ||
1e02e182 LP |
1007 | if (t->answer_dnssec_result == DNSSEC_INCOMPATIBLE_SERVER) |
1008 | dns_server_warn_downgrade(t->server); | |
1009 | ||
547973de LP |
1010 | dns_transaction_cache_answer(t); |
1011 | ||
1012 | if (t->answer_rcode == DNS_RCODE_SUCCESS) | |
1013 | dns_transaction_complete(t, DNS_TRANSACTION_SUCCESS); | |
1014 | else | |
3bbdc31d | 1015 | dns_transaction_complete(t, DNS_TRANSACTION_RCODE_FAILURE); |
7cc6ed7b LP |
1016 | |
1017 | return; | |
1018 | ||
1019 | fail: | |
fd8a3017 | 1020 | dns_transaction_complete_errno(t, r); |
547973de LP |
1021 | } |
1022 | ||
eac7cda2 LP |
1023 | static int dns_transaction_has_positive_answer(DnsTransaction *t, DnsAnswerFlags *flags) { |
1024 | int r; | |
1025 | ||
1026 | assert(t); | |
1027 | ||
1028 | /* Checks whether the answer is positive, i.e. either a direct | |
1029 | * answer to the question, or a CNAME/DNAME for it */ | |
1030 | ||
42df9532 | 1031 | r = dns_answer_match_key(t->answer, dns_transaction_key(t), flags); |
eac7cda2 LP |
1032 | if (r != 0) |
1033 | return r; | |
1034 | ||
42df9532 | 1035 | r = dns_answer_find_cname_or_dname(t->answer, dns_transaction_key(t), NULL, flags); |
eac7cda2 LP |
1036 | if (r != 0) |
1037 | return r; | |
1038 | ||
1039 | return false; | |
1040 | } | |
1041 | ||
1042 | static int dns_transaction_fix_rcode(DnsTransaction *t) { | |
1043 | int r; | |
1044 | ||
1045 | assert(t); | |
1046 | ||
1047 | /* Fix up the RCODE to SUCCESS if we get at least one matching RR in a response. Note that this contradicts the | |
1048 | * DNS RFCs a bit. Specifically, RFC 6604 Section 3 clarifies that the RCODE shall say something about a | |
1049 | * CNAME/DNAME chain element coming after the last chain element contained in the message, and not the first | |
1050 | * one included. However, it also indicates that not all DNS servers implement this correctly. Moreover, when | |
1051 | * using DNSSEC we usually only can prove the first element of a CNAME/DNAME chain anyway, hence let's settle | |
1052 | * on always processing the RCODE as referring to the immediate look-up we do, i.e. the first element of a | |
1053 | * CNAME/DNAME chain. This way, we uniformly handle CNAME/DNAME chains, regardless if the DNS server | |
1054 | * incorrectly implements RCODE, whether DNSSEC is in use, or whether the DNS server only supplied us with an | |
1055 | * incomplete CNAME/DNAME chain. | |
1056 | * | |
1057 | * Or in other words: if we get at least one positive reply in a message we patch NXDOMAIN to become SUCCESS, | |
1058 | * and then rely on the CNAME chasing logic to figure out that there's actually a CNAME error with a new | |
1059 | * lookup. */ | |
1060 | ||
1061 | if (t->answer_rcode != DNS_RCODE_NXDOMAIN) | |
1062 | return 0; | |
1063 | ||
1064 | r = dns_transaction_has_positive_answer(t, NULL); | |
1065 | if (r <= 0) | |
1066 | return r; | |
1067 | ||
1068 | t->answer_rcode = DNS_RCODE_SUCCESS; | |
1069 | return 0; | |
1070 | } | |
1071 | ||
43fc4baa | 1072 | void dns_transaction_process_reply(DnsTransaction *t, DnsPacket *p, bool encrypted) { |
acbf761b | 1073 | bool retry_with_tcp = false; |
ec2c5e43 LP |
1074 | int r; |
1075 | ||
1076 | assert(t); | |
1077 | assert(p); | |
9df3ba6c TG |
1078 | assert(t->scope); |
1079 | assert(t->scope->manager); | |
ec2c5e43 | 1080 | |
5a7e41a3 LP |
1081 | if (t->state != DNS_TRANSACTION_PENDING) |
1082 | return; | |
1083 | ||
bc837621 KV |
1084 | /* Increment the total failure counter only when it is the first attempt at querying and the upstream |
1085 | * server returns a failure response code. This ensures a more accurate count of the number of queries | |
1086 | * that received a failure response code, as it doesn't consider retries. */ | |
1087 | ||
1088 | if (t->n_attempts == 1 && !IN_SET(DNS_PACKET_RCODE(p), DNS_RCODE_SUCCESS, DNS_RCODE_NXDOMAIN)) | |
1089 | t->scope->manager->n_failure_responses_total++; | |
1090 | ||
ec2c5e43 LP |
1091 | /* Note that this call might invalidate the query. Callers |
1092 | * should hence not attempt to access the query or transaction | |
1093 | * after calling this function. */ | |
1094 | ||
f7155840 LP |
1095 | log_debug("Processing incoming packet of size %zu on transaction %" PRIu16" (rcode=%s).", |
1096 | p->size, | |
0d609349 | 1097 | t->id, FORMAT_DNS_RCODE(DNS_PACKET_RCODE(p))); |
b5efcf29 | 1098 | |
106784eb | 1099 | switch (t->scope->protocol) { |
b5efcf29 | 1100 | |
106784eb | 1101 | case DNS_PROTOCOL_LLMNR: |
97ebebbc | 1102 | /* For LLMNR we will not accept any packets from other interfaces */ |
ec2c5e43 | 1103 | |
97ebebbc | 1104 | if (p->ifindex != dns_scope_ifindex(t->scope)) |
ec2c5e43 LP |
1105 | return; |
1106 | ||
1107 | if (p->family != t->scope->family) | |
1108 | return; | |
1109 | ||
1110 | /* Tentative packets are not full responses but still | |
1111 | * useful for identifying uniqueness conflicts during | |
1112 | * probing. */ | |
8b757a38 | 1113 | if (DNS_PACKET_LLMNR_T(p)) { |
ec2c5e43 LP |
1114 | dns_transaction_tentative(t, p); |
1115 | return; | |
1116 | } | |
106784eb DM |
1117 | |
1118 | break; | |
1119 | ||
4e5bf5e1 | 1120 | case DNS_PROTOCOL_MDNS: |
4e5bf5e1 | 1121 | /* For mDNS we will not accept any packets from other interfaces */ |
97ebebbc LP |
1122 | |
1123 | if (p->ifindex != dns_scope_ifindex(t->scope)) | |
4e5bf5e1 DM |
1124 | return; |
1125 | ||
1126 | if (p->family != t->scope->family) | |
1127 | return; | |
1128 | ||
1129 | break; | |
1130 | ||
106784eb | 1131 | case DNS_PROTOCOL_DNS: |
8ad182a1 LP |
1132 | /* Note that we do not need to verify the |
1133 | * addresses/port numbers of incoming traffic, as we | |
1134 | * invoked connect() on our UDP socket in which case | |
1135 | * the kernel already does the needed verification for | |
1136 | * us. */ | |
106784eb DM |
1137 | break; |
1138 | ||
1139 | default: | |
04499a70 | 1140 | assert_not_reached(); |
ec2c5e43 LP |
1141 | } |
1142 | ||
899e3cda ZJS |
1143 | if (t->received != p) |
1144 | DNS_PACKET_REPLACE(t->received, dns_packet_ref(p)); | |
ec2c5e43 | 1145 | |
c3bc53e6 LP |
1146 | t->answer_source = DNS_TRANSACTION_NETWORK; |
1147 | ||
ec2c5e43 LP |
1148 | if (p->ipproto == IPPROTO_TCP) { |
1149 | if (DNS_PACKET_TC(p)) { | |
1150 | /* Truncated via TCP? Somebody must be fucking with us */ | |
1151 | dns_transaction_complete(t, DNS_TRANSACTION_INVALID_REPLY); | |
1152 | return; | |
1153 | } | |
1154 | ||
1155 | if (DNS_PACKET_ID(p) != t->id) { | |
1156 | /* Not the reply to our query? Somebody must be fucking with us */ | |
1157 | dns_transaction_complete(t, DNS_TRANSACTION_INVALID_REPLY); | |
1158 | return; | |
1159 | } | |
1160 | } | |
1161 | ||
3fcd8364 RP |
1162 | if (DNS_PACKET_TC(p)) { |
1163 | ||
1164 | /* Truncated packets for mDNS are not allowed. Give up immediately. */ | |
1165 | if (t->scope->protocol == DNS_PROTOCOL_MDNS) { | |
1166 | dns_transaction_complete(t, DNS_TRANSACTION_INVALID_REPLY); | |
1167 | return; | |
1168 | } | |
1169 | ||
1170 | /* Response was truncated, let's try again with good old TCP */ | |
1171 | log_debug("Reply truncated, retrying via TCP."); | |
1172 | retry_with_tcp = true; | |
1173 | ||
1174 | } else if (t->scope->protocol == DNS_PROTOCOL_DNS && | |
1175 | DNS_PACKET_IS_FRAGMENTED(p)) { | |
1176 | ||
1177 | /* Report the fragment size, so that we downgrade from LARGE to regular EDNS0 if needed */ | |
1178 | if (t->server) | |
1179 | dns_server_packet_udp_fragmented(t->server, dns_packet_size_unfragmented(p)); | |
1180 | ||
1181 | if (t->current_feature_level > DNS_SERVER_FEATURE_LEVEL_UDP) { | |
1182 | /* Packet was fragmented. Let's retry with TCP to avoid fragmentation attack | |
1183 | * issues. (We don't do that on the lowest feature level however, since crappy DNS | |
1184 | * servers often do not implement TCP, hence falling back to TCP on fragmentation is | |
1185 | * counter-productive there.) */ | |
1186 | ||
1187 | log_debug("Reply fragmented, retrying via TCP. (Largest fragment size: %zu; Datagram size: %zu)", | |
1188 | p->fragsize, p->size); | |
1189 | retry_with_tcp = true; | |
1190 | } | |
1191 | } | |
1192 | ||
1193 | if (retry_with_tcp) { | |
1194 | r = dns_transaction_emit_tcp(t); | |
1195 | if (r == -ESRCH) { | |
1196 | /* No servers found? Damn! */ | |
1197 | dns_transaction_complete(t, DNS_TRANSACTION_NO_SERVERS); | |
1198 | return; | |
1199 | } | |
1200 | if (r == -EOPNOTSUPP) { | |
1201 | /* Tried to ask for DNSSEC RRs, on a server that doesn't do DNSSEC */ | |
1202 | dns_transaction_complete(t, DNS_TRANSACTION_RR_TYPE_UNSUPPORTED); | |
1203 | return; | |
1204 | } | |
1205 | if (r < 0) { | |
1206 | /* On LLMNR, if we cannot connect to the host, | |
1207 | * we immediately give up */ | |
1208 | if (t->scope->protocol != DNS_PROTOCOL_DNS) | |
1209 | goto fail; | |
1210 | ||
1211 | /* On DNS, couldn't send? Try immediately again, with a new server */ | |
1212 | if (dns_transaction_limited_retry(t)) | |
1213 | return; | |
1214 | ||
1215 | /* No new server to try, give up */ | |
1216 | dns_transaction_complete(t, DNS_TRANSACTION_ATTEMPTS_MAX_REACHED); | |
1217 | } | |
1218 | ||
1219 | return; | |
1220 | } | |
1221 | ||
1222 | /* After the superficial checks, actually parse the message. */ | |
1223 | r = dns_packet_extract(p); | |
1224 | if (r < 0) { | |
1225 | if (t->server) { | |
1226 | dns_server_packet_invalid(t->server, t->current_feature_level); | |
1227 | ||
1228 | r = dns_transaction_maybe_restart(t); | |
1229 | if (r < 0) | |
1230 | goto fail; | |
1231 | if (r > 0) /* Transaction got restarted... */ | |
1232 | return; | |
1233 | } | |
1234 | ||
1235 | dns_transaction_complete(t, DNS_TRANSACTION_INVALID_REPLY); | |
1236 | return; | |
1237 | } | |
1238 | ||
9df3ba6c | 1239 | switch (t->scope->protocol) { |
8af5b883 | 1240 | |
ac684446 | 1241 | case DNS_PROTOCOL_DNS: { |
9df3ba6c TG |
1242 | assert(t->server); |
1243 | ||
c08fd7be | 1244 | (void) dns_packet_ede_rcode(p, &t->answer_ede_rcode, &t->answer_ede_msg); |
ac684446 | 1245 | |
775ae354 LP |
1246 | if (!t->bypass && |
1247 | IN_SET(DNS_PACKET_RCODE(p), DNS_RCODE_FORMERR, DNS_RCODE_SERVFAIL, DNS_RCODE_NOTIMP)) { | |
ac684446 RP |
1248 | /* If the server has replied with detailed error data, using a degraded feature set |
1249 | * will likely not help anyone. Examine the detailed error to determine the best | |
1250 | * course of action. */ | |
c08fd7be | 1251 | if (t->answer_ede_rcode >= 0 && DNS_PACKET_RCODE(p) == DNS_RCODE_SERVFAIL) { |
ac684446 RP |
1252 | /* These codes are related to DNSSEC configuration errors. If accurate, |
1253 | * this is the domain operator's problem, and retrying won't help. */ | |
c08fd7be | 1254 | if (dns_ede_rcode_is_dnssec(t->answer_ede_rcode)) { |
ac684446 | 1255 | log_debug("Server returned error: %s (%s%s%s). Lookup failed.", |
c08fd7be YW |
1256 | FORMAT_DNS_RCODE(DNS_PACKET_RCODE(p)), |
1257 | FORMAT_DNS_EDE_RCODE(t->answer_ede_rcode), | |
1258 | isempty(t->answer_ede_msg) ? "" : ": ", | |
1259 | strempty(t->answer_ede_msg)); | |
a72cf22d YW |
1260 | |
1261 | t->answer_dnssec_result = DNSSEC_UPSTREAM_FAILURE; | |
9ae51762 | 1262 | dns_transaction_complete(t, DNS_TRANSACTION_DNSSEC_FAILED); |
ac684446 RP |
1263 | return; |
1264 | } | |
1265 | ||
1266 | /* These codes probably indicate a transient error. Let's try again. */ | |
c08fd7be | 1267 | if (IN_SET(t->answer_ede_rcode, DNS_EDE_RCODE_NOT_READY, DNS_EDE_RCODE_NET_ERROR)) { |
ac684446 | 1268 | log_debug("Server returned error: %s (%s%s%s), retrying transaction.", |
c08fd7be YW |
1269 | FORMAT_DNS_RCODE(DNS_PACKET_RCODE(p)), |
1270 | FORMAT_DNS_EDE_RCODE(t->answer_ede_rcode), | |
1271 | isempty(t->answer_ede_msg) ? "" : ": ", | |
1272 | strempty(t->answer_ede_msg)); | |
ac684446 RP |
1273 | dns_transaction_retry(t, false); |
1274 | return; | |
1275 | } | |
1276 | ||
1277 | /* OK, the query failed, but we still shouldn't degrade the feature set for | |
1278 | * this server. */ | |
1279 | log_debug("Server returned error: %s (%s%s%s)", | |
c08fd7be YW |
1280 | FORMAT_DNS_RCODE(DNS_PACKET_RCODE(p)), |
1281 | FORMAT_DNS_EDE_RCODE(t->answer_ede_rcode), | |
1282 | isempty(t->answer_ede_msg) ? "" : ": ", | |
1283 | strempty(t->answer_ede_msg)); | |
ac684446 | 1284 | break; |
c08fd7be | 1285 | } |
4e0b8b17 | 1286 | |
8af5b883 | 1287 | /* Request failed, immediately try again with reduced features */ |
4e0b8b17 | 1288 | |
7d581a65 | 1289 | if (t->current_feature_level <= DNS_SERVER_FEATURE_LEVEL_UDP) { |
44db02d0 | 1290 | |
7d581a65 | 1291 | /* This was already at UDP feature level? If so, it doesn't make sense to downgrade |
44db02d0 LP |
1292 | * this transaction anymore, but let's see if it might make sense to send the request |
1293 | * to a different DNS server instead. If not let's process the response, and accept the | |
7d581a65 LP |
1294 | * rcode. Note that we don't retry on TCP, since that's a suitable way to mitigate |
1295 | * packet loss, but is not going to give us better rcodes should we actually have | |
1296 | * managed to get them already at UDP level. */ | |
1297 | ||
9147b591 | 1298 | if (dns_transaction_limited_retry(t)) |
44db02d0 | 1299 | return; |
44db02d0 LP |
1300 | |
1301 | /* Give up, accept the rcode */ | |
0d609349 | 1302 | log_debug("Server returned error: %s", FORMAT_DNS_RCODE(DNS_PACKET_RCODE(p))); |
d001e0a3 LP |
1303 | break; |
1304 | } | |
1305 | ||
8a33aa19 SS |
1306 | /* SERVFAIL can happen for many reasons and may be transient. |
1307 | * To avoid unnecessary downgrades retry once with the initial level. | |
1308 | * Check for clamp_feature_level_servfail having an invalid value as a sign that this is the | |
1309 | * first attempt to downgrade. If so, clamp to the current value so that the transaction | |
1310 | * is retried without actually downgrading. If the next try also fails we will downgrade by | |
1311 | * hitting the else branch below. */ | |
1312 | if (DNS_PACKET_RCODE(p) == DNS_RCODE_SERVFAIL && | |
1313 | t->clamp_feature_level_servfail < 0) { | |
1314 | t->clamp_feature_level_servfail = t->current_feature_level; | |
1315 | log_debug("Server returned error %s, retrying transaction.", | |
0d609349 | 1316 | FORMAT_DNS_RCODE(DNS_PACKET_RCODE(p))); |
8a33aa19 SS |
1317 | } else { |
1318 | /* Reduce this feature level by one and try again. */ | |
1319 | switch (t->current_feature_level) { | |
1320 | case DNS_SERVER_FEATURE_LEVEL_TLS_DO: | |
1321 | t->clamp_feature_level_servfail = DNS_SERVER_FEATURE_LEVEL_TLS_PLAIN; | |
1322 | break; | |
1323 | case DNS_SERVER_FEATURE_LEVEL_TLS_PLAIN + 1: | |
1324 | /* Skip plain TLS when TLS is not supported */ | |
1325 | t->clamp_feature_level_servfail = DNS_SERVER_FEATURE_LEVEL_TLS_PLAIN - 1; | |
1326 | break; | |
1327 | default: | |
1328 | t->clamp_feature_level_servfail = t->current_feature_level - 1; | |
1329 | } | |
1330 | ||
1331 | log_debug("Server returned error %s, retrying transaction with reduced feature level %s.", | |
0d609349 | 1332 | FORMAT_DNS_RCODE(DNS_PACKET_RCODE(p)), |
8a33aa19 | 1333 | dns_server_feature_level_to_string(t->clamp_feature_level_servfail)); |
5d67a7ae | 1334 | } |
d001e0a3 | 1335 | |
d001e0a3 | 1336 | dns_transaction_retry(t, false /* use the same server */); |
4e0b8b17 | 1337 | return; |
eb08640a LP |
1338 | } |
1339 | ||
1340 | if (DNS_PACKET_RCODE(p) == DNS_RCODE_REFUSED) { | |
1341 | /* This server refused our request? If so, try again, use a different server */ | |
c08fd7be | 1342 | if (t->answer_ede_rcode >= 0) |
ac684446 | 1343 | log_debug("Server returned REFUSED (%s), switching servers, and retrying.", |
c08fd7be | 1344 | FORMAT_DNS_EDE_RCODE(t->answer_ede_rcode)); |
ac684446 RP |
1345 | else |
1346 | log_debug("Server returned REFUSED, switching servers, and retrying."); | |
9147b591 LP |
1347 | |
1348 | if (dns_transaction_limited_retry(t)) | |
1349 | return; | |
1350 | ||
1351 | break; | |
eb08640a LP |
1352 | } |
1353 | ||
1354 | if (DNS_PACKET_TC(p)) | |
274b8748 | 1355 | dns_server_packet_truncated(t->server, t->current_feature_level); |
9df3ba6c TG |
1356 | |
1357 | break; | |
ac684446 | 1358 | } |
8af5b883 | 1359 | |
9df3ba6c TG |
1360 | case DNS_PROTOCOL_LLMNR: |
1361 | case DNS_PROTOCOL_MDNS: | |
5777c613 | 1362 | dns_scope_packet_received(t->scope, p->timestamp - t->start_usec); |
9df3ba6c | 1363 | break; |
8af5b883 | 1364 | |
9df3ba6c | 1365 | default: |
04499a70 | 1366 | assert_not_reached(); |
9df3ba6c TG |
1367 | } |
1368 | ||
ed9717fc | 1369 | if (t->server) { |
d001e0a3 LP |
1370 | /* Report that we successfully received a valid packet with a good rcode after we initially got a bad |
1371 | * rcode and subsequently downgraded the protocol */ | |
1372 | ||
1373 | if (IN_SET(DNS_PACKET_RCODE(p), DNS_RCODE_SUCCESS, DNS_RCODE_NXDOMAIN) && | |
1ed4e584 LP |
1374 | t->clamp_feature_level_servfail != _DNS_SERVER_FEATURE_LEVEL_INVALID) |
1375 | dns_server_packet_rcode_downgrade(t->server, t->clamp_feature_level_servfail); | |
d001e0a3 LP |
1376 | |
1377 | /* Report that the OPT RR was missing */ | |
ed9717fc LP |
1378 | if (!p->opt) |
1379 | dns_server_packet_bad_opt(t->server, t->current_feature_level); | |
1380 | ||
d96275d8 LP |
1381 | /* Report that the server didn't copy our query DO bit from request to response */ |
1382 | if (DNS_PACKET_DO(t->sent) && !DNS_PACKET_DO(t->received)) | |
1383 | dns_server_packet_do_off(t->server, t->current_feature_level); | |
1384 | ||
acbf761b LP |
1385 | /* Report that we successfully received a packet. We keep track of the largest packet |
1386 | * size/fragment size we got. Which is useful for announcing the EDNS(0) packet size we can | |
1387 | * receive to our server. */ | |
1388 | dns_server_packet_received(t->server, p->ipproto, t->current_feature_level, dns_packet_size_unfragmented(p)); | |
ed9717fc | 1389 | } |
de54e62b | 1390 | |
c02cf2f4 LP |
1391 | /* See if we know things we didn't know before that indicate we better restart the lookup immediately. */ |
1392 | r = dns_transaction_maybe_restart(t); | |
7cc6ed7b LP |
1393 | if (r < 0) |
1394 | goto fail; | |
c02cf2f4 LP |
1395 | if (r > 0) /* Transaction got restarted... */ |
1396 | return; | |
1397 | ||
8facd1ce LP |
1398 | /* When dealing with protocols other than mDNS only consider responses with equivalent query section |
1399 | * to the request. For mDNS this check doesn't make sense, because the section 6 of RFC6762 states | |
1400 | * that "Multicast DNS responses MUST NOT contain any questions in the Question Section". */ | |
1401 | if (t->scope->protocol != DNS_PROTOCOL_MDNS) { | |
42df9532 | 1402 | r = dns_packet_is_reply_for(p, dns_transaction_key(t)); |
8facd1ce LP |
1403 | if (r < 0) |
1404 | goto fail; | |
1405 | if (r == 0) { | |
1406 | dns_transaction_complete(t, DNS_TRANSACTION_INVALID_REPLY); | |
1407 | return; | |
547493c5 | 1408 | } |
8facd1ce | 1409 | } |
29815b6c | 1410 | |
775ae354 LP |
1411 | /* Install the answer as answer to the transaction. We ref the answer twice here: the main `answer` |
1412 | * field is later replaced by the DNSSEC validated subset. The 'answer_auxiliary' field carries the | |
1413 | * original complete record set, including RRSIG and friends. We use this when passing data to | |
1414 | * clients that ask for DNSSEC metadata. */ | |
1117a960 | 1415 | DNS_ANSWER_REPLACE(t->answer, dns_answer_ref(p->answer)); |
8facd1ce LP |
1416 | t->answer_rcode = DNS_PACKET_RCODE(p); |
1417 | t->answer_dnssec_result = _DNSSEC_RESULT_INVALID; | |
6f055e43 | 1418 | SET_FLAG(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED, false); |
43fc4baa | 1419 | SET_FLAG(t->answer_query_flags, SD_RESOLVED_CONFIDENTIAL, encrypted); |
79e24931 | 1420 | |
8facd1ce LP |
1421 | r = dns_transaction_fix_rcode(t); |
1422 | if (r < 0) | |
1423 | goto fail; | |
eac7cda2 | 1424 | |
8facd1ce LP |
1425 | /* Block GC while starting requests for additional DNSSEC RRs */ |
1426 | t->block_gc++; | |
1427 | r = dns_transaction_request_dnssec_keys(t); | |
1428 | t->block_gc--; | |
51e399bc | 1429 | |
8facd1ce LP |
1430 | /* Maybe the transaction is ready for GC'ing now? If so, free it and return. */ |
1431 | if (!dns_transaction_gc(t)) | |
1432 | return; | |
51e399bc | 1433 | |
8facd1ce LP |
1434 | /* Requesting additional keys might have resulted in this transaction to fail, since the auxiliary |
1435 | * request failed for some reason. If so, we are not in pending state anymore, and we should exit | |
1436 | * quickly. */ | |
1437 | if (t->state != DNS_TRANSACTION_PENDING) | |
1438 | return; | |
1439 | if (r < 0) | |
1440 | goto fail; | |
1441 | if (r > 0) { | |
1442 | /* There are DNSSEC transactions pending now. Update the state accordingly. */ | |
1443 | t->state = DNS_TRANSACTION_VALIDATING; | |
80710ade | 1444 | dns_transaction_close_connection(t, true); |
8facd1ce LP |
1445 | dns_transaction_stop_timeout(t); |
1446 | return; | |
547493c5 | 1447 | } |
ec2c5e43 | 1448 | |
547973de | 1449 | dns_transaction_process_dnssec(t); |
7cc6ed7b LP |
1450 | return; |
1451 | ||
1452 | fail: | |
fd8a3017 | 1453 | dns_transaction_complete_errno(t, r); |
ec2c5e43 LP |
1454 | } |
1455 | ||
c19ffd9f TG |
1456 | static int on_dns_packet(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
1457 | _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL; | |
99534007 | 1458 | DnsTransaction *t = ASSERT_PTR(userdata); |
c19ffd9f TG |
1459 | int r; |
1460 | ||
c19ffd9f TG |
1461 | assert(t->scope); |
1462 | ||
1463 | r = manager_recv(t->scope->manager, fd, DNS_PROTOCOL_DNS, &p); | |
0bdea17c DL |
1464 | if (r < 0) { |
1465 | if (ERRNO_IS_DISCONNECT(r)) { | |
1466 | usec_t usec; | |
7e1851e3 | 1467 | |
0bdea17c DL |
1468 | /* UDP connection failures get reported via ICMP and then are possibly delivered to us on the |
1469 | * next recvmsg(). Treat this like a lost packet. */ | |
7e1851e3 | 1470 | |
0bdea17c DL |
1471 | log_debug_errno(r, "Connection failure for DNS UDP packet: %m"); |
1472 | assert_se(sd_event_now(t->scope->manager->event, CLOCK_BOOTTIME, &usec) >= 0); | |
1473 | dns_server_packet_lost(t->server, IPPROTO_UDP, t->current_feature_level); | |
d68dbb37 | 1474 | |
0bdea17c | 1475 | dns_transaction_close_connection(t, /* use_graveyard = */ false); |
9147b591 | 1476 | |
0bdea17c DL |
1477 | if (dns_transaction_limited_retry(t)) /* Try a different server */ |
1478 | return 0; | |
1479 | } | |
fd8a3017 | 1480 | dns_transaction_complete_errno(t, r); |
7e1851e3 LP |
1481 | return 0; |
1482 | } | |
f731fd5b ZJS |
1483 | if (r == 0) |
1484 | /* Spurious wakeup without any data */ | |
1485 | return 0; | |
7e1851e3 LP |
1486 | |
1487 | r = dns_packet_validate_reply(p); | |
1488 | if (r < 0) { | |
1489 | log_debug_errno(r, "Received invalid DNS packet as response, ignoring: %m"); | |
1490 | return 0; | |
1491 | } | |
1492 | if (r == 0) { | |
e09f605e | 1493 | log_debug("Received inappropriate DNS packet as response, ignoring."); |
7e1851e3 LP |
1494 | return 0; |
1495 | } | |
1496 | ||
1497 | if (DNS_PACKET_ID(p) != t->id) { | |
e09f605e | 1498 | log_debug("Received packet with incorrect transaction ID, ignoring."); |
7e1851e3 LP |
1499 | return 0; |
1500 | } | |
c19ffd9f | 1501 | |
43fc4baa | 1502 | dns_transaction_process_reply(t, p, false); |
c19ffd9f TG |
1503 | return 0; |
1504 | } | |
1505 | ||
49cce12d | 1506 | static int dns_transaction_emit_udp(DnsTransaction *t) { |
c19ffd9f TG |
1507 | int r; |
1508 | ||
1509 | assert(t); | |
c19ffd9f | 1510 | |
519ef046 | 1511 | if (t->scope->protocol == DNS_PROTOCOL_DNS) { |
c19ffd9f | 1512 | |
519ef046 | 1513 | r = dns_transaction_pick_server(t); |
471d40d9 TG |
1514 | if (r < 0) |
1515 | return r; | |
c19ffd9f | 1516 | |
49ef064c LP |
1517 | if (manager_server_is_stub(t->scope->manager, t->server)) |
1518 | return -ELOOP; | |
1519 | ||
5d67a7ae | 1520 | if (t->current_feature_level < DNS_SERVER_FEATURE_LEVEL_UDP || DNS_SERVER_FEATURE_LEVEL_IS_TLS(t->current_feature_level)) |
7d581a65 | 1521 | return -EAGAIN; /* Sorry, can't do UDP, try TCP! */ |
519ef046 | 1522 | |
775ae354 | 1523 | if (!t->bypass && !dns_server_dnssec_supported(t->server) && dns_type_is_dnssec(dns_transaction_key(t)->type)) |
91adc4db LP |
1524 | return -EOPNOTSUPP; |
1525 | ||
519ef046 LP |
1526 | if (r > 0 || t->dns_udp_fd < 0) { /* Server changed, or no connection yet. */ |
1527 | int fd; | |
1528 | ||
80710ade LP |
1529 | dns_transaction_close_connection(t, true); |
1530 | ||
1531 | /* Before we allocate a new UDP socket, let's process the graveyard a bit to free some fds */ | |
1532 | manager_socket_graveyard_process(t->scope->manager); | |
c19ffd9f | 1533 | |
da9de738 | 1534 | fd = dns_scope_socket_udp(t->scope, t->server); |
519ef046 LP |
1535 | if (fd < 0) |
1536 | return fd; | |
1537 | ||
1538 | r = sd_event_add_io(t->scope->manager->event, &t->dns_udp_event_source, fd, EPOLLIN, on_dns_packet, t); | |
1539 | if (r < 0) { | |
1540 | safe_close(fd); | |
1541 | return r; | |
1542 | } | |
1543 | ||
aa4a9deb | 1544 | (void) sd_event_source_set_description(t->dns_udp_event_source, "dns-transaction-udp"); |
519ef046 LP |
1545 | t->dns_udp_fd = fd; |
1546 | } | |
1547 | ||
775ae354 LP |
1548 | if (!t->bypass) { |
1549 | r = dns_server_adjust_opt(t->server, t->sent, t->current_feature_level); | |
1550 | if (r < 0) | |
1551 | return r; | |
1552 | } | |
519ef046 | 1553 | } else |
80710ade | 1554 | dns_transaction_close_connection(t, true); |
519ef046 | 1555 | |
d79677ab | 1556 | r = dns_scope_emit_udp(t->scope, t->dns_udp_fd, t->server ? t->server->family : AF_UNSPEC, t->sent); |
471d40d9 TG |
1557 | if (r < 0) |
1558 | return r; | |
c19ffd9f | 1559 | |
519ef046 | 1560 | dns_transaction_reset_answer(t); |
be808ea0 | 1561 | |
471d40d9 | 1562 | return 0; |
c19ffd9f TG |
1563 | } |
1564 | ||
ec2c5e43 | 1565 | static int on_transaction_timeout(sd_event_source *s, usec_t usec, void *userdata) { |
99534007 | 1566 | DnsTransaction *t = ASSERT_PTR(userdata); |
ec2c5e43 LP |
1567 | |
1568 | assert(s); | |
ec2c5e43 | 1569 | |
bc837621 KV |
1570 | t->seen_timeout = true; |
1571 | ||
213cb4f7 LP |
1572 | if (t->initial_jitter_scheduled && !t->initial_jitter_elapsed) { |
1573 | log_debug("Initial jitter phase for transaction %" PRIu16 " elapsed.", t->id); | |
1574 | t->initial_jitter_elapsed = true; | |
1575 | } else { | |
ef7ce6df DM |
1576 | /* Timeout reached? Increase the timeout for the server used */ |
1577 | switch (t->scope->protocol) { | |
49cce12d | 1578 | |
ef7ce6df DM |
1579 | case DNS_PROTOCOL_DNS: |
1580 | assert(t->server); | |
3da3cdd5 | 1581 | dns_server_packet_lost(t->server, t->stream ? IPPROTO_TCP : IPPROTO_UDP, t->current_feature_level); |
ef7ce6df | 1582 | break; |
49cce12d | 1583 | |
ef7ce6df DM |
1584 | case DNS_PROTOCOL_LLMNR: |
1585 | case DNS_PROTOCOL_MDNS: | |
1586 | dns_scope_packet_lost(t->scope, usec - t->start_usec); | |
ef7ce6df | 1587 | break; |
49cce12d | 1588 | |
ef7ce6df | 1589 | default: |
04499a70 | 1590 | assert_not_reached(); |
ef7ce6df DM |
1591 | } |
1592 | ||
213cb4f7 | 1593 | log_debug("Timeout reached on transaction %" PRIu16 ".", t->id); |
be808ea0 TG |
1594 | } |
1595 | ||
213cb4f7 LP |
1596 | dns_transaction_retry(t, /* next_server= */ true); /* try a different server, but given this means |
1597 | * packet loss, let's do so even if we already | |
1598 | * tried a bunch */ | |
ec2c5e43 LP |
1599 | return 0; |
1600 | } | |
1601 | ||
87b91644 YW |
1602 | static int dns_transaction_setup_timeout( |
1603 | DnsTransaction *t, | |
1604 | usec_t timeout_usec /* relative */, | |
1605 | usec_t next_usec /* CLOCK_BOOTTIME */) { | |
1606 | ||
1607 | int r; | |
1608 | ||
1609 | assert(t); | |
1610 | ||
1611 | dns_transaction_stop_timeout(t); | |
1612 | ||
1613 | r = sd_event_add_time_relative( | |
1614 | t->scope->manager->event, | |
1615 | &t->timeout_event_source, | |
1616 | CLOCK_BOOTTIME, | |
1617 | timeout_usec, 0, | |
1618 | on_transaction_timeout, t); | |
1619 | if (r < 0) | |
1620 | return r; | |
1621 | ||
1622 | (void) sd_event_source_set_description(t->timeout_event_source, "dns-transaction-timeout"); | |
1623 | ||
1624 | t->next_attempt_after = next_usec; | |
1625 | t->state = DNS_TRANSACTION_PENDING; | |
1626 | return 0; | |
1627 | } | |
1628 | ||
9df3ba6c TG |
1629 | static usec_t transaction_get_resend_timeout(DnsTransaction *t) { |
1630 | assert(t); | |
1631 | assert(t->scope); | |
1632 | ||
1633 | switch (t->scope->protocol) { | |
49cce12d | 1634 | |
9df3ba6c | 1635 | case DNS_PROTOCOL_DNS: |
dc349f5f LP |
1636 | |
1637 | /* When we do TCP, grant a much longer timeout, as in this case there's no need for us to quickly | |
1638 | * resend, as the kernel does that anyway for us, and we really don't want to interrupt it in that | |
1639 | * needlessly. */ | |
1640 | if (t->stream) | |
1641 | return TRANSACTION_TCP_TIMEOUT_USEC; | |
1642 | ||
dbc4661a | 1643 | return DNS_TIMEOUT_USEC; |
49cce12d | 1644 | |
9df3ba6c | 1645 | case DNS_PROTOCOL_MDNS: |
53fda2bb DR |
1646 | if (t->probing) |
1647 | return MDNS_PROBING_INTERVAL_USEC; | |
4b2ceb8a YW |
1648 | |
1649 | /* See RFC 6762 Section 5.1 suggests that timeout should be a few seconds. */ | |
1650 | assert(t->n_attempts > 0); | |
1651 | return (1 << (t->n_attempts - 1)) * USEC_PER_SEC; | |
49cce12d | 1652 | |
11a27c2e | 1653 | case DNS_PROTOCOL_LLMNR: |
9df3ba6c | 1654 | return t->scope->resend_timeout; |
49cce12d | 1655 | |
9df3ba6c | 1656 | default: |
04499a70 | 1657 | assert_not_reached(); |
9df3ba6c TG |
1658 | } |
1659 | } | |
1660 | ||
3f0a7b3a LP |
1661 | static void dns_transaction_randomize_answer(DnsTransaction *t) { |
1662 | int r; | |
1663 | ||
1664 | assert(t); | |
1665 | ||
1666 | /* Randomizes the order of the answer array. This is done for all cached responses, so that we return | |
1667 | * a different order each time. We do this only for DNS traffic, in order to do some minimal, crappy | |
1668 | * load balancing. We don't do this for LLMNR or mDNS, since the order (preferring link-local | |
1669 | * addresses, and such like) might have meaning there, and load balancing is pointless. */ | |
1670 | ||
1671 | if (t->scope->protocol != DNS_PROTOCOL_DNS) | |
1672 | return; | |
1673 | ||
1674 | /* No point in randomizing, if there's just one RR */ | |
1675 | if (dns_answer_size(t->answer) <= 1) | |
1676 | return; | |
1677 | ||
1678 | r = dns_answer_reserve_or_clone(&t->answer, 0); | |
1679 | if (r < 0) /* If this fails, just don't randomize, this is non-essential stuff after all */ | |
1680 | return (void) log_debug_errno(r, "Failed to clone answer record, not randomizing RR order of answer: %m"); | |
1681 | ||
1682 | dns_answer_randomize(t->answer); | |
1683 | } | |
1684 | ||
c842ff24 | 1685 | static int dns_transaction_prepare(DnsTransaction *t, usec_t ts) { |
ec2c5e43 LP |
1686 | int r; |
1687 | ||
1688 | assert(t); | |
1689 | ||
4ea8b443 ZJS |
1690 | /* Returns 0 if dns_transaction_complete() has been called. In that case the transaction and query |
1691 | * candidate objects may have been invalidated and must not be accessed. Returns 1 if the transaction | |
1692 | * has been prepared. */ | |
1693 | ||
f535705a | 1694 | dns_transaction_stop_timeout(t); |
ec2c5e43 | 1695 | |
bc837621 KV |
1696 | if (t->n_attempts == 1 && t->seen_timeout) |
1697 | t->scope->manager->n_timeouts_total++; | |
1698 | ||
86b112a3 | 1699 | if (!dns_scope_network_good(t->scope)) { |
edbcc1fd LP |
1700 | dns_transaction_complete(t, DNS_TRANSACTION_NETWORK_DOWN); |
1701 | return 0; | |
1702 | } | |
1703 | ||
c3dbb132 | 1704 | if (t->n_attempts >= TRANSACTION_ATTEMPTS_MAX(t->scope->protocol)) { |
e53b8cc5 ZJS |
1705 | DnsTransactionState result; |
1706 | ||
1707 | if (t->scope->protocol == DNS_PROTOCOL_LLMNR) | |
1708 | /* If we didn't find anything on LLMNR, it's not an error, but a failure to resolve | |
1709 | * the name. */ | |
1710 | result = DNS_TRANSACTION_NOT_FOUND; | |
1711 | else | |
1712 | result = DNS_TRANSACTION_ATTEMPTS_MAX_REACHED; | |
1713 | ||
1714 | dns_transaction_complete(t, result); | |
ec2c5e43 LP |
1715 | return 0; |
1716 | } | |
1717 | ||
cbe4216d | 1718 | if (t->scope->protocol == DNS_PROTOCOL_LLMNR && t->tried_stream) { |
ec2c5e43 LP |
1719 | /* If we already tried via a stream, then we don't |
1720 | * retry on LLMNR. See RFC 4795, Section 2.7. */ | |
1721 | dns_transaction_complete(t, DNS_TRANSACTION_ATTEMPTS_MAX_REACHED); | |
1722 | return 0; | |
1723 | } | |
1724 | ||
1725 | t->n_attempts++; | |
9df3ba6c | 1726 | t->start_usec = ts; |
c61d2b44 LP |
1727 | |
1728 | dns_transaction_reset_answer(t); | |
c5b4f861 | 1729 | dns_transaction_flush_dnssec_transactions(t); |
ec2c5e43 | 1730 | |
0d2cd476 | 1731 | /* Check the trust anchor. Do so only on classic DNS, since DNSSEC does not apply otherwise. */ |
775ae354 LP |
1732 | if (t->scope->protocol == DNS_PROTOCOL_DNS && |
1733 | !FLAGS_SET(t->query_flags, SD_RESOLVED_NO_TRUST_ANCHOR)) { | |
42df9532 | 1734 | r = dns_trust_anchor_lookup_positive(&t->scope->manager->trust_anchor, dns_transaction_key(t), &t->answer); |
0d2cd476 LP |
1735 | if (r < 0) |
1736 | return r; | |
1737 | if (r > 0) { | |
1738 | t->answer_rcode = DNS_RCODE_SUCCESS; | |
1739 | t->answer_source = DNS_TRANSACTION_TRUST_ANCHOR; | |
43fc4baa | 1740 | SET_FLAG(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED|SD_RESOLVED_CONFIDENTIAL, true); |
0d2cd476 LP |
1741 | dns_transaction_complete(t, DNS_TRANSACTION_SUCCESS); |
1742 | return 0; | |
1743 | } | |
b2b796b8 | 1744 | |
42df9532 LP |
1745 | if (dns_name_is_root(dns_resource_key_name(dns_transaction_key(t))) && |
1746 | dns_transaction_key(t)->type == DNS_TYPE_DS) { | |
b2b796b8 | 1747 | |
775ae354 LP |
1748 | /* Hmm, this is a request for the root DS? A DS RR doesn't exist in the root zone, |
1749 | * and if our trust anchor didn't know it either, this means we cannot do any DNSSEC | |
1750 | * logic anymore. */ | |
b2b796b8 | 1751 | |
1ed8c0fb | 1752 | if (t->scope->dnssec_mode == DNSSEC_ALLOW_DOWNGRADE) { |
775ae354 LP |
1753 | /* We are in downgrade mode. In this case, synthesize an unsigned empty |
1754 | * response, so that the any lookup depending on this one can continue | |
1755 | * assuming there was no DS, and hence the root zone was unsigned. */ | |
b2b796b8 LP |
1756 | |
1757 | t->answer_rcode = DNS_RCODE_SUCCESS; | |
1758 | t->answer_source = DNS_TRANSACTION_TRUST_ANCHOR; | |
6f055e43 | 1759 | SET_FLAG(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED, false); |
43fc4baa | 1760 | SET_FLAG(t->answer_query_flags, SD_RESOLVED_CONFIDENTIAL, true); |
b2b796b8 LP |
1761 | dns_transaction_complete(t, DNS_TRANSACTION_SUCCESS); |
1762 | } else | |
775ae354 LP |
1763 | /* If we are not in downgrade mode, then fail the lookup, because we cannot |
1764 | * reasonably answer it. There might be DS RRs, but we don't know them, and | |
1765 | * the DNS server won't tell them to us (and even if it would, we couldn't | |
1766 | * validate and trust them. */ | |
b2b796b8 LP |
1767 | dns_transaction_complete(t, DNS_TRANSACTION_NO_TRUST_ANCHOR); |
1768 | ||
1769 | return 0; | |
1770 | } | |
0d2cd476 LP |
1771 | } |
1772 | ||
775ae354 LP |
1773 | /* Check the zone. */ |
1774 | if (!FLAGS_SET(t->query_flags, SD_RESOLVED_NO_ZONE)) { | |
42df9532 | 1775 | r = dns_zone_lookup(&t->scope->zone, dns_transaction_key(t), dns_scope_ifindex(t->scope), &t->answer, NULL, NULL); |
d746bb3e LP |
1776 | if (r < 0) |
1777 | return r; | |
1778 | if (r > 0) { | |
ae6a4bbf | 1779 | t->answer_rcode = DNS_RCODE_SUCCESS; |
c3bc53e6 | 1780 | t->answer_source = DNS_TRANSACTION_ZONE; |
43fc4baa | 1781 | SET_FLAG(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED|SD_RESOLVED_CONFIDENTIAL, true); |
d746bb3e LP |
1782 | dns_transaction_complete(t, DNS_TRANSACTION_SUCCESS); |
1783 | return 0; | |
1784 | } | |
1785 | } | |
1786 | ||
775ae354 LP |
1787 | /* Check the cache. */ |
1788 | if (!FLAGS_SET(t->query_flags, SD_RESOLVED_NO_CACHE)) { | |
2c27fbca | 1789 | |
775ae354 LP |
1790 | /* Before trying the cache, let's make sure we figured out a server to use. Should this cause |
1791 | * a change of server this might flush the cache. */ | |
5cdb8930 | 1792 | (void) dns_scope_get_dns_server(t->scope); |
2c27fbca | 1793 | |
4d926a69 LP |
1794 | /* Let's then prune all outdated entries */ |
1795 | dns_cache_prune(&t->scope->cache); | |
1796 | ||
5ed91481 KV |
1797 | /* For the initial attempt or when no stale data is requested, disable serve stale |
1798 | * and answer the question from the cache (honors ttl property). | |
1799 | * On the second attempt, if StaleRetentionSec is greater than zero, | |
1800 | * try to answer the question using stale date (honors until property) */ | |
1801 | uint64_t query_flags = t->query_flags; | |
1802 | if (t->n_attempts == 1 || t->scope->manager->stale_retention_usec == 0) | |
1803 | query_flags |= SD_RESOLVED_NO_STALE; | |
1804 | ||
775ae354 LP |
1805 | r = dns_cache_lookup( |
1806 | &t->scope->cache, | |
1807 | dns_transaction_key(t), | |
5ed91481 | 1808 | query_flags, |
775ae354 LP |
1809 | &t->answer_rcode, |
1810 | &t->answer, | |
1811 | &t->received, | |
6f055e43 | 1812 | &t->answer_query_flags, |
775ae354 | 1813 | &t->answer_dnssec_result); |
4d926a69 LP |
1814 | if (r < 0) |
1815 | return r; | |
1816 | if (r > 0) { | |
3f0a7b3a LP |
1817 | dns_transaction_randomize_answer(t); |
1818 | ||
775ae354 LP |
1819 | if (t->bypass && t->scope->protocol == DNS_PROTOCOL_DNS && !t->received) |
1820 | /* When bypass mode is on, do not use cached data unless it came with a full | |
1821 | * packet. */ | |
1822 | dns_transaction_reset_answer(t); | |
1823 | else { | |
5ed91481 | 1824 | if (t->n_attempts > 1 && !FLAGS_SET(query_flags, SD_RESOLVED_NO_STALE)) { |
bc837621 KV |
1825 | |
1826 | if (t->answer_rcode == DNS_RCODE_SUCCESS) { | |
1827 | if (t->seen_timeout) | |
1828 | t->scope->manager->n_timeouts_served_stale_total++; | |
1829 | else | |
1830 | t->scope->manager->n_failure_responses_served_stale_total++; | |
1831 | } | |
1832 | ||
5ed91481 KV |
1833 | char key_str[DNS_RESOURCE_KEY_STRING_MAX]; |
1834 | log_debug("Serve Stale response rcode=%s for %s", | |
1835 | FORMAT_DNS_RCODE(t->answer_rcode), | |
1836 | dns_resource_key_to_string(dns_transaction_key(t), key_str, sizeof key_str)); | |
1837 | } | |
1838 | ||
775ae354 LP |
1839 | t->answer_source = DNS_TRANSACTION_CACHE; |
1840 | if (t->answer_rcode == DNS_RCODE_SUCCESS) | |
1841 | dns_transaction_complete(t, DNS_TRANSACTION_SUCCESS); | |
37baa00f YW |
1842 | else { |
1843 | if (t->received) | |
1844 | (void) dns_packet_ede_rcode(t->received, &t->answer_ede_rcode, &t->answer_ede_msg); | |
1845 | ||
775ae354 | 1846 | dns_transaction_complete(t, DNS_TRANSACTION_RCODE_FAILURE); |
37baa00f | 1847 | } |
775ae354 LP |
1848 | return 0; |
1849 | } | |
4d926a69 | 1850 | } |
ec2c5e43 LP |
1851 | } |
1852 | ||
775ae354 LP |
1853 | if (FLAGS_SET(t->query_flags, SD_RESOLVED_NO_NETWORK)) { |
1854 | dns_transaction_complete(t, DNS_TRANSACTION_NO_SOURCE); | |
1855 | return 0; | |
1856 | } | |
1857 | ||
1effe965 DM |
1858 | return 1; |
1859 | } | |
1860 | ||
325513bc YW |
1861 | static int dns_packet_append_zone(DnsPacket *p, DnsTransaction *t, DnsResourceKey *k, unsigned *nscount) { |
1862 | _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; | |
1863 | bool tentative; | |
1864 | int r; | |
1865 | ||
1866 | assert(p); | |
1867 | assert(t); | |
1868 | assert(k); | |
1869 | ||
1870 | if (k->type != DNS_TYPE_ANY) | |
1871 | return 0; | |
1872 | ||
1873 | r = dns_zone_lookup(&t->scope->zone, k, t->scope->link->ifindex, &answer, NULL, &tentative); | |
1874 | if (r < 0) | |
1875 | return r; | |
1876 | ||
1877 | return dns_packet_append_answer(p, answer, nscount); | |
1878 | } | |
1879 | ||
7645e5a8 | 1880 | static int mdns_make_dummy_packet(DnsTransaction *t, DnsPacket **ret_packet, Set **ret_keys) { |
0afa57e2 | 1881 | _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL; |
0d5ee47d | 1882 | _cleanup_set_free_ Set *keys = NULL; |
325513bc | 1883 | bool add_known_answers = false; |
7645e5a8 | 1884 | unsigned qdcount; |
0afa57e2 DM |
1885 | usec_t ts; |
1886 | int r; | |
1887 | ||
1888 | assert(t); | |
7645e5a8 | 1889 | assert(t->scope); |
0afa57e2 | 1890 | assert(t->scope->protocol == DNS_PROTOCOL_MDNS); |
7645e5a8 YW |
1891 | assert(ret_packet); |
1892 | assert(ret_keys); | |
0afa57e2 | 1893 | |
0afa57e2 DM |
1894 | r = dns_packet_new_query(&p, t->scope->protocol, 0, false); |
1895 | if (r < 0) | |
1896 | return r; | |
1897 | ||
42df9532 | 1898 | r = dns_packet_append_key(p, dns_transaction_key(t), 0, NULL); |
0afa57e2 DM |
1899 | if (r < 0) |
1900 | return r; | |
1901 | ||
1902 | qdcount = 1; | |
1903 | ||
42df9532 | 1904 | if (dns_key_is_shared(dns_transaction_key(t))) |
7778dfff DM |
1905 | add_known_answers = true; |
1906 | ||
325513bc YW |
1907 | r = dns_packet_append_zone(p, t, dns_transaction_key(t), NULL); |
1908 | if (r < 0) | |
1909 | return r; | |
1910 | ||
1911 | /* Save appended keys */ | |
1912 | r = set_ensure_put(&keys, &dns_resource_key_hash_ops, dns_transaction_key(t)); | |
1913 | if (r < 0) | |
1914 | return r; | |
0d5ee47d | 1915 | |
ba4e0427 | 1916 | assert_se(sd_event_now(t->scope->manager->event, CLOCK_BOOTTIME, &ts) >= 0); |
0afa57e2 | 1917 | |
7645e5a8 | 1918 | LIST_FOREACH(transactions_by_scope, other, t->scope->transactions) { |
0afa57e2 | 1919 | |
7645e5a8 YW |
1920 | /* Skip ourselves */ |
1921 | if (other == t) | |
1922 | continue; | |
0afa57e2 | 1923 | |
7645e5a8 YW |
1924 | if (other->state != DNS_TRANSACTION_PENDING) |
1925 | continue; | |
0afa57e2 | 1926 | |
7645e5a8 YW |
1927 | if (other->next_attempt_after > ts) |
1928 | continue; | |
0afa57e2 | 1929 | |
7645e5a8 YW |
1930 | if (!set_contains(keys, dns_transaction_key(other))) { |
1931 | size_t saved_packet_size; | |
0afa57e2 | 1932 | |
7645e5a8 YW |
1933 | r = dns_packet_append_key(p, dns_transaction_key(other), 0, &saved_packet_size); |
1934 | /* If we can't stuff more questions into the packet, just give up. | |
1935 | * One of the 'other' transactions will fire later and take care of the rest. */ | |
1936 | if (r == -EMSGSIZE) | |
1937 | break; | |
325513bc YW |
1938 | if (r < 0) |
1939 | return r; | |
0afa57e2 | 1940 | |
7645e5a8 YW |
1941 | r = dns_packet_append_zone(p, t, dns_transaction_key(other), NULL); |
1942 | if (r == -EMSGSIZE) { | |
1943 | dns_packet_truncate(p, saved_packet_size); | |
325513bc YW |
1944 | break; |
1945 | } | |
7645e5a8 YW |
1946 | if (r < 0) |
1947 | return r; | |
aa4a9deb | 1948 | |
7645e5a8 | 1949 | r = set_ensure_put(&keys, &dns_resource_key_hash_ops, dns_transaction_key(other)); |
325513bc YW |
1950 | if (r < 0) |
1951 | return r; | |
7645e5a8 YW |
1952 | } |
1953 | ||
1954 | r = dns_transaction_prepare(other, ts); | |
1955 | if (r < 0) | |
1956 | return r; | |
1957 | if (r == 0) | |
1958 | /* In this case, not only this transaction, but multiple transactions may be | |
1959 | * freed. Hence, we need to restart the loop. */ | |
1960 | return -EAGAIN; | |
0afa57e2 | 1961 | |
7645e5a8 YW |
1962 | usec_t timeout = transaction_get_resend_timeout(other); |
1963 | r = dns_transaction_setup_timeout(other, timeout, usec_add(ts, timeout)); | |
1964 | if (r < 0) | |
1965 | return r; | |
7778dfff | 1966 | |
7645e5a8 YW |
1967 | if (dns_key_is_shared(dns_transaction_key(other))) |
1968 | add_known_answers = true; | |
0d5ee47d | 1969 | |
7645e5a8 YW |
1970 | qdcount++; |
1971 | if (qdcount >= UINT16_MAX) | |
1972 | break; | |
0afa57e2 DM |
1973 | } |
1974 | ||
7645e5a8 YW |
1975 | DNS_PACKET_HEADER(p)->qdcount = htobe16(qdcount); |
1976 | ||
1977 | /* Append known answers section if we're asking for any shared record */ | |
7778dfff | 1978 | if (add_known_answers) { |
325513bc | 1979 | r = dns_cache_export_shared_to_packet(&t->scope->cache, p, ts, 0); |
7778dfff DM |
1980 | if (r < 0) |
1981 | return r; | |
7645e5a8 | 1982 | } |
325513bc | 1983 | |
7645e5a8 YW |
1984 | *ret_packet = TAKE_PTR(p); |
1985 | *ret_keys = TAKE_PTR(keys); | |
1986 | return add_known_answers; | |
1987 | } | |
1988 | ||
1989 | static int dns_transaction_make_packet_mdns(DnsTransaction *t) { | |
1990 | _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL, *dummy = NULL; | |
1991 | _cleanup_set_free_ Set *keys = NULL; | |
1992 | bool add_known_answers; | |
1993 | DnsResourceKey *k; | |
1994 | unsigned c; | |
1995 | int r; | |
1996 | ||
1997 | assert(t); | |
1998 | assert(t->scope->protocol == DNS_PROTOCOL_MDNS); | |
1999 | ||
2000 | /* Discard any previously prepared packet, so we can start over and coalesce again */ | |
2001 | t->sent = dns_packet_unref(t->sent); | |
2002 | ||
2003 | /* First, create a dummy packet to calculate the number of known answers to be appended in the first packet. */ | |
2004 | for (;;) { | |
2005 | r = mdns_make_dummy_packet(t, &dummy, &keys); | |
2006 | if (r == -EAGAIN) | |
2007 | continue; | |
2008 | if (r < 0) | |
2009 | return r; | |
2010 | ||
2011 | add_known_answers = r; | |
2012 | break; | |
7778dfff DM |
2013 | } |
2014 | ||
5162b2a1 | 2015 | /* Then, create actual packet. */ |
325513bc YW |
2016 | r = dns_packet_new_query(&p, t->scope->protocol, 0, false); |
2017 | if (r < 0) | |
2018 | return r; | |
2019 | ||
2020 | /* Questions */ | |
7645e5a8 | 2021 | c = 0; |
325513bc YW |
2022 | SET_FOREACH(k, keys) { |
2023 | r = dns_packet_append_key(p, k, 0, NULL); | |
2024 | if (r < 0) | |
2025 | return r; | |
7645e5a8 | 2026 | c++; |
325513bc | 2027 | } |
7645e5a8 | 2028 | DNS_PACKET_HEADER(p)->qdcount = htobe16(c); |
0d5ee47d | 2029 | |
325513bc YW |
2030 | /* Known answers */ |
2031 | if (add_known_answers) { | |
7645e5a8 YW |
2032 | usec_t ts; |
2033 | ||
2034 | assert_se(sd_event_now(t->scope->manager->event, CLOCK_BOOTTIME, &ts) >= 0); | |
2035 | ||
2036 | r = dns_cache_export_shared_to_packet(&t->scope->cache, p, ts, be16toh(DNS_PACKET_HEADER(dummy)->ancount)); | |
0d5ee47d DR |
2037 | if (r < 0) |
2038 | return r; | |
325513bc | 2039 | } |
0d5ee47d | 2040 | |
325513bc | 2041 | /* Authorities */ |
7645e5a8 | 2042 | c = 0; |
325513bc | 2043 | SET_FOREACH(k, keys) { |
7645e5a8 | 2044 | r = dns_packet_append_zone(p, t, k, &c); |
0d5ee47d DR |
2045 | if (r < 0) |
2046 | return r; | |
0d5ee47d | 2047 | } |
7645e5a8 | 2048 | DNS_PACKET_HEADER(p)->nscount = htobe16(c); |
0d5ee47d | 2049 | |
1cc6c93a | 2050 | t->sent = TAKE_PTR(p); |
0afa57e2 DM |
2051 | return 0; |
2052 | } | |
2053 | ||
2054 | static int dns_transaction_make_packet(DnsTransaction *t) { | |
2055 | _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL; | |
2056 | int r; | |
2057 | ||
2058 | assert(t); | |
2059 | ||
2060 | if (t->scope->protocol == DNS_PROTOCOL_MDNS) | |
2061 | return dns_transaction_make_packet_mdns(t); | |
2062 | ||
2063 | if (t->sent) | |
2064 | return 0; | |
2065 | ||
775ae354 LP |
2066 | if (t->bypass && t->bypass->protocol == t->scope->protocol) { |
2067 | /* If bypass logic is enabled and the protocol if the original packet and our scope match, | |
2068 | * take the original packet, copy it, and patch in our new ID */ | |
2069 | r = dns_packet_dup(&p, t->bypass); | |
2070 | if (r < 0) | |
2071 | return r; | |
2072 | } else { | |
2073 | r = dns_packet_new_query( | |
2074 | &p, t->scope->protocol, | |
2075 | /* min_alloc_dsize = */ 0, | |
2076 | /* dnssec_cd = */ !FLAGS_SET(t->query_flags, SD_RESOLVED_NO_VALIDATE) && | |
2077 | t->scope->dnssec_mode != DNSSEC_NO); | |
2078 | if (r < 0) | |
2079 | return r; | |
0afa57e2 | 2080 | |
775ae354 LP |
2081 | r = dns_packet_append_key(p, dns_transaction_key(t), 0, NULL); |
2082 | if (r < 0) | |
2083 | return r; | |
2084 | ||
2085 | DNS_PACKET_HEADER(p)->qdcount = htobe16(1); | |
2086 | } | |
0afa57e2 | 2087 | |
0afa57e2 DM |
2088 | DNS_PACKET_HEADER(p)->id = t->id; |
2089 | ||
1cc6c93a | 2090 | t->sent = TAKE_PTR(p); |
0afa57e2 DM |
2091 | return 0; |
2092 | } | |
2093 | ||
1effe965 DM |
2094 | int dns_transaction_go(DnsTransaction *t) { |
2095 | usec_t ts; | |
2096 | int r; | |
202b76ae | 2097 | char key_str[DNS_RESOURCE_KEY_STRING_MAX]; |
1effe965 DM |
2098 | |
2099 | assert(t); | |
2100 | ||
4ea8b443 ZJS |
2101 | /* Returns > 0 if the transaction is now pending, returns 0 if could be processed immediately and has |
2102 | * finished now. In the latter case, the transaction and query candidate objects must not be accessed. | |
2103 | */ | |
5278bbfe | 2104 | |
ba4e0427 | 2105 | assert_se(sd_event_now(t->scope->manager->event, CLOCK_BOOTTIME, &ts) >= 0); |
547973de | 2106 | |
c842ff24 | 2107 | r = dns_transaction_prepare(t, ts); |
1effe965 DM |
2108 | if (r <= 0) |
2109 | return r; | |
2110 | ||
213cb4f7 LP |
2111 | log_debug("Firing %s transaction %" PRIu16 " for <%s> scope %s on %s/%s (validate=%s).", |
2112 | t->bypass ? "bypass" : "regular", | |
a5784c49 | 2113 | t->id, |
42df9532 | 2114 | dns_resource_key_to_string(dns_transaction_key(t), key_str, sizeof key_str), |
a5784c49 | 2115 | dns_protocol_to_string(t->scope->protocol), |
6ff79f76 | 2116 | t->scope->link ? t->scope->link->ifname : "*", |
775ae354 LP |
2117 | af_to_name_short(t->scope->family), |
2118 | yes_no(!FLAGS_SET(t->query_flags, SD_RESOLVED_NO_VALIDATE))); | |
1effe965 | 2119 | |
ef7ce6df | 2120 | if (!t->initial_jitter_scheduled && |
3742095b | 2121 | IN_SET(t->scope->protocol, DNS_PROTOCOL_LLMNR, DNS_PROTOCOL_MDNS)) { |
765647ba | 2122 | usec_t jitter; |
6e068472 | 2123 | |
765647ba YW |
2124 | /* RFC 4795 Section 2.7 suggests all LLMNR queries should be delayed by a random time from 0 to |
2125 | * JITTER_INTERVAL. | |
2126 | * RFC 6762 Section 8.1 suggests initial probe queries should be delayed by a random time from | |
2127 | * 0 to 250ms. */ | |
6e068472 | 2128 | |
ef7ce6df | 2129 | t->initial_jitter_scheduled = true; |
87b91644 | 2130 | t->n_attempts = 0; |
6e068472 | 2131 | |
ea12bcc7 | 2132 | switch (t->scope->protocol) { |
519ef046 | 2133 | |
ea12bcc7 | 2134 | case DNS_PROTOCOL_LLMNR: |
9ee18bf4 | 2135 | jitter = random_u64_range(LLMNR_JITTER_INTERVAL_USEC); |
ea12bcc7 | 2136 | break; |
519ef046 | 2137 | |
ea12bcc7 | 2138 | case DNS_PROTOCOL_MDNS: |
765647ba YW |
2139 | if (t->probing) |
2140 | jitter = random_u64_range(MDNS_PROBING_INTERVAL_USEC); | |
2141 | else | |
2142 | jitter = 0; | |
ea12bcc7 DM |
2143 | break; |
2144 | default: | |
04499a70 | 2145 | assert_not_reached(); |
ea12bcc7 | 2146 | } |
6e068472 | 2147 | |
87b91644 | 2148 | r = dns_transaction_setup_timeout(t, jitter, ts); |
6e068472 LP |
2149 | if (r < 0) |
2150 | return r; | |
2151 | ||
2f9c3b2a LP |
2152 | log_debug("Delaying %s transaction %" PRIu16 " for " USEC_FMT "us.", |
2153 | dns_protocol_to_string(t->scope->protocol), | |
2154 | t->id, | |
2155 | jitter); | |
4ea8b443 | 2156 | return 1; |
6e068472 LP |
2157 | } |
2158 | ||
ec2c5e43 LP |
2159 | /* Otherwise, we need to ask the network */ |
2160 | r = dns_transaction_make_packet(t); | |
ec2c5e43 LP |
2161 | if (r < 0) |
2162 | return r; | |
2163 | ||
2164 | if (t->scope->protocol == DNS_PROTOCOL_LLMNR && | |
42df9532 LP |
2165 | (dns_name_endswith(dns_resource_key_name(dns_transaction_key(t)), "in-addr.arpa") > 0 || |
2166 | dns_name_endswith(dns_resource_key_name(dns_transaction_key(t)), "ip6.arpa") > 0)) { | |
ec2c5e43 LP |
2167 | |
2168 | /* RFC 4795, Section 2.4. says reverse lookups shall | |
2169 | * always be made via TCP on LLMNR */ | |
98767d75 | 2170 | r = dns_transaction_emit_tcp(t); |
ec2c5e43 | 2171 | } else { |
be808ea0 TG |
2172 | /* Try via UDP, and if that fails due to large size or lack of |
2173 | * support try via TCP */ | |
49cce12d | 2174 | r = dns_transaction_emit_udp(t); |
29ab0552 LP |
2175 | if (r == -EMSGSIZE) |
2176 | log_debug("Sending query via TCP since it is too large."); | |
dc349f5f | 2177 | else if (r == -EAGAIN) |
6c0bacc1 | 2178 | log_debug("Sending query via TCP since UDP isn't supported or DNS-over-TLS is selected."); |
3dd6336a JB |
2179 | else if (r == -EPERM) |
2180 | log_debug("Sending query via TCP since UDP is blocked."); | |
2181 | if (IN_SET(r, -EMSGSIZE, -EAGAIN, -EPERM)) | |
98767d75 | 2182 | r = dns_transaction_emit_tcp(t); |
ec2c5e43 | 2183 | } |
49ef064c LP |
2184 | if (r == -ELOOP) { |
2185 | if (t->scope->protocol != DNS_PROTOCOL_DNS) | |
2186 | return r; | |
2187 | ||
2188 | /* One of our own stub listeners */ | |
2189 | log_debug_errno(r, "Detected that specified DNS server is our own extra listener, switching DNS servers."); | |
2190 | ||
5e8bc852 | 2191 | dns_scope_next_dns_server(t->scope, t->server); |
49ef064c LP |
2192 | |
2193 | if (dns_scope_get_dns_server(t->scope) == t->server) { | |
2194 | log_debug_errno(r, "Still pointing to extra listener after switching DNS servers, refusing operation."); | |
2195 | dns_transaction_complete(t, DNS_TRANSACTION_STUB_LOOP); | |
2196 | return 0; | |
2197 | } | |
be808ea0 | 2198 | |
49ef064c LP |
2199 | return dns_transaction_go(t); |
2200 | } | |
ec2c5e43 LP |
2201 | if (r == -ESRCH) { |
2202 | /* No servers to send this to? */ | |
2203 | dns_transaction_complete(t, DNS_TRANSACTION_NO_SERVERS); | |
2204 | return 0; | |
91adc4db LP |
2205 | } |
2206 | if (r == -EOPNOTSUPP) { | |
2207 | /* Tried to ask for DNSSEC RRs, on a server that doesn't do DNSSEC */ | |
2208 | dns_transaction_complete(t, DNS_TRANSACTION_RR_TYPE_UNSUPPORTED); | |
2209 | return 0; | |
2210 | } | |
13d84288 | 2211 | if (t->scope->protocol == DNS_PROTOCOL_LLMNR && ERRNO_IS_NEG_DISCONNECT(r)) { |
e94968ba | 2212 | /* On LLMNR, if we cannot connect to a host via TCP when doing reverse lookups. This means we cannot |
0791110f LP |
2213 | * answer this request with this protocol. */ |
2214 | dns_transaction_complete(t, DNS_TRANSACTION_NOT_FOUND); | |
2215 | return 0; | |
2216 | } | |
91adc4db | 2217 | if (r < 0) { |
7cc6ed7b LP |
2218 | if (t->scope->protocol != DNS_PROTOCOL_DNS) |
2219 | return r; | |
13b551ac | 2220 | |
ec2c5e43 | 2221 | /* Couldn't send? Try immediately again, with a new server */ |
5e8bc852 | 2222 | dns_scope_next_dns_server(t->scope, t->server); |
ec2c5e43 LP |
2223 | |
2224 | return dns_transaction_go(t); | |
2225 | } | |
2226 | ||
87b91644 YW |
2227 | usec_t timeout = transaction_get_resend_timeout(t); |
2228 | r = dns_transaction_setup_timeout(t, timeout, usec_add(ts, timeout)); | |
ec2c5e43 LP |
2229 | if (r < 0) |
2230 | return r; | |
2231 | ||
ec2c5e43 LP |
2232 | return 1; |
2233 | } | |
2234 | ||
f2992dc1 LP |
2235 | static int dns_transaction_find_cyclic(DnsTransaction *t, DnsTransaction *aux) { |
2236 | DnsTransaction *n; | |
f2992dc1 LP |
2237 | int r; |
2238 | ||
2239 | assert(t); | |
2240 | assert(aux); | |
2241 | ||
2242 | /* Try to find cyclic dependencies between transaction objects */ | |
2243 | ||
2244 | if (t == aux) | |
2245 | return 1; | |
2246 | ||
90e74a66 | 2247 | SET_FOREACH(n, aux->dnssec_transactions) { |
f2992dc1 LP |
2248 | r = dns_transaction_find_cyclic(t, n); |
2249 | if (r != 0) | |
2250 | return r; | |
2251 | } | |
2252 | ||
3eb6aa00 | 2253 | return 0; |
f2992dc1 LP |
2254 | } |
2255 | ||
547973de | 2256 | static int dns_transaction_add_dnssec_transaction(DnsTransaction *t, DnsResourceKey *key, DnsTransaction **ret) { |
29bd6012 | 2257 | _cleanup_(dns_transaction_gcp) DnsTransaction *aux = NULL; |
547973de LP |
2258 | int r; |
2259 | ||
2260 | assert(t); | |
2261 | assert(ret); | |
2262 | assert(key); | |
2263 | ||
775ae354 | 2264 | aux = dns_scope_find_transaction(t->scope, key, t->query_flags); |
547973de | 2265 | if (!aux) { |
775ae354 | 2266 | r = dns_transaction_new(&aux, t->scope, key, NULL, t->query_flags); |
547973de LP |
2267 | if (r < 0) |
2268 | return r; | |
2269 | } else { | |
2270 | if (set_contains(t->dnssec_transactions, aux)) { | |
2271 | *ret = aux; | |
2272 | return 0; | |
2273 | } | |
f2992dc1 LP |
2274 | |
2275 | r = dns_transaction_find_cyclic(t, aux); | |
2276 | if (r < 0) | |
2277 | return r; | |
2278 | if (r > 0) { | |
202b76ae ZJS |
2279 | char s[DNS_RESOURCE_KEY_STRING_MAX], saux[DNS_RESOURCE_KEY_STRING_MAX]; |
2280 | ||
baaa35ad ZJS |
2281 | return log_debug_errno(SYNTHETIC_ERRNO(ELOOP), |
2282 | "Potential cyclic dependency, refusing to add transaction %" PRIu16 " (%s) as dependency for %" PRIu16 " (%s).", | |
2283 | aux->id, | |
42df9532 | 2284 | dns_resource_key_to_string(dns_transaction_key(t), s, sizeof s), |
baaa35ad | 2285 | t->id, |
42df9532 | 2286 | dns_resource_key_to_string(dns_transaction_key(aux), saux, sizeof saux)); |
f2992dc1 | 2287 | } |
547973de LP |
2288 | } |
2289 | ||
35aa04e9 LP |
2290 | r = set_ensure_allocated(&aux->notify_transactions_done, NULL); |
2291 | if (r < 0) | |
29bd6012 | 2292 | return r; |
547973de | 2293 | |
de7fef4b | 2294 | r = set_ensure_put(&t->dnssec_transactions, NULL, aux); |
547973de | 2295 | if (r < 0) |
a75cb4e2 | 2296 | return r; |
547973de | 2297 | |
de7fef4b | 2298 | r = set_ensure_put(&aux->notify_transactions, NULL, t); |
547973de LP |
2299 | if (r < 0) { |
2300 | (void) set_remove(t->dnssec_transactions, aux); | |
29bd6012 | 2301 | return r; |
547973de LP |
2302 | } |
2303 | ||
29bd6012 | 2304 | *ret = TAKE_PTR(aux); |
547973de | 2305 | return 1; |
547973de LP |
2306 | } |
2307 | ||
ce5b9d5b | 2308 | static int dns_transaction_request_dnssec_rr_full(DnsTransaction *t, DnsResourceKey *key, DnsTransaction **ret) { |
547973de LP |
2309 | _cleanup_(dns_answer_unrefp) DnsAnswer *a = NULL; |
2310 | DnsTransaction *aux; | |
2311 | int r; | |
2312 | ||
2313 | assert(t); | |
2314 | assert(key); | |
2315 | ||
2316 | /* Try to get the data from the trust anchor */ | |
8e54f5d9 | 2317 | r = dns_trust_anchor_lookup_positive(&t->scope->manager->trust_anchor, key, &a); |
547973de LP |
2318 | if (r < 0) |
2319 | return r; | |
2320 | if (r > 0) { | |
2321 | r = dns_answer_extend(&t->validated_keys, a); | |
2322 | if (r < 0) | |
2323 | return r; | |
2324 | ||
40017103 RP |
2325 | if (ret) |
2326 | *ret = NULL; | |
547973de LP |
2327 | return 0; |
2328 | } | |
2329 | ||
2330 | /* This didn't work, ask for it via the network/cache then. */ | |
2331 | r = dns_transaction_add_dnssec_transaction(t, key, &aux); | |
40017103 RP |
2332 | if (r == -ELOOP) { /* This would result in a cyclic dependency */ |
2333 | if (ret) | |
2334 | *ret = NULL; | |
f2992dc1 | 2335 | return 0; |
40017103 | 2336 | } |
547973de LP |
2337 | if (r < 0) |
2338 | return r; | |
2339 | ||
2340 | if (aux->state == DNS_TRANSACTION_NULL) { | |
2341 | r = dns_transaction_go(aux); | |
2342 | if (r < 0) | |
2343 | return r; | |
2344 | } | |
73f77f38 RP |
2345 | if (ret) |
2346 | *ret = aux; | |
547973de | 2347 | |
f2992dc1 | 2348 | return 1; |
547973de LP |
2349 | } |
2350 | ||
ce5b9d5b RP |
2351 | static int dns_transaction_request_dnssec_rr(DnsTransaction *t, DnsResourceKey *key) { |
2352 | assert(t); | |
2353 | assert(key); | |
2354 | return dns_transaction_request_dnssec_rr_full(t, key, NULL); | |
2355 | } | |
2356 | ||
8a516214 LP |
2357 | static int dns_transaction_negative_trust_anchor_lookup(DnsTransaction *t, const char *name) { |
2358 | int r; | |
2359 | ||
2360 | assert(t); | |
2361 | ||
c629ff58 | 2362 | /* Check whether the specified name is in the NTA |
8a516214 LP |
2363 | * database, either in the global one, or the link-local |
2364 | * one. */ | |
2365 | ||
2366 | r = dns_trust_anchor_lookup_negative(&t->scope->manager->trust_anchor, name); | |
2367 | if (r != 0) | |
2368 | return r; | |
2369 | ||
2370 | if (!t->scope->link) | |
2371 | return 0; | |
2372 | ||
7e8a93b7 | 2373 | return link_negative_trust_anchor_lookup(t->scope->link, name); |
8a516214 LP |
2374 | } |
2375 | ||
26b23d11 | 2376 | static int dns_transaction_has_negative_answer(DnsTransaction *t) { |
105e1512 LP |
2377 | int r; |
2378 | ||
2379 | assert(t); | |
2380 | ||
2381 | /* Checks whether the answer is negative, and lacks NSEC/NSEC3 | |
2382 | * RRs to prove it */ | |
2383 | ||
2384 | r = dns_transaction_has_positive_answer(t, NULL); | |
2385 | if (r < 0) | |
2386 | return r; | |
2387 | if (r > 0) | |
2388 | return false; | |
2389 | ||
8e54f5d9 LP |
2390 | /* Is this key explicitly listed as a negative trust anchor? |
2391 | * If so, it's nothing we need to care about */ | |
42df9532 | 2392 | r = dns_transaction_negative_trust_anchor_lookup(t, dns_resource_key_name(dns_transaction_key(t))); |
8e54f5d9 LP |
2393 | if (r < 0) |
2394 | return r; | |
26b23d11 | 2395 | return !r; |
105e1512 LP |
2396 | } |
2397 | ||
2398 | static int dns_transaction_is_primary_response(DnsTransaction *t, DnsResourceRecord *rr) { | |
2399 | int r; | |
2400 | ||
2401 | assert(t); | |
2402 | assert(rr); | |
2403 | ||
2404 | /* Check if the specified RR is the "primary" response, | |
2405 | * i.e. either matches the question precisely or is a | |
4cb94977 | 2406 | * CNAME/DNAME for it. */ |
105e1512 | 2407 | |
42df9532 | 2408 | r = dns_resource_key_match_rr(dns_transaction_key(t), rr, NULL); |
105e1512 LP |
2409 | if (r != 0) |
2410 | return r; | |
2411 | ||
42df9532 | 2412 | return dns_resource_key_match_cname_or_dname(dns_transaction_key(t), rr->key, NULL); |
105e1512 LP |
2413 | } |
2414 | ||
92ec902a LP |
2415 | static bool dns_transaction_dnssec_supported(DnsTransaction *t) { |
2416 | assert(t); | |
2417 | ||
2418 | /* Checks whether our transaction's DNS server is assumed to be compatible with DNSSEC. Returns false as soon | |
2419 | * as we changed our mind about a server, and now believe it is incompatible with DNSSEC. */ | |
2420 | ||
2421 | if (t->scope->protocol != DNS_PROTOCOL_DNS) | |
2422 | return false; | |
2423 | ||
2424 | /* If we have picked no server, then we are working from the cache or some other source, and DNSSEC might well | |
2425 | * be supported, hence return true. */ | |
2426 | if (!t->server) | |
2427 | return true; | |
2428 | ||
d001e0a3 LP |
2429 | /* Note that we do not check the feature level actually used for the transaction but instead the feature level |
2430 | * the server is known to support currently, as the transaction feature level might be lower than what the | |
2431 | * server actually supports, since we might have downgraded this transaction's feature level because we got a | |
2432 | * SERVFAIL earlier and wanted to check whether downgrading fixes it. */ | |
92ec902a LP |
2433 | |
2434 | return dns_server_dnssec_supported(t->server); | |
2435 | } | |
2436 | ||
2437 | static bool dns_transaction_dnssec_supported_full(DnsTransaction *t) { | |
2438 | DnsTransaction *dt; | |
92ec902a LP |
2439 | |
2440 | assert(t); | |
2441 | ||
2442 | /* Checks whether our transaction our any of the auxiliary transactions couldn't do DNSSEC. */ | |
2443 | ||
2444 | if (!dns_transaction_dnssec_supported(t)) | |
2445 | return false; | |
2446 | ||
90e74a66 | 2447 | SET_FOREACH(dt, t->dnssec_transactions) |
92ec902a LP |
2448 | if (!dns_transaction_dnssec_supported(dt)) |
2449 | return false; | |
2450 | ||
2451 | return true; | |
2452 | } | |
2453 | ||
547973de LP |
2454 | int dns_transaction_request_dnssec_keys(DnsTransaction *t) { |
2455 | DnsResourceRecord *rr; | |
105e1512 | 2456 | |
47690634 RP |
2457 | /* Have we already requested a record that would be sufficient to validate an insecure delegation? */ |
2458 | bool chased_insecure = false; | |
547973de LP |
2459 | int r; |
2460 | ||
2461 | assert(t); | |
2462 | ||
105e1512 LP |
2463 | /* |
2464 | * Retrieve all auxiliary RRs for the answer we got, so that | |
2465 | * we can verify signatures or prove that RRs are rightfully | |
2466 | * unsigned. Specifically: | |
2467 | * | |
2468 | * - For RRSIG we get the matching DNSKEY | |
2469 | * - For DNSKEY we get the matching DS | |
2470 | * - For unsigned SOA/NS we get the matching DS | |
47690634 RP |
2471 | * - For unsigned CNAME/DNAME/DS we get the parent DS RR |
2472 | * - For other unsigned RRs we get the matching DS RR | |
4bbc06cc | 2473 | * - For SOA/NS queries with no matching response RR, and no NSEC/NSEC3, the DS RR |
47690634 RP |
2474 | * - For DS queries with no matching response RRs, and no NSEC/NSEC3, the parent's DS RR |
2475 | * - For other queries with no matching response RRs, and no NSEC/NSEC3, the DS RR | |
105e1512 LP |
2476 | */ |
2477 | ||
775ae354 | 2478 | if (FLAGS_SET(t->query_flags, SD_RESOLVED_NO_VALIDATE) || t->scope->dnssec_mode == DNSSEC_NO) |
547973de | 2479 | return 0; |
92ec902a LP |
2480 | if (t->answer_source != DNS_TRANSACTION_NETWORK) |
2481 | return 0; /* We only need to validate stuff from the network */ | |
2482 | if (!dns_transaction_dnssec_supported(t)) | |
5238e957 | 2483 | return 0; /* If we can't do DNSSEC anyway there's no point in getting the auxiliary RRs */ |
b652d4a2 | 2484 | |
547973de LP |
2485 | DNS_ANSWER_FOREACH(rr, t->answer) { |
2486 | ||
105e1512 LP |
2487 | if (dns_type_is_pseudo(rr->key->type)) |
2488 | continue; | |
2489 | ||
8e54f5d9 | 2490 | /* If this RR is in the negative trust anchor, we don't need to validate it. */ |
1c02e7ba | 2491 | r = dns_transaction_negative_trust_anchor_lookup(t, dns_resource_key_name(rr->key)); |
8e54f5d9 LP |
2492 | if (r < 0) |
2493 | return r; | |
2494 | if (r > 0) | |
2495 | continue; | |
2496 | ||
547973de LP |
2497 | switch (rr->key->type) { |
2498 | ||
2499 | case DNS_TYPE_RRSIG: { | |
2500 | /* For each RRSIG we request the matching DNSKEY */ | |
2501 | _cleanup_(dns_resource_key_unrefp) DnsResourceKey *dnskey = NULL; | |
40017103 | 2502 | DnsTransaction *aux; |
547973de LP |
2503 | |
2504 | /* If this RRSIG is about a DNSKEY RR and the | |
2505 | * signer is the same as the owner, then we | |
2506 | * already have the DNSKEY, and we don't have | |
2507 | * to look for more. */ | |
2508 | if (rr->rrsig.type_covered == DNS_TYPE_DNSKEY) { | |
1c02e7ba | 2509 | r = dns_name_equal(rr->rrsig.signer, dns_resource_key_name(rr->key)); |
547973de LP |
2510 | if (r < 0) |
2511 | return r; | |
2512 | if (r > 0) | |
2513 | continue; | |
2514 | } | |
2515 | ||
105e1512 LP |
2516 | /* If the signer is not a parent of our |
2517 | * original query, then this is about an | |
2518 | * auxiliary RRset, but not anything we asked | |
2519 | * for. In this case we aren't interested, | |
2520 | * because we don't want to request additional | |
2521 | * RRs for stuff we didn't really ask for, and | |
2522 | * also to avoid request loops, where | |
2523 | * additional RRs from one transaction result | |
5238e957 | 2524 | * in another transaction whose additional RRs |
105e1512 LP |
2525 | * point back to the original transaction, and |
2526 | * we deadlock. */ | |
42df9532 | 2527 | r = dns_name_endswith(dns_resource_key_name(dns_transaction_key(t)), rr->rrsig.signer); |
547973de LP |
2528 | if (r < 0) |
2529 | return r; | |
2530 | if (r == 0) | |
2531 | continue; | |
2532 | ||
2533 | dnskey = dns_resource_key_new(rr->key->class, DNS_TYPE_DNSKEY, rr->rrsig.signer); | |
2534 | if (!dnskey) | |
2535 | return -ENOMEM; | |
2536 | ||
1c02e7ba ZJS |
2537 | log_debug("Requesting DNSKEY to validate transaction %" PRIu16" (%s, RRSIG with key tag: %" PRIu16 ").", |
2538 | t->id, dns_resource_key_name(rr->key), rr->rrsig.key_tag); | |
ce5b9d5b | 2539 | r = dns_transaction_request_dnssec_rr_full(t, dnskey, &aux); |
547973de LP |
2540 | if (r < 0) |
2541 | return r; | |
ce5b9d5b | 2542 | |
2e097e4a | 2543 | /* If we are requesting a DNSKEY, we can anticipate that we will want the matching DS |
ce5b9d5b RP |
2544 | * in the near future. Let's request it in advance so we don't have to wait in the |
2545 | * common case. */ | |
2546 | if (aux) { | |
2547 | _cleanup_(dns_resource_key_unrefp) DnsResourceKey *ds = | |
2548 | dns_resource_key_new(rr->key->class, DNS_TYPE_DS, dns_resource_key_name(dnskey)); | |
40017103 RP |
2549 | if (!ds) |
2550 | return -ENOMEM; | |
ce5b9d5b RP |
2551 | r = dns_transaction_request_dnssec_rr(t, ds); |
2552 | if (r < 0) | |
2553 | return r; | |
2554 | } | |
547973de LP |
2555 | break; |
2556 | } | |
2557 | ||
2558 | case DNS_TYPE_DNSKEY: { | |
2559 | /* For each DNSKEY we request the matching DS */ | |
2560 | _cleanup_(dns_resource_key_unrefp) DnsResourceKey *ds = NULL; | |
2561 | ||
105e1512 LP |
2562 | /* If the DNSKEY we are looking at is not for |
2563 | * zone we are interested in, nor any of its | |
2564 | * parents, we aren't interested, and don't | |
2565 | * request it. After all, we don't want to end | |
2566 | * up in request loops, and want to keep | |
2567 | * additional traffic down. */ | |
2568 | ||
42df9532 | 2569 | r = dns_name_endswith(dns_resource_key_name(dns_transaction_key(t)), dns_resource_key_name(rr->key)); |
105e1512 LP |
2570 | if (r < 0) |
2571 | return r; | |
2572 | if (r == 0) | |
2573 | continue; | |
2574 | ||
1c02e7ba | 2575 | ds = dns_resource_key_new(rr->key->class, DNS_TYPE_DS, dns_resource_key_name(rr->key)); |
547973de LP |
2576 | if (!ds) |
2577 | return -ENOMEM; | |
2578 | ||
1c02e7ba ZJS |
2579 | log_debug("Requesting DS to validate transaction %" PRIu16" (%s, DNSKEY with key tag: %" PRIu16 ").", |
2580 | t->id, dns_resource_key_name(rr->key), dnssec_keytag(rr, false)); | |
105e1512 LP |
2581 | r = dns_transaction_request_dnssec_rr(t, ds); |
2582 | if (r < 0) | |
2583 | return r; | |
547973de | 2584 | |
105e1512 LP |
2585 | break; |
2586 | } | |
2587 | ||
105e1512 LP |
2588 | case DNS_TYPE_SOA: |
2589 | case DNS_TYPE_NS: { | |
2590 | _cleanup_(dns_resource_key_unrefp) DnsResourceKey *ds = NULL; | |
2591 | ||
2592 | /* For an unsigned SOA or NS, try to acquire | |
2593 | * the matching DS RR, as we are at a zone cut | |
2594 | * then, and whether a DS exists tells us | |
2595 | * whether the zone is signed. Do so only if | |
2596 | * this RR matches our original question, | |
2597 | * however. */ | |
2598 | ||
42df9532 | 2599 | r = dns_resource_key_match_rr(dns_transaction_key(t), rr, NULL); |
105e1512 LP |
2600 | if (r < 0) |
2601 | return r; | |
6993d264 LP |
2602 | if (r == 0) { |
2603 | /* Hmm, so this SOA RR doesn't match our original question. In this case, maybe this is | |
d51c4fca | 2604 | * a negative reply, and we need the SOA RR's TTL in order to cache a negative entry? |
6993d264 LP |
2605 | * If so, we need to validate it, too. */ |
2606 | ||
42df9532 | 2607 | r = dns_answer_match_key(t->answer, dns_transaction_key(t), NULL); |
6993d264 LP |
2608 | if (r < 0) |
2609 | return r; | |
2610 | if (r > 0) /* positive reply, we won't need the SOA and hence don't need to validate | |
2611 | * it. */ | |
2612 | continue; | |
d5acaa51 LP |
2613 | |
2614 | /* Only bother with this if the SOA/NS RR we are looking at is actually a parent of | |
2615 | * what we are looking for, otherwise there's no value in it for us. */ | |
42df9532 | 2616 | r = dns_name_endswith(dns_resource_key_name(dns_transaction_key(t)), dns_resource_key_name(rr->key)); |
d5acaa51 LP |
2617 | if (r < 0) |
2618 | return r; | |
2619 | if (r == 0) | |
2620 | continue; | |
6993d264 | 2621 | } |
105e1512 LP |
2622 | |
2623 | r = dnssec_has_rrsig(t->answer, rr->key); | |
2624 | if (r < 0) | |
2625 | return r; | |
2626 | if (r > 0) | |
2627 | continue; | |
2628 | ||
47690634 | 2629 | chased_insecure = true; |
1c02e7ba | 2630 | ds = dns_resource_key_new(rr->key->class, DNS_TYPE_DS, dns_resource_key_name(rr->key)); |
105e1512 LP |
2631 | if (!ds) |
2632 | return -ENOMEM; | |
2633 | ||
1c02e7ba ZJS |
2634 | log_debug("Requesting DS to validate transaction %" PRIu16 " (%s, unsigned SOA/NS RRset).", |
2635 | t->id, dns_resource_key_name(rr->key)); | |
547973de LP |
2636 | r = dns_transaction_request_dnssec_rr(t, ds); |
2637 | if (r < 0) | |
2638 | return r; | |
2639 | ||
2640 | break; | |
105e1512 LP |
2641 | } |
2642 | ||
b63fca62 | 2643 | case DNS_TYPE_DS: |
105e1512 LP |
2644 | case DNS_TYPE_CNAME: |
2645 | case DNS_TYPE_DNAME: { | |
47690634 | 2646 | _cleanup_(dns_resource_key_unrefp) DnsResourceKey *ds = NULL; |
105e1512 LP |
2647 | const char *name; |
2648 | ||
2649 | /* CNAMEs and DNAMEs cannot be located at a | |
47690634 | 2650 | * zone apex, hence ask for the parent DS for |
105e1512 LP |
2651 | * unsigned CNAME/DNAME RRs, maybe that's the |
2652 | * apex. But do all that only if this is | |
2653 | * actually a response to our original | |
b63fca62 LP |
2654 | * question. |
2655 | * | |
2656 | * Similar for DS RRs, which are signed when | |
2657 | * the parent SOA is signed. */ | |
105e1512 LP |
2658 | |
2659 | r = dns_transaction_is_primary_response(t, rr); | |
2660 | if (r < 0) | |
2661 | return r; | |
2662 | if (r == 0) | |
2663 | continue; | |
2664 | ||
2665 | r = dnssec_has_rrsig(t->answer, rr->key); | |
2666 | if (r < 0) | |
2667 | return r; | |
2668 | if (r > 0) | |
2669 | continue; | |
2670 | ||
43e6779a LP |
2671 | r = dns_answer_has_dname_for_cname(t->answer, rr); |
2672 | if (r < 0) | |
2673 | return r; | |
2674 | if (r > 0) | |
2675 | continue; | |
2676 | ||
1c02e7ba | 2677 | name = dns_resource_key_name(rr->key); |
105e1512 LP |
2678 | r = dns_name_parent(&name); |
2679 | if (r < 0) | |
2680 | return r; | |
2681 | if (r == 0) | |
2682 | continue; | |
2683 | ||
47690634 RP |
2684 | ds = dns_resource_key_new(rr->key->class, DNS_TYPE_DS, name); |
2685 | if (!ds) | |
105e1512 LP |
2686 | return -ENOMEM; |
2687 | ||
47690634 | 2688 | log_debug("Requesting parent DS to validate transaction %" PRIu16 " (%s, unsigned CNAME/DNAME/DS RRset).", |
1c02e7ba | 2689 | t->id, dns_resource_key_name(rr->key)); |
47690634 | 2690 | r = dns_transaction_request_dnssec_rr(t, ds); |
105e1512 LP |
2691 | if (r < 0) |
2692 | return r; | |
2693 | ||
2694 | break; | |
2695 | } | |
2696 | ||
2697 | default: { | |
47690634 | 2698 | _cleanup_(dns_resource_key_unrefp) DnsResourceKey *ds = NULL; |
105e1512 | 2699 | |
b63fca62 LP |
2700 | /* For other unsigned RRsets (including |
2701 | * NSEC/NSEC3!), look for proof the zone is | |
47690634 | 2702 | * unsigned, by requesting the DS RR of the |
b63fca62 LP |
2703 | * zone. However, do so only if they are |
2704 | * directly relevant to our original | |
105e1512 LP |
2705 | * question. */ |
2706 | ||
2707 | r = dns_transaction_is_primary_response(t, rr); | |
2708 | if (r < 0) | |
2709 | return r; | |
2710 | if (r == 0) | |
2711 | continue; | |
2712 | ||
2713 | r = dnssec_has_rrsig(t->answer, rr->key); | |
2714 | if (r < 0) | |
2715 | return r; | |
2716 | if (r > 0) | |
2717 | continue; | |
2718 | ||
47690634 RP |
2719 | ds = dns_resource_key_new(rr->key->class, DNS_TYPE_DS, dns_resource_key_name(rr->key)); |
2720 | if (!ds) | |
105e1512 LP |
2721 | return -ENOMEM; |
2722 | ||
47690634 | 2723 | log_debug("Requesting DS to validate transaction %" PRIu16 " (%s, unsigned non-SOA/NS RRset <%s>).", |
1c02e7ba | 2724 | t->id, dns_resource_key_name(rr->key), dns_resource_record_to_string(rr)); |
47690634 | 2725 | r = dns_transaction_request_dnssec_rr(t, ds); |
105e1512 LP |
2726 | if (r < 0) |
2727 | return r; | |
2728 | break; | |
547973de LP |
2729 | }} |
2730 | } | |
2731 | ||
105e1512 LP |
2732 | /* Above, we requested everything necessary to validate what |
2733 | * we got. Now, let's request what we need to validate what we | |
2734 | * didn't get... */ | |
2735 | ||
26b23d11 | 2736 | r = dns_transaction_has_negative_answer(t); |
105e1512 LP |
2737 | if (r < 0) |
2738 | return r; | |
2739 | if (r > 0) { | |
47690634 RP |
2740 | const char *name = dns_resource_key_name(dns_transaction_key(t)); |
2741 | bool was_signed = dns_answer_contains_nsec_or_nsec3(t->answer); | |
105e1512 | 2742 | |
47690634 RP |
2743 | /* If the response is empty, seek the DS for this name, just in case we're at a zone cut |
2744 | * already, unless we just requested the DS, in which case we have to ask the parent to make | |
2745 | * progress. | |
2746 | * | |
2747 | * If this was an SOA or NS request, we could also skip to the parent, but in real world | |
2748 | * setups there are too many broken DNS servers (Hello, incapdns.net!) where non-terminal | |
2749 | * zones return NXDOMAIN even though they have further children. */ | |
2750 | ||
2751 | if (chased_insecure || was_signed) | |
2503c377 | 2752 | /* In this case we already requested what we need above. */ |
47690634 RP |
2753 | name = NULL; |
2754 | else if (dns_transaction_key(t)->type == DNS_TYPE_DS) | |
2755 | /* If the DS response is empty, we'll walk up the dns labels requesting DS until we | |
2756 | * find a referral to the SOA or hit it anyway and get a positive DS response. */ | |
2757 | if (dns_name_parent(&name) <= 0) | |
105e1512 | 2758 | name = NULL; |
4bbc06cc | 2759 | |
105e1512 | 2760 | if (name) { |
47690634 | 2761 | _cleanup_(dns_resource_key_unrefp) DnsResourceKey *ds = NULL; |
105e1512 | 2762 | |
47690634 RP |
2763 | log_debug("Requesting DS (%s %s) to validate transaction %" PRIu16 " (%s empty response).", |
2764 | special_glyph(SPECIAL_GLYPH_ARROW_RIGHT), name, t->id, | |
2765 | dns_resource_key_name(dns_transaction_key(t))); | |
2766 | ||
2767 | ds = dns_resource_key_new(dns_transaction_key(t)->class, DNS_TYPE_DS, name); | |
2768 | if (!ds) | |
105e1512 LP |
2769 | return -ENOMEM; |
2770 | ||
47690634 | 2771 | r = dns_transaction_request_dnssec_rr(t, ds); |
105e1512 LP |
2772 | if (r < 0) |
2773 | return r; | |
2774 | } | |
2775 | } | |
2776 | ||
2777 | return dns_transaction_dnssec_is_live(t); | |
547973de LP |
2778 | } |
2779 | ||
2780 | void dns_transaction_notify(DnsTransaction *t, DnsTransaction *source) { | |
547973de | 2781 | assert(t); |
547973de LP |
2782 | assert(source); |
2783 | ||
942eb2e7 LP |
2784 | /* Invoked whenever any of our auxiliary DNSSEC transactions completed its work. If the state is still PENDING, |
2785 | we are still in the loop that adds further DNSSEC transactions, hence don't check if we are ready yet. If | |
2786 | the state is VALIDATING however, we should check if we are complete now. */ | |
105e1512 | 2787 | |
942eb2e7 LP |
2788 | if (t->state == DNS_TRANSACTION_VALIDATING) |
2789 | dns_transaction_process_dnssec(t); | |
547973de LP |
2790 | } |
2791 | ||
105e1512 | 2792 | static int dns_transaction_validate_dnskey_by_ds(DnsTransaction *t) { |
04617bf8 LP |
2793 | DnsAnswerItem *item; |
2794 | int r; | |
105e1512 LP |
2795 | |
2796 | assert(t); | |
2797 | ||
2798 | /* Add all DNSKEY RRs from the answer that are validated by DS | |
2799 | * RRs from the list of validated keys to the list of | |
2800 | * validated keys. */ | |
2801 | ||
04617bf8 | 2802 | DNS_ANSWER_FOREACH_ITEM(item, t->answer) { |
105e1512 | 2803 | |
04617bf8 | 2804 | r = dnssec_verify_dnskey_by_ds_search(item->rr, t->validated_keys); |
105e1512 LP |
2805 | if (r < 0) |
2806 | return r; | |
2807 | if (r == 0) | |
2808 | continue; | |
2809 | ||
2810 | /* If so, the DNSKEY is validated too. */ | |
04617bf8 | 2811 | r = dns_answer_add_extend(&t->validated_keys, item->rr, item->ifindex, item->flags|DNS_ANSWER_AUTHENTICATED, item->rrsig); |
105e1512 LP |
2812 | if (r < 0) |
2813 | return r; | |
2814 | } | |
2815 | ||
2816 | return 0; | |
2817 | } | |
2818 | ||
2819 | static int dns_transaction_requires_rrsig(DnsTransaction *t, DnsResourceRecord *rr) { | |
56352fe9 LP |
2820 | int r; |
2821 | ||
2822 | assert(t); | |
2823 | assert(rr); | |
2824 | ||
105e1512 LP |
2825 | /* Checks if the RR we are looking for must be signed with an |
2826 | * RRSIG. This is used for positive responses. */ | |
24a5b982 | 2827 | |
b652d4a2 | 2828 | if (t->scope->dnssec_mode == DNSSEC_NO) |
105e1512 | 2829 | return false; |
56352fe9 | 2830 | |
105e1512 LP |
2831 | if (dns_type_is_pseudo(rr->key->type)) |
2832 | return -EINVAL; | |
56352fe9 | 2833 | |
1c02e7ba | 2834 | r = dns_transaction_negative_trust_anchor_lookup(t, dns_resource_key_name(rr->key)); |
8e54f5d9 LP |
2835 | if (r < 0) |
2836 | return r; | |
2837 | if (r > 0) | |
2838 | return false; | |
2839 | ||
105e1512 | 2840 | switch (rr->key->type) { |
56352fe9 | 2841 | |
105e1512 LP |
2842 | case DNS_TYPE_RRSIG: |
2843 | /* RRSIGs are the signatures themselves, they need no signing. */ | |
2844 | return false; | |
2845 | ||
2846 | case DNS_TYPE_SOA: | |
2847 | case DNS_TYPE_NS: { | |
2848 | DnsTransaction *dt; | |
105e1512 | 2849 | |
b63fca62 | 2850 | /* For SOA or NS RRs we look for a matching DS transaction */ |
90e74a66 | 2851 | SET_FOREACH(dt, t->dnssec_transactions) { |
105e1512 | 2852 | |
42df9532 | 2853 | if (dns_transaction_key(dt)->class != rr->key->class) |
105e1512 | 2854 | continue; |
42df9532 | 2855 | if (dns_transaction_key(dt)->type != DNS_TYPE_DS) |
105e1512 LP |
2856 | continue; |
2857 | ||
47690634 | 2858 | r = dns_name_endswith(dns_resource_key_name(rr->key), dns_resource_key_name(dns_transaction_key(dt))); |
105e1512 LP |
2859 | if (r < 0) |
2860 | return r; | |
2861 | if (r == 0) | |
2862 | continue; | |
2863 | ||
2864 | /* We found a DS transactions for the SOA/NS | |
2865 | * RRs we are looking at. If it discovered signed DS | |
2866 | * RRs, then we need to be signed, too. */ | |
2867 | ||
6f055e43 | 2868 | if (!FLAGS_SET(dt->answer_query_flags, SD_RESOLVED_AUTHENTICATED)) |
097a2517 | 2869 | return false; |
105e1512 | 2870 | |
42df9532 | 2871 | return dns_answer_match_key(dt->answer, dns_transaction_key(dt), NULL); |
105e1512 LP |
2872 | } |
2873 | ||
2874 | /* We found nothing that proves this is safe to leave | |
2875 | * this unauthenticated, hence ask inist on | |
2876 | * authentication. */ | |
2877 | return true; | |
2878 | } | |
2879 | ||
b63fca62 | 2880 | case DNS_TYPE_DS: |
105e1512 LP |
2881 | case DNS_TYPE_CNAME: |
2882 | case DNS_TYPE_DNAME: { | |
2883 | const char *parent = NULL; | |
2884 | DnsTransaction *dt; | |
105e1512 | 2885 | |
b63fca62 | 2886 | /* |
47690634 | 2887 | * CNAME/DNAME RRs cannot be located at a zone apex, hence look directly for the parent DS. |
b63fca62 | 2888 | * |
47690634 | 2889 | * DS RRs are signed if the parent is signed, hence also look at the parent DS |
b63fca62 | 2890 | */ |
105e1512 | 2891 | |
90e74a66 | 2892 | SET_FOREACH(dt, t->dnssec_transactions) { |
105e1512 | 2893 | |
42df9532 | 2894 | if (dns_transaction_key(dt)->class != rr->key->class) |
105e1512 | 2895 | continue; |
47690634 | 2896 | if (dns_transaction_key(dt)->type != DNS_TYPE_DS) |
105e1512 LP |
2897 | continue; |
2898 | ||
2899 | if (!parent) { | |
1c02e7ba | 2900 | parent = dns_resource_key_name(rr->key); |
105e1512 LP |
2901 | r = dns_name_parent(&parent); |
2902 | if (r < 0) | |
2903 | return r; | |
2904 | if (r == 0) { | |
b63fca62 LP |
2905 | if (rr->key->type == DNS_TYPE_DS) |
2906 | return true; | |
2907 | ||
105e1512 | 2908 | /* A CNAME/DNAME without a parent? That's sooo weird. */ |
baaa35ad ZJS |
2909 | return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG), |
2910 | "Transaction %" PRIu16 " claims CNAME/DNAME at root. Refusing.", t->id); | |
105e1512 LP |
2911 | } |
2912 | } | |
2913 | ||
47690634 | 2914 | r = dns_name_endswith(parent, dns_resource_key_name(dns_transaction_key(dt))); |
105e1512 LP |
2915 | if (r < 0) |
2916 | return r; | |
2917 | if (r == 0) | |
2918 | continue; | |
2919 | ||
3b4cc143 | 2920 | return FLAGS_SET(dt->answer_query_flags, SD_RESOLVED_AUTHENTICATED); |
105e1512 LP |
2921 | } |
2922 | ||
2923 | return true; | |
2924 | } | |
2925 | ||
2926 | default: { | |
2927 | DnsTransaction *dt; | |
105e1512 | 2928 | |
47690634 | 2929 | /* Any other kind of RR (including DNSKEY/NSEC/NSEC3). Let's see if our DS lookup was authenticated */ |
105e1512 | 2930 | |
90e74a66 | 2931 | SET_FOREACH(dt, t->dnssec_transactions) { |
42df9532 | 2932 | if (dns_transaction_key(dt)->class != rr->key->class) |
105e1512 | 2933 | continue; |
47690634 | 2934 | if (dns_transaction_key(dt)->type != DNS_TYPE_DS) |
105e1512 LP |
2935 | continue; |
2936 | ||
47690634 | 2937 | r = dns_name_endswith(dns_resource_key_name(rr->key), dns_resource_key_name(dns_transaction_key(dt))); |
105e1512 LP |
2938 | if (r < 0) |
2939 | return r; | |
2940 | if (r == 0) | |
2941 | continue; | |
2942 | ||
47690634 RP |
2943 | if (!FLAGS_SET(dt->answer_query_flags, SD_RESOLVED_AUTHENTICATED)) |
2944 | return false; | |
2945 | ||
2946 | /* We expect this to be signed when the DS record exists, and don't expect it to be | |
2947 | * signed when the DS record is proven not to exist. */ | |
2948 | return dns_answer_match_key(dt->answer, dns_transaction_key(dt), NULL); | |
105e1512 LP |
2949 | } |
2950 | ||
2951 | return true; | |
2952 | }} | |
56352fe9 LP |
2953 | } |
2954 | ||
d33b6cf3 LP |
2955 | static int dns_transaction_in_private_tld(DnsTransaction *t, const DnsResourceKey *key) { |
2956 | DnsTransaction *dt; | |
2957 | const char *tld; | |
d33b6cf3 LP |
2958 | int r; |
2959 | ||
2960 | /* If DNSSEC downgrade mode is on, checks whether the | |
2961 | * specified RR is one level below a TLD we have proven not to | |
2962 | * exist. In such a case we assume that this is a private | |
2963 | * domain, and permit it. | |
2964 | * | |
2965 | * This detects cases like the Fritz!Box router networks. Each | |
2966 | * Fritz!Box router serves a private "fritz.box" zone, in the | |
2967 | * non-existing TLD "box". Requests for the "fritz.box" domain | |
2968 | * are served by the router itself, while requests for the | |
2969 | * "box" domain will result in NXDOMAIN. | |
2970 | * | |
2971 | * Note that this logic is unable to detect cases where a | |
2972 | * router serves a private DNS zone directly under | |
2973 | * non-existing TLD. In such a case we cannot detect whether | |
2974 | * the TLD is supposed to exist or not, as all requests we | |
2975 | * make for it will be answered by the router's zone, and not | |
2976 | * by the root zone. */ | |
2977 | ||
2978 | assert(t); | |
2979 | ||
2980 | if (t->scope->dnssec_mode != DNSSEC_ALLOW_DOWNGRADE) | |
2981 | return false; /* In strict DNSSEC mode what doesn't exist, doesn't exist */ | |
2982 | ||
1c02e7ba | 2983 | tld = dns_resource_key_name(key); |
d33b6cf3 LP |
2984 | r = dns_name_parent(&tld); |
2985 | if (r < 0) | |
2986 | return r; | |
2987 | if (r == 0) | |
2988 | return false; /* Already the root domain */ | |
2989 | ||
2990 | if (!dns_name_is_single_label(tld)) | |
2991 | return false; | |
2992 | ||
90e74a66 | 2993 | SET_FOREACH(dt, t->dnssec_transactions) { |
d33b6cf3 | 2994 | |
42df9532 | 2995 | if (dns_transaction_key(dt)->class != key->class) |
d33b6cf3 LP |
2996 | continue; |
2997 | ||
42df9532 | 2998 | r = dns_name_equal(dns_resource_key_name(dns_transaction_key(dt)), tld); |
d33b6cf3 LP |
2999 | if (r < 0) |
3000 | return r; | |
3001 | if (r == 0) | |
3002 | continue; | |
3003 | ||
3004 | /* We found an auxiliary lookup we did for the TLD. If | |
3005 | * that returned with NXDOMAIN, we know the TLD didn't | |
3006 | * exist, and hence this might be a private zone. */ | |
3007 | ||
3008 | return dt->answer_rcode == DNS_RCODE_NXDOMAIN; | |
3009 | } | |
3010 | ||
3011 | return false; | |
3012 | } | |
3013 | ||
105e1512 | 3014 | static int dns_transaction_requires_nsec(DnsTransaction *t) { |
4bbc06cc | 3015 | char key_str[DNS_RESOURCE_KEY_STRING_MAX]; |
105e1512 LP |
3016 | DnsTransaction *dt; |
3017 | const char *name; | |
105e1512 | 3018 | int r; |
56352fe9 LP |
3019 | |
3020 | assert(t); | |
3021 | ||
105e1512 LP |
3022 | /* Checks if we need to insist on NSEC/NSEC3 RRs for proving |
3023 | * this negative reply */ | |
56352fe9 | 3024 | |
b652d4a2 | 3025 | if (t->scope->dnssec_mode == DNSSEC_NO) |
105e1512 | 3026 | return false; |
56352fe9 | 3027 | |
42df9532 | 3028 | if (dns_type_is_pseudo(dns_transaction_key(t)->type)) |
105e1512 LP |
3029 | return -EINVAL; |
3030 | ||
42df9532 | 3031 | r = dns_transaction_negative_trust_anchor_lookup(t, dns_resource_key_name(dns_transaction_key(t))); |
8e54f5d9 LP |
3032 | if (r < 0) |
3033 | return r; | |
3034 | if (r > 0) | |
3035 | return false; | |
3036 | ||
42df9532 | 3037 | r = dns_transaction_in_private_tld(t, dns_transaction_key(t)); |
d33b6cf3 LP |
3038 | if (r < 0) |
3039 | return r; | |
3040 | if (r > 0) { | |
3041 | /* The lookup is from a TLD that is proven not to | |
3042 | * exist, and we are in downgrade mode, hence ignore | |
13e785f7 | 3043 | * that fact that we didn't get any NSEC RRs. */ |
d33b6cf3 | 3044 | |
202b76ae | 3045 | log_info("Detected a negative query %s in a private DNS zone, permitting unsigned response.", |
42df9532 | 3046 | dns_resource_key_to_string(dns_transaction_key(t), key_str, sizeof key_str)); |
d33b6cf3 LP |
3047 | return false; |
3048 | } | |
3049 | ||
42df9532 | 3050 | name = dns_resource_key_name(dns_transaction_key(t)); |
105e1512 | 3051 | |
47690634 RP |
3052 | if (IN_SET(dns_transaction_key(t)->type, DNS_TYPE_DS, DNS_TYPE_CNAME, DNS_TYPE_DNAME)) { |
3053 | /* We got a negative reply for this DS/CNAME/DNAME lookup? Check the parent in this case to | |
3054 | * see if this answer should have been signed. */ | |
105e1512 | 3055 | r = dns_name_parent(&name); |
56352fe9 LP |
3056 | if (r < 0) |
3057 | return r; | |
3058 | if (r == 0) | |
105e1512 | 3059 | return true; |
47690634 | 3060 | } |
4bbc06cc | 3061 | |
47690634 | 3062 | /* For all other RRs we check the DS on the same level to see |
105e1512 LP |
3063 | * if it's signed. */ |
3064 | ||
90e74a66 | 3065 | SET_FOREACH(dt, t->dnssec_transactions) { |
42df9532 | 3066 | if (dns_transaction_key(dt)->class != dns_transaction_key(t)->class) |
105e1512 | 3067 | continue; |
47690634 | 3068 | if (dns_transaction_key(dt)->type != DNS_TYPE_DS) |
56352fe9 LP |
3069 | continue; |
3070 | ||
47690634 | 3071 | r = dns_name_endswith(name, dns_resource_key_name(dns_transaction_key(dt))); |
56352fe9 LP |
3072 | if (r < 0) |
3073 | return r; | |
105e1512 LP |
3074 | if (r == 0) |
3075 | continue; | |
3076 | ||
47690634 RP |
3077 | if (!FLAGS_SET(dt->answer_query_flags, SD_RESOLVED_AUTHENTICATED)) |
3078 | return false; | |
3079 | ||
3080 | /* We expect this to be signed when the DS record exists, and don't expect it to be signed | |
3081 | * when the DS record is proven not to exist. */ | |
3082 | return dns_answer_match_key(dt->answer, dns_transaction_key(dt), NULL); | |
56352fe9 LP |
3083 | } |
3084 | ||
105e1512 LP |
3085 | /* If in doubt, require NSEC/NSEC3 */ |
3086 | return true; | |
56352fe9 LP |
3087 | } |
3088 | ||
94aa7071 LP |
3089 | static int dns_transaction_dnskey_authenticated(DnsTransaction *t, DnsResourceRecord *rr) { |
3090 | DnsResourceRecord *rrsig; | |
3091 | bool found = false; | |
3092 | int r; | |
3093 | ||
3094 | /* Checks whether any of the DNSKEYs used for the RRSIGs for | |
3095 | * the specified RRset is authenticated (i.e. has a matching | |
3096 | * DS RR). */ | |
3097 | ||
1c02e7ba | 3098 | r = dns_transaction_negative_trust_anchor_lookup(t, dns_resource_key_name(rr->key)); |
8e54f5d9 LP |
3099 | if (r < 0) |
3100 | return r; | |
3101 | if (r > 0) | |
3102 | return false; | |
3103 | ||
94aa7071 LP |
3104 | DNS_ANSWER_FOREACH(rrsig, t->answer) { |
3105 | DnsTransaction *dt; | |
94aa7071 LP |
3106 | |
3107 | r = dnssec_key_match_rrsig(rr->key, rrsig); | |
3108 | if (r < 0) | |
3109 | return r; | |
3110 | if (r == 0) | |
3111 | continue; | |
3112 | ||
90e74a66 | 3113 | SET_FOREACH(dt, t->dnssec_transactions) { |
94aa7071 | 3114 | |
42df9532 | 3115 | if (dns_transaction_key(dt)->class != rr->key->class) |
94aa7071 LP |
3116 | continue; |
3117 | ||
42df9532 | 3118 | if (dns_transaction_key(dt)->type == DNS_TYPE_DNSKEY) { |
94aa7071 | 3119 | |
42df9532 | 3120 | r = dns_name_equal(dns_resource_key_name(dns_transaction_key(dt)), rrsig->rrsig.signer); |
94aa7071 LP |
3121 | if (r < 0) |
3122 | return r; | |
3123 | if (r == 0) | |
3124 | continue; | |
3125 | ||
6f055e43 LP |
3126 | /* OK, we found an auxiliary DNSKEY lookup. If that lookup is authenticated, |
3127 | * report this. */ | |
94aa7071 | 3128 | |
6f055e43 | 3129 | if (FLAGS_SET(dt->answer_query_flags, SD_RESOLVED_AUTHENTICATED)) |
94aa7071 LP |
3130 | return true; |
3131 | ||
3132 | found = true; | |
3133 | ||
42df9532 | 3134 | } else if (dns_transaction_key(dt)->type == DNS_TYPE_DS) { |
94aa7071 | 3135 | |
42df9532 | 3136 | r = dns_name_equal(dns_resource_key_name(dns_transaction_key(dt)), rrsig->rrsig.signer); |
94aa7071 LP |
3137 | if (r < 0) |
3138 | return r; | |
3139 | if (r == 0) | |
3140 | continue; | |
3141 | ||
6f055e43 LP |
3142 | /* OK, we found an auxiliary DS lookup. If that lookup is authenticated and |
3143 | * non-zero, we won! */ | |
94aa7071 | 3144 | |
6f055e43 | 3145 | if (!FLAGS_SET(dt->answer_query_flags, SD_RESOLVED_AUTHENTICATED)) |
94aa7071 LP |
3146 | return false; |
3147 | ||
42df9532 | 3148 | return dns_answer_match_key(dt->answer, dns_transaction_key(dt), NULL); |
94aa7071 LP |
3149 | } |
3150 | } | |
3151 | } | |
3152 | ||
3153 | return found ? false : -ENXIO; | |
3154 | } | |
3155 | ||
b652d4a2 LP |
3156 | static int dns_transaction_known_signed(DnsTransaction *t, DnsResourceRecord *rr) { |
3157 | assert(t); | |
3158 | assert(rr); | |
3159 | ||
3160 | /* We know that the root domain is signed, hence if it appears | |
3161 | * not to be signed, there's a problem with the DNS server */ | |
3162 | ||
3163 | return rr->key->class == DNS_CLASS_IN && | |
1c02e7ba | 3164 | dns_name_is_root(dns_resource_key_name(rr->key)); |
b652d4a2 LP |
3165 | } |
3166 | ||
0f87f3e8 LP |
3167 | static int dns_transaction_check_revoked_trust_anchors(DnsTransaction *t) { |
3168 | DnsResourceRecord *rr; | |
3169 | int r; | |
3170 | ||
3171 | assert(t); | |
3172 | ||
3173 | /* Maybe warn the user that we encountered a revoked DNSKEY | |
3174 | * for a key from our trust anchor. Note that we don't care | |
3175 | * whether the DNSKEY can be authenticated or not. It's | |
3176 | * sufficient if it is self-signed. */ | |
3177 | ||
3178 | DNS_ANSWER_FOREACH(rr, t->answer) { | |
d424da2a | 3179 | r = dns_trust_anchor_check_revoked(&t->scope->manager->trust_anchor, rr, t->answer); |
0f87f3e8 LP |
3180 | if (r < 0) |
3181 | return r; | |
3182 | } | |
3183 | ||
3184 | return 0; | |
3185 | } | |
3186 | ||
c9c72065 LP |
3187 | static int dns_transaction_invalidate_revoked_keys(DnsTransaction *t) { |
3188 | bool changed; | |
3189 | int r; | |
3190 | ||
3191 | assert(t); | |
3192 | ||
3193 | /* Removes all DNSKEY/DS objects from t->validated_keys that | |
3194 | * our trust anchors database considers revoked. */ | |
3195 | ||
3196 | do { | |
3197 | DnsResourceRecord *rr; | |
3198 | ||
3199 | changed = false; | |
3200 | ||
3201 | DNS_ANSWER_FOREACH(rr, t->validated_keys) { | |
3202 | r = dns_trust_anchor_is_revoked(&t->scope->manager->trust_anchor, rr); | |
3203 | if (r < 0) | |
3204 | return r; | |
3205 | if (r > 0) { | |
3206 | r = dns_answer_remove_by_rr(&t->validated_keys, rr); | |
3207 | if (r < 0) | |
3208 | return r; | |
3209 | ||
3210 | assert(r > 0); | |
3211 | changed = true; | |
3212 | break; | |
3213 | } | |
3214 | } | |
3215 | } while (changed); | |
3216 | ||
3217 | return 0; | |
3218 | } | |
3219 | ||
942eb2e7 LP |
3220 | static int dns_transaction_copy_validated(DnsTransaction *t) { |
3221 | DnsTransaction *dt; | |
942eb2e7 LP |
3222 | int r; |
3223 | ||
3224 | assert(t); | |
3225 | ||
3226 | /* Copy all validated RRs from the auxiliary DNSSEC transactions into our set of validated RRs */ | |
3227 | ||
90e74a66 | 3228 | SET_FOREACH(dt, t->dnssec_transactions) { |
942eb2e7 LP |
3229 | |
3230 | if (DNS_TRANSACTION_IS_LIVE(dt->state)) | |
3231 | continue; | |
3232 | ||
6f055e43 | 3233 | if (!FLAGS_SET(dt->answer_query_flags, SD_RESOLVED_AUTHENTICATED)) |
942eb2e7 LP |
3234 | continue; |
3235 | ||
3236 | r = dns_answer_extend(&t->validated_keys, dt->answer); | |
3237 | if (r < 0) | |
3238 | return r; | |
3239 | } | |
3240 | ||
3241 | return 0; | |
3242 | } | |
3243 | ||
c690b20a ZJS |
3244 | typedef enum { |
3245 | DNSSEC_PHASE_DNSKEY, /* Phase #1, only validate DNSKEYs */ | |
3246 | DNSSEC_PHASE_NSEC, /* Phase #2, only validate NSEC+NSEC3 */ | |
3247 | DNSSEC_PHASE_ALL, /* Phase #3, validate everything else */ | |
3248 | } Phase; | |
3249 | ||
3250 | static int dnssec_validate_records( | |
3251 | DnsTransaction *t, | |
3252 | Phase phase, | |
3253 | bool *have_nsec, | |
67d0ce88 | 3254 | unsigned *nvalidations, |
c690b20a ZJS |
3255 | DnsAnswer **validated) { |
3256 | ||
547973de | 3257 | DnsResourceRecord *rr; |
56352fe9 | 3258 | int r; |
547973de | 3259 | |
67d0ce88 RP |
3260 | assert(nvalidations); |
3261 | ||
c690b20a | 3262 | /* Returns negative on error, 0 if validation failed, 1 to restart validation, 2 when finished. */ |
547973de | 3263 | |
c690b20a | 3264 | DNS_ANSWER_FOREACH(rr, t->answer) { |
71aee23d | 3265 | _unused_ _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr_ref = dns_resource_record_ref(rr); |
c690b20a ZJS |
3266 | DnsResourceRecord *rrsig = NULL; |
3267 | DnssecResult result; | |
547973de | 3268 | |
c690b20a ZJS |
3269 | switch (rr->key->type) { |
3270 | case DNS_TYPE_RRSIG: | |
3271 | continue; | |
547973de | 3272 | |
c690b20a ZJS |
3273 | case DNS_TYPE_DNSKEY: |
3274 | /* We validate DNSKEYs only in the DNSKEY and ALL phases */ | |
3275 | if (phase == DNSSEC_PHASE_NSEC) | |
3276 | continue; | |
3277 | break; | |
547973de | 3278 | |
c690b20a ZJS |
3279 | case DNS_TYPE_NSEC: |
3280 | case DNS_TYPE_NSEC3: | |
3281 | *have_nsec = true; | |
547973de | 3282 | |
c690b20a ZJS |
3283 | /* We validate NSEC/NSEC3 only in the NSEC and ALL phases */ |
3284 | if (phase == DNSSEC_PHASE_DNSKEY) | |
3285 | continue; | |
3286 | break; | |
105e1512 | 3287 | |
c690b20a ZJS |
3288 | default: |
3289 | /* We validate all other RRs only in the ALL phases */ | |
3290 | if (phase != DNSSEC_PHASE_ALL) | |
3291 | continue; | |
3292 | } | |
b652d4a2 | 3293 | |
04617bf8 LP |
3294 | r = dnssec_verify_rrset_search( |
3295 | t->answer, | |
3296 | rr->key, | |
3297 | t->validated_keys, | |
3298 | USEC_INFINITY, | |
3299 | &result, | |
3300 | &rrsig); | |
c690b20a ZJS |
3301 | if (r < 0) |
3302 | return r; | |
67d0ce88 | 3303 | *nvalidations += r; |
547973de | 3304 | |
c690b20a | 3305 | log_debug("Looking at %s: %s", strna(dns_resource_record_to_string(rr)), dnssec_result_to_string(result)); |
0f87f3e8 | 3306 | |
c690b20a | 3307 | if (result == DNSSEC_VALIDATED) { |
04617bf8 | 3308 | assert(rrsig); |
942eb2e7 | 3309 | |
c690b20a ZJS |
3310 | if (rr->key->type == DNS_TYPE_DNSKEY) { |
3311 | /* If we just validated a DNSKEY RRset, then let's add these keys to | |
3312 | * the set of validated keys for this transaction. */ | |
547973de | 3313 | |
04617bf8 | 3314 | r = dns_answer_copy_by_key(&t->validated_keys, t->answer, rr->key, DNS_ANSWER_AUTHENTICATED, rrsig); |
c690b20a ZJS |
3315 | if (r < 0) |
3316 | return r; | |
c9c72065 | 3317 | |
c690b20a ZJS |
3318 | /* Some of the DNSKEYs we just added might already have been revoked, |
3319 | * remove them again in that case. */ | |
3320 | r = dns_transaction_invalidate_revoked_keys(t); | |
3321 | if (r < 0) | |
3322 | return r; | |
3323 | } | |
547973de | 3324 | |
04617bf8 LP |
3325 | /* Add the validated RRset to the new list of validated RRsets, and remove it from |
3326 | * the unvalidated RRsets. We mark the RRset as authenticated and cacheable. */ | |
3327 | r = dns_answer_move_by_key(validated, &t->answer, rr->key, DNS_ANSWER_AUTHENTICATED|DNS_ANSWER_CACHEABLE, rrsig); | |
c690b20a ZJS |
3328 | if (r < 0) |
3329 | return r; | |
547973de | 3330 | |
c690b20a | 3331 | manager_dnssec_verdict(t->scope->manager, DNSSEC_SECURE, rr->key); |
0c7bff0a | 3332 | |
c690b20a ZJS |
3333 | /* Exit the loop, we dropped something from the answer, start from the beginning */ |
3334 | return 1; | |
3335 | } | |
547973de | 3336 | |
c690b20a ZJS |
3337 | /* If we haven't read all DNSKEYs yet a negative result of the validation is irrelevant, as |
3338 | * there might be more DNSKEYs coming. Similar, if we haven't read all NSEC/NSEC3 RRs yet, | |
3339 | * we cannot do positive wildcard proofs yet, as those require the NSEC/NSEC3 RRs. */ | |
3340 | if (phase != DNSSEC_PHASE_ALL) | |
3341 | continue; | |
0c7bff0a | 3342 | |
c690b20a ZJS |
3343 | if (result == DNSSEC_VALIDATED_WILDCARD) { |
3344 | bool authenticated = false; | |
3345 | const char *source; | |
0c7bff0a | 3346 | |
04617bf8 LP |
3347 | assert(rrsig); |
3348 | ||
c690b20a | 3349 | /* This RRset validated, but as a wildcard. This means we need |
13e785f7 | 3350 | * to prove via NSEC/NSEC3 that no matching non-wildcard RR exists. */ |
0c7bff0a | 3351 | |
c690b20a ZJS |
3352 | /* First step, determine the source of synthesis */ |
3353 | r = dns_resource_record_source(rrsig, &source); | |
3354 | if (r < 0) | |
3355 | return r; | |
0c7bff0a | 3356 | |
c690b20a | 3357 | r = dnssec_test_positive_wildcard(*validated, |
1c02e7ba | 3358 | dns_resource_key_name(rr->key), |
c690b20a ZJS |
3359 | source, |
3360 | rrsig->rrsig.signer, | |
3361 | &authenticated); | |
0c7bff0a | 3362 | |
c690b20a ZJS |
3363 | /* Unless the NSEC proof showed that the key really doesn't exist something is off. */ |
3364 | if (r == 0) | |
3365 | result = DNSSEC_INVALID; | |
3366 | else { | |
04617bf8 LP |
3367 | r = dns_answer_move_by_key( |
3368 | validated, | |
3369 | &t->answer, | |
3370 | rr->key, | |
3371 | authenticated ? (DNS_ANSWER_AUTHENTICATED|DNS_ANSWER_CACHEABLE) : 0, | |
3372 | rrsig); | |
c690b20a ZJS |
3373 | if (r < 0) |
3374 | return r; | |
3375 | ||
3376 | manager_dnssec_verdict(t->scope->manager, authenticated ? DNSSEC_SECURE : DNSSEC_INSECURE, rr->key); | |
3377 | ||
3378 | /* Exit the loop, we dropped something from the answer, start from the beginning */ | |
3379 | return 1; | |
0c7bff0a | 3380 | } |
c690b20a | 3381 | } |
0c7bff0a | 3382 | |
c690b20a ZJS |
3383 | if (result == DNSSEC_NO_SIGNATURE) { |
3384 | r = dns_transaction_requires_rrsig(t, rr); | |
547973de LP |
3385 | if (r < 0) |
3386 | return r; | |
c690b20a ZJS |
3387 | if (r == 0) { |
3388 | /* Data does not require signing. In that case, just copy it over, | |
13e785f7 | 3389 | * but remember that this is by no means authenticated. */ |
04617bf8 LP |
3390 | r = dns_answer_move_by_key( |
3391 | validated, | |
3392 | &t->answer, | |
3393 | rr->key, | |
3394 | 0, | |
3395 | NULL); | |
c690b20a ZJS |
3396 | if (r < 0) |
3397 | return r; | |
3398 | ||
3399 | manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, rr->key); | |
3400 | return 1; | |
3401 | } | |
547973de | 3402 | |
c690b20a ZJS |
3403 | r = dns_transaction_known_signed(t, rr); |
3404 | if (r < 0) | |
3405 | return r; | |
3406 | if (r > 0) { | |
3407 | /* This is an RR we know has to be signed. If it isn't this means | |
3408 | * the server is not attaching RRSIGs, hence complain. */ | |
547973de | 3409 | |
c690b20a | 3410 | dns_server_packet_rrsig_missing(t->server, t->current_feature_level); |
547973de | 3411 | |
c690b20a | 3412 | if (t->scope->dnssec_mode == DNSSEC_ALLOW_DOWNGRADE) { |
547973de | 3413 | |
c690b20a | 3414 | /* Downgrading is OK? If so, just consider the information unsigned */ |
c9c72065 | 3415 | |
04617bf8 | 3416 | r = dns_answer_move_by_key(validated, &t->answer, rr->key, 0, NULL); |
c9c72065 LP |
3417 | if (r < 0) |
3418 | return r; | |
547973de | 3419 | |
c690b20a ZJS |
3420 | manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, rr->key); |
3421 | return 1; | |
3422 | } | |
a150ff5e | 3423 | |
c690b20a ZJS |
3424 | /* Otherwise, fail */ |
3425 | t->answer_dnssec_result = DNSSEC_INCOMPATIBLE_SERVER; | |
3426 | return 0; | |
f3cf586d | 3427 | } |
547973de | 3428 | |
c690b20a ZJS |
3429 | r = dns_transaction_in_private_tld(t, rr->key); |
3430 | if (r < 0) | |
3431 | return r; | |
3432 | if (r > 0) { | |
202b76ae | 3433 | char s[DNS_RESOURCE_KEY_STRING_MAX]; |
b652d4a2 | 3434 | |
c690b20a ZJS |
3435 | /* The data is from a TLD that is proven not to exist, and we are in downgrade |
3436 | * mode, hence ignore the fact that this was not signed. */ | |
0c7bff0a | 3437 | |
202b76ae ZJS |
3438 | log_info("Detected RRset %s is in a private DNS zone, permitting unsigned RRs.", |
3439 | dns_resource_key_to_string(rr->key, s, sizeof s)); | |
0c7bff0a | 3440 | |
04617bf8 | 3441 | r = dns_answer_move_by_key(validated, &t->answer, rr->key, 0, NULL); |
0c7bff0a LP |
3442 | if (r < 0) |
3443 | return r; | |
0c7bff0a | 3444 | |
c690b20a ZJS |
3445 | manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, rr->key); |
3446 | return 1; | |
3447 | } | |
3448 | } | |
0c7bff0a | 3449 | |
1ca36001 JM |
3450 | /* https://datatracker.ietf.org/doc/html/rfc6840#section-5.2 */ |
3451 | if (result == DNSSEC_UNSUPPORTED_ALGORITHM) { | |
3452 | r = dns_answer_move_by_key(validated, &t->answer, rr->key, 0, NULL); | |
3453 | if (r < 0) | |
3454 | return r; | |
3455 | ||
3456 | manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, rr->key); | |
3457 | return 1; | |
3458 | } | |
3459 | ||
c690b20a ZJS |
3460 | if (IN_SET(result, |
3461 | DNSSEC_MISSING_KEY, | |
1ca36001 | 3462 | DNSSEC_SIGNATURE_EXPIRED)) { |
0c7bff0a | 3463 | |
c690b20a ZJS |
3464 | r = dns_transaction_dnskey_authenticated(t, rr); |
3465 | if (r < 0 && r != -ENXIO) | |
3466 | return r; | |
3467 | if (r == 0) { | |
3468 | /* The DNSKEY transaction was not authenticated, this means there's | |
3469 | * no DS for this, which means it's OK if no keys are found for this signature. */ | |
0c7bff0a | 3470 | |
04617bf8 | 3471 | r = dns_answer_move_by_key(validated, &t->answer, rr->key, 0, NULL); |
f3cf586d LP |
3472 | if (r < 0) |
3473 | return r; | |
b652d4a2 | 3474 | |
c690b20a ZJS |
3475 | manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, rr->key); |
3476 | return 1; | |
3477 | } | |
3478 | } | |
b652d4a2 | 3479 | |
c690b20a ZJS |
3480 | r = dns_transaction_is_primary_response(t, rr); |
3481 | if (r < 0) | |
3482 | return r; | |
3483 | if (r > 0) { | |
3484 | /* Look for a matching DNAME for this CNAME */ | |
3485 | r = dns_answer_has_dname_for_cname(t->answer, rr); | |
3486 | if (r < 0) | |
3487 | return r; | |
3488 | if (r == 0) { | |
3489 | /* Also look among the stuff we already validated */ | |
3490 | r = dns_answer_has_dname_for_cname(*validated, rr); | |
f3cf586d LP |
3491 | if (r < 0) |
3492 | return r; | |
c690b20a | 3493 | } |
d33b6cf3 | 3494 | |
c690b20a ZJS |
3495 | if (r == 0) { |
3496 | if (IN_SET(result, | |
3497 | DNSSEC_INVALID, | |
3498 | DNSSEC_SIGNATURE_EXPIRED, | |
3499 | DNSSEC_NO_SIGNATURE)) | |
3500 | manager_dnssec_verdict(t->scope->manager, DNSSEC_BOGUS, rr->key); | |
67d0ce88 RP |
3501 | else /* DNSSEC_MISSING_KEY, DNSSEC_UNSUPPORTED_ALGORITHM, |
3502 | or DNSSEC_TOO_MANY_VALIDATIONS */ | |
c690b20a ZJS |
3503 | manager_dnssec_verdict(t->scope->manager, DNSSEC_INDETERMINATE, rr->key); |
3504 | ||
3505 | /* This is a primary response to our question, and it failed validation. | |
3506 | * That's fatal. */ | |
3507 | t->answer_dnssec_result = result; | |
3508 | return 0; | |
3509 | } | |
d33b6cf3 | 3510 | |
c690b20a ZJS |
3511 | /* This is a primary response, but we do have a DNAME RR |
3512 | * in the RR that can replay this CNAME, hence rely on | |
3513 | * that, and we can remove the CNAME in favour of it. */ | |
3514 | } | |
d33b6cf3 | 3515 | |
c690b20a ZJS |
3516 | /* This is just some auxiliary data. Just remove the RRset and continue. */ |
3517 | r = dns_answer_remove_by_key(&t->answer, rr->key); | |
3518 | if (r < 0) | |
3519 | return r; | |
d33b6cf3 | 3520 | |
c690b20a ZJS |
3521 | /* We dropped something from the answer, start from the beginning. */ |
3522 | return 1; | |
3523 | } | |
f3cf586d | 3524 | |
c690b20a ZJS |
3525 | return 2; /* Finito. */ |
3526 | } | |
94aa7071 | 3527 | |
c690b20a ZJS |
3528 | int dns_transaction_validate_dnssec(DnsTransaction *t) { |
3529 | _cleanup_(dns_answer_unrefp) DnsAnswer *validated = NULL; | |
3530 | Phase phase; | |
3531 | DnsAnswerFlags flags; | |
3532 | int r; | |
202b76ae | 3533 | char key_str[DNS_RESOURCE_KEY_STRING_MAX]; |
94aa7071 | 3534 | |
c690b20a | 3535 | assert(t); |
94aa7071 | 3536 | |
775ae354 | 3537 | /* We have now collected all DS and DNSKEY RRs in t->validated_keys, let's see which RRs we can now |
c690b20a | 3538 | * authenticate with that. */ |
94aa7071 | 3539 | |
775ae354 | 3540 | if (FLAGS_SET(t->query_flags, SD_RESOLVED_NO_VALIDATE) || t->scope->dnssec_mode == DNSSEC_NO) |
c690b20a | 3541 | return 0; |
a150ff5e | 3542 | |
c690b20a ZJS |
3543 | /* Already validated */ |
3544 | if (t->answer_dnssec_result != _DNSSEC_RESULT_INVALID) | |
3545 | return 0; | |
105e1512 | 3546 | |
c690b20a ZJS |
3547 | /* Our own stuff needs no validation */ |
3548 | if (IN_SET(t->answer_source, DNS_TRANSACTION_ZONE, DNS_TRANSACTION_TRUST_ANCHOR)) { | |
3549 | t->answer_dnssec_result = DNSSEC_VALIDATED; | |
6f055e43 | 3550 | SET_FLAG(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED, true); |
c690b20a ZJS |
3551 | return 0; |
3552 | } | |
a150ff5e | 3553 | |
c690b20a ZJS |
3554 | /* Cached stuff is not affected by validation. */ |
3555 | if (t->answer_source != DNS_TRANSACTION_NETWORK) | |
3556 | return 0; | |
f3cf586d | 3557 | |
c690b20a ZJS |
3558 | if (!dns_transaction_dnssec_supported_full(t)) { |
3559 | /* The server does not support DNSSEC, or doesn't augment responses with RRSIGs. */ | |
3560 | t->answer_dnssec_result = DNSSEC_INCOMPATIBLE_SERVER; | |
d001e0a3 | 3561 | log_debug("Not validating response for %" PRIu16 ", used server feature level does not support DNSSEC.", t->id); |
c690b20a ZJS |
3562 | return 0; |
3563 | } | |
f3cf586d | 3564 | |
202b76ae ZJS |
3565 | log_debug("Validating response from transaction %" PRIu16 " (%s).", |
3566 | t->id, | |
42df9532 | 3567 | dns_resource_key_to_string(dns_transaction_key(t), key_str, sizeof key_str)); |
547973de | 3568 | |
c690b20a ZJS |
3569 | /* First, see if this response contains any revoked trust |
3570 | * anchors we care about */ | |
3571 | r = dns_transaction_check_revoked_trust_anchors(t); | |
3572 | if (r < 0) | |
3573 | return r; | |
43e6779a | 3574 | |
c690b20a ZJS |
3575 | /* Third, copy all RRs we acquired successfully from auxiliary RRs over. */ |
3576 | r = dns_transaction_copy_validated(t); | |
3577 | if (r < 0) | |
3578 | return r; | |
43e6779a | 3579 | |
c690b20a ZJS |
3580 | /* Second, see if there are DNSKEYs we already know a |
3581 | * validated DS for. */ | |
3582 | r = dns_transaction_validate_dnskey_by_ds(t); | |
3583 | if (r < 0) | |
3584 | return r; | |
43e6779a | 3585 | |
c690b20a ZJS |
3586 | /* Fourth, remove all DNSKEY and DS RRs again that our trust |
3587 | * anchor says are revoked. After all we might have marked | |
3588 | * some keys revoked above, but they might still be lingering | |
3589 | * in our validated_keys list. */ | |
3590 | r = dns_transaction_invalidate_revoked_keys(t); | |
3591 | if (r < 0) | |
3592 | return r; | |
f3cf586d | 3593 | |
c690b20a | 3594 | phase = DNSSEC_PHASE_DNSKEY; |
67d0ce88 | 3595 | for (unsigned nvalidations = 0;;) { |
c690b20a | 3596 | bool have_nsec = false; |
f3cf586d | 3597 | |
67d0ce88 | 3598 | r = dnssec_validate_records(t, phase, &have_nsec, &nvalidations, &validated); |
0dfea62e RP |
3599 | if (r <= 0) { |
3600 | DNS_ANSWER_REPLACE(t->answer, TAKE_PTR(validated)); | |
c690b20a | 3601 | return r; |
0dfea62e | 3602 | } |
547973de | 3603 | |
67d0ce88 RP |
3604 | if (nvalidations > DNSSEC_VALIDATION_MAX) { |
3605 | /* This reply requires an onerous number of signature validations to verify. Let's | |
3606 | * not waste our time trying, as this shouldn't happen for well-behaved domains | |
3607 | * anyway. */ | |
3608 | t->answer_dnssec_result = DNSSEC_TOO_MANY_VALIDATIONS; | |
0dfea62e | 3609 | DNS_ANSWER_REPLACE(t->answer, TAKE_PTR(validated)); |
67d0ce88 RP |
3610 | return 0; |
3611 | } | |
3612 | ||
c690b20a ZJS |
3613 | /* Try again as long as we managed to achieve something */ |
3614 | if (r == 1) | |
547973de LP |
3615 | continue; |
3616 | ||
c690b20a | 3617 | if (phase == DNSSEC_PHASE_DNSKEY && have_nsec) { |
0c7bff0a | 3618 | /* OK, we processed all DNSKEYs, and there are NSEC/NSEC3 RRs, look at those now. */ |
c690b20a | 3619 | phase = DNSSEC_PHASE_NSEC; |
0c7bff0a LP |
3620 | continue; |
3621 | } | |
3622 | ||
c690b20a ZJS |
3623 | if (phase != DNSSEC_PHASE_ALL) { |
3624 | /* OK, we processed all DNSKEYs and NSEC/NSEC3 RRs, look at all the rest now. | |
3625 | * Note that in this third phase we start to remove RRs we couldn't validate. */ | |
3626 | phase = DNSSEC_PHASE_ALL; | |
56352fe9 | 3627 | continue; |
547973de LP |
3628 | } |
3629 | ||
56352fe9 | 3630 | /* We're done */ |
547973de LP |
3631 | break; |
3632 | } | |
3633 | ||
1117a960 | 3634 | DNS_ANSWER_REPLACE(t->answer, TAKE_PTR(validated)); |
547973de | 3635 | |
72667f08 LP |
3636 | /* At this point the answer only contains validated |
3637 | * RRsets. Now, let's see if it actually answers the question | |
3638 | * we asked. If so, great! If it doesn't, then see if | |
3639 | * NSEC/NSEC3 can prove this. */ | |
105e1512 | 3640 | r = dns_transaction_has_positive_answer(t, &flags); |
72667f08 | 3641 | if (r > 0) { |
105e1512 LP |
3642 | /* Yes, it answers the question! */ |
3643 | ||
3644 | if (flags & DNS_ANSWER_AUTHENTICATED) { | |
3645 | /* The answer is fully authenticated, yay. */ | |
019036a4 | 3646 | t->answer_dnssec_result = DNSSEC_VALIDATED; |
105e1512 | 3647 | t->answer_rcode = DNS_RCODE_SUCCESS; |
6f055e43 | 3648 | SET_FLAG(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED, true); |
105e1512 LP |
3649 | } else { |
3650 | /* The answer is not fully authenticated. */ | |
019036a4 | 3651 | t->answer_dnssec_result = DNSSEC_UNSIGNED; |
6f055e43 | 3652 | SET_FLAG(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED, false); |
105e1512 LP |
3653 | } |
3654 | ||
72667f08 LP |
3655 | } else if (r == 0) { |
3656 | DnssecNsecResult nr; | |
ed29bfdc | 3657 | bool authenticated = false; |
72667f08 LP |
3658 | |
3659 | /* Bummer! Let's check NSEC/NSEC3 */ | |
42df9532 | 3660 | r = dnssec_nsec_test(t->answer, dns_transaction_key(t), &nr, &authenticated, &t->answer_nsec_ttl); |
72667f08 LP |
3661 | if (r < 0) |
3662 | return r; | |
3663 | ||
3664 | switch (nr) { | |
3665 | ||
3666 | case DNSSEC_NSEC_NXDOMAIN: | |
3667 | /* NSEC proves the domain doesn't exist. Very good. */ | |
202b76ae | 3668 | log_debug("Proved NXDOMAIN via NSEC/NSEC3 for transaction %u (%s)", t->id, key_str); |
019036a4 | 3669 | t->answer_dnssec_result = DNSSEC_VALIDATED; |
72667f08 | 3670 | t->answer_rcode = DNS_RCODE_NXDOMAIN; |
6f055e43 | 3671 | SET_FLAG(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED, authenticated); |
7aa8ce98 | 3672 | |
42df9532 | 3673 | manager_dnssec_verdict(t->scope->manager, authenticated ? DNSSEC_SECURE : DNSSEC_INSECURE, dns_transaction_key(t)); |
72667f08 LP |
3674 | break; |
3675 | ||
3676 | case DNSSEC_NSEC_NODATA: | |
3677 | /* NSEC proves that there's no data here, very good. */ | |
202b76ae | 3678 | log_debug("Proved NODATA via NSEC/NSEC3 for transaction %u (%s)", t->id, key_str); |
019036a4 | 3679 | t->answer_dnssec_result = DNSSEC_VALIDATED; |
72667f08 | 3680 | t->answer_rcode = DNS_RCODE_SUCCESS; |
6f055e43 | 3681 | SET_FLAG(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED, authenticated); |
7aa8ce98 | 3682 | |
42df9532 | 3683 | manager_dnssec_verdict(t->scope->manager, authenticated ? DNSSEC_SECURE : DNSSEC_INSECURE, dns_transaction_key(t)); |
72667f08 LP |
3684 | break; |
3685 | ||
105e1512 LP |
3686 | case DNSSEC_NSEC_OPTOUT: |
3687 | /* NSEC3 says the data might not be signed */ | |
202b76ae | 3688 | log_debug("Data is NSEC3 opt-out via NSEC/NSEC3 for transaction %u (%s)", t->id, key_str); |
019036a4 | 3689 | t->answer_dnssec_result = DNSSEC_UNSIGNED; |
6f055e43 | 3690 | SET_FLAG(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED, false); |
7aa8ce98 | 3691 | |
42df9532 | 3692 | manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, dns_transaction_key(t)); |
105e1512 LP |
3693 | break; |
3694 | ||
72667f08 LP |
3695 | case DNSSEC_NSEC_NO_RR: |
3696 | /* No NSEC data? Bummer! */ | |
105e1512 LP |
3697 | |
3698 | r = dns_transaction_requires_nsec(t); | |
3699 | if (r < 0) | |
3700 | return r; | |
7aa8ce98 | 3701 | if (r > 0) { |
019036a4 | 3702 | t->answer_dnssec_result = DNSSEC_NO_SIGNATURE; |
42df9532 | 3703 | manager_dnssec_verdict(t->scope->manager, DNSSEC_BOGUS, dns_transaction_key(t)); |
7aa8ce98 | 3704 | } else { |
019036a4 | 3705 | t->answer_dnssec_result = DNSSEC_UNSIGNED; |
6f055e43 | 3706 | SET_FLAG(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED, false); |
42df9532 | 3707 | manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, dns_transaction_key(t)); |
105e1512 LP |
3708 | } |
3709 | ||
3710 | break; | |
3711 | ||
3712 | case DNSSEC_NSEC_UNSUPPORTED_ALGORITHM: | |
3713 | /* We don't know the NSEC3 algorithm used? */ | |
019036a4 | 3714 | t->answer_dnssec_result = DNSSEC_UNSUPPORTED_ALGORITHM; |
42df9532 | 3715 | manager_dnssec_verdict(t->scope->manager, DNSSEC_INDETERMINATE, dns_transaction_key(t)); |
72667f08 LP |
3716 | break; |
3717 | ||
3718 | case DNSSEC_NSEC_FOUND: | |
146035b3 | 3719 | case DNSSEC_NSEC_CNAME: |
72667f08 | 3720 | /* NSEC says it needs to be there, but we couldn't find it? Bummer! */ |
019036a4 | 3721 | t->answer_dnssec_result = DNSSEC_NSEC_MISMATCH; |
42df9532 | 3722 | manager_dnssec_verdict(t->scope->manager, DNSSEC_BOGUS, dns_transaction_key(t)); |
72667f08 LP |
3723 | break; |
3724 | ||
3725 | default: | |
04499a70 | 3726 | assert_not_reached(); |
72667f08 LP |
3727 | } |
3728 | } | |
3729 | ||
547973de LP |
3730 | return 1; |
3731 | } | |
3732 | ||
ec2c5e43 | 3733 | static const char* const dns_transaction_state_table[_DNS_TRANSACTION_STATE_MAX] = { |
e3e64a1a ZJS |
3734 | [DNS_TRANSACTION_NULL] = "null", |
3735 | [DNS_TRANSACTION_PENDING] = "pending", | |
3736 | [DNS_TRANSACTION_VALIDATING] = "validating", | |
3737 | [DNS_TRANSACTION_RCODE_FAILURE] = "rcode-failure", | |
3738 | [DNS_TRANSACTION_SUCCESS] = "success", | |
3739 | [DNS_TRANSACTION_NO_SERVERS] = "no-servers", | |
3740 | [DNS_TRANSACTION_TIMEOUT] = "timeout", | |
ec2c5e43 | 3741 | [DNS_TRANSACTION_ATTEMPTS_MAX_REACHED] = "attempts-max-reached", |
e3e64a1a ZJS |
3742 | [DNS_TRANSACTION_INVALID_REPLY] = "invalid-reply", |
3743 | [DNS_TRANSACTION_ERRNO] = "errno", | |
3744 | [DNS_TRANSACTION_ABORTED] = "aborted", | |
3745 | [DNS_TRANSACTION_DNSSEC_FAILED] = "dnssec-failed", | |
3746 | [DNS_TRANSACTION_NO_TRUST_ANCHOR] = "no-trust-anchor", | |
3747 | [DNS_TRANSACTION_RR_TYPE_UNSUPPORTED] = "rr-type-unsupported", | |
3748 | [DNS_TRANSACTION_NETWORK_DOWN] = "network-down", | |
3749 | [DNS_TRANSACTION_NOT_FOUND] = "not-found", | |
3750 | [DNS_TRANSACTION_NO_SOURCE] = "no-source", | |
3751 | [DNS_TRANSACTION_STUB_LOOP] = "stub-loop", | |
ec2c5e43 LP |
3752 | }; |
3753 | DEFINE_STRING_TABLE_LOOKUP(dns_transaction_state, DnsTransactionState); | |
c3bc53e6 LP |
3754 | |
3755 | static const char* const dns_transaction_source_table[_DNS_TRANSACTION_SOURCE_MAX] = { | |
e3e64a1a ZJS |
3756 | [DNS_TRANSACTION_NETWORK] = "network", |
3757 | [DNS_TRANSACTION_CACHE] = "cache", | |
3758 | [DNS_TRANSACTION_ZONE] = "zone", | |
0d2cd476 | 3759 | [DNS_TRANSACTION_TRUST_ANCHOR] = "trust-anchor", |
c3bc53e6 LP |
3760 | }; |
3761 | DEFINE_STRING_TABLE_LOOKUP(dns_transaction_source, DnsTransactionSource); |