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