]>
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 LP |
1099 | |
1100 | /* If we don't know on which link a DNS packet would be | |
1101 | * delivered, let's find the largest MTU that works on all | |
1102 | * interfaces we know of */ | |
1103 | ||
90e74a66 | 1104 | HASHMAP_FOREACH(l, m->links) { |
e1c95994 LP |
1105 | if (l->mtu <= 0) |
1106 | continue; | |
1107 | ||
1108 | if (mtu <= 0 || l->mtu < mtu) | |
1109 | mtu = l->mtu; | |
1110 | } | |
1111 | ||
1112 | return mtu; | |
1113 | } | |
1716f6dc | 1114 | |
623a4c97 | 1115 | int manager_find_ifindex(Manager *m, int family, const union in_addr_union *in_addr) { |
ec2c5e43 LP |
1116 | LinkAddress *a; |
1117 | ||
1118 | assert(m); | |
1119 | ||
bb3b08ad YW |
1120 | if (!IN_SET(family, AF_INET, AF_INET6)) |
1121 | return 0; | |
1122 | ||
1123 | if (!in_addr) | |
1124 | return 0; | |
1125 | ||
4e945a6f | 1126 | a = manager_find_link_address(m, family, in_addr); |
ec2c5e43 LP |
1127 | if (a) |
1128 | return a->link->ifindex; | |
1129 | ||
1130 | return 0; | |
1131 | } | |
1132 | ||
eb60f9cd | 1133 | void manager_refresh_rrs(Manager *m) { |
eb60f9cd | 1134 | Link *l; |
6db6a464 | 1135 | DnssdService *s; |
eb60f9cd LP |
1136 | |
1137 | assert(m); | |
1138 | ||
78c6a153 LP |
1139 | m->llmnr_host_ipv4_key = dns_resource_key_unref(m->llmnr_host_ipv4_key); |
1140 | m->llmnr_host_ipv6_key = dns_resource_key_unref(m->llmnr_host_ipv6_key); | |
400cb36e DR |
1141 | m->mdns_host_ipv4_key = dns_resource_key_unref(m->mdns_host_ipv4_key); |
1142 | m->mdns_host_ipv6_key = dns_resource_key_unref(m->mdns_host_ipv6_key); | |
eb60f9cd | 1143 | |
6db6a464 | 1144 | if (m->mdns_support == RESOLVE_SUPPORT_YES) |
90e74a66 | 1145 | HASHMAP_FOREACH(s, m->dnssd_services) |
6db6a464 DR |
1146 | if (dnssd_update_rrs(s) < 0) |
1147 | log_warning("Failed to refresh DNS-SD service '%s'", s->name); | |
1148 | ||
90e74a66 | 1149 | HASHMAP_FOREACH(l, m->links) { |
eb60f9cd LP |
1150 | link_add_rrs(l, true); |
1151 | link_add_rrs(l, false); | |
1152 | } | |
1153 | } | |
1154 | ||
e7c1b0e4 | 1155 | static int manager_next_random_name(const char *old, char **ret_new) { |
ec2c5e43 | 1156 | const char *p; |
556a2294 | 1157 | uint64_t u, a; |
e7c1b0e4 | 1158 | char *n; |
623a4c97 | 1159 | |
e7c1b0e4 | 1160 | p = strchr(old, 0); |
ec2c5e43 LP |
1161 | assert(p); |
1162 | ||
e7c1b0e4 DR |
1163 | while (p > old) { |
1164 | if (!strchr(DIGITS, p[-1])) | |
ec2c5e43 LP |
1165 | break; |
1166 | ||
1167 | p--; | |
1168 | } | |
1169 | ||
1170 | if (*p == 0 || safe_atou64(p, &u) < 0 || u <= 0) | |
1171 | u = 1; | |
1172 | ||
556a2294 LP |
1173 | /* Add a random number to the old value. This way we can avoid |
1174 | * that two hosts pick the same hostname, win on IPv4 and lose | |
1175 | * on IPv6 (or vice versa), and pick the same hostname | |
1176 | * replacement hostname, ad infinitum. We still want the | |
1177 | * numbers to go up monotonically, hence we just add a random | |
1178 | * value 1..10 */ | |
1179 | ||
1180 | random_bytes(&a, sizeof(a)); | |
1181 | u += 1 + a % 10; | |
ec2c5e43 | 1182 | |
e7c1b0e4 | 1183 | if (asprintf(&n, "%.*s%" PRIu64, (int) (p - old), old, u) < 0) |
ec2c5e43 LP |
1184 | return -ENOMEM; |
1185 | ||
e7c1b0e4 DR |
1186 | *ret_new = n; |
1187 | ||
1188 | return 0; | |
1189 | } | |
1190 | ||
1191 | int manager_next_hostname(Manager *m) { | |
1192 | _cleanup_free_ char *h = NULL, *k = NULL; | |
1193 | int r; | |
1194 | ||
1195 | assert(m); | |
1196 | ||
1197 | r = manager_next_random_name(m->llmnr_hostname, &h); | |
1198 | if (r < 0) | |
1199 | return r; | |
1200 | ||
7470cc4c | 1201 | r = dns_name_concat(h, "local", 0, &k); |
e7c1b0e4 | 1202 | if (r < 0) |
78c6a153 | 1203 | return r; |
78c6a153 LP |
1204 | |
1205 | log_info("Hostname conflict, changing published hostname from '%s' to '%s'.", m->llmnr_hostname, h); | |
1206 | ||
e7c1b0e4 DR |
1207 | free_and_replace(m->llmnr_hostname, h); |
1208 | free_and_replace(m->mdns_hostname, k); | |
ec2c5e43 | 1209 | |
eb60f9cd | 1210 | manager_refresh_rrs(m); |
86c0411e | 1211 | (void) manager_send_changed(m, "LLMNRHostname"); |
623a4c97 LP |
1212 | |
1213 | return 0; | |
1214 | } | |
ec2c5e43 | 1215 | |
4e945a6f | 1216 | LinkAddress* manager_find_link_address(Manager *m, int family, const union in_addr_union *in_addr) { |
ec2c5e43 LP |
1217 | Link *l; |
1218 | ||
1219 | assert(m); | |
1220 | ||
bb3b08ad YW |
1221 | if (!IN_SET(family, AF_INET, AF_INET6)) |
1222 | return NULL; | |
1223 | ||
1224 | if (!in_addr) | |
1225 | return NULL; | |
1226 | ||
90e74a66 | 1227 | HASHMAP_FOREACH(l, m->links) { |
ec2c5e43 LP |
1228 | LinkAddress *a; |
1229 | ||
1230 | a = link_find_address(l, family, in_addr); | |
1231 | if (a) | |
1232 | return a; | |
1233 | } | |
1234 | ||
1235 | return NULL; | |
1236 | } | |
1237 | ||
94378145 | 1238 | bool manager_packet_from_local_address(Manager *m, DnsPacket *p) { |
ec2c5e43 LP |
1239 | assert(m); |
1240 | assert(p); | |
1241 | ||
94378145 LP |
1242 | /* Let's see if this packet comes from an IP address we have on any local interface */ |
1243 | ||
4e945a6f | 1244 | return !!manager_find_link_address(m, p->family, &p->sender); |
ec2c5e43 | 1245 | } |
4e945a6f | 1246 | |
a9fd8837 LP |
1247 | bool manager_packet_from_our_transaction(Manager *m, DnsPacket *p) { |
1248 | DnsTransaction *t; | |
1249 | ||
1250 | assert(m); | |
1251 | assert(p); | |
1252 | ||
1253 | /* Let's see if we have a transaction with a query message with the exact same binary contents as the | |
1254 | * one we just got. If so, it's almost definitely a packet loop of some kind. */ | |
1255 | ||
1256 | t = hashmap_get(m->dns_transactions, UINT_TO_PTR(DNS_PACKET_ID(p))); | |
1257 | if (!t) | |
1258 | return false; | |
1259 | ||
1260 | return t->sent && dns_packet_equal(t->sent, p); | |
1261 | } | |
1262 | ||
a4076574 LP |
1263 | DnsScope* manager_find_scope(Manager *m, DnsPacket *p) { |
1264 | Link *l; | |
1265 | ||
1266 | assert(m); | |
1267 | assert(p); | |
1268 | ||
1269 | l = hashmap_get(m->links, INT_TO_PTR(p->ifindex)); | |
1270 | if (!l) | |
1271 | return NULL; | |
1272 | ||
b4f1862d DM |
1273 | switch (p->protocol) { |
1274 | case DNS_PROTOCOL_LLMNR: | |
a4076574 LP |
1275 | if (p->family == AF_INET) |
1276 | return l->llmnr_ipv4_scope; | |
1277 | else if (p->family == AF_INET6) | |
1278 | return l->llmnr_ipv6_scope; | |
b4f1862d DM |
1279 | |
1280 | break; | |
1281 | ||
1282 | case DNS_PROTOCOL_MDNS: | |
1283 | if (p->family == AF_INET) | |
1284 | return l->mdns_ipv4_scope; | |
1285 | else if (p->family == AF_INET6) | |
1286 | return l->mdns_ipv6_scope; | |
1287 | ||
1288 | break; | |
1289 | ||
1290 | default: | |
1291 | break; | |
a4076574 LP |
1292 | } |
1293 | ||
1294 | return NULL; | |
1295 | } | |
1296 | ||
902bb5d8 LP |
1297 | void manager_verify_all(Manager *m) { |
1298 | DnsScope *s; | |
1299 | ||
1300 | assert(m); | |
1301 | ||
1302 | LIST_FOREACH(scopes, s, m->dns_scopes) | |
1303 | dns_zone_verify_all(&s->zone); | |
1304 | } | |
1305 | ||
78c6a153 | 1306 | int manager_is_own_hostname(Manager *m, const char *name) { |
78c6a153 LP |
1307 | int r; |
1308 | ||
1309 | assert(m); | |
1310 | assert(name); | |
1311 | ||
1312 | if (m->llmnr_hostname) { | |
1313 | r = dns_name_equal(name, m->llmnr_hostname); | |
1314 | if (r != 0) | |
1315 | return r; | |
1316 | } | |
1317 | ||
e96de0ce LP |
1318 | if (m->mdns_hostname) { |
1319 | r = dns_name_equal(name, m->mdns_hostname); | |
1320 | if (r != 0) | |
1321 | return r; | |
1322 | } | |
1323 | ||
1324 | if (m->full_hostname) | |
1325 | return dns_name_equal(name, m->full_hostname); | |
78c6a153 LP |
1326 | |
1327 | return 0; | |
1328 | } | |
1329 | ||
9176a57c LP |
1330 | int manager_compile_dns_servers(Manager *m, OrderedSet **dns) { |
1331 | DnsServer *s; | |
9176a57c LP |
1332 | Link *l; |
1333 | int r; | |
1334 | ||
1335 | assert(m); | |
1336 | assert(dns); | |
1337 | ||
1338 | r = ordered_set_ensure_allocated(dns, &dns_server_hash_ops); | |
1339 | if (r < 0) | |
1340 | return r; | |
1341 | ||
1342 | /* First add the system-wide servers and domains */ | |
1343 | LIST_FOREACH(servers, s, m->dns_servers) { | |
1344 | r = ordered_set_put(*dns, s); | |
1345 | if (r == -EEXIST) | |
1346 | continue; | |
1347 | if (r < 0) | |
1348 | return r; | |
1349 | } | |
1350 | ||
1351 | /* Then, add the per-link servers */ | |
90e74a66 | 1352 | HASHMAP_FOREACH(l, m->links) { |
9176a57c LP |
1353 | LIST_FOREACH(servers, s, l->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 | ||
1362 | /* If we found nothing, add the fallback servers */ | |
1363 | if (ordered_set_isempty(*dns)) { | |
1364 | LIST_FOREACH(servers, s, m->fallback_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 | ||
1373 | return 0; | |
1374 | } | |
1375 | ||
94363cbb MP |
1376 | /* filter_route is a tri-state: |
1377 | * < 0: no filtering | |
1378 | * = 0 or false: return only domains which should be used for searching | |
1379 | * > 0 or true: return only domains which are for routing only | |
1380 | */ | |
6f7da49d | 1381 | int manager_compile_search_domains(Manager *m, OrderedSet **domains, int filter_route) { |
9176a57c | 1382 | DnsSearchDomain *d; |
9176a57c LP |
1383 | Link *l; |
1384 | int r; | |
1385 | ||
1386 | assert(m); | |
1387 | assert(domains); | |
1388 | ||
1389 | r = ordered_set_ensure_allocated(domains, &dns_name_hash_ops); | |
1390 | if (r < 0) | |
1391 | return r; | |
1392 | ||
1393 | LIST_FOREACH(domains, d, m->search_domains) { | |
6f7da49d LP |
1394 | |
1395 | if (filter_route >= 0 && | |
1396 | d->route_only != !!filter_route) | |
1397 | continue; | |
1398 | ||
9176a57c LP |
1399 | r = ordered_set_put(*domains, d->name); |
1400 | if (r == -EEXIST) | |
1401 | continue; | |
1402 | if (r < 0) | |
1403 | return r; | |
1404 | } | |
1405 | ||
90e74a66 | 1406 | HASHMAP_FOREACH(l, m->links) { |
9176a57c LP |
1407 | |
1408 | LIST_FOREACH(domains, d, l->search_domains) { | |
6f7da49d LP |
1409 | |
1410 | if (filter_route >= 0 && | |
1411 | d->route_only != !!filter_route) | |
1412 | continue; | |
1413 | ||
9176a57c LP |
1414 | r = ordered_set_put(*domains, d->name); |
1415 | if (r == -EEXIST) | |
1416 | continue; | |
1417 | if (r < 0) | |
1418 | return r; | |
1419 | } | |
1420 | } | |
1421 | ||
1422 | return 0; | |
1423 | } | |
c69fa7e3 LP |
1424 | |
1425 | DnssecMode manager_get_dnssec_mode(Manager *m) { | |
1426 | assert(m); | |
1427 | ||
1428 | if (m->dnssec_mode != _DNSSEC_MODE_INVALID) | |
1429 | return m->dnssec_mode; | |
1430 | ||
1431 | return DNSSEC_NO; | |
1432 | } | |
1433 | ||
1434 | bool manager_dnssec_supported(Manager *m) { | |
1435 | DnsServer *server; | |
c69fa7e3 LP |
1436 | Link *l; |
1437 | ||
1438 | assert(m); | |
1439 | ||
1440 | if (manager_get_dnssec_mode(m) == DNSSEC_NO) | |
1441 | return false; | |
1442 | ||
1443 | server = manager_get_dns_server(m); | |
1444 | if (server && !dns_server_dnssec_supported(server)) | |
1445 | return false; | |
1446 | ||
90e74a66 | 1447 | HASHMAP_FOREACH(l, m->links) |
c69fa7e3 LP |
1448 | if (!link_dnssec_supported(l)) |
1449 | return false; | |
1450 | ||
1451 | return true; | |
1452 | } | |
59c5b597 | 1453 | |
c9299be2 | 1454 | DnsOverTlsMode manager_get_dns_over_tls_mode(Manager *m) { |
5d67a7ae IT |
1455 | assert(m); |
1456 | ||
c9299be2 IT |
1457 | if (m->dns_over_tls_mode != _DNS_OVER_TLS_MODE_INVALID) |
1458 | return m->dns_over_tls_mode; | |
5d67a7ae | 1459 | |
c9299be2 | 1460 | return DNS_OVER_TLS_NO; |
5d67a7ae IT |
1461 | } |
1462 | ||
59c5b597 LP |
1463 | void manager_dnssec_verdict(Manager *m, DnssecVerdict verdict, const DnsResourceKey *key) { |
1464 | ||
1465 | assert(verdict >= 0); | |
1466 | assert(verdict < _DNSSEC_VERDICT_MAX); | |
1467 | ||
f1d34068 | 1468 | if (DEBUG_LOGGING) { |
202b76ae | 1469 | char s[DNS_RESOURCE_KEY_STRING_MAX]; |
59c5b597 | 1470 | |
202b76ae ZJS |
1471 | log_debug("Found verdict for lookup %s: %s", |
1472 | dns_resource_key_to_string(key, s, sizeof s), | |
1473 | dnssec_verdict_to_string(verdict)); | |
59c5b597 LP |
1474 | } |
1475 | ||
1476 | m->n_dnssec_verdict[verdict]++; | |
1477 | } | |
011696f7 | 1478 | |
de4a0138 | 1479 | bool manager_routable(Manager *m) { |
011696f7 LP |
1480 | Link *l; |
1481 | ||
1482 | assert(m); | |
1483 | ||
de4a0138 | 1484 | /* Returns true if the host has at least one interface with a routable address (regardless if IPv4 or IPv6) */ |
011696f7 | 1485 | |
90e74a66 | 1486 | HASHMAP_FOREACH(l, m->links) |
de4a0138 | 1487 | if (link_relevant(l, AF_UNSPEC, false)) |
011696f7 LP |
1488 | return true; |
1489 | ||
1490 | return false; | |
1491 | } | |
ba35662f | 1492 | |
90df0fbe | 1493 | void manager_flush_caches(Manager *m, int log_level) { |
ba35662f LP |
1494 | DnsScope *scope; |
1495 | ||
1496 | assert(m); | |
1497 | ||
1498 | LIST_FOREACH(scopes, scope, m->dns_scopes) | |
1499 | dns_cache_flush(&scope->cache); | |
aac57b35 | 1500 | |
90df0fbe | 1501 | log_full(log_level, "Flushed all caches."); |
ba35662f | 1502 | } |
943ef07c | 1503 | |
59c0fd0e | 1504 | void manager_reset_server_features(Manager *m) { |
59c0fd0e LP |
1505 | Link *l; |
1506 | ||
1507 | dns_server_reset_features_all(m->dns_servers); | |
1508 | dns_server_reset_features_all(m->fallback_dns_servers); | |
1509 | ||
90e74a66 | 1510 | HASHMAP_FOREACH(l, m->links) |
59c0fd0e LP |
1511 | dns_server_reset_features_all(l->dns_servers); |
1512 | ||
1513 | log_info("Resetting learnt feature levels on all servers."); | |
1514 | } | |
1515 | ||
943ef07c LP |
1516 | void manager_cleanup_saved_user(Manager *m) { |
1517 | _cleanup_closedir_ DIR *d = NULL; | |
1518 | struct dirent *de; | |
943ef07c LP |
1519 | |
1520 | assert(m); | |
1521 | ||
1522 | /* Clean up all saved per-link files in /run/systemd/resolve/netif/ that don't have a matching interface | |
1523 | * anymore. These files are created to persist settings pushed in by the user via the bus, so that resolved can | |
1524 | * be restarted without losing this data. */ | |
1525 | ||
1526 | d = opendir("/run/systemd/resolve/netif/"); | |
1527 | if (!d) { | |
1528 | if (errno == ENOENT) | |
1529 | return; | |
1530 | ||
1531 | log_warning_errno(errno, "Failed to open interface directory: %m"); | |
1532 | return; | |
1533 | } | |
1534 | ||
1535 | FOREACH_DIRENT_ALL(de, d, log_error_errno(errno, "Failed to read interface directory: %m")) { | |
1536 | _cleanup_free_ char *p = NULL; | |
1537 | int ifindex; | |
1538 | Link *l; | |
1539 | ||
1540 | if (!IN_SET(de->d_type, DT_UNKNOWN, DT_REG)) | |
1541 | continue; | |
1542 | ||
49bfc877 | 1543 | if (dot_or_dot_dot(de->d_name)) |
943ef07c LP |
1544 | continue; |
1545 | ||
597da51b ZJS |
1546 | ifindex = parse_ifindex(de->d_name); |
1547 | if (ifindex < 0) /* Probably some temporary file from a previous run. Delete it */ | |
943ef07c LP |
1548 | goto rm; |
1549 | ||
1550 | l = hashmap_get(m->links, INT_TO_PTR(ifindex)); | |
1551 | if (!l) /* link vanished */ | |
1552 | goto rm; | |
1553 | ||
1554 | if (l->is_managed) /* now managed by networkd, hence the bus settings are useless */ | |
1555 | goto rm; | |
1556 | ||
1557 | continue; | |
1558 | ||
1559 | rm: | |
b910cc72 | 1560 | p = path_join("/run/systemd/resolve/netif", de->d_name); |
943ef07c LP |
1561 | if (!p) { |
1562 | log_oom(); | |
1563 | return; | |
1564 | } | |
1565 | ||
1566 | (void) unlink(p); | |
1567 | } | |
1568 | } | |
e7c1b0e4 DR |
1569 | |
1570 | bool manager_next_dnssd_names(Manager *m) { | |
e7c1b0e4 DR |
1571 | DnssdService *s; |
1572 | bool tried = false; | |
1573 | int r; | |
1574 | ||
1575 | assert(m); | |
1576 | ||
90e74a66 | 1577 | HASHMAP_FOREACH(s, m->dnssd_services) { |
e7c1b0e4 DR |
1578 | _cleanup_free_ char * new_name = NULL; |
1579 | ||
1580 | if (!s->withdrawn) | |
1581 | continue; | |
1582 | ||
1583 | r = manager_next_random_name(s->name_template, &new_name); | |
1584 | if (r < 0) { | |
1585 | log_warning_errno(r, "Failed to get new name for service '%s': %m", s->name); | |
1586 | continue; | |
1587 | } | |
1588 | ||
1589 | free_and_replace(s->name_template, new_name); | |
1590 | ||
1591 | s->withdrawn = false; | |
1592 | ||
1593 | tried = true; | |
1594 | } | |
1595 | ||
1596 | if (tried) | |
1597 | manager_refresh_rrs(m); | |
1598 | ||
1599 | return tried; | |
1600 | } | |
49ef064c LP |
1601 | |
1602 | bool manager_server_is_stub(Manager *m, DnsServer *s) { | |
1603 | DnsStubListenerExtra *l; | |
1604 | ||
1605 | assert(m); | |
1606 | assert(s); | |
1607 | ||
1608 | /* Safety check: we generally already skip the main stub when parsing configuration. But let's be | |
1609 | * extra careful, and check here again */ | |
1610 | if (s->family == AF_INET && | |
1611 | s->address.in.s_addr == htobe32(INADDR_DNS_STUB) && | |
1612 | dns_server_port(s) == 53) | |
1613 | return true; | |
1614 | ||
1615 | /* Main reason to call this is to check server data against the extra listeners, and filter things | |
1616 | * out. */ | |
1617 | ORDERED_SET_FOREACH(l, m->dns_extra_stub_listeners) | |
1618 | if (s->family == l->family && | |
1619 | in_addr_equal(s->family, &s->address, &l->address) && | |
1620 | dns_server_port(s) == dns_stub_listener_extra_port(l)) | |
1621 | return true; | |
1622 | ||
1623 | return false; | |
1624 | } | |
eb170e75 LP |
1625 | |
1626 | int socket_disable_pmtud(int fd, int af) { | |
1627 | int r; | |
1628 | ||
1629 | assert(fd >= 0); | |
1630 | ||
1631 | if (af == AF_UNSPEC) { | |
1632 | r = socket_get_family(fd, &af); | |
1633 | if (r < 0) | |
1634 | return r; | |
1635 | } | |
1636 | ||
1637 | switch (af) { | |
1638 | ||
1639 | case AF_INET: { | |
1640 | /* Turn off path MTU discovery, let's rather fragment on the way than to open us up against | |
1641 | * PMTU forgery vulnerabilities. | |
1642 | * | |
1643 | * There appears to be no documentation about IP_PMTUDISC_OMIT, but it has the effect that | |
1644 | * the "Don't Fragment" bit in the IPv4 header is turned off, thus enforcing fragmentation if | |
1645 | * our datagram size exceeds the MTU of a router in the path, and turning off path MTU | |
1646 | * discovery. | |
1647 | * | |
1648 | * This helps mitigating the PMTUD vulnerability described here: | |
1649 | * | |
1650 | * https://blog.apnic.net/2019/07/12/its-time-to-consider-avoiding-ip-fragmentation-in-the-dns/ | |
1651 | * | |
1652 | * Similar logic is in place in most DNS servers. | |
1653 | * | |
1654 | * There are multiple conflicting goals: we want to allow the largest datagrams possible (for | |
1655 | * efficiency reasons), but not have fragmentation (for security reasons), nor use PMTUD (for | |
1656 | * security reasons, too). Our strategy to deal with this is: use large packets, turn off | |
1657 | * PMTUD, but watch fragmentation taking place, and then size our packets to the max of the | |
1658 | * fragments seen — and if we need larger packets always go to TCP. | |
1659 | */ | |
1660 | ||
1661 | r = setsockopt_int(fd, IPPROTO_IP, IP_MTU_DISCOVER, IP_PMTUDISC_OMIT); | |
1662 | if (r < 0) | |
1663 | return r; | |
1664 | ||
1665 | return 0; | |
1666 | } | |
1667 | ||
1668 | case AF_INET6: { | |
1669 | /* On IPv6 fragmentation only is done by the sender — never by routers on the path. PMTUD is | |
1670 | * mandatory. If we want to turn off PMTUD, the only way is by sending with minimal MTU only, | |
1671 | * so that we apply maximum fragmentation locally already, and thus PMTUD doesn't happen | |
1672 | * because there's nothing that could be fragmented further anymore. */ | |
1673 | ||
1674 | r = setsockopt_int(fd, IPPROTO_IPV6, IPV6_MTU, IPV6_MIN_MTU); | |
1675 | if (r < 0) | |
1676 | return r; | |
1677 | ||
1678 | return 0; | |
1679 | } | |
1680 | ||
1681 | default: | |
1682 | return -EAFNOSUPPORT; | |
1683 | } | |
1684 | } |