]>
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 | 794 | l = recvmsg_safe(fd, &mh, 0); |
8add30a0 YW |
795 | if (l < 0) { |
796 | if (ERRNO_IS_TRANSIENT(l)) | |
797 | return 0; | |
3691bcf3 | 798 | return l; |
8add30a0 YW |
799 | } |
800 | if (l == 0) | |
801 | return 0; | |
091a364c | 802 | |
1716f6dc LP |
803 | assert(!(mh.msg_flags & MSG_TRUNC)); |
804 | ||
74b2466e | 805 | p->size = (size_t) l; |
091a364c | 806 | |
1716f6dc | 807 | p->family = sa.sa.sa_family; |
623a4c97 LP |
808 | p->ipproto = IPPROTO_UDP; |
809 | if (p->family == AF_INET) { | |
1716f6dc | 810 | p->sender.in = sa.in.sin_addr; |
623a4c97 LP |
811 | p->sender_port = be16toh(sa.in.sin_port); |
812 | } else if (p->family == AF_INET6) { | |
1716f6dc | 813 | p->sender.in6 = sa.in6.sin6_addr; |
623a4c97 LP |
814 | p->sender_port = be16toh(sa.in6.sin6_port); |
815 | p->ifindex = sa.in6.sin6_scope_id; | |
816 | } else | |
1716f6dc | 817 | return -EAFNOSUPPORT; |
74b2466e | 818 | |
5777c613 LP |
819 | p->timestamp = now(clock_boottime_or_monotonic()); |
820 | ||
2a1288ff | 821 | CMSG_FOREACH(cmsg, &mh) { |
74b2466e | 822 | |
1716f6dc LP |
823 | if (cmsg->cmsg_level == IPPROTO_IPV6) { |
824 | assert(p->family == AF_INET6); | |
74b2466e | 825 | |
1716f6dc | 826 | switch (cmsg->cmsg_type) { |
74b2466e | 827 | |
1716f6dc LP |
828 | case IPV6_PKTINFO: { |
829 | struct in6_pktinfo *i = (struct in6_pktinfo*) CMSG_DATA(cmsg); | |
74b2466e | 830 | |
623a4c97 LP |
831 | if (p->ifindex <= 0) |
832 | p->ifindex = i->ipi6_ifindex; | |
833 | ||
1716f6dc LP |
834 | p->destination.in6 = i->ipi6_addr; |
835 | break; | |
836 | } | |
74b2466e | 837 | |
1716f6dc LP |
838 | case IPV6_HOPLIMIT: |
839 | p->ttl = *(int *) CMSG_DATA(cmsg); | |
840 | break; | |
74b2466e | 841 | |
20a001bd LP |
842 | case IPV6_RECVFRAGSIZE: |
843 | p->fragsize = *(int *) CMSG_DATA(cmsg); | |
844 | break; | |
1716f6dc LP |
845 | } |
846 | } else if (cmsg->cmsg_level == IPPROTO_IP) { | |
847 | assert(p->family == AF_INET); | |
74b2466e | 848 | |
1716f6dc | 849 | switch (cmsg->cmsg_type) { |
74b2466e | 850 | |
1716f6dc LP |
851 | case IP_PKTINFO: { |
852 | struct in_pktinfo *i = (struct in_pktinfo*) CMSG_DATA(cmsg); | |
091a364c | 853 | |
623a4c97 LP |
854 | if (p->ifindex <= 0) |
855 | p->ifindex = i->ipi_ifindex; | |
856 | ||
1716f6dc LP |
857 | p->destination.in = i->ipi_addr; |
858 | break; | |
859 | } | |
74b2466e | 860 | |
623a4c97 | 861 | case IP_TTL: |
1716f6dc LP |
862 | p->ttl = *(int *) CMSG_DATA(cmsg); |
863 | break; | |
20a001bd LP |
864 | |
865 | case IP_RECVFRAGSIZE: | |
866 | p->fragsize = *(int *) CMSG_DATA(cmsg); | |
867 | break; | |
1716f6dc LP |
868 | } |
869 | } | |
870 | } | |
74b2466e | 871 | |
623a4c97 LP |
872 | /* The Linux kernel sets the interface index to the loopback |
873 | * device if the packet came from the local host since it | |
874 | * avoids the routing table in such a case. Let's unset the | |
875 | * interface index in such a case. */ | |
a5f03596 | 876 | if (p->ifindex == LOOPBACK_IFINDEX) |
623a4c97 LP |
877 | p->ifindex = 0; |
878 | ||
86ad4cd7 TG |
879 | if (protocol != DNS_PROTOCOL_DNS) { |
880 | /* If we don't know the interface index still, we look for the | |
881 | * first local interface with a matching address. Yuck! */ | |
882 | if (p->ifindex <= 0) | |
883 | p->ifindex = manager_find_ifindex(m, p->family, &p->destination); | |
884 | } | |
623a4c97 | 885 | |
f96b500e LP |
886 | if (DEBUG_LOGGING) { |
887 | _cleanup_free_ char *sender_address = NULL, *destination_address = NULL; | |
888 | ||
889 | (void) in_addr_to_string(p->family, &p->sender, &sender_address); | |
890 | (void) in_addr_to_string(p->family, &p->destination, &destination_address); | |
891 | ||
892 | log_debug("Received %s UDP packet of size %zu, ifindex=%i, ttl=%i, fragsize=%zu, sender=%s, destination=%s", | |
893 | dns_protocol_to_string(protocol), p->size, p->ifindex, p->ttl, p->fragsize, | |
894 | strna(sender_address), strna(destination_address)); | |
895 | } | |
74b2466e | 896 | |
f7155840 | 897 | *ret = TAKE_PTR(p); |
74b2466e LP |
898 | return 1; |
899 | } | |
900 | ||
74b2466e LP |
901 | static int sendmsg_loop(int fd, struct msghdr *mh, int flags) { |
902 | int r; | |
903 | ||
904 | assert(fd >= 0); | |
905 | assert(mh); | |
906 | ||
907 | for (;;) { | |
908 | if (sendmsg(fd, mh, flags) >= 0) | |
909 | return 0; | |
910 | ||
911 | if (errno == EINTR) | |
912 | continue; | |
913 | ||
914 | if (errno != EAGAIN) | |
915 | return -errno; | |
916 | ||
917 | r = fd_wait_for_event(fd, POLLOUT, SEND_TIMEOUT_USEC); | |
918 | if (r < 0) | |
919 | return r; | |
920 | if (r == 0) | |
921 | return -ETIMEDOUT; | |
922 | } | |
923 | } | |
924 | ||
72290734 TG |
925 | static int write_loop(int fd, void *message, size_t length) { |
926 | int r; | |
927 | ||
928 | assert(fd >= 0); | |
929 | assert(message); | |
930 | ||
931 | for (;;) { | |
932 | if (write(fd, message, length) >= 0) | |
933 | return 0; | |
934 | ||
935 | if (errno == EINTR) | |
936 | continue; | |
937 | ||
938 | if (errno != EAGAIN) | |
939 | return -errno; | |
940 | ||
941 | r = fd_wait_for_event(fd, POLLOUT, SEND_TIMEOUT_USEC); | |
942 | if (r < 0) | |
943 | return r; | |
944 | if (r == 0) | |
945 | return -ETIMEDOUT; | |
946 | } | |
947 | } | |
948 | ||
949 | int manager_write(Manager *m, int fd, DnsPacket *p) { | |
950 | int r; | |
951 | ||
76f77229 LP |
952 | log_debug("Sending %s%s packet with id %" PRIu16 " of size %zu.", |
953 | DNS_PACKET_TC(p) ? "truncated (!) " : "", | |
954 | DNS_PACKET_QR(p) ? "response" : "query", | |
955 | DNS_PACKET_ID(p), | |
956 | p->size); | |
72290734 TG |
957 | |
958 | r = write_loop(fd, DNS_PACKET_DATA(p), p->size); | |
959 | if (r < 0) | |
960 | return r; | |
961 | ||
962 | return 0; | |
963 | } | |
964 | ||
b30bf55d LP |
965 | static int manager_ipv4_send( |
966 | Manager *m, | |
967 | int fd, | |
968 | int ifindex, | |
969 | const struct in_addr *destination, | |
970 | uint16_t port, | |
971 | const struct in_addr *source, | |
972 | DnsPacket *p) { | |
fb29cdbe LP |
973 | |
974 | CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct in_pktinfo))) control = {}; | |
f55f2dce | 975 | union sockaddr_union sa; |
74b2466e | 976 | struct iovec iov; |
f55f2dce YW |
977 | struct msghdr mh = { |
978 | .msg_iov = &iov, | |
979 | .msg_iovlen = 1, | |
980 | .msg_name = &sa.sa, | |
981 | .msg_namelen = sizeof(sa.in), | |
982 | }; | |
74b2466e LP |
983 | |
984 | assert(m); | |
1716f6dc | 985 | assert(fd >= 0); |
b30bf55d | 986 | assert(destination); |
1716f6dc | 987 | assert(port > 0); |
74b2466e LP |
988 | assert(p); |
989 | ||
cb310866 | 990 | iov = IOVEC_MAKE(DNS_PACKET_DATA(p), p->size); |
091a364c | 991 | |
f55f2dce YW |
992 | sa = (union sockaddr_union) { |
993 | .in.sin_family = AF_INET, | |
994 | .in.sin_addr = *destination, | |
995 | .in.sin_port = htobe16(port), | |
996 | }; | |
091a364c | 997 | |
74b2466e LP |
998 | if (ifindex > 0) { |
999 | struct cmsghdr *cmsg; | |
1000 | struct in_pktinfo *pi; | |
1001 | ||
1716f6dc | 1002 | mh.msg_control = &control; |
a258f491 | 1003 | mh.msg_controllen = sizeof(control); |
74b2466e LP |
1004 | |
1005 | cmsg = CMSG_FIRSTHDR(&mh); | |
a258f491 | 1006 | cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo)); |
74b2466e LP |
1007 | cmsg->cmsg_level = IPPROTO_IP; |
1008 | cmsg->cmsg_type = IP_PKTINFO; | |
1009 | ||
1010 | pi = (struct in_pktinfo*) CMSG_DATA(cmsg); | |
1011 | pi->ipi_ifindex = ifindex; | |
b30bf55d LP |
1012 | |
1013 | if (source) | |
1014 | pi->ipi_spec_dst = *source; | |
74b2466e LP |
1015 | } |
1016 | ||
1017 | return sendmsg_loop(fd, &mh, 0); | |
1018 | } | |
1019 | ||
b30bf55d LP |
1020 | static int manager_ipv6_send( |
1021 | Manager *m, | |
1022 | int fd, | |
1023 | int ifindex, | |
1024 | const struct in6_addr *destination, | |
1025 | uint16_t port, | |
1026 | const struct in6_addr *source, | |
1027 | DnsPacket *p) { | |
1028 | ||
fb29cdbe | 1029 | CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct in6_pktinfo))) control = {}; |
f55f2dce | 1030 | union sockaddr_union sa; |
74b2466e | 1031 | struct iovec iov; |
f55f2dce YW |
1032 | struct msghdr mh = { |
1033 | .msg_iov = &iov, | |
1034 | .msg_iovlen = 1, | |
1035 | .msg_name = &sa.sa, | |
1036 | .msg_namelen = sizeof(sa.in6), | |
1037 | }; | |
74b2466e LP |
1038 | |
1039 | assert(m); | |
1716f6dc | 1040 | assert(fd >= 0); |
b30bf55d | 1041 | assert(destination); |
1716f6dc | 1042 | assert(port > 0); |
74b2466e LP |
1043 | assert(p); |
1044 | ||
cb310866 | 1045 | iov = IOVEC_MAKE(DNS_PACKET_DATA(p), p->size); |
74b2466e | 1046 | |
f55f2dce YW |
1047 | sa = (union sockaddr_union) { |
1048 | .in6.sin6_family = AF_INET6, | |
1049 | .in6.sin6_addr = *destination, | |
1050 | .in6.sin6_port = htobe16(port), | |
1051 | .in6.sin6_scope_id = ifindex, | |
1052 | }; | |
74b2466e LP |
1053 | |
1054 | if (ifindex > 0) { | |
1055 | struct cmsghdr *cmsg; | |
1056 | struct in6_pktinfo *pi; | |
1057 | ||
1716f6dc | 1058 | mh.msg_control = &control; |
a258f491 | 1059 | mh.msg_controllen = sizeof(control); |
74b2466e LP |
1060 | |
1061 | cmsg = CMSG_FIRSTHDR(&mh); | |
a258f491 | 1062 | cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); |
74b2466e LP |
1063 | cmsg->cmsg_level = IPPROTO_IPV6; |
1064 | cmsg->cmsg_type = IPV6_PKTINFO; | |
1065 | ||
1066 | pi = (struct in6_pktinfo*) CMSG_DATA(cmsg); | |
1067 | pi->ipi6_ifindex = ifindex; | |
b30bf55d LP |
1068 | |
1069 | if (source) | |
1070 | pi->ipi6_addr = *source; | |
74b2466e LP |
1071 | } |
1072 | ||
1073 | return sendmsg_loop(fd, &mh, 0); | |
1074 | } | |
1075 | ||
b30bf55d LP |
1076 | int manager_send( |
1077 | Manager *m, | |
1078 | int fd, | |
1079 | int ifindex, | |
1080 | int family, | |
1081 | const union in_addr_union *destination, | |
1082 | uint16_t port, | |
1083 | const union in_addr_union *source, | |
1084 | DnsPacket *p) { | |
1085 | ||
1716f6dc LP |
1086 | assert(m); |
1087 | assert(fd >= 0); | |
b30bf55d | 1088 | assert(destination); |
1716f6dc LP |
1089 | assert(port > 0); |
1090 | assert(p); | |
1091 | ||
76f77229 LP |
1092 | log_debug("Sending %s%s packet with id %" PRIu16 " on interface %i/%s of size %zu.", |
1093 | DNS_PACKET_TC(p) ? "truncated (!) " : "", | |
1094 | DNS_PACKET_QR(p) ? "response" : "query", | |
1095 | DNS_PACKET_ID(p), | |
1096 | ifindex, af_to_name(family), | |
1097 | p->size); | |
a2a416f7 | 1098 | |
1716f6dc | 1099 | if (family == AF_INET) |
25270cf3 | 1100 | return manager_ipv4_send(m, fd, ifindex, &destination->in, port, source ? &source->in : NULL, p); |
2817157b | 1101 | if (family == AF_INET6) |
25270cf3 | 1102 | return manager_ipv6_send(m, fd, ifindex, &destination->in6, port, source ? &source->in6 : NULL, p); |
1716f6dc LP |
1103 | |
1104 | return -EAFNOSUPPORT; | |
1105 | } | |
1106 | ||
e1c95994 LP |
1107 | uint32_t manager_find_mtu(Manager *m) { |
1108 | uint32_t mtu = 0; | |
1109 | Link *l; | |
e1c95994 | 1110 | |
5a0d0b8f | 1111 | /* If we don't know on which link a DNS packet would be delivered, let's find the largest MTU that |
4301cb32 | 1112 | * works on all interfaces we know of that have an IP address associated */ |
e1c95994 | 1113 | |
90e74a66 | 1114 | HASHMAP_FOREACH(l, m->links) { |
5a0d0b8f LP |
1115 | /* Let's filter out links without IP addresses (e.g. AF_CAN links and suchlike) */ |
1116 | if (!l->addresses) | |
1117 | continue; | |
1118 | ||
1119 | /* Safety check: MTU shorter than what we need for the absolutely shortest DNS request? Then | |
1120 | * let's ignore this link. */ | |
1121 | if (l->mtu < MIN(UDP4_PACKET_HEADER_SIZE + DNS_PACKET_HEADER_SIZE, | |
1122 | UDP6_PACKET_HEADER_SIZE + DNS_PACKET_HEADER_SIZE)) | |
e1c95994 LP |
1123 | continue; |
1124 | ||
1125 | if (mtu <= 0 || l->mtu < mtu) | |
1126 | mtu = l->mtu; | |
1127 | } | |
1128 | ||
5a0d0b8f LP |
1129 | if (mtu == 0) /* found nothing? then let's assume the typical Ethernet MTU for lack of anything more precise */ |
1130 | return 1500; | |
1131 | ||
e1c95994 LP |
1132 | return mtu; |
1133 | } | |
1716f6dc | 1134 | |
623a4c97 | 1135 | int manager_find_ifindex(Manager *m, int family, const union in_addr_union *in_addr) { |
ec2c5e43 LP |
1136 | LinkAddress *a; |
1137 | ||
1138 | assert(m); | |
1139 | ||
bb3b08ad YW |
1140 | if (!IN_SET(family, AF_INET, AF_INET6)) |
1141 | return 0; | |
1142 | ||
1143 | if (!in_addr) | |
1144 | return 0; | |
1145 | ||
4e945a6f | 1146 | a = manager_find_link_address(m, family, in_addr); |
ec2c5e43 LP |
1147 | if (a) |
1148 | return a->link->ifindex; | |
1149 | ||
1150 | return 0; | |
1151 | } | |
1152 | ||
eb60f9cd | 1153 | void manager_refresh_rrs(Manager *m) { |
eb60f9cd | 1154 | Link *l; |
6db6a464 | 1155 | DnssdService *s; |
eb60f9cd LP |
1156 | |
1157 | assert(m); | |
1158 | ||
78c6a153 LP |
1159 | m->llmnr_host_ipv4_key = dns_resource_key_unref(m->llmnr_host_ipv4_key); |
1160 | m->llmnr_host_ipv6_key = dns_resource_key_unref(m->llmnr_host_ipv6_key); | |
400cb36e DR |
1161 | m->mdns_host_ipv4_key = dns_resource_key_unref(m->mdns_host_ipv4_key); |
1162 | m->mdns_host_ipv6_key = dns_resource_key_unref(m->mdns_host_ipv6_key); | |
eb60f9cd | 1163 | |
ee3713b7 RB |
1164 | HASHMAP_FOREACH(l, m->links) |
1165 | link_add_rrs(l, true); | |
1166 | ||
6db6a464 | 1167 | if (m->mdns_support == RESOLVE_SUPPORT_YES) |
90e74a66 | 1168 | HASHMAP_FOREACH(s, m->dnssd_services) |
6db6a464 DR |
1169 | if (dnssd_update_rrs(s) < 0) |
1170 | log_warning("Failed to refresh DNS-SD service '%s'", s->name); | |
1171 | ||
ee3713b7 | 1172 | HASHMAP_FOREACH(l, m->links) |
eb60f9cd | 1173 | link_add_rrs(l, false); |
eb60f9cd LP |
1174 | } |
1175 | ||
e7c1b0e4 | 1176 | static int manager_next_random_name(const char *old, char **ret_new) { |
ec2c5e43 | 1177 | const char *p; |
556a2294 | 1178 | uint64_t u, a; |
e7c1b0e4 | 1179 | char *n; |
623a4c97 | 1180 | |
e7c1b0e4 | 1181 | p = strchr(old, 0); |
ec2c5e43 LP |
1182 | assert(p); |
1183 | ||
e7c1b0e4 DR |
1184 | while (p > old) { |
1185 | if (!strchr(DIGITS, p[-1])) | |
ec2c5e43 LP |
1186 | break; |
1187 | ||
1188 | p--; | |
1189 | } | |
1190 | ||
1191 | if (*p == 0 || safe_atou64(p, &u) < 0 || u <= 0) | |
1192 | u = 1; | |
1193 | ||
556a2294 LP |
1194 | /* Add a random number to the old value. This way we can avoid |
1195 | * that two hosts pick the same hostname, win on IPv4 and lose | |
1196 | * on IPv6 (or vice versa), and pick the same hostname | |
1197 | * replacement hostname, ad infinitum. We still want the | |
1198 | * numbers to go up monotonically, hence we just add a random | |
1199 | * value 1..10 */ | |
1200 | ||
1201 | random_bytes(&a, sizeof(a)); | |
1202 | u += 1 + a % 10; | |
ec2c5e43 | 1203 | |
e7c1b0e4 | 1204 | if (asprintf(&n, "%.*s%" PRIu64, (int) (p - old), old, u) < 0) |
ec2c5e43 LP |
1205 | return -ENOMEM; |
1206 | ||
e7c1b0e4 DR |
1207 | *ret_new = n; |
1208 | ||
1209 | return 0; | |
1210 | } | |
1211 | ||
1212 | int manager_next_hostname(Manager *m) { | |
1213 | _cleanup_free_ char *h = NULL, *k = NULL; | |
1214 | int r; | |
1215 | ||
1216 | assert(m); | |
1217 | ||
1218 | r = manager_next_random_name(m->llmnr_hostname, &h); | |
1219 | if (r < 0) | |
1220 | return r; | |
1221 | ||
7470cc4c | 1222 | r = dns_name_concat(h, "local", 0, &k); |
e7c1b0e4 | 1223 | if (r < 0) |
78c6a153 | 1224 | return r; |
78c6a153 LP |
1225 | |
1226 | log_info("Hostname conflict, changing published hostname from '%s' to '%s'.", m->llmnr_hostname, h); | |
1227 | ||
e7c1b0e4 DR |
1228 | free_and_replace(m->llmnr_hostname, h); |
1229 | free_and_replace(m->mdns_hostname, k); | |
ec2c5e43 | 1230 | |
eb60f9cd | 1231 | manager_refresh_rrs(m); |
86c0411e | 1232 | (void) manager_send_changed(m, "LLMNRHostname"); |
623a4c97 LP |
1233 | |
1234 | return 0; | |
1235 | } | |
ec2c5e43 | 1236 | |
4e945a6f | 1237 | LinkAddress* manager_find_link_address(Manager *m, int family, const union in_addr_union *in_addr) { |
ec2c5e43 LP |
1238 | Link *l; |
1239 | ||
1240 | assert(m); | |
1241 | ||
bb3b08ad YW |
1242 | if (!IN_SET(family, AF_INET, AF_INET6)) |
1243 | return NULL; | |
1244 | ||
1245 | if (!in_addr) | |
1246 | return NULL; | |
1247 | ||
90e74a66 | 1248 | HASHMAP_FOREACH(l, m->links) { |
ec2c5e43 LP |
1249 | LinkAddress *a; |
1250 | ||
1251 | a = link_find_address(l, family, in_addr); | |
1252 | if (a) | |
1253 | return a; | |
1254 | } | |
1255 | ||
1256 | return NULL; | |
1257 | } | |
1258 | ||
94378145 | 1259 | bool manager_packet_from_local_address(Manager *m, DnsPacket *p) { |
ec2c5e43 LP |
1260 | assert(m); |
1261 | assert(p); | |
1262 | ||
94378145 LP |
1263 | /* Let's see if this packet comes from an IP address we have on any local interface */ |
1264 | ||
4e945a6f | 1265 | return !!manager_find_link_address(m, p->family, &p->sender); |
ec2c5e43 | 1266 | } |
4e945a6f | 1267 | |
a9fd8837 LP |
1268 | bool manager_packet_from_our_transaction(Manager *m, DnsPacket *p) { |
1269 | DnsTransaction *t; | |
1270 | ||
1271 | assert(m); | |
1272 | assert(p); | |
1273 | ||
1274 | /* Let's see if we have a transaction with a query message with the exact same binary contents as the | |
1275 | * one we just got. If so, it's almost definitely a packet loop of some kind. */ | |
1276 | ||
1277 | t = hashmap_get(m->dns_transactions, UINT_TO_PTR(DNS_PACKET_ID(p))); | |
1278 | if (!t) | |
1279 | return false; | |
1280 | ||
1281 | return t->sent && dns_packet_equal(t->sent, p); | |
1282 | } | |
1283 | ||
a4076574 LP |
1284 | DnsScope* manager_find_scope(Manager *m, DnsPacket *p) { |
1285 | Link *l; | |
1286 | ||
1287 | assert(m); | |
1288 | assert(p); | |
1289 | ||
1290 | l = hashmap_get(m->links, INT_TO_PTR(p->ifindex)); | |
1291 | if (!l) | |
1292 | return NULL; | |
1293 | ||
b4f1862d DM |
1294 | switch (p->protocol) { |
1295 | case DNS_PROTOCOL_LLMNR: | |
a4076574 LP |
1296 | if (p->family == AF_INET) |
1297 | return l->llmnr_ipv4_scope; | |
1298 | else if (p->family == AF_INET6) | |
1299 | return l->llmnr_ipv6_scope; | |
b4f1862d DM |
1300 | |
1301 | break; | |
1302 | ||
1303 | case DNS_PROTOCOL_MDNS: | |
1304 | if (p->family == AF_INET) | |
1305 | return l->mdns_ipv4_scope; | |
1306 | else if (p->family == AF_INET6) | |
1307 | return l->mdns_ipv6_scope; | |
1308 | ||
1309 | break; | |
1310 | ||
1311 | default: | |
1312 | break; | |
a4076574 LP |
1313 | } |
1314 | ||
1315 | return NULL; | |
1316 | } | |
1317 | ||
902bb5d8 LP |
1318 | void manager_verify_all(Manager *m) { |
1319 | DnsScope *s; | |
1320 | ||
1321 | assert(m); | |
1322 | ||
1323 | LIST_FOREACH(scopes, s, m->dns_scopes) | |
1324 | dns_zone_verify_all(&s->zone); | |
1325 | } | |
1326 | ||
78c6a153 | 1327 | int manager_is_own_hostname(Manager *m, const char *name) { |
78c6a153 LP |
1328 | int r; |
1329 | ||
1330 | assert(m); | |
1331 | assert(name); | |
1332 | ||
1333 | if (m->llmnr_hostname) { | |
1334 | r = dns_name_equal(name, m->llmnr_hostname); | |
1335 | if (r != 0) | |
1336 | return r; | |
1337 | } | |
1338 | ||
e96de0ce LP |
1339 | if (m->mdns_hostname) { |
1340 | r = dns_name_equal(name, m->mdns_hostname); | |
1341 | if (r != 0) | |
1342 | return r; | |
1343 | } | |
1344 | ||
1345 | if (m->full_hostname) | |
1346 | return dns_name_equal(name, m->full_hostname); | |
78c6a153 LP |
1347 | |
1348 | return 0; | |
1349 | } | |
1350 | ||
9176a57c LP |
1351 | int manager_compile_dns_servers(Manager *m, OrderedSet **dns) { |
1352 | DnsServer *s; | |
9176a57c LP |
1353 | Link *l; |
1354 | int r; | |
1355 | ||
1356 | assert(m); | |
1357 | assert(dns); | |
1358 | ||
1359 | r = ordered_set_ensure_allocated(dns, &dns_server_hash_ops); | |
1360 | if (r < 0) | |
1361 | return r; | |
1362 | ||
1363 | /* First add the system-wide servers and domains */ | |
1364 | LIST_FOREACH(servers, s, m->dns_servers) { | |
1365 | r = ordered_set_put(*dns, s); | |
1366 | if (r == -EEXIST) | |
1367 | continue; | |
1368 | if (r < 0) | |
1369 | return r; | |
1370 | } | |
1371 | ||
1372 | /* Then, add the per-link servers */ | |
90e74a66 | 1373 | HASHMAP_FOREACH(l, m->links) { |
9176a57c LP |
1374 | LIST_FOREACH(servers, s, l->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 | /* If we found nothing, add the fallback servers */ | |
1384 | if (ordered_set_isempty(*dns)) { | |
1385 | LIST_FOREACH(servers, s, m->fallback_dns_servers) { | |
1386 | r = ordered_set_put(*dns, s); | |
1387 | if (r == -EEXIST) | |
1388 | continue; | |
1389 | if (r < 0) | |
1390 | return r; | |
1391 | } | |
1392 | } | |
1393 | ||
1394 | return 0; | |
1395 | } | |
1396 | ||
94363cbb MP |
1397 | /* filter_route is a tri-state: |
1398 | * < 0: no filtering | |
1399 | * = 0 or false: return only domains which should be used for searching | |
1400 | * > 0 or true: return only domains which are for routing only | |
1401 | */ | |
6f7da49d | 1402 | int manager_compile_search_domains(Manager *m, OrderedSet **domains, int filter_route) { |
9176a57c | 1403 | DnsSearchDomain *d; |
9176a57c LP |
1404 | Link *l; |
1405 | int r; | |
1406 | ||
1407 | assert(m); | |
1408 | assert(domains); | |
1409 | ||
1410 | r = ordered_set_ensure_allocated(domains, &dns_name_hash_ops); | |
1411 | if (r < 0) | |
1412 | return r; | |
1413 | ||
1414 | LIST_FOREACH(domains, d, m->search_domains) { | |
6f7da49d LP |
1415 | |
1416 | if (filter_route >= 0 && | |
1417 | d->route_only != !!filter_route) | |
1418 | continue; | |
1419 | ||
9176a57c LP |
1420 | r = ordered_set_put(*domains, d->name); |
1421 | if (r == -EEXIST) | |
1422 | continue; | |
1423 | if (r < 0) | |
1424 | return r; | |
1425 | } | |
1426 | ||
90e74a66 | 1427 | HASHMAP_FOREACH(l, m->links) { |
9176a57c LP |
1428 | |
1429 | LIST_FOREACH(domains, d, l->search_domains) { | |
6f7da49d LP |
1430 | |
1431 | if (filter_route >= 0 && | |
1432 | d->route_only != !!filter_route) | |
1433 | continue; | |
1434 | ||
9176a57c LP |
1435 | r = ordered_set_put(*domains, d->name); |
1436 | if (r == -EEXIST) | |
1437 | continue; | |
1438 | if (r < 0) | |
1439 | return r; | |
1440 | } | |
1441 | } | |
1442 | ||
1443 | return 0; | |
1444 | } | |
c69fa7e3 LP |
1445 | |
1446 | DnssecMode manager_get_dnssec_mode(Manager *m) { | |
1447 | assert(m); | |
1448 | ||
1449 | if (m->dnssec_mode != _DNSSEC_MODE_INVALID) | |
1450 | return m->dnssec_mode; | |
1451 | ||
1452 | return DNSSEC_NO; | |
1453 | } | |
1454 | ||
1455 | bool manager_dnssec_supported(Manager *m) { | |
1456 | DnsServer *server; | |
c69fa7e3 LP |
1457 | Link *l; |
1458 | ||
1459 | assert(m); | |
1460 | ||
1461 | if (manager_get_dnssec_mode(m) == DNSSEC_NO) | |
1462 | return false; | |
1463 | ||
1464 | server = manager_get_dns_server(m); | |
1465 | if (server && !dns_server_dnssec_supported(server)) | |
1466 | return false; | |
1467 | ||
90e74a66 | 1468 | HASHMAP_FOREACH(l, m->links) |
c69fa7e3 LP |
1469 | if (!link_dnssec_supported(l)) |
1470 | return false; | |
1471 | ||
1472 | return true; | |
1473 | } | |
59c5b597 | 1474 | |
c9299be2 | 1475 | DnsOverTlsMode manager_get_dns_over_tls_mode(Manager *m) { |
5d67a7ae IT |
1476 | assert(m); |
1477 | ||
c9299be2 IT |
1478 | if (m->dns_over_tls_mode != _DNS_OVER_TLS_MODE_INVALID) |
1479 | return m->dns_over_tls_mode; | |
5d67a7ae | 1480 | |
c9299be2 | 1481 | return DNS_OVER_TLS_NO; |
5d67a7ae IT |
1482 | } |
1483 | ||
59c5b597 LP |
1484 | void manager_dnssec_verdict(Manager *m, DnssecVerdict verdict, const DnsResourceKey *key) { |
1485 | ||
1486 | assert(verdict >= 0); | |
1487 | assert(verdict < _DNSSEC_VERDICT_MAX); | |
1488 | ||
f1d34068 | 1489 | if (DEBUG_LOGGING) { |
202b76ae | 1490 | char s[DNS_RESOURCE_KEY_STRING_MAX]; |
59c5b597 | 1491 | |
202b76ae ZJS |
1492 | log_debug("Found verdict for lookup %s: %s", |
1493 | dns_resource_key_to_string(key, s, sizeof s), | |
1494 | dnssec_verdict_to_string(verdict)); | |
59c5b597 LP |
1495 | } |
1496 | ||
1497 | m->n_dnssec_verdict[verdict]++; | |
1498 | } | |
011696f7 | 1499 | |
de4a0138 | 1500 | bool manager_routable(Manager *m) { |
011696f7 LP |
1501 | Link *l; |
1502 | ||
1503 | assert(m); | |
1504 | ||
de4a0138 | 1505 | /* Returns true if the host has at least one interface with a routable address (regardless if IPv4 or IPv6) */ |
011696f7 | 1506 | |
90e74a66 | 1507 | HASHMAP_FOREACH(l, m->links) |
de4a0138 | 1508 | if (link_relevant(l, AF_UNSPEC, false)) |
011696f7 LP |
1509 | return true; |
1510 | ||
1511 | return false; | |
1512 | } | |
ba35662f | 1513 | |
90df0fbe | 1514 | void manager_flush_caches(Manager *m, int log_level) { |
ba35662f LP |
1515 | DnsScope *scope; |
1516 | ||
1517 | assert(m); | |
1518 | ||
1519 | LIST_FOREACH(scopes, scope, m->dns_scopes) | |
1520 | dns_cache_flush(&scope->cache); | |
aac57b35 | 1521 | |
90df0fbe | 1522 | log_full(log_level, "Flushed all caches."); |
ba35662f | 1523 | } |
943ef07c | 1524 | |
59c0fd0e | 1525 | void manager_reset_server_features(Manager *m) { |
59c0fd0e LP |
1526 | Link *l; |
1527 | ||
1528 | dns_server_reset_features_all(m->dns_servers); | |
1529 | dns_server_reset_features_all(m->fallback_dns_servers); | |
1530 | ||
90e74a66 | 1531 | HASHMAP_FOREACH(l, m->links) |
59c0fd0e LP |
1532 | dns_server_reset_features_all(l->dns_servers); |
1533 | ||
1534 | log_info("Resetting learnt feature levels on all servers."); | |
1535 | } | |
1536 | ||
943ef07c LP |
1537 | void manager_cleanup_saved_user(Manager *m) { |
1538 | _cleanup_closedir_ DIR *d = NULL; | |
943ef07c LP |
1539 | |
1540 | assert(m); | |
1541 | ||
1542 | /* Clean up all saved per-link files in /run/systemd/resolve/netif/ that don't have a matching interface | |
1543 | * anymore. These files are created to persist settings pushed in by the user via the bus, so that resolved can | |
1544 | * be restarted without losing this data. */ | |
1545 | ||
1546 | d = opendir("/run/systemd/resolve/netif/"); | |
1547 | if (!d) { | |
1548 | if (errno == ENOENT) | |
1549 | return; | |
1550 | ||
1551 | log_warning_errno(errno, "Failed to open interface directory: %m"); | |
1552 | return; | |
1553 | } | |
1554 | ||
1555 | FOREACH_DIRENT_ALL(de, d, log_error_errno(errno, "Failed to read interface directory: %m")) { | |
1556 | _cleanup_free_ char *p = NULL; | |
1557 | int ifindex; | |
1558 | Link *l; | |
1559 | ||
1560 | if (!IN_SET(de->d_type, DT_UNKNOWN, DT_REG)) | |
1561 | continue; | |
1562 | ||
49bfc877 | 1563 | if (dot_or_dot_dot(de->d_name)) |
943ef07c LP |
1564 | continue; |
1565 | ||
597da51b ZJS |
1566 | ifindex = parse_ifindex(de->d_name); |
1567 | if (ifindex < 0) /* Probably some temporary file from a previous run. Delete it */ | |
943ef07c LP |
1568 | goto rm; |
1569 | ||
1570 | l = hashmap_get(m->links, INT_TO_PTR(ifindex)); | |
1571 | if (!l) /* link vanished */ | |
1572 | goto rm; | |
1573 | ||
1574 | if (l->is_managed) /* now managed by networkd, hence the bus settings are useless */ | |
1575 | goto rm; | |
1576 | ||
1577 | continue; | |
1578 | ||
1579 | rm: | |
b910cc72 | 1580 | p = path_join("/run/systemd/resolve/netif", de->d_name); |
943ef07c LP |
1581 | if (!p) { |
1582 | log_oom(); | |
1583 | return; | |
1584 | } | |
1585 | ||
1586 | (void) unlink(p); | |
1587 | } | |
1588 | } | |
e7c1b0e4 DR |
1589 | |
1590 | bool manager_next_dnssd_names(Manager *m) { | |
e7c1b0e4 DR |
1591 | DnssdService *s; |
1592 | bool tried = false; | |
1593 | int r; | |
1594 | ||
1595 | assert(m); | |
1596 | ||
90e74a66 | 1597 | HASHMAP_FOREACH(s, m->dnssd_services) { |
e7c1b0e4 DR |
1598 | _cleanup_free_ char * new_name = NULL; |
1599 | ||
1600 | if (!s->withdrawn) | |
1601 | continue; | |
1602 | ||
1603 | r = manager_next_random_name(s->name_template, &new_name); | |
1604 | if (r < 0) { | |
1605 | log_warning_errno(r, "Failed to get new name for service '%s': %m", s->name); | |
1606 | continue; | |
1607 | } | |
1608 | ||
1609 | free_and_replace(s->name_template, new_name); | |
1610 | ||
1611 | s->withdrawn = false; | |
1612 | ||
1613 | tried = true; | |
1614 | } | |
1615 | ||
1616 | if (tried) | |
1617 | manager_refresh_rrs(m); | |
1618 | ||
1619 | return tried; | |
1620 | } | |
49ef064c | 1621 | |
0ad4efb1 | 1622 | bool manager_server_address_is_stub(Manager *m, int family, const union in_addr_union *address, uint16_t port) { |
49ef064c LP |
1623 | DnsStubListenerExtra *l; |
1624 | ||
1625 | assert(m); | |
0ad4efb1 | 1626 | assert(address); |
49ef064c LP |
1627 | |
1628 | /* Safety check: we generally already skip the main stub when parsing configuration. But let's be | |
1629 | * extra careful, and check here again */ | |
0ad4efb1 ZJS |
1630 | if (family == AF_INET && |
1631 | address->in.s_addr == htobe32(INADDR_DNS_STUB) && | |
1632 | port == 53) | |
49ef064c LP |
1633 | return true; |
1634 | ||
1635 | /* Main reason to call this is to check server data against the extra listeners, and filter things | |
1636 | * out. */ | |
1637 | ORDERED_SET_FOREACH(l, m->dns_extra_stub_listeners) | |
0ad4efb1 ZJS |
1638 | if (family == l->family && |
1639 | in_addr_equal(family, address, &l->address) && | |
1640 | port == dns_stub_listener_extra_port(l)) | |
49ef064c LP |
1641 | return true; |
1642 | ||
1643 | return false; | |
1644 | } | |
eb170e75 | 1645 | |
0ad4efb1 ZJS |
1646 | bool manager_server_is_stub(Manager *m, DnsServer *s) { |
1647 | assert(m); | |
1648 | assert(s); | |
1649 | ||
1650 | return manager_server_address_is_stub(m, s->family, &s->address, dns_server_port(s)); | |
1651 | } | |
1652 | ||
eb170e75 LP |
1653 | int socket_disable_pmtud(int fd, int af) { |
1654 | int r; | |
1655 | ||
1656 | assert(fd >= 0); | |
1657 | ||
1658 | if (af == AF_UNSPEC) { | |
1659 | r = socket_get_family(fd, &af); | |
1660 | if (r < 0) | |
1661 | return r; | |
1662 | } | |
1663 | ||
1664 | switch (af) { | |
1665 | ||
1666 | case AF_INET: { | |
1667 | /* Turn off path MTU discovery, let's rather fragment on the way than to open us up against | |
1668 | * PMTU forgery vulnerabilities. | |
1669 | * | |
1670 | * There appears to be no documentation about IP_PMTUDISC_OMIT, but it has the effect that | |
1671 | * the "Don't Fragment" bit in the IPv4 header is turned off, thus enforcing fragmentation if | |
1672 | * our datagram size exceeds the MTU of a router in the path, and turning off path MTU | |
1673 | * discovery. | |
1674 | * | |
1675 | * This helps mitigating the PMTUD vulnerability described here: | |
1676 | * | |
1677 | * https://blog.apnic.net/2019/07/12/its-time-to-consider-avoiding-ip-fragmentation-in-the-dns/ | |
1678 | * | |
1679 | * Similar logic is in place in most DNS servers. | |
1680 | * | |
1681 | * There are multiple conflicting goals: we want to allow the largest datagrams possible (for | |
1682 | * efficiency reasons), but not have fragmentation (for security reasons), nor use PMTUD (for | |
1683 | * security reasons, too). Our strategy to deal with this is: use large packets, turn off | |
1684 | * PMTUD, but watch fragmentation taking place, and then size our packets to the max of the | |
1685 | * fragments seen — and if we need larger packets always go to TCP. | |
1686 | */ | |
1687 | ||
1688 | r = setsockopt_int(fd, IPPROTO_IP, IP_MTU_DISCOVER, IP_PMTUDISC_OMIT); | |
1689 | if (r < 0) | |
1690 | return r; | |
1691 | ||
1692 | return 0; | |
1693 | } | |
1694 | ||
1695 | case AF_INET6: { | |
1696 | /* On IPv6 fragmentation only is done by the sender — never by routers on the path. PMTUD is | |
1697 | * mandatory. If we want to turn off PMTUD, the only way is by sending with minimal MTU only, | |
1698 | * so that we apply maximum fragmentation locally already, and thus PMTUD doesn't happen | |
1699 | * because there's nothing that could be fragmented further anymore. */ | |
1700 | ||
1701 | r = setsockopt_int(fd, IPPROTO_IPV6, IPV6_MTU, IPV6_MIN_MTU); | |
1702 | if (r < 0) | |
1703 | return r; | |
1704 | ||
1705 | return 0; | |
1706 | } | |
1707 | ||
1708 | default: | |
1709 | return -EAFNOSUPPORT; | |
1710 | } | |
1711 | } |