]>
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 | 390 | _cleanup_free_ char *h = NULL, *n = NULL, *m = NULL; |
fd7e9887 | 391 | char label[DNS_LABEL_MAX+1]; |
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 | |
2a1ffd3e | 734 | hashmap_free(m->polkit_registry); |
2400ae29 | 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, | |
ae55c9c0 | 1108 | DnsPacket *question_bypass, |
72c2d39e LP |
1109 | DnsQuestion *collected_questions, |
1110 | DnsAnswer *answer) { | |
1111 | ||
1112 | _cleanup_(json_variant_unrefp) JsonVariant *jquestion = NULL, *jcollected_questions = NULL, *janswer = NULL; | |
1113 | _cleanup_(dns_question_unrefp) DnsQuestion *merged = NULL; | |
cb456374 | 1114 | Varlink *connection; |
72c2d39e LP |
1115 | DnsAnswerItem *rri; |
1116 | int r; | |
cb456374 SK |
1117 | |
1118 | assert(m); | |
1119 | ||
1120 | if (set_isempty(m->varlink_subscription)) | |
1121 | return 0; | |
1122 | ||
ae55c9c0 | 1123 | /* Merge all questions into one */ |
72c2d39e LP |
1124 | r = dns_question_merge(question_idna, question_utf8, &merged); |
1125 | if (r < 0) | |
1126 | return log_error_errno(r, "Failed to merge UTF8/IDNA questions: %m"); | |
1127 | ||
ae55c9c0 LP |
1128 | if (question_bypass) { |
1129 | _cleanup_(dns_question_unrefp) DnsQuestion *merged2 = NULL; | |
1130 | ||
1131 | r = dns_question_merge(merged, question_bypass->question, &merged2); | |
1132 | if (r < 0) | |
1133 | return log_error_errno(r, "Failed to merge UTF8/IDNA questions and DNS packet question: %m"); | |
1134 | ||
1135 | dns_question_unref(merged); | |
1136 | merged = TAKE_PTR(merged2); | |
1137 | } | |
1138 | ||
72c2d39e LP |
1139 | /* Convert the current primary question to JSON */ |
1140 | r = dns_question_to_json(merged, &jquestion); | |
1141 | if (r < 0) | |
1142 | return log_error_errno(r, "Failed to convert question to JSON: %m"); | |
cb456374 | 1143 | |
64ebc0da | 1144 | /* Generate a JSON array of the questions preceding the current one in the CNAME chain */ |
72c2d39e LP |
1145 | r = dns_question_to_json(collected_questions, &jcollected_questions); |
1146 | if (r < 0) | |
1147 | return log_error_errno(r, "Failed to convert question to JSON: %m"); | |
1148 | ||
1149 | DNS_ANSWER_FOREACH_ITEM(rri, answer) { | |
c91f581c | 1150 | _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; |
72c2d39e LP |
1151 | |
1152 | r = dns_resource_record_to_json(rri->rr, &v); | |
cb456374 | 1153 | if (r < 0) |
72c2d39e | 1154 | return log_error_errno(r, "Failed to convert answer resource record to JSON: %m"); |
cb456374 | 1155 | |
72c2d39e LP |
1156 | 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 */ |
1157 | if (r < 0) | |
1158 | return log_error_errno(r, "Failed to generate RR wire format: %m"); | |
cb456374 | 1159 | |
c91f581c LP |
1160 | r = json_variant_append_arrayb( |
1161 | &janswer, | |
1162 | JSON_BUILD_OBJECT( | |
1163 | JSON_BUILD_PAIR_CONDITION(v, "rr", JSON_BUILD_VARIANT(v)), | |
1164 | JSON_BUILD_PAIR("raw", JSON_BUILD_BASE64(rri->rr->wire_format, rri->rr->wire_format_size)), | |
1165 | JSON_BUILD_PAIR_CONDITION(rri->ifindex > 0, "ifindex", JSON_BUILD_INTEGER(rri->ifindex)))); | |
72c2d39e LP |
1166 | if (r < 0) |
1167 | return log_debug_errno(r, "Failed to append notification entry to array: %m"); | |
1168 | } | |
cb456374 SK |
1169 | |
1170 | SET_FOREACH(connection, m->varlink_subscription) { | |
1171 | r = varlink_notifyb(connection, | |
72c2d39e LP |
1172 | JSON_BUILD_OBJECT(JSON_BUILD_PAIR("state", JSON_BUILD_STRING(dns_transaction_state_to_string(state))), |
1173 | JSON_BUILD_PAIR_CONDITION(state == DNS_TRANSACTION_RCODE_FAILURE, "rcode", JSON_BUILD_INTEGER(rcode)), | |
1174 | JSON_BUILD_PAIR_CONDITION(state == DNS_TRANSACTION_ERRNO, "errno", JSON_BUILD_INTEGER(error)), | |
1175 | JSON_BUILD_PAIR("question", JSON_BUILD_VARIANT(jquestion)), | |
1176 | JSON_BUILD_PAIR_CONDITION(jcollected_questions, "collectedQuestions", JSON_BUILD_VARIANT(jcollected_questions)), | |
1177 | JSON_BUILD_PAIR_CONDITION(janswer, "answer", JSON_BUILD_VARIANT(janswer)))); | |
cb456374 | 1178 | if (r < 0) |
72c2d39e | 1179 | log_debug_errno(r, "Failed to send monitor event, ignoring: %m"); |
cb456374 | 1180 | } |
72c2d39e | 1181 | |
cb456374 SK |
1182 | return 0; |
1183 | } | |
1184 | ||
b30bf55d LP |
1185 | int manager_send( |
1186 | Manager *m, | |
1187 | int fd, | |
1188 | int ifindex, | |
1189 | int family, | |
1190 | const union in_addr_union *destination, | |
1191 | uint16_t port, | |
1192 | const union in_addr_union *source, | |
1193 | DnsPacket *p) { | |
1194 | ||
1716f6dc LP |
1195 | assert(m); |
1196 | assert(fd >= 0); | |
b30bf55d | 1197 | assert(destination); |
1716f6dc LP |
1198 | assert(port > 0); |
1199 | assert(p); | |
1200 | ||
614af79c YW |
1201 | /* For mDNS, it is natural that the packet have truncated flag when we have many known answers. */ |
1202 | bool truncated = DNS_PACKET_TC(p) && (p->protocol != DNS_PROTOCOL_MDNS || !p->more); | |
1203 | ||
76f77229 | 1204 | log_debug("Sending %s%s packet with id %" PRIu16 " on interface %i/%s of size %zu.", |
614af79c | 1205 | truncated ? "truncated (!) " : "", |
76f77229 LP |
1206 | DNS_PACKET_QR(p) ? "response" : "query", |
1207 | DNS_PACKET_ID(p), | |
1208 | ifindex, af_to_name(family), | |
1209 | p->size); | |
a2a416f7 | 1210 | |
1716f6dc | 1211 | if (family == AF_INET) |
25270cf3 | 1212 | return manager_ipv4_send(m, fd, ifindex, &destination->in, port, source ? &source->in : NULL, p); |
2817157b | 1213 | if (family == AF_INET6) |
25270cf3 | 1214 | return manager_ipv6_send(m, fd, ifindex, &destination->in6, port, source ? &source->in6 : NULL, p); |
1716f6dc LP |
1215 | |
1216 | return -EAFNOSUPPORT; | |
1217 | } | |
1218 | ||
e1c95994 LP |
1219 | uint32_t manager_find_mtu(Manager *m) { |
1220 | uint32_t mtu = 0; | |
1221 | Link *l; | |
e1c95994 | 1222 | |
5a0d0b8f | 1223 | /* If we don't know on which link a DNS packet would be delivered, let's find the largest MTU that |
4301cb32 | 1224 | * works on all interfaces we know of that have an IP address associated */ |
e1c95994 | 1225 | |
90e74a66 | 1226 | HASHMAP_FOREACH(l, m->links) { |
5a0d0b8f LP |
1227 | /* Let's filter out links without IP addresses (e.g. AF_CAN links and suchlike) */ |
1228 | if (!l->addresses) | |
1229 | continue; | |
1230 | ||
1231 | /* Safety check: MTU shorter than what we need for the absolutely shortest DNS request? Then | |
1232 | * let's ignore this link. */ | |
1233 | if (l->mtu < MIN(UDP4_PACKET_HEADER_SIZE + DNS_PACKET_HEADER_SIZE, | |
1234 | UDP6_PACKET_HEADER_SIZE + DNS_PACKET_HEADER_SIZE)) | |
e1c95994 LP |
1235 | continue; |
1236 | ||
1237 | if (mtu <= 0 || l->mtu < mtu) | |
1238 | mtu = l->mtu; | |
1239 | } | |
1240 | ||
5a0d0b8f LP |
1241 | if (mtu == 0) /* found nothing? then let's assume the typical Ethernet MTU for lack of anything more precise */ |
1242 | return 1500; | |
1243 | ||
e1c95994 LP |
1244 | return mtu; |
1245 | } | |
1716f6dc | 1246 | |
623a4c97 | 1247 | int manager_find_ifindex(Manager *m, int family, const union in_addr_union *in_addr) { |
ec2c5e43 LP |
1248 | LinkAddress *a; |
1249 | ||
1250 | assert(m); | |
1251 | ||
bb3b08ad YW |
1252 | if (!IN_SET(family, AF_INET, AF_INET6)) |
1253 | return 0; | |
1254 | ||
1255 | if (!in_addr) | |
1256 | return 0; | |
1257 | ||
4e945a6f | 1258 | a = manager_find_link_address(m, family, in_addr); |
ec2c5e43 LP |
1259 | if (a) |
1260 | return a->link->ifindex; | |
1261 | ||
1262 | return 0; | |
1263 | } | |
1264 | ||
eb60f9cd | 1265 | void manager_refresh_rrs(Manager *m) { |
eb60f9cd | 1266 | Link *l; |
6db6a464 | 1267 | DnssdService *s; |
eb60f9cd LP |
1268 | |
1269 | assert(m); | |
1270 | ||
78c6a153 LP |
1271 | m->llmnr_host_ipv4_key = dns_resource_key_unref(m->llmnr_host_ipv4_key); |
1272 | m->llmnr_host_ipv6_key = dns_resource_key_unref(m->llmnr_host_ipv6_key); | |
400cb36e DR |
1273 | m->mdns_host_ipv4_key = dns_resource_key_unref(m->mdns_host_ipv4_key); |
1274 | m->mdns_host_ipv6_key = dns_resource_key_unref(m->mdns_host_ipv6_key); | |
eb60f9cd | 1275 | |
ee3713b7 RB |
1276 | HASHMAP_FOREACH(l, m->links) |
1277 | link_add_rrs(l, true); | |
1278 | ||
6db6a464 | 1279 | if (m->mdns_support == RESOLVE_SUPPORT_YES) |
90e74a66 | 1280 | HASHMAP_FOREACH(s, m->dnssd_services) |
6db6a464 DR |
1281 | if (dnssd_update_rrs(s) < 0) |
1282 | log_warning("Failed to refresh DNS-SD service '%s'", s->name); | |
1283 | ||
ee3713b7 | 1284 | HASHMAP_FOREACH(l, m->links) |
eb60f9cd | 1285 | link_add_rrs(l, false); |
eb60f9cd LP |
1286 | } |
1287 | ||
e7c1b0e4 | 1288 | static int manager_next_random_name(const char *old, char **ret_new) { |
ec2c5e43 | 1289 | const char *p; |
556a2294 | 1290 | uint64_t u, a; |
e7c1b0e4 | 1291 | char *n; |
623a4c97 | 1292 | |
e7c1b0e4 | 1293 | p = strchr(old, 0); |
ec2c5e43 LP |
1294 | assert(p); |
1295 | ||
e7c1b0e4 | 1296 | while (p > old) { |
ff25d338 | 1297 | if (!ascii_isdigit(p[-1])) |
ec2c5e43 LP |
1298 | break; |
1299 | ||
1300 | p--; | |
1301 | } | |
1302 | ||
1303 | if (*p == 0 || safe_atou64(p, &u) < 0 || u <= 0) | |
1304 | u = 1; | |
1305 | ||
556a2294 LP |
1306 | /* Add a random number to the old value. This way we can avoid |
1307 | * that two hosts pick the same hostname, win on IPv4 and lose | |
1308 | * on IPv6 (or vice versa), and pick the same hostname | |
1309 | * replacement hostname, ad infinitum. We still want the | |
1310 | * numbers to go up monotonically, hence we just add a random | |
1311 | * value 1..10 */ | |
1312 | ||
1313 | random_bytes(&a, sizeof(a)); | |
1314 | u += 1 + a % 10; | |
ec2c5e43 | 1315 | |
e7c1b0e4 | 1316 | if (asprintf(&n, "%.*s%" PRIu64, (int) (p - old), old, u) < 0) |
ec2c5e43 LP |
1317 | return -ENOMEM; |
1318 | ||
e7c1b0e4 DR |
1319 | *ret_new = n; |
1320 | ||
1321 | return 0; | |
1322 | } | |
1323 | ||
1324 | int manager_next_hostname(Manager *m) { | |
1325 | _cleanup_free_ char *h = NULL, *k = NULL; | |
1326 | int r; | |
1327 | ||
1328 | assert(m); | |
1329 | ||
1330 | r = manager_next_random_name(m->llmnr_hostname, &h); | |
1331 | if (r < 0) | |
1332 | return r; | |
1333 | ||
7470cc4c | 1334 | r = dns_name_concat(h, "local", 0, &k); |
e7c1b0e4 | 1335 | if (r < 0) |
78c6a153 | 1336 | return r; |
78c6a153 LP |
1337 | |
1338 | log_info("Hostname conflict, changing published hostname from '%s' to '%s'.", m->llmnr_hostname, h); | |
1339 | ||
e7c1b0e4 DR |
1340 | free_and_replace(m->llmnr_hostname, h); |
1341 | free_and_replace(m->mdns_hostname, k); | |
ec2c5e43 | 1342 | |
eb60f9cd | 1343 | manager_refresh_rrs(m); |
86c0411e | 1344 | (void) manager_send_changed(m, "LLMNRHostname"); |
623a4c97 LP |
1345 | |
1346 | return 0; | |
1347 | } | |
ec2c5e43 | 1348 | |
4e945a6f | 1349 | LinkAddress* manager_find_link_address(Manager *m, int family, const union in_addr_union *in_addr) { |
ec2c5e43 LP |
1350 | Link *l; |
1351 | ||
1352 | assert(m); | |
1353 | ||
bb3b08ad YW |
1354 | if (!IN_SET(family, AF_INET, AF_INET6)) |
1355 | return NULL; | |
1356 | ||
1357 | if (!in_addr) | |
1358 | return NULL; | |
1359 | ||
90e74a66 | 1360 | HASHMAP_FOREACH(l, m->links) { |
ec2c5e43 LP |
1361 | LinkAddress *a; |
1362 | ||
1363 | a = link_find_address(l, family, in_addr); | |
1364 | if (a) | |
1365 | return a; | |
1366 | } | |
1367 | ||
1368 | return NULL; | |
1369 | } | |
1370 | ||
94378145 | 1371 | bool manager_packet_from_local_address(Manager *m, DnsPacket *p) { |
ec2c5e43 LP |
1372 | assert(m); |
1373 | assert(p); | |
1374 | ||
94378145 LP |
1375 | /* Let's see if this packet comes from an IP address we have on any local interface */ |
1376 | ||
4e945a6f | 1377 | return !!manager_find_link_address(m, p->family, &p->sender); |
ec2c5e43 | 1378 | } |
4e945a6f | 1379 | |
a9fd8837 LP |
1380 | bool manager_packet_from_our_transaction(Manager *m, DnsPacket *p) { |
1381 | DnsTransaction *t; | |
1382 | ||
1383 | assert(m); | |
1384 | assert(p); | |
1385 | ||
1386 | /* Let's see if we have a transaction with a query message with the exact same binary contents as the | |
1387 | * one we just got. If so, it's almost definitely a packet loop of some kind. */ | |
1388 | ||
1389 | t = hashmap_get(m->dns_transactions, UINT_TO_PTR(DNS_PACKET_ID(p))); | |
1390 | if (!t) | |
1391 | return false; | |
1392 | ||
1393 | return t->sent && dns_packet_equal(t->sent, p); | |
1394 | } | |
1395 | ||
a4076574 LP |
1396 | DnsScope* manager_find_scope(Manager *m, DnsPacket *p) { |
1397 | Link *l; | |
1398 | ||
1399 | assert(m); | |
1400 | assert(p); | |
1401 | ||
1402 | l = hashmap_get(m->links, INT_TO_PTR(p->ifindex)); | |
1403 | if (!l) | |
1404 | return NULL; | |
1405 | ||
b4f1862d DM |
1406 | switch (p->protocol) { |
1407 | case DNS_PROTOCOL_LLMNR: | |
a4076574 LP |
1408 | if (p->family == AF_INET) |
1409 | return l->llmnr_ipv4_scope; | |
1410 | else if (p->family == AF_INET6) | |
1411 | return l->llmnr_ipv6_scope; | |
b4f1862d DM |
1412 | |
1413 | break; | |
1414 | ||
1415 | case DNS_PROTOCOL_MDNS: | |
1416 | if (p->family == AF_INET) | |
1417 | return l->mdns_ipv4_scope; | |
1418 | else if (p->family == AF_INET6) | |
1419 | return l->mdns_ipv6_scope; | |
1420 | ||
1421 | break; | |
1422 | ||
1423 | default: | |
1424 | break; | |
a4076574 LP |
1425 | } |
1426 | ||
1427 | return NULL; | |
1428 | } | |
1429 | ||
902bb5d8 | 1430 | void manager_verify_all(Manager *m) { |
902bb5d8 LP |
1431 | assert(m); |
1432 | ||
1433 | LIST_FOREACH(scopes, s, m->dns_scopes) | |
1434 | dns_zone_verify_all(&s->zone); | |
1435 | } | |
1436 | ||
78c6a153 | 1437 | int manager_is_own_hostname(Manager *m, const char *name) { |
78c6a153 LP |
1438 | int r; |
1439 | ||
1440 | assert(m); | |
1441 | assert(name); | |
1442 | ||
1443 | if (m->llmnr_hostname) { | |
1444 | r = dns_name_equal(name, m->llmnr_hostname); | |
1445 | if (r != 0) | |
1446 | return r; | |
1447 | } | |
1448 | ||
e96de0ce LP |
1449 | if (m->mdns_hostname) { |
1450 | r = dns_name_equal(name, m->mdns_hostname); | |
1451 | if (r != 0) | |
1452 | return r; | |
1453 | } | |
1454 | ||
1455 | if (m->full_hostname) | |
1456 | return dns_name_equal(name, m->full_hostname); | |
78c6a153 LP |
1457 | |
1458 | return 0; | |
1459 | } | |
1460 | ||
9176a57c | 1461 | int manager_compile_dns_servers(Manager *m, OrderedSet **dns) { |
9176a57c LP |
1462 | Link *l; |
1463 | int r; | |
1464 | ||
1465 | assert(m); | |
1466 | assert(dns); | |
1467 | ||
1468 | r = ordered_set_ensure_allocated(dns, &dns_server_hash_ops); | |
1469 | if (r < 0) | |
1470 | return r; | |
1471 | ||
1472 | /* First add the system-wide servers and domains */ | |
1473 | LIST_FOREACH(servers, s, m->dns_servers) { | |
1474 | r = ordered_set_put(*dns, s); | |
1475 | if (r == -EEXIST) | |
1476 | continue; | |
1477 | if (r < 0) | |
1478 | return r; | |
1479 | } | |
1480 | ||
1481 | /* Then, add the per-link servers */ | |
90e74a66 | 1482 | HASHMAP_FOREACH(l, m->links) { |
9176a57c LP |
1483 | LIST_FOREACH(servers, s, l->dns_servers) { |
1484 | r = ordered_set_put(*dns, s); | |
1485 | if (r == -EEXIST) | |
1486 | continue; | |
1487 | if (r < 0) | |
1488 | return r; | |
1489 | } | |
1490 | } | |
1491 | ||
1492 | /* If we found nothing, add the fallback servers */ | |
1493 | if (ordered_set_isempty(*dns)) { | |
1494 | LIST_FOREACH(servers, s, m->fallback_dns_servers) { | |
1495 | r = ordered_set_put(*dns, s); | |
1496 | if (r == -EEXIST) | |
1497 | continue; | |
1498 | if (r < 0) | |
1499 | return r; | |
1500 | } | |
1501 | } | |
1502 | ||
1503 | return 0; | |
1504 | } | |
1505 | ||
94363cbb MP |
1506 | /* filter_route is a tri-state: |
1507 | * < 0: no filtering | |
1508 | * = 0 or false: return only domains which should be used for searching | |
1509 | * > 0 or true: return only domains which are for routing only | |
1510 | */ | |
6f7da49d | 1511 | int manager_compile_search_domains(Manager *m, OrderedSet **domains, int filter_route) { |
9176a57c LP |
1512 | Link *l; |
1513 | int r; | |
1514 | ||
1515 | assert(m); | |
1516 | assert(domains); | |
1517 | ||
1518 | r = ordered_set_ensure_allocated(domains, &dns_name_hash_ops); | |
1519 | if (r < 0) | |
1520 | return r; | |
1521 | ||
1522 | LIST_FOREACH(domains, d, m->search_domains) { | |
6f7da49d LP |
1523 | |
1524 | if (filter_route >= 0 && | |
1525 | d->route_only != !!filter_route) | |
1526 | continue; | |
1527 | ||
9176a57c LP |
1528 | r = ordered_set_put(*domains, d->name); |
1529 | if (r == -EEXIST) | |
1530 | continue; | |
1531 | if (r < 0) | |
1532 | return r; | |
1533 | } | |
1534 | ||
90e74a66 | 1535 | HASHMAP_FOREACH(l, m->links) { |
9176a57c LP |
1536 | |
1537 | LIST_FOREACH(domains, d, l->search_domains) { | |
6f7da49d LP |
1538 | |
1539 | if (filter_route >= 0 && | |
1540 | d->route_only != !!filter_route) | |
1541 | continue; | |
1542 | ||
9176a57c LP |
1543 | r = ordered_set_put(*domains, d->name); |
1544 | if (r == -EEXIST) | |
1545 | continue; | |
1546 | if (r < 0) | |
1547 | return r; | |
1548 | } | |
1549 | } | |
1550 | ||
1551 | return 0; | |
1552 | } | |
c69fa7e3 LP |
1553 | |
1554 | DnssecMode manager_get_dnssec_mode(Manager *m) { | |
1555 | assert(m); | |
1556 | ||
1557 | if (m->dnssec_mode != _DNSSEC_MODE_INVALID) | |
1558 | return m->dnssec_mode; | |
1559 | ||
1560 | return DNSSEC_NO; | |
1561 | } | |
1562 | ||
1563 | bool manager_dnssec_supported(Manager *m) { | |
1564 | DnsServer *server; | |
c69fa7e3 LP |
1565 | Link *l; |
1566 | ||
1567 | assert(m); | |
1568 | ||
1569 | if (manager_get_dnssec_mode(m) == DNSSEC_NO) | |
1570 | return false; | |
1571 | ||
1572 | server = manager_get_dns_server(m); | |
1573 | if (server && !dns_server_dnssec_supported(server)) | |
1574 | return false; | |
1575 | ||
90e74a66 | 1576 | HASHMAP_FOREACH(l, m->links) |
c69fa7e3 LP |
1577 | if (!link_dnssec_supported(l)) |
1578 | return false; | |
1579 | ||
1580 | return true; | |
1581 | } | |
59c5b597 | 1582 | |
c9299be2 | 1583 | DnsOverTlsMode manager_get_dns_over_tls_mode(Manager *m) { |
5d67a7ae IT |
1584 | assert(m); |
1585 | ||
c9299be2 IT |
1586 | if (m->dns_over_tls_mode != _DNS_OVER_TLS_MODE_INVALID) |
1587 | return m->dns_over_tls_mode; | |
5d67a7ae | 1588 | |
c9299be2 | 1589 | return DNS_OVER_TLS_NO; |
5d67a7ae IT |
1590 | } |
1591 | ||
59c5b597 LP |
1592 | void manager_dnssec_verdict(Manager *m, DnssecVerdict verdict, const DnsResourceKey *key) { |
1593 | ||
1594 | assert(verdict >= 0); | |
1595 | assert(verdict < _DNSSEC_VERDICT_MAX); | |
1596 | ||
f1d34068 | 1597 | if (DEBUG_LOGGING) { |
202b76ae | 1598 | char s[DNS_RESOURCE_KEY_STRING_MAX]; |
59c5b597 | 1599 | |
202b76ae ZJS |
1600 | log_debug("Found verdict for lookup %s: %s", |
1601 | dns_resource_key_to_string(key, s, sizeof s), | |
1602 | dnssec_verdict_to_string(verdict)); | |
59c5b597 LP |
1603 | } |
1604 | ||
1605 | m->n_dnssec_verdict[verdict]++; | |
1606 | } | |
011696f7 | 1607 | |
de4a0138 | 1608 | bool manager_routable(Manager *m) { |
011696f7 LP |
1609 | Link *l; |
1610 | ||
1611 | assert(m); | |
1612 | ||
de4a0138 | 1613 | /* Returns true if the host has at least one interface with a routable address (regardless if IPv4 or IPv6) */ |
011696f7 | 1614 | |
90e74a66 | 1615 | HASHMAP_FOREACH(l, m->links) |
de4a0138 | 1616 | if (link_relevant(l, AF_UNSPEC, false)) |
011696f7 LP |
1617 | return true; |
1618 | ||
1619 | return false; | |
1620 | } | |
ba35662f | 1621 | |
90df0fbe | 1622 | void manager_flush_caches(Manager *m, int log_level) { |
ba35662f LP |
1623 | assert(m); |
1624 | ||
1625 | LIST_FOREACH(scopes, scope, m->dns_scopes) | |
1626 | dns_cache_flush(&scope->cache); | |
aac57b35 | 1627 | |
90df0fbe | 1628 | log_full(log_level, "Flushed all caches."); |
ba35662f | 1629 | } |
943ef07c | 1630 | |
59c0fd0e | 1631 | void manager_reset_server_features(Manager *m) { |
59c0fd0e LP |
1632 | Link *l; |
1633 | ||
1634 | dns_server_reset_features_all(m->dns_servers); | |
1635 | dns_server_reset_features_all(m->fallback_dns_servers); | |
1636 | ||
90e74a66 | 1637 | HASHMAP_FOREACH(l, m->links) |
59c0fd0e LP |
1638 | dns_server_reset_features_all(l->dns_servers); |
1639 | ||
1640 | log_info("Resetting learnt feature levels on all servers."); | |
1641 | } | |
1642 | ||
943ef07c LP |
1643 | void manager_cleanup_saved_user(Manager *m) { |
1644 | _cleanup_closedir_ DIR *d = NULL; | |
943ef07c LP |
1645 | |
1646 | assert(m); | |
1647 | ||
1648 | /* Clean up all saved per-link files in /run/systemd/resolve/netif/ that don't have a matching interface | |
1649 | * anymore. These files are created to persist settings pushed in by the user via the bus, so that resolved can | |
1650 | * be restarted without losing this data. */ | |
1651 | ||
1652 | d = opendir("/run/systemd/resolve/netif/"); | |
1653 | if (!d) { | |
1654 | if (errno == ENOENT) | |
1655 | return; | |
1656 | ||
1657 | log_warning_errno(errno, "Failed to open interface directory: %m"); | |
1658 | return; | |
1659 | } | |
1660 | ||
1661 | FOREACH_DIRENT_ALL(de, d, log_error_errno(errno, "Failed to read interface directory: %m")) { | |
1662 | _cleanup_free_ char *p = NULL; | |
1663 | int ifindex; | |
1664 | Link *l; | |
1665 | ||
1666 | if (!IN_SET(de->d_type, DT_UNKNOWN, DT_REG)) | |
1667 | continue; | |
1668 | ||
49bfc877 | 1669 | if (dot_or_dot_dot(de->d_name)) |
943ef07c LP |
1670 | continue; |
1671 | ||
597da51b ZJS |
1672 | ifindex = parse_ifindex(de->d_name); |
1673 | if (ifindex < 0) /* Probably some temporary file from a previous run. Delete it */ | |
943ef07c LP |
1674 | goto rm; |
1675 | ||
1676 | l = hashmap_get(m->links, INT_TO_PTR(ifindex)); | |
1677 | if (!l) /* link vanished */ | |
1678 | goto rm; | |
1679 | ||
1680 | if (l->is_managed) /* now managed by networkd, hence the bus settings are useless */ | |
1681 | goto rm; | |
1682 | ||
1683 | continue; | |
1684 | ||
1685 | rm: | |
b910cc72 | 1686 | p = path_join("/run/systemd/resolve/netif", de->d_name); |
943ef07c LP |
1687 | if (!p) { |
1688 | log_oom(); | |
1689 | return; | |
1690 | } | |
1691 | ||
1692 | (void) unlink(p); | |
1693 | } | |
1694 | } | |
e7c1b0e4 DR |
1695 | |
1696 | bool manager_next_dnssd_names(Manager *m) { | |
e7c1b0e4 DR |
1697 | DnssdService *s; |
1698 | bool tried = false; | |
1699 | int r; | |
1700 | ||
1701 | assert(m); | |
1702 | ||
90e74a66 | 1703 | HASHMAP_FOREACH(s, m->dnssd_services) { |
e7c1b0e4 DR |
1704 | _cleanup_free_ char * new_name = NULL; |
1705 | ||
1706 | if (!s->withdrawn) | |
1707 | continue; | |
1708 | ||
1709 | r = manager_next_random_name(s->name_template, &new_name); | |
1710 | if (r < 0) { | |
1711 | log_warning_errno(r, "Failed to get new name for service '%s': %m", s->name); | |
1712 | continue; | |
1713 | } | |
1714 | ||
1715 | free_and_replace(s->name_template, new_name); | |
1716 | ||
1717 | s->withdrawn = false; | |
1718 | ||
1719 | tried = true; | |
1720 | } | |
1721 | ||
1722 | if (tried) | |
1723 | manager_refresh_rrs(m); | |
1724 | ||
1725 | return tried; | |
1726 | } | |
49ef064c | 1727 | |
281df579 | 1728 | bool manager_server_is_stub(Manager *m, DnsServer *s) { |
49ef064c LP |
1729 | DnsStubListenerExtra *l; |
1730 | ||
1731 | assert(m); | |
281df579 | 1732 | assert(s); |
49ef064c LP |
1733 | |
1734 | /* Safety check: we generally already skip the main stub when parsing configuration. But let's be | |
1735 | * extra careful, and check here again */ | |
281df579 LP |
1736 | if (s->family == AF_INET && |
1737 | s->address.in.s_addr == htobe32(INADDR_DNS_STUB) && | |
1738 | dns_server_port(s) == 53) | |
49ef064c LP |
1739 | return true; |
1740 | ||
1741 | /* Main reason to call this is to check server data against the extra listeners, and filter things | |
1742 | * out. */ | |
1743 | ORDERED_SET_FOREACH(l, m->dns_extra_stub_listeners) | |
281df579 LP |
1744 | if (s->family == l->family && |
1745 | in_addr_equal(s->family, &s->address, &l->address) && | |
1746 | dns_server_port(s) == dns_stub_listener_extra_port(l)) | |
49ef064c LP |
1747 | return true; |
1748 | ||
1749 | return false; | |
1750 | } | |
eb170e75 LP |
1751 | |
1752 | int socket_disable_pmtud(int fd, int af) { | |
1753 | int r; | |
1754 | ||
1755 | assert(fd >= 0); | |
1756 | ||
1757 | if (af == AF_UNSPEC) { | |
5f64d2bf LP |
1758 | af = socket_get_family(fd); |
1759 | if (af < 0) | |
1760 | return af; | |
eb170e75 LP |
1761 | } |
1762 | ||
1763 | switch (af) { | |
1764 | ||
1765 | case AF_INET: { | |
1766 | /* Turn off path MTU discovery, let's rather fragment on the way than to open us up against | |
1767 | * PMTU forgery vulnerabilities. | |
1768 | * | |
1769 | * There appears to be no documentation about IP_PMTUDISC_OMIT, but it has the effect that | |
1770 | * the "Don't Fragment" bit in the IPv4 header is turned off, thus enforcing fragmentation if | |
1771 | * our datagram size exceeds the MTU of a router in the path, and turning off path MTU | |
1772 | * discovery. | |
1773 | * | |
1774 | * This helps mitigating the PMTUD vulnerability described here: | |
1775 | * | |
1776 | * https://blog.apnic.net/2019/07/12/its-time-to-consider-avoiding-ip-fragmentation-in-the-dns/ | |
1777 | * | |
1778 | * Similar logic is in place in most DNS servers. | |
1779 | * | |
1780 | * There are multiple conflicting goals: we want to allow the largest datagrams possible (for | |
1781 | * efficiency reasons), but not have fragmentation (for security reasons), nor use PMTUD (for | |
1782 | * security reasons, too). Our strategy to deal with this is: use large packets, turn off | |
1783 | * PMTUD, but watch fragmentation taking place, and then size our packets to the max of the | |
1784 | * fragments seen — and if we need larger packets always go to TCP. | |
1785 | */ | |
1786 | ||
1787 | r = setsockopt_int(fd, IPPROTO_IP, IP_MTU_DISCOVER, IP_PMTUDISC_OMIT); | |
1788 | if (r < 0) | |
1789 | return r; | |
1790 | ||
1791 | return 0; | |
1792 | } | |
1793 | ||
1794 | case AF_INET6: { | |
1795 | /* On IPv6 fragmentation only is done by the sender — never by routers on the path. PMTUD is | |
1796 | * mandatory. If we want to turn off PMTUD, the only way is by sending with minimal MTU only, | |
1797 | * so that we apply maximum fragmentation locally already, and thus PMTUD doesn't happen | |
1798 | * because there's nothing that could be fragmented further anymore. */ | |
1799 | ||
1800 | r = setsockopt_int(fd, IPPROTO_IPV6, IPV6_MTU, IPV6_MIN_MTU); | |
1801 | if (r < 0) | |
1802 | return r; | |
1803 | ||
1804 | return 0; | |
1805 | } | |
1806 | ||
1807 | default: | |
1808 | return -EAFNOSUPPORT; | |
1809 | } | |
1810 | } | |
bc837621 KV |
1811 | |
1812 | int dns_manager_dump_statistics_json(Manager *m, JsonVariant **ret) { | |
1813 | uint64_t size = 0, hit = 0, miss = 0; | |
1814 | ||
1815 | assert(m); | |
1816 | assert(ret); | |
1817 | ||
1818 | LIST_FOREACH(scopes, s, m->dns_scopes) { | |
1819 | size += dns_cache_size(&s->cache); | |
1820 | hit += s->cache.n_hit; | |
1821 | miss += s->cache.n_miss; | |
1822 | } | |
1823 | ||
1824 | return json_build(ret, | |
1825 | JSON_BUILD_OBJECT( | |
1826 | JSON_BUILD_PAIR("transactions", JSON_BUILD_OBJECT( | |
1827 | JSON_BUILD_PAIR_UNSIGNED("currentTransactions", hashmap_size(m->dns_transactions)), | |
1828 | JSON_BUILD_PAIR_UNSIGNED("totalTransactions", m->n_transactions_total), | |
1829 | JSON_BUILD_PAIR_UNSIGNED("totalTimeouts", m->n_timeouts_total), | |
1830 | JSON_BUILD_PAIR_UNSIGNED("totalTimeoutsServedStale", m->n_timeouts_served_stale_total), | |
1831 | JSON_BUILD_PAIR_UNSIGNED("totalFailedResponses", m->n_failure_responses_total), | |
1832 | JSON_BUILD_PAIR_UNSIGNED("totalFailedResponsesServedStale", m->n_failure_responses_served_stale_total) | |
1833 | )), | |
1834 | JSON_BUILD_PAIR("cache", JSON_BUILD_OBJECT( | |
1835 | JSON_BUILD_PAIR_UNSIGNED("size", size), | |
1836 | JSON_BUILD_PAIR_UNSIGNED("hits", hit), | |
1837 | JSON_BUILD_PAIR_UNSIGNED("misses", miss) | |
1838 | )), | |
1839 | JSON_BUILD_PAIR("dnssec", JSON_BUILD_OBJECT( | |
1840 | JSON_BUILD_PAIR_UNSIGNED("secure", m->n_dnssec_verdict[DNSSEC_SECURE]), | |
1841 | JSON_BUILD_PAIR_UNSIGNED("insecure", m->n_dnssec_verdict[DNSSEC_INSECURE]), | |
1842 | JSON_BUILD_PAIR_UNSIGNED("bogus", m->n_dnssec_verdict[DNSSEC_BOGUS]), | |
1843 | JSON_BUILD_PAIR_UNSIGNED("indeterminate", m->n_dnssec_verdict[DNSSEC_INDETERMINATE]) | |
1844 | )))); | |
1845 | } | |
1846 | ||
a67e5c6e | 1847 | void dns_manager_reset_statistics(Manager *m) { |
bc837621 KV |
1848 | |
1849 | assert(m); | |
1850 | ||
1851 | LIST_FOREACH(scopes, s, m->dns_scopes) | |
1852 | s->cache.n_hit = s->cache.n_miss = 0; | |
1853 | ||
1854 | m->n_transactions_total = 0; | |
1855 | m->n_timeouts_total = 0; | |
1856 | m->n_timeouts_served_stale_total = 0; | |
1857 | m->n_failure_responses_total = 0; | |
1858 | m->n_failure_responses_served_stale_total = 0; | |
1859 | zero(m->n_dnssec_verdict); | |
1860 | } |