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