Merge pull request #14102 from ssahani/acd-duplicate-ip
[thirdparty/systemd.git] / src / resolve / resolved-manager.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
091a364c 2
ca78ad1d 3#include <fcntl.h>
07630cea
LP
4#include <netinet/in.h>
5#include <poll.h>
74b2466e 6#include <sys/ioctl.h>
ca78ad1d
ZJS
7#include <sys/stat.h>
8#include <sys/types.h>
9#include <unistd.h>
091a364c 10
349cc4a5 11#if HAVE_LIBIDN2
87057e24
ZJS
12#include <idn2.h>
13#endif
14
a2a416f7 15#include "af-list.h"
b5efdb8a 16#include "alloc-util.h"
2400ae29 17#include "bus-util.h"
943ef07c 18#include "dirent-util.h"
07630cea 19#include "dns-domain.h"
3ffd4af2 20#include "fd-util.h"
ee228be1 21#include "fileio.h"
07630cea 22#include "hostname-util.h"
c004493c 23#include "io-util.h"
ef118d00 24#include "missing_network.h"
07630cea
LP
25#include "netlink-util.h"
26#include "network-internal.h"
822db23c 27#include "ordered-set.h"
6bedfcbb 28#include "parse-util.h"
3df3e884 29#include "random-util.h"
39d8db04 30#include "resolved-bus.h"
07630cea 31#include "resolved-conf.h"
b30bf55d 32#include "resolved-dns-stub.h"
cb310866 33#include "resolved-dnssd.h"
dd0bc0f1 34#include "resolved-etc-hosts.h"
5f402ae8 35#include "resolved-llmnr.h"
07630cea 36#include "resolved-manager.h"
bc7702b0 37#include "resolved-mdns.h"
dd0bc0f1 38#include "resolved-resolv-conf.h"
07630cea 39#include "socket-util.h"
8b43440b 40#include "string-table.h"
07630cea
LP
41#include "string-util.h"
42#include "utf8.h"
74b2466e
LP
43
44#define SEND_TIMEOUT_USEC (200 * USEC_PER_MSEC)
45
1c4baffc 46static int manager_process_link(sd_netlink *rtnl, sd_netlink_message *mm, void *userdata) {
74b2466e
LP
47 Manager *m = userdata;
48 uint16_t type;
49 Link *l;
50 int ifindex, r;
51
52 assert(rtnl);
53 assert(m);
54 assert(mm);
55
1c4baffc 56 r = sd_netlink_message_get_type(mm, &type);
74b2466e
LP
57 if (r < 0)
58 goto fail;
59
60 r = sd_rtnl_message_link_get_ifindex(mm, &ifindex);
61 if (r < 0)
62 goto fail;
63
64 l = hashmap_get(m->links, INT_TO_PTR(ifindex));
65
66 switch (type) {
67
a2a416f7
LP
68 case RTM_NEWLINK:{
69 bool is_new = !l;
74b2466e 70
a2a416f7 71 if (!l) {
74b2466e
LP
72 r = link_new(m, &l, ifindex);
73 if (r < 0)
74 goto fail;
75 }
76
943ef07c 77 r = link_process_rtnl(l, mm);
74b2466e
LP
78 if (r < 0)
79 goto fail;
80
943ef07c 81 r = link_update(l);
21d73c87
LP
82 if (r < 0)
83 goto fail;
84
a2a416f7 85 if (is_new)
6ff79f76 86 log_debug("Found new link %i/%s", ifindex, l->ifname);
a2a416f7 87
74b2466e 88 break;
a2a416f7 89 }
74b2466e
LP
90
91 case RTM_DELLINK:
92 if (l) {
6ff79f76 93 log_debug("Removing link %i/%s", l->ifindex, l->ifname);
943ef07c 94 link_remove_user(l);
74b2466e
LP
95 link_free(l);
96 }
97
98 break;
99 }
100
101 return 0;
102
103fail:
da927ba9 104 log_warning_errno(r, "Failed to process RTNL link message: %m");
74b2466e
LP
105 return 0;
106}
107
1c4baffc 108static int manager_process_address(sd_netlink *rtnl, sd_netlink_message *mm, void *userdata) {
74b2466e
LP
109 Manager *m = userdata;
110 union in_addr_union address;
74b2466e 111 uint16_t type;
0dd25fb9 112 int r, ifindex, family;
74b2466e
LP
113 LinkAddress *a;
114 Link *l;
115
116 assert(rtnl);
117 assert(mm);
118 assert(m);
119
1c4baffc 120 r = sd_netlink_message_get_type(mm, &type);
74b2466e
LP
121 if (r < 0)
122 goto fail;
123
124 r = sd_rtnl_message_addr_get_ifindex(mm, &ifindex);
125 if (r < 0)
126 goto fail;
127
128 l = hashmap_get(m->links, INT_TO_PTR(ifindex));
129 if (!l)
130 return 0;
131
132 r = sd_rtnl_message_addr_get_family(mm, &family);
133 if (r < 0)
134 goto fail;
135
136 switch (family) {
137
138 case AF_INET:
1c4baffc 139 r = sd_netlink_message_read_in_addr(mm, IFA_LOCAL, &address.in);
74b2466e 140 if (r < 0) {
1c4baffc 141 r = sd_netlink_message_read_in_addr(mm, IFA_ADDRESS, &address.in);
74b2466e
LP
142 if (r < 0)
143 goto fail;
144 }
145
146 break;
147
148 case AF_INET6:
1c4baffc 149 r = sd_netlink_message_read_in6_addr(mm, IFA_LOCAL, &address.in6);
74b2466e 150 if (r < 0) {
1c4baffc 151 r = sd_netlink_message_read_in6_addr(mm, IFA_ADDRESS, &address.in6);
74b2466e
LP
152 if (r < 0)
153 goto fail;
154 }
155
156 break;
157
158 default:
159 return 0;
160 }
161
162 a = link_find_address(l, family, &address);
163
164 switch (type) {
165
166 case RTM_NEWADDR:
167
168 if (!a) {
169 r = link_address_new(l, &a, family, &address);
170 if (r < 0)
171 return r;
172 }
173
174 r = link_address_update_rtnl(a, mm);
175 if (r < 0)
176 return r;
177
178 break;
179
180 case RTM_DELADDR:
3e044c49 181 link_address_free(a);
74b2466e
LP
182 break;
183 }
184
185 return 0;
186
187fail:
da927ba9 188 log_warning_errno(r, "Failed to process RTNL address message: %m");
74b2466e
LP
189 return 0;
190}
191
74b2466e 192static int manager_rtnl_listen(Manager *m) {
4afd3348 193 _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
1c4baffc 194 sd_netlink_message *i;
74b2466e
LP
195 int r;
196
197 assert(m);
198
cc98b302 199 /* First, subscribe to interfaces coming and going */
1c4baffc 200 r = sd_netlink_open(&m->rtnl);
74b2466e
LP
201 if (r < 0)
202 return r;
203
2d895038 204 r = sd_netlink_attach_event(m->rtnl, m->event, SD_EVENT_PRIORITY_IMPORTANT);
74b2466e
LP
205 if (r < 0)
206 return r;
207
8190a388 208 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWLINK, manager_process_link, NULL, m, "resolve-NEWLINK");
74b2466e
LP
209 if (r < 0)
210 return r;
211
8190a388 212 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELLINK, manager_process_link, NULL, m, "resolve-DELLINK");
74b2466e
LP
213 if (r < 0)
214 return r;
215
8190a388 216 r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWADDR, manager_process_address, NULL, m, "resolve-NEWADDR");
74b2466e
LP
217 if (r < 0)
218 return r;
091a364c 219
8190a388 220 r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELADDR, manager_process_address, NULL, m, "resolve-DELADDR");
74b2466e
LP
221 if (r < 0)
222 return r;
223
224 /* Then, enumerate all links */
225 r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0);
226 if (r < 0)
227 return r;
228
1c4baffc 229 r = sd_netlink_message_request_dump(req, true);
74b2466e
LP
230 if (r < 0)
231 return r;
232
1c4baffc 233 r = sd_netlink_call(m->rtnl, req, 0, &reply);
74b2466e
LP
234 if (r < 0)
235 return r;
236
1c4baffc 237 for (i = reply; i; i = sd_netlink_message_next(i)) {
74b2466e
LP
238 r = manager_process_link(m->rtnl, i, m);
239 if (r < 0)
240 return r;
241 }
242
1c4baffc
TG
243 req = sd_netlink_message_unref(req);
244 reply = sd_netlink_message_unref(reply);
74b2466e
LP
245
246 /* Finally, enumerate all addresses, too */
247 r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, 0, AF_UNSPEC);
248 if (r < 0)
249 return r;
250
1c4baffc 251 r = sd_netlink_message_request_dump(req, true);
74b2466e
LP
252 if (r < 0)
253 return r;
254
1c4baffc 255 r = sd_netlink_call(m->rtnl, req, 0, &reply);
74b2466e
LP
256 if (r < 0)
257 return r;
258
1c4baffc 259 for (i = reply; i; i = sd_netlink_message_next(i)) {
74b2466e
LP
260 r = manager_process_address(m->rtnl, i, m);
261 if (r < 0)
262 return r;
263 }
264
265 return r;
266}
267
268static int on_network_event(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
269 Manager *m = userdata;
270 Iterator i;
271 Link *l;
272 int r;
273
274 assert(m);
275
276 sd_network_monitor_flush(m->network_monitor);
277
278 HASHMAP_FOREACH(l, m->links, i) {
943ef07c 279 r = link_update(l);
74b2466e 280 if (r < 0)
da927ba9 281 log_warning_errno(r, "Failed to update monitor information for %i: %m", l->ifindex);
74b2466e
LP
282 }
283
7207052d 284 (void) manager_write_resolv_conf(m);
5f3340ca 285 (void) manager_send_changed(m, "DNS");
74b2466e
LP
286
287 return 0;
288}
289
290static int manager_network_monitor_listen(Manager *m) {
291 int r, fd, events;
292
293 assert(m);
294
0014a4ad 295 r = sd_network_monitor_new(&m->network_monitor, NULL);
74b2466e
LP
296 if (r < 0)
297 return r;
298
299 fd = sd_network_monitor_get_fd(m->network_monitor);
300 if (fd < 0)
301 return fd;
302
303 events = sd_network_monitor_get_events(m->network_monitor);
304 if (events < 0)
305 return events;
306
307 r = sd_event_add_io(m->event, &m->network_event_source, fd, events, &on_network_event, m);
308 if (r < 0)
2d895038
LP
309 return r;
310
311 r = sd_event_source_set_priority(m->network_event_source, SD_EVENT_PRIORITY_IMPORTANT+5);
312 if (r < 0)
74b2466e
LP
313 return r;
314
aa4a9deb
LP
315 (void) sd_event_source_set_description(m->network_event_source, "network-monitor");
316
74b2466e
LP
317 return 0;
318}
319
e96de0ce 320static int determine_hostname(char **full_hostname, char **llmnr_hostname, char **mdns_hostname) {
eb60f9cd 321 _cleanup_free_ char *h = NULL, *n = NULL;
349cc4a5 322#if HAVE_LIBIDN2
87057e24 323 _cleanup_free_ char *utf8 = NULL;
349cc4a5 324#elif HAVE_LIBIDN
87057e24
ZJS
325 int k;
326#endif
78c6a153 327 char label[DNS_LABEL_MAX];
87057e24
ZJS
328 const char *p, *decoded;
329 int r;
eb60f9cd 330
e96de0ce 331 assert(full_hostname);
78c6a153
LP
332 assert(llmnr_hostname);
333 assert(mdns_hostname);
334
a25b0dc8 335 /* Extract and normalize the first label of the locally configured hostname, and check it's not "localhost". */
eb60f9cd 336
a25b0dc8
LP
337 r = gethostname_strict(&h);
338 if (r < 0)
339 return log_debug_errno(r, "Can't determine system hostname: %m");
eb60f9cd 340
78c6a153 341 p = h;
7470cc4c 342 r = dns_label_unescape(&p, label, sizeof label, 0);
78c6a153
LP
343 if (r < 0)
344 return log_error_errno(r, "Failed to unescape host name: %m");
baaa35ad
ZJS
345 if (r == 0)
346 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
347 "Couldn't find a single label in hostname.");
78c6a153 348
349cc4a5 349#if HAVE_LIBIDN2
87057e24
ZJS
350 r = idn2_to_unicode_8z8z(label, &utf8, 0);
351 if (r != IDN2_OK)
352 return log_error("Failed to undo IDNA: %s", idn2_strerror(r));
353 assert(utf8_is_valid(utf8));
354
355 r = strlen(utf8);
356 decoded = utf8;
349cc4a5 357#elif HAVE_LIBIDN
87057e24 358 k = dns_label_undo_idna(label, r, label, sizeof label);
78c6a153
LP
359 if (k < 0)
360 return log_error_errno(k, "Failed to undo IDNA: %m");
361 if (k > 0)
362 r = k;
363
baaa35ad
ZJS
364 if (!utf8_is_valid(label))
365 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
366 "System hostname is not UTF-8 clean.");
87057e24
ZJS
367 decoded = label;
368#else
369 decoded = label; /* no decoding */
370#endif
eb60f9cd 371
87057e24 372 r = dns_label_escape_new(decoded, r, &n);
78c6a153
LP
373 if (r < 0)
374 return log_error_errno(r, "Failed to escape host name: %m");
375
baaa35ad
ZJS
376 if (is_localhost(n))
377 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
378 "System hostname is 'localhost', ignoring.");
eb60f9cd 379
7470cc4c 380 r = dns_name_concat(n, "local", 0, mdns_hostname);
78c6a153
LP
381 if (r < 0)
382 return log_error_errno(r, "Failed to determine mDNS hostname: %m");
383
ae2a15bc
LP
384 *llmnr_hostname = TAKE_PTR(n);
385 *full_hostname = TAKE_PTR(h);
e96de0ce 386
eb60f9cd
LP
387 return 0;
388}
389
a25b0dc8
LP
390static const char *fallback_hostname(void) {
391
392 /* Determine the fall back hostname. For exposing this system to the outside world, we cannot have it to be
393 * "localhost" even if that's the compiled in hostname. In this case, let's revert to "linux" instead. */
394
395 if (is_localhost(FALLBACK_HOSTNAME))
396 return "linux";
397
398 return FALLBACK_HOSTNAME;
399}
400
401static int make_fallback_hostnames(char **full_hostname, char **llmnr_hostname, char **mdns_hostname) {
402 _cleanup_free_ char *n = NULL, *m = NULL;
403 char label[DNS_LABEL_MAX], *h;
404 const char *p;
405 int r;
406
407 assert(full_hostname);
408 assert(llmnr_hostname);
409 assert(mdns_hostname);
410
411 p = fallback_hostname();
7470cc4c 412 r = dns_label_unescape(&p, label, sizeof label, 0);
a25b0dc8
LP
413 if (r < 0)
414 return log_error_errno(r, "Failed to unescape fallback host name: %m");
415
416 assert(r > 0); /* The fallback hostname must have at least one label */
417
418 r = dns_label_escape_new(label, r, &n);
419 if (r < 0)
420 return log_error_errno(r, "Failed to escape fallback hostname: %m");
421
7470cc4c 422 r = dns_name_concat(n, "local", 0, &m);
a25b0dc8
LP
423 if (r < 0)
424 return log_error_errno(r, "Failed to concatenate mDNS hostname: %m");
425
426 h = strdup(fallback_hostname());
427 if (!h)
428 return log_oom();
429
ae2a15bc
LP
430 *llmnr_hostname = TAKE_PTR(n);
431 *mdns_hostname = TAKE_PTR(m);
a25b0dc8
LP
432
433 *full_hostname = h;
434
435 return 0;
436}
437
eb60f9cd 438static int on_hostname_change(sd_event_source *es, int fd, uint32_t revents, void *userdata) {
e96de0ce 439 _cleanup_free_ char *full_hostname = NULL, *llmnr_hostname = NULL, *mdns_hostname = NULL;
eb60f9cd 440 Manager *m = userdata;
86c0411e 441 bool llmnr_hostname_changed;
eb60f9cd
LP
442 int r;
443
444 assert(m);
445
e96de0ce 446 r = determine_hostname(&full_hostname, &llmnr_hostname, &mdns_hostname);
eb60f9cd
LP
447 if (r < 0)
448 return 0; /* ignore invalid hostnames */
449
86c0411e 450 llmnr_hostname_changed = !streq(llmnr_hostname, m->llmnr_hostname);
e96de0ce 451 if (streq(full_hostname, m->full_hostname) &&
86c0411e 452 !llmnr_hostname_changed &&
e96de0ce 453 streq(mdns_hostname, m->mdns_hostname))
eb60f9cd
LP
454 return 0;
455
e96de0ce 456 log_info("System hostname changed to '%s'.", full_hostname);
78c6a153 457
e96de0ce
LP
458 free_and_replace(m->full_hostname, full_hostname);
459 free_and_replace(m->llmnr_hostname, llmnr_hostname);
460 free_and_replace(m->mdns_hostname, mdns_hostname);
eb60f9cd
LP
461
462 manager_refresh_rrs(m);
86c0411e 463 (void) manager_send_changed(m, "LLMNRHostname");
eb60f9cd
LP
464
465 return 0;
466}
467
468static int manager_watch_hostname(Manager *m) {
eb60f9cd
LP
469 int r;
470
471 assert(m);
472
db4a47e9
LP
473 m->hostname_fd = open("/proc/sys/kernel/hostname",
474 O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
eb60f9cd 475 if (m->hostname_fd < 0) {
56f64d95 476 log_warning_errno(errno, "Failed to watch hostname: %m");
eb60f9cd
LP
477 return 0;
478 }
479
480 r = sd_event_add_io(m->event, &m->hostname_event_source, m->hostname_fd, 0, on_hostname_change, m);
481 if (r < 0) {
482 if (r == -EPERM)
483 /* kernels prior to 3.2 don't support polling this file. Ignore the failure. */
484 m->hostname_fd = safe_close(m->hostname_fd);
8d3d7072
MS
485 else
486 return log_error_errno(r, "Failed to add hostname event source: %m");
eb60f9cd
LP
487 }
488
aa4a9deb
LP
489 (void) sd_event_source_set_description(m->hostname_event_source, "hostname");
490
e96de0ce 491 r = determine_hostname(&m->full_hostname, &m->llmnr_hostname, &m->mdns_hostname);
eb60f9cd 492 if (r < 0) {
a25b0dc8 493 log_info("Defaulting to hostname '%s'.", fallback_hostname());
78c6a153 494
a25b0dc8
LP
495 r = make_fallback_hostnames(&m->full_hostname, &m->llmnr_hostname, &m->mdns_hostname);
496 if (r < 0)
497 return r;
eb60f9cd 498 } else
e96de0ce 499 log_info("Using system hostname '%s'.", m->full_hostname);
eb60f9cd
LP
500
501 return 0;
502}
503
4d506d6b
LP
504static int manager_sigusr1(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
505 _cleanup_free_ char *buffer = NULL;
506 _cleanup_fclose_ FILE *f = NULL;
507 Manager *m = userdata;
cf84484a 508 DnsServer *server;
4d506d6b
LP
509 size_t size = 0;
510 DnsScope *scope;
cf84484a
LP
511 Iterator i;
512 Link *l;
4d506d6b
LP
513
514 assert(s);
515 assert(si);
516 assert(m);
517
2fe21124 518 f = open_memstream_unlocked(&buffer, &size);
4d506d6b
LP
519 if (!f)
520 return log_oom();
521
522 LIST_FOREACH(scopes, scope, m->dns_scopes)
523 dns_scope_dump(scope, f);
524
cf84484a
LP
525 LIST_FOREACH(servers, server, m->dns_servers)
526 dns_server_dump(server, f);
527 LIST_FOREACH(servers, server, m->fallback_dns_servers)
528 dns_server_dump(server, f);
529 HASHMAP_FOREACH(l, m->links, i)
530 LIST_FOREACH(servers, server, l->dns_servers)
531 dns_server_dump(server, f);
532
4d506d6b
LP
533 if (fflush_and_check(f) < 0)
534 return log_oom();
535
536 log_dump(LOG_INFO, buffer);
537 return 0;
538}
539
bc81447e
LP
540static int manager_sigusr2(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
541 Manager *m = userdata;
bc81447e
LP
542
543 assert(s);
544 assert(si);
545 assert(m);
546
ba35662f 547 manager_flush_caches(m);
ba35662f 548
bc81447e
LP
549 return 0;
550}
551
d55b0463
LP
552static int manager_sigrtmin1(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
553 Manager *m = userdata;
554
555 assert(s);
556 assert(si);
557 assert(m);
558
559 manager_reset_server_features(m);
560 return 0;
561}
562
091a364c 563int manager_new(Manager **ret) {
74b2466e 564 _cleanup_(manager_freep) Manager *m = NULL;
091a364c
TG
565 int r;
566
c92e531c
LP
567 assert(ret);
568
f55f2dce 569 m = new(Manager, 1);
091a364c
TG
570 if (!m)
571 return -ENOMEM;
572
f55f2dce
YW
573 *m = (Manager) {
574 .llmnr_ipv4_udp_fd = -1,
575 .llmnr_ipv6_udp_fd = -1,
576 .llmnr_ipv4_tcp_fd = -1,
577 .llmnr_ipv6_tcp_fd = -1,
578 .mdns_ipv4_fd = -1,
579 .mdns_ipv6_fd = -1,
580 .dns_stub_udp_fd = -1,
581 .dns_stub_tcp_fd = -1,
582 .hostname_fd = -1,
583
584 .llmnr_support = RESOLVE_SUPPORT_YES,
585 .mdns_support = RESOLVE_SUPPORT_YES,
586 .dnssec_mode = DEFAULT_DNSSEC_MODE,
587 .dns_over_tls_mode = DEFAULT_DNS_OVER_TLS_MODE,
37d7a7d9 588 .enable_cache = DNS_CACHE_MODE_YES,
4b987478 589 .dns_stub_listener_mode = DNS_STUB_LISTENER_YES,
f55f2dce
YW
590 .read_resolv_conf = true,
591 .need_builtin_fallbacks = true,
592 .etc_hosts_last = USEC_INFINITY,
593 .etc_hosts_mtime = USEC_INFINITY,
594 .read_etc_hosts = true,
595 };
091a364c 596
0d2cd476
LP
597 r = dns_trust_anchor_load(&m->trust_anchor);
598 if (r < 0)
599 return r;
600
ad6c0475
LP
601 r = manager_parse_config_file(m);
602 if (r < 0)
5ce497b5 603 log_warning_errno(r, "Failed to parse configuration file: %m");
ad6c0475 604
e22c5b20 605#if ENABLE_DNS_OVER_TLS
71a681ae
IT
606 r = dnstls_manager_init(m);
607 if (r < 0)
608 return r;
e22c5b20
IT
609#endif
610
091a364c
TG
611 r = sd_event_default(&m->event);
612 if (r < 0)
613 return r;
614
1ae17292
YW
615 (void) sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
616 (void) sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);
091a364c 617
1ae17292 618 (void) sd_event_set_watchdog(m->event, true);
091a364c 619
eb60f9cd
LP
620 r = manager_watch_hostname(m);
621 if (r < 0)
622 return r;
623
6501dd31
DR
624 r = dnssd_load(m);
625 if (r < 0)
626 log_warning_errno(r, "Failed to load DNS-SD configuration files: %m");
627
1716f6dc 628 r = dns_scope_new(m, &m->unicast_scope, NULL, DNS_PROTOCOL_DNS, AF_UNSPEC);
74b2466e
LP
629 if (r < 0)
630 return r;
631
632 r = manager_network_monitor_listen(m);
633 if (r < 0)
634 return r;
635
636 r = manager_rtnl_listen(m);
637 if (r < 0)
638 return r;
639
640 r = manager_connect_bus(m);
641 if (r < 0)
642 return r;
643
4d506d6b 644 (void) sd_event_add_signal(m->event, &m->sigusr1_event_source, SIGUSR1, manager_sigusr1, m);
bc81447e 645 (void) sd_event_add_signal(m->event, &m->sigusr2_event_source, SIGUSR2, manager_sigusr2, m);
d55b0463 646 (void) sd_event_add_signal(m->event, &m->sigrtmin1_event_source, SIGRTMIN+1, manager_sigrtmin1, m);
4d506d6b 647
943ef07c
LP
648 manager_cleanup_saved_user(m);
649
1cc6c93a 650 *ret = TAKE_PTR(m);
091a364c
TG
651
652 return 0;
653}
654
edc501d4
LP
655int manager_start(Manager *m) {
656 int r;
657
658 assert(m);
659
b30bf55d
LP
660 r = manager_dns_stub_start(m);
661 if (r < 0)
662 return r;
663
edc501d4
LP
664 return 0;
665}
666
74b2466e
LP
667Manager *manager_free(Manager *m) {
668 Link *l;
6501dd31 669 DnssdService *s;
091a364c
TG
670
671 if (!m)
74b2466e
LP
672 return NULL;
673
4b95f179
LP
674 dns_server_unlink_all(m->dns_servers);
675 dns_server_unlink_all(m->fallback_dns_servers);
a51c1048
LP
676 dns_search_domain_unlink_all(m->search_domains);
677
74b2466e
LP
678 while ((l = hashmap_first(m->links)))
679 link_free(l);
f0e15467
LP
680
681 while (m->dns_queries)
682 dns_query_free(m->dns_queries);
74b2466e 683
cab5b059
LP
684 dns_scope_free(m->unicast_scope);
685
b30bf55d
LP
686 /* At this point only orphaned streams should remain. All others should have been freed already by their
687 * owners */
688 while (m->dns_streams)
689 dns_stream_unref(m->dns_streams);
690
e22c5b20
IT
691#if ENABLE_DNS_OVER_TLS
692 dnstls_manager_free(m);
693#endif
694
f0e15467
LP
695 hashmap_free(m->links);
696 hashmap_free(m->dns_transactions);
697
096b6773
LP
698 sd_event_source_unref(m->network_event_source);
699 sd_network_monitor_unref(m->network_monitor);
091a364c 700
a564ca2f
LP
701 sd_netlink_unref(m->rtnl);
702 sd_event_source_unref(m->rtnl_event_source);
703
edc501d4 704 manager_llmnr_stop(m);
bc7702b0 705 manager_mdns_stop(m);
b30bf55d 706 manager_dns_stub_stop(m);
623a4c97 707
2400ae29
YW
708 bus_verify_polkit_async_registry_free(m->polkit_registry);
709
92e31da1 710 sd_bus_flush_close_unref(m->bus);
091a364c 711
4d506d6b 712 sd_event_source_unref(m->sigusr1_event_source);
bc81447e 713 sd_event_source_unref(m->sigusr2_event_source);
d55b0463 714 sd_event_source_unref(m->sigrtmin1_event_source);
4d506d6b 715
74b2466e 716 sd_event_unref(m->event);
623a4c97 717
78c6a153
LP
718 dns_resource_key_unref(m->llmnr_host_ipv4_key);
719 dns_resource_key_unref(m->llmnr_host_ipv6_key);
400cb36e
DR
720 dns_resource_key_unref(m->mdns_host_ipv4_key);
721 dns_resource_key_unref(m->mdns_host_ipv6_key);
eb60f9cd 722
eb60f9cd 723 sd_event_source_unref(m->hostname_event_source);
d9fcf2ba 724 safe_close(m->hostname_fd);
e96de0ce
LP
725
726 free(m->full_hostname);
78c6a153
LP
727 free(m->llmnr_hostname);
728 free(m->mdns_hostname);
eb60f9cd 729
6501dd31
DR
730 while ((s = hashmap_first(m->dnssd_services)))
731 dnssd_service_free(s);
732 hashmap_free(m->dnssd_services);
733
0d2cd476 734 dns_trust_anchor_flush(&m->trust_anchor);
dd0bc0f1 735 manager_etc_hosts_flush(m);
0d2cd476 736
6b430fdb 737 return mfree(m);
091a364c
TG
738}
739
1716f6dc 740int manager_recv(Manager *m, int fd, DnsProtocol protocol, DnsPacket **ret) {
74b2466e 741 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
1716f6dc
LP
742 union {
743 struct cmsghdr header; /* For alignment */
40a1eebd 744 uint8_t buffer[CMSG_SPACE(MAXSIZE(struct in_pktinfo, struct in6_pktinfo))
1716f6dc 745 + CMSG_SPACE(int) /* ttl/hoplimit */
623a4c97 746 + EXTRA_CMSG_SPACE /* kernel appears to require extra buffer space */];
1716f6dc
LP
747 } control;
748 union sockaddr_union sa;
74b2466e 749 struct iovec iov;
f55f2dce
YW
750 struct msghdr mh = {
751 .msg_name = &sa.sa,
752 .msg_namelen = sizeof(sa),
753 .msg_iov = &iov,
754 .msg_iovlen = 1,
755 .msg_control = &control,
756 .msg_controllen = sizeof(control),
757 };
758 struct cmsghdr *cmsg;
4edc2c9b
LP
759 ssize_t ms, l;
760 int r;
74b2466e
LP
761
762 assert(m);
1716f6dc 763 assert(fd >= 0);
74b2466e
LP
764 assert(ret);
765
4edc2c9b 766 ms = next_datagram_size_fd(fd);
74b2466e 767 if (ms < 0)
4edc2c9b 768 return ms;
74b2466e 769
51027656 770 r = dns_packet_new(&p, protocol, ms, DNS_PACKET_SIZE_MAX);
74b2466e
LP
771 if (r < 0)
772 return r;
773
cb310866 774 iov = IOVEC_MAKE(DNS_PACKET_DATA(p), p->allocated);
74b2466e 775
a38d9945 776 l = recvmsg(fd, &mh, 0);
74b2466e 777 if (l < 0) {
4c701096 778 if (IN_SET(errno, EAGAIN, EINTR))
74b2466e
LP
779 return 0;
780
781 return -errno;
091a364c 782 }
cb310866
LP
783 if (l == 0)
784 return 0;
091a364c 785
1716f6dc
LP
786 assert(!(mh.msg_flags & MSG_CTRUNC));
787 assert(!(mh.msg_flags & MSG_TRUNC));
788
74b2466e 789 p->size = (size_t) l;
091a364c 790
1716f6dc 791 p->family = sa.sa.sa_family;
623a4c97
LP
792 p->ipproto = IPPROTO_UDP;
793 if (p->family == AF_INET) {
1716f6dc 794 p->sender.in = sa.in.sin_addr;
623a4c97
LP
795 p->sender_port = be16toh(sa.in.sin_port);
796 } else if (p->family == AF_INET6) {
1716f6dc 797 p->sender.in6 = sa.in6.sin6_addr;
623a4c97
LP
798 p->sender_port = be16toh(sa.in6.sin6_port);
799 p->ifindex = sa.in6.sin6_scope_id;
800 } else
1716f6dc 801 return -EAFNOSUPPORT;
74b2466e 802
2a1288ff 803 CMSG_FOREACH(cmsg, &mh) {
74b2466e 804
1716f6dc
LP
805 if (cmsg->cmsg_level == IPPROTO_IPV6) {
806 assert(p->family == AF_INET6);
74b2466e 807
1716f6dc 808 switch (cmsg->cmsg_type) {
74b2466e 809
1716f6dc
LP
810 case IPV6_PKTINFO: {
811 struct in6_pktinfo *i = (struct in6_pktinfo*) CMSG_DATA(cmsg);
74b2466e 812
623a4c97
LP
813 if (p->ifindex <= 0)
814 p->ifindex = i->ipi6_ifindex;
815
1716f6dc
LP
816 p->destination.in6 = i->ipi6_addr;
817 break;
818 }
74b2466e 819
1716f6dc
LP
820 case IPV6_HOPLIMIT:
821 p->ttl = *(int *) CMSG_DATA(cmsg);
822 break;
74b2466e 823
1716f6dc
LP
824 }
825 } else if (cmsg->cmsg_level == IPPROTO_IP) {
826 assert(p->family == AF_INET);
74b2466e 827
1716f6dc 828 switch (cmsg->cmsg_type) {
74b2466e 829
1716f6dc
LP
830 case IP_PKTINFO: {
831 struct in_pktinfo *i = (struct in_pktinfo*) CMSG_DATA(cmsg);
091a364c 832
623a4c97
LP
833 if (p->ifindex <= 0)
834 p->ifindex = i->ipi_ifindex;
835
1716f6dc
LP
836 p->destination.in = i->ipi_addr;
837 break;
838 }
74b2466e 839
623a4c97 840 case IP_TTL:
1716f6dc
LP
841 p->ttl = *(int *) CMSG_DATA(cmsg);
842 break;
843 }
844 }
845 }
74b2466e 846
623a4c97
LP
847 /* The Linux kernel sets the interface index to the loopback
848 * device if the packet came from the local host since it
849 * avoids the routing table in such a case. Let's unset the
850 * interface index in such a case. */
a5f03596 851 if (p->ifindex == LOOPBACK_IFINDEX)
623a4c97
LP
852 p->ifindex = 0;
853
86ad4cd7
TG
854 if (protocol != DNS_PROTOCOL_DNS) {
855 /* If we don't know the interface index still, we look for the
856 * first local interface with a matching address. Yuck! */
857 if (p->ifindex <= 0)
858 p->ifindex = manager_find_ifindex(m, p->family, &p->destination);
859 }
623a4c97 860
1cc6c93a 861 *ret = TAKE_PTR(p);
74b2466e
LP
862
863 return 1;
864}
865
74b2466e
LP
866static int sendmsg_loop(int fd, struct msghdr *mh, int flags) {
867 int r;
868
869 assert(fd >= 0);
870 assert(mh);
871
872 for (;;) {
873 if (sendmsg(fd, mh, flags) >= 0)
874 return 0;
875
876 if (errno == EINTR)
877 continue;
878
879 if (errno != EAGAIN)
880 return -errno;
881
882 r = fd_wait_for_event(fd, POLLOUT, SEND_TIMEOUT_USEC);
883 if (r < 0)
884 return r;
885 if (r == 0)
886 return -ETIMEDOUT;
887 }
888}
889
72290734
TG
890static int write_loop(int fd, void *message, size_t length) {
891 int r;
892
893 assert(fd >= 0);
894 assert(message);
895
896 for (;;) {
897 if (write(fd, message, length) >= 0)
898 return 0;
899
900 if (errno == EINTR)
901 continue;
902
903 if (errno != EAGAIN)
904 return -errno;
905
906 r = fd_wait_for_event(fd, POLLOUT, SEND_TIMEOUT_USEC);
907 if (r < 0)
908 return r;
909 if (r == 0)
910 return -ETIMEDOUT;
911 }
912}
913
914int manager_write(Manager *m, int fd, DnsPacket *p) {
915 int r;
916
deb3f3d3 917 log_debug("Sending %s packet with id %" PRIu16 ".", DNS_PACKET_QR(p) ? "response" : "query", DNS_PACKET_ID(p));
72290734
TG
918
919 r = write_loop(fd, DNS_PACKET_DATA(p), p->size);
920 if (r < 0)
921 return r;
922
923 return 0;
924}
925
b30bf55d
LP
926static int manager_ipv4_send(
927 Manager *m,
928 int fd,
929 int ifindex,
930 const struct in_addr *destination,
931 uint16_t port,
932 const struct in_addr *source,
933 DnsPacket *p) {
1716f6dc
LP
934 union {
935 struct cmsghdr header; /* For alignment */
936 uint8_t buffer[CMSG_SPACE(sizeof(struct in_pktinfo))];
f55f2dce
YW
937 } control = {};
938 union sockaddr_union sa;
74b2466e 939 struct iovec iov;
f55f2dce
YW
940 struct msghdr mh = {
941 .msg_iov = &iov,
942 .msg_iovlen = 1,
943 .msg_name = &sa.sa,
944 .msg_namelen = sizeof(sa.in),
945 };
74b2466e
LP
946
947 assert(m);
1716f6dc 948 assert(fd >= 0);
b30bf55d 949 assert(destination);
1716f6dc 950 assert(port > 0);
74b2466e
LP
951 assert(p);
952
cb310866 953 iov = IOVEC_MAKE(DNS_PACKET_DATA(p), p->size);
091a364c 954
f55f2dce
YW
955 sa = (union sockaddr_union) {
956 .in.sin_family = AF_INET,
957 .in.sin_addr = *destination,
958 .in.sin_port = htobe16(port),
959 };
091a364c 960
74b2466e
LP
961 if (ifindex > 0) {
962 struct cmsghdr *cmsg;
963 struct in_pktinfo *pi;
964
1716f6dc 965 mh.msg_control = &control;
74b2466e
LP
966 mh.msg_controllen = CMSG_LEN(sizeof(struct in_pktinfo));
967
968 cmsg = CMSG_FIRSTHDR(&mh);
969 cmsg->cmsg_len = mh.msg_controllen;
970 cmsg->cmsg_level = IPPROTO_IP;
971 cmsg->cmsg_type = IP_PKTINFO;
972
973 pi = (struct in_pktinfo*) CMSG_DATA(cmsg);
974 pi->ipi_ifindex = ifindex;
b30bf55d
LP
975
976 if (source)
977 pi->ipi_spec_dst = *source;
74b2466e
LP
978 }
979
980 return sendmsg_loop(fd, &mh, 0);
981}
982
b30bf55d
LP
983static int manager_ipv6_send(
984 Manager *m,
985 int fd,
986 int ifindex,
987 const struct in6_addr *destination,
988 uint16_t port,
989 const struct in6_addr *source,
990 DnsPacket *p) {
991
1716f6dc
LP
992 union {
993 struct cmsghdr header; /* For alignment */
994 uint8_t buffer[CMSG_SPACE(sizeof(struct in6_pktinfo))];
f55f2dce
YW
995 } control = {};
996 union sockaddr_union sa;
74b2466e 997 struct iovec iov;
f55f2dce
YW
998 struct msghdr mh = {
999 .msg_iov = &iov,
1000 .msg_iovlen = 1,
1001 .msg_name = &sa.sa,
1002 .msg_namelen = sizeof(sa.in6),
1003 };
74b2466e
LP
1004
1005 assert(m);
1716f6dc 1006 assert(fd >= 0);
b30bf55d 1007 assert(destination);
1716f6dc 1008 assert(port > 0);
74b2466e
LP
1009 assert(p);
1010
cb310866 1011 iov = IOVEC_MAKE(DNS_PACKET_DATA(p), p->size);
74b2466e 1012
f55f2dce
YW
1013 sa = (union sockaddr_union) {
1014 .in6.sin6_family = AF_INET6,
1015 .in6.sin6_addr = *destination,
1016 .in6.sin6_port = htobe16(port),
1017 .in6.sin6_scope_id = ifindex,
1018 };
74b2466e
LP
1019
1020 if (ifindex > 0) {
1021 struct cmsghdr *cmsg;
1022 struct in6_pktinfo *pi;
1023
1716f6dc 1024 mh.msg_control = &control;
74b2466e
LP
1025 mh.msg_controllen = CMSG_LEN(sizeof(struct in6_pktinfo));
1026
1027 cmsg = CMSG_FIRSTHDR(&mh);
1028 cmsg->cmsg_len = mh.msg_controllen;
1029 cmsg->cmsg_level = IPPROTO_IPV6;
1030 cmsg->cmsg_type = IPV6_PKTINFO;
1031
1032 pi = (struct in6_pktinfo*) CMSG_DATA(cmsg);
1033 pi->ipi6_ifindex = ifindex;
b30bf55d
LP
1034
1035 if (source)
1036 pi->ipi6_addr = *source;
74b2466e
LP
1037 }
1038
1039 return sendmsg_loop(fd, &mh, 0);
1040}
1041
b30bf55d
LP
1042int manager_send(
1043 Manager *m,
1044 int fd,
1045 int ifindex,
1046 int family,
1047 const union in_addr_union *destination,
1048 uint16_t port,
1049 const union in_addr_union *source,
1050 DnsPacket *p) {
1051
1716f6dc
LP
1052 assert(m);
1053 assert(fd >= 0);
b30bf55d 1054 assert(destination);
1716f6dc
LP
1055 assert(port > 0);
1056 assert(p);
1057
deb3f3d3 1058 log_debug("Sending %s packet with id %" PRIu16 " on interface %i/%s.", DNS_PACKET_QR(p) ? "response" : "query", DNS_PACKET_ID(p), ifindex, af_to_name(family));
a2a416f7 1059
1716f6dc 1060 if (family == AF_INET)
25270cf3 1061 return manager_ipv4_send(m, fd, ifindex, &destination->in, port, source ? &source->in : NULL, p);
2817157b 1062 if (family == AF_INET6)
25270cf3 1063 return manager_ipv6_send(m, fd, ifindex, &destination->in6, port, source ? &source->in6 : NULL, p);
1716f6dc
LP
1064
1065 return -EAFNOSUPPORT;
1066}
1067
e1c95994
LP
1068uint32_t manager_find_mtu(Manager *m) {
1069 uint32_t mtu = 0;
1070 Link *l;
1071 Iterator i;
1072
1073 /* If we don't know on which link a DNS packet would be
1074 * delivered, let's find the largest MTU that works on all
1075 * interfaces we know of */
1076
1077 HASHMAP_FOREACH(l, m->links, i) {
1078 if (l->mtu <= 0)
1079 continue;
1080
1081 if (mtu <= 0 || l->mtu < mtu)
1082 mtu = l->mtu;
1083 }
1084
1085 return mtu;
1086}
1716f6dc 1087
623a4c97 1088int manager_find_ifindex(Manager *m, int family, const union in_addr_union *in_addr) {
ec2c5e43
LP
1089 LinkAddress *a;
1090
1091 assert(m);
1092
4e945a6f 1093 a = manager_find_link_address(m, family, in_addr);
ec2c5e43
LP
1094 if (a)
1095 return a->link->ifindex;
1096
1097 return 0;
1098}
1099
eb60f9cd
LP
1100void manager_refresh_rrs(Manager *m) {
1101 Iterator i;
1102 Link *l;
6db6a464 1103 DnssdService *s;
eb60f9cd
LP
1104
1105 assert(m);
1106
78c6a153
LP
1107 m->llmnr_host_ipv4_key = dns_resource_key_unref(m->llmnr_host_ipv4_key);
1108 m->llmnr_host_ipv6_key = dns_resource_key_unref(m->llmnr_host_ipv6_key);
400cb36e
DR
1109 m->mdns_host_ipv4_key = dns_resource_key_unref(m->mdns_host_ipv4_key);
1110 m->mdns_host_ipv6_key = dns_resource_key_unref(m->mdns_host_ipv6_key);
eb60f9cd 1111
6db6a464
DR
1112 if (m->mdns_support == RESOLVE_SUPPORT_YES)
1113 HASHMAP_FOREACH(s, m->dnssd_services, i)
1114 if (dnssd_update_rrs(s) < 0)
1115 log_warning("Failed to refresh DNS-SD service '%s'", s->name);
1116
eb60f9cd
LP
1117 HASHMAP_FOREACH(l, m->links, i) {
1118 link_add_rrs(l, true);
1119 link_add_rrs(l, false);
1120 }
1121}
1122
e7c1b0e4 1123static int manager_next_random_name(const char *old, char **ret_new) {
ec2c5e43 1124 const char *p;
556a2294 1125 uint64_t u, a;
e7c1b0e4 1126 char *n;
623a4c97 1127
e7c1b0e4 1128 p = strchr(old, 0);
ec2c5e43
LP
1129 assert(p);
1130
e7c1b0e4
DR
1131 while (p > old) {
1132 if (!strchr(DIGITS, p[-1]))
ec2c5e43
LP
1133 break;
1134
1135 p--;
1136 }
1137
1138 if (*p == 0 || safe_atou64(p, &u) < 0 || u <= 0)
1139 u = 1;
1140
556a2294
LP
1141 /* Add a random number to the old value. This way we can avoid
1142 * that two hosts pick the same hostname, win on IPv4 and lose
1143 * on IPv6 (or vice versa), and pick the same hostname
1144 * replacement hostname, ad infinitum. We still want the
1145 * numbers to go up monotonically, hence we just add a random
1146 * value 1..10 */
1147
1148 random_bytes(&a, sizeof(a));
1149 u += 1 + a % 10;
ec2c5e43 1150
e7c1b0e4 1151 if (asprintf(&n, "%.*s%" PRIu64, (int) (p - old), old, u) < 0)
ec2c5e43
LP
1152 return -ENOMEM;
1153
e7c1b0e4
DR
1154 *ret_new = n;
1155
1156 return 0;
1157}
1158
1159int manager_next_hostname(Manager *m) {
1160 _cleanup_free_ char *h = NULL, *k = NULL;
1161 int r;
1162
1163 assert(m);
1164
1165 r = manager_next_random_name(m->llmnr_hostname, &h);
1166 if (r < 0)
1167 return r;
1168
7470cc4c 1169 r = dns_name_concat(h, "local", 0, &k);
e7c1b0e4 1170 if (r < 0)
78c6a153 1171 return r;
78c6a153
LP
1172
1173 log_info("Hostname conflict, changing published hostname from '%s' to '%s'.", m->llmnr_hostname, h);
1174
e7c1b0e4
DR
1175 free_and_replace(m->llmnr_hostname, h);
1176 free_and_replace(m->mdns_hostname, k);
ec2c5e43 1177
eb60f9cd 1178 manager_refresh_rrs(m);
86c0411e 1179 (void) manager_send_changed(m, "LLMNRHostname");
623a4c97
LP
1180
1181 return 0;
1182}
ec2c5e43 1183
4e945a6f 1184LinkAddress* manager_find_link_address(Manager *m, int family, const union in_addr_union *in_addr) {
ec2c5e43
LP
1185 Iterator i;
1186 Link *l;
1187
1188 assert(m);
1189
1190 HASHMAP_FOREACH(l, m->links, i) {
1191 LinkAddress *a;
1192
1193 a = link_find_address(l, family, in_addr);
1194 if (a)
1195 return a;
1196 }
1197
1198 return NULL;
1199}
1200
a4076574 1201bool manager_our_packet(Manager *m, DnsPacket *p) {
ec2c5e43
LP
1202 assert(m);
1203 assert(p);
1204
4e945a6f 1205 return !!manager_find_link_address(m, p->family, &p->sender);
ec2c5e43 1206}
4e945a6f 1207
a4076574
LP
1208DnsScope* manager_find_scope(Manager *m, DnsPacket *p) {
1209 Link *l;
1210
1211 assert(m);
1212 assert(p);
1213
1214 l = hashmap_get(m->links, INT_TO_PTR(p->ifindex));
1215 if (!l)
1216 return NULL;
1217
b4f1862d
DM
1218 switch (p->protocol) {
1219 case DNS_PROTOCOL_LLMNR:
a4076574
LP
1220 if (p->family == AF_INET)
1221 return l->llmnr_ipv4_scope;
1222 else if (p->family == AF_INET6)
1223 return l->llmnr_ipv6_scope;
b4f1862d
DM
1224
1225 break;
1226
1227 case DNS_PROTOCOL_MDNS:
1228 if (p->family == AF_INET)
1229 return l->mdns_ipv4_scope;
1230 else if (p->family == AF_INET6)
1231 return l->mdns_ipv6_scope;
1232
1233 break;
1234
1235 default:
1236 break;
a4076574
LP
1237 }
1238
1239 return NULL;
1240}
1241
902bb5d8
LP
1242void manager_verify_all(Manager *m) {
1243 DnsScope *s;
1244
1245 assert(m);
1246
1247 LIST_FOREACH(scopes, s, m->dns_scopes)
1248 dns_zone_verify_all(&s->zone);
1249}
1250
78c6a153 1251int manager_is_own_hostname(Manager *m, const char *name) {
78c6a153
LP
1252 int r;
1253
1254 assert(m);
1255 assert(name);
1256
1257 if (m->llmnr_hostname) {
1258 r = dns_name_equal(name, m->llmnr_hostname);
1259 if (r != 0)
1260 return r;
1261 }
1262
e96de0ce
LP
1263 if (m->mdns_hostname) {
1264 r = dns_name_equal(name, m->mdns_hostname);
1265 if (r != 0)
1266 return r;
1267 }
1268
1269 if (m->full_hostname)
1270 return dns_name_equal(name, m->full_hostname);
78c6a153
LP
1271
1272 return 0;
1273}
1274
9176a57c
LP
1275int manager_compile_dns_servers(Manager *m, OrderedSet **dns) {
1276 DnsServer *s;
1277 Iterator i;
1278 Link *l;
1279 int r;
1280
1281 assert(m);
1282 assert(dns);
1283
1284 r = ordered_set_ensure_allocated(dns, &dns_server_hash_ops);
1285 if (r < 0)
1286 return r;
1287
1288 /* First add the system-wide servers and domains */
1289 LIST_FOREACH(servers, s, m->dns_servers) {
1290 r = ordered_set_put(*dns, s);
1291 if (r == -EEXIST)
1292 continue;
1293 if (r < 0)
1294 return r;
1295 }
1296
1297 /* Then, add the per-link servers */
1298 HASHMAP_FOREACH(l, m->links, i) {
1299 LIST_FOREACH(servers, s, l->dns_servers) {
1300 r = ordered_set_put(*dns, s);
1301 if (r == -EEXIST)
1302 continue;
1303 if (r < 0)
1304 return r;
1305 }
1306 }
1307
1308 /* If we found nothing, add the fallback servers */
1309 if (ordered_set_isempty(*dns)) {
1310 LIST_FOREACH(servers, s, m->fallback_dns_servers) {
1311 r = ordered_set_put(*dns, s);
1312 if (r == -EEXIST)
1313 continue;
1314 if (r < 0)
1315 return r;
1316 }
1317 }
1318
1319 return 0;
1320}
1321
94363cbb
MP
1322/* filter_route is a tri-state:
1323 * < 0: no filtering
1324 * = 0 or false: return only domains which should be used for searching
1325 * > 0 or true: return only domains which are for routing only
1326 */
6f7da49d 1327int manager_compile_search_domains(Manager *m, OrderedSet **domains, int filter_route) {
9176a57c
LP
1328 DnsSearchDomain *d;
1329 Iterator i;
1330 Link *l;
1331 int r;
1332
1333 assert(m);
1334 assert(domains);
1335
1336 r = ordered_set_ensure_allocated(domains, &dns_name_hash_ops);
1337 if (r < 0)
1338 return r;
1339
1340 LIST_FOREACH(domains, d, m->search_domains) {
6f7da49d
LP
1341
1342 if (filter_route >= 0 &&
1343 d->route_only != !!filter_route)
1344 continue;
1345
9176a57c
LP
1346 r = ordered_set_put(*domains, d->name);
1347 if (r == -EEXIST)
1348 continue;
1349 if (r < 0)
1350 return r;
1351 }
1352
1353 HASHMAP_FOREACH(l, m->links, i) {
1354
1355 LIST_FOREACH(domains, d, l->search_domains) {
6f7da49d
LP
1356
1357 if (filter_route >= 0 &&
1358 d->route_only != !!filter_route)
1359 continue;
1360
9176a57c
LP
1361 r = ordered_set_put(*domains, d->name);
1362 if (r == -EEXIST)
1363 continue;
1364 if (r < 0)
1365 return r;
1366 }
1367 }
1368
1369 return 0;
1370}
c69fa7e3
LP
1371
1372DnssecMode manager_get_dnssec_mode(Manager *m) {
1373 assert(m);
1374
1375 if (m->dnssec_mode != _DNSSEC_MODE_INVALID)
1376 return m->dnssec_mode;
1377
1378 return DNSSEC_NO;
1379}
1380
1381bool manager_dnssec_supported(Manager *m) {
1382 DnsServer *server;
1383 Iterator i;
1384 Link *l;
1385
1386 assert(m);
1387
1388 if (manager_get_dnssec_mode(m) == DNSSEC_NO)
1389 return false;
1390
1391 server = manager_get_dns_server(m);
1392 if (server && !dns_server_dnssec_supported(server))
1393 return false;
1394
1395 HASHMAP_FOREACH(l, m->links, i)
1396 if (!link_dnssec_supported(l))
1397 return false;
1398
1399 return true;
1400}
59c5b597 1401
c9299be2 1402DnsOverTlsMode manager_get_dns_over_tls_mode(Manager *m) {
5d67a7ae
IT
1403 assert(m);
1404
c9299be2
IT
1405 if (m->dns_over_tls_mode != _DNS_OVER_TLS_MODE_INVALID)
1406 return m->dns_over_tls_mode;
5d67a7ae 1407
c9299be2 1408 return DNS_OVER_TLS_NO;
5d67a7ae
IT
1409}
1410
59c5b597
LP
1411void manager_dnssec_verdict(Manager *m, DnssecVerdict verdict, const DnsResourceKey *key) {
1412
1413 assert(verdict >= 0);
1414 assert(verdict < _DNSSEC_VERDICT_MAX);
1415
f1d34068 1416 if (DEBUG_LOGGING) {
202b76ae 1417 char s[DNS_RESOURCE_KEY_STRING_MAX];
59c5b597 1418
202b76ae
ZJS
1419 log_debug("Found verdict for lookup %s: %s",
1420 dns_resource_key_to_string(key, s, sizeof s),
1421 dnssec_verdict_to_string(verdict));
59c5b597
LP
1422 }
1423
1424 m->n_dnssec_verdict[verdict]++;
1425}
011696f7
LP
1426
1427bool manager_routable(Manager *m, int family) {
1428 Iterator i;
1429 Link *l;
1430
1431 assert(m);
1432
1433 /* Returns true if the host has at least one interface with a routable address of the specified type */
1434
1435 HASHMAP_FOREACH(l, m->links, i)
1436 if (link_relevant(l, family, false))
1437 return true;
1438
1439 return false;
1440}
ba35662f
LP
1441
1442void manager_flush_caches(Manager *m) {
1443 DnsScope *scope;
1444
1445 assert(m);
1446
1447 LIST_FOREACH(scopes, scope, m->dns_scopes)
1448 dns_cache_flush(&scope->cache);
aac57b35
ZJS
1449
1450 log_info("Flushed all caches.");
ba35662f 1451}
943ef07c 1452
59c0fd0e
LP
1453void manager_reset_server_features(Manager *m) {
1454 Iterator i;
1455 Link *l;
1456
1457 dns_server_reset_features_all(m->dns_servers);
1458 dns_server_reset_features_all(m->fallback_dns_servers);
1459
1460 HASHMAP_FOREACH(l, m->links, i)
1461 dns_server_reset_features_all(l->dns_servers);
1462
1463 log_info("Resetting learnt feature levels on all servers.");
1464}
1465
943ef07c
LP
1466void manager_cleanup_saved_user(Manager *m) {
1467 _cleanup_closedir_ DIR *d = NULL;
1468 struct dirent *de;
1469 int r;
1470
1471 assert(m);
1472
1473 /* Clean up all saved per-link files in /run/systemd/resolve/netif/ that don't have a matching interface
1474 * anymore. These files are created to persist settings pushed in by the user via the bus, so that resolved can
1475 * be restarted without losing this data. */
1476
1477 d = opendir("/run/systemd/resolve/netif/");
1478 if (!d) {
1479 if (errno == ENOENT)
1480 return;
1481
1482 log_warning_errno(errno, "Failed to open interface directory: %m");
1483 return;
1484 }
1485
1486 FOREACH_DIRENT_ALL(de, d, log_error_errno(errno, "Failed to read interface directory: %m")) {
1487 _cleanup_free_ char *p = NULL;
1488 int ifindex;
1489 Link *l;
1490
1491 if (!IN_SET(de->d_type, DT_UNKNOWN, DT_REG))
1492 continue;
1493
49bfc877 1494 if (dot_or_dot_dot(de->d_name))
943ef07c
LP
1495 continue;
1496
1497 r = parse_ifindex(de->d_name, &ifindex);
1498 if (r < 0) /* Probably some temporary file from a previous run. Delete it */
1499 goto rm;
1500
1501 l = hashmap_get(m->links, INT_TO_PTR(ifindex));
1502 if (!l) /* link vanished */
1503 goto rm;
1504
1505 if (l->is_managed) /* now managed by networkd, hence the bus settings are useless */
1506 goto rm;
1507
1508 continue;
1509
1510 rm:
b910cc72 1511 p = path_join("/run/systemd/resolve/netif", de->d_name);
943ef07c
LP
1512 if (!p) {
1513 log_oom();
1514 return;
1515 }
1516
1517 (void) unlink(p);
1518 }
1519}
e7c1b0e4
DR
1520
1521bool manager_next_dnssd_names(Manager *m) {
1522 Iterator i;
1523 DnssdService *s;
1524 bool tried = false;
1525 int r;
1526
1527 assert(m);
1528
1529 HASHMAP_FOREACH(s, m->dnssd_services, i) {
1530 _cleanup_free_ char * new_name = NULL;
1531
1532 if (!s->withdrawn)
1533 continue;
1534
1535 r = manager_next_random_name(s->name_template, &new_name);
1536 if (r < 0) {
1537 log_warning_errno(r, "Failed to get new name for service '%s': %m", s->name);
1538 continue;
1539 }
1540
1541 free_and_replace(s->name_template, new_name);
1542
1543 s->withdrawn = false;
1544
1545 tried = true;
1546 }
1547
1548 if (tried)
1549 manager_refresh_rrs(m);
1550
1551 return tried;
1552}