]>
Commit | Line | Data |
---|---|---|
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 | ||
20 | #include <netinet/in.h> | |
21 | #include <poll.h> | |
22 | #include <sys/ioctl.h> | |
23 | ||
24 | #if HAVE_LIBIDN2 | |
25 | #include <idn2.h> | |
26 | #endif | |
27 | ||
28 | #include "af-list.h" | |
29 | #include "alloc-util.h" | |
30 | #include "dirent-util.h" | |
31 | #include "dns-domain.h" | |
32 | #include "fd-util.h" | |
33 | #include "fileio-label.h" | |
34 | #include "hostname-util.h" | |
35 | #include "io-util.h" | |
36 | #include "netlink-util.h" | |
37 | #include "network-internal.h" | |
38 | #include "ordered-set.h" | |
39 | #include "parse-util.h" | |
40 | #include "random-util.h" | |
41 | #include "resolved-bus.h" | |
42 | #include "resolved-conf.h" | |
43 | #include "resolved-dns-stub.h" | |
44 | #include "resolved-etc-hosts.h" | |
45 | #include "resolved-llmnr.h" | |
46 | #include "resolved-manager.h" | |
47 | #include "resolved-mdns.h" | |
48 | #include "resolved-resolv-conf.h" | |
49 | #include "socket-util.h" | |
50 | #include "string-table.h" | |
51 | #include "string-util.h" | |
52 | #include "utf8.h" | |
53 | ||
54 | #define SEND_TIMEOUT_USEC (200 * USEC_PER_MSEC) | |
55 | ||
56 | static int manager_process_link(sd_netlink *rtnl, sd_netlink_message *mm, void *userdata) { | |
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 | ||
66 | r = sd_netlink_message_get_type(mm, &type); | |
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 | ||
78 | case RTM_NEWLINK:{ | |
79 | bool is_new = !l; | |
80 | ||
81 | if (!l) { | |
82 | r = link_new(m, &l, ifindex); | |
83 | if (r < 0) | |
84 | goto fail; | |
85 | } | |
86 | ||
87 | r = link_process_rtnl(l, mm); | |
88 | if (r < 0) | |
89 | goto fail; | |
90 | ||
91 | r = link_update(l); | |
92 | if (r < 0) | |
93 | goto fail; | |
94 | ||
95 | if (is_new) | |
96 | log_debug("Found new link %i/%s", ifindex, l->name); | |
97 | ||
98 | break; | |
99 | } | |
100 | ||
101 | case RTM_DELLINK: | |
102 | if (l) { | |
103 | log_debug("Removing link %i/%s", l->ifindex, l->name); | |
104 | link_remove_user(l); | |
105 | link_free(l); | |
106 | } | |
107 | ||
108 | break; | |
109 | } | |
110 | ||
111 | return 0; | |
112 | ||
113 | fail: | |
114 | log_warning_errno(r, "Failed to process RTNL link message: %m"); | |
115 | return 0; | |
116 | } | |
117 | ||
118 | static int manager_process_address(sd_netlink *rtnl, sd_netlink_message *mm, void *userdata) { | |
119 | Manager *m = userdata; | |
120 | union in_addr_union address; | |
121 | uint16_t type; | |
122 | int r, ifindex, family; | |
123 | LinkAddress *a; | |
124 | Link *l; | |
125 | ||
126 | assert(rtnl); | |
127 | assert(mm); | |
128 | assert(m); | |
129 | ||
130 | r = sd_netlink_message_get_type(mm, &type); | |
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: | |
149 | r = sd_netlink_message_read_in_addr(mm, IFA_LOCAL, &address.in); | |
150 | if (r < 0) { | |
151 | r = sd_netlink_message_read_in_addr(mm, IFA_ADDRESS, &address.in); | |
152 | if (r < 0) | |
153 | goto fail; | |
154 | } | |
155 | ||
156 | break; | |
157 | ||
158 | case AF_INET6: | |
159 | r = sd_netlink_message_read_in6_addr(mm, IFA_LOCAL, &address.in6); | |
160 | if (r < 0) { | |
161 | r = sd_netlink_message_read_in6_addr(mm, IFA_ADDRESS, &address.in6); | |
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: | |
191 | link_address_free(a); | |
192 | break; | |
193 | } | |
194 | ||
195 | return 0; | |
196 | ||
197 | fail: | |
198 | log_warning_errno(r, "Failed to process RTNL address message: %m"); | |
199 | return 0; | |
200 | } | |
201 | ||
202 | static int manager_rtnl_listen(Manager *m) { | |
203 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; | |
204 | sd_netlink_message *i; | |
205 | int r; | |
206 | ||
207 | assert(m); | |
208 | ||
209 | /* First, subscribe to interfaces coming and going */ | |
210 | r = sd_netlink_open(&m->rtnl); | |
211 | if (r < 0) | |
212 | return r; | |
213 | ||
214 | r = sd_netlink_attach_event(m->rtnl, m->event, SD_EVENT_PRIORITY_IMPORTANT); | |
215 | if (r < 0) | |
216 | return r; | |
217 | ||
218 | r = sd_netlink_add_match(m->rtnl, RTM_NEWLINK, manager_process_link, m); | |
219 | if (r < 0) | |
220 | return r; | |
221 | ||
222 | r = sd_netlink_add_match(m->rtnl, RTM_DELLINK, manager_process_link, m); | |
223 | if (r < 0) | |
224 | return r; | |
225 | ||
226 | r = sd_netlink_add_match(m->rtnl, RTM_NEWADDR, manager_process_address, m); | |
227 | if (r < 0) | |
228 | return r; | |
229 | ||
230 | r = sd_netlink_add_match(m->rtnl, RTM_DELADDR, manager_process_address, m); | |
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 | ||
239 | r = sd_netlink_message_request_dump(req, true); | |
240 | if (r < 0) | |
241 | return r; | |
242 | ||
243 | r = sd_netlink_call(m->rtnl, req, 0, &reply); | |
244 | if (r < 0) | |
245 | return r; | |
246 | ||
247 | for (i = reply; i; i = sd_netlink_message_next(i)) { | |
248 | r = manager_process_link(m->rtnl, i, m); | |
249 | if (r < 0) | |
250 | return r; | |
251 | } | |
252 | ||
253 | req = sd_netlink_message_unref(req); | |
254 | reply = sd_netlink_message_unref(reply); | |
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 | ||
261 | r = sd_netlink_message_request_dump(req, true); | |
262 | if (r < 0) | |
263 | return r; | |
264 | ||
265 | r = sd_netlink_call(m->rtnl, req, 0, &reply); | |
266 | if (r < 0) | |
267 | return r; | |
268 | ||
269 | for (i = reply; i; i = sd_netlink_message_next(i)) { | |
270 | r = manager_process_address(m->rtnl, i, m); | |
271 | if (r < 0) | |
272 | return r; | |
273 | } | |
274 | ||
275 | return r; | |
276 | } | |
277 | ||
278 | static 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) { | |
289 | r = link_update(l); | |
290 | if (r < 0) | |
291 | log_warning_errno(r, "Failed to update monitor information for %i: %m", l->ifindex); | |
292 | } | |
293 | ||
294 | (void) manager_write_resolv_conf(m); | |
295 | ||
296 | return 0; | |
297 | } | |
298 | ||
299 | static int manager_network_monitor_listen(Manager *m) { | |
300 | int r, fd, events; | |
301 | ||
302 | assert(m); | |
303 | ||
304 | r = sd_network_monitor_new(&m->network_monitor, NULL); | |
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) | |
318 | return r; | |
319 | ||
320 | r = sd_event_source_set_priority(m->network_event_source, SD_EVENT_PRIORITY_IMPORTANT+5); | |
321 | if (r < 0) | |
322 | return r; | |
323 | ||
324 | (void) sd_event_source_set_description(m->network_event_source, "network-monitor"); | |
325 | ||
326 | return 0; | |
327 | } | |
328 | ||
329 | static int determine_hostname(char **full_hostname, char **llmnr_hostname, char **mdns_hostname) { | |
330 | _cleanup_free_ char *h = NULL, *n = NULL; | |
331 | #if HAVE_LIBIDN2 | |
332 | _cleanup_free_ char *utf8 = NULL; | |
333 | #elif HAVE_LIBIDN | |
334 | int k; | |
335 | #endif | |
336 | char label[DNS_LABEL_MAX]; | |
337 | const char *p, *decoded; | |
338 | int r; | |
339 | ||
340 | assert(full_hostname); | |
341 | assert(llmnr_hostname); | |
342 | assert(mdns_hostname); | |
343 | ||
344 | /* Extract and normalize the first label of the locally configured hostname, and check it's not "localhost". */ | |
345 | ||
346 | r = gethostname_strict(&h); | |
347 | if (r < 0) | |
348 | return log_debug_errno(r, "Can't determine system hostname: %m"); | |
349 | ||
350 | p = h; | |
351 | r = dns_label_unescape(&p, label, sizeof label); | |
352 | if (r < 0) | |
353 | return log_error_errno(r, "Failed to unescape host name: %m"); | |
354 | if (r == 0) { | |
355 | log_error("Couldn't find a single label in hostname."); | |
356 | return -EINVAL; | |
357 | } | |
358 | ||
359 | #if HAVE_LIBIDN2 | |
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; | |
367 | #elif HAVE_LIBIDN | |
368 | k = dns_label_undo_idna(label, r, label, sizeof label); | |
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)) { | |
375 | log_error("System hostname is not UTF-8 clean."); | |
376 | return -EINVAL; | |
377 | } | |
378 | decoded = label; | |
379 | #else | |
380 | decoded = label; /* no decoding */ | |
381 | #endif | |
382 | ||
383 | r = dns_label_escape_new(decoded, r, &n); | |
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; | |
390 | } | |
391 | ||
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; | |
397 | n = NULL; | |
398 | ||
399 | *full_hostname = h; | |
400 | h = NULL; | |
401 | ||
402 | return 0; | |
403 | } | |
404 | ||
405 | static 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 | ||
416 | static 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 | ||
456 | static int on_hostname_change(sd_event_source *es, int fd, uint32_t revents, void *userdata) { | |
457 | _cleanup_free_ char *full_hostname = NULL, *llmnr_hostname = NULL, *mdns_hostname = NULL; | |
458 | Manager *m = userdata; | |
459 | int r; | |
460 | ||
461 | assert(m); | |
462 | ||
463 | r = determine_hostname(&full_hostname, &llmnr_hostname, &mdns_hostname); | |
464 | if (r < 0) | |
465 | return 0; /* ignore invalid hostnames */ | |
466 | ||
467 | if (streq(full_hostname, m->full_hostname) && | |
468 | streq(llmnr_hostname, m->llmnr_hostname) && | |
469 | streq(mdns_hostname, m->mdns_hostname)) | |
470 | return 0; | |
471 | ||
472 | log_info("System hostname changed to '%s'.", full_hostname); | |
473 | ||
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); | |
477 | ||
478 | manager_refresh_rrs(m); | |
479 | ||
480 | return 0; | |
481 | } | |
482 | ||
483 | static int manager_watch_hostname(Manager *m) { | |
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) { | |
490 | log_warning_errno(errno, "Failed to watch hostname: %m"); | |
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); | |
499 | else | |
500 | return log_error_errno(r, "Failed to add hostname event source: %m"); | |
501 | } | |
502 | ||
503 | (void) sd_event_source_set_description(m->hostname_event_source, "hostname"); | |
504 | ||
505 | r = determine_hostname(&m->full_hostname, &m->llmnr_hostname, &m->mdns_hostname); | |
506 | if (r < 0) { | |
507 | log_info("Defaulting to hostname '%s'.", fallback_hostname()); | |
508 | ||
509 | r = make_fallback_hostnames(&m->full_hostname, &m->llmnr_hostname, &m->mdns_hostname); | |
510 | if (r < 0) | |
511 | return r; | |
512 | } else | |
513 | log_info("Using system hostname '%s'.", m->full_hostname); | |
514 | ||
515 | return 0; | |
516 | } | |
517 | ||
518 | static 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 | ||
543 | static int manager_sigusr2(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { | |
544 | Manager *m = userdata; | |
545 | ||
546 | assert(s); | |
547 | assert(si); | |
548 | assert(m); | |
549 | ||
550 | manager_flush_caches(m); | |
551 | ||
552 | return 0; | |
553 | } | |
554 | ||
555 | int manager_new(Manager **ret) { | |
556 | _cleanup_(manager_freep) Manager *m = NULL; | |
557 | int r; | |
558 | ||
559 | assert(ret); | |
560 | ||
561 | m = new0(Manager, 1); | |
562 | if (!m) | |
563 | return -ENOMEM; | |
564 | ||
565 | m->llmnr_ipv4_udp_fd = m->llmnr_ipv6_udp_fd = -1; | |
566 | m->llmnr_ipv4_tcp_fd = m->llmnr_ipv6_tcp_fd = -1; | |
567 | m->mdns_ipv4_fd = m->mdns_ipv6_fd = -1; | |
568 | m->dns_stub_udp_fd = m->dns_stub_tcp_fd = -1; | |
569 | m->hostname_fd = -1; | |
570 | ||
571 | m->llmnr_support = RESOLVE_SUPPORT_YES; | |
572 | m->mdns_support = RESOLVE_SUPPORT_YES; | |
573 | m->dnssec_mode = DEFAULT_DNSSEC_MODE; | |
574 | m->enable_cache = true; | |
575 | m->dns_stub_listener_mode = DNS_STUB_LISTENER_UDP; | |
576 | m->read_resolv_conf = true; | |
577 | m->need_builtin_fallbacks = true; | |
578 | m->etc_hosts_last = m->etc_hosts_mtime = USEC_INFINITY; | |
579 | ||
580 | r = dns_trust_anchor_load(&m->trust_anchor); | |
581 | if (r < 0) | |
582 | return r; | |
583 | ||
584 | r = manager_parse_config_file(m); | |
585 | if (r < 0) | |
586 | log_warning_errno(r, "Failed to parse configuration file: %m"); | |
587 | ||
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 | ||
597 | r = manager_watch_hostname(m); | |
598 | if (r < 0) | |
599 | return r; | |
600 | ||
601 | r = dns_scope_new(m, &m->unicast_scope, NULL, DNS_PROTOCOL_DNS, AF_UNSPEC); | |
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 | ||
617 | (void) sd_event_add_signal(m->event, &m->sigusr1_event_source, SIGUSR1, manager_sigusr1, m); | |
618 | (void) sd_event_add_signal(m->event, &m->sigusr2_event_source, SIGUSR2, manager_sigusr2, m); | |
619 | ||
620 | manager_cleanup_saved_user(m); | |
621 | ||
622 | *ret = m; | |
623 | m = NULL; | |
624 | ||
625 | return 0; | |
626 | } | |
627 | ||
628 | int manager_start(Manager *m) { | |
629 | int r; | |
630 | ||
631 | assert(m); | |
632 | ||
633 | r = manager_dns_stub_start(m); | |
634 | if (r < 0) | |
635 | return r; | |
636 | ||
637 | return 0; | |
638 | } | |
639 | ||
640 | Manager *manager_free(Manager *m) { | |
641 | Link *l; | |
642 | ||
643 | if (!m) | |
644 | return NULL; | |
645 | ||
646 | dns_server_unlink_all(m->dns_servers); | |
647 | dns_server_unlink_all(m->fallback_dns_servers); | |
648 | dns_search_domain_unlink_all(m->search_domains); | |
649 | ||
650 | while ((l = hashmap_first(m->links))) | |
651 | link_free(l); | |
652 | ||
653 | while (m->dns_queries) | |
654 | dns_query_free(m->dns_queries); | |
655 | ||
656 | dns_scope_free(m->unicast_scope); | |
657 | ||
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 | ||
663 | hashmap_free(m->links); | |
664 | hashmap_free(m->dns_transactions); | |
665 | ||
666 | sd_event_source_unref(m->network_event_source); | |
667 | sd_network_monitor_unref(m->network_monitor); | |
668 | ||
669 | sd_netlink_unref(m->rtnl); | |
670 | sd_event_source_unref(m->rtnl_event_source); | |
671 | ||
672 | manager_llmnr_stop(m); | |
673 | manager_mdns_stop(m); | |
674 | manager_dns_stub_stop(m); | |
675 | ||
676 | sd_bus_slot_unref(m->prepare_for_sleep_slot); | |
677 | sd_event_source_unref(m->bus_retry_event_source); | |
678 | sd_bus_unref(m->bus); | |
679 | ||
680 | sd_event_source_unref(m->sigusr1_event_source); | |
681 | sd_event_source_unref(m->sigusr2_event_source); | |
682 | ||
683 | sd_event_unref(m->event); | |
684 | ||
685 | dns_resource_key_unref(m->llmnr_host_ipv4_key); | |
686 | dns_resource_key_unref(m->llmnr_host_ipv6_key); | |
687 | dns_resource_key_unref(m->mdns_host_ipv4_key); | |
688 | dns_resource_key_unref(m->mdns_host_ipv6_key); | |
689 | ||
690 | sd_event_source_unref(m->hostname_event_source); | |
691 | safe_close(m->hostname_fd); | |
692 | ||
693 | free(m->full_hostname); | |
694 | free(m->llmnr_hostname); | |
695 | free(m->mdns_hostname); | |
696 | ||
697 | dns_trust_anchor_flush(&m->trust_anchor); | |
698 | manager_etc_hosts_flush(m); | |
699 | ||
700 | return mfree(m); | |
701 | } | |
702 | ||
703 | int manager_recv(Manager *m, int fd, DnsProtocol protocol, DnsPacket **ret) { | |
704 | _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL; | |
705 | union { | |
706 | struct cmsghdr header; /* For alignment */ | |
707 | uint8_t buffer[CMSG_SPACE(MAXSIZE(struct in_pktinfo, struct in6_pktinfo)) | |
708 | + CMSG_SPACE(int) /* ttl/hoplimit */ | |
709 | + EXTRA_CMSG_SPACE /* kernel appears to require extra buffer space */]; | |
710 | } control; | |
711 | union sockaddr_union sa; | |
712 | struct msghdr mh = {}; | |
713 | struct cmsghdr *cmsg; | |
714 | struct iovec iov; | |
715 | ssize_t ms, l; | |
716 | int r; | |
717 | ||
718 | assert(m); | |
719 | assert(fd >= 0); | |
720 | assert(ret); | |
721 | ||
722 | ms = next_datagram_size_fd(fd); | |
723 | if (ms < 0) | |
724 | return ms; | |
725 | ||
726 | r = dns_packet_new(&p, protocol, ms, DNS_PACKET_SIZE_MAX); | |
727 | if (r < 0) | |
728 | return r; | |
729 | ||
730 | iov.iov_base = DNS_PACKET_DATA(p); | |
731 | iov.iov_len = p->allocated; | |
732 | ||
733 | mh.msg_name = &sa.sa; | |
734 | mh.msg_namelen = sizeof(sa); | |
735 | mh.msg_iov = &iov; | |
736 | mh.msg_iovlen = 1; | |
737 | mh.msg_control = &control; | |
738 | mh.msg_controllen = sizeof(control); | |
739 | ||
740 | l = recvmsg(fd, &mh, 0); | |
741 | if (l == 0) | |
742 | return 0; | |
743 | if (l < 0) { | |
744 | if (IN_SET(errno, EAGAIN, EINTR)) | |
745 | return 0; | |
746 | ||
747 | return -errno; | |
748 | } | |
749 | ||
750 | assert(!(mh.msg_flags & MSG_CTRUNC)); | |
751 | assert(!(mh.msg_flags & MSG_TRUNC)); | |
752 | ||
753 | p->size = (size_t) l; | |
754 | ||
755 | p->family = sa.sa.sa_family; | |
756 | p->ipproto = IPPROTO_UDP; | |
757 | if (p->family == AF_INET) { | |
758 | p->sender.in = sa.in.sin_addr; | |
759 | p->sender_port = be16toh(sa.in.sin_port); | |
760 | } else if (p->family == AF_INET6) { | |
761 | p->sender.in6 = sa.in6.sin6_addr; | |
762 | p->sender_port = be16toh(sa.in6.sin6_port); | |
763 | p->ifindex = sa.in6.sin6_scope_id; | |
764 | } else | |
765 | return -EAFNOSUPPORT; | |
766 | ||
767 | CMSG_FOREACH(cmsg, &mh) { | |
768 | ||
769 | if (cmsg->cmsg_level == IPPROTO_IPV6) { | |
770 | assert(p->family == AF_INET6); | |
771 | ||
772 | switch (cmsg->cmsg_type) { | |
773 | ||
774 | case IPV6_PKTINFO: { | |
775 | struct in6_pktinfo *i = (struct in6_pktinfo*) CMSG_DATA(cmsg); | |
776 | ||
777 | if (p->ifindex <= 0) | |
778 | p->ifindex = i->ipi6_ifindex; | |
779 | ||
780 | p->destination.in6 = i->ipi6_addr; | |
781 | break; | |
782 | } | |
783 | ||
784 | case IPV6_HOPLIMIT: | |
785 | p->ttl = *(int *) CMSG_DATA(cmsg); | |
786 | break; | |
787 | ||
788 | } | |
789 | } else if (cmsg->cmsg_level == IPPROTO_IP) { | |
790 | assert(p->family == AF_INET); | |
791 | ||
792 | switch (cmsg->cmsg_type) { | |
793 | ||
794 | case IP_PKTINFO: { | |
795 | struct in_pktinfo *i = (struct in_pktinfo*) CMSG_DATA(cmsg); | |
796 | ||
797 | if (p->ifindex <= 0) | |
798 | p->ifindex = i->ipi_ifindex; | |
799 | ||
800 | p->destination.in = i->ipi_addr; | |
801 | break; | |
802 | } | |
803 | ||
804 | case IP_TTL: | |
805 | p->ttl = *(int *) CMSG_DATA(cmsg); | |
806 | break; | |
807 | } | |
808 | } | |
809 | } | |
810 | ||
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. */ | |
815 | if (p->ifindex == LOOPBACK_IFINDEX) | |
816 | p->ifindex = 0; | |
817 | ||
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 | } | |
824 | ||
825 | *ret = p; | |
826 | p = NULL; | |
827 | ||
828 | return 1; | |
829 | } | |
830 | ||
831 | static 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 | ||
855 | static 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 | ||
879 | int manager_write(Manager *m, int fd, DnsPacket *p) { | |
880 | int r; | |
881 | ||
882 | log_debug("Sending %s packet with id %" PRIu16 ".", DNS_PACKET_QR(p) ? "response" : "query", DNS_PACKET_ID(p)); | |
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 | ||
891 | static 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) { | |
899 | union sockaddr_union sa = { | |
900 | .in.sin_family = AF_INET, | |
901 | }; | |
902 | union { | |
903 | struct cmsghdr header; /* For alignment */ | |
904 | uint8_t buffer[CMSG_SPACE(sizeof(struct in_pktinfo))]; | |
905 | } control; | |
906 | struct msghdr mh = {}; | |
907 | struct iovec iov; | |
908 | ||
909 | assert(m); | |
910 | assert(fd >= 0); | |
911 | assert(destination); | |
912 | assert(port > 0); | |
913 | assert(p); | |
914 | ||
915 | iov.iov_base = DNS_PACKET_DATA(p); | |
916 | iov.iov_len = p->size; | |
917 | ||
918 | sa.in.sin_addr = *destination; | |
919 | sa.in.sin_port = htobe16(port), | |
920 | ||
921 | mh.msg_iov = &iov; | |
922 | mh.msg_iovlen = 1; | |
923 | mh.msg_name = &sa.sa; | |
924 | mh.msg_namelen = sizeof(sa.in); | |
925 | ||
926 | if (ifindex > 0) { | |
927 | struct cmsghdr *cmsg; | |
928 | struct in_pktinfo *pi; | |
929 | ||
930 | zero(control); | |
931 | ||
932 | mh.msg_control = &control; | |
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; | |
942 | ||
943 | if (source) | |
944 | pi->ipi_spec_dst = *source; | |
945 | } | |
946 | ||
947 | return sendmsg_loop(fd, &mh, 0); | |
948 | } | |
949 | ||
950 | static 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 | ||
959 | union sockaddr_union sa = { | |
960 | .in6.sin6_family = AF_INET6, | |
961 | }; | |
962 | union { | |
963 | struct cmsghdr header; /* For alignment */ | |
964 | uint8_t buffer[CMSG_SPACE(sizeof(struct in6_pktinfo))]; | |
965 | } control; | |
966 | struct msghdr mh = {}; | |
967 | struct iovec iov; | |
968 | ||
969 | assert(m); | |
970 | assert(fd >= 0); | |
971 | assert(destination); | |
972 | assert(port > 0); | |
973 | assert(p); | |
974 | ||
975 | iov.iov_base = DNS_PACKET_DATA(p); | |
976 | iov.iov_len = p->size; | |
977 | ||
978 | sa.in6.sin6_addr = *destination; | |
979 | sa.in6.sin6_port = htobe16(port), | |
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 | ||
993 | mh.msg_control = &control; | |
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; | |
1003 | ||
1004 | if (source) | |
1005 | pi->ipi6_addr = *source; | |
1006 | } | |
1007 | ||
1008 | return sendmsg_loop(fd, &mh, 0); | |
1009 | } | |
1010 | ||
1011 | int 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 | ||
1021 | assert(m); | |
1022 | assert(fd >= 0); | |
1023 | assert(destination); | |
1024 | assert(port > 0); | |
1025 | assert(p); | |
1026 | ||
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)); | |
1028 | ||
1029 | if (family == AF_INET) | |
1030 | return manager_ipv4_send(m, fd, ifindex, &destination->in, port, &source->in, p); | |
1031 | if (family == AF_INET6) | |
1032 | return manager_ipv6_send(m, fd, ifindex, &destination->in6, port, &source->in6, p); | |
1033 | ||
1034 | return -EAFNOSUPPORT; | |
1035 | } | |
1036 | ||
1037 | uint32_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 | } | |
1056 | ||
1057 | int manager_find_ifindex(Manager *m, int family, const union in_addr_union *in_addr) { | |
1058 | LinkAddress *a; | |
1059 | ||
1060 | assert(m); | |
1061 | ||
1062 | a = manager_find_link_address(m, family, in_addr); | |
1063 | if (a) | |
1064 | return a->link->ifindex; | |
1065 | ||
1066 | return 0; | |
1067 | } | |
1068 | ||
1069 | void manager_refresh_rrs(Manager *m) { | |
1070 | Iterator i; | |
1071 | Link *l; | |
1072 | ||
1073 | assert(m); | |
1074 | ||
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); | |
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); | |
1079 | ||
1080 | HASHMAP_FOREACH(l, m->links, i) { | |
1081 | link_add_rrs(l, true); | |
1082 | link_add_rrs(l, false); | |
1083 | } | |
1084 | } | |
1085 | ||
1086 | int manager_next_hostname(Manager *m) { | |
1087 | const char *p; | |
1088 | uint64_t u, a; | |
1089 | char *h, *k; | |
1090 | int r; | |
1091 | ||
1092 | assert(m); | |
1093 | ||
1094 | p = strchr(m->llmnr_hostname, 0); | |
1095 | assert(p); | |
1096 | ||
1097 | while (p > m->llmnr_hostname) { | |
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 | ||
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; | |
1116 | ||
1117 | if (asprintf(&h, "%.*s%" PRIu64, (int) (p - m->llmnr_hostname), m->llmnr_hostname, u) < 0) | |
1118 | return -ENOMEM; | |
1119 | ||
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; | |
1130 | ||
1131 | free(m->mdns_hostname); | |
1132 | m->mdns_hostname = k; | |
1133 | ||
1134 | manager_refresh_rrs(m); | |
1135 | ||
1136 | return 0; | |
1137 | } | |
1138 | ||
1139 | LinkAddress* manager_find_link_address(Manager *m, int family, const union in_addr_union *in_addr) { | |
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 | ||
1156 | bool manager_our_packet(Manager *m, DnsPacket *p) { | |
1157 | assert(m); | |
1158 | assert(p); | |
1159 | ||
1160 | return !!manager_find_link_address(m, p->family, &p->sender); | |
1161 | } | |
1162 | ||
1163 | DnsScope* 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 | ||
1173 | switch (p->protocol) { | |
1174 | case DNS_PROTOCOL_LLMNR: | |
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; | |
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; | |
1192 | } | |
1193 | ||
1194 | return NULL; | |
1195 | } | |
1196 | ||
1197 | void 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 | ||
1206 | int manager_is_own_hostname(Manager *m, const char *name) { | |
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 | ||
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); | |
1226 | ||
1227 | return 0; | |
1228 | } | |
1229 | ||
1230 | int 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 | ||
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 | */ | |
1282 | int manager_compile_search_domains(Manager *m, OrderedSet **domains, int filter_route) { | |
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) { | |
1296 | ||
1297 | if (filter_route >= 0 && | |
1298 | d->route_only != !!filter_route) | |
1299 | continue; | |
1300 | ||
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) { | |
1311 | ||
1312 | if (filter_route >= 0 && | |
1313 | d->route_only != !!filter_route) | |
1314 | continue; | |
1315 | ||
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 | } | |
1326 | ||
1327 | DnssecMode 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 | ||
1336 | bool 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 | } | |
1356 | ||
1357 | void 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) { | |
1363 | char s[DNS_RESOURCE_KEY_STRING_MAX]; | |
1364 | ||
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)); | |
1368 | } | |
1369 | ||
1370 | m->n_dnssec_verdict[verdict]++; | |
1371 | } | |
1372 | ||
1373 | bool 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 | } | |
1387 | ||
1388 | void 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); | |
1395 | ||
1396 | log_info("Flushed all caches."); | |
1397 | } | |
1398 | ||
1399 | void manager_reset_server_features(Manager *m) { | |
1400 | Iterator i; | |
1401 | Link *l; | |
1402 | ||
1403 | dns_server_reset_features_all(m->dns_servers); | |
1404 | dns_server_reset_features_all(m->fallback_dns_servers); | |
1405 | ||
1406 | HASHMAP_FOREACH(l, m->links, i) | |
1407 | dns_server_reset_features_all(l->dns_servers); | |
1408 | ||
1409 | log_info("Resetting learnt feature levels on all servers."); | |
1410 | } | |
1411 | ||
1412 | void manager_cleanup_saved_user(Manager *m) { | |
1413 | _cleanup_closedir_ DIR *d = NULL; | |
1414 | struct dirent *de; | |
1415 | int r; | |
1416 | ||
1417 | assert(m); | |
1418 | ||
1419 | /* Clean up all saved per-link files in /run/systemd/resolve/netif/ that don't have a matching interface | |
1420 | * anymore. These files are created to persist settings pushed in by the user via the bus, so that resolved can | |
1421 | * be restarted without losing this data. */ | |
1422 | ||
1423 | d = opendir("/run/systemd/resolve/netif/"); | |
1424 | if (!d) { | |
1425 | if (errno == ENOENT) | |
1426 | return; | |
1427 | ||
1428 | log_warning_errno(errno, "Failed to open interface directory: %m"); | |
1429 | return; | |
1430 | } | |
1431 | ||
1432 | FOREACH_DIRENT_ALL(de, d, log_error_errno(errno, "Failed to read interface directory: %m")) { | |
1433 | _cleanup_free_ char *p = NULL; | |
1434 | int ifindex; | |
1435 | Link *l; | |
1436 | ||
1437 | if (!IN_SET(de->d_type, DT_UNKNOWN, DT_REG)) | |
1438 | continue; | |
1439 | ||
1440 | if (dot_or_dot_dot(de->d_name)) | |
1441 | continue; | |
1442 | ||
1443 | r = parse_ifindex(de->d_name, &ifindex); | |
1444 | if (r < 0) /* Probably some temporary file from a previous run. Delete it */ | |
1445 | goto rm; | |
1446 | ||
1447 | l = hashmap_get(m->links, INT_TO_PTR(ifindex)); | |
1448 | if (!l) /* link vanished */ | |
1449 | goto rm; | |
1450 | ||
1451 | if (l->is_managed) /* now managed by networkd, hence the bus settings are useless */ | |
1452 | goto rm; | |
1453 | ||
1454 | continue; | |
1455 | ||
1456 | rm: | |
1457 | p = strappend("/run/systemd/resolve/netif/", de->d_name); | |
1458 | if (!p) { | |
1459 | log_oom(); | |
1460 | return; | |
1461 | } | |
1462 | ||
1463 | (void) unlink(p); | |
1464 | } | |
1465 | } |