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