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