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