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