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