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