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