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