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