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