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