]>
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 | ||
522 | log_dump(LOG_INFO, buffer); | |
523 | return 0; | |
524 | } | |
525 | ||
bc81447e | 526 | static int manager_sigusr2(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { |
99534007 | 527 | Manager *m = ASSERT_PTR(userdata); |
bc81447e LP |
528 | |
529 | assert(s); | |
530 | assert(si); | |
bc81447e | 531 | |
90df0fbe | 532 | manager_flush_caches(m, LOG_INFO); |
ba35662f | 533 | |
bc81447e LP |
534 | return 0; |
535 | } | |
536 | ||
d55b0463 | 537 | static int manager_sigrtmin1(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { |
99534007 | 538 | Manager *m = ASSERT_PTR(userdata); |
d55b0463 LP |
539 | |
540 | assert(s); | |
541 | assert(si); | |
d55b0463 LP |
542 | |
543 | manager_reset_server_features(m); | |
544 | return 0; | |
545 | } | |
546 | ||
bb351718 LP |
547 | static int manager_memory_pressure(sd_event_source *s, void *userdata) { |
548 | Manager *m = ASSERT_PTR(userdata); | |
549 | ||
550 | log_info("Under memory pressure, flushing caches."); | |
551 | ||
552 | manager_flush_caches(m, LOG_INFO); | |
553 | sd_event_trim_memory(); | |
554 | ||
555 | return 0; | |
556 | } | |
557 | ||
558 | static int manager_memory_pressure_listen(Manager *m) { | |
559 | int r; | |
560 | ||
561 | assert(m); | |
562 | ||
563 | r = sd_event_add_memory_pressure(m->event, NULL, manager_memory_pressure, m); | |
564 | if (r < 0) | |
565 | log_full_errno(ERRNO_IS_NOT_SUPPORTED(r) || ERRNO_IS_PRIVILEGE(r) || (r == -EHOSTDOWN )? LOG_DEBUG : LOG_NOTICE, r, | |
566 | "Failed to install memory pressure event source, ignoring: %m"); | |
567 | ||
568 | return 0; | |
569 | } | |
570 | ||
091a364c | 571 | int manager_new(Manager **ret) { |
74b2466e | 572 | _cleanup_(manager_freep) Manager *m = NULL; |
091a364c TG |
573 | int r; |
574 | ||
c92e531c LP |
575 | assert(ret); |
576 | ||
f55f2dce | 577 | m = new(Manager, 1); |
091a364c TG |
578 | if (!m) |
579 | return -ENOMEM; | |
580 | ||
f55f2dce | 581 | *m = (Manager) { |
254d1313 ZJS |
582 | .llmnr_ipv4_udp_fd = -EBADF, |
583 | .llmnr_ipv6_udp_fd = -EBADF, | |
584 | .llmnr_ipv4_tcp_fd = -EBADF, | |
585 | .llmnr_ipv6_tcp_fd = -EBADF, | |
586 | .mdns_ipv4_fd = -EBADF, | |
587 | .mdns_ipv6_fd = -EBADF, | |
588 | .hostname_fd = -EBADF, | |
f55f2dce | 589 | |
3614df05 ZJS |
590 | .llmnr_support = DEFAULT_LLMNR_MODE, |
591 | .mdns_support = DEFAULT_MDNS_MODE, | |
f55f2dce YW |
592 | .dnssec_mode = DEFAULT_DNSSEC_MODE, |
593 | .dns_over_tls_mode = DEFAULT_DNS_OVER_TLS_MODE, | |
37d7a7d9 | 594 | .enable_cache = DNS_CACHE_MODE_YES, |
4b987478 | 595 | .dns_stub_listener_mode = DNS_STUB_LISTENER_YES, |
f55f2dce YW |
596 | .read_resolv_conf = true, |
597 | .need_builtin_fallbacks = true, | |
598 | .etc_hosts_last = USEC_INFINITY, | |
f55f2dce | 599 | .read_etc_hosts = true, |
bb351718 LP |
600 | |
601 | .sigrtmin18_info.memory_pressure_handler = manager_memory_pressure, | |
602 | .sigrtmin18_info.memory_pressure_userdata = m, | |
f55f2dce | 603 | }; |
091a364c | 604 | |
0d2cd476 LP |
605 | r = dns_trust_anchor_load(&m->trust_anchor); |
606 | if (r < 0) | |
607 | return r; | |
608 | ||
ad6c0475 LP |
609 | r = manager_parse_config_file(m); |
610 | if (r < 0) | |
5ce497b5 | 611 | log_warning_errno(r, "Failed to parse configuration file: %m"); |
ad6c0475 | 612 | |
e22c5b20 | 613 | #if ENABLE_DNS_OVER_TLS |
71a681ae IT |
614 | r = dnstls_manager_init(m); |
615 | if (r < 0) | |
616 | return r; | |
e22c5b20 IT |
617 | #endif |
618 | ||
091a364c TG |
619 | r = sd_event_default(&m->event); |
620 | if (r < 0) | |
621 | return r; | |
622 | ||
1ae17292 YW |
623 | (void) sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL); |
624 | (void) sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL); | |
091a364c | 625 | |
1ae17292 | 626 | (void) sd_event_set_watchdog(m->event, true); |
091a364c | 627 | |
eb60f9cd LP |
628 | r = manager_watch_hostname(m); |
629 | if (r < 0) | |
630 | return r; | |
631 | ||
6501dd31 DR |
632 | r = dnssd_load(m); |
633 | if (r < 0) | |
634 | log_warning_errno(r, "Failed to load DNS-SD configuration files: %m"); | |
635 | ||
1716f6dc | 636 | r = dns_scope_new(m, &m->unicast_scope, NULL, DNS_PROTOCOL_DNS, AF_UNSPEC); |
74b2466e LP |
637 | if (r < 0) |
638 | return r; | |
639 | ||
640 | r = manager_network_monitor_listen(m); | |
641 | if (r < 0) | |
642 | return r; | |
643 | ||
644 | r = manager_rtnl_listen(m); | |
645 | if (r < 0) | |
646 | return r; | |
647 | ||
90df0fbe LP |
648 | r = manager_clock_change_listen(m); |
649 | if (r < 0) | |
650 | return r; | |
651 | ||
bb351718 LP |
652 | r = manager_memory_pressure_listen(m); |
653 | if (r < 0) | |
654 | return r; | |
655 | ||
74b2466e LP |
656 | r = manager_connect_bus(m); |
657 | if (r < 0) | |
658 | return r; | |
659 | ||
4d506d6b | 660 | (void) sd_event_add_signal(m->event, &m->sigusr1_event_source, SIGUSR1, manager_sigusr1, m); |
bc81447e | 661 | (void) sd_event_add_signal(m->event, &m->sigusr2_event_source, SIGUSR2, manager_sigusr2, m); |
d55b0463 | 662 | (void) sd_event_add_signal(m->event, &m->sigrtmin1_event_source, SIGRTMIN+1, manager_sigrtmin1, m); |
bb351718 | 663 | (void) sd_event_add_signal(m->event, NULL, SIGRTMIN+18, sigrtmin18_handler, &m->sigrtmin18_info); |
4d506d6b | 664 | |
943ef07c LP |
665 | manager_cleanup_saved_user(m); |
666 | ||
1cc6c93a | 667 | *ret = TAKE_PTR(m); |
091a364c TG |
668 | |
669 | return 0; | |
670 | } | |
671 | ||
edc501d4 LP |
672 | int manager_start(Manager *m) { |
673 | int r; | |
674 | ||
675 | assert(m); | |
676 | ||
b30bf55d LP |
677 | r = manager_dns_stub_start(m); |
678 | if (r < 0) | |
679 | return r; | |
680 | ||
9581bb84 LP |
681 | r = manager_varlink_init(m); |
682 | if (r < 0) | |
683 | return r; | |
684 | ||
edc501d4 LP |
685 | return 0; |
686 | } | |
687 | ||
74b2466e LP |
688 | Manager *manager_free(Manager *m) { |
689 | Link *l; | |
6501dd31 | 690 | DnssdService *s; |
091a364c TG |
691 | |
692 | if (!m) | |
74b2466e LP |
693 | return NULL; |
694 | ||
4b95f179 LP |
695 | dns_server_unlink_all(m->dns_servers); |
696 | dns_server_unlink_all(m->fallback_dns_servers); | |
a51c1048 LP |
697 | dns_search_domain_unlink_all(m->search_domains); |
698 | ||
74b2466e LP |
699 | while ((l = hashmap_first(m->links))) |
700 | link_free(l); | |
f0e15467 LP |
701 | |
702 | while (m->dns_queries) | |
703 | dns_query_free(m->dns_queries); | |
74b2466e | 704 | |
bde69bbd LP |
705 | m->stub_queries_by_packet = hashmap_free(m->stub_queries_by_packet); |
706 | ||
cab5b059 LP |
707 | dns_scope_free(m->unicast_scope); |
708 | ||
b30bf55d LP |
709 | /* At this point only orphaned streams should remain. All others should have been freed already by their |
710 | * owners */ | |
711 | while (m->dns_streams) | |
712 | dns_stream_unref(m->dns_streams); | |
713 | ||
e22c5b20 IT |
714 | #if ENABLE_DNS_OVER_TLS |
715 | dnstls_manager_free(m); | |
716 | #endif | |
717 | ||
f0e15467 LP |
718 | hashmap_free(m->links); |
719 | hashmap_free(m->dns_transactions); | |
720 | ||
096b6773 LP |
721 | sd_event_source_unref(m->network_event_source); |
722 | sd_network_monitor_unref(m->network_monitor); | |
091a364c | 723 | |
a564ca2f LP |
724 | sd_netlink_unref(m->rtnl); |
725 | sd_event_source_unref(m->rtnl_event_source); | |
90df0fbe | 726 | sd_event_source_unref(m->clock_change_event_source); |
a564ca2f | 727 | |
edc501d4 | 728 | manager_llmnr_stop(m); |
bc7702b0 | 729 | manager_mdns_stop(m); |
b30bf55d | 730 | manager_dns_stub_stop(m); |
9581bb84 | 731 | manager_varlink_done(m); |
623a4c97 | 732 | |
80710ade LP |
733 | manager_socket_graveyard_clear(m); |
734 | ||
46505826 | 735 | ordered_set_free(m->dns_extra_stub_listeners); |
1f05101f | 736 | |
2400ae29 YW |
737 | bus_verify_polkit_async_registry_free(m->polkit_registry); |
738 | ||
92e31da1 | 739 | sd_bus_flush_close_unref(m->bus); |
091a364c | 740 | |
4d506d6b | 741 | sd_event_source_unref(m->sigusr1_event_source); |
bc81447e | 742 | sd_event_source_unref(m->sigusr2_event_source); |
d55b0463 | 743 | sd_event_source_unref(m->sigrtmin1_event_source); |
4d506d6b | 744 | |
78c6a153 LP |
745 | dns_resource_key_unref(m->llmnr_host_ipv4_key); |
746 | dns_resource_key_unref(m->llmnr_host_ipv6_key); | |
400cb36e DR |
747 | dns_resource_key_unref(m->mdns_host_ipv4_key); |
748 | dns_resource_key_unref(m->mdns_host_ipv6_key); | |
eb60f9cd | 749 | |
eb60f9cd | 750 | sd_event_source_unref(m->hostname_event_source); |
d9fcf2ba | 751 | safe_close(m->hostname_fd); |
e96de0ce | 752 | |
f2ec080e ZJS |
753 | sd_event_unref(m->event); |
754 | ||
e96de0ce | 755 | free(m->full_hostname); |
78c6a153 LP |
756 | free(m->llmnr_hostname); |
757 | free(m->mdns_hostname); | |
eb60f9cd | 758 | |
6501dd31 DR |
759 | while ((s = hashmap_first(m->dnssd_services))) |
760 | dnssd_service_free(s); | |
761 | hashmap_free(m->dnssd_services); | |
762 | ||
0d2cd476 | 763 | dns_trust_anchor_flush(&m->trust_anchor); |
dd0bc0f1 | 764 | manager_etc_hosts_flush(m); |
0d2cd476 | 765 | |
6b430fdb | 766 | return mfree(m); |
091a364c TG |
767 | } |
768 | ||
1716f6dc | 769 | int manager_recv(Manager *m, int fd, DnsProtocol protocol, DnsPacket **ret) { |
74b2466e | 770 | _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL; |
fb29cdbe LP |
771 | CMSG_BUFFER_TYPE(CMSG_SPACE(MAXSIZE(struct in_pktinfo, struct in6_pktinfo)) |
772 | + CMSG_SPACE(int) /* ttl/hoplimit */ | |
773 | + EXTRA_CMSG_SPACE /* kernel appears to require extra buffer space */) control; | |
1716f6dc | 774 | union sockaddr_union sa; |
74b2466e | 775 | struct iovec iov; |
f55f2dce YW |
776 | struct msghdr mh = { |
777 | .msg_name = &sa.sa, | |
778 | .msg_namelen = sizeof(sa), | |
779 | .msg_iov = &iov, | |
780 | .msg_iovlen = 1, | |
781 | .msg_control = &control, | |
782 | .msg_controllen = sizeof(control), | |
783 | }; | |
784 | struct cmsghdr *cmsg; | |
4edc2c9b LP |
785 | ssize_t ms, l; |
786 | int r; | |
74b2466e LP |
787 | |
788 | assert(m); | |
1716f6dc | 789 | assert(fd >= 0); |
74b2466e LP |
790 | assert(ret); |
791 | ||
4edc2c9b | 792 | ms = next_datagram_size_fd(fd); |
74b2466e | 793 | if (ms < 0) |
4edc2c9b | 794 | return ms; |
74b2466e | 795 | |
51027656 | 796 | r = dns_packet_new(&p, protocol, ms, DNS_PACKET_SIZE_MAX); |
74b2466e LP |
797 | if (r < 0) |
798 | return r; | |
799 | ||
cb310866 | 800 | iov = IOVEC_MAKE(DNS_PACKET_DATA(p), p->allocated); |
74b2466e | 801 | |
3691bcf3 | 802 | l = recvmsg_safe(fd, &mh, 0); |
8add30a0 YW |
803 | if (l < 0) { |
804 | if (ERRNO_IS_TRANSIENT(l)) | |
805 | return 0; | |
3691bcf3 | 806 | return l; |
8add30a0 YW |
807 | } |
808 | if (l == 0) | |
809 | return 0; | |
091a364c | 810 | |
1716f6dc LP |
811 | assert(!(mh.msg_flags & MSG_TRUNC)); |
812 | ||
74b2466e | 813 | p->size = (size_t) l; |
091a364c | 814 | |
1716f6dc | 815 | p->family = sa.sa.sa_family; |
623a4c97 LP |
816 | p->ipproto = IPPROTO_UDP; |
817 | if (p->family == AF_INET) { | |
1716f6dc | 818 | p->sender.in = sa.in.sin_addr; |
623a4c97 LP |
819 | p->sender_port = be16toh(sa.in.sin_port); |
820 | } else if (p->family == AF_INET6) { | |
1716f6dc | 821 | p->sender.in6 = sa.in6.sin6_addr; |
623a4c97 LP |
822 | p->sender_port = be16toh(sa.in6.sin6_port); |
823 | p->ifindex = sa.in6.sin6_scope_id; | |
824 | } else | |
1716f6dc | 825 | return -EAFNOSUPPORT; |
74b2466e | 826 | |
ba4e0427 | 827 | p->timestamp = now(CLOCK_BOOTTIME); |
5777c613 | 828 | |
2a1288ff | 829 | CMSG_FOREACH(cmsg, &mh) { |
74b2466e | 830 | |
1716f6dc LP |
831 | if (cmsg->cmsg_level == IPPROTO_IPV6) { |
832 | assert(p->family == AF_INET6); | |
74b2466e | 833 | |
1716f6dc | 834 | switch (cmsg->cmsg_type) { |
74b2466e | 835 | |
1716f6dc | 836 | case IPV6_PKTINFO: { |
b1d02191 | 837 | struct in6_pktinfo *i = CMSG_TYPED_DATA(cmsg, struct in6_pktinfo); |
74b2466e | 838 | |
623a4c97 LP |
839 | if (p->ifindex <= 0) |
840 | p->ifindex = i->ipi6_ifindex; | |
841 | ||
1716f6dc LP |
842 | p->destination.in6 = i->ipi6_addr; |
843 | break; | |
844 | } | |
74b2466e | 845 | |
1716f6dc | 846 | case IPV6_HOPLIMIT: |
b1d02191 | 847 | p->ttl = *CMSG_TYPED_DATA(cmsg, int); |
1716f6dc | 848 | break; |
74b2466e | 849 | |
20a001bd | 850 | case IPV6_RECVFRAGSIZE: |
b1d02191 | 851 | p->fragsize = *CMSG_TYPED_DATA(cmsg, int); |
20a001bd | 852 | break; |
1716f6dc LP |
853 | } |
854 | } else if (cmsg->cmsg_level == IPPROTO_IP) { | |
855 | assert(p->family == AF_INET); | |
74b2466e | 856 | |
1716f6dc | 857 | switch (cmsg->cmsg_type) { |
74b2466e | 858 | |
1716f6dc | 859 | case IP_PKTINFO: { |
b1d02191 | 860 | struct in_pktinfo *i = CMSG_TYPED_DATA(cmsg, struct in_pktinfo); |
091a364c | 861 | |
623a4c97 LP |
862 | if (p->ifindex <= 0) |
863 | p->ifindex = i->ipi_ifindex; | |
864 | ||
1716f6dc LP |
865 | p->destination.in = i->ipi_addr; |
866 | break; | |
867 | } | |
74b2466e | 868 | |
623a4c97 | 869 | case IP_TTL: |
b1d02191 | 870 | p->ttl = *CMSG_TYPED_DATA(cmsg, int); |
1716f6dc | 871 | break; |
20a001bd LP |
872 | |
873 | case IP_RECVFRAGSIZE: | |
b1d02191 | 874 | p->fragsize = *CMSG_TYPED_DATA(cmsg, int); |
20a001bd | 875 | break; |
1716f6dc LP |
876 | } |
877 | } | |
878 | } | |
74b2466e | 879 | |
623a4c97 LP |
880 | /* The Linux kernel sets the interface index to the loopback |
881 | * device if the packet came from the local host since it | |
882 | * avoids the routing table in such a case. Let's unset the | |
883 | * interface index in such a case. */ | |
a5f03596 | 884 | if (p->ifindex == LOOPBACK_IFINDEX) |
623a4c97 LP |
885 | p->ifindex = 0; |
886 | ||
86ad4cd7 TG |
887 | if (protocol != DNS_PROTOCOL_DNS) { |
888 | /* If we don't know the interface index still, we look for the | |
889 | * first local interface with a matching address. Yuck! */ | |
890 | if (p->ifindex <= 0) | |
891 | p->ifindex = manager_find_ifindex(m, p->family, &p->destination); | |
892 | } | |
623a4c97 | 893 | |
c0f86d66 | 894 | log_debug("Received %s UDP packet of size %zu, ifindex=%i, ttl=%u, fragsize=%zu, sender=%s, destination=%s", |
84dbb3fd ZJS |
895 | dns_protocol_to_string(protocol), p->size, p->ifindex, p->ttl, p->fragsize, |
896 | IN_ADDR_TO_STRING(p->family, &p->sender), | |
897 | IN_ADDR_TO_STRING(p->family, &p->destination)); | |
74b2466e | 898 | |
f7155840 | 899 | *ret = TAKE_PTR(p); |
74b2466e LP |
900 | return 1; |
901 | } | |
902 | ||
74b2466e | 903 | static int sendmsg_loop(int fd, struct msghdr *mh, int flags) { |
6d66a221 | 904 | usec_t end; |
74b2466e LP |
905 | int r; |
906 | ||
907 | assert(fd >= 0); | |
908 | assert(mh); | |
909 | ||
6d66a221 LP |
910 | end = usec_add(now(CLOCK_MONOTONIC), SEND_TIMEOUT_USEC); |
911 | ||
74b2466e LP |
912 | for (;;) { |
913 | if (sendmsg(fd, mh, flags) >= 0) | |
914 | return 0; | |
74b2466e LP |
915 | if (errno == EINTR) |
916 | continue; | |
74b2466e LP |
917 | if (errno != EAGAIN) |
918 | return -errno; | |
919 | ||
6d66a221 LP |
920 | r = fd_wait_for_event(fd, POLLOUT, LESS_BY(end, now(CLOCK_MONOTONIC))); |
921 | if (r < 0) { | |
922 | if (ERRNO_IS_TRANSIENT(r)) | |
923 | continue; | |
74b2466e | 924 | return r; |
6d66a221 | 925 | } |
74b2466e LP |
926 | if (r == 0) |
927 | return -ETIMEDOUT; | |
928 | } | |
929 | } | |
930 | ||
72290734 | 931 | static int write_loop(int fd, void *message, size_t length) { |
6d66a221 | 932 | usec_t end; |
72290734 TG |
933 | int r; |
934 | ||
935 | assert(fd >= 0); | |
936 | assert(message); | |
937 | ||
6d66a221 LP |
938 | end = usec_add(now(CLOCK_MONOTONIC), SEND_TIMEOUT_USEC); |
939 | ||
72290734 TG |
940 | for (;;) { |
941 | if (write(fd, message, length) >= 0) | |
942 | return 0; | |
72290734 TG |
943 | if (errno == EINTR) |
944 | continue; | |
72290734 TG |
945 | if (errno != EAGAIN) |
946 | return -errno; | |
947 | ||
6d66a221 LP |
948 | r = fd_wait_for_event(fd, POLLOUT, LESS_BY(end, now(CLOCK_MONOTONIC))); |
949 | if (r < 0) { | |
950 | if (ERRNO_IS_TRANSIENT(r)) | |
951 | continue; | |
72290734 | 952 | return r; |
6d66a221 | 953 | } |
72290734 TG |
954 | if (r == 0) |
955 | return -ETIMEDOUT; | |
956 | } | |
957 | } | |
958 | ||
959 | int manager_write(Manager *m, int fd, DnsPacket *p) { | |
960 | int r; | |
961 | ||
76f77229 LP |
962 | log_debug("Sending %s%s packet with id %" PRIu16 " of size %zu.", |
963 | DNS_PACKET_TC(p) ? "truncated (!) " : "", | |
964 | DNS_PACKET_QR(p) ? "response" : "query", | |
965 | DNS_PACKET_ID(p), | |
966 | p->size); | |
72290734 TG |
967 | |
968 | r = write_loop(fd, DNS_PACKET_DATA(p), p->size); | |
969 | if (r < 0) | |
970 | return r; | |
971 | ||
972 | return 0; | |
973 | } | |
974 | ||
b30bf55d LP |
975 | static int manager_ipv4_send( |
976 | Manager *m, | |
977 | int fd, | |
978 | int ifindex, | |
979 | const struct in_addr *destination, | |
980 | uint16_t port, | |
981 | const struct in_addr *source, | |
982 | DnsPacket *p) { | |
fb29cdbe LP |
983 | |
984 | CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct in_pktinfo))) control = {}; | |
f55f2dce | 985 | union sockaddr_union sa; |
74b2466e | 986 | struct iovec iov; |
f55f2dce YW |
987 | struct msghdr mh = { |
988 | .msg_iov = &iov, | |
989 | .msg_iovlen = 1, | |
990 | .msg_name = &sa.sa, | |
991 | .msg_namelen = sizeof(sa.in), | |
992 | }; | |
74b2466e LP |
993 | |
994 | assert(m); | |
1716f6dc | 995 | assert(fd >= 0); |
b30bf55d | 996 | assert(destination); |
1716f6dc | 997 | assert(port > 0); |
74b2466e LP |
998 | assert(p); |
999 | ||
cb310866 | 1000 | iov = IOVEC_MAKE(DNS_PACKET_DATA(p), p->size); |
091a364c | 1001 | |
f55f2dce YW |
1002 | sa = (union sockaddr_union) { |
1003 | .in.sin_family = AF_INET, | |
1004 | .in.sin_addr = *destination, | |
1005 | .in.sin_port = htobe16(port), | |
1006 | }; | |
091a364c | 1007 | |
74b2466e LP |
1008 | if (ifindex > 0) { |
1009 | struct cmsghdr *cmsg; | |
1010 | struct in_pktinfo *pi; | |
1011 | ||
1716f6dc | 1012 | mh.msg_control = &control; |
a258f491 | 1013 | mh.msg_controllen = sizeof(control); |
74b2466e LP |
1014 | |
1015 | cmsg = CMSG_FIRSTHDR(&mh); | |
a258f491 | 1016 | cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo)); |
74b2466e LP |
1017 | cmsg->cmsg_level = IPPROTO_IP; |
1018 | cmsg->cmsg_type = IP_PKTINFO; | |
1019 | ||
1020 | pi = (struct in_pktinfo*) CMSG_DATA(cmsg); | |
1021 | pi->ipi_ifindex = ifindex; | |
b30bf55d LP |
1022 | |
1023 | if (source) | |
1024 | pi->ipi_spec_dst = *source; | |
74b2466e LP |
1025 | } |
1026 | ||
1027 | return sendmsg_loop(fd, &mh, 0); | |
1028 | } | |
1029 | ||
b30bf55d LP |
1030 | static int manager_ipv6_send( |
1031 | Manager *m, | |
1032 | int fd, | |
1033 | int ifindex, | |
1034 | const struct in6_addr *destination, | |
1035 | uint16_t port, | |
1036 | const struct in6_addr *source, | |
1037 | DnsPacket *p) { | |
1038 | ||
fb29cdbe | 1039 | CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct in6_pktinfo))) control = {}; |
f55f2dce | 1040 | union sockaddr_union sa; |
74b2466e | 1041 | struct iovec iov; |
f55f2dce YW |
1042 | struct msghdr mh = { |
1043 | .msg_iov = &iov, | |
1044 | .msg_iovlen = 1, | |
1045 | .msg_name = &sa.sa, | |
1046 | .msg_namelen = sizeof(sa.in6), | |
1047 | }; | |
74b2466e LP |
1048 | |
1049 | assert(m); | |
1716f6dc | 1050 | assert(fd >= 0); |
b30bf55d | 1051 | assert(destination); |
1716f6dc | 1052 | assert(port > 0); |
74b2466e LP |
1053 | assert(p); |
1054 | ||
cb310866 | 1055 | iov = IOVEC_MAKE(DNS_PACKET_DATA(p), p->size); |
74b2466e | 1056 | |
f55f2dce YW |
1057 | sa = (union sockaddr_union) { |
1058 | .in6.sin6_family = AF_INET6, | |
1059 | .in6.sin6_addr = *destination, | |
1060 | .in6.sin6_port = htobe16(port), | |
1061 | .in6.sin6_scope_id = ifindex, | |
1062 | }; | |
74b2466e LP |
1063 | |
1064 | if (ifindex > 0) { | |
1065 | struct cmsghdr *cmsg; | |
1066 | struct in6_pktinfo *pi; | |
1067 | ||
1716f6dc | 1068 | mh.msg_control = &control; |
a258f491 | 1069 | mh.msg_controllen = sizeof(control); |
74b2466e LP |
1070 | |
1071 | cmsg = CMSG_FIRSTHDR(&mh); | |
a258f491 | 1072 | cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); |
74b2466e LP |
1073 | cmsg->cmsg_level = IPPROTO_IPV6; |
1074 | cmsg->cmsg_type = IPV6_PKTINFO; | |
1075 | ||
1076 | pi = (struct in6_pktinfo*) CMSG_DATA(cmsg); | |
1077 | pi->ipi6_ifindex = ifindex; | |
b30bf55d LP |
1078 | |
1079 | if (source) | |
1080 | pi->ipi6_addr = *source; | |
74b2466e LP |
1081 | } |
1082 | ||
1083 | return sendmsg_loop(fd, &mh, 0); | |
1084 | } | |
1085 | ||
72c2d39e LP |
1086 | static int dns_question_to_json(DnsQuestion *q, JsonVariant **ret) { |
1087 | _cleanup_(json_variant_unrefp) JsonVariant *l = NULL; | |
1088 | DnsResourceKey *key; | |
1089 | int r; | |
1090 | ||
1091 | assert(ret); | |
1092 | ||
1093 | DNS_QUESTION_FOREACH(key, q) { | |
1094 | _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; | |
1095 | ||
1096 | r = dns_resource_key_to_json(key, &v); | |
1097 | if (r < 0) | |
1098 | return r; | |
1099 | ||
1100 | r = json_variant_append_array(&l, v); | |
1101 | if (r < 0) | |
1102 | return r; | |
1103 | } | |
1104 | ||
1105 | *ret = TAKE_PTR(l); | |
1106 | return 0; | |
1107 | } | |
1108 | ||
1109 | int manager_monitor_send( | |
1110 | Manager *m, | |
1111 | int state, | |
1112 | int rcode, | |
1113 | int error, | |
1114 | DnsQuestion *question_idna, | |
1115 | DnsQuestion *question_utf8, | |
1116 | DnsQuestion *collected_questions, | |
1117 | DnsAnswer *answer) { | |
1118 | ||
1119 | _cleanup_(json_variant_unrefp) JsonVariant *jquestion = NULL, *jcollected_questions = NULL, *janswer = NULL; | |
1120 | _cleanup_(dns_question_unrefp) DnsQuestion *merged = NULL; | |
cb456374 | 1121 | Varlink *connection; |
72c2d39e LP |
1122 | DnsAnswerItem *rri; |
1123 | int r; | |
cb456374 SK |
1124 | |
1125 | assert(m); | |
1126 | ||
1127 | if (set_isempty(m->varlink_subscription)) | |
1128 | return 0; | |
1129 | ||
72c2d39e LP |
1130 | /* Merge both questions format into one */ |
1131 | r = dns_question_merge(question_idna, question_utf8, &merged); | |
1132 | if (r < 0) | |
1133 | return log_error_errno(r, "Failed to merge UTF8/IDNA questions: %m"); | |
1134 | ||
1135 | /* Convert the current primary question to JSON */ | |
1136 | r = dns_question_to_json(merged, &jquestion); | |
1137 | if (r < 0) | |
1138 | return log_error_errno(r, "Failed to convert question to JSON: %m"); | |
cb456374 | 1139 | |
64ebc0da | 1140 | /* Generate a JSON array of the questions preceding the current one in the CNAME chain */ |
72c2d39e LP |
1141 | r = dns_question_to_json(collected_questions, &jcollected_questions); |
1142 | if (r < 0) | |
1143 | return log_error_errno(r, "Failed to convert question to JSON: %m"); | |
1144 | ||
1145 | DNS_ANSWER_FOREACH_ITEM(rri, answer) { | |
1146 | _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *w = NULL; | |
1147 | ||
1148 | r = dns_resource_record_to_json(rri->rr, &v); | |
cb456374 | 1149 | if (r < 0) |
72c2d39e | 1150 | return log_error_errno(r, "Failed to convert answer resource record to JSON: %m"); |
cb456374 | 1151 | |
72c2d39e LP |
1152 | 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 */ |
1153 | if (r < 0) | |
1154 | return log_error_errno(r, "Failed to generate RR wire format: %m"); | |
cb456374 | 1155 | |
72c2d39e LP |
1156 | r = json_build(&w, JSON_BUILD_OBJECT( |
1157 | JSON_BUILD_PAIR_CONDITION(v, "rr", JSON_BUILD_VARIANT(v)), | |
1158 | JSON_BUILD_PAIR("raw", JSON_BUILD_BASE64(rri->rr->wire_format, rri->rr->wire_format_size)), | |
1159 | JSON_BUILD_PAIR_CONDITION(rri->ifindex > 0, "ifindex", JSON_BUILD_INTEGER(rri->ifindex)))); | |
1160 | if (r < 0) | |
1161 | return log_error_errno(r, "Failed to make answer RR object: %m"); | |
1162 | ||
1163 | r = json_variant_append_array(&janswer, w); | |
1164 | if (r < 0) | |
1165 | return log_debug_errno(r, "Failed to append notification entry to array: %m"); | |
1166 | } | |
cb456374 SK |
1167 | |
1168 | SET_FOREACH(connection, m->varlink_subscription) { | |
1169 | r = varlink_notifyb(connection, | |
72c2d39e LP |
1170 | JSON_BUILD_OBJECT(JSON_BUILD_PAIR("state", JSON_BUILD_STRING(dns_transaction_state_to_string(state))), |
1171 | JSON_BUILD_PAIR_CONDITION(state == DNS_TRANSACTION_RCODE_FAILURE, "rcode", JSON_BUILD_INTEGER(rcode)), | |
1172 | JSON_BUILD_PAIR_CONDITION(state == DNS_TRANSACTION_ERRNO, "errno", JSON_BUILD_INTEGER(error)), | |
1173 | JSON_BUILD_PAIR("question", JSON_BUILD_VARIANT(jquestion)), | |
1174 | JSON_BUILD_PAIR_CONDITION(jcollected_questions, "collectedQuestions", JSON_BUILD_VARIANT(jcollected_questions)), | |
1175 | JSON_BUILD_PAIR_CONDITION(janswer, "answer", JSON_BUILD_VARIANT(janswer)))); | |
cb456374 | 1176 | if (r < 0) |
72c2d39e | 1177 | log_debug_errno(r, "Failed to send monitor event, ignoring: %m"); |
cb456374 | 1178 | } |
72c2d39e | 1179 | |
cb456374 SK |
1180 | return 0; |
1181 | } | |
1182 | ||
b30bf55d LP |
1183 | int manager_send( |
1184 | Manager *m, | |
1185 | int fd, | |
1186 | int ifindex, | |
1187 | int family, | |
1188 | const union in_addr_union *destination, | |
1189 | uint16_t port, | |
1190 | const union in_addr_union *source, | |
1191 | DnsPacket *p) { | |
1192 | ||
1716f6dc LP |
1193 | assert(m); |
1194 | assert(fd >= 0); | |
b30bf55d | 1195 | assert(destination); |
1716f6dc LP |
1196 | assert(port > 0); |
1197 | assert(p); | |
1198 | ||
76f77229 LP |
1199 | log_debug("Sending %s%s packet with id %" PRIu16 " on interface %i/%s of size %zu.", |
1200 | DNS_PACKET_TC(p) ? "truncated (!) " : "", | |
1201 | DNS_PACKET_QR(p) ? "response" : "query", | |
1202 | DNS_PACKET_ID(p), | |
1203 | ifindex, af_to_name(family), | |
1204 | p->size); | |
a2a416f7 | 1205 | |
1716f6dc | 1206 | if (family == AF_INET) |
25270cf3 | 1207 | return manager_ipv4_send(m, fd, ifindex, &destination->in, port, source ? &source->in : NULL, p); |
2817157b | 1208 | if (family == AF_INET6) |
25270cf3 | 1209 | return manager_ipv6_send(m, fd, ifindex, &destination->in6, port, source ? &source->in6 : NULL, p); |
1716f6dc LP |
1210 | |
1211 | return -EAFNOSUPPORT; | |
1212 | } | |
1213 | ||
e1c95994 LP |
1214 | uint32_t manager_find_mtu(Manager *m) { |
1215 | uint32_t mtu = 0; | |
1216 | Link *l; | |
e1c95994 | 1217 | |
5a0d0b8f | 1218 | /* If we don't know on which link a DNS packet would be delivered, let's find the largest MTU that |
4301cb32 | 1219 | * works on all interfaces we know of that have an IP address associated */ |
e1c95994 | 1220 | |
90e74a66 | 1221 | HASHMAP_FOREACH(l, m->links) { |
5a0d0b8f LP |
1222 | /* Let's filter out links without IP addresses (e.g. AF_CAN links and suchlike) */ |
1223 | if (!l->addresses) | |
1224 | continue; | |
1225 | ||
1226 | /* Safety check: MTU shorter than what we need for the absolutely shortest DNS request? Then | |
1227 | * let's ignore this link. */ | |
1228 | if (l->mtu < MIN(UDP4_PACKET_HEADER_SIZE + DNS_PACKET_HEADER_SIZE, | |
1229 | UDP6_PACKET_HEADER_SIZE + DNS_PACKET_HEADER_SIZE)) | |
e1c95994 LP |
1230 | continue; |
1231 | ||
1232 | if (mtu <= 0 || l->mtu < mtu) | |
1233 | mtu = l->mtu; | |
1234 | } | |
1235 | ||
5a0d0b8f LP |
1236 | if (mtu == 0) /* found nothing? then let's assume the typical Ethernet MTU for lack of anything more precise */ |
1237 | return 1500; | |
1238 | ||
e1c95994 LP |
1239 | return mtu; |
1240 | } | |
1716f6dc | 1241 | |
623a4c97 | 1242 | int manager_find_ifindex(Manager *m, int family, const union in_addr_union *in_addr) { |
ec2c5e43 LP |
1243 | LinkAddress *a; |
1244 | ||
1245 | assert(m); | |
1246 | ||
bb3b08ad YW |
1247 | if (!IN_SET(family, AF_INET, AF_INET6)) |
1248 | return 0; | |
1249 | ||
1250 | if (!in_addr) | |
1251 | return 0; | |
1252 | ||
4e945a6f | 1253 | a = manager_find_link_address(m, family, in_addr); |
ec2c5e43 LP |
1254 | if (a) |
1255 | return a->link->ifindex; | |
1256 | ||
1257 | return 0; | |
1258 | } | |
1259 | ||
eb60f9cd | 1260 | void manager_refresh_rrs(Manager *m) { |
eb60f9cd | 1261 | Link *l; |
6db6a464 | 1262 | DnssdService *s; |
eb60f9cd LP |
1263 | |
1264 | assert(m); | |
1265 | ||
78c6a153 LP |
1266 | m->llmnr_host_ipv4_key = dns_resource_key_unref(m->llmnr_host_ipv4_key); |
1267 | m->llmnr_host_ipv6_key = dns_resource_key_unref(m->llmnr_host_ipv6_key); | |
400cb36e DR |
1268 | m->mdns_host_ipv4_key = dns_resource_key_unref(m->mdns_host_ipv4_key); |
1269 | m->mdns_host_ipv6_key = dns_resource_key_unref(m->mdns_host_ipv6_key); | |
eb60f9cd | 1270 | |
ee3713b7 RB |
1271 | HASHMAP_FOREACH(l, m->links) |
1272 | link_add_rrs(l, true); | |
1273 | ||
6db6a464 | 1274 | if (m->mdns_support == RESOLVE_SUPPORT_YES) |
90e74a66 | 1275 | HASHMAP_FOREACH(s, m->dnssd_services) |
6db6a464 DR |
1276 | if (dnssd_update_rrs(s) < 0) |
1277 | log_warning("Failed to refresh DNS-SD service '%s'", s->name); | |
1278 | ||
ee3713b7 | 1279 | HASHMAP_FOREACH(l, m->links) |
eb60f9cd | 1280 | link_add_rrs(l, false); |
eb60f9cd LP |
1281 | } |
1282 | ||
e7c1b0e4 | 1283 | static int manager_next_random_name(const char *old, char **ret_new) { |
ec2c5e43 | 1284 | const char *p; |
556a2294 | 1285 | uint64_t u, a; |
e7c1b0e4 | 1286 | char *n; |
623a4c97 | 1287 | |
e7c1b0e4 | 1288 | p = strchr(old, 0); |
ec2c5e43 LP |
1289 | assert(p); |
1290 | ||
e7c1b0e4 | 1291 | while (p > old) { |
ff25d338 | 1292 | if (!ascii_isdigit(p[-1])) |
ec2c5e43 LP |
1293 | break; |
1294 | ||
1295 | p--; | |
1296 | } | |
1297 | ||
1298 | if (*p == 0 || safe_atou64(p, &u) < 0 || u <= 0) | |
1299 | u = 1; | |
1300 | ||
556a2294 LP |
1301 | /* Add a random number to the old value. This way we can avoid |
1302 | * that two hosts pick the same hostname, win on IPv4 and lose | |
1303 | * on IPv6 (or vice versa), and pick the same hostname | |
1304 | * replacement hostname, ad infinitum. We still want the | |
1305 | * numbers to go up monotonically, hence we just add a random | |
1306 | * value 1..10 */ | |
1307 | ||
1308 | random_bytes(&a, sizeof(a)); | |
1309 | u += 1 + a % 10; | |
ec2c5e43 | 1310 | |
e7c1b0e4 | 1311 | if (asprintf(&n, "%.*s%" PRIu64, (int) (p - old), old, u) < 0) |
ec2c5e43 LP |
1312 | return -ENOMEM; |
1313 | ||
e7c1b0e4 DR |
1314 | *ret_new = n; |
1315 | ||
1316 | return 0; | |
1317 | } | |
1318 | ||
1319 | int manager_next_hostname(Manager *m) { | |
1320 | _cleanup_free_ char *h = NULL, *k = NULL; | |
1321 | int r; | |
1322 | ||
1323 | assert(m); | |
1324 | ||
1325 | r = manager_next_random_name(m->llmnr_hostname, &h); | |
1326 | if (r < 0) | |
1327 | return r; | |
1328 | ||
7470cc4c | 1329 | r = dns_name_concat(h, "local", 0, &k); |
e7c1b0e4 | 1330 | if (r < 0) |
78c6a153 | 1331 | return r; |
78c6a153 LP |
1332 | |
1333 | log_info("Hostname conflict, changing published hostname from '%s' to '%s'.", m->llmnr_hostname, h); | |
1334 | ||
e7c1b0e4 DR |
1335 | free_and_replace(m->llmnr_hostname, h); |
1336 | free_and_replace(m->mdns_hostname, k); | |
ec2c5e43 | 1337 | |
eb60f9cd | 1338 | manager_refresh_rrs(m); |
86c0411e | 1339 | (void) manager_send_changed(m, "LLMNRHostname"); |
623a4c97 LP |
1340 | |
1341 | return 0; | |
1342 | } | |
ec2c5e43 | 1343 | |
4e945a6f | 1344 | LinkAddress* manager_find_link_address(Manager *m, int family, const union in_addr_union *in_addr) { |
ec2c5e43 LP |
1345 | Link *l; |
1346 | ||
1347 | assert(m); | |
1348 | ||
bb3b08ad YW |
1349 | if (!IN_SET(family, AF_INET, AF_INET6)) |
1350 | return NULL; | |
1351 | ||
1352 | if (!in_addr) | |
1353 | return NULL; | |
1354 | ||
90e74a66 | 1355 | HASHMAP_FOREACH(l, m->links) { |
ec2c5e43 LP |
1356 | LinkAddress *a; |
1357 | ||
1358 | a = link_find_address(l, family, in_addr); | |
1359 | if (a) | |
1360 | return a; | |
1361 | } | |
1362 | ||
1363 | return NULL; | |
1364 | } | |
1365 | ||
94378145 | 1366 | bool manager_packet_from_local_address(Manager *m, DnsPacket *p) { |
ec2c5e43 LP |
1367 | assert(m); |
1368 | assert(p); | |
1369 | ||
94378145 LP |
1370 | /* Let's see if this packet comes from an IP address we have on any local interface */ |
1371 | ||
4e945a6f | 1372 | return !!manager_find_link_address(m, p->family, &p->sender); |
ec2c5e43 | 1373 | } |
4e945a6f | 1374 | |
a9fd8837 LP |
1375 | bool manager_packet_from_our_transaction(Manager *m, DnsPacket *p) { |
1376 | DnsTransaction *t; | |
1377 | ||
1378 | assert(m); | |
1379 | assert(p); | |
1380 | ||
1381 | /* Let's see if we have a transaction with a query message with the exact same binary contents as the | |
1382 | * one we just got. If so, it's almost definitely a packet loop of some kind. */ | |
1383 | ||
1384 | t = hashmap_get(m->dns_transactions, UINT_TO_PTR(DNS_PACKET_ID(p))); | |
1385 | if (!t) | |
1386 | return false; | |
1387 | ||
1388 | return t->sent && dns_packet_equal(t->sent, p); | |
1389 | } | |
1390 | ||
a4076574 LP |
1391 | DnsScope* manager_find_scope(Manager *m, DnsPacket *p) { |
1392 | Link *l; | |
1393 | ||
1394 | assert(m); | |
1395 | assert(p); | |
1396 | ||
1397 | l = hashmap_get(m->links, INT_TO_PTR(p->ifindex)); | |
1398 | if (!l) | |
1399 | return NULL; | |
1400 | ||
b4f1862d DM |
1401 | switch (p->protocol) { |
1402 | case DNS_PROTOCOL_LLMNR: | |
a4076574 LP |
1403 | if (p->family == AF_INET) |
1404 | return l->llmnr_ipv4_scope; | |
1405 | else if (p->family == AF_INET6) | |
1406 | return l->llmnr_ipv6_scope; | |
b4f1862d DM |
1407 | |
1408 | break; | |
1409 | ||
1410 | case DNS_PROTOCOL_MDNS: | |
1411 | if (p->family == AF_INET) | |
1412 | return l->mdns_ipv4_scope; | |
1413 | else if (p->family == AF_INET6) | |
1414 | return l->mdns_ipv6_scope; | |
1415 | ||
1416 | break; | |
1417 | ||
1418 | default: | |
1419 | break; | |
a4076574 LP |
1420 | } |
1421 | ||
1422 | return NULL; | |
1423 | } | |
1424 | ||
902bb5d8 | 1425 | void manager_verify_all(Manager *m) { |
902bb5d8 LP |
1426 | assert(m); |
1427 | ||
1428 | LIST_FOREACH(scopes, s, m->dns_scopes) | |
1429 | dns_zone_verify_all(&s->zone); | |
1430 | } | |
1431 | ||
78c6a153 | 1432 | int manager_is_own_hostname(Manager *m, const char *name) { |
78c6a153 LP |
1433 | int r; |
1434 | ||
1435 | assert(m); | |
1436 | assert(name); | |
1437 | ||
1438 | if (m->llmnr_hostname) { | |
1439 | r = dns_name_equal(name, m->llmnr_hostname); | |
1440 | if (r != 0) | |
1441 | return r; | |
1442 | } | |
1443 | ||
e96de0ce LP |
1444 | if (m->mdns_hostname) { |
1445 | r = dns_name_equal(name, m->mdns_hostname); | |
1446 | if (r != 0) | |
1447 | return r; | |
1448 | } | |
1449 | ||
1450 | if (m->full_hostname) | |
1451 | return dns_name_equal(name, m->full_hostname); | |
78c6a153 LP |
1452 | |
1453 | return 0; | |
1454 | } | |
1455 | ||
9176a57c | 1456 | int manager_compile_dns_servers(Manager *m, OrderedSet **dns) { |
9176a57c LP |
1457 | Link *l; |
1458 | int r; | |
1459 | ||
1460 | assert(m); | |
1461 | assert(dns); | |
1462 | ||
1463 | r = ordered_set_ensure_allocated(dns, &dns_server_hash_ops); | |
1464 | if (r < 0) | |
1465 | return r; | |
1466 | ||
1467 | /* First add the system-wide servers and domains */ | |
1468 | LIST_FOREACH(servers, s, m->dns_servers) { | |
1469 | r = ordered_set_put(*dns, s); | |
1470 | if (r == -EEXIST) | |
1471 | continue; | |
1472 | if (r < 0) | |
1473 | return r; | |
1474 | } | |
1475 | ||
1476 | /* Then, add the per-link servers */ | |
90e74a66 | 1477 | HASHMAP_FOREACH(l, m->links) { |
9176a57c LP |
1478 | LIST_FOREACH(servers, s, l->dns_servers) { |
1479 | r = ordered_set_put(*dns, s); | |
1480 | if (r == -EEXIST) | |
1481 | continue; | |
1482 | if (r < 0) | |
1483 | return r; | |
1484 | } | |
1485 | } | |
1486 | ||
1487 | /* If we found nothing, add the fallback servers */ | |
1488 | if (ordered_set_isempty(*dns)) { | |
1489 | LIST_FOREACH(servers, s, m->fallback_dns_servers) { | |
1490 | r = ordered_set_put(*dns, s); | |
1491 | if (r == -EEXIST) | |
1492 | continue; | |
1493 | if (r < 0) | |
1494 | return r; | |
1495 | } | |
1496 | } | |
1497 | ||
1498 | return 0; | |
1499 | } | |
1500 | ||
94363cbb MP |
1501 | /* filter_route is a tri-state: |
1502 | * < 0: no filtering | |
1503 | * = 0 or false: return only domains which should be used for searching | |
1504 | * > 0 or true: return only domains which are for routing only | |
1505 | */ | |
6f7da49d | 1506 | int manager_compile_search_domains(Manager *m, OrderedSet **domains, int filter_route) { |
9176a57c LP |
1507 | Link *l; |
1508 | int r; | |
1509 | ||
1510 | assert(m); | |
1511 | assert(domains); | |
1512 | ||
1513 | r = ordered_set_ensure_allocated(domains, &dns_name_hash_ops); | |
1514 | if (r < 0) | |
1515 | return r; | |
1516 | ||
1517 | LIST_FOREACH(domains, d, m->search_domains) { | |
6f7da49d LP |
1518 | |
1519 | if (filter_route >= 0 && | |
1520 | d->route_only != !!filter_route) | |
1521 | continue; | |
1522 | ||
9176a57c LP |
1523 | r = ordered_set_put(*domains, d->name); |
1524 | if (r == -EEXIST) | |
1525 | continue; | |
1526 | if (r < 0) | |
1527 | return r; | |
1528 | } | |
1529 | ||
90e74a66 | 1530 | HASHMAP_FOREACH(l, m->links) { |
9176a57c LP |
1531 | |
1532 | LIST_FOREACH(domains, d, l->search_domains) { | |
6f7da49d LP |
1533 | |
1534 | if (filter_route >= 0 && | |
1535 | d->route_only != !!filter_route) | |
1536 | continue; | |
1537 | ||
9176a57c LP |
1538 | r = ordered_set_put(*domains, d->name); |
1539 | if (r == -EEXIST) | |
1540 | continue; | |
1541 | if (r < 0) | |
1542 | return r; | |
1543 | } | |
1544 | } | |
1545 | ||
1546 | return 0; | |
1547 | } | |
c69fa7e3 LP |
1548 | |
1549 | DnssecMode manager_get_dnssec_mode(Manager *m) { | |
1550 | assert(m); | |
1551 | ||
1552 | if (m->dnssec_mode != _DNSSEC_MODE_INVALID) | |
1553 | return m->dnssec_mode; | |
1554 | ||
1555 | return DNSSEC_NO; | |
1556 | } | |
1557 | ||
1558 | bool manager_dnssec_supported(Manager *m) { | |
1559 | DnsServer *server; | |
c69fa7e3 LP |
1560 | Link *l; |
1561 | ||
1562 | assert(m); | |
1563 | ||
1564 | if (manager_get_dnssec_mode(m) == DNSSEC_NO) | |
1565 | return false; | |
1566 | ||
1567 | server = manager_get_dns_server(m); | |
1568 | if (server && !dns_server_dnssec_supported(server)) | |
1569 | return false; | |
1570 | ||
90e74a66 | 1571 | HASHMAP_FOREACH(l, m->links) |
c69fa7e3 LP |
1572 | if (!link_dnssec_supported(l)) |
1573 | return false; | |
1574 | ||
1575 | return true; | |
1576 | } | |
59c5b597 | 1577 | |
c9299be2 | 1578 | DnsOverTlsMode manager_get_dns_over_tls_mode(Manager *m) { |
5d67a7ae IT |
1579 | assert(m); |
1580 | ||
c9299be2 IT |
1581 | if (m->dns_over_tls_mode != _DNS_OVER_TLS_MODE_INVALID) |
1582 | return m->dns_over_tls_mode; | |
5d67a7ae | 1583 | |
c9299be2 | 1584 | return DNS_OVER_TLS_NO; |
5d67a7ae IT |
1585 | } |
1586 | ||
59c5b597 LP |
1587 | void manager_dnssec_verdict(Manager *m, DnssecVerdict verdict, const DnsResourceKey *key) { |
1588 | ||
1589 | assert(verdict >= 0); | |
1590 | assert(verdict < _DNSSEC_VERDICT_MAX); | |
1591 | ||
f1d34068 | 1592 | if (DEBUG_LOGGING) { |
202b76ae | 1593 | char s[DNS_RESOURCE_KEY_STRING_MAX]; |
59c5b597 | 1594 | |
202b76ae ZJS |
1595 | log_debug("Found verdict for lookup %s: %s", |
1596 | dns_resource_key_to_string(key, s, sizeof s), | |
1597 | dnssec_verdict_to_string(verdict)); | |
59c5b597 LP |
1598 | } |
1599 | ||
1600 | m->n_dnssec_verdict[verdict]++; | |
1601 | } | |
011696f7 | 1602 | |
de4a0138 | 1603 | bool manager_routable(Manager *m) { |
011696f7 LP |
1604 | Link *l; |
1605 | ||
1606 | assert(m); | |
1607 | ||
de4a0138 | 1608 | /* Returns true if the host has at least one interface with a routable address (regardless if IPv4 or IPv6) */ |
011696f7 | 1609 | |
90e74a66 | 1610 | HASHMAP_FOREACH(l, m->links) |
de4a0138 | 1611 | if (link_relevant(l, AF_UNSPEC, false)) |
011696f7 LP |
1612 | return true; |
1613 | ||
1614 | return false; | |
1615 | } | |
ba35662f | 1616 | |
90df0fbe | 1617 | void manager_flush_caches(Manager *m, int log_level) { |
ba35662f LP |
1618 | assert(m); |
1619 | ||
1620 | LIST_FOREACH(scopes, scope, m->dns_scopes) | |
1621 | dns_cache_flush(&scope->cache); | |
aac57b35 | 1622 | |
90df0fbe | 1623 | log_full(log_level, "Flushed all caches."); |
ba35662f | 1624 | } |
943ef07c | 1625 | |
59c0fd0e | 1626 | void manager_reset_server_features(Manager *m) { |
59c0fd0e LP |
1627 | Link *l; |
1628 | ||
1629 | dns_server_reset_features_all(m->dns_servers); | |
1630 | dns_server_reset_features_all(m->fallback_dns_servers); | |
1631 | ||
90e74a66 | 1632 | HASHMAP_FOREACH(l, m->links) |
59c0fd0e LP |
1633 | dns_server_reset_features_all(l->dns_servers); |
1634 | ||
1635 | log_info("Resetting learnt feature levels on all servers."); | |
1636 | } | |
1637 | ||
943ef07c LP |
1638 | void manager_cleanup_saved_user(Manager *m) { |
1639 | _cleanup_closedir_ DIR *d = NULL; | |
943ef07c LP |
1640 | |
1641 | assert(m); | |
1642 | ||
1643 | /* Clean up all saved per-link files in /run/systemd/resolve/netif/ that don't have a matching interface | |
1644 | * anymore. These files are created to persist settings pushed in by the user via the bus, so that resolved can | |
1645 | * be restarted without losing this data. */ | |
1646 | ||
1647 | d = opendir("/run/systemd/resolve/netif/"); | |
1648 | if (!d) { | |
1649 | if (errno == ENOENT) | |
1650 | return; | |
1651 | ||
1652 | log_warning_errno(errno, "Failed to open interface directory: %m"); | |
1653 | return; | |
1654 | } | |
1655 | ||
1656 | FOREACH_DIRENT_ALL(de, d, log_error_errno(errno, "Failed to read interface directory: %m")) { | |
1657 | _cleanup_free_ char *p = NULL; | |
1658 | int ifindex; | |
1659 | Link *l; | |
1660 | ||
1661 | if (!IN_SET(de->d_type, DT_UNKNOWN, DT_REG)) | |
1662 | continue; | |
1663 | ||
49bfc877 | 1664 | if (dot_or_dot_dot(de->d_name)) |
943ef07c LP |
1665 | continue; |
1666 | ||
597da51b ZJS |
1667 | ifindex = parse_ifindex(de->d_name); |
1668 | if (ifindex < 0) /* Probably some temporary file from a previous run. Delete it */ | |
943ef07c LP |
1669 | goto rm; |
1670 | ||
1671 | l = hashmap_get(m->links, INT_TO_PTR(ifindex)); | |
1672 | if (!l) /* link vanished */ | |
1673 | goto rm; | |
1674 | ||
1675 | if (l->is_managed) /* now managed by networkd, hence the bus settings are useless */ | |
1676 | goto rm; | |
1677 | ||
1678 | continue; | |
1679 | ||
1680 | rm: | |
b910cc72 | 1681 | p = path_join("/run/systemd/resolve/netif", de->d_name); |
943ef07c LP |
1682 | if (!p) { |
1683 | log_oom(); | |
1684 | return; | |
1685 | } | |
1686 | ||
1687 | (void) unlink(p); | |
1688 | } | |
1689 | } | |
e7c1b0e4 DR |
1690 | |
1691 | bool manager_next_dnssd_names(Manager *m) { | |
e7c1b0e4 DR |
1692 | DnssdService *s; |
1693 | bool tried = false; | |
1694 | int r; | |
1695 | ||
1696 | assert(m); | |
1697 | ||
90e74a66 | 1698 | HASHMAP_FOREACH(s, m->dnssd_services) { |
e7c1b0e4 DR |
1699 | _cleanup_free_ char * new_name = NULL; |
1700 | ||
1701 | if (!s->withdrawn) | |
1702 | continue; | |
1703 | ||
1704 | r = manager_next_random_name(s->name_template, &new_name); | |
1705 | if (r < 0) { | |
1706 | log_warning_errno(r, "Failed to get new name for service '%s': %m", s->name); | |
1707 | continue; | |
1708 | } | |
1709 | ||
1710 | free_and_replace(s->name_template, new_name); | |
1711 | ||
1712 | s->withdrawn = false; | |
1713 | ||
1714 | tried = true; | |
1715 | } | |
1716 | ||
1717 | if (tried) | |
1718 | manager_refresh_rrs(m); | |
1719 | ||
1720 | return tried; | |
1721 | } | |
49ef064c | 1722 | |
281df579 | 1723 | bool manager_server_is_stub(Manager *m, DnsServer *s) { |
49ef064c LP |
1724 | DnsStubListenerExtra *l; |
1725 | ||
1726 | assert(m); | |
281df579 | 1727 | assert(s); |
49ef064c LP |
1728 | |
1729 | /* Safety check: we generally already skip the main stub when parsing configuration. But let's be | |
1730 | * extra careful, and check here again */ | |
281df579 LP |
1731 | if (s->family == AF_INET && |
1732 | s->address.in.s_addr == htobe32(INADDR_DNS_STUB) && | |
1733 | dns_server_port(s) == 53) | |
49ef064c LP |
1734 | return true; |
1735 | ||
1736 | /* Main reason to call this is to check server data against the extra listeners, and filter things | |
1737 | * out. */ | |
1738 | ORDERED_SET_FOREACH(l, m->dns_extra_stub_listeners) | |
281df579 LP |
1739 | if (s->family == l->family && |
1740 | in_addr_equal(s->family, &s->address, &l->address) && | |
1741 | dns_server_port(s) == dns_stub_listener_extra_port(l)) | |
49ef064c LP |
1742 | return true; |
1743 | ||
1744 | return false; | |
1745 | } | |
eb170e75 LP |
1746 | |
1747 | int socket_disable_pmtud(int fd, int af) { | |
1748 | int r; | |
1749 | ||
1750 | assert(fd >= 0); | |
1751 | ||
1752 | if (af == AF_UNSPEC) { | |
5f64d2bf LP |
1753 | af = socket_get_family(fd); |
1754 | if (af < 0) | |
1755 | return af; | |
eb170e75 LP |
1756 | } |
1757 | ||
1758 | switch (af) { | |
1759 | ||
1760 | case AF_INET: { | |
1761 | /* Turn off path MTU discovery, let's rather fragment on the way than to open us up against | |
1762 | * PMTU forgery vulnerabilities. | |
1763 | * | |
1764 | * There appears to be no documentation about IP_PMTUDISC_OMIT, but it has the effect that | |
1765 | * the "Don't Fragment" bit in the IPv4 header is turned off, thus enforcing fragmentation if | |
1766 | * our datagram size exceeds the MTU of a router in the path, and turning off path MTU | |
1767 | * discovery. | |
1768 | * | |
1769 | * This helps mitigating the PMTUD vulnerability described here: | |
1770 | * | |
1771 | * https://blog.apnic.net/2019/07/12/its-time-to-consider-avoiding-ip-fragmentation-in-the-dns/ | |
1772 | * | |
1773 | * Similar logic is in place in most DNS servers. | |
1774 | * | |
1775 | * There are multiple conflicting goals: we want to allow the largest datagrams possible (for | |
1776 | * efficiency reasons), but not have fragmentation (for security reasons), nor use PMTUD (for | |
1777 | * security reasons, too). Our strategy to deal with this is: use large packets, turn off | |
1778 | * PMTUD, but watch fragmentation taking place, and then size our packets to the max of the | |
1779 | * fragments seen — and if we need larger packets always go to TCP. | |
1780 | */ | |
1781 | ||
1782 | r = setsockopt_int(fd, IPPROTO_IP, IP_MTU_DISCOVER, IP_PMTUDISC_OMIT); | |
1783 | if (r < 0) | |
1784 | return r; | |
1785 | ||
1786 | return 0; | |
1787 | } | |
1788 | ||
1789 | case AF_INET6: { | |
1790 | /* On IPv6 fragmentation only is done by the sender — never by routers on the path. PMTUD is | |
1791 | * mandatory. If we want to turn off PMTUD, the only way is by sending with minimal MTU only, | |
1792 | * so that we apply maximum fragmentation locally already, and thus PMTUD doesn't happen | |
1793 | * because there's nothing that could be fragmented further anymore. */ | |
1794 | ||
1795 | r = setsockopt_int(fd, IPPROTO_IPV6, IPV6_MTU, IPV6_MIN_MTU); | |
1796 | if (r < 0) | |
1797 | return r; | |
1798 | ||
1799 | return 0; | |
1800 | } | |
1801 | ||
1802 | default: | |
1803 | return -EAFNOSUPPORT; | |
1804 | } | |
1805 | } |