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