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