]>
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 | ||
a25b0dc8 LP |
445 | static const char *fallback_hostname(void) { |
446 | ||
447 | /* Determine the fall back hostname. For exposing this system to the outside world, we cannot have it to be | |
448 | * "localhost" even if that's the compiled in hostname. In this case, let's revert to "linux" instead. */ | |
449 | ||
450 | if (is_localhost(FALLBACK_HOSTNAME)) | |
451 | return "linux"; | |
452 | ||
453 | return FALLBACK_HOSTNAME; | |
454 | } | |
455 | ||
456 | static int make_fallback_hostnames(char **full_hostname, char **llmnr_hostname, char **mdns_hostname) { | |
457 | _cleanup_free_ char *n = NULL, *m = NULL; | |
458 | char label[DNS_LABEL_MAX], *h; | |
459 | const char *p; | |
460 | int r; | |
461 | ||
462 | assert(full_hostname); | |
463 | assert(llmnr_hostname); | |
464 | assert(mdns_hostname); | |
465 | ||
466 | p = fallback_hostname(); | |
7470cc4c | 467 | r = dns_label_unescape(&p, label, sizeof label, 0); |
a25b0dc8 | 468 | if (r < 0) |
38b38500 | 469 | return log_error_errno(r, "Failed to unescape fallback hostname: %m"); |
a25b0dc8 LP |
470 | |
471 | assert(r > 0); /* The fallback hostname must have at least one label */ | |
472 | ||
473 | r = dns_label_escape_new(label, r, &n); | |
474 | if (r < 0) | |
475 | return log_error_errno(r, "Failed to escape fallback hostname: %m"); | |
476 | ||
7470cc4c | 477 | r = dns_name_concat(n, "local", 0, &m); |
a25b0dc8 LP |
478 | if (r < 0) |
479 | return log_error_errno(r, "Failed to concatenate mDNS hostname: %m"); | |
480 | ||
481 | h = strdup(fallback_hostname()); | |
482 | if (!h) | |
483 | return log_oom(); | |
484 | ||
ae2a15bc LP |
485 | *llmnr_hostname = TAKE_PTR(n); |
486 | *mdns_hostname = TAKE_PTR(m); | |
a25b0dc8 LP |
487 | |
488 | *full_hostname = h; | |
489 | ||
490 | return 0; | |
491 | } | |
492 | ||
eb60f9cd | 493 | static int on_hostname_change(sd_event_source *es, int fd, uint32_t revents, void *userdata) { |
e96de0ce | 494 | _cleanup_free_ char *full_hostname = NULL, *llmnr_hostname = NULL, *mdns_hostname = NULL; |
eb60f9cd | 495 | Manager *m = userdata; |
86c0411e | 496 | bool llmnr_hostname_changed; |
eb60f9cd LP |
497 | int r; |
498 | ||
499 | assert(m); | |
500 | ||
e96de0ce | 501 | r = determine_hostname(&full_hostname, &llmnr_hostname, &mdns_hostname); |
eb60f9cd LP |
502 | if (r < 0) |
503 | return 0; /* ignore invalid hostnames */ | |
504 | ||
86c0411e | 505 | llmnr_hostname_changed = !streq(llmnr_hostname, m->llmnr_hostname); |
e96de0ce | 506 | if (streq(full_hostname, m->full_hostname) && |
86c0411e | 507 | !llmnr_hostname_changed && |
e96de0ce | 508 | streq(mdns_hostname, m->mdns_hostname)) |
eb60f9cd LP |
509 | return 0; |
510 | ||
e96de0ce | 511 | log_info("System hostname changed to '%s'.", full_hostname); |
78c6a153 | 512 | |
e96de0ce LP |
513 | free_and_replace(m->full_hostname, full_hostname); |
514 | free_and_replace(m->llmnr_hostname, llmnr_hostname); | |
515 | free_and_replace(m->mdns_hostname, mdns_hostname); | |
eb60f9cd LP |
516 | |
517 | manager_refresh_rrs(m); | |
86c0411e | 518 | (void) manager_send_changed(m, "LLMNRHostname"); |
eb60f9cd LP |
519 | |
520 | return 0; | |
521 | } | |
522 | ||
523 | static int manager_watch_hostname(Manager *m) { | |
eb60f9cd LP |
524 | int r; |
525 | ||
526 | assert(m); | |
527 | ||
db4a47e9 LP |
528 | m->hostname_fd = open("/proc/sys/kernel/hostname", |
529 | O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); | |
eb60f9cd | 530 | if (m->hostname_fd < 0) { |
56f64d95 | 531 | log_warning_errno(errno, "Failed to watch hostname: %m"); |
eb60f9cd LP |
532 | return 0; |
533 | } | |
534 | ||
535 | r = sd_event_add_io(m->event, &m->hostname_event_source, m->hostname_fd, 0, on_hostname_change, m); | |
536 | if (r < 0) { | |
537 | if (r == -EPERM) | |
538 | /* kernels prior to 3.2 don't support polling this file. Ignore the failure. */ | |
539 | m->hostname_fd = safe_close(m->hostname_fd); | |
8d3d7072 MS |
540 | else |
541 | return log_error_errno(r, "Failed to add hostname event source: %m"); | |
eb60f9cd LP |
542 | } |
543 | ||
aa4a9deb LP |
544 | (void) sd_event_source_set_description(m->hostname_event_source, "hostname"); |
545 | ||
e96de0ce | 546 | r = determine_hostname(&m->full_hostname, &m->llmnr_hostname, &m->mdns_hostname); |
eb60f9cd | 547 | if (r < 0) { |
a25b0dc8 | 548 | log_info("Defaulting to hostname '%s'.", fallback_hostname()); |
78c6a153 | 549 | |
a25b0dc8 LP |
550 | r = make_fallback_hostnames(&m->full_hostname, &m->llmnr_hostname, &m->mdns_hostname); |
551 | if (r < 0) | |
552 | return r; | |
eb60f9cd | 553 | } else |
e96de0ce | 554 | log_info("Using system hostname '%s'.", m->full_hostname); |
eb60f9cd LP |
555 | |
556 | return 0; | |
557 | } | |
558 | ||
4d506d6b LP |
559 | static int manager_sigusr1(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { |
560 | _cleanup_free_ char *buffer = NULL; | |
561 | _cleanup_fclose_ FILE *f = NULL; | |
562 | Manager *m = userdata; | |
cf84484a | 563 | DnsServer *server; |
4d506d6b LP |
564 | size_t size = 0; |
565 | DnsScope *scope; | |
cf84484a | 566 | Link *l; |
4d506d6b LP |
567 | |
568 | assert(s); | |
569 | assert(si); | |
570 | assert(m); | |
571 | ||
2fe21124 | 572 | f = open_memstream_unlocked(&buffer, &size); |
4d506d6b LP |
573 | if (!f) |
574 | return log_oom(); | |
575 | ||
576 | LIST_FOREACH(scopes, scope, m->dns_scopes) | |
577 | dns_scope_dump(scope, f); | |
578 | ||
cf84484a LP |
579 | LIST_FOREACH(servers, server, m->dns_servers) |
580 | dns_server_dump(server, f); | |
581 | LIST_FOREACH(servers, server, m->fallback_dns_servers) | |
582 | dns_server_dump(server, f); | |
90e74a66 | 583 | HASHMAP_FOREACH(l, m->links) |
cf84484a LP |
584 | LIST_FOREACH(servers, server, l->dns_servers) |
585 | dns_server_dump(server, f); | |
586 | ||
4d506d6b LP |
587 | if (fflush_and_check(f) < 0) |
588 | return log_oom(); | |
589 | ||
590 | log_dump(LOG_INFO, buffer); | |
591 | return 0; | |
592 | } | |
593 | ||
bc81447e LP |
594 | static int manager_sigusr2(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { |
595 | Manager *m = userdata; | |
bc81447e LP |
596 | |
597 | assert(s); | |
598 | assert(si); | |
599 | assert(m); | |
600 | ||
90df0fbe | 601 | manager_flush_caches(m, LOG_INFO); |
ba35662f | 602 | |
bc81447e LP |
603 | return 0; |
604 | } | |
605 | ||
d55b0463 LP |
606 | static int manager_sigrtmin1(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { |
607 | Manager *m = userdata; | |
608 | ||
609 | assert(s); | |
610 | assert(si); | |
611 | assert(m); | |
612 | ||
613 | manager_reset_server_features(m); | |
614 | return 0; | |
615 | } | |
616 | ||
091a364c | 617 | int manager_new(Manager **ret) { |
74b2466e | 618 | _cleanup_(manager_freep) Manager *m = NULL; |
091a364c TG |
619 | int r; |
620 | ||
c92e531c LP |
621 | assert(ret); |
622 | ||
f55f2dce | 623 | m = new(Manager, 1); |
091a364c TG |
624 | if (!m) |
625 | return -ENOMEM; | |
626 | ||
f55f2dce YW |
627 | *m = (Manager) { |
628 | .llmnr_ipv4_udp_fd = -1, | |
629 | .llmnr_ipv6_udp_fd = -1, | |
630 | .llmnr_ipv4_tcp_fd = -1, | |
631 | .llmnr_ipv6_tcp_fd = -1, | |
632 | .mdns_ipv4_fd = -1, | |
633 | .mdns_ipv6_fd = -1, | |
f55f2dce YW |
634 | .hostname_fd = -1, |
635 | ||
3614df05 ZJS |
636 | .llmnr_support = DEFAULT_LLMNR_MODE, |
637 | .mdns_support = DEFAULT_MDNS_MODE, | |
f55f2dce YW |
638 | .dnssec_mode = DEFAULT_DNSSEC_MODE, |
639 | .dns_over_tls_mode = DEFAULT_DNS_OVER_TLS_MODE, | |
37d7a7d9 | 640 | .enable_cache = DNS_CACHE_MODE_YES, |
4b987478 | 641 | .dns_stub_listener_mode = DNS_STUB_LISTENER_YES, |
f55f2dce YW |
642 | .read_resolv_conf = true, |
643 | .need_builtin_fallbacks = true, | |
644 | .etc_hosts_last = USEC_INFINITY, | |
f55f2dce YW |
645 | .read_etc_hosts = true, |
646 | }; | |
091a364c | 647 | |
0d2cd476 LP |
648 | r = dns_trust_anchor_load(&m->trust_anchor); |
649 | if (r < 0) | |
650 | return r; | |
651 | ||
ad6c0475 LP |
652 | r = manager_parse_config_file(m); |
653 | if (r < 0) | |
5ce497b5 | 654 | log_warning_errno(r, "Failed to parse configuration file: %m"); |
ad6c0475 | 655 | |
e22c5b20 | 656 | #if ENABLE_DNS_OVER_TLS |
71a681ae IT |
657 | r = dnstls_manager_init(m); |
658 | if (r < 0) | |
659 | return r; | |
e22c5b20 IT |
660 | #endif |
661 | ||
091a364c TG |
662 | r = sd_event_default(&m->event); |
663 | if (r < 0) | |
664 | return r; | |
665 | ||
1ae17292 YW |
666 | (void) sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL); |
667 | (void) sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL); | |
091a364c | 668 | |
1ae17292 | 669 | (void) sd_event_set_watchdog(m->event, true); |
091a364c | 670 | |
eb60f9cd LP |
671 | r = manager_watch_hostname(m); |
672 | if (r < 0) | |
673 | return r; | |
674 | ||
6501dd31 DR |
675 | r = dnssd_load(m); |
676 | if (r < 0) | |
677 | log_warning_errno(r, "Failed to load DNS-SD configuration files: %m"); | |
678 | ||
1716f6dc | 679 | r = dns_scope_new(m, &m->unicast_scope, NULL, DNS_PROTOCOL_DNS, AF_UNSPEC); |
74b2466e LP |
680 | if (r < 0) |
681 | return r; | |
682 | ||
683 | r = manager_network_monitor_listen(m); | |
684 | if (r < 0) | |
685 | return r; | |
686 | ||
687 | r = manager_rtnl_listen(m); | |
688 | if (r < 0) | |
689 | return r; | |
690 | ||
90df0fbe LP |
691 | r = manager_clock_change_listen(m); |
692 | if (r < 0) | |
693 | return r; | |
694 | ||
74b2466e LP |
695 | r = manager_connect_bus(m); |
696 | if (r < 0) | |
697 | return r; | |
698 | ||
4d506d6b | 699 | (void) sd_event_add_signal(m->event, &m->sigusr1_event_source, SIGUSR1, manager_sigusr1, m); |
bc81447e | 700 | (void) sd_event_add_signal(m->event, &m->sigusr2_event_source, SIGUSR2, manager_sigusr2, m); |
d55b0463 | 701 | (void) sd_event_add_signal(m->event, &m->sigrtmin1_event_source, SIGRTMIN+1, manager_sigrtmin1, m); |
4d506d6b | 702 | |
943ef07c LP |
703 | manager_cleanup_saved_user(m); |
704 | ||
1cc6c93a | 705 | *ret = TAKE_PTR(m); |
091a364c TG |
706 | |
707 | return 0; | |
708 | } | |
709 | ||
edc501d4 LP |
710 | int manager_start(Manager *m) { |
711 | int r; | |
712 | ||
713 | assert(m); | |
714 | ||
b30bf55d LP |
715 | r = manager_dns_stub_start(m); |
716 | if (r < 0) | |
717 | return r; | |
718 | ||
9581bb84 LP |
719 | r = manager_varlink_init(m); |
720 | if (r < 0) | |
721 | return r; | |
722 | ||
edc501d4 LP |
723 | return 0; |
724 | } | |
725 | ||
74b2466e LP |
726 | Manager *manager_free(Manager *m) { |
727 | Link *l; | |
6501dd31 | 728 | DnssdService *s; |
091a364c TG |
729 | |
730 | if (!m) | |
74b2466e LP |
731 | return NULL; |
732 | ||
4b95f179 LP |
733 | dns_server_unlink_all(m->dns_servers); |
734 | dns_server_unlink_all(m->fallback_dns_servers); | |
a51c1048 LP |
735 | dns_search_domain_unlink_all(m->search_domains); |
736 | ||
74b2466e LP |
737 | while ((l = hashmap_first(m->links))) |
738 | link_free(l); | |
f0e15467 LP |
739 | |
740 | while (m->dns_queries) | |
741 | dns_query_free(m->dns_queries); | |
74b2466e | 742 | |
bde69bbd LP |
743 | m->stub_queries_by_packet = hashmap_free(m->stub_queries_by_packet); |
744 | ||
cab5b059 LP |
745 | dns_scope_free(m->unicast_scope); |
746 | ||
b30bf55d LP |
747 | /* At this point only orphaned streams should remain. All others should have been freed already by their |
748 | * owners */ | |
749 | while (m->dns_streams) | |
750 | dns_stream_unref(m->dns_streams); | |
751 | ||
e22c5b20 IT |
752 | #if ENABLE_DNS_OVER_TLS |
753 | dnstls_manager_free(m); | |
754 | #endif | |
755 | ||
f0e15467 LP |
756 | hashmap_free(m->links); |
757 | hashmap_free(m->dns_transactions); | |
758 | ||
096b6773 LP |
759 | sd_event_source_unref(m->network_event_source); |
760 | sd_network_monitor_unref(m->network_monitor); | |
091a364c | 761 | |
a564ca2f LP |
762 | sd_netlink_unref(m->rtnl); |
763 | sd_event_source_unref(m->rtnl_event_source); | |
90df0fbe | 764 | sd_event_source_unref(m->clock_change_event_source); |
a564ca2f | 765 | |
edc501d4 | 766 | manager_llmnr_stop(m); |
bc7702b0 | 767 | manager_mdns_stop(m); |
b30bf55d | 768 | manager_dns_stub_stop(m); |
9581bb84 | 769 | manager_varlink_done(m); |
623a4c97 | 770 | |
80710ade LP |
771 | manager_socket_graveyard_clear(m); |
772 | ||
46505826 | 773 | ordered_set_free(m->dns_extra_stub_listeners); |
1f05101f | 774 | |
2400ae29 YW |
775 | bus_verify_polkit_async_registry_free(m->polkit_registry); |
776 | ||
92e31da1 | 777 | sd_bus_flush_close_unref(m->bus); |
091a364c | 778 | |
4d506d6b | 779 | sd_event_source_unref(m->sigusr1_event_source); |
bc81447e | 780 | sd_event_source_unref(m->sigusr2_event_source); |
d55b0463 | 781 | sd_event_source_unref(m->sigrtmin1_event_source); |
4d506d6b | 782 | |
74b2466e | 783 | sd_event_unref(m->event); |
623a4c97 | 784 | |
78c6a153 LP |
785 | dns_resource_key_unref(m->llmnr_host_ipv4_key); |
786 | dns_resource_key_unref(m->llmnr_host_ipv6_key); | |
400cb36e DR |
787 | dns_resource_key_unref(m->mdns_host_ipv4_key); |
788 | dns_resource_key_unref(m->mdns_host_ipv6_key); | |
eb60f9cd | 789 | |
eb60f9cd | 790 | sd_event_source_unref(m->hostname_event_source); |
d9fcf2ba | 791 | safe_close(m->hostname_fd); |
e96de0ce LP |
792 | |
793 | free(m->full_hostname); | |
78c6a153 LP |
794 | free(m->llmnr_hostname); |
795 | free(m->mdns_hostname); | |
eb60f9cd | 796 | |
6501dd31 DR |
797 | while ((s = hashmap_first(m->dnssd_services))) |
798 | dnssd_service_free(s); | |
799 | hashmap_free(m->dnssd_services); | |
800 | ||
0d2cd476 | 801 | dns_trust_anchor_flush(&m->trust_anchor); |
dd0bc0f1 | 802 | manager_etc_hosts_flush(m); |
0d2cd476 | 803 | |
6b430fdb | 804 | return mfree(m); |
091a364c TG |
805 | } |
806 | ||
1716f6dc | 807 | int manager_recv(Manager *m, int fd, DnsProtocol protocol, DnsPacket **ret) { |
74b2466e | 808 | _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL; |
fb29cdbe LP |
809 | CMSG_BUFFER_TYPE(CMSG_SPACE(MAXSIZE(struct in_pktinfo, struct in6_pktinfo)) |
810 | + CMSG_SPACE(int) /* ttl/hoplimit */ | |
811 | + EXTRA_CMSG_SPACE /* kernel appears to require extra buffer space */) control; | |
1716f6dc | 812 | union sockaddr_union sa; |
74b2466e | 813 | struct iovec iov; |
f55f2dce YW |
814 | struct msghdr mh = { |
815 | .msg_name = &sa.sa, | |
816 | .msg_namelen = sizeof(sa), | |
817 | .msg_iov = &iov, | |
818 | .msg_iovlen = 1, | |
819 | .msg_control = &control, | |
820 | .msg_controllen = sizeof(control), | |
821 | }; | |
822 | struct cmsghdr *cmsg; | |
4edc2c9b LP |
823 | ssize_t ms, l; |
824 | int r; | |
74b2466e LP |
825 | |
826 | assert(m); | |
1716f6dc | 827 | assert(fd >= 0); |
74b2466e LP |
828 | assert(ret); |
829 | ||
4edc2c9b | 830 | ms = next_datagram_size_fd(fd); |
74b2466e | 831 | if (ms < 0) |
4edc2c9b | 832 | return ms; |
74b2466e | 833 | |
51027656 | 834 | r = dns_packet_new(&p, protocol, ms, DNS_PACKET_SIZE_MAX); |
74b2466e LP |
835 | if (r < 0) |
836 | return r; | |
837 | ||
cb310866 | 838 | iov = IOVEC_MAKE(DNS_PACKET_DATA(p), p->allocated); |
74b2466e | 839 | |
3691bcf3 LP |
840 | l = recvmsg_safe(fd, &mh, 0); |
841 | if (IN_SET(l, -EAGAIN, -EINTR)) | |
842 | return 0; | |
d7a0f1f4 | 843 | if (l <= 0) |
3691bcf3 | 844 | return l; |
091a364c | 845 | |
1716f6dc LP |
846 | assert(!(mh.msg_flags & MSG_TRUNC)); |
847 | ||
74b2466e | 848 | p->size = (size_t) l; |
091a364c | 849 | |
1716f6dc | 850 | p->family = sa.sa.sa_family; |
623a4c97 LP |
851 | p->ipproto = IPPROTO_UDP; |
852 | if (p->family == AF_INET) { | |
1716f6dc | 853 | p->sender.in = sa.in.sin_addr; |
623a4c97 LP |
854 | p->sender_port = be16toh(sa.in.sin_port); |
855 | } else if (p->family == AF_INET6) { | |
1716f6dc | 856 | p->sender.in6 = sa.in6.sin6_addr; |
623a4c97 LP |
857 | p->sender_port = be16toh(sa.in6.sin6_port); |
858 | p->ifindex = sa.in6.sin6_scope_id; | |
859 | } else | |
1716f6dc | 860 | return -EAFNOSUPPORT; |
74b2466e | 861 | |
5777c613 LP |
862 | p->timestamp = now(clock_boottime_or_monotonic()); |
863 | ||
2a1288ff | 864 | CMSG_FOREACH(cmsg, &mh) { |
74b2466e | 865 | |
1716f6dc LP |
866 | if (cmsg->cmsg_level == IPPROTO_IPV6) { |
867 | assert(p->family == AF_INET6); | |
74b2466e | 868 | |
1716f6dc | 869 | switch (cmsg->cmsg_type) { |
74b2466e | 870 | |
1716f6dc LP |
871 | case IPV6_PKTINFO: { |
872 | struct in6_pktinfo *i = (struct in6_pktinfo*) CMSG_DATA(cmsg); | |
74b2466e | 873 | |
623a4c97 LP |
874 | if (p->ifindex <= 0) |
875 | p->ifindex = i->ipi6_ifindex; | |
876 | ||
1716f6dc LP |
877 | p->destination.in6 = i->ipi6_addr; |
878 | break; | |
879 | } | |
74b2466e | 880 | |
1716f6dc LP |
881 | case IPV6_HOPLIMIT: |
882 | p->ttl = *(int *) CMSG_DATA(cmsg); | |
883 | break; | |
74b2466e | 884 | |
20a001bd LP |
885 | case IPV6_RECVFRAGSIZE: |
886 | p->fragsize = *(int *) CMSG_DATA(cmsg); | |
887 | break; | |
1716f6dc LP |
888 | } |
889 | } else if (cmsg->cmsg_level == IPPROTO_IP) { | |
890 | assert(p->family == AF_INET); | |
74b2466e | 891 | |
1716f6dc | 892 | switch (cmsg->cmsg_type) { |
74b2466e | 893 | |
1716f6dc LP |
894 | case IP_PKTINFO: { |
895 | struct in_pktinfo *i = (struct in_pktinfo*) CMSG_DATA(cmsg); | |
091a364c | 896 | |
623a4c97 LP |
897 | if (p->ifindex <= 0) |
898 | p->ifindex = i->ipi_ifindex; | |
899 | ||
1716f6dc LP |
900 | p->destination.in = i->ipi_addr; |
901 | break; | |
902 | } | |
74b2466e | 903 | |
623a4c97 | 904 | case IP_TTL: |
1716f6dc LP |
905 | p->ttl = *(int *) CMSG_DATA(cmsg); |
906 | break; | |
20a001bd LP |
907 | |
908 | case IP_RECVFRAGSIZE: | |
909 | p->fragsize = *(int *) CMSG_DATA(cmsg); | |
910 | break; | |
1716f6dc LP |
911 | } |
912 | } | |
913 | } | |
74b2466e | 914 | |
623a4c97 LP |
915 | /* The Linux kernel sets the interface index to the loopback |
916 | * device if the packet came from the local host since it | |
917 | * avoids the routing table in such a case. Let's unset the | |
918 | * interface index in such a case. */ | |
a5f03596 | 919 | if (p->ifindex == LOOPBACK_IFINDEX) |
623a4c97 LP |
920 | p->ifindex = 0; |
921 | ||
86ad4cd7 TG |
922 | if (protocol != DNS_PROTOCOL_DNS) { |
923 | /* If we don't know the interface index still, we look for the | |
924 | * first local interface with a matching address. Yuck! */ | |
925 | if (p->ifindex <= 0) | |
926 | p->ifindex = manager_find_ifindex(m, p->family, &p->destination); | |
927 | } | |
623a4c97 | 928 | |
f7155840 LP |
929 | log_debug("Received %s UDP packet of size %zu, ifindex=%i, ttl=%i, fragsize=%zu", |
930 | dns_protocol_to_string(protocol), p->size, p->ifindex, p->ttl, p->fragsize); | |
74b2466e | 931 | |
f7155840 | 932 | *ret = TAKE_PTR(p); |
74b2466e LP |
933 | return 1; |
934 | } | |
935 | ||
74b2466e LP |
936 | static int sendmsg_loop(int fd, struct msghdr *mh, int flags) { |
937 | int r; | |
938 | ||
939 | assert(fd >= 0); | |
940 | assert(mh); | |
941 | ||
942 | for (;;) { | |
943 | if (sendmsg(fd, mh, flags) >= 0) | |
944 | return 0; | |
945 | ||
946 | if (errno == EINTR) | |
947 | continue; | |
948 | ||
949 | if (errno != EAGAIN) | |
950 | return -errno; | |
951 | ||
952 | r = fd_wait_for_event(fd, POLLOUT, SEND_TIMEOUT_USEC); | |
953 | if (r < 0) | |
954 | return r; | |
955 | if (r == 0) | |
956 | return -ETIMEDOUT; | |
957 | } | |
958 | } | |
959 | ||
72290734 TG |
960 | static int write_loop(int fd, void *message, size_t length) { |
961 | int r; | |
962 | ||
963 | assert(fd >= 0); | |
964 | assert(message); | |
965 | ||
966 | for (;;) { | |
967 | if (write(fd, message, length) >= 0) | |
968 | return 0; | |
969 | ||
970 | if (errno == EINTR) | |
971 | continue; | |
972 | ||
973 | if (errno != EAGAIN) | |
974 | return -errno; | |
975 | ||
976 | r = fd_wait_for_event(fd, POLLOUT, SEND_TIMEOUT_USEC); | |
977 | if (r < 0) | |
978 | return r; | |
979 | if (r == 0) | |
980 | return -ETIMEDOUT; | |
981 | } | |
982 | } | |
983 | ||
984 | int manager_write(Manager *m, int fd, DnsPacket *p) { | |
985 | int r; | |
986 | ||
76f77229 LP |
987 | log_debug("Sending %s%s packet with id %" PRIu16 " of size %zu.", |
988 | DNS_PACKET_TC(p) ? "truncated (!) " : "", | |
989 | DNS_PACKET_QR(p) ? "response" : "query", | |
990 | DNS_PACKET_ID(p), | |
991 | p->size); | |
72290734 TG |
992 | |
993 | r = write_loop(fd, DNS_PACKET_DATA(p), p->size); | |
994 | if (r < 0) | |
995 | return r; | |
996 | ||
997 | return 0; | |
998 | } | |
999 | ||
b30bf55d LP |
1000 | static int manager_ipv4_send( |
1001 | Manager *m, | |
1002 | int fd, | |
1003 | int ifindex, | |
1004 | const struct in_addr *destination, | |
1005 | uint16_t port, | |
1006 | const struct in_addr *source, | |
1007 | DnsPacket *p) { | |
fb29cdbe LP |
1008 | |
1009 | CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct in_pktinfo))) control = {}; | |
f55f2dce | 1010 | union sockaddr_union sa; |
74b2466e | 1011 | struct iovec iov; |
f55f2dce YW |
1012 | struct msghdr mh = { |
1013 | .msg_iov = &iov, | |
1014 | .msg_iovlen = 1, | |
1015 | .msg_name = &sa.sa, | |
1016 | .msg_namelen = sizeof(sa.in), | |
1017 | }; | |
74b2466e LP |
1018 | |
1019 | assert(m); | |
1716f6dc | 1020 | assert(fd >= 0); |
b30bf55d | 1021 | assert(destination); |
1716f6dc | 1022 | assert(port > 0); |
74b2466e LP |
1023 | assert(p); |
1024 | ||
cb310866 | 1025 | iov = IOVEC_MAKE(DNS_PACKET_DATA(p), p->size); |
091a364c | 1026 | |
f55f2dce YW |
1027 | sa = (union sockaddr_union) { |
1028 | .in.sin_family = AF_INET, | |
1029 | .in.sin_addr = *destination, | |
1030 | .in.sin_port = htobe16(port), | |
1031 | }; | |
091a364c | 1032 | |
74b2466e LP |
1033 | if (ifindex > 0) { |
1034 | struct cmsghdr *cmsg; | |
1035 | struct in_pktinfo *pi; | |
1036 | ||
1716f6dc | 1037 | mh.msg_control = &control; |
a258f491 | 1038 | mh.msg_controllen = sizeof(control); |
74b2466e LP |
1039 | |
1040 | cmsg = CMSG_FIRSTHDR(&mh); | |
a258f491 | 1041 | cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo)); |
74b2466e LP |
1042 | cmsg->cmsg_level = IPPROTO_IP; |
1043 | cmsg->cmsg_type = IP_PKTINFO; | |
1044 | ||
1045 | pi = (struct in_pktinfo*) CMSG_DATA(cmsg); | |
1046 | pi->ipi_ifindex = ifindex; | |
b30bf55d LP |
1047 | |
1048 | if (source) | |
1049 | pi->ipi_spec_dst = *source; | |
74b2466e LP |
1050 | } |
1051 | ||
1052 | return sendmsg_loop(fd, &mh, 0); | |
1053 | } | |
1054 | ||
b30bf55d LP |
1055 | static int manager_ipv6_send( |
1056 | Manager *m, | |
1057 | int fd, | |
1058 | int ifindex, | |
1059 | const struct in6_addr *destination, | |
1060 | uint16_t port, | |
1061 | const struct in6_addr *source, | |
1062 | DnsPacket *p) { | |
1063 | ||
fb29cdbe | 1064 | CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct in6_pktinfo))) control = {}; |
f55f2dce | 1065 | union sockaddr_union sa; |
74b2466e | 1066 | struct iovec iov; |
f55f2dce YW |
1067 | struct msghdr mh = { |
1068 | .msg_iov = &iov, | |
1069 | .msg_iovlen = 1, | |
1070 | .msg_name = &sa.sa, | |
1071 | .msg_namelen = sizeof(sa.in6), | |
1072 | }; | |
74b2466e LP |
1073 | |
1074 | assert(m); | |
1716f6dc | 1075 | assert(fd >= 0); |
b30bf55d | 1076 | assert(destination); |
1716f6dc | 1077 | assert(port > 0); |
74b2466e LP |
1078 | assert(p); |
1079 | ||
cb310866 | 1080 | iov = IOVEC_MAKE(DNS_PACKET_DATA(p), p->size); |
74b2466e | 1081 | |
f55f2dce YW |
1082 | sa = (union sockaddr_union) { |
1083 | .in6.sin6_family = AF_INET6, | |
1084 | .in6.sin6_addr = *destination, | |
1085 | .in6.sin6_port = htobe16(port), | |
1086 | .in6.sin6_scope_id = ifindex, | |
1087 | }; | |
74b2466e LP |
1088 | |
1089 | if (ifindex > 0) { | |
1090 | struct cmsghdr *cmsg; | |
1091 | struct in6_pktinfo *pi; | |
1092 | ||
1716f6dc | 1093 | mh.msg_control = &control; |
a258f491 | 1094 | mh.msg_controllen = sizeof(control); |
74b2466e LP |
1095 | |
1096 | cmsg = CMSG_FIRSTHDR(&mh); | |
a258f491 | 1097 | cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); |
74b2466e LP |
1098 | cmsg->cmsg_level = IPPROTO_IPV6; |
1099 | cmsg->cmsg_type = IPV6_PKTINFO; | |
1100 | ||
1101 | pi = (struct in6_pktinfo*) CMSG_DATA(cmsg); | |
1102 | pi->ipi6_ifindex = ifindex; | |
b30bf55d LP |
1103 | |
1104 | if (source) | |
1105 | pi->ipi6_addr = *source; | |
74b2466e LP |
1106 | } |
1107 | ||
1108 | return sendmsg_loop(fd, &mh, 0); | |
1109 | } | |
1110 | ||
b30bf55d LP |
1111 | int manager_send( |
1112 | Manager *m, | |
1113 | int fd, | |
1114 | int ifindex, | |
1115 | int family, | |
1116 | const union in_addr_union *destination, | |
1117 | uint16_t port, | |
1118 | const union in_addr_union *source, | |
1119 | DnsPacket *p) { | |
1120 | ||
1716f6dc LP |
1121 | assert(m); |
1122 | assert(fd >= 0); | |
b30bf55d | 1123 | assert(destination); |
1716f6dc LP |
1124 | assert(port > 0); |
1125 | assert(p); | |
1126 | ||
76f77229 LP |
1127 | log_debug("Sending %s%s packet with id %" PRIu16 " on interface %i/%s of size %zu.", |
1128 | DNS_PACKET_TC(p) ? "truncated (!) " : "", | |
1129 | DNS_PACKET_QR(p) ? "response" : "query", | |
1130 | DNS_PACKET_ID(p), | |
1131 | ifindex, af_to_name(family), | |
1132 | p->size); | |
a2a416f7 | 1133 | |
1716f6dc | 1134 | if (family == AF_INET) |
25270cf3 | 1135 | return manager_ipv4_send(m, fd, ifindex, &destination->in, port, source ? &source->in : NULL, p); |
2817157b | 1136 | if (family == AF_INET6) |
25270cf3 | 1137 | return manager_ipv6_send(m, fd, ifindex, &destination->in6, port, source ? &source->in6 : NULL, p); |
1716f6dc LP |
1138 | |
1139 | return -EAFNOSUPPORT; | |
1140 | } | |
1141 | ||
e1c95994 LP |
1142 | uint32_t manager_find_mtu(Manager *m) { |
1143 | uint32_t mtu = 0; | |
1144 | Link *l; | |
e1c95994 LP |
1145 | |
1146 | /* If we don't know on which link a DNS packet would be | |
1147 | * delivered, let's find the largest MTU that works on all | |
1148 | * interfaces we know of */ | |
1149 | ||
90e74a66 | 1150 | HASHMAP_FOREACH(l, m->links) { |
e1c95994 LP |
1151 | if (l->mtu <= 0) |
1152 | continue; | |
1153 | ||
1154 | if (mtu <= 0 || l->mtu < mtu) | |
1155 | mtu = l->mtu; | |
1156 | } | |
1157 | ||
1158 | return mtu; | |
1159 | } | |
1716f6dc | 1160 | |
623a4c97 | 1161 | int manager_find_ifindex(Manager *m, int family, const union in_addr_union *in_addr) { |
ec2c5e43 LP |
1162 | LinkAddress *a; |
1163 | ||
1164 | assert(m); | |
1165 | ||
bb3b08ad YW |
1166 | if (!IN_SET(family, AF_INET, AF_INET6)) |
1167 | return 0; | |
1168 | ||
1169 | if (!in_addr) | |
1170 | return 0; | |
1171 | ||
4e945a6f | 1172 | a = manager_find_link_address(m, family, in_addr); |
ec2c5e43 LP |
1173 | if (a) |
1174 | return a->link->ifindex; | |
1175 | ||
1176 | return 0; | |
1177 | } | |
1178 | ||
eb60f9cd | 1179 | void manager_refresh_rrs(Manager *m) { |
eb60f9cd | 1180 | Link *l; |
6db6a464 | 1181 | DnssdService *s; |
eb60f9cd LP |
1182 | |
1183 | assert(m); | |
1184 | ||
78c6a153 LP |
1185 | m->llmnr_host_ipv4_key = dns_resource_key_unref(m->llmnr_host_ipv4_key); |
1186 | m->llmnr_host_ipv6_key = dns_resource_key_unref(m->llmnr_host_ipv6_key); | |
400cb36e DR |
1187 | m->mdns_host_ipv4_key = dns_resource_key_unref(m->mdns_host_ipv4_key); |
1188 | m->mdns_host_ipv6_key = dns_resource_key_unref(m->mdns_host_ipv6_key); | |
eb60f9cd | 1189 | |
6db6a464 | 1190 | if (m->mdns_support == RESOLVE_SUPPORT_YES) |
90e74a66 | 1191 | HASHMAP_FOREACH(s, m->dnssd_services) |
6db6a464 DR |
1192 | if (dnssd_update_rrs(s) < 0) |
1193 | log_warning("Failed to refresh DNS-SD service '%s'", s->name); | |
1194 | ||
90e74a66 | 1195 | HASHMAP_FOREACH(l, m->links) { |
eb60f9cd LP |
1196 | link_add_rrs(l, true); |
1197 | link_add_rrs(l, false); | |
1198 | } | |
1199 | } | |
1200 | ||
e7c1b0e4 | 1201 | static int manager_next_random_name(const char *old, char **ret_new) { |
ec2c5e43 | 1202 | const char *p; |
556a2294 | 1203 | uint64_t u, a; |
e7c1b0e4 | 1204 | char *n; |
623a4c97 | 1205 | |
e7c1b0e4 | 1206 | p = strchr(old, 0); |
ec2c5e43 LP |
1207 | assert(p); |
1208 | ||
e7c1b0e4 DR |
1209 | while (p > old) { |
1210 | if (!strchr(DIGITS, p[-1])) | |
ec2c5e43 LP |
1211 | break; |
1212 | ||
1213 | p--; | |
1214 | } | |
1215 | ||
1216 | if (*p == 0 || safe_atou64(p, &u) < 0 || u <= 0) | |
1217 | u = 1; | |
1218 | ||
556a2294 LP |
1219 | /* Add a random number to the old value. This way we can avoid |
1220 | * that two hosts pick the same hostname, win on IPv4 and lose | |
1221 | * on IPv6 (or vice versa), and pick the same hostname | |
1222 | * replacement hostname, ad infinitum. We still want the | |
1223 | * numbers to go up monotonically, hence we just add a random | |
1224 | * value 1..10 */ | |
1225 | ||
1226 | random_bytes(&a, sizeof(a)); | |
1227 | u += 1 + a % 10; | |
ec2c5e43 | 1228 | |
e7c1b0e4 | 1229 | if (asprintf(&n, "%.*s%" PRIu64, (int) (p - old), old, u) < 0) |
ec2c5e43 LP |
1230 | return -ENOMEM; |
1231 | ||
e7c1b0e4 DR |
1232 | *ret_new = n; |
1233 | ||
1234 | return 0; | |
1235 | } | |
1236 | ||
1237 | int manager_next_hostname(Manager *m) { | |
1238 | _cleanup_free_ char *h = NULL, *k = NULL; | |
1239 | int r; | |
1240 | ||
1241 | assert(m); | |
1242 | ||
1243 | r = manager_next_random_name(m->llmnr_hostname, &h); | |
1244 | if (r < 0) | |
1245 | return r; | |
1246 | ||
7470cc4c | 1247 | r = dns_name_concat(h, "local", 0, &k); |
e7c1b0e4 | 1248 | if (r < 0) |
78c6a153 | 1249 | return r; |
78c6a153 LP |
1250 | |
1251 | log_info("Hostname conflict, changing published hostname from '%s' to '%s'.", m->llmnr_hostname, h); | |
1252 | ||
e7c1b0e4 DR |
1253 | free_and_replace(m->llmnr_hostname, h); |
1254 | free_and_replace(m->mdns_hostname, k); | |
ec2c5e43 | 1255 | |
eb60f9cd | 1256 | manager_refresh_rrs(m); |
86c0411e | 1257 | (void) manager_send_changed(m, "LLMNRHostname"); |
623a4c97 LP |
1258 | |
1259 | return 0; | |
1260 | } | |
ec2c5e43 | 1261 | |
4e945a6f | 1262 | LinkAddress* manager_find_link_address(Manager *m, int family, const union in_addr_union *in_addr) { |
ec2c5e43 LP |
1263 | Link *l; |
1264 | ||
1265 | assert(m); | |
1266 | ||
bb3b08ad YW |
1267 | if (!IN_SET(family, AF_INET, AF_INET6)) |
1268 | return NULL; | |
1269 | ||
1270 | if (!in_addr) | |
1271 | return NULL; | |
1272 | ||
90e74a66 | 1273 | HASHMAP_FOREACH(l, m->links) { |
ec2c5e43 LP |
1274 | LinkAddress *a; |
1275 | ||
1276 | a = link_find_address(l, family, in_addr); | |
1277 | if (a) | |
1278 | return a; | |
1279 | } | |
1280 | ||
1281 | return NULL; | |
1282 | } | |
1283 | ||
94378145 | 1284 | bool manager_packet_from_local_address(Manager *m, DnsPacket *p) { |
ec2c5e43 LP |
1285 | assert(m); |
1286 | assert(p); | |
1287 | ||
94378145 LP |
1288 | /* Let's see if this packet comes from an IP address we have on any local interface */ |
1289 | ||
4e945a6f | 1290 | return !!manager_find_link_address(m, p->family, &p->sender); |
ec2c5e43 | 1291 | } |
4e945a6f | 1292 | |
a9fd8837 LP |
1293 | bool manager_packet_from_our_transaction(Manager *m, DnsPacket *p) { |
1294 | DnsTransaction *t; | |
1295 | ||
1296 | assert(m); | |
1297 | assert(p); | |
1298 | ||
1299 | /* Let's see if we have a transaction with a query message with the exact same binary contents as the | |
1300 | * one we just got. If so, it's almost definitely a packet loop of some kind. */ | |
1301 | ||
1302 | t = hashmap_get(m->dns_transactions, UINT_TO_PTR(DNS_PACKET_ID(p))); | |
1303 | if (!t) | |
1304 | return false; | |
1305 | ||
1306 | return t->sent && dns_packet_equal(t->sent, p); | |
1307 | } | |
1308 | ||
a4076574 LP |
1309 | DnsScope* manager_find_scope(Manager *m, DnsPacket *p) { |
1310 | Link *l; | |
1311 | ||
1312 | assert(m); | |
1313 | assert(p); | |
1314 | ||
1315 | l = hashmap_get(m->links, INT_TO_PTR(p->ifindex)); | |
1316 | if (!l) | |
1317 | return NULL; | |
1318 | ||
b4f1862d DM |
1319 | switch (p->protocol) { |
1320 | case DNS_PROTOCOL_LLMNR: | |
a4076574 LP |
1321 | if (p->family == AF_INET) |
1322 | return l->llmnr_ipv4_scope; | |
1323 | else if (p->family == AF_INET6) | |
1324 | return l->llmnr_ipv6_scope; | |
b4f1862d DM |
1325 | |
1326 | break; | |
1327 | ||
1328 | case DNS_PROTOCOL_MDNS: | |
1329 | if (p->family == AF_INET) | |
1330 | return l->mdns_ipv4_scope; | |
1331 | else if (p->family == AF_INET6) | |
1332 | return l->mdns_ipv6_scope; | |
1333 | ||
1334 | break; | |
1335 | ||
1336 | default: | |
1337 | break; | |
a4076574 LP |
1338 | } |
1339 | ||
1340 | return NULL; | |
1341 | } | |
1342 | ||
902bb5d8 LP |
1343 | void manager_verify_all(Manager *m) { |
1344 | DnsScope *s; | |
1345 | ||
1346 | assert(m); | |
1347 | ||
1348 | LIST_FOREACH(scopes, s, m->dns_scopes) | |
1349 | dns_zone_verify_all(&s->zone); | |
1350 | } | |
1351 | ||
78c6a153 | 1352 | int manager_is_own_hostname(Manager *m, const char *name) { |
78c6a153 LP |
1353 | int r; |
1354 | ||
1355 | assert(m); | |
1356 | assert(name); | |
1357 | ||
1358 | if (m->llmnr_hostname) { | |
1359 | r = dns_name_equal(name, m->llmnr_hostname); | |
1360 | if (r != 0) | |
1361 | return r; | |
1362 | } | |
1363 | ||
e96de0ce LP |
1364 | if (m->mdns_hostname) { |
1365 | r = dns_name_equal(name, m->mdns_hostname); | |
1366 | if (r != 0) | |
1367 | return r; | |
1368 | } | |
1369 | ||
1370 | if (m->full_hostname) | |
1371 | return dns_name_equal(name, m->full_hostname); | |
78c6a153 LP |
1372 | |
1373 | return 0; | |
1374 | } | |
1375 | ||
9176a57c LP |
1376 | int manager_compile_dns_servers(Manager *m, OrderedSet **dns) { |
1377 | DnsServer *s; | |
9176a57c LP |
1378 | Link *l; |
1379 | int r; | |
1380 | ||
1381 | assert(m); | |
1382 | assert(dns); | |
1383 | ||
1384 | r = ordered_set_ensure_allocated(dns, &dns_server_hash_ops); | |
1385 | if (r < 0) | |
1386 | return r; | |
1387 | ||
1388 | /* First add the system-wide servers and domains */ | |
1389 | LIST_FOREACH(servers, s, m->dns_servers) { | |
1390 | r = ordered_set_put(*dns, s); | |
1391 | if (r == -EEXIST) | |
1392 | continue; | |
1393 | if (r < 0) | |
1394 | return r; | |
1395 | } | |
1396 | ||
1397 | /* Then, add the per-link servers */ | |
90e74a66 | 1398 | HASHMAP_FOREACH(l, m->links) { |
9176a57c LP |
1399 | LIST_FOREACH(servers, s, l->dns_servers) { |
1400 | r = ordered_set_put(*dns, s); | |
1401 | if (r == -EEXIST) | |
1402 | continue; | |
1403 | if (r < 0) | |
1404 | return r; | |
1405 | } | |
1406 | } | |
1407 | ||
1408 | /* If we found nothing, add the fallback servers */ | |
1409 | if (ordered_set_isempty(*dns)) { | |
1410 | LIST_FOREACH(servers, s, m->fallback_dns_servers) { | |
1411 | r = ordered_set_put(*dns, s); | |
1412 | if (r == -EEXIST) | |
1413 | continue; | |
1414 | if (r < 0) | |
1415 | return r; | |
1416 | } | |
1417 | } | |
1418 | ||
1419 | return 0; | |
1420 | } | |
1421 | ||
94363cbb MP |
1422 | /* filter_route is a tri-state: |
1423 | * < 0: no filtering | |
1424 | * = 0 or false: return only domains which should be used for searching | |
1425 | * > 0 or true: return only domains which are for routing only | |
1426 | */ | |
6f7da49d | 1427 | int manager_compile_search_domains(Manager *m, OrderedSet **domains, int filter_route) { |
9176a57c | 1428 | DnsSearchDomain *d; |
9176a57c LP |
1429 | Link *l; |
1430 | int r; | |
1431 | ||
1432 | assert(m); | |
1433 | assert(domains); | |
1434 | ||
1435 | r = ordered_set_ensure_allocated(domains, &dns_name_hash_ops); | |
1436 | if (r < 0) | |
1437 | return r; | |
1438 | ||
1439 | LIST_FOREACH(domains, d, m->search_domains) { | |
6f7da49d LP |
1440 | |
1441 | if (filter_route >= 0 && | |
1442 | d->route_only != !!filter_route) | |
1443 | continue; | |
1444 | ||
9176a57c LP |
1445 | r = ordered_set_put(*domains, d->name); |
1446 | if (r == -EEXIST) | |
1447 | continue; | |
1448 | if (r < 0) | |
1449 | return r; | |
1450 | } | |
1451 | ||
90e74a66 | 1452 | HASHMAP_FOREACH(l, m->links) { |
9176a57c LP |
1453 | |
1454 | LIST_FOREACH(domains, d, l->search_domains) { | |
6f7da49d LP |
1455 | |
1456 | if (filter_route >= 0 && | |
1457 | d->route_only != !!filter_route) | |
1458 | continue; | |
1459 | ||
9176a57c LP |
1460 | r = ordered_set_put(*domains, d->name); |
1461 | if (r == -EEXIST) | |
1462 | continue; | |
1463 | if (r < 0) | |
1464 | return r; | |
1465 | } | |
1466 | } | |
1467 | ||
1468 | return 0; | |
1469 | } | |
c69fa7e3 LP |
1470 | |
1471 | DnssecMode manager_get_dnssec_mode(Manager *m) { | |
1472 | assert(m); | |
1473 | ||
1474 | if (m->dnssec_mode != _DNSSEC_MODE_INVALID) | |
1475 | return m->dnssec_mode; | |
1476 | ||
1477 | return DNSSEC_NO; | |
1478 | } | |
1479 | ||
1480 | bool manager_dnssec_supported(Manager *m) { | |
1481 | DnsServer *server; | |
c69fa7e3 LP |
1482 | Link *l; |
1483 | ||
1484 | assert(m); | |
1485 | ||
1486 | if (manager_get_dnssec_mode(m) == DNSSEC_NO) | |
1487 | return false; | |
1488 | ||
1489 | server = manager_get_dns_server(m); | |
1490 | if (server && !dns_server_dnssec_supported(server)) | |
1491 | return false; | |
1492 | ||
90e74a66 | 1493 | HASHMAP_FOREACH(l, m->links) |
c69fa7e3 LP |
1494 | if (!link_dnssec_supported(l)) |
1495 | return false; | |
1496 | ||
1497 | return true; | |
1498 | } | |
59c5b597 | 1499 | |
c9299be2 | 1500 | DnsOverTlsMode manager_get_dns_over_tls_mode(Manager *m) { |
5d67a7ae IT |
1501 | assert(m); |
1502 | ||
c9299be2 IT |
1503 | if (m->dns_over_tls_mode != _DNS_OVER_TLS_MODE_INVALID) |
1504 | return m->dns_over_tls_mode; | |
5d67a7ae | 1505 | |
c9299be2 | 1506 | return DNS_OVER_TLS_NO; |
5d67a7ae IT |
1507 | } |
1508 | ||
59c5b597 LP |
1509 | void manager_dnssec_verdict(Manager *m, DnssecVerdict verdict, const DnsResourceKey *key) { |
1510 | ||
1511 | assert(verdict >= 0); | |
1512 | assert(verdict < _DNSSEC_VERDICT_MAX); | |
1513 | ||
f1d34068 | 1514 | if (DEBUG_LOGGING) { |
202b76ae | 1515 | char s[DNS_RESOURCE_KEY_STRING_MAX]; |
59c5b597 | 1516 | |
202b76ae ZJS |
1517 | log_debug("Found verdict for lookup %s: %s", |
1518 | dns_resource_key_to_string(key, s, sizeof s), | |
1519 | dnssec_verdict_to_string(verdict)); | |
59c5b597 LP |
1520 | } |
1521 | ||
1522 | m->n_dnssec_verdict[verdict]++; | |
1523 | } | |
011696f7 | 1524 | |
de4a0138 | 1525 | bool manager_routable(Manager *m) { |
011696f7 LP |
1526 | Link *l; |
1527 | ||
1528 | assert(m); | |
1529 | ||
de4a0138 | 1530 | /* Returns true if the host has at least one interface with a routable address (regardless if IPv4 or IPv6) */ |
011696f7 | 1531 | |
90e74a66 | 1532 | HASHMAP_FOREACH(l, m->links) |
de4a0138 | 1533 | if (link_relevant(l, AF_UNSPEC, false)) |
011696f7 LP |
1534 | return true; |
1535 | ||
1536 | return false; | |
1537 | } | |
ba35662f | 1538 | |
90df0fbe | 1539 | void manager_flush_caches(Manager *m, int log_level) { |
ba35662f LP |
1540 | DnsScope *scope; |
1541 | ||
1542 | assert(m); | |
1543 | ||
1544 | LIST_FOREACH(scopes, scope, m->dns_scopes) | |
1545 | dns_cache_flush(&scope->cache); | |
aac57b35 | 1546 | |
90df0fbe | 1547 | log_full(log_level, "Flushed all caches."); |
ba35662f | 1548 | } |
943ef07c | 1549 | |
59c0fd0e | 1550 | void manager_reset_server_features(Manager *m) { |
59c0fd0e LP |
1551 | Link *l; |
1552 | ||
1553 | dns_server_reset_features_all(m->dns_servers); | |
1554 | dns_server_reset_features_all(m->fallback_dns_servers); | |
1555 | ||
90e74a66 | 1556 | HASHMAP_FOREACH(l, m->links) |
59c0fd0e LP |
1557 | dns_server_reset_features_all(l->dns_servers); |
1558 | ||
1559 | log_info("Resetting learnt feature levels on all servers."); | |
1560 | } | |
1561 | ||
943ef07c LP |
1562 | void manager_cleanup_saved_user(Manager *m) { |
1563 | _cleanup_closedir_ DIR *d = NULL; | |
1564 | struct dirent *de; | |
943ef07c LP |
1565 | |
1566 | assert(m); | |
1567 | ||
1568 | /* Clean up all saved per-link files in /run/systemd/resolve/netif/ that don't have a matching interface | |
1569 | * anymore. These files are created to persist settings pushed in by the user via the bus, so that resolved can | |
1570 | * be restarted without losing this data. */ | |
1571 | ||
1572 | d = opendir("/run/systemd/resolve/netif/"); | |
1573 | if (!d) { | |
1574 | if (errno == ENOENT) | |
1575 | return; | |
1576 | ||
1577 | log_warning_errno(errno, "Failed to open interface directory: %m"); | |
1578 | return; | |
1579 | } | |
1580 | ||
1581 | FOREACH_DIRENT_ALL(de, d, log_error_errno(errno, "Failed to read interface directory: %m")) { | |
1582 | _cleanup_free_ char *p = NULL; | |
1583 | int ifindex; | |
1584 | Link *l; | |
1585 | ||
1586 | if (!IN_SET(de->d_type, DT_UNKNOWN, DT_REG)) | |
1587 | continue; | |
1588 | ||
49bfc877 | 1589 | if (dot_or_dot_dot(de->d_name)) |
943ef07c LP |
1590 | continue; |
1591 | ||
597da51b ZJS |
1592 | ifindex = parse_ifindex(de->d_name); |
1593 | if (ifindex < 0) /* Probably some temporary file from a previous run. Delete it */ | |
943ef07c LP |
1594 | goto rm; |
1595 | ||
1596 | l = hashmap_get(m->links, INT_TO_PTR(ifindex)); | |
1597 | if (!l) /* link vanished */ | |
1598 | goto rm; | |
1599 | ||
1600 | if (l->is_managed) /* now managed by networkd, hence the bus settings are useless */ | |
1601 | goto rm; | |
1602 | ||
1603 | continue; | |
1604 | ||
1605 | rm: | |
b910cc72 | 1606 | p = path_join("/run/systemd/resolve/netif", de->d_name); |
943ef07c LP |
1607 | if (!p) { |
1608 | log_oom(); | |
1609 | return; | |
1610 | } | |
1611 | ||
1612 | (void) unlink(p); | |
1613 | } | |
1614 | } | |
e7c1b0e4 DR |
1615 | |
1616 | bool manager_next_dnssd_names(Manager *m) { | |
e7c1b0e4 DR |
1617 | DnssdService *s; |
1618 | bool tried = false; | |
1619 | int r; | |
1620 | ||
1621 | assert(m); | |
1622 | ||
90e74a66 | 1623 | HASHMAP_FOREACH(s, m->dnssd_services) { |
e7c1b0e4 DR |
1624 | _cleanup_free_ char * new_name = NULL; |
1625 | ||
1626 | if (!s->withdrawn) | |
1627 | continue; | |
1628 | ||
1629 | r = manager_next_random_name(s->name_template, &new_name); | |
1630 | if (r < 0) { | |
1631 | log_warning_errno(r, "Failed to get new name for service '%s': %m", s->name); | |
1632 | continue; | |
1633 | } | |
1634 | ||
1635 | free_and_replace(s->name_template, new_name); | |
1636 | ||
1637 | s->withdrawn = false; | |
1638 | ||
1639 | tried = true; | |
1640 | } | |
1641 | ||
1642 | if (tried) | |
1643 | manager_refresh_rrs(m); | |
1644 | ||
1645 | return tried; | |
1646 | } | |
49ef064c LP |
1647 | |
1648 | bool manager_server_is_stub(Manager *m, DnsServer *s) { | |
1649 | DnsStubListenerExtra *l; | |
1650 | ||
1651 | assert(m); | |
1652 | assert(s); | |
1653 | ||
1654 | /* Safety check: we generally already skip the main stub when parsing configuration. But let's be | |
1655 | * extra careful, and check here again */ | |
1656 | if (s->family == AF_INET && | |
1657 | s->address.in.s_addr == htobe32(INADDR_DNS_STUB) && | |
1658 | dns_server_port(s) == 53) | |
1659 | return true; | |
1660 | ||
1661 | /* Main reason to call this is to check server data against the extra listeners, and filter things | |
1662 | * out. */ | |
1663 | ORDERED_SET_FOREACH(l, m->dns_extra_stub_listeners) | |
1664 | if (s->family == l->family && | |
1665 | in_addr_equal(s->family, &s->address, &l->address) && | |
1666 | dns_server_port(s) == dns_stub_listener_extra_port(l)) | |
1667 | return true; | |
1668 | ||
1669 | return false; | |
1670 | } | |
eb170e75 LP |
1671 | |
1672 | int socket_disable_pmtud(int fd, int af) { | |
1673 | int r; | |
1674 | ||
1675 | assert(fd >= 0); | |
1676 | ||
1677 | if (af == AF_UNSPEC) { | |
1678 | r = socket_get_family(fd, &af); | |
1679 | if (r < 0) | |
1680 | return r; | |
1681 | } | |
1682 | ||
1683 | switch (af) { | |
1684 | ||
1685 | case AF_INET: { | |
1686 | /* Turn off path MTU discovery, let's rather fragment on the way than to open us up against | |
1687 | * PMTU forgery vulnerabilities. | |
1688 | * | |
1689 | * There appears to be no documentation about IP_PMTUDISC_OMIT, but it has the effect that | |
1690 | * the "Don't Fragment" bit in the IPv4 header is turned off, thus enforcing fragmentation if | |
1691 | * our datagram size exceeds the MTU of a router in the path, and turning off path MTU | |
1692 | * discovery. | |
1693 | * | |
1694 | * This helps mitigating the PMTUD vulnerability described here: | |
1695 | * | |
1696 | * https://blog.apnic.net/2019/07/12/its-time-to-consider-avoiding-ip-fragmentation-in-the-dns/ | |
1697 | * | |
1698 | * Similar logic is in place in most DNS servers. | |
1699 | * | |
1700 | * There are multiple conflicting goals: we want to allow the largest datagrams possible (for | |
1701 | * efficiency reasons), but not have fragmentation (for security reasons), nor use PMTUD (for | |
1702 | * security reasons, too). Our strategy to deal with this is: use large packets, turn off | |
1703 | * PMTUD, but watch fragmentation taking place, and then size our packets to the max of the | |
1704 | * fragments seen — and if we need larger packets always go to TCP. | |
1705 | */ | |
1706 | ||
1707 | r = setsockopt_int(fd, IPPROTO_IP, IP_MTU_DISCOVER, IP_PMTUDISC_OMIT); | |
1708 | if (r < 0) | |
1709 | return r; | |
1710 | ||
1711 | return 0; | |
1712 | } | |
1713 | ||
1714 | case AF_INET6: { | |
1715 | /* On IPv6 fragmentation only is done by the sender — never by routers on the path. PMTUD is | |
1716 | * mandatory. If we want to turn off PMTUD, the only way is by sending with minimal MTU only, | |
1717 | * so that we apply maximum fragmentation locally already, and thus PMTUD doesn't happen | |
1718 | * because there's nothing that could be fragmented further anymore. */ | |
1719 | ||
1720 | r = setsockopt_int(fd, IPPROTO_IPV6, IPV6_MTU, IPV6_MIN_MTU); | |
1721 | if (r < 0) | |
1722 | return r; | |
1723 | ||
1724 | return 0; | |
1725 | } | |
1726 | ||
1727 | default: | |
1728 | return -EAFNOSUPPORT; | |
1729 | } | |
1730 | } |