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