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