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