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