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