Update mailmap and contributor list (#7006)
[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;
522 size_t size = 0;
523 DnsScope *scope;
524
525 assert(s);
526 assert(si);
527 assert(m);
528
529 f = open_memstream(&buffer, &size);
530 if (!f)
531 return log_oom();
532
533 LIST_FOREACH(scopes, scope, m->dns_scopes)
534 dns_scope_dump(scope, f);
535
536 if (fflush_and_check(f) < 0)
537 return log_oom();
538
539 log_dump(LOG_INFO, buffer);
540 return 0;
541}
542
bc81447e
LP
543static int manager_sigusr2(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
544 Manager *m = userdata;
bc81447e
LP
545
546 assert(s);
547 assert(si);
548 assert(m);
549
ba35662f 550 manager_flush_caches(m);
ba35662f 551
bc81447e
LP
552 return 0;
553}
554
091a364c 555int manager_new(Manager **ret) {
74b2466e 556 _cleanup_(manager_freep) Manager *m = NULL;
091a364c
TG
557 int r;
558
c92e531c
LP
559 assert(ret);
560
091a364c
TG
561 m = new0(Manager, 1);
562 if (!m)
563 return -ENOMEM;
564
1716f6dc 565 m->llmnr_ipv4_udp_fd = m->llmnr_ipv6_udp_fd = -1;
623a4c97 566 m->llmnr_ipv4_tcp_fd = m->llmnr_ipv6_tcp_fd = -1;
bc7702b0 567 m->mdns_ipv4_fd = m->mdns_ipv6_fd = -1;
b30bf55d 568 m->dns_stub_udp_fd = m->dns_stub_tcp_fd = -1;
eb60f9cd 569 m->hostname_fd = -1;
1716f6dc 570
af49ca27 571 m->llmnr_support = RESOLVE_SUPPORT_YES;
7bef8e8e 572 m->mdns_support = RESOLVE_SUPPORT_YES;
61ecb465 573 m->dnssec_mode = DEFAULT_DNSSEC_MODE;
ceeddf79 574 m->enable_cache = true;
1ae43295 575 m->dns_stub_listener_mode = DNS_STUB_LISTENER_UDP;
5cb36f41 576 m->read_resolv_conf = true;
00fa60ae 577 m->need_builtin_fallbacks = true;
dd0bc0f1 578 m->etc_hosts_last = m->etc_hosts_mtime = USEC_INFINITY;
091a364c 579
0d2cd476
LP
580 r = dns_trust_anchor_load(&m->trust_anchor);
581 if (r < 0)
582 return r;
583
ad6c0475
LP
584 r = manager_parse_config_file(m);
585 if (r < 0)
5ce497b5 586 log_warning_errno(r, "Failed to parse configuration file: %m");
ad6c0475 587
091a364c
TG
588 r = sd_event_default(&m->event);
589 if (r < 0)
590 return r;
591
592 sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL);
593 sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL);
594
595 sd_event_set_watchdog(m->event, true);
596
eb60f9cd
LP
597 r = manager_watch_hostname(m);
598 if (r < 0)
599 return r;
600
1716f6dc 601 r = dns_scope_new(m, &m->unicast_scope, NULL, DNS_PROTOCOL_DNS, AF_UNSPEC);
74b2466e
LP
602 if (r < 0)
603 return r;
604
605 r = manager_network_monitor_listen(m);
606 if (r < 0)
607 return r;
608
609 r = manager_rtnl_listen(m);
610 if (r < 0)
611 return r;
612
613 r = manager_connect_bus(m);
614 if (r < 0)
615 return r;
616
4d506d6b 617 (void) sd_event_add_signal(m->event, &m->sigusr1_event_source, SIGUSR1, manager_sigusr1, m);
bc81447e 618 (void) sd_event_add_signal(m->event, &m->sigusr2_event_source, SIGUSR2, manager_sigusr2, m);
4d506d6b 619
943ef07c
LP
620 manager_cleanup_saved_user(m);
621
091a364c
TG
622 *ret = m;
623 m = NULL;
624
625 return 0;
626}
627
edc501d4
LP
628int manager_start(Manager *m) {
629 int r;
630
631 assert(m);
632
b30bf55d
LP
633 r = manager_dns_stub_start(m);
634 if (r < 0)
635 return r;
636
edc501d4
LP
637 return 0;
638}
639
74b2466e
LP
640Manager *manager_free(Manager *m) {
641 Link *l;
091a364c
TG
642
643 if (!m)
74b2466e
LP
644 return NULL;
645
4b95f179
LP
646 dns_server_unlink_all(m->dns_servers);
647 dns_server_unlink_all(m->fallback_dns_servers);
a51c1048
LP
648 dns_search_domain_unlink_all(m->search_domains);
649
74b2466e
LP
650 while ((l = hashmap_first(m->links)))
651 link_free(l);
f0e15467
LP
652
653 while (m->dns_queries)
654 dns_query_free(m->dns_queries);
74b2466e 655
cab5b059
LP
656 dns_scope_free(m->unicast_scope);
657
b30bf55d
LP
658 /* At this point only orphaned streams should remain. All others should have been freed already by their
659 * owners */
660 while (m->dns_streams)
661 dns_stream_unref(m->dns_streams);
662
f0e15467
LP
663 hashmap_free(m->links);
664 hashmap_free(m->dns_transactions);
665
096b6773
LP
666 sd_event_source_unref(m->network_event_source);
667 sd_network_monitor_unref(m->network_monitor);
091a364c 668
a564ca2f
LP
669 sd_netlink_unref(m->rtnl);
670 sd_event_source_unref(m->rtnl_event_source);
671
edc501d4 672 manager_llmnr_stop(m);
bc7702b0 673 manager_mdns_stop(m);
b30bf55d 674 manager_dns_stub_stop(m);
623a4c97 675
902bb5d8 676 sd_bus_slot_unref(m->prepare_for_sleep_slot);
74b2466e
LP
677 sd_event_source_unref(m->bus_retry_event_source);
678 sd_bus_unref(m->bus);
091a364c 679
4d506d6b 680 sd_event_source_unref(m->sigusr1_event_source);
bc81447e 681 sd_event_source_unref(m->sigusr2_event_source);
4d506d6b 682
74b2466e 683 sd_event_unref(m->event);
623a4c97 684
78c6a153
LP
685 dns_resource_key_unref(m->llmnr_host_ipv4_key);
686 dns_resource_key_unref(m->llmnr_host_ipv6_key);
400cb36e
DR
687 dns_resource_key_unref(m->mdns_host_ipv4_key);
688 dns_resource_key_unref(m->mdns_host_ipv6_key);
eb60f9cd 689
eb60f9cd 690 sd_event_source_unref(m->hostname_event_source);
d9fcf2ba 691 safe_close(m->hostname_fd);
e96de0ce
LP
692
693 free(m->full_hostname);
78c6a153
LP
694 free(m->llmnr_hostname);
695 free(m->mdns_hostname);
eb60f9cd 696
0d2cd476 697 dns_trust_anchor_flush(&m->trust_anchor);
dd0bc0f1 698 manager_etc_hosts_flush(m);
0d2cd476 699
6b430fdb 700 return mfree(m);
091a364c
TG
701}
702
1716f6dc 703int manager_recv(Manager *m, int fd, DnsProtocol protocol, DnsPacket **ret) {
74b2466e 704 _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
1716f6dc
LP
705 union {
706 struct cmsghdr header; /* For alignment */
40a1eebd 707 uint8_t buffer[CMSG_SPACE(MAXSIZE(struct in_pktinfo, struct in6_pktinfo))
1716f6dc 708 + CMSG_SPACE(int) /* ttl/hoplimit */
623a4c97 709 + EXTRA_CMSG_SPACE /* kernel appears to require extra buffer space */];
1716f6dc
LP
710 } control;
711 union sockaddr_union sa;
74b2466e 712 struct msghdr mh = {};
1716f6dc 713 struct cmsghdr *cmsg;
74b2466e 714 struct iovec iov;
4edc2c9b
LP
715 ssize_t ms, l;
716 int r;
74b2466e
LP
717
718 assert(m);
1716f6dc 719 assert(fd >= 0);
74b2466e
LP
720 assert(ret);
721
4edc2c9b 722 ms = next_datagram_size_fd(fd);
74b2466e 723 if (ms < 0)
4edc2c9b 724 return ms;
74b2466e 725
51027656 726 r = dns_packet_new(&p, protocol, ms, DNS_PACKET_SIZE_MAX);
74b2466e
LP
727 if (r < 0)
728 return r;
729
730 iov.iov_base = DNS_PACKET_DATA(p);
731 iov.iov_len = p->allocated;
732
1716f6dc
LP
733 mh.msg_name = &sa.sa;
734 mh.msg_namelen = sizeof(sa);
74b2466e
LP
735 mh.msg_iov = &iov;
736 mh.msg_iovlen = 1;
1716f6dc
LP
737 mh.msg_control = &control;
738 mh.msg_controllen = sizeof(control);
74b2466e 739
a38d9945 740 l = recvmsg(fd, &mh, 0);
f134289a
EV
741 if (l == 0)
742 return 0;
74b2466e 743 if (l < 0) {
4c701096 744 if (IN_SET(errno, EAGAIN, EINTR))
74b2466e
LP
745 return 0;
746
747 return -errno;
091a364c
TG
748 }
749
1716f6dc
LP
750 assert(!(mh.msg_flags & MSG_CTRUNC));
751 assert(!(mh.msg_flags & MSG_TRUNC));
752
74b2466e 753 p->size = (size_t) l;
091a364c 754
1716f6dc 755 p->family = sa.sa.sa_family;
623a4c97
LP
756 p->ipproto = IPPROTO_UDP;
757 if (p->family == AF_INET) {
1716f6dc 758 p->sender.in = sa.in.sin_addr;
623a4c97
LP
759 p->sender_port = be16toh(sa.in.sin_port);
760 } else if (p->family == AF_INET6) {
1716f6dc 761 p->sender.in6 = sa.in6.sin6_addr;
623a4c97
LP
762 p->sender_port = be16toh(sa.in6.sin6_port);
763 p->ifindex = sa.in6.sin6_scope_id;
764 } else
1716f6dc 765 return -EAFNOSUPPORT;
74b2466e 766
2a1288ff 767 CMSG_FOREACH(cmsg, &mh) {
74b2466e 768
1716f6dc
LP
769 if (cmsg->cmsg_level == IPPROTO_IPV6) {
770 assert(p->family == AF_INET6);
74b2466e 771
1716f6dc 772 switch (cmsg->cmsg_type) {
74b2466e 773
1716f6dc
LP
774 case IPV6_PKTINFO: {
775 struct in6_pktinfo *i = (struct in6_pktinfo*) CMSG_DATA(cmsg);
74b2466e 776
623a4c97
LP
777 if (p->ifindex <= 0)
778 p->ifindex = i->ipi6_ifindex;
779
1716f6dc
LP
780 p->destination.in6 = i->ipi6_addr;
781 break;
782 }
74b2466e 783
1716f6dc
LP
784 case IPV6_HOPLIMIT:
785 p->ttl = *(int *) CMSG_DATA(cmsg);
786 break;
74b2466e 787
1716f6dc
LP
788 }
789 } else if (cmsg->cmsg_level == IPPROTO_IP) {
790 assert(p->family == AF_INET);
74b2466e 791
1716f6dc 792 switch (cmsg->cmsg_type) {
74b2466e 793
1716f6dc
LP
794 case IP_PKTINFO: {
795 struct in_pktinfo *i = (struct in_pktinfo*) CMSG_DATA(cmsg);
091a364c 796
623a4c97
LP
797 if (p->ifindex <= 0)
798 p->ifindex = i->ipi_ifindex;
799
1716f6dc
LP
800 p->destination.in = i->ipi_addr;
801 break;
802 }
74b2466e 803
623a4c97 804 case IP_TTL:
1716f6dc
LP
805 p->ttl = *(int *) CMSG_DATA(cmsg);
806 break;
807 }
808 }
809 }
74b2466e 810
623a4c97
LP
811 /* The Linux kernel sets the interface index to the loopback
812 * device if the packet came from the local host since it
813 * avoids the routing table in such a case. Let's unset the
814 * interface index in such a case. */
a5f03596 815 if (p->ifindex == LOOPBACK_IFINDEX)
623a4c97
LP
816 p->ifindex = 0;
817
86ad4cd7
TG
818 if (protocol != DNS_PROTOCOL_DNS) {
819 /* If we don't know the interface index still, we look for the
820 * first local interface with a matching address. Yuck! */
821 if (p->ifindex <= 0)
822 p->ifindex = manager_find_ifindex(m, p->family, &p->destination);
823 }
623a4c97 824
74b2466e
LP
825 *ret = p;
826 p = NULL;
827
828 return 1;
829}
830
74b2466e
LP
831static int sendmsg_loop(int fd, struct msghdr *mh, int flags) {
832 int r;
833
834 assert(fd >= 0);
835 assert(mh);
836
837 for (;;) {
838 if (sendmsg(fd, mh, flags) >= 0)
839 return 0;
840
841 if (errno == EINTR)
842 continue;
843
844 if (errno != EAGAIN)
845 return -errno;
846
847 r = fd_wait_for_event(fd, POLLOUT, SEND_TIMEOUT_USEC);
848 if (r < 0)
849 return r;
850 if (r == 0)
851 return -ETIMEDOUT;
852 }
853}
854
72290734
TG
855static int write_loop(int fd, void *message, size_t length) {
856 int r;
857
858 assert(fd >= 0);
859 assert(message);
860
861 for (;;) {
862 if (write(fd, message, length) >= 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
879int manager_write(Manager *m, int fd, DnsPacket *p) {
880 int r;
881
deb3f3d3 882 log_debug("Sending %s packet with id %" PRIu16 ".", DNS_PACKET_QR(p) ? "response" : "query", DNS_PACKET_ID(p));
72290734
TG
883
884 r = write_loop(fd, DNS_PACKET_DATA(p), p->size);
885 if (r < 0)
886 return r;
887
888 return 0;
889}
890
b30bf55d
LP
891static int manager_ipv4_send(
892 Manager *m,
893 int fd,
894 int ifindex,
895 const struct in_addr *destination,
896 uint16_t port,
897 const struct in_addr *source,
898 DnsPacket *p) {
74b2466e
LP
899 union sockaddr_union sa = {
900 .in.sin_family = AF_INET,
74b2466e 901 };
1716f6dc
LP
902 union {
903 struct cmsghdr header; /* For alignment */
904 uint8_t buffer[CMSG_SPACE(sizeof(struct in_pktinfo))];
905 } control;
74b2466e
LP
906 struct msghdr mh = {};
907 struct iovec iov;
74b2466e
LP
908
909 assert(m);
1716f6dc 910 assert(fd >= 0);
b30bf55d 911 assert(destination);
1716f6dc 912 assert(port > 0);
74b2466e
LP
913 assert(p);
914
74b2466e
LP
915 iov.iov_base = DNS_PACKET_DATA(p);
916 iov.iov_len = p->size;
091a364c 917
b30bf55d 918 sa.in.sin_addr = *destination;
1716f6dc 919 sa.in.sin_port = htobe16(port),
091a364c 920
74b2466e
LP
921 mh.msg_iov = &iov;
922 mh.msg_iovlen = 1;
923 mh.msg_name = &sa.sa;
924 mh.msg_namelen = sizeof(sa.in);
091a364c 925
74b2466e
LP
926 if (ifindex > 0) {
927 struct cmsghdr *cmsg;
928 struct in_pktinfo *pi;
929
930 zero(control);
931
1716f6dc 932 mh.msg_control = &control;
74b2466e
LP
933 mh.msg_controllen = CMSG_LEN(sizeof(struct in_pktinfo));
934
935 cmsg = CMSG_FIRSTHDR(&mh);
936 cmsg->cmsg_len = mh.msg_controllen;
937 cmsg->cmsg_level = IPPROTO_IP;
938 cmsg->cmsg_type = IP_PKTINFO;
939
940 pi = (struct in_pktinfo*) CMSG_DATA(cmsg);
941 pi->ipi_ifindex = ifindex;
b30bf55d
LP
942
943 if (source)
944 pi->ipi_spec_dst = *source;
74b2466e
LP
945 }
946
947 return sendmsg_loop(fd, &mh, 0);
948}
949
b30bf55d
LP
950static int manager_ipv6_send(
951 Manager *m,
952 int fd,
953 int ifindex,
954 const struct in6_addr *destination,
955 uint16_t port,
956 const struct in6_addr *source,
957 DnsPacket *p) {
958
74b2466e
LP
959 union sockaddr_union sa = {
960 .in6.sin6_family = AF_INET6,
74b2466e 961 };
1716f6dc
LP
962 union {
963 struct cmsghdr header; /* For alignment */
964 uint8_t buffer[CMSG_SPACE(sizeof(struct in6_pktinfo))];
965 } control;
74b2466e
LP
966 struct msghdr mh = {};
967 struct iovec iov;
74b2466e
LP
968
969 assert(m);
1716f6dc 970 assert(fd >= 0);
b30bf55d 971 assert(destination);
1716f6dc 972 assert(port > 0);
74b2466e
LP
973 assert(p);
974
74b2466e
LP
975 iov.iov_base = DNS_PACKET_DATA(p);
976 iov.iov_len = p->size;
977
b30bf55d 978 sa.in6.sin6_addr = *destination;
1716f6dc 979 sa.in6.sin6_port = htobe16(port),
74b2466e
LP
980 sa.in6.sin6_scope_id = ifindex;
981
982 mh.msg_iov = &iov;
983 mh.msg_iovlen = 1;
984 mh.msg_name = &sa.sa;
985 mh.msg_namelen = sizeof(sa.in6);
986
987 if (ifindex > 0) {
988 struct cmsghdr *cmsg;
989 struct in6_pktinfo *pi;
990
991 zero(control);
992
1716f6dc 993 mh.msg_control = &control;
74b2466e
LP
994 mh.msg_controllen = CMSG_LEN(sizeof(struct in6_pktinfo));
995
996 cmsg = CMSG_FIRSTHDR(&mh);
997 cmsg->cmsg_len = mh.msg_controllen;
998 cmsg->cmsg_level = IPPROTO_IPV6;
999 cmsg->cmsg_type = IPV6_PKTINFO;
1000
1001 pi = (struct in6_pktinfo*) CMSG_DATA(cmsg);
1002 pi->ipi6_ifindex = ifindex;
b30bf55d
LP
1003
1004 if (source)
1005 pi->ipi6_addr = *source;
74b2466e
LP
1006 }
1007
1008 return sendmsg_loop(fd, &mh, 0);
1009}
1010
b30bf55d
LP
1011int manager_send(
1012 Manager *m,
1013 int fd,
1014 int ifindex,
1015 int family,
1016 const union in_addr_union *destination,
1017 uint16_t port,
1018 const union in_addr_union *source,
1019 DnsPacket *p) {
1020
1716f6dc
LP
1021 assert(m);
1022 assert(fd >= 0);
b30bf55d 1023 assert(destination);
1716f6dc
LP
1024 assert(port > 0);
1025 assert(p);
1026
deb3f3d3 1027 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 1028
1716f6dc 1029 if (family == AF_INET)
b30bf55d 1030 return manager_ipv4_send(m, fd, ifindex, &destination->in, port, &source->in, p);
2817157b 1031 if (family == AF_INET6)
b30bf55d 1032 return manager_ipv6_send(m, fd, ifindex, &destination->in6, port, &source->in6, p);
1716f6dc
LP
1033
1034 return -EAFNOSUPPORT;
1035}
1036
e1c95994
LP
1037uint32_t manager_find_mtu(Manager *m) {
1038 uint32_t mtu = 0;
1039 Link *l;
1040 Iterator i;
1041
1042 /* If we don't know on which link a DNS packet would be
1043 * delivered, let's find the largest MTU that works on all
1044 * interfaces we know of */
1045
1046 HASHMAP_FOREACH(l, m->links, i) {
1047 if (l->mtu <= 0)
1048 continue;
1049
1050 if (mtu <= 0 || l->mtu < mtu)
1051 mtu = l->mtu;
1052 }
1053
1054 return mtu;
1055}
1716f6dc 1056
623a4c97 1057int manager_find_ifindex(Manager *m, int family, const union in_addr_union *in_addr) {
ec2c5e43
LP
1058 LinkAddress *a;
1059
1060 assert(m);
1061
4e945a6f 1062 a = manager_find_link_address(m, family, in_addr);
ec2c5e43
LP
1063 if (a)
1064 return a->link->ifindex;
1065
1066 return 0;
1067}
1068
eb60f9cd
LP
1069void manager_refresh_rrs(Manager *m) {
1070 Iterator i;
1071 Link *l;
1072
1073 assert(m);
1074
78c6a153
LP
1075 m->llmnr_host_ipv4_key = dns_resource_key_unref(m->llmnr_host_ipv4_key);
1076 m->llmnr_host_ipv6_key = dns_resource_key_unref(m->llmnr_host_ipv6_key);
400cb36e
DR
1077 m->mdns_host_ipv4_key = dns_resource_key_unref(m->mdns_host_ipv4_key);
1078 m->mdns_host_ipv6_key = dns_resource_key_unref(m->mdns_host_ipv6_key);
eb60f9cd
LP
1079
1080 HASHMAP_FOREACH(l, m->links, i) {
1081 link_add_rrs(l, true);
1082 link_add_rrs(l, false);
1083 }
1084}
1085
ec2c5e43
LP
1086int manager_next_hostname(Manager *m) {
1087 const char *p;
556a2294 1088 uint64_t u, a;
78c6a153
LP
1089 char *h, *k;
1090 int r;
623a4c97
LP
1091
1092 assert(m);
1093
78c6a153 1094 p = strchr(m->llmnr_hostname, 0);
ec2c5e43
LP
1095 assert(p);
1096
78c6a153 1097 while (p > m->llmnr_hostname) {
ec2c5e43
LP
1098 if (!strchr("0123456789", p[-1]))
1099 break;
1100
1101 p--;
1102 }
1103
1104 if (*p == 0 || safe_atou64(p, &u) < 0 || u <= 0)
1105 u = 1;
1106
556a2294
LP
1107 /* Add a random number to the old value. This way we can avoid
1108 * that two hosts pick the same hostname, win on IPv4 and lose
1109 * on IPv6 (or vice versa), and pick the same hostname
1110 * replacement hostname, ad infinitum. We still want the
1111 * numbers to go up monotonically, hence we just add a random
1112 * value 1..10 */
1113
1114 random_bytes(&a, sizeof(a));
1115 u += 1 + a % 10;
ec2c5e43 1116
78c6a153 1117 if (asprintf(&h, "%.*s%" PRIu64, (int) (p - m->llmnr_hostname), m->llmnr_hostname, u) < 0)
ec2c5e43
LP
1118 return -ENOMEM;
1119
78c6a153
LP
1120 r = dns_name_concat(h, "local", &k);
1121 if (r < 0) {
1122 free(h);
1123 return r;
1124 }
1125
1126 log_info("Hostname conflict, changing published hostname from '%s' to '%s'.", m->llmnr_hostname, h);
1127
1128 free(m->llmnr_hostname);
1129 m->llmnr_hostname = h;
ec2c5e43 1130
78c6a153
LP
1131 free(m->mdns_hostname);
1132 m->mdns_hostname = k;
ec2c5e43 1133
eb60f9cd 1134 manager_refresh_rrs(m);
623a4c97
LP
1135
1136 return 0;
1137}
ec2c5e43 1138
4e945a6f 1139LinkAddress* manager_find_link_address(Manager *m, int family, const union in_addr_union *in_addr) {
ec2c5e43
LP
1140 Iterator i;
1141 Link *l;
1142
1143 assert(m);
1144
1145 HASHMAP_FOREACH(l, m->links, i) {
1146 LinkAddress *a;
1147
1148 a = link_find_address(l, family, in_addr);
1149 if (a)
1150 return a;
1151 }
1152
1153 return NULL;
1154}
1155
a4076574 1156bool manager_our_packet(Manager *m, DnsPacket *p) {
ec2c5e43
LP
1157 assert(m);
1158 assert(p);
1159
4e945a6f 1160 return !!manager_find_link_address(m, p->family, &p->sender);
ec2c5e43 1161}
4e945a6f 1162
a4076574
LP
1163DnsScope* manager_find_scope(Manager *m, DnsPacket *p) {
1164 Link *l;
1165
1166 assert(m);
1167 assert(p);
1168
1169 l = hashmap_get(m->links, INT_TO_PTR(p->ifindex));
1170 if (!l)
1171 return NULL;
1172
b4f1862d
DM
1173 switch (p->protocol) {
1174 case DNS_PROTOCOL_LLMNR:
a4076574
LP
1175 if (p->family == AF_INET)
1176 return l->llmnr_ipv4_scope;
1177 else if (p->family == AF_INET6)
1178 return l->llmnr_ipv6_scope;
b4f1862d
DM
1179
1180 break;
1181
1182 case DNS_PROTOCOL_MDNS:
1183 if (p->family == AF_INET)
1184 return l->mdns_ipv4_scope;
1185 else if (p->family == AF_INET6)
1186 return l->mdns_ipv6_scope;
1187
1188 break;
1189
1190 default:
1191 break;
a4076574
LP
1192 }
1193
1194 return NULL;
1195}
1196
902bb5d8
LP
1197void manager_verify_all(Manager *m) {
1198 DnsScope *s;
1199
1200 assert(m);
1201
1202 LIST_FOREACH(scopes, s, m->dns_scopes)
1203 dns_zone_verify_all(&s->zone);
1204}
1205
78c6a153 1206int manager_is_own_hostname(Manager *m, const char *name) {
78c6a153
LP
1207 int r;
1208
1209 assert(m);
1210 assert(name);
1211
1212 if (m->llmnr_hostname) {
1213 r = dns_name_equal(name, m->llmnr_hostname);
1214 if (r != 0)
1215 return r;
1216 }
1217
e96de0ce
LP
1218 if (m->mdns_hostname) {
1219 r = dns_name_equal(name, m->mdns_hostname);
1220 if (r != 0)
1221 return r;
1222 }
1223
1224 if (m->full_hostname)
1225 return dns_name_equal(name, m->full_hostname);
78c6a153
LP
1226
1227 return 0;
1228}
1229
9176a57c
LP
1230int manager_compile_dns_servers(Manager *m, OrderedSet **dns) {
1231 DnsServer *s;
1232 Iterator i;
1233 Link *l;
1234 int r;
1235
1236 assert(m);
1237 assert(dns);
1238
1239 r = ordered_set_ensure_allocated(dns, &dns_server_hash_ops);
1240 if (r < 0)
1241 return r;
1242
1243 /* First add the system-wide servers and domains */
1244 LIST_FOREACH(servers, s, m->dns_servers) {
1245 r = ordered_set_put(*dns, s);
1246 if (r == -EEXIST)
1247 continue;
1248 if (r < 0)
1249 return r;
1250 }
1251
1252 /* Then, add the per-link servers */
1253 HASHMAP_FOREACH(l, m->links, i) {
1254 LIST_FOREACH(servers, s, l->dns_servers) {
1255 r = ordered_set_put(*dns, s);
1256 if (r == -EEXIST)
1257 continue;
1258 if (r < 0)
1259 return r;
1260 }
1261 }
1262
1263 /* If we found nothing, add the fallback servers */
1264 if (ordered_set_isempty(*dns)) {
1265 LIST_FOREACH(servers, s, m->fallback_dns_servers) {
1266 r = ordered_set_put(*dns, s);
1267 if (r == -EEXIST)
1268 continue;
1269 if (r < 0)
1270 return r;
1271 }
1272 }
1273
1274 return 0;
1275}
1276
94363cbb
MP
1277/* filter_route is a tri-state:
1278 * < 0: no filtering
1279 * = 0 or false: return only domains which should be used for searching
1280 * > 0 or true: return only domains which are for routing only
1281 */
6f7da49d 1282int manager_compile_search_domains(Manager *m, OrderedSet **domains, int filter_route) {
9176a57c
LP
1283 DnsSearchDomain *d;
1284 Iterator i;
1285 Link *l;
1286 int r;
1287
1288 assert(m);
1289 assert(domains);
1290
1291 r = ordered_set_ensure_allocated(domains, &dns_name_hash_ops);
1292 if (r < 0)
1293 return r;
1294
1295 LIST_FOREACH(domains, d, m->search_domains) {
6f7da49d
LP
1296
1297 if (filter_route >= 0 &&
1298 d->route_only != !!filter_route)
1299 continue;
1300
9176a57c
LP
1301 r = ordered_set_put(*domains, d->name);
1302 if (r == -EEXIST)
1303 continue;
1304 if (r < 0)
1305 return r;
1306 }
1307
1308 HASHMAP_FOREACH(l, m->links, i) {
1309
1310 LIST_FOREACH(domains, d, l->search_domains) {
6f7da49d
LP
1311
1312 if (filter_route >= 0 &&
1313 d->route_only != !!filter_route)
1314 continue;
1315
9176a57c
LP
1316 r = ordered_set_put(*domains, d->name);
1317 if (r == -EEXIST)
1318 continue;
1319 if (r < 0)
1320 return r;
1321 }
1322 }
1323
1324 return 0;
1325}
c69fa7e3
LP
1326
1327DnssecMode manager_get_dnssec_mode(Manager *m) {
1328 assert(m);
1329
1330 if (m->dnssec_mode != _DNSSEC_MODE_INVALID)
1331 return m->dnssec_mode;
1332
1333 return DNSSEC_NO;
1334}
1335
1336bool manager_dnssec_supported(Manager *m) {
1337 DnsServer *server;
1338 Iterator i;
1339 Link *l;
1340
1341 assert(m);
1342
1343 if (manager_get_dnssec_mode(m) == DNSSEC_NO)
1344 return false;
1345
1346 server = manager_get_dns_server(m);
1347 if (server && !dns_server_dnssec_supported(server))
1348 return false;
1349
1350 HASHMAP_FOREACH(l, m->links, i)
1351 if (!link_dnssec_supported(l))
1352 return false;
1353
1354 return true;
1355}
59c5b597
LP
1356
1357void manager_dnssec_verdict(Manager *m, DnssecVerdict verdict, const DnsResourceKey *key) {
1358
1359 assert(verdict >= 0);
1360 assert(verdict < _DNSSEC_VERDICT_MAX);
1361
1362 if (log_get_max_level() >= LOG_DEBUG) {
202b76ae 1363 char s[DNS_RESOURCE_KEY_STRING_MAX];
59c5b597 1364
202b76ae
ZJS
1365 log_debug("Found verdict for lookup %s: %s",
1366 dns_resource_key_to_string(key, s, sizeof s),
1367 dnssec_verdict_to_string(verdict));
59c5b597
LP
1368 }
1369
1370 m->n_dnssec_verdict[verdict]++;
1371}
011696f7
LP
1372
1373bool manager_routable(Manager *m, int family) {
1374 Iterator i;
1375 Link *l;
1376
1377 assert(m);
1378
1379 /* Returns true if the host has at least one interface with a routable address of the specified type */
1380
1381 HASHMAP_FOREACH(l, m->links, i)
1382 if (link_relevant(l, family, false))
1383 return true;
1384
1385 return false;
1386}
ba35662f
LP
1387
1388void manager_flush_caches(Manager *m) {
1389 DnsScope *scope;
1390
1391 assert(m);
1392
1393 LIST_FOREACH(scopes, scope, m->dns_scopes)
1394 dns_cache_flush(&scope->cache);
aac57b35
ZJS
1395
1396 log_info("Flushed all caches.");
ba35662f 1397}
943ef07c
LP
1398
1399void manager_cleanup_saved_user(Manager *m) {
1400 _cleanup_closedir_ DIR *d = NULL;
1401 struct dirent *de;
1402 int r;
1403
1404 assert(m);
1405
1406 /* Clean up all saved per-link files in /run/systemd/resolve/netif/ that don't have a matching interface
1407 * anymore. These files are created to persist settings pushed in by the user via the bus, so that resolved can
1408 * be restarted without losing this data. */
1409
1410 d = opendir("/run/systemd/resolve/netif/");
1411 if (!d) {
1412 if (errno == ENOENT)
1413 return;
1414
1415 log_warning_errno(errno, "Failed to open interface directory: %m");
1416 return;
1417 }
1418
1419 FOREACH_DIRENT_ALL(de, d, log_error_errno(errno, "Failed to read interface directory: %m")) {
1420 _cleanup_free_ char *p = NULL;
1421 int ifindex;
1422 Link *l;
1423
1424 if (!IN_SET(de->d_type, DT_UNKNOWN, DT_REG))
1425 continue;
1426
49bfc877 1427 if (dot_or_dot_dot(de->d_name))
943ef07c
LP
1428 continue;
1429
1430 r = parse_ifindex(de->d_name, &ifindex);
1431 if (r < 0) /* Probably some temporary file from a previous run. Delete it */
1432 goto rm;
1433
1434 l = hashmap_get(m->links, INT_TO_PTR(ifindex));
1435 if (!l) /* link vanished */
1436 goto rm;
1437
1438 if (l->is_managed) /* now managed by networkd, hence the bus settings are useless */
1439 goto rm;
1440
1441 continue;
1442
1443 rm:
1444 p = strappend("/run/systemd/resolve/netif/", de->d_name);
1445 if (!p) {
1446 log_oom();
1447 return;
1448 }
1449
1450 (void) unlink(p);
1451 }
1452}