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