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