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