]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
091a364c | 2 | /*** |
96b2fb93 | 3 | Copyright © 2014 Tom Gundersen <teg@jklm.no> |
091a364c TG |
4 | ***/ |
5 | ||
07630cea LP |
6 | #include <netinet/in.h> |
7 | #include <poll.h> | |
0d536673 | 8 | #include <stdio_ext.h> |
74b2466e | 9 | #include <sys/ioctl.h> |
091a364c | 10 | |
349cc4a5 | 11 | #if HAVE_LIBIDN2 |
87057e24 ZJS |
12 | #include <idn2.h> |
13 | #endif | |
14 | ||
a2a416f7 | 15 | #include "af-list.h" |
b5efdb8a | 16 | #include "alloc-util.h" |
943ef07c | 17 | #include "dirent-util.h" |
07630cea | 18 | #include "dns-domain.h" |
3ffd4af2 | 19 | #include "fd-util.h" |
a5a807e6 | 20 | #include "fileio-label.h" |
07630cea | 21 | #include "hostname-util.h" |
c004493c | 22 | #include "io-util.h" |
07630cea LP |
23 | #include "netlink-util.h" |
24 | #include "network-internal.h" | |
822db23c | 25 | #include "ordered-set.h" |
6bedfcbb | 26 | #include "parse-util.h" |
3df3e884 | 27 | #include "random-util.h" |
39d8db04 | 28 | #include "resolved-bus.h" |
07630cea | 29 | #include "resolved-conf.h" |
6501dd31 | 30 | #include "resolved-dnssd.h" |
b30bf55d | 31 | #include "resolved-dns-stub.h" |
dd0bc0f1 | 32 | #include "resolved-etc-hosts.h" |
5f402ae8 | 33 | #include "resolved-llmnr.h" |
07630cea | 34 | #include "resolved-manager.h" |
bc7702b0 | 35 | #include "resolved-mdns.h" |
dd0bc0f1 | 36 | #include "resolved-resolv-conf.h" |
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 LP |
83 | if (is_new) |
84 | log_debug("Found new link %i/%s", ifindex, l->name); | |
85 | ||
74b2466e | 86 | break; |
a2a416f7 | 87 | } |
74b2466e LP |
88 | |
89 | case RTM_DELLINK: | |
90 | if (l) { | |
a2a416f7 | 91 | log_debug("Removing link %i/%s", l->ifindex, l->name); |
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 | ||
1c4baffc | 206 | r = sd_netlink_add_match(m->rtnl, RTM_NEWLINK, manager_process_link, m); |
74b2466e LP |
207 | if (r < 0) |
208 | return r; | |
209 | ||
1c4baffc | 210 | r = sd_netlink_add_match(m->rtnl, RTM_DELLINK, manager_process_link, m); |
74b2466e LP |
211 | if (r < 0) |
212 | return r; | |
213 | ||
1c4baffc | 214 | r = sd_netlink_add_match(m->rtnl, RTM_NEWADDR, manager_process_address, m); |
74b2466e LP |
215 | if (r < 0) |
216 | return r; | |
091a364c | 217 | |
1c4baffc | 218 | r = sd_netlink_add_match(m->rtnl, RTM_DELADDR, manager_process_address, m); |
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; | |
268 | Iterator i; | |
269 | Link *l; | |
270 | int r; | |
271 | ||
272 | assert(m); | |
273 | ||
274 | sd_network_monitor_flush(m->network_monitor); | |
275 | ||
276 | HASHMAP_FOREACH(l, m->links, i) { | |
943ef07c | 277 | r = link_update(l); |
74b2466e | 278 | if (r < 0) |
da927ba9 | 279 | log_warning_errno(r, "Failed to update monitor information for %i: %m", l->ifindex); |
74b2466e LP |
280 | } |
281 | ||
7207052d | 282 | (void) manager_write_resolv_conf(m); |
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 | ||
e96de0ce | 317 | static int determine_hostname(char **full_hostname, char **llmnr_hostname, char **mdns_hostname) { |
eb60f9cd | 318 | _cleanup_free_ char *h = NULL, *n = NULL; |
349cc4a5 | 319 | #if HAVE_LIBIDN2 |
87057e24 | 320 | _cleanup_free_ char *utf8 = NULL; |
349cc4a5 | 321 | #elif HAVE_LIBIDN |
87057e24 ZJS |
322 | int k; |
323 | #endif | |
78c6a153 | 324 | char label[DNS_LABEL_MAX]; |
87057e24 ZJS |
325 | const char *p, *decoded; |
326 | int r; | |
eb60f9cd | 327 | |
e96de0ce | 328 | assert(full_hostname); |
78c6a153 LP |
329 | assert(llmnr_hostname); |
330 | assert(mdns_hostname); | |
331 | ||
a25b0dc8 | 332 | /* Extract and normalize the first label of the locally configured hostname, and check it's not "localhost". */ |
eb60f9cd | 333 | |
a25b0dc8 LP |
334 | r = gethostname_strict(&h); |
335 | if (r < 0) | |
336 | return log_debug_errno(r, "Can't determine system hostname: %m"); | |
eb60f9cd | 337 | |
78c6a153 | 338 | p = h; |
87057e24 | 339 | r = dns_label_unescape(&p, label, sizeof label); |
78c6a153 LP |
340 | if (r < 0) |
341 | return log_error_errno(r, "Failed to unescape host name: %m"); | |
342 | if (r == 0) { | |
a25b0dc8 | 343 | log_error("Couldn't find a single label in hostname."); |
78c6a153 LP |
344 | return -EINVAL; |
345 | } | |
346 | ||
349cc4a5 | 347 | #if HAVE_LIBIDN2 |
87057e24 ZJS |
348 | r = idn2_to_unicode_8z8z(label, &utf8, 0); |
349 | if (r != IDN2_OK) | |
350 | return log_error("Failed to undo IDNA: %s", idn2_strerror(r)); | |
351 | assert(utf8_is_valid(utf8)); | |
352 | ||
353 | r = strlen(utf8); | |
354 | decoded = utf8; | |
349cc4a5 | 355 | #elif HAVE_LIBIDN |
87057e24 | 356 | k = dns_label_undo_idna(label, r, label, sizeof label); |
78c6a153 LP |
357 | if (k < 0) |
358 | return log_error_errno(k, "Failed to undo IDNA: %m"); | |
359 | if (k > 0) | |
360 | r = k; | |
361 | ||
362 | if (!utf8_is_valid(label)) { | |
eb60f9cd LP |
363 | log_error("System hostname is not UTF-8 clean."); |
364 | return -EINVAL; | |
365 | } | |
87057e24 ZJS |
366 | decoded = label; |
367 | #else | |
368 | decoded = label; /* no decoding */ | |
369 | #endif | |
eb60f9cd | 370 | |
87057e24 | 371 | r = dns_label_escape_new(decoded, r, &n); |
78c6a153 LP |
372 | if (r < 0) |
373 | return log_error_errno(r, "Failed to escape host name: %m"); | |
374 | ||
375 | if (is_localhost(n)) { | |
376 | log_debug("System hostname is 'localhost', ignoring."); | |
377 | return -EINVAL; | |
eb60f9cd LP |
378 | } |
379 | ||
78c6a153 LP |
380 | r = dns_name_concat(n, "local", mdns_hostname); |
381 | if (r < 0) | |
382 | return log_error_errno(r, "Failed to determine mDNS hostname: %m"); | |
383 | ||
ae2a15bc LP |
384 | *llmnr_hostname = TAKE_PTR(n); |
385 | *full_hostname = TAKE_PTR(h); | |
e96de0ce | 386 | |
eb60f9cd LP |
387 | return 0; |
388 | } | |
389 | ||
a25b0dc8 LP |
390 | static const char *fallback_hostname(void) { |
391 | ||
392 | /* Determine the fall back hostname. For exposing this system to the outside world, we cannot have it to be | |
393 | * "localhost" even if that's the compiled in hostname. In this case, let's revert to "linux" instead. */ | |
394 | ||
395 | if (is_localhost(FALLBACK_HOSTNAME)) | |
396 | return "linux"; | |
397 | ||
398 | return FALLBACK_HOSTNAME; | |
399 | } | |
400 | ||
401 | static int make_fallback_hostnames(char **full_hostname, char **llmnr_hostname, char **mdns_hostname) { | |
402 | _cleanup_free_ char *n = NULL, *m = NULL; | |
403 | char label[DNS_LABEL_MAX], *h; | |
404 | const char *p; | |
405 | int r; | |
406 | ||
407 | assert(full_hostname); | |
408 | assert(llmnr_hostname); | |
409 | assert(mdns_hostname); | |
410 | ||
411 | p = fallback_hostname(); | |
412 | r = dns_label_unescape(&p, label, sizeof(label)); | |
413 | if (r < 0) | |
414 | return log_error_errno(r, "Failed to unescape fallback host name: %m"); | |
415 | ||
416 | assert(r > 0); /* The fallback hostname must have at least one label */ | |
417 | ||
418 | r = dns_label_escape_new(label, r, &n); | |
419 | if (r < 0) | |
420 | return log_error_errno(r, "Failed to escape fallback hostname: %m"); | |
421 | ||
422 | r = dns_name_concat(n, "local", &m); | |
423 | if (r < 0) | |
424 | return log_error_errno(r, "Failed to concatenate mDNS hostname: %m"); | |
425 | ||
426 | h = strdup(fallback_hostname()); | |
427 | if (!h) | |
428 | return log_oom(); | |
429 | ||
ae2a15bc LP |
430 | *llmnr_hostname = TAKE_PTR(n); |
431 | *mdns_hostname = TAKE_PTR(m); | |
a25b0dc8 LP |
432 | |
433 | *full_hostname = h; | |
434 | ||
435 | return 0; | |
436 | } | |
437 | ||
eb60f9cd | 438 | static int on_hostname_change(sd_event_source *es, int fd, uint32_t revents, void *userdata) { |
e96de0ce | 439 | _cleanup_free_ char *full_hostname = NULL, *llmnr_hostname = NULL, *mdns_hostname = NULL; |
eb60f9cd LP |
440 | Manager *m = userdata; |
441 | int r; | |
442 | ||
443 | assert(m); | |
444 | ||
e96de0ce | 445 | r = determine_hostname(&full_hostname, &llmnr_hostname, &mdns_hostname); |
eb60f9cd LP |
446 | if (r < 0) |
447 | return 0; /* ignore invalid hostnames */ | |
448 | ||
e96de0ce LP |
449 | if (streq(full_hostname, m->full_hostname) && |
450 | streq(llmnr_hostname, m->llmnr_hostname) && | |
451 | streq(mdns_hostname, m->mdns_hostname)) | |
eb60f9cd LP |
452 | return 0; |
453 | ||
e96de0ce | 454 | log_info("System hostname changed to '%s'.", full_hostname); |
78c6a153 | 455 | |
e96de0ce LP |
456 | free_and_replace(m->full_hostname, full_hostname); |
457 | free_and_replace(m->llmnr_hostname, llmnr_hostname); | |
458 | free_and_replace(m->mdns_hostname, mdns_hostname); | |
eb60f9cd LP |
459 | |
460 | manager_refresh_rrs(m); | |
461 | ||
462 | return 0; | |
463 | } | |
464 | ||
465 | static int manager_watch_hostname(Manager *m) { | |
eb60f9cd LP |
466 | int r; |
467 | ||
468 | assert(m); | |
469 | ||
db4a47e9 LP |
470 | m->hostname_fd = open("/proc/sys/kernel/hostname", |
471 | O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); | |
eb60f9cd | 472 | if (m->hostname_fd < 0) { |
56f64d95 | 473 | log_warning_errno(errno, "Failed to watch hostname: %m"); |
eb60f9cd LP |
474 | return 0; |
475 | } | |
476 | ||
477 | r = sd_event_add_io(m->event, &m->hostname_event_source, m->hostname_fd, 0, on_hostname_change, m); | |
478 | if (r < 0) { | |
479 | if (r == -EPERM) | |
480 | /* kernels prior to 3.2 don't support polling this file. Ignore the failure. */ | |
481 | m->hostname_fd = safe_close(m->hostname_fd); | |
8d3d7072 MS |
482 | else |
483 | return log_error_errno(r, "Failed to add hostname event source: %m"); | |
eb60f9cd LP |
484 | } |
485 | ||
aa4a9deb LP |
486 | (void) sd_event_source_set_description(m->hostname_event_source, "hostname"); |
487 | ||
e96de0ce | 488 | r = determine_hostname(&m->full_hostname, &m->llmnr_hostname, &m->mdns_hostname); |
eb60f9cd | 489 | if (r < 0) { |
a25b0dc8 | 490 | log_info("Defaulting to hostname '%s'.", fallback_hostname()); |
78c6a153 | 491 | |
a25b0dc8 LP |
492 | r = make_fallback_hostnames(&m->full_hostname, &m->llmnr_hostname, &m->mdns_hostname); |
493 | if (r < 0) | |
494 | return r; | |
eb60f9cd | 495 | } else |
e96de0ce | 496 | log_info("Using system hostname '%s'.", m->full_hostname); |
eb60f9cd LP |
497 | |
498 | return 0; | |
499 | } | |
500 | ||
4d506d6b LP |
501 | static int manager_sigusr1(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { |
502 | _cleanup_free_ char *buffer = NULL; | |
503 | _cleanup_fclose_ FILE *f = NULL; | |
504 | Manager *m = userdata; | |
cf84484a | 505 | DnsServer *server; |
4d506d6b LP |
506 | size_t size = 0; |
507 | DnsScope *scope; | |
cf84484a LP |
508 | Iterator i; |
509 | Link *l; | |
4d506d6b LP |
510 | |
511 | assert(s); | |
512 | assert(si); | |
513 | assert(m); | |
514 | ||
515 | f = open_memstream(&buffer, &size); | |
516 | if (!f) | |
517 | return log_oom(); | |
518 | ||
0d536673 LP |
519 | (void) __fsetlocking(f, FSETLOCKING_BYCALLER); |
520 | ||
4d506d6b LP |
521 | LIST_FOREACH(scopes, scope, m->dns_scopes) |
522 | dns_scope_dump(scope, f); | |
523 | ||
cf84484a LP |
524 | LIST_FOREACH(servers, server, m->dns_servers) |
525 | dns_server_dump(server, f); | |
526 | LIST_FOREACH(servers, server, m->fallback_dns_servers) | |
527 | dns_server_dump(server, f); | |
528 | HASHMAP_FOREACH(l, m->links, i) | |
529 | LIST_FOREACH(servers, server, l->dns_servers) | |
530 | dns_server_dump(server, f); | |
531 | ||
4d506d6b LP |
532 | if (fflush_and_check(f) < 0) |
533 | return log_oom(); | |
534 | ||
535 | log_dump(LOG_INFO, buffer); | |
536 | return 0; | |
537 | } | |
538 | ||
bc81447e LP |
539 | static int manager_sigusr2(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { |
540 | Manager *m = userdata; | |
bc81447e LP |
541 | |
542 | assert(s); | |
543 | assert(si); | |
544 | assert(m); | |
545 | ||
ba35662f | 546 | manager_flush_caches(m); |
ba35662f | 547 | |
bc81447e LP |
548 | return 0; |
549 | } | |
550 | ||
d55b0463 LP |
551 | static int manager_sigrtmin1(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { |
552 | Manager *m = userdata; | |
553 | ||
554 | assert(s); | |
555 | assert(si); | |
556 | assert(m); | |
557 | ||
558 | manager_reset_server_features(m); | |
559 | return 0; | |
560 | } | |
561 | ||
091a364c | 562 | int manager_new(Manager **ret) { |
74b2466e | 563 | _cleanup_(manager_freep) Manager *m = NULL; |
091a364c TG |
564 | int r; |
565 | ||
c92e531c LP |
566 | assert(ret); |
567 | ||
091a364c TG |
568 | m = new0(Manager, 1); |
569 | if (!m) | |
570 | return -ENOMEM; | |
571 | ||
1716f6dc | 572 | m->llmnr_ipv4_udp_fd = m->llmnr_ipv6_udp_fd = -1; |
623a4c97 | 573 | m->llmnr_ipv4_tcp_fd = m->llmnr_ipv6_tcp_fd = -1; |
bc7702b0 | 574 | m->mdns_ipv4_fd = m->mdns_ipv6_fd = -1; |
b30bf55d | 575 | m->dns_stub_udp_fd = m->dns_stub_tcp_fd = -1; |
eb60f9cd | 576 | m->hostname_fd = -1; |
1716f6dc | 577 | |
af49ca27 | 578 | m->llmnr_support = RESOLVE_SUPPORT_YES; |
7bef8e8e | 579 | m->mdns_support = RESOLVE_SUPPORT_YES; |
61ecb465 | 580 | m->dnssec_mode = DEFAULT_DNSSEC_MODE; |
c9299be2 | 581 | m->dns_over_tls_mode = DEFAULT_DNS_OVER_TLS_MODE; |
ceeddf79 | 582 | m->enable_cache = true; |
1ae43295 | 583 | m->dns_stub_listener_mode = DNS_STUB_LISTENER_UDP; |
5cb36f41 | 584 | m->read_resolv_conf = true; |
00fa60ae | 585 | m->need_builtin_fallbacks = true; |
dd0bc0f1 | 586 | m->etc_hosts_last = m->etc_hosts_mtime = USEC_INFINITY; |
091a364c | 587 | |
0d2cd476 LP |
588 | r = dns_trust_anchor_load(&m->trust_anchor); |
589 | if (r < 0) | |
590 | return r; | |
591 | ||
ad6c0475 LP |
592 | r = manager_parse_config_file(m); |
593 | if (r < 0) | |
5ce497b5 | 594 | log_warning_errno(r, "Failed to parse configuration file: %m"); |
ad6c0475 | 595 | |
091a364c TG |
596 | r = sd_event_default(&m->event); |
597 | if (r < 0) | |
598 | return r; | |
599 | ||
600 | sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL); | |
601 | sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL); | |
602 | ||
603 | sd_event_set_watchdog(m->event, true); | |
604 | ||
eb60f9cd LP |
605 | r = manager_watch_hostname(m); |
606 | if (r < 0) | |
607 | return r; | |
608 | ||
6501dd31 DR |
609 | r = dnssd_load(m); |
610 | if (r < 0) | |
611 | log_warning_errno(r, "Failed to load DNS-SD configuration files: %m"); | |
612 | ||
1716f6dc | 613 | r = dns_scope_new(m, &m->unicast_scope, NULL, DNS_PROTOCOL_DNS, AF_UNSPEC); |
74b2466e LP |
614 | if (r < 0) |
615 | return r; | |
616 | ||
617 | r = manager_network_monitor_listen(m); | |
618 | if (r < 0) | |
619 | return r; | |
620 | ||
621 | r = manager_rtnl_listen(m); | |
622 | if (r < 0) | |
623 | return r; | |
624 | ||
625 | r = manager_connect_bus(m); | |
626 | if (r < 0) | |
627 | return r; | |
628 | ||
4d506d6b | 629 | (void) sd_event_add_signal(m->event, &m->sigusr1_event_source, SIGUSR1, manager_sigusr1, m); |
bc81447e | 630 | (void) sd_event_add_signal(m->event, &m->sigusr2_event_source, SIGUSR2, manager_sigusr2, m); |
d55b0463 | 631 | (void) sd_event_add_signal(m->event, &m->sigrtmin1_event_source, SIGRTMIN+1, manager_sigrtmin1, m); |
4d506d6b | 632 | |
943ef07c LP |
633 | manager_cleanup_saved_user(m); |
634 | ||
1cc6c93a | 635 | *ret = TAKE_PTR(m); |
091a364c TG |
636 | |
637 | return 0; | |
638 | } | |
639 | ||
edc501d4 LP |
640 | int manager_start(Manager *m) { |
641 | int r; | |
642 | ||
643 | assert(m); | |
644 | ||
b30bf55d LP |
645 | r = manager_dns_stub_start(m); |
646 | if (r < 0) | |
647 | return r; | |
648 | ||
edc501d4 LP |
649 | return 0; |
650 | } | |
651 | ||
74b2466e LP |
652 | Manager *manager_free(Manager *m) { |
653 | Link *l; | |
6501dd31 | 654 | DnssdService *s; |
091a364c TG |
655 | |
656 | if (!m) | |
74b2466e LP |
657 | return NULL; |
658 | ||
4b95f179 LP |
659 | dns_server_unlink_all(m->dns_servers); |
660 | dns_server_unlink_all(m->fallback_dns_servers); | |
a51c1048 LP |
661 | dns_search_domain_unlink_all(m->search_domains); |
662 | ||
74b2466e LP |
663 | while ((l = hashmap_first(m->links))) |
664 | link_free(l); | |
f0e15467 LP |
665 | |
666 | while (m->dns_queries) | |
667 | dns_query_free(m->dns_queries); | |
74b2466e | 668 | |
cab5b059 LP |
669 | dns_scope_free(m->unicast_scope); |
670 | ||
b30bf55d LP |
671 | /* At this point only orphaned streams should remain. All others should have been freed already by their |
672 | * owners */ | |
673 | while (m->dns_streams) | |
674 | dns_stream_unref(m->dns_streams); | |
675 | ||
f0e15467 LP |
676 | hashmap_free(m->links); |
677 | hashmap_free(m->dns_transactions); | |
678 | ||
096b6773 LP |
679 | sd_event_source_unref(m->network_event_source); |
680 | sd_network_monitor_unref(m->network_monitor); | |
091a364c | 681 | |
a564ca2f LP |
682 | sd_netlink_unref(m->rtnl); |
683 | sd_event_source_unref(m->rtnl_event_source); | |
684 | ||
edc501d4 | 685 | manager_llmnr_stop(m); |
bc7702b0 | 686 | manager_mdns_stop(m); |
b30bf55d | 687 | manager_dns_stub_stop(m); |
623a4c97 | 688 | |
902bb5d8 | 689 | sd_bus_slot_unref(m->prepare_for_sleep_slot); |
74b2466e | 690 | sd_bus_unref(m->bus); |
091a364c | 691 | |
4d506d6b | 692 | sd_event_source_unref(m->sigusr1_event_source); |
bc81447e | 693 | sd_event_source_unref(m->sigusr2_event_source); |
d55b0463 | 694 | sd_event_source_unref(m->sigrtmin1_event_source); |
4d506d6b | 695 | |
74b2466e | 696 | sd_event_unref(m->event); |
623a4c97 | 697 | |
78c6a153 LP |
698 | dns_resource_key_unref(m->llmnr_host_ipv4_key); |
699 | dns_resource_key_unref(m->llmnr_host_ipv6_key); | |
400cb36e DR |
700 | dns_resource_key_unref(m->mdns_host_ipv4_key); |
701 | dns_resource_key_unref(m->mdns_host_ipv6_key); | |
eb60f9cd | 702 | |
eb60f9cd | 703 | sd_event_source_unref(m->hostname_event_source); |
d9fcf2ba | 704 | safe_close(m->hostname_fd); |
e96de0ce LP |
705 | |
706 | free(m->full_hostname); | |
78c6a153 LP |
707 | free(m->llmnr_hostname); |
708 | free(m->mdns_hostname); | |
eb60f9cd | 709 | |
6501dd31 DR |
710 | while ((s = hashmap_first(m->dnssd_services))) |
711 | dnssd_service_free(s); | |
712 | hashmap_free(m->dnssd_services); | |
713 | ||
0d2cd476 | 714 | dns_trust_anchor_flush(&m->trust_anchor); |
dd0bc0f1 | 715 | manager_etc_hosts_flush(m); |
0d2cd476 | 716 | |
6b430fdb | 717 | return mfree(m); |
091a364c TG |
718 | } |
719 | ||
1716f6dc | 720 | int manager_recv(Manager *m, int fd, DnsProtocol protocol, DnsPacket **ret) { |
74b2466e | 721 | _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL; |
1716f6dc LP |
722 | union { |
723 | struct cmsghdr header; /* For alignment */ | |
40a1eebd | 724 | uint8_t buffer[CMSG_SPACE(MAXSIZE(struct in_pktinfo, struct in6_pktinfo)) |
1716f6dc | 725 | + CMSG_SPACE(int) /* ttl/hoplimit */ |
623a4c97 | 726 | + EXTRA_CMSG_SPACE /* kernel appears to require extra buffer space */]; |
1716f6dc LP |
727 | } control; |
728 | union sockaddr_union sa; | |
74b2466e | 729 | struct msghdr mh = {}; |
1716f6dc | 730 | struct cmsghdr *cmsg; |
74b2466e | 731 | struct iovec iov; |
4edc2c9b LP |
732 | ssize_t ms, l; |
733 | int r; | |
74b2466e LP |
734 | |
735 | assert(m); | |
1716f6dc | 736 | assert(fd >= 0); |
74b2466e LP |
737 | assert(ret); |
738 | ||
4edc2c9b | 739 | ms = next_datagram_size_fd(fd); |
74b2466e | 740 | if (ms < 0) |
4edc2c9b | 741 | return ms; |
74b2466e | 742 | |
51027656 | 743 | r = dns_packet_new(&p, protocol, ms, DNS_PACKET_SIZE_MAX); |
74b2466e LP |
744 | if (r < 0) |
745 | return r; | |
746 | ||
747 | iov.iov_base = DNS_PACKET_DATA(p); | |
748 | iov.iov_len = p->allocated; | |
749 | ||
1716f6dc LP |
750 | mh.msg_name = &sa.sa; |
751 | mh.msg_namelen = sizeof(sa); | |
74b2466e LP |
752 | mh.msg_iov = &iov; |
753 | mh.msg_iovlen = 1; | |
1716f6dc LP |
754 | mh.msg_control = &control; |
755 | mh.msg_controllen = sizeof(control); | |
74b2466e | 756 | |
a38d9945 | 757 | l = recvmsg(fd, &mh, 0); |
f134289a EV |
758 | if (l == 0) |
759 | return 0; | |
74b2466e | 760 | if (l < 0) { |
4c701096 | 761 | if (IN_SET(errno, EAGAIN, EINTR)) |
74b2466e LP |
762 | return 0; |
763 | ||
764 | return -errno; | |
091a364c TG |
765 | } |
766 | ||
1716f6dc LP |
767 | assert(!(mh.msg_flags & MSG_CTRUNC)); |
768 | assert(!(mh.msg_flags & MSG_TRUNC)); | |
769 | ||
74b2466e | 770 | p->size = (size_t) l; |
091a364c | 771 | |
1716f6dc | 772 | p->family = sa.sa.sa_family; |
623a4c97 LP |
773 | p->ipproto = IPPROTO_UDP; |
774 | if (p->family == AF_INET) { | |
1716f6dc | 775 | p->sender.in = sa.in.sin_addr; |
623a4c97 LP |
776 | p->sender_port = be16toh(sa.in.sin_port); |
777 | } else if (p->family == AF_INET6) { | |
1716f6dc | 778 | p->sender.in6 = sa.in6.sin6_addr; |
623a4c97 LP |
779 | p->sender_port = be16toh(sa.in6.sin6_port); |
780 | p->ifindex = sa.in6.sin6_scope_id; | |
781 | } else | |
1716f6dc | 782 | return -EAFNOSUPPORT; |
74b2466e | 783 | |
2a1288ff | 784 | CMSG_FOREACH(cmsg, &mh) { |
74b2466e | 785 | |
1716f6dc LP |
786 | if (cmsg->cmsg_level == IPPROTO_IPV6) { |
787 | assert(p->family == AF_INET6); | |
74b2466e | 788 | |
1716f6dc | 789 | switch (cmsg->cmsg_type) { |
74b2466e | 790 | |
1716f6dc LP |
791 | case IPV6_PKTINFO: { |
792 | struct in6_pktinfo *i = (struct in6_pktinfo*) CMSG_DATA(cmsg); | |
74b2466e | 793 | |
623a4c97 LP |
794 | if (p->ifindex <= 0) |
795 | p->ifindex = i->ipi6_ifindex; | |
796 | ||
1716f6dc LP |
797 | p->destination.in6 = i->ipi6_addr; |
798 | break; | |
799 | } | |
74b2466e | 800 | |
1716f6dc LP |
801 | case IPV6_HOPLIMIT: |
802 | p->ttl = *(int *) CMSG_DATA(cmsg); | |
803 | break; | |
74b2466e | 804 | |
1716f6dc LP |
805 | } |
806 | } else if (cmsg->cmsg_level == IPPROTO_IP) { | |
807 | assert(p->family == AF_INET); | |
74b2466e | 808 | |
1716f6dc | 809 | switch (cmsg->cmsg_type) { |
74b2466e | 810 | |
1716f6dc LP |
811 | case IP_PKTINFO: { |
812 | struct in_pktinfo *i = (struct in_pktinfo*) CMSG_DATA(cmsg); | |
091a364c | 813 | |
623a4c97 LP |
814 | if (p->ifindex <= 0) |
815 | p->ifindex = i->ipi_ifindex; | |
816 | ||
1716f6dc LP |
817 | p->destination.in = i->ipi_addr; |
818 | break; | |
819 | } | |
74b2466e | 820 | |
623a4c97 | 821 | case IP_TTL: |
1716f6dc LP |
822 | p->ttl = *(int *) CMSG_DATA(cmsg); |
823 | break; | |
824 | } | |
825 | } | |
826 | } | |
74b2466e | 827 | |
623a4c97 LP |
828 | /* The Linux kernel sets the interface index to the loopback |
829 | * device if the packet came from the local host since it | |
830 | * avoids the routing table in such a case. Let's unset the | |
831 | * interface index in such a case. */ | |
a5f03596 | 832 | if (p->ifindex == LOOPBACK_IFINDEX) |
623a4c97 LP |
833 | p->ifindex = 0; |
834 | ||
86ad4cd7 TG |
835 | if (protocol != DNS_PROTOCOL_DNS) { |
836 | /* If we don't know the interface index still, we look for the | |
837 | * first local interface with a matching address. Yuck! */ | |
838 | if (p->ifindex <= 0) | |
839 | p->ifindex = manager_find_ifindex(m, p->family, &p->destination); | |
840 | } | |
623a4c97 | 841 | |
1cc6c93a | 842 | *ret = TAKE_PTR(p); |
74b2466e LP |
843 | |
844 | return 1; | |
845 | } | |
846 | ||
74b2466e LP |
847 | static int sendmsg_loop(int fd, struct msghdr *mh, int flags) { |
848 | int r; | |
849 | ||
850 | assert(fd >= 0); | |
851 | assert(mh); | |
852 | ||
853 | for (;;) { | |
854 | if (sendmsg(fd, mh, flags) >= 0) | |
855 | return 0; | |
856 | ||
857 | if (errno == EINTR) | |
858 | continue; | |
859 | ||
860 | if (errno != EAGAIN) | |
861 | return -errno; | |
862 | ||
863 | r = fd_wait_for_event(fd, POLLOUT, SEND_TIMEOUT_USEC); | |
864 | if (r < 0) | |
865 | return r; | |
866 | if (r == 0) | |
867 | return -ETIMEDOUT; | |
868 | } | |
869 | } | |
870 | ||
72290734 TG |
871 | static int write_loop(int fd, void *message, size_t length) { |
872 | int r; | |
873 | ||
874 | assert(fd >= 0); | |
875 | assert(message); | |
876 | ||
877 | for (;;) { | |
878 | if (write(fd, message, length) >= 0) | |
879 | return 0; | |
880 | ||
881 | if (errno == EINTR) | |
882 | continue; | |
883 | ||
884 | if (errno != EAGAIN) | |
885 | return -errno; | |
886 | ||
887 | r = fd_wait_for_event(fd, POLLOUT, SEND_TIMEOUT_USEC); | |
888 | if (r < 0) | |
889 | return r; | |
890 | if (r == 0) | |
891 | return -ETIMEDOUT; | |
892 | } | |
893 | } | |
894 | ||
895 | int manager_write(Manager *m, int fd, DnsPacket *p) { | |
896 | int r; | |
897 | ||
deb3f3d3 | 898 | log_debug("Sending %s packet with id %" PRIu16 ".", DNS_PACKET_QR(p) ? "response" : "query", DNS_PACKET_ID(p)); |
72290734 TG |
899 | |
900 | r = write_loop(fd, DNS_PACKET_DATA(p), p->size); | |
901 | if (r < 0) | |
902 | return r; | |
903 | ||
904 | return 0; | |
905 | } | |
906 | ||
b30bf55d LP |
907 | static int manager_ipv4_send( |
908 | Manager *m, | |
909 | int fd, | |
910 | int ifindex, | |
911 | const struct in_addr *destination, | |
912 | uint16_t port, | |
913 | const struct in_addr *source, | |
914 | DnsPacket *p) { | |
74b2466e LP |
915 | union sockaddr_union sa = { |
916 | .in.sin_family = AF_INET, | |
74b2466e | 917 | }; |
1716f6dc LP |
918 | union { |
919 | struct cmsghdr header; /* For alignment */ | |
920 | uint8_t buffer[CMSG_SPACE(sizeof(struct in_pktinfo))]; | |
921 | } control; | |
74b2466e LP |
922 | struct msghdr mh = {}; |
923 | struct iovec iov; | |
74b2466e LP |
924 | |
925 | assert(m); | |
1716f6dc | 926 | assert(fd >= 0); |
b30bf55d | 927 | assert(destination); |
1716f6dc | 928 | assert(port > 0); |
74b2466e LP |
929 | assert(p); |
930 | ||
74b2466e LP |
931 | iov.iov_base = DNS_PACKET_DATA(p); |
932 | iov.iov_len = p->size; | |
091a364c | 933 | |
b30bf55d | 934 | sa.in.sin_addr = *destination; |
1716f6dc | 935 | sa.in.sin_port = htobe16(port), |
091a364c | 936 | |
74b2466e LP |
937 | mh.msg_iov = &iov; |
938 | mh.msg_iovlen = 1; | |
939 | mh.msg_name = &sa.sa; | |
940 | mh.msg_namelen = sizeof(sa.in); | |
091a364c | 941 | |
74b2466e LP |
942 | if (ifindex > 0) { |
943 | struct cmsghdr *cmsg; | |
944 | struct in_pktinfo *pi; | |
945 | ||
946 | zero(control); | |
947 | ||
1716f6dc | 948 | mh.msg_control = &control; |
74b2466e LP |
949 | mh.msg_controllen = CMSG_LEN(sizeof(struct in_pktinfo)); |
950 | ||
951 | cmsg = CMSG_FIRSTHDR(&mh); | |
952 | cmsg->cmsg_len = mh.msg_controllen; | |
953 | cmsg->cmsg_level = IPPROTO_IP; | |
954 | cmsg->cmsg_type = IP_PKTINFO; | |
955 | ||
956 | pi = (struct in_pktinfo*) CMSG_DATA(cmsg); | |
957 | pi->ipi_ifindex = ifindex; | |
b30bf55d LP |
958 | |
959 | if (source) | |
960 | pi->ipi_spec_dst = *source; | |
74b2466e LP |
961 | } |
962 | ||
963 | return sendmsg_loop(fd, &mh, 0); | |
964 | } | |
965 | ||
b30bf55d LP |
966 | static int manager_ipv6_send( |
967 | Manager *m, | |
968 | int fd, | |
969 | int ifindex, | |
970 | const struct in6_addr *destination, | |
971 | uint16_t port, | |
972 | const struct in6_addr *source, | |
973 | DnsPacket *p) { | |
974 | ||
74b2466e LP |
975 | union sockaddr_union sa = { |
976 | .in6.sin6_family = AF_INET6, | |
74b2466e | 977 | }; |
1716f6dc LP |
978 | union { |
979 | struct cmsghdr header; /* For alignment */ | |
980 | uint8_t buffer[CMSG_SPACE(sizeof(struct in6_pktinfo))]; | |
981 | } control; | |
74b2466e LP |
982 | struct msghdr mh = {}; |
983 | struct iovec iov; | |
74b2466e LP |
984 | |
985 | assert(m); | |
1716f6dc | 986 | assert(fd >= 0); |
b30bf55d | 987 | assert(destination); |
1716f6dc | 988 | assert(port > 0); |
74b2466e LP |
989 | assert(p); |
990 | ||
74b2466e LP |
991 | iov.iov_base = DNS_PACKET_DATA(p); |
992 | iov.iov_len = p->size; | |
993 | ||
b30bf55d | 994 | sa.in6.sin6_addr = *destination; |
1716f6dc | 995 | sa.in6.sin6_port = htobe16(port), |
74b2466e LP |
996 | sa.in6.sin6_scope_id = ifindex; |
997 | ||
998 | mh.msg_iov = &iov; | |
999 | mh.msg_iovlen = 1; | |
1000 | mh.msg_name = &sa.sa; | |
1001 | mh.msg_namelen = sizeof(sa.in6); | |
1002 | ||
1003 | if (ifindex > 0) { | |
1004 | struct cmsghdr *cmsg; | |
1005 | struct in6_pktinfo *pi; | |
1006 | ||
1007 | zero(control); | |
1008 | ||
1716f6dc | 1009 | mh.msg_control = &control; |
74b2466e LP |
1010 | mh.msg_controllen = CMSG_LEN(sizeof(struct in6_pktinfo)); |
1011 | ||
1012 | cmsg = CMSG_FIRSTHDR(&mh); | |
1013 | cmsg->cmsg_len = mh.msg_controllen; | |
1014 | cmsg->cmsg_level = IPPROTO_IPV6; | |
1015 | cmsg->cmsg_type = IPV6_PKTINFO; | |
1016 | ||
1017 | pi = (struct in6_pktinfo*) CMSG_DATA(cmsg); | |
1018 | pi->ipi6_ifindex = ifindex; | |
b30bf55d LP |
1019 | |
1020 | if (source) | |
1021 | pi->ipi6_addr = *source; | |
74b2466e LP |
1022 | } |
1023 | ||
1024 | return sendmsg_loop(fd, &mh, 0); | |
1025 | } | |
1026 | ||
b30bf55d LP |
1027 | int manager_send( |
1028 | Manager *m, | |
1029 | int fd, | |
1030 | int ifindex, | |
1031 | int family, | |
1032 | const union in_addr_union *destination, | |
1033 | uint16_t port, | |
1034 | const union in_addr_union *source, | |
1035 | DnsPacket *p) { | |
1036 | ||
1716f6dc LP |
1037 | assert(m); |
1038 | assert(fd >= 0); | |
b30bf55d | 1039 | assert(destination); |
1716f6dc LP |
1040 | assert(port > 0); |
1041 | assert(p); | |
1042 | ||
deb3f3d3 | 1043 | log_debug("Sending %s packet with id %" PRIu16 " on interface %i/%s.", DNS_PACKET_QR(p) ? "response" : "query", DNS_PACKET_ID(p), ifindex, af_to_name(family)); |
a2a416f7 | 1044 | |
1716f6dc | 1045 | if (family == AF_INET) |
b30bf55d | 1046 | return manager_ipv4_send(m, fd, ifindex, &destination->in, port, &source->in, p); |
2817157b | 1047 | if (family == AF_INET6) |
b30bf55d | 1048 | return manager_ipv6_send(m, fd, ifindex, &destination->in6, port, &source->in6, p); |
1716f6dc LP |
1049 | |
1050 | return -EAFNOSUPPORT; | |
1051 | } | |
1052 | ||
e1c95994 LP |
1053 | uint32_t manager_find_mtu(Manager *m) { |
1054 | uint32_t mtu = 0; | |
1055 | Link *l; | |
1056 | Iterator i; | |
1057 | ||
1058 | /* If we don't know on which link a DNS packet would be | |
1059 | * delivered, let's find the largest MTU that works on all | |
1060 | * interfaces we know of */ | |
1061 | ||
1062 | HASHMAP_FOREACH(l, m->links, i) { | |
1063 | if (l->mtu <= 0) | |
1064 | continue; | |
1065 | ||
1066 | if (mtu <= 0 || l->mtu < mtu) | |
1067 | mtu = l->mtu; | |
1068 | } | |
1069 | ||
1070 | return mtu; | |
1071 | } | |
1716f6dc | 1072 | |
623a4c97 | 1073 | int manager_find_ifindex(Manager *m, int family, const union in_addr_union *in_addr) { |
ec2c5e43 LP |
1074 | LinkAddress *a; |
1075 | ||
1076 | assert(m); | |
1077 | ||
4e945a6f | 1078 | a = manager_find_link_address(m, family, in_addr); |
ec2c5e43 LP |
1079 | if (a) |
1080 | return a->link->ifindex; | |
1081 | ||
1082 | return 0; | |
1083 | } | |
1084 | ||
eb60f9cd LP |
1085 | void manager_refresh_rrs(Manager *m) { |
1086 | Iterator i; | |
1087 | Link *l; | |
6db6a464 | 1088 | DnssdService *s; |
eb60f9cd LP |
1089 | |
1090 | assert(m); | |
1091 | ||
78c6a153 LP |
1092 | m->llmnr_host_ipv4_key = dns_resource_key_unref(m->llmnr_host_ipv4_key); |
1093 | m->llmnr_host_ipv6_key = dns_resource_key_unref(m->llmnr_host_ipv6_key); | |
400cb36e DR |
1094 | m->mdns_host_ipv4_key = dns_resource_key_unref(m->mdns_host_ipv4_key); |
1095 | m->mdns_host_ipv6_key = dns_resource_key_unref(m->mdns_host_ipv6_key); | |
eb60f9cd | 1096 | |
6db6a464 DR |
1097 | if (m->mdns_support == RESOLVE_SUPPORT_YES) |
1098 | HASHMAP_FOREACH(s, m->dnssd_services, i) | |
1099 | if (dnssd_update_rrs(s) < 0) | |
1100 | log_warning("Failed to refresh DNS-SD service '%s'", s->name); | |
1101 | ||
eb60f9cd LP |
1102 | HASHMAP_FOREACH(l, m->links, i) { |
1103 | link_add_rrs(l, true); | |
1104 | link_add_rrs(l, false); | |
1105 | } | |
1106 | } | |
1107 | ||
e7c1b0e4 | 1108 | static int manager_next_random_name(const char *old, char **ret_new) { |
ec2c5e43 | 1109 | const char *p; |
556a2294 | 1110 | uint64_t u, a; |
e7c1b0e4 | 1111 | char *n; |
623a4c97 | 1112 | |
e7c1b0e4 | 1113 | p = strchr(old, 0); |
ec2c5e43 LP |
1114 | assert(p); |
1115 | ||
e7c1b0e4 DR |
1116 | while (p > old) { |
1117 | if (!strchr(DIGITS, p[-1])) | |
ec2c5e43 LP |
1118 | break; |
1119 | ||
1120 | p--; | |
1121 | } | |
1122 | ||
1123 | if (*p == 0 || safe_atou64(p, &u) < 0 || u <= 0) | |
1124 | u = 1; | |
1125 | ||
556a2294 LP |
1126 | /* Add a random number to the old value. This way we can avoid |
1127 | * that two hosts pick the same hostname, win on IPv4 and lose | |
1128 | * on IPv6 (or vice versa), and pick the same hostname | |
1129 | * replacement hostname, ad infinitum. We still want the | |
1130 | * numbers to go up monotonically, hence we just add a random | |
1131 | * value 1..10 */ | |
1132 | ||
1133 | random_bytes(&a, sizeof(a)); | |
1134 | u += 1 + a % 10; | |
ec2c5e43 | 1135 | |
e7c1b0e4 | 1136 | if (asprintf(&n, "%.*s%" PRIu64, (int) (p - old), old, u) < 0) |
ec2c5e43 LP |
1137 | return -ENOMEM; |
1138 | ||
e7c1b0e4 DR |
1139 | *ret_new = n; |
1140 | ||
1141 | return 0; | |
1142 | } | |
1143 | ||
1144 | int manager_next_hostname(Manager *m) { | |
1145 | _cleanup_free_ char *h = NULL, *k = NULL; | |
1146 | int r; | |
1147 | ||
1148 | assert(m); | |
1149 | ||
1150 | r = manager_next_random_name(m->llmnr_hostname, &h); | |
1151 | if (r < 0) | |
1152 | return r; | |
1153 | ||
78c6a153 | 1154 | r = dns_name_concat(h, "local", &k); |
e7c1b0e4 | 1155 | if (r < 0) |
78c6a153 | 1156 | return r; |
78c6a153 LP |
1157 | |
1158 | log_info("Hostname conflict, changing published hostname from '%s' to '%s'.", m->llmnr_hostname, h); | |
1159 | ||
e7c1b0e4 DR |
1160 | free_and_replace(m->llmnr_hostname, h); |
1161 | free_and_replace(m->mdns_hostname, k); | |
ec2c5e43 | 1162 | |
eb60f9cd | 1163 | manager_refresh_rrs(m); |
623a4c97 LP |
1164 | |
1165 | return 0; | |
1166 | } | |
ec2c5e43 | 1167 | |
4e945a6f | 1168 | LinkAddress* manager_find_link_address(Manager *m, int family, const union in_addr_union *in_addr) { |
ec2c5e43 LP |
1169 | Iterator i; |
1170 | Link *l; | |
1171 | ||
1172 | assert(m); | |
1173 | ||
1174 | HASHMAP_FOREACH(l, m->links, i) { | |
1175 | LinkAddress *a; | |
1176 | ||
1177 | a = link_find_address(l, family, in_addr); | |
1178 | if (a) | |
1179 | return a; | |
1180 | } | |
1181 | ||
1182 | return NULL; | |
1183 | } | |
1184 | ||
a4076574 | 1185 | bool manager_our_packet(Manager *m, DnsPacket *p) { |
ec2c5e43 LP |
1186 | assert(m); |
1187 | assert(p); | |
1188 | ||
4e945a6f | 1189 | return !!manager_find_link_address(m, p->family, &p->sender); |
ec2c5e43 | 1190 | } |
4e945a6f | 1191 | |
a4076574 LP |
1192 | DnsScope* manager_find_scope(Manager *m, DnsPacket *p) { |
1193 | Link *l; | |
1194 | ||
1195 | assert(m); | |
1196 | assert(p); | |
1197 | ||
1198 | l = hashmap_get(m->links, INT_TO_PTR(p->ifindex)); | |
1199 | if (!l) | |
1200 | return NULL; | |
1201 | ||
b4f1862d DM |
1202 | switch (p->protocol) { |
1203 | case DNS_PROTOCOL_LLMNR: | |
a4076574 LP |
1204 | if (p->family == AF_INET) |
1205 | return l->llmnr_ipv4_scope; | |
1206 | else if (p->family == AF_INET6) | |
1207 | return l->llmnr_ipv6_scope; | |
b4f1862d DM |
1208 | |
1209 | break; | |
1210 | ||
1211 | case DNS_PROTOCOL_MDNS: | |
1212 | if (p->family == AF_INET) | |
1213 | return l->mdns_ipv4_scope; | |
1214 | else if (p->family == AF_INET6) | |
1215 | return l->mdns_ipv6_scope; | |
1216 | ||
1217 | break; | |
1218 | ||
1219 | default: | |
1220 | break; | |
a4076574 LP |
1221 | } |
1222 | ||
1223 | return NULL; | |
1224 | } | |
1225 | ||
902bb5d8 LP |
1226 | void manager_verify_all(Manager *m) { |
1227 | DnsScope *s; | |
1228 | ||
1229 | assert(m); | |
1230 | ||
1231 | LIST_FOREACH(scopes, s, m->dns_scopes) | |
1232 | dns_zone_verify_all(&s->zone); | |
1233 | } | |
1234 | ||
78c6a153 | 1235 | int manager_is_own_hostname(Manager *m, const char *name) { |
78c6a153 LP |
1236 | int r; |
1237 | ||
1238 | assert(m); | |
1239 | assert(name); | |
1240 | ||
1241 | if (m->llmnr_hostname) { | |
1242 | r = dns_name_equal(name, m->llmnr_hostname); | |
1243 | if (r != 0) | |
1244 | return r; | |
1245 | } | |
1246 | ||
e96de0ce LP |
1247 | if (m->mdns_hostname) { |
1248 | r = dns_name_equal(name, m->mdns_hostname); | |
1249 | if (r != 0) | |
1250 | return r; | |
1251 | } | |
1252 | ||
1253 | if (m->full_hostname) | |
1254 | return dns_name_equal(name, m->full_hostname); | |
78c6a153 LP |
1255 | |
1256 | return 0; | |
1257 | } | |
1258 | ||
9176a57c LP |
1259 | int manager_compile_dns_servers(Manager *m, OrderedSet **dns) { |
1260 | DnsServer *s; | |
1261 | Iterator i; | |
1262 | Link *l; | |
1263 | int r; | |
1264 | ||
1265 | assert(m); | |
1266 | assert(dns); | |
1267 | ||
1268 | r = ordered_set_ensure_allocated(dns, &dns_server_hash_ops); | |
1269 | if (r < 0) | |
1270 | return r; | |
1271 | ||
1272 | /* First add the system-wide servers and domains */ | |
1273 | LIST_FOREACH(servers, s, m->dns_servers) { | |
1274 | r = ordered_set_put(*dns, s); | |
1275 | if (r == -EEXIST) | |
1276 | continue; | |
1277 | if (r < 0) | |
1278 | return r; | |
1279 | } | |
1280 | ||
1281 | /* Then, add the per-link servers */ | |
1282 | HASHMAP_FOREACH(l, m->links, i) { | |
1283 | LIST_FOREACH(servers, s, l->dns_servers) { | |
1284 | r = ordered_set_put(*dns, s); | |
1285 | if (r == -EEXIST) | |
1286 | continue; | |
1287 | if (r < 0) | |
1288 | return r; | |
1289 | } | |
1290 | } | |
1291 | ||
1292 | /* If we found nothing, add the fallback servers */ | |
1293 | if (ordered_set_isempty(*dns)) { | |
1294 | LIST_FOREACH(servers, s, m->fallback_dns_servers) { | |
1295 | r = ordered_set_put(*dns, s); | |
1296 | if (r == -EEXIST) | |
1297 | continue; | |
1298 | if (r < 0) | |
1299 | return r; | |
1300 | } | |
1301 | } | |
1302 | ||
1303 | return 0; | |
1304 | } | |
1305 | ||
94363cbb MP |
1306 | /* filter_route is a tri-state: |
1307 | * < 0: no filtering | |
1308 | * = 0 or false: return only domains which should be used for searching | |
1309 | * > 0 or true: return only domains which are for routing only | |
1310 | */ | |
6f7da49d | 1311 | int manager_compile_search_domains(Manager *m, OrderedSet **domains, int filter_route) { |
9176a57c LP |
1312 | DnsSearchDomain *d; |
1313 | Iterator i; | |
1314 | Link *l; | |
1315 | int r; | |
1316 | ||
1317 | assert(m); | |
1318 | assert(domains); | |
1319 | ||
1320 | r = ordered_set_ensure_allocated(domains, &dns_name_hash_ops); | |
1321 | if (r < 0) | |
1322 | return r; | |
1323 | ||
1324 | LIST_FOREACH(domains, d, m->search_domains) { | |
6f7da49d LP |
1325 | |
1326 | if (filter_route >= 0 && | |
1327 | d->route_only != !!filter_route) | |
1328 | continue; | |
1329 | ||
9176a57c LP |
1330 | r = ordered_set_put(*domains, d->name); |
1331 | if (r == -EEXIST) | |
1332 | continue; | |
1333 | if (r < 0) | |
1334 | return r; | |
1335 | } | |
1336 | ||
1337 | HASHMAP_FOREACH(l, m->links, i) { | |
1338 | ||
1339 | LIST_FOREACH(domains, d, l->search_domains) { | |
6f7da49d LP |
1340 | |
1341 | if (filter_route >= 0 && | |
1342 | d->route_only != !!filter_route) | |
1343 | continue; | |
1344 | ||
9176a57c LP |
1345 | r = ordered_set_put(*domains, d->name); |
1346 | if (r == -EEXIST) | |
1347 | continue; | |
1348 | if (r < 0) | |
1349 | return r; | |
1350 | } | |
1351 | } | |
1352 | ||
1353 | return 0; | |
1354 | } | |
c69fa7e3 LP |
1355 | |
1356 | DnssecMode manager_get_dnssec_mode(Manager *m) { | |
1357 | assert(m); | |
1358 | ||
1359 | if (m->dnssec_mode != _DNSSEC_MODE_INVALID) | |
1360 | return m->dnssec_mode; | |
1361 | ||
1362 | return DNSSEC_NO; | |
1363 | } | |
1364 | ||
1365 | bool manager_dnssec_supported(Manager *m) { | |
1366 | DnsServer *server; | |
1367 | Iterator i; | |
1368 | Link *l; | |
1369 | ||
1370 | assert(m); | |
1371 | ||
1372 | if (manager_get_dnssec_mode(m) == DNSSEC_NO) | |
1373 | return false; | |
1374 | ||
1375 | server = manager_get_dns_server(m); | |
1376 | if (server && !dns_server_dnssec_supported(server)) | |
1377 | return false; | |
1378 | ||
1379 | HASHMAP_FOREACH(l, m->links, i) | |
1380 | if (!link_dnssec_supported(l)) | |
1381 | return false; | |
1382 | ||
1383 | return true; | |
1384 | } | |
59c5b597 | 1385 | |
c9299be2 | 1386 | DnsOverTlsMode manager_get_dns_over_tls_mode(Manager *m) { |
5d67a7ae IT |
1387 | assert(m); |
1388 | ||
c9299be2 IT |
1389 | if (m->dns_over_tls_mode != _DNS_OVER_TLS_MODE_INVALID) |
1390 | return m->dns_over_tls_mode; | |
5d67a7ae | 1391 | |
c9299be2 | 1392 | return DNS_OVER_TLS_NO; |
5d67a7ae IT |
1393 | } |
1394 | ||
59c5b597 LP |
1395 | void manager_dnssec_verdict(Manager *m, DnssecVerdict verdict, const DnsResourceKey *key) { |
1396 | ||
1397 | assert(verdict >= 0); | |
1398 | assert(verdict < _DNSSEC_VERDICT_MAX); | |
1399 | ||
f1d34068 | 1400 | if (DEBUG_LOGGING) { |
202b76ae | 1401 | char s[DNS_RESOURCE_KEY_STRING_MAX]; |
59c5b597 | 1402 | |
202b76ae ZJS |
1403 | log_debug("Found verdict for lookup %s: %s", |
1404 | dns_resource_key_to_string(key, s, sizeof s), | |
1405 | dnssec_verdict_to_string(verdict)); | |
59c5b597 LP |
1406 | } |
1407 | ||
1408 | m->n_dnssec_verdict[verdict]++; | |
1409 | } | |
011696f7 LP |
1410 | |
1411 | bool manager_routable(Manager *m, int family) { | |
1412 | Iterator i; | |
1413 | Link *l; | |
1414 | ||
1415 | assert(m); | |
1416 | ||
1417 | /* Returns true if the host has at least one interface with a routable address of the specified type */ | |
1418 | ||
1419 | HASHMAP_FOREACH(l, m->links, i) | |
1420 | if (link_relevant(l, family, false)) | |
1421 | return true; | |
1422 | ||
1423 | return false; | |
1424 | } | |
ba35662f LP |
1425 | |
1426 | void manager_flush_caches(Manager *m) { | |
1427 | DnsScope *scope; | |
1428 | ||
1429 | assert(m); | |
1430 | ||
1431 | LIST_FOREACH(scopes, scope, m->dns_scopes) | |
1432 | dns_cache_flush(&scope->cache); | |
aac57b35 ZJS |
1433 | |
1434 | log_info("Flushed all caches."); | |
ba35662f | 1435 | } |
943ef07c | 1436 | |
59c0fd0e LP |
1437 | void manager_reset_server_features(Manager *m) { |
1438 | Iterator i; | |
1439 | Link *l; | |
1440 | ||
1441 | dns_server_reset_features_all(m->dns_servers); | |
1442 | dns_server_reset_features_all(m->fallback_dns_servers); | |
1443 | ||
1444 | HASHMAP_FOREACH(l, m->links, i) | |
1445 | dns_server_reset_features_all(l->dns_servers); | |
1446 | ||
1447 | log_info("Resetting learnt feature levels on all servers."); | |
1448 | } | |
1449 | ||
943ef07c LP |
1450 | void manager_cleanup_saved_user(Manager *m) { |
1451 | _cleanup_closedir_ DIR *d = NULL; | |
1452 | struct dirent *de; | |
1453 | int r; | |
1454 | ||
1455 | assert(m); | |
1456 | ||
1457 | /* Clean up all saved per-link files in /run/systemd/resolve/netif/ that don't have a matching interface | |
1458 | * anymore. These files are created to persist settings pushed in by the user via the bus, so that resolved can | |
1459 | * be restarted without losing this data. */ | |
1460 | ||
1461 | d = opendir("/run/systemd/resolve/netif/"); | |
1462 | if (!d) { | |
1463 | if (errno == ENOENT) | |
1464 | return; | |
1465 | ||
1466 | log_warning_errno(errno, "Failed to open interface directory: %m"); | |
1467 | return; | |
1468 | } | |
1469 | ||
1470 | FOREACH_DIRENT_ALL(de, d, log_error_errno(errno, "Failed to read interface directory: %m")) { | |
1471 | _cleanup_free_ char *p = NULL; | |
1472 | int ifindex; | |
1473 | Link *l; | |
1474 | ||
1475 | if (!IN_SET(de->d_type, DT_UNKNOWN, DT_REG)) | |
1476 | continue; | |
1477 | ||
49bfc877 | 1478 | if (dot_or_dot_dot(de->d_name)) |
943ef07c LP |
1479 | continue; |
1480 | ||
1481 | r = parse_ifindex(de->d_name, &ifindex); | |
1482 | if (r < 0) /* Probably some temporary file from a previous run. Delete it */ | |
1483 | goto rm; | |
1484 | ||
1485 | l = hashmap_get(m->links, INT_TO_PTR(ifindex)); | |
1486 | if (!l) /* link vanished */ | |
1487 | goto rm; | |
1488 | ||
1489 | if (l->is_managed) /* now managed by networkd, hence the bus settings are useless */ | |
1490 | goto rm; | |
1491 | ||
1492 | continue; | |
1493 | ||
1494 | rm: | |
1495 | p = strappend("/run/systemd/resolve/netif/", de->d_name); | |
1496 | if (!p) { | |
1497 | log_oom(); | |
1498 | return; | |
1499 | } | |
1500 | ||
1501 | (void) unlink(p); | |
1502 | } | |
1503 | } | |
e7c1b0e4 DR |
1504 | |
1505 | bool manager_next_dnssd_names(Manager *m) { | |
1506 | Iterator i; | |
1507 | DnssdService *s; | |
1508 | bool tried = false; | |
1509 | int r; | |
1510 | ||
1511 | assert(m); | |
1512 | ||
1513 | HASHMAP_FOREACH(s, m->dnssd_services, i) { | |
1514 | _cleanup_free_ char * new_name = NULL; | |
1515 | ||
1516 | if (!s->withdrawn) | |
1517 | continue; | |
1518 | ||
1519 | r = manager_next_random_name(s->name_template, &new_name); | |
1520 | if (r < 0) { | |
1521 | log_warning_errno(r, "Failed to get new name for service '%s': %m", s->name); | |
1522 | continue; | |
1523 | } | |
1524 | ||
1525 | free_and_replace(s->name_template, new_name); | |
1526 | ||
1527 | s->withdrawn = false; | |
1528 | ||
1529 | tried = true; | |
1530 | } | |
1531 | ||
1532 | if (tried) | |
1533 | manager_refresh_rrs(m); | |
1534 | ||
1535 | return tried; | |
1536 | } |