]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
623a4c97 | 2 | |
b5efdb8a | 3 | #include "alloc-util.h" |
4ad7f276 | 4 | #include "dns-domain.h" |
07630cea | 5 | #include "list.h" |
623a4c97 | 6 | #include "resolved-dns-packet.h" |
07630cea | 7 | #include "resolved-dns-zone.h" |
c3036641 | 8 | #include "resolved-dnssd.h" |
be28f72d | 9 | #include "resolved-manager.h" |
07630cea | 10 | #include "string-util.h" |
623a4c97 LP |
11 | |
12 | /* Never allow more than 1K entries */ | |
13 | #define ZONE_MAX 1024 | |
14 | ||
3ef64445 | 15 | void dns_zone_item_probe_stop(DnsZoneItem *i) { |
ec2c5e43 LP |
16 | DnsTransaction *t; |
17 | assert(i); | |
623a4c97 | 18 | |
ec2c5e43 LP |
19 | if (!i->probe_transaction) |
20 | return; | |
21 | ||
ae2a15bc | 22 | t = TAKE_PTR(i->probe_transaction); |
ec2c5e43 | 23 | |
547973de | 24 | set_remove(t->notify_zone_items, i); |
35aa04e9 | 25 | set_remove(t->notify_zone_items_done, i); |
ec2c5e43 LP |
26 | dns_transaction_gc(t); |
27 | } | |
623a4c97 LP |
28 | |
29 | static void dns_zone_item_free(DnsZoneItem *i) { | |
30 | if (!i) | |
31 | return; | |
32 | ||
ec2c5e43 | 33 | dns_zone_item_probe_stop(i); |
623a4c97 | 34 | dns_resource_record_unref(i->rr); |
ec2c5e43 | 35 | |
623a4c97 LP |
36 | free(i); |
37 | } | |
38 | ||
39 | DEFINE_TRIVIAL_CLEANUP_FUNC(DnsZoneItem*, dns_zone_item_free); | |
40 | ||
41 | static void dns_zone_item_remove_and_free(DnsZone *z, DnsZoneItem *i) { | |
42 | DnsZoneItem *first; | |
43 | ||
44 | assert(z); | |
45 | ||
46 | if (!i) | |
47 | return; | |
48 | ||
49 | first = hashmap_get(z->by_key, i->rr->key); | |
50 | LIST_REMOVE(by_key, first, i); | |
51 | if (first) | |
52 | assert_se(hashmap_replace(z->by_key, first->rr->key, first) >= 0); | |
53 | else | |
54 | hashmap_remove(z->by_key, i->rr->key); | |
55 | ||
1c02e7ba | 56 | first = hashmap_get(z->by_name, dns_resource_key_name(i->rr->key)); |
623a4c97 LP |
57 | LIST_REMOVE(by_name, first, i); |
58 | if (first) | |
1c02e7ba | 59 | assert_se(hashmap_replace(z->by_name, dns_resource_key_name(first->rr->key), first) >= 0); |
623a4c97 | 60 | else |
1c02e7ba | 61 | hashmap_remove(z->by_name, dns_resource_key_name(i->rr->key)); |
623a4c97 LP |
62 | |
63 | dns_zone_item_free(i); | |
64 | } | |
65 | ||
66 | void dns_zone_flush(DnsZone *z) { | |
67 | DnsZoneItem *i; | |
68 | ||
69 | assert(z); | |
70 | ||
71 | while ((i = hashmap_first(z->by_key))) | |
72 | dns_zone_item_remove_and_free(z, i); | |
73 | ||
74 | assert(hashmap_size(z->by_key) == 0); | |
75 | assert(hashmap_size(z->by_name) == 0); | |
76 | ||
525d3cc7 LP |
77 | z->by_key = hashmap_free(z->by_key); |
78 | z->by_name = hashmap_free(z->by_name); | |
623a4c97 LP |
79 | } |
80 | ||
8d67e72c | 81 | DnsZoneItem* dns_zone_get(DnsZone *z, DnsResourceRecord *rr) { |
623a4c97 LP |
82 | DnsZoneItem *i; |
83 | ||
84 | assert(z); | |
85 | assert(rr); | |
86 | ||
87 | LIST_FOREACH(by_key, i, hashmap_get(z->by_key, rr->key)) | |
3ef77d04 | 88 | if (dns_resource_record_equal(i->rr, rr) > 0) |
623a4c97 LP |
89 | return i; |
90 | ||
91 | return NULL; | |
92 | } | |
93 | ||
94 | void dns_zone_remove_rr(DnsZone *z, DnsResourceRecord *rr) { | |
95 | DnsZoneItem *i; | |
96 | ||
97 | assert(z); | |
98 | assert(rr); | |
99 | ||
100 | i = dns_zone_get(z, rr); | |
101 | if (i) | |
102 | dns_zone_item_remove_and_free(z, i); | |
103 | } | |
104 | ||
6db6a464 DR |
105 | int dns_zone_remove_rrs_by_key(DnsZone *z, DnsResourceKey *key) { |
106 | _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL, *soa = NULL; | |
107 | DnsResourceRecord *rr; | |
108 | bool tentative; | |
109 | int r; | |
110 | ||
111 | r = dns_zone_lookup(z, key, 0, &answer, &soa, &tentative); | |
112 | if (r < 0) | |
113 | return r; | |
114 | ||
115 | DNS_ANSWER_FOREACH(rr, answer) | |
116 | dns_zone_remove_rr(z, rr); | |
117 | ||
118 | return 0; | |
119 | } | |
120 | ||
623a4c97 LP |
121 | static int dns_zone_init(DnsZone *z) { |
122 | int r; | |
123 | ||
124 | assert(z); | |
125 | ||
d5099efc | 126 | r = hashmap_ensure_allocated(&z->by_key, &dns_resource_key_hash_ops); |
623a4c97 LP |
127 | if (r < 0) |
128 | return r; | |
129 | ||
d5099efc | 130 | r = hashmap_ensure_allocated(&z->by_name, &dns_name_hash_ops); |
623a4c97 LP |
131 | if (r < 0) |
132 | return r; | |
133 | ||
134 | return 0; | |
135 | } | |
136 | ||
137 | static int dns_zone_link_item(DnsZone *z, DnsZoneItem *i) { | |
138 | DnsZoneItem *first; | |
139 | int r; | |
140 | ||
141 | first = hashmap_get(z->by_key, i->rr->key); | |
142 | if (first) { | |
143 | LIST_PREPEND(by_key, first, i); | |
144 | assert_se(hashmap_replace(z->by_key, first->rr->key, first) >= 0); | |
145 | } else { | |
146 | r = hashmap_put(z->by_key, i->rr->key, i); | |
147 | if (r < 0) | |
148 | return r; | |
149 | } | |
150 | ||
1c02e7ba | 151 | first = hashmap_get(z->by_name, dns_resource_key_name(i->rr->key)); |
623a4c97 LP |
152 | if (first) { |
153 | LIST_PREPEND(by_name, first, i); | |
1c02e7ba | 154 | assert_se(hashmap_replace(z->by_name, dns_resource_key_name(first->rr->key), first) >= 0); |
623a4c97 | 155 | } else { |
1c02e7ba | 156 | r = hashmap_put(z->by_name, dns_resource_key_name(i->rr->key), i); |
623a4c97 LP |
157 | if (r < 0) |
158 | return r; | |
159 | } | |
160 | ||
161 | return 0; | |
162 | } | |
163 | ||
ec2c5e43 | 164 | static int dns_zone_item_probe_start(DnsZoneItem *i) { |
29bd6012 | 165 | _cleanup_(dns_transaction_gcp) DnsTransaction *t = NULL; |
ec2c5e43 LP |
166 | int r; |
167 | ||
168 | assert(i); | |
169 | ||
170 | if (i->probe_transaction) | |
171 | return 0; | |
172 | ||
1c02e7ba | 173 | t = dns_scope_find_transaction(i->scope, &DNS_RESOURCE_KEY_CONST(i->rr->key->class, DNS_TYPE_ANY, dns_resource_key_name(i->rr->key)), false); |
ec2c5e43 | 174 | if (!t) { |
1b4f6e79 LP |
175 | _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL; |
176 | ||
1c02e7ba | 177 | key = dns_resource_key_new(i->rr->key->class, DNS_TYPE_ANY, dns_resource_key_name(i->rr->key)); |
1b4f6e79 LP |
178 | if (!key) |
179 | return -ENOMEM; | |
180 | ||
f52e61da | 181 | r = dns_transaction_new(&t, i->scope, key); |
ec2c5e43 LP |
182 | if (r < 0) |
183 | return r; | |
184 | } | |
185 | ||
35aa04e9 LP |
186 | r = set_ensure_allocated(&t->notify_zone_items_done, NULL); |
187 | if (r < 0) | |
29bd6012 | 188 | return r; |
35aa04e9 | 189 | |
de7fef4b | 190 | r = set_ensure_put(&t->notify_zone_items, NULL, i); |
ec2c5e43 | 191 | if (r < 0) |
29bd6012 | 192 | return r; |
ec2c5e43 | 193 | |
53fda2bb | 194 | t->probing = true; |
29bd6012 | 195 | i->probe_transaction = TAKE_PTR(t); |
ec2c5e43 | 196 | |
29bd6012 | 197 | if (i->probe_transaction->state == DNS_TRANSACTION_NULL) { |
ec2c5e43 | 198 | i->block_ready++; |
29bd6012 | 199 | r = dns_transaction_go(i->probe_transaction); |
ec2c5e43 LP |
200 | i->block_ready--; |
201 | ||
202 | if (r < 0) { | |
203 | dns_zone_item_probe_stop(i); | |
204 | return r; | |
205 | } | |
206 | } | |
207 | ||
547973de | 208 | dns_zone_item_notify(i); |
ec2c5e43 | 209 | return 0; |
ec2c5e43 LP |
210 | } |
211 | ||
212 | int dns_zone_put(DnsZone *z, DnsScope *s, DnsResourceRecord *rr, bool probe) { | |
623a4c97 LP |
213 | _cleanup_(dns_zone_item_freep) DnsZoneItem *i = NULL; |
214 | DnsZoneItem *existing; | |
215 | int r; | |
216 | ||
217 | assert(z); | |
ec2c5e43 | 218 | assert(s); |
623a4c97 LP |
219 | assert(rr); |
220 | ||
222148b6 | 221 | if (dns_class_is_pseudo(rr->key->class)) |
1d3b690f | 222 | return -EINVAL; |
222148b6 | 223 | if (dns_type_is_pseudo(rr->key->type)) |
1d3b690f LP |
224 | return -EINVAL; |
225 | ||
623a4c97 LP |
226 | existing = dns_zone_get(z, rr); |
227 | if (existing) | |
228 | return 0; | |
229 | ||
230 | r = dns_zone_init(z); | |
231 | if (r < 0) | |
232 | return r; | |
233 | ||
234 | i = new0(DnsZoneItem, 1); | |
235 | if (!i) | |
236 | return -ENOMEM; | |
237 | ||
ec2c5e43 | 238 | i->scope = s; |
623a4c97 | 239 | i->rr = dns_resource_record_ref(rr); |
ec2c5e43 | 240 | i->probing_enabled = probe; |
623a4c97 LP |
241 | |
242 | r = dns_zone_link_item(z, i); | |
243 | if (r < 0) | |
244 | return r; | |
245 | ||
ec2c5e43 | 246 | if (probe) { |
cd1b20f9 LP |
247 | DnsZoneItem *first, *j; |
248 | bool established = false; | |
249 | ||
250 | /* Check if there's already an RR with the same name | |
251 | * established. If so, it has been probed already, and | |
5238e957 | 252 | * we don't need to probe again. */ |
cd1b20f9 LP |
253 | |
254 | LIST_FIND_HEAD(by_name, i, first); | |
255 | LIST_FOREACH(by_name, j, first) { | |
256 | if (i == j) | |
257 | continue; | |
258 | ||
259 | if (j->state == DNS_ZONE_ITEM_ESTABLISHED) | |
260 | established = true; | |
ec2c5e43 LP |
261 | } |
262 | ||
cd1b20f9 LP |
263 | if (established) |
264 | i->state = DNS_ZONE_ITEM_ESTABLISHED; | |
265 | else { | |
60eb3f7c LP |
266 | i->state = DNS_ZONE_ITEM_PROBING; |
267 | ||
cd1b20f9 LP |
268 | r = dns_zone_item_probe_start(i); |
269 | if (r < 0) { | |
270 | dns_zone_item_remove_and_free(z, i); | |
271 | i = NULL; | |
272 | return r; | |
273 | } | |
cd1b20f9 | 274 | } |
ec2c5e43 LP |
275 | } else |
276 | i->state = DNS_ZONE_ITEM_ESTABLISHED; | |
277 | ||
623a4c97 LP |
278 | i = NULL; |
279 | return 0; | |
280 | } | |
281 | ||
748a548e DR |
282 | static int dns_zone_add_authenticated_answer(DnsAnswer *a, DnsZoneItem *i, int ifindex) { |
283 | DnsAnswerFlags flags; | |
284 | ||
285 | /* From RFC 6762, Section 10.2 | |
286 | * "They (the rules about when to set the cache-flush bit) apply to | |
287 | * startup announcements as described in Section 8.3, "Announcing", | |
288 | * and to responses generated as a result of receiving query messages." | |
289 | * So, set the cache-flush bit for mDNS answers except for DNS-SD | |
290 | * service enumeration PTRs described in RFC 6763, Section 4.1. */ | |
291 | if (i->scope->protocol == DNS_PROTOCOL_MDNS && | |
292 | !dns_resource_key_is_dnssd_ptr(i->rr->key)) | |
293 | flags = DNS_ANSWER_AUTHENTICATED|DNS_ANSWER_CACHE_FLUSH; | |
294 | else | |
295 | flags = DNS_ANSWER_AUTHENTICATED; | |
296 | ||
297 | return dns_answer_add(a, i->rr, ifindex, flags); | |
298 | } | |
299 | ||
97ebebbc | 300 | int dns_zone_lookup(DnsZone *z, DnsResourceKey *key, int ifindex, DnsAnswer **ret_answer, DnsAnswer **ret_soa, bool *ret_tentative) { |
8bf52d3d | 301 | _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL, *soa = NULL; |
5032b16d LP |
302 | unsigned n_answer = 0; |
303 | DnsZoneItem *j, *first; | |
304 | bool tentative = true, need_soa = false; | |
d5323661 | 305 | int r; |
623a4c97 | 306 | |
97ebebbc LP |
307 | /* Note that we don't actually need the ifindex for anything. However when it is passed we'll initialize the |
308 | * ifindex field in the answer with it */ | |
309 | ||
623a4c97 | 310 | assert(z); |
5032b16d | 311 | assert(key); |
8bf52d3d | 312 | assert(ret_answer); |
623a4c97 | 313 | |
5032b16d | 314 | /* First iteration, count what we have */ |
ec2c5e43 | 315 | |
5032b16d LP |
316 | if (key->type == DNS_TYPE_ANY || key->class == DNS_CLASS_ANY) { |
317 | bool found = false, added = false; | |
318 | int k; | |
623a4c97 | 319 | |
5032b16d LP |
320 | /* If this is a generic match, then we have to |
321 | * go through the list by the name and look | |
322 | * for everything manually */ | |
623a4c97 | 323 | |
1c02e7ba | 324 | first = hashmap_get(z->by_name, dns_resource_key_name(key)); |
5032b16d LP |
325 | LIST_FOREACH(by_name, j, first) { |
326 | if (!IN_SET(j->state, DNS_ZONE_ITEM_PROBING, DNS_ZONE_ITEM_ESTABLISHED, DNS_ZONE_ITEM_VERIFYING)) | |
327 | continue; | |
d5323661 | 328 | |
5032b16d | 329 | found = true; |
d5323661 | 330 | |
801ad6a6 | 331 | k = dns_resource_key_match_rr(key, j->rr, NULL); |
5032b16d LP |
332 | if (k < 0) |
333 | return k; | |
334 | if (k > 0) { | |
335 | n_answer++; | |
336 | added = true; | |
337 | } | |
ec2c5e43 | 338 | |
5032b16d | 339 | } |
ec2c5e43 | 340 | |
5032b16d LP |
341 | if (found && !added) |
342 | need_soa = true; | |
ec2c5e43 | 343 | |
5032b16d LP |
344 | } else { |
345 | bool found = false; | |
d5323661 | 346 | |
5032b16d LP |
347 | /* If this is a specific match, then look for |
348 | * the right key immediately */ | |
ec2c5e43 | 349 | |
5032b16d LP |
350 | first = hashmap_get(z->by_key, key); |
351 | LIST_FOREACH(by_key, j, first) { | |
352 | if (!IN_SET(j->state, DNS_ZONE_ITEM_PROBING, DNS_ZONE_ITEM_ESTABLISHED, DNS_ZONE_ITEM_VERIFYING)) | |
353 | continue; | |
ec2c5e43 | 354 | |
5032b16d LP |
355 | found = true; |
356 | n_answer++; | |
357 | } | |
ec2c5e43 | 358 | |
5032b16d | 359 | if (!found) { |
1c02e7ba | 360 | first = hashmap_get(z->by_name, dns_resource_key_name(key)); |
5032b16d | 361 | LIST_FOREACH(by_name, j, first) { |
ec2c5e43 LP |
362 | if (!IN_SET(j->state, DNS_ZONE_ITEM_PROBING, DNS_ZONE_ITEM_ESTABLISHED, DNS_ZONE_ITEM_VERIFYING)) |
363 | continue; | |
364 | ||
5032b16d LP |
365 | need_soa = true; |
366 | break; | |
d5323661 | 367 | } |
d5323661 | 368 | } |
623a4c97 LP |
369 | } |
370 | ||
5032b16d LP |
371 | if (n_answer <= 0 && !need_soa) |
372 | goto return_empty; | |
623a4c97 | 373 | |
8bf52d3d LP |
374 | if (n_answer > 0) { |
375 | answer = dns_answer_new(n_answer); | |
376 | if (!answer) | |
377 | return -ENOMEM; | |
378 | } | |
623a4c97 | 379 | |
5032b16d LP |
380 | if (need_soa) { |
381 | soa = dns_answer_new(1); | |
8bf52d3d LP |
382 | if (!soa) |
383 | return -ENOMEM; | |
384 | } | |
385 | ||
386 | /* Second iteration, actually add the RRs to the answers */ | |
5032b16d LP |
387 | if (key->type == DNS_TYPE_ANY || key->class == DNS_CLASS_ANY) { |
388 | bool found = false, added = false; | |
389 | int k; | |
d5323661 | 390 | |
1c02e7ba | 391 | first = hashmap_get(z->by_name, dns_resource_key_name(key)); |
5032b16d LP |
392 | LIST_FOREACH(by_name, j, first) { |
393 | if (!IN_SET(j->state, DNS_ZONE_ITEM_PROBING, DNS_ZONE_ITEM_ESTABLISHED, DNS_ZONE_ITEM_VERIFYING)) | |
394 | continue; | |
ec2c5e43 | 395 | |
5032b16d | 396 | found = true; |
ec2c5e43 | 397 | |
5032b16d LP |
398 | if (j->state != DNS_ZONE_ITEM_PROBING) |
399 | tentative = false; | |
ec2c5e43 | 400 | |
801ad6a6 | 401 | k = dns_resource_key_match_rr(key, j->rr, NULL); |
5032b16d LP |
402 | if (k < 0) |
403 | return k; | |
404 | if (k > 0) { | |
748a548e | 405 | r = dns_zone_add_authenticated_answer(answer, j, ifindex); |
d5323661 LP |
406 | if (r < 0) |
407 | return r; | |
5032b16d LP |
408 | |
409 | added = true; | |
d5323661 | 410 | } |
5032b16d | 411 | } |
d5323661 | 412 | |
5032b16d | 413 | if (found && !added) { |
97ebebbc | 414 | r = dns_answer_add_soa(soa, dns_resource_key_name(key), LLMNR_DEFAULT_TTL, ifindex); |
5032b16d LP |
415 | if (r < 0) |
416 | return r; | |
417 | } | |
418 | } else { | |
419 | bool found = false; | |
ec2c5e43 | 420 | |
5032b16d LP |
421 | first = hashmap_get(z->by_key, key); |
422 | LIST_FOREACH(by_key, j, first) { | |
423 | if (!IN_SET(j->state, DNS_ZONE_ITEM_PROBING, DNS_ZONE_ITEM_ESTABLISHED, DNS_ZONE_ITEM_VERIFYING)) | |
424 | continue; | |
ec2c5e43 | 425 | |
5032b16d | 426 | found = true; |
ec2c5e43 | 427 | |
5032b16d LP |
428 | if (j->state != DNS_ZONE_ITEM_PROBING) |
429 | tentative = false; | |
ec2c5e43 | 430 | |
748a548e | 431 | r = dns_zone_add_authenticated_answer(answer, j, ifindex); |
5032b16d LP |
432 | if (r < 0) |
433 | return r; | |
434 | } | |
ec2c5e43 | 435 | |
5032b16d LP |
436 | if (!found) { |
437 | bool add_soa = false; | |
ec2c5e43 | 438 | |
1c02e7ba | 439 | first = hashmap_get(z->by_name, dns_resource_key_name(key)); |
5032b16d LP |
440 | LIST_FOREACH(by_name, j, first) { |
441 | if (!IN_SET(j->state, DNS_ZONE_ITEM_PROBING, DNS_ZONE_ITEM_ESTABLISHED, DNS_ZONE_ITEM_VERIFYING)) | |
442 | continue; | |
ec2c5e43 | 443 | |
5032b16d LP |
444 | if (j->state != DNS_ZONE_ITEM_PROBING) |
445 | tentative = false; | |
ec2c5e43 | 446 | |
5032b16d LP |
447 | add_soa = true; |
448 | } | |
449 | ||
450 | if (add_soa) { | |
97ebebbc | 451 | r = dns_answer_add_soa(soa, dns_resource_key_name(key), LLMNR_DEFAULT_TTL, ifindex); |
5032b16d LP |
452 | if (r < 0) |
453 | return r; | |
d5323661 | 454 | } |
623a4c97 LP |
455 | } |
456 | } | |
457 | ||
5032b16d LP |
458 | /* If the caller sets ret_tentative to NULL, then use this as |
459 | * indication to not return tentative entries */ | |
460 | ||
461 | if (!ret_tentative && tentative) | |
462 | goto return_empty; | |
463 | ||
1cc6c93a | 464 | *ret_answer = TAKE_PTR(answer); |
623a4c97 | 465 | |
1cc6c93a YW |
466 | if (ret_soa) |
467 | *ret_soa = TAKE_PTR(soa); | |
8bf52d3d | 468 | |
ec2c5e43 LP |
469 | if (ret_tentative) |
470 | *ret_tentative = tentative; | |
471 | ||
623a4c97 | 472 | return 1; |
5032b16d LP |
473 | |
474 | return_empty: | |
475 | *ret_answer = NULL; | |
476 | ||
477 | if (ret_soa) | |
478 | *ret_soa = NULL; | |
479 | ||
480 | if (ret_tentative) | |
481 | *ret_tentative = false; | |
482 | ||
483 | return 0; | |
623a4c97 | 484 | } |
ec2c5e43 LP |
485 | |
486 | void dns_zone_item_conflict(DnsZoneItem *i) { | |
ec2c5e43 LP |
487 | assert(i); |
488 | ||
a4076574 LP |
489 | if (!IN_SET(i->state, DNS_ZONE_ITEM_PROBING, DNS_ZONE_ITEM_VERIFYING, DNS_ZONE_ITEM_ESTABLISHED)) |
490 | return; | |
491 | ||
7b50eb2e | 492 | log_info("Detected conflict on %s", strna(dns_resource_record_to_string(i->rr))); |
ec2c5e43 | 493 | |
d84b686f LP |
494 | dns_zone_item_probe_stop(i); |
495 | ||
ec2c5e43 LP |
496 | /* Withdraw the conflict item */ |
497 | i->state = DNS_ZONE_ITEM_WITHDRAWN; | |
498 | ||
c3036641 DR |
499 | dnssd_signal_conflict(i->scope->manager, dns_resource_key_name(i->rr->key)); |
500 | ||
ec2c5e43 | 501 | /* Maybe change the hostname */ |
1c02e7ba | 502 | if (manager_is_own_hostname(i->scope->manager, dns_resource_key_name(i->rr->key)) > 0) |
ec2c5e43 LP |
503 | manager_next_hostname(i->scope->manager); |
504 | } | |
505 | ||
547973de | 506 | void dns_zone_item_notify(DnsZoneItem *i) { |
ec2c5e43 LP |
507 | assert(i); |
508 | assert(i->probe_transaction); | |
509 | ||
510 | if (i->block_ready > 0) | |
511 | return; | |
512 | ||
547973de | 513 | if (IN_SET(i->probe_transaction->state, DNS_TRANSACTION_NULL, DNS_TRANSACTION_PENDING, DNS_TRANSACTION_VALIDATING)) |
ec2c5e43 LP |
514 | return; |
515 | ||
a4076574 LP |
516 | if (i->probe_transaction->state == DNS_TRANSACTION_SUCCESS) { |
517 | bool we_lost = false; | |
ec2c5e43 | 518 | |
a4076574 | 519 | /* The probe got a successful reply. If we so far |
008d4ab7 DR |
520 | * weren't established we just give up. |
521 | * | |
522 | * In LLMNR case if we already | |
a4076574 | 523 | * were established, and the peer has the |
4d91eec4 | 524 | * lexicographically larger IP address we continue |
a4076574 LP |
525 | * and defend it. */ |
526 | ||
2fb3034c LP |
527 | if (!IN_SET(i->state, DNS_ZONE_ITEM_ESTABLISHED, DNS_ZONE_ITEM_VERIFYING)) { |
528 | log_debug("Got a successful probe for not yet established RR, we lost."); | |
a4076574 | 529 | we_lost = true; |
008d4ab7 | 530 | } else if (i->probe_transaction->scope->protocol == DNS_PROTOCOL_LLMNR) { |
a4076574 | 531 | assert(i->probe_transaction->received); |
4d91eec4 | 532 | we_lost = memcmp(&i->probe_transaction->received->sender, &i->probe_transaction->received->destination, FAMILY_ADDRESS_SIZE(i->probe_transaction->received->family)) < 0; |
2fb3034c | 533 | if (we_lost) |
4d91eec4 | 534 | log_debug("Got a successful probe reply for an established RR, and we have a lexicographically larger IP address and thus lost."); |
a4076574 LP |
535 | } |
536 | ||
537 | if (we_lost) { | |
538 | dns_zone_item_conflict(i); | |
539 | return; | |
540 | } | |
541 | ||
542 | log_debug("Got a successful probe reply, but peer has lexicographically lower IP address and thus lost."); | |
543 | } | |
544 | ||
7b50eb2e | 545 | log_debug("Record %s successfully probed.", strna(dns_resource_record_to_string(i->rr))); |
ec2c5e43 | 546 | |
a4076574 LP |
547 | dns_zone_item_probe_stop(i); |
548 | i->state = DNS_ZONE_ITEM_ESTABLISHED; | |
549 | } | |
550 | ||
551 | static int dns_zone_item_verify(DnsZoneItem *i) { | |
552 | int r; | |
553 | ||
554 | assert(i); | |
555 | ||
556 | if (i->state != DNS_ZONE_ITEM_ESTABLISHED) | |
557 | return 0; | |
558 | ||
7b50eb2e | 559 | log_debug("Verifying RR %s", strna(dns_resource_record_to_string(i->rr))); |
2fb3034c | 560 | |
a4076574 LP |
561 | i->state = DNS_ZONE_ITEM_VERIFYING; |
562 | r = dns_zone_item_probe_start(i); | |
563 | if (r < 0) { | |
da927ba9 | 564 | log_error_errno(r, "Failed to start probing for verifying RR: %m"); |
ec2c5e43 | 565 | i->state = DNS_ZONE_ITEM_ESTABLISHED; |
a4076574 LP |
566 | return r; |
567 | } | |
568 | ||
569 | return 0; | |
570 | } | |
571 | ||
572 | int dns_zone_check_conflicts(DnsZone *zone, DnsResourceRecord *rr) { | |
573 | DnsZoneItem *i, *first; | |
bf1594f5 | 574 | int c = 0; |
a4076574 LP |
575 | |
576 | assert(zone); | |
577 | assert(rr); | |
578 | ||
579 | /* This checks whether a response RR we received from somebody | |
580 | * else is one that we actually thought was uniquely ours. If | |
581 | * so, we'll verify our RRs. */ | |
582 | ||
583 | /* No conflict if we don't have the name at all. */ | |
1c02e7ba | 584 | first = hashmap_get(zone->by_name, dns_resource_key_name(rr->key)); |
a4076574 LP |
585 | if (!first) |
586 | return 0; | |
587 | ||
588 | /* No conflict if we have the exact same RR */ | |
589 | if (dns_zone_get(zone, rr)) | |
590 | return 0; | |
591 | ||
d686f15c DR |
592 | /* No conflict if it is DNS-SD RR used for service enumeration. */ |
593 | if (dns_resource_key_is_dnssd_ptr(rr->key)) | |
594 | return 0; | |
595 | ||
a4076574 LP |
596 | /* OK, somebody else has RRs for the same name. Yuck! Let's |
597 | * start probing again */ | |
598 | ||
599 | LIST_FOREACH(by_name, i, first) { | |
600 | if (dns_resource_record_equal(i->rr, rr)) | |
601 | continue; | |
602 | ||
603 | dns_zone_item_verify(i); | |
604 | c++; | |
605 | } | |
606 | ||
607 | return c; | |
608 | } | |
609 | ||
610 | int dns_zone_verify_conflicts(DnsZone *zone, DnsResourceKey *key) { | |
611 | DnsZoneItem *i, *first; | |
bf1594f5 | 612 | int c = 0; |
a4076574 LP |
613 | |
614 | assert(zone); | |
615 | ||
616 | /* Somebody else notified us about a possible conflict. Let's | |
617 | * verify if that's true. */ | |
618 | ||
1c02e7ba | 619 | first = hashmap_get(zone->by_name, dns_resource_key_name(key)); |
a4076574 LP |
620 | if (!first) |
621 | return 0; | |
622 | ||
623 | LIST_FOREACH(by_name, i, first) { | |
624 | dns_zone_item_verify(i); | |
625 | c++; | |
626 | } | |
627 | ||
628 | return c; | |
ec2c5e43 | 629 | } |
902bb5d8 LP |
630 | |
631 | void dns_zone_verify_all(DnsZone *zone) { | |
632 | DnsZoneItem *i; | |
633 | Iterator iterator; | |
634 | ||
635 | assert(zone); | |
636 | ||
637 | HASHMAP_FOREACH(i, zone->by_key, iterator) { | |
638 | DnsZoneItem *j; | |
639 | ||
640 | LIST_FOREACH(by_key, j, i) | |
641 | dns_zone_item_verify(j); | |
642 | } | |
643 | } | |
4d506d6b LP |
644 | |
645 | void dns_zone_dump(DnsZone *zone, FILE *f) { | |
646 | Iterator iterator; | |
647 | DnsZoneItem *i; | |
4d506d6b LP |
648 | |
649 | if (!zone) | |
650 | return; | |
651 | ||
652 | if (!f) | |
653 | f = stdout; | |
654 | ||
655 | HASHMAP_FOREACH(i, zone->by_key, iterator) { | |
656 | DnsZoneItem *j; | |
657 | ||
658 | LIST_FOREACH(by_key, j, i) { | |
7b50eb2e | 659 | const char *t; |
4d506d6b | 660 | |
7b50eb2e LP |
661 | t = dns_resource_record_to_string(j->rr); |
662 | if (!t) { | |
4d506d6b LP |
663 | log_oom(); |
664 | continue; | |
665 | } | |
666 | ||
667 | fputc('\t', f); | |
668 | fputs(t, f); | |
669 | fputc('\n', f); | |
670 | } | |
671 | } | |
672 | } | |
673 | ||
674 | bool dns_zone_is_empty(DnsZone *zone) { | |
675 | if (!zone) | |
676 | return true; | |
677 | ||
678 | return hashmap_isempty(zone->by_key); | |
679 | } | |
a2bf8a19 DR |
680 | |
681 | bool dns_zone_contains_name(DnsZone *z, const char *name) { | |
682 | DnsZoneItem *i, *first; | |
683 | ||
684 | first = hashmap_get(z->by_name, name); | |
685 | if (!first) | |
686 | return false; | |
687 | ||
688 | LIST_FOREACH(by_name, i, first) { | |
689 | if (!IN_SET(i->state, DNS_ZONE_ITEM_PROBING, DNS_ZONE_ITEM_ESTABLISHED, DNS_ZONE_ITEM_VERIFYING)) | |
690 | continue; | |
691 | ||
692 | return true; | |
693 | } | |
694 | ||
695 | return false; | |
696 | } |