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