| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <fcntl.h> |
| 4 |
#include <linux/ipv6.h> |
| 5 |
#include <netinet/in.h> |
| 6 |
#include <poll.h> |
| 7 |
#include <unistd.h> |
| 8 |
|
| 9 |
#include "sd-bus.h" |
| 10 |
#include "sd-netlink.h" |
| 11 |
#include "sd-network.h" |
| 12 |
|
| 13 |
#include "af-list.h" |
| 14 |
#include "alloc-util.h" |
| 15 |
#include "daemon-util.h" |
| 16 |
#include "dirent-util.h" |
| 17 |
#include "dns-answer.h" |
| 18 |
#include "dns-domain.h" |
| 19 |
#include "dns-packet.h" |
| 20 |
#include "dns-question.h" |
| 21 |
#include "dns-rr.h" |
| 22 |
#include "errno-util.h" |
| 23 |
#include "event-util.h" |
| 24 |
#include "fd-util.h" |
| 25 |
#include "hostname-setup.h" |
| 26 |
#include "hostname-util.h" |
| 27 |
#include "io-util.h" |
| 28 |
#include "iovec-util.h" |
| 29 |
#include "json-util.h" |
| 30 |
#include "memstream-util.h" |
| 31 |
#include "missing-network.h" |
| 32 |
#include "ordered-set.h" |
| 33 |
#include "parse-util.h" |
| 34 |
#include "random-util.h" |
| 35 |
#include "resolved-bus.h" |
| 36 |
#include "resolved-conf.h" |
| 37 |
#include "resolved-dns-delegate.h" |
| 38 |
#include "resolved-dns-query.h" |
| 39 |
#include "resolved-dns-scope.h" |
| 40 |
#include "resolved-dns-search-domain.h" |
| 41 |
#include "resolved-dns-server.h" |
| 42 |
#include "resolved-dns-stub.h" |
| 43 |
#include "resolved-dns-transaction.h" |
| 44 |
#include "resolved-dnssd.h" |
| 45 |
#include "resolved-etc-hosts.h" |
| 46 |
#include "resolved-link.h" |
| 47 |
#include "resolved-llmnr.h" |
| 48 |
#include "resolved-manager.h" |
| 49 |
#include "resolved-mdns.h" |
| 50 |
#include "resolved-resolv-conf.h" |
| 51 |
#include "resolved-socket-graveyard.h" |
| 52 |
#include "resolved-static-records.h" |
| 53 |
#include "resolved-util.h" |
| 54 |
#include "resolved-varlink.h" |
| 55 |
#include "set.h" |
| 56 |
#include "socket-util.h" |
| 57 |
#include "string-util.h" |
| 58 |
#include "time-util.h" |
| 59 |
#include "varlink-util.h" |
| 60 |
|
| 61 |
#define SEND_TIMEOUT_USEC (200 * USEC_PER_MSEC) |
| 62 |
|
| 63 |
static int manager_process_link(sd_netlink *rtnl, sd_netlink_message *mm, void *userdata) { |
| 64 |
Manager *m = ASSERT_PTR(userdata); |
| 65 |
uint16_t type; |
| 66 |
Link *l; |
| 67 |
int ifindex, r; |
| 68 |
|
| 69 |
assert(rtnl); |
| 70 |
assert(mm); |
| 71 |
|
| 72 |
r = sd_netlink_message_get_type(mm, &type); |
| 73 |
if (r < 0) |
| 74 |
goto fail; |
| 75 |
|
| 76 |
r = sd_rtnl_message_link_get_ifindex(mm, &ifindex); |
| 77 |
if (r < 0) |
| 78 |
goto fail; |
| 79 |
|
| 80 |
l = hashmap_get(m->links, INT_TO_PTR(ifindex)); |
| 81 |
|
| 82 |
switch (type) { |
| 83 |
|
| 84 |
case RTM_NEWLINK:{ |
| 85 |
bool is_new = !l; |
| 86 |
|
| 87 |
if (!l) { |
| 88 |
r = link_new(m, &l, ifindex); |
| 89 |
if (r < 0) |
| 90 |
goto fail; |
| 91 |
} |
| 92 |
|
| 93 |
r = link_process_rtnl(l, mm); |
| 94 |
if (r < 0) |
| 95 |
goto fail; |
| 96 |
|
| 97 |
r = link_update(l); |
| 98 |
if (r < 0) |
| 99 |
goto fail; |
| 100 |
|
| 101 |
if (is_new) |
| 102 |
log_debug("Found new link %i/%s", ifindex, l->ifname); |
| 103 |
|
| 104 |
break; |
| 105 |
} |
| 106 |
|
| 107 |
case RTM_DELLINK: |
| 108 |
if (l) { |
| 109 |
log_debug("Removing link %i/%s", l->ifindex, l->ifname); |
| 110 |
link_remove_user(l); |
| 111 |
link_free(l); |
| 112 |
|
| 113 |
/* Make sure DNS servers are dropped from written resolv.conf if their link goes away */ |
| 114 |
manager_write_resolv_conf(m); |
| 115 |
} |
| 116 |
|
| 117 |
break; |
| 118 |
} |
| 119 |
|
| 120 |
/* Now check all the links, and if mDNS/llmr are disabled everywhere, stop them globally too. */ |
| 121 |
manager_llmnr_maybe_stop(m); |
| 122 |
manager_mdns_maybe_stop(m); |
| 123 |
|
| 124 |
/* The accessible flag on link DNS servers will have been reset by |
| 125 |
* link_update(). Just reset the global DNS servers. */ |
| 126 |
(void) manager_send_dns_configuration_changed(m, NULL, /* reset= */ true); |
| 127 |
|
| 128 |
return 0; |
| 129 |
|
| 130 |
fail: |
| 131 |
log_warning_errno(r, "Failed to process RTNL link message: %m"); |
| 132 |
return 0; |
| 133 |
} |
| 134 |
|
| 135 |
static int manager_process_address(sd_netlink *rtnl, sd_netlink_message *mm, void *userdata) { |
| 136 |
Manager *m = ASSERT_PTR(userdata); |
| 137 |
union in_addr_union address, broadcast = {}; |
| 138 |
uint16_t type; |
| 139 |
int r, ifindex, family; |
| 140 |
LinkAddress *a; |
| 141 |
Link *l; |
| 142 |
|
| 143 |
assert(rtnl); |
| 144 |
assert(mm); |
| 145 |
|
| 146 |
r = sd_netlink_message_get_type(mm, &type); |
| 147 |
if (r < 0) |
| 148 |
goto fail; |
| 149 |
|
| 150 |
r = sd_rtnl_message_addr_get_ifindex(mm, &ifindex); |
| 151 |
if (r < 0) |
| 152 |
goto fail; |
| 153 |
|
| 154 |
l = hashmap_get(m->links, INT_TO_PTR(ifindex)); |
| 155 |
if (!l) |
| 156 |
return 0; |
| 157 |
|
| 158 |
r = sd_rtnl_message_addr_get_family(mm, &family); |
| 159 |
if (r < 0) |
| 160 |
goto fail; |
| 161 |
|
| 162 |
switch (family) { |
| 163 |
|
| 164 |
case AF_INET: |
| 165 |
sd_netlink_message_read_in_addr(mm, IFA_BROADCAST, &broadcast.in); |
| 166 |
r = sd_netlink_message_read_in_addr(mm, IFA_LOCAL, &address.in); |
| 167 |
if (r < 0) { |
| 168 |
r = sd_netlink_message_read_in_addr(mm, IFA_ADDRESS, &address.in); |
| 169 |
if (r < 0) |
| 170 |
goto fail; |
| 171 |
} |
| 172 |
|
| 173 |
break; |
| 174 |
|
| 175 |
case AF_INET6: |
| 176 |
r = sd_netlink_message_read_in6_addr(mm, IFA_LOCAL, &address.in6); |
| 177 |
if (r < 0) { |
| 178 |
r = sd_netlink_message_read_in6_addr(mm, IFA_ADDRESS, &address.in6); |
| 179 |
if (r < 0) |
| 180 |
goto fail; |
| 181 |
} |
| 182 |
|
| 183 |
break; |
| 184 |
|
| 185 |
default: |
| 186 |
return 0; |
| 187 |
} |
| 188 |
|
| 189 |
a = link_find_address(l, family, &address); |
| 190 |
|
| 191 |
switch (type) { |
| 192 |
|
| 193 |
case RTM_NEWADDR: |
| 194 |
|
| 195 |
if (!a) { |
| 196 |
r = link_address_new(l, &a, family, &address, &broadcast); |
| 197 |
if (r < 0) |
| 198 |
return r; |
| 199 |
} |
| 200 |
|
| 201 |
r = link_address_update_rtnl(a, mm); |
| 202 |
if (r < 0) |
| 203 |
return r; |
| 204 |
|
| 205 |
break; |
| 206 |
|
| 207 |
case RTM_DELADDR: |
| 208 |
link_address_free(a); |
| 209 |
break; |
| 210 |
} |
| 211 |
|
| 212 |
(void) manager_send_dns_configuration_changed(m, l, /* reset= */ true); |
| 213 |
|
| 214 |
return 0; |
| 215 |
|
| 216 |
fail: |
| 217 |
log_warning_errno(r, "Failed to process RTNL address message: %m"); |
| 218 |
return 0; |
| 219 |
} |
| 220 |
|
| 221 |
static int manager_process_route(sd_netlink *rtnl, sd_netlink_message *mm, void *userdata) { |
| 222 |
Manager *m = ASSERT_PTR(userdata); |
| 223 |
Link *l = NULL; |
| 224 |
uint16_t type; |
| 225 |
uint32_t ifindex = 0; |
| 226 |
int r; |
| 227 |
|
| 228 |
assert(rtnl); |
| 229 |
assert(mm); |
| 230 |
|
| 231 |
r = sd_netlink_message_get_type(mm, &type); |
| 232 |
if (r < 0) { |
| 233 |
log_warning_errno(r, "Failed to get rtnl message type, ignoring: %m"); |
| 234 |
return 0; |
| 235 |
} |
| 236 |
|
| 237 |
if (!IN_SET(type, RTM_NEWROUTE, RTM_DELROUTE)) { |
| 238 |
log_warning("Unexpected message type %u when processing route, ignoring.", type); |
| 239 |
return 0; |
| 240 |
} |
| 241 |
|
| 242 |
r = sd_netlink_message_read_u32(mm, RTA_OIF, &ifindex); |
| 243 |
if (r < 0) |
| 244 |
log_full_errno(r == -ENODATA ? LOG_DEBUG : LOG_WARNING, r, "Failed to get route ifindex, ignoring: %m"); |
| 245 |
else |
| 246 |
l = hashmap_get(m->links, INT_TO_PTR(ifindex)); |
| 247 |
|
| 248 |
(void) manager_send_dns_configuration_changed(m, l, /* reset= */ true); |
| 249 |
|
| 250 |
return 0; |
| 251 |
} |
| 252 |
|
| 253 |
static int manager_rtnl_listen(Manager *m) { |
| 254 |
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL; |
| 255 |
int r; |
| 256 |
|
| 257 |
assert(m); |
| 258 |
|
| 259 |
/* First, subscribe to interfaces coming and going */ |
| 260 |
r = sd_netlink_open(&m->rtnl); |
| 261 |
if (r < 0) |
| 262 |
return r; |
| 263 |
|
| 264 |
r = sd_netlink_attach_event(m->rtnl, m->event, SD_EVENT_PRIORITY_IMPORTANT); |
| 265 |
if (r < 0) |
| 266 |
return r; |
| 267 |
|
| 268 |
r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWLINK, manager_process_link, NULL, m, "resolve-NEWLINK"); |
| 269 |
if (r < 0) |
| 270 |
return r; |
| 271 |
|
| 272 |
r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELLINK, manager_process_link, NULL, m, "resolve-DELLINK"); |
| 273 |
if (r < 0) |
| 274 |
return r; |
| 275 |
|
| 276 |
r = sd_netlink_add_match(m->rtnl, NULL, RTM_NEWADDR, manager_process_address, NULL, m, "resolve-NEWADDR"); |
| 277 |
if (r < 0) |
| 278 |
return r; |
| 279 |
|
| 280 |
r = sd_netlink_add_match(m->rtnl, NULL, RTM_DELADDR, manager_process_address, NULL, m, "resolve-DELADDR"); |
| 281 |
if (r < 0) |
| 282 |
return r; |
| 283 |
|
| 284 |
/* Then, enumerate all links */ |
| 285 |
r = sd_rtnl_message_new_link(m->rtnl, &req, RTM_GETLINK, 0); |
| 286 |
if (r < 0) |
| 287 |
return r; |
| 288 |
|
| 289 |
r = sd_netlink_message_set_request_dump(req, true); |
| 290 |
if (r < 0) |
| 291 |
return r; |
| 292 |
|
| 293 |
r = sd_netlink_call(m->rtnl, req, 0, &reply); |
| 294 |
if (r < 0) |
| 295 |
return r; |
| 296 |
|
| 297 |
for (sd_netlink_message *i = reply; i; i = sd_netlink_message_next(i)) { |
| 298 |
r = manager_process_link(m->rtnl, i, m); |
| 299 |
if (r < 0) |
| 300 |
return r; |
| 301 |
} |
| 302 |
|
| 303 |
req = sd_netlink_message_unref(req); |
| 304 |
reply = sd_netlink_message_unref(reply); |
| 305 |
|
| 306 |
/* Finally, enumerate all addresses, too */ |
| 307 |
r = sd_rtnl_message_new_addr(m->rtnl, &req, RTM_GETADDR, 0, AF_UNSPEC); |
| 308 |
if (r < 0) |
| 309 |
return r; |
| 310 |
|
| 311 |
r = sd_netlink_message_set_request_dump(req, true); |
| 312 |
if (r < 0) |
| 313 |
return r; |
| 314 |
|
| 315 |
r = sd_netlink_call(m->rtnl, req, 0, &reply); |
| 316 |
if (r < 0) |
| 317 |
return r; |
| 318 |
|
| 319 |
for (sd_netlink_message *i = reply; i; i = sd_netlink_message_next(i)) { |
| 320 |
r = manager_process_address(m->rtnl, i, m); |
| 321 |
if (r < 0) |
| 322 |
return r; |
| 323 |
} |
| 324 |
|
| 325 |
return r; |
| 326 |
} |
| 327 |
|
| 328 |
static int on_network_event(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
| 329 |
Manager *m = ASSERT_PTR(userdata); |
| 330 |
Link *l; |
| 331 |
int r; |
| 332 |
|
| 333 |
sd_network_monitor_flush(m->network_monitor); |
| 334 |
|
| 335 |
HASHMAP_FOREACH(l, m->links) { |
| 336 |
r = link_update(l); |
| 337 |
if (r < 0) |
| 338 |
log_warning_errno(r, "Failed to update monitor information for %i: %m", l->ifindex); |
| 339 |
} |
| 340 |
|
| 341 |
(void) manager_write_resolv_conf(m); |
| 342 |
(void) manager_send_changed(m, "DNS"); |
| 343 |
(void) manager_send_dns_configuration_changed(m, NULL, /* reset= */ true); |
| 344 |
|
| 345 |
/* Now check all the links, and if mDNS/llmr are disabled everywhere, stop them globally too. */ |
| 346 |
manager_llmnr_maybe_stop(m); |
| 347 |
manager_mdns_maybe_stop(m); |
| 348 |
return 0; |
| 349 |
} |
| 350 |
|
| 351 |
static int manager_network_monitor_listen(Manager *m) { |
| 352 |
int r, fd, events; |
| 353 |
|
| 354 |
assert(m); |
| 355 |
|
| 356 |
r = sd_network_monitor_new(&m->network_monitor, NULL); |
| 357 |
if (r < 0) |
| 358 |
return r; |
| 359 |
|
| 360 |
fd = sd_network_monitor_get_fd(m->network_monitor); |
| 361 |
if (fd < 0) |
| 362 |
return fd; |
| 363 |
|
| 364 |
events = sd_network_monitor_get_events(m->network_monitor); |
| 365 |
if (events < 0) |
| 366 |
return events; |
| 367 |
|
| 368 |
r = sd_event_add_io(m->event, &m->network_event_source, fd, events, &on_network_event, m); |
| 369 |
if (r < 0) |
| 370 |
return r; |
| 371 |
|
| 372 |
r = sd_event_source_set_priority(m->network_event_source, SD_EVENT_PRIORITY_IMPORTANT-5); |
| 373 |
if (r < 0) |
| 374 |
return r; |
| 375 |
|
| 376 |
(void) sd_event_source_set_description(m->network_event_source, "network-monitor"); |
| 377 |
|
| 378 |
return 0; |
| 379 |
} |
| 380 |
|
| 381 |
static int manager_clock_change_listen(Manager *m); |
| 382 |
|
| 383 |
static int on_clock_change(sd_event_source *source, int fd, uint32_t revents, void *userdata) { |
| 384 |
Manager *m = ASSERT_PTR(userdata); |
| 385 |
|
| 386 |
/* The clock has changed, let's flush all caches. Why that? That's because DNSSEC validation takes |
| 387 |
* the system clock into consideration, and if the clock changes the old validations might have been |
| 388 |
* wrong. Let's redo all validation with the new, correct time. |
| 389 |
* |
| 390 |
* (Also, this is triggered after system suspend, which is also a good reason to drop caches, since |
| 391 |
* we might be connected to a different network now without this being visible in a dropped link |
| 392 |
* carrier or so.) */ |
| 393 |
|
| 394 |
log_info("Clock change detected. Flushing caches."); |
| 395 |
manager_flush_caches(m, LOG_DEBUG /* downgrade the functions own log message, since we already logged here at LOG_INFO level */); |
| 396 |
|
| 397 |
/* The clock change timerfd is unusable after it triggered once, create a new one. */ |
| 398 |
return manager_clock_change_listen(m); |
| 399 |
} |
| 400 |
|
| 401 |
static int manager_clock_change_listen(Manager *m) { |
| 402 |
int r; |
| 403 |
|
| 404 |
assert(m); |
| 405 |
|
| 406 |
m->clock_change_event_source = sd_event_source_disable_unref(m->clock_change_event_source); |
| 407 |
|
| 408 |
r = event_add_time_change(m->event, &m->clock_change_event_source, on_clock_change, m); |
| 409 |
if (r < 0) |
| 410 |
return log_error_errno(r, "Failed to create clock change event source: %m"); |
| 411 |
|
| 412 |
return 0; |
| 413 |
} |
| 414 |
|
| 415 |
static int determine_hostnames(char **full_hostname, char **llmnr_hostname, char **mdns_hostname) { |
| 416 |
_cleanup_free_ char *h = NULL, *n = NULL; |
| 417 |
int r; |
| 418 |
|
| 419 |
assert(full_hostname); |
| 420 |
assert(llmnr_hostname); |
| 421 |
assert(mdns_hostname); |
| 422 |
|
| 423 |
r = resolve_system_hostname(&h, &n); |
| 424 |
if (r < 0) |
| 425 |
return r; |
| 426 |
|
| 427 |
r = dns_name_concat(n, "local", 0, mdns_hostname); |
| 428 |
if (r < 0) |
| 429 |
return log_error_errno(r, "Failed to determine mDNS hostname: %m"); |
| 430 |
|
| 431 |
*llmnr_hostname = TAKE_PTR(n); |
| 432 |
*full_hostname = TAKE_PTR(h); |
| 433 |
|
| 434 |
return 0; |
| 435 |
} |
| 436 |
|
| 437 |
static char* fallback_hostname(void) { |
| 438 |
|
| 439 |
/* Determine the fall back hostname. For exposing this system to the outside world, we cannot have it |
| 440 |
* to be "localhost" even if that's the default hostname. In this case, let's revert to "linux" |
| 441 |
* instead. */ |
| 442 |
|
| 443 |
_cleanup_free_ char *n = get_default_hostname(); |
| 444 |
if (!n) |
| 445 |
return NULL; |
| 446 |
|
| 447 |
if (is_localhost(n)) |
| 448 |
return strdup("linux"); |
| 449 |
|
| 450 |
return TAKE_PTR(n); |
| 451 |
} |
| 452 |
|
| 453 |
static int make_fallback_hostnames(char **full_hostname, char **llmnr_hostname, char **mdns_hostname) { |
| 454 |
_cleanup_free_ char *h = NULL, *n = NULL, *m = NULL; |
| 455 |
char label[DNS_LABEL_MAX+1]; |
| 456 |
const char *p; |
| 457 |
int r; |
| 458 |
|
| 459 |
assert(full_hostname); |
| 460 |
assert(llmnr_hostname); |
| 461 |
assert(mdns_hostname); |
| 462 |
|
| 463 |
p = h = fallback_hostname(); |
| 464 |
if (!h) |
| 465 |
return log_oom(); |
| 466 |
|
| 467 |
r = dns_label_unescape(&p, label, sizeof label, 0); |
| 468 |
if (r < 0) |
| 469 |
return log_error_errno(r, "Failed to unescape fallback hostname: %m"); |
| 470 |
|
| 471 |
assert(r > 0); /* The fallback hostname must have at least one label */ |
| 472 |
|
| 473 |
r = dns_label_escape_new(label, r, &n); |
| 474 |
if (r < 0) |
| 475 |
return log_error_errno(r, "Failed to escape fallback hostname: %m"); |
| 476 |
|
| 477 |
r = dns_name_concat(n, "local", 0, &m); |
| 478 |
if (r < 0) |
| 479 |
return log_error_errno(r, "Failed to concatenate mDNS hostname: %m"); |
| 480 |
|
| 481 |
*llmnr_hostname = TAKE_PTR(n); |
| 482 |
*mdns_hostname = TAKE_PTR(m); |
| 483 |
*full_hostname = TAKE_PTR(h); |
| 484 |
|
| 485 |
return 0; |
| 486 |
} |
| 487 |
|
| 488 |
static int on_hostname_change(sd_event_source *es, int fd, uint32_t revents, void *userdata) { |
| 489 |
_cleanup_free_ char *full_hostname = NULL, *llmnr_hostname = NULL, *mdns_hostname = NULL; |
| 490 |
Manager *m = ASSERT_PTR(userdata); |
| 491 |
bool llmnr_hostname_changed; |
| 492 |
int r; |
| 493 |
|
| 494 |
r = determine_hostnames(&full_hostname, &llmnr_hostname, &mdns_hostname); |
| 495 |
if (r < 0) { |
| 496 |
log_warning_errno(r, "Failed to determine the local hostname and LLMNR/mDNS names, ignoring: %m"); |
| 497 |
return 0; /* ignore invalid hostnames */ |
| 498 |
} |
| 499 |
|
| 500 |
llmnr_hostname_changed = !streq(llmnr_hostname, m->llmnr_hostname); |
| 501 |
if (streq(full_hostname, m->full_hostname) && |
| 502 |
!llmnr_hostname_changed && |
| 503 |
streq(mdns_hostname, m->mdns_hostname)) |
| 504 |
return 0; |
| 505 |
|
| 506 |
log_info("System hostname changed to '%s'.", full_hostname); |
| 507 |
|
| 508 |
free_and_replace(m->full_hostname, full_hostname); |
| 509 |
free_and_replace(m->llmnr_hostname, llmnr_hostname); |
| 510 |
free_and_replace(m->mdns_hostname, mdns_hostname); |
| 511 |
|
| 512 |
manager_refresh_rrs(m); |
| 513 |
(void) manager_send_changed(m, "LLMNRHostname"); |
| 514 |
|
| 515 |
return 0; |
| 516 |
} |
| 517 |
|
| 518 |
static int manager_watch_hostname(Manager *m) { |
| 519 |
int r; |
| 520 |
|
| 521 |
assert(m); |
| 522 |
|
| 523 |
m->hostname_fd = open("/proc/sys/kernel/hostname", |
| 524 |
O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY); |
| 525 |
if (m->hostname_fd < 0) { |
| 526 |
log_warning_errno(errno, "Failed to watch hostname: %m"); |
| 527 |
return 0; |
| 528 |
} |
| 529 |
|
| 530 |
r = sd_event_add_io(m->event, &m->hostname_event_source, m->hostname_fd, 0, on_hostname_change, m); |
| 531 |
if (r < 0) |
| 532 |
return log_error_errno(r, "Failed to add hostname event source: %m"); |
| 533 |
|
| 534 |
(void) sd_event_source_set_description(m->hostname_event_source, "hostname"); |
| 535 |
|
| 536 |
r = determine_hostnames(&m->full_hostname, &m->llmnr_hostname, &m->mdns_hostname); |
| 537 |
if (r < 0) { |
| 538 |
_cleanup_free_ char *d = NULL; |
| 539 |
|
| 540 |
d = fallback_hostname(); |
| 541 |
if (!d) |
| 542 |
return log_oom(); |
| 543 |
|
| 544 |
log_info("Defaulting to hostname '%s'.", d); |
| 545 |
|
| 546 |
r = make_fallback_hostnames(&m->full_hostname, &m->llmnr_hostname, &m->mdns_hostname); |
| 547 |
if (r < 0) |
| 548 |
return r; |
| 549 |
} else |
| 550 |
log_info("Using system hostname '%s'.", m->full_hostname); |
| 551 |
|
| 552 |
return 0; |
| 553 |
} |
| 554 |
|
| 555 |
static int manager_sigusr1(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { |
| 556 |
_cleanup_(memstream_done) MemStream ms = {}; |
| 557 |
Manager *m = ASSERT_PTR(userdata); |
| 558 |
Link *l; |
| 559 |
FILE *f; |
| 560 |
|
| 561 |
assert(s); |
| 562 |
assert(si); |
| 563 |
|
| 564 |
f = memstream_init(&ms); |
| 565 |
if (!f) |
| 566 |
return log_oom(); |
| 567 |
|
| 568 |
LIST_FOREACH(scopes, scope, m->dns_scopes) |
| 569 |
dns_scope_dump(scope, f); |
| 570 |
|
| 571 |
LIST_FOREACH(servers, server, m->dns_servers) |
| 572 |
dns_server_dump(server, f); |
| 573 |
LIST_FOREACH(servers, server, m->fallback_dns_servers) |
| 574 |
dns_server_dump(server, f); |
| 575 |
HASHMAP_FOREACH(l, m->links) |
| 576 |
LIST_FOREACH(servers, server, l->dns_servers) |
| 577 |
dns_server_dump(server, f); |
| 578 |
DnsDelegate *delegate; |
| 579 |
HASHMAP_FOREACH(delegate, m->delegates) |
| 580 |
LIST_FOREACH(servers, server, delegate->dns_servers) |
| 581 |
dns_server_dump(server, f); |
| 582 |
|
| 583 |
return memstream_dump(LOG_INFO, &ms); |
| 584 |
} |
| 585 |
|
| 586 |
static int manager_sigusr2(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { |
| 587 |
Manager *m = ASSERT_PTR(userdata); |
| 588 |
|
| 589 |
assert(s); |
| 590 |
assert(si); |
| 591 |
|
| 592 |
manager_flush_caches(m, LOG_INFO); |
| 593 |
|
| 594 |
return 0; |
| 595 |
} |
| 596 |
|
| 597 |
static int manager_sigrtmin1(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { |
| 598 |
Manager *m = ASSERT_PTR(userdata); |
| 599 |
|
| 600 |
assert(s); |
| 601 |
assert(si); |
| 602 |
|
| 603 |
manager_reset_server_features(m); |
| 604 |
return 0; |
| 605 |
} |
| 606 |
|
| 607 |
static int manager_memory_pressure(sd_event_source *s, void *userdata) { |
| 608 |
Manager *m = ASSERT_PTR(userdata); |
| 609 |
|
| 610 |
log_info("Under memory pressure, flushing caches."); |
| 611 |
|
| 612 |
manager_flush_caches(m, LOG_INFO); |
| 613 |
sd_event_trim_memory(); |
| 614 |
|
| 615 |
return 0; |
| 616 |
} |
| 617 |
|
| 618 |
static int manager_memory_pressure_listen(Manager *m) { |
| 619 |
int r; |
| 620 |
|
| 621 |
assert(m); |
| 622 |
|
| 623 |
r = sd_event_add_memory_pressure(m->event, NULL, manager_memory_pressure, m); |
| 624 |
if (r < 0) |
| 625 |
log_full_errno(ERRNO_IS_NOT_SUPPORTED(r) || ERRNO_IS_PRIVILEGE(r) || (r == -EHOSTDOWN )? LOG_DEBUG : LOG_NOTICE, r, |
| 626 |
"Failed to install memory pressure event source, ignoring: %m"); |
| 627 |
|
| 628 |
return 0; |
| 629 |
} |
| 630 |
|
| 631 |
static void manager_set_defaults(Manager *m) { |
| 632 |
assert(m); |
| 633 |
|
| 634 |
m->llmnr_support = DEFAULT_LLMNR_MODE; |
| 635 |
m->mdns_support = DEFAULT_MDNS_MODE; |
| 636 |
m->dnssec_mode = DEFAULT_DNSSEC_MODE; |
| 637 |
m->dns_over_tls_mode = DEFAULT_DNS_OVER_TLS_MODE; |
| 638 |
m->enable_cache = DNS_CACHE_MODE_YES; |
| 639 |
m->dns_stub_listener_mode = DNS_STUB_LISTENER_YES; |
| 640 |
m->read_etc_hosts = true; |
| 641 |
m->read_static_records = true; |
| 642 |
m->resolve_unicast_single_label = false; |
| 643 |
m->cache_from_localhost = false; |
| 644 |
for (DnsProtocol p = 0; p < _DNS_PROTOCOL_MAX; p++) |
| 645 |
m->cache_max[p] = DEFAULT_CACHE_MAX; |
| 646 |
m->stale_retention_usec = 0; |
| 647 |
m->refuse_record_types = set_free(m->refuse_record_types); |
| 648 |
m->resolv_conf_stat = (struct stat) {}; |
| 649 |
} |
| 650 |
|
| 651 |
static int manager_dispatch_reload_signal(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { |
| 652 |
Manager *m = ASSERT_PTR(userdata); |
| 653 |
Link *l; |
| 654 |
int r; |
| 655 |
|
| 656 |
(void) notify_reloading(); |
| 657 |
|
| 658 |
dns_server_unlink_on_reload(m->dns_servers); |
| 659 |
dns_server_unlink_on_reload(m->fallback_dns_servers); |
| 660 |
m->dns_extra_stub_listeners = ordered_set_free(m->dns_extra_stub_listeners); |
| 661 |
manager_dns_stub_stop(m); |
| 662 |
dnssd_registered_service_clear_on_reload(m->dnssd_registered_services); |
| 663 |
m->unicast_scope = dns_scope_free(m->unicast_scope); |
| 664 |
m->delegates = hashmap_free(m->delegates); |
| 665 |
dns_trust_anchor_flush(&m->trust_anchor); |
| 666 |
manager_etc_hosts_flush(m); |
| 667 |
manager_static_records_flush(m); |
| 668 |
|
| 669 |
m->etc_hosts_last = USEC_INFINITY; |
| 670 |
m->static_records_last = USEC_INFINITY; |
| 671 |
|
| 672 |
manager_set_defaults(m); |
| 673 |
|
| 674 |
r = dns_trust_anchor_load(&m->trust_anchor); |
| 675 |
if (r < 0) |
| 676 |
return sd_event_exit(sd_event_source_get_event(s), r); |
| 677 |
|
| 678 |
r = manager_parse_config_file(m); |
| 679 |
if (r < 0) |
| 680 |
log_warning_errno(r, "Failed to parse configuration file on reload, ignoring: %m"); |
| 681 |
else |
| 682 |
log_info("Config file reloaded."); |
| 683 |
|
| 684 |
(void) dnssd_load(m); |
| 685 |
(void) manager_load_delegates(m); |
| 686 |
|
| 687 |
/* The default scope configuration is influenced by the manager's configuration (modes, etc.), so |
| 688 |
* recreate it on reload. */ |
| 689 |
r = dns_scope_new(m, &m->unicast_scope, DNS_SCOPE_GLOBAL, /* link= */ NULL, /* delegate= */ NULL, DNS_PROTOCOL_DNS, AF_UNSPEC); |
| 690 |
if (r < 0) |
| 691 |
return sd_event_exit(sd_event_source_get_event(s), r); |
| 692 |
|
| 693 |
/* A link's unicast scope may also be influenced by the manager's configuration. I.e., DNSSEC= and DNSOverTLS= |
| 694 |
* from the manager will be used if not explicitly configured on the link. Free the scopes here so that |
| 695 |
* link_allocate_scopes() in on_network_event() re-creates them. */ |
| 696 |
HASHMAP_FOREACH(l, m->links) |
| 697 |
l->unicast_scope = dns_scope_free(l->unicast_scope); |
| 698 |
|
| 699 |
/* The configuration has changed, so reload the per-interface configuration too in order to take |
| 700 |
* into account any changes (e.g.: enable/disable DNSSEC). */ |
| 701 |
r = on_network_event(/* source= */ NULL, -EBADF, /* revents= */ 0, m); |
| 702 |
if (r < 0) |
| 703 |
log_warning_errno(r, "Failed to update network information on reload, ignoring: %m"); |
| 704 |
|
| 705 |
/* We have new configuration, which means potentially new servers, so close all connections and drop |
| 706 |
* all caches, so that we can start fresh. */ |
| 707 |
(void) dns_stream_disconnect_all(m); |
| 708 |
manager_flush_caches(m, LOG_INFO); |
| 709 |
manager_verify_all(m); |
| 710 |
|
| 711 |
r = manager_dns_stub_start(m); |
| 712 |
if (r < 0) |
| 713 |
return sd_event_exit(sd_event_source_get_event(s), r); |
| 714 |
|
| 715 |
(void) sd_notify(/* unset_environment= */ false, NOTIFY_READY_MESSAGE); |
| 716 |
return 0; |
| 717 |
} |
| 718 |
|
| 719 |
int manager_new(Manager **ret) { |
| 720 |
_cleanup_(manager_freep) Manager *m = NULL; |
| 721 |
int r; |
| 722 |
|
| 723 |
assert(ret); |
| 724 |
|
| 725 |
m = new(Manager, 1); |
| 726 |
if (!m) |
| 727 |
return -ENOMEM; |
| 728 |
|
| 729 |
*m = (Manager) { |
| 730 |
.llmnr_ipv4_udp_fd = -EBADF, |
| 731 |
.llmnr_ipv6_udp_fd = -EBADF, |
| 732 |
.llmnr_ipv4_tcp_fd = -EBADF, |
| 733 |
.llmnr_ipv6_tcp_fd = -EBADF, |
| 734 |
.mdns_ipv4_fd = -EBADF, |
| 735 |
.mdns_ipv6_fd = -EBADF, |
| 736 |
.hostname_fd = -EBADF, |
| 737 |
|
| 738 |
.read_resolv_conf = true, |
| 739 |
.need_builtin_fallbacks = true, |
| 740 |
.etc_hosts_last = USEC_INFINITY, |
| 741 |
.static_records_last = USEC_INFINITY, |
| 742 |
|
| 743 |
.sigrtmin18_info.memory_pressure_handler = manager_memory_pressure, |
| 744 |
.sigrtmin18_info.memory_pressure_userdata = m, |
| 745 |
}; |
| 746 |
|
| 747 |
manager_set_defaults(m); |
| 748 |
|
| 749 |
r = dns_trust_anchor_load(&m->trust_anchor); |
| 750 |
if (r < 0) |
| 751 |
return r; |
| 752 |
|
| 753 |
r = manager_parse_config_file(m); |
| 754 |
if (r < 0) |
| 755 |
log_warning_errno(r, "Failed to parse configuration file, ignoring: %m"); |
| 756 |
|
| 757 |
#if ENABLE_DNS_OVER_TLS |
| 758 |
r = dnstls_manager_init(m); |
| 759 |
if (r < 0) |
| 760 |
return r; |
| 761 |
#endif |
| 762 |
|
| 763 |
r = sd_event_default(&m->event); |
| 764 |
if (r < 0) |
| 765 |
return r; |
| 766 |
|
| 767 |
r = sd_event_set_signal_exit(m->event, true); |
| 768 |
if (r < 0) |
| 769 |
return r; |
| 770 |
|
| 771 |
(void) sd_event_set_watchdog(m->event, true); |
| 772 |
|
| 773 |
r = manager_watch_hostname(m); |
| 774 |
if (r < 0) |
| 775 |
return r; |
| 776 |
|
| 777 |
(void) dnssd_load(m); |
| 778 |
(void) manager_load_delegates(m); |
| 779 |
|
| 780 |
r = dns_scope_new(m, &m->unicast_scope, DNS_SCOPE_GLOBAL, /* link= */ NULL, /* delegate= */ NULL, DNS_PROTOCOL_DNS, AF_UNSPEC); |
| 781 |
if (r < 0) |
| 782 |
return r; |
| 783 |
|
| 784 |
r = manager_network_monitor_listen(m); |
| 785 |
if (r < 0) |
| 786 |
return r; |
| 787 |
|
| 788 |
r = manager_rtnl_listen(m); |
| 789 |
if (r < 0) |
| 790 |
return r; |
| 791 |
|
| 792 |
r = manager_clock_change_listen(m); |
| 793 |
if (r < 0) |
| 794 |
return r; |
| 795 |
|
| 796 |
r = manager_memory_pressure_listen(m); |
| 797 |
if (r < 0) |
| 798 |
return r; |
| 799 |
|
| 800 |
r = manager_connect_bus(m); |
| 801 |
if (r < 0) |
| 802 |
return r; |
| 803 |
|
| 804 |
r = sd_event_add_signal(m->event, /* ret= */ NULL, SIGHUP | SD_EVENT_SIGNAL_PROCMASK, manager_dispatch_reload_signal, m); |
| 805 |
if (r < 0) |
| 806 |
return log_debug_errno(r, "Failed to install SIGHUP handler: %m"); |
| 807 |
|
| 808 |
r = sd_event_add_signal(m->event, /* ret= */ NULL, SIGUSR1 | SD_EVENT_SIGNAL_PROCMASK, manager_sigusr1, m); |
| 809 |
if (r < 0) |
| 810 |
return log_debug_errno(r, "Failed to install SIGUSR1 handler: %m"); |
| 811 |
|
| 812 |
r = sd_event_add_signal(m->event, /* ret= */ NULL, SIGUSR2 | SD_EVENT_SIGNAL_PROCMASK, manager_sigusr2, m); |
| 813 |
if (r < 0) |
| 814 |
return log_debug_errno(r, "Failed to install SIGUSR2 handler: %m"); |
| 815 |
|
| 816 |
r = sd_event_add_signal(m->event, /* ret= */ NULL, (SIGRTMIN+1) | SD_EVENT_SIGNAL_PROCMASK, manager_sigrtmin1, m); |
| 817 |
if (r < 0) |
| 818 |
return log_debug_errno(r, "Failed to install SIGRTMIN+1 handler: %m"); |
| 819 |
|
| 820 |
r = sd_event_add_signal(m->event, /* ret= */ NULL, (SIGRTMIN+18) | SD_EVENT_SIGNAL_PROCMASK, sigrtmin18_handler, &m->sigrtmin18_info); |
| 821 |
if (r < 0) |
| 822 |
return log_debug_errno(r, "Failed to install SIGRTMIN+18 handler: %m"); |
| 823 |
|
| 824 |
manager_cleanup_saved_user(m); |
| 825 |
|
| 826 |
*ret = TAKE_PTR(m); |
| 827 |
|
| 828 |
return 0; |
| 829 |
} |
| 830 |
|
| 831 |
int manager_start(Manager *m) { |
| 832 |
int r; |
| 833 |
|
| 834 |
assert(m); |
| 835 |
|
| 836 |
r = manager_dns_stub_start(m); |
| 837 |
if (r < 0) |
| 838 |
return r; |
| 839 |
|
| 840 |
r = manager_varlink_init(m); |
| 841 |
if (r < 0) |
| 842 |
return r; |
| 843 |
|
| 844 |
return 0; |
| 845 |
} |
| 846 |
|
| 847 |
Manager* manager_free(Manager *m) { |
| 848 |
Link *l; |
| 849 |
DnssdRegisteredService *s; |
| 850 |
DnsServiceBrowser *sb; |
| 851 |
|
| 852 |
if (!m) |
| 853 |
return NULL; |
| 854 |
|
| 855 |
dns_server_unlink_all(m->dns_servers); |
| 856 |
dns_server_unlink_all(m->fallback_dns_servers); |
| 857 |
dns_search_domain_unlink_all(m->search_domains); |
| 858 |
|
| 859 |
while ((l = hashmap_first(m->links))) |
| 860 |
link_free(l); |
| 861 |
|
| 862 |
m->delegates = hashmap_free(m->delegates); |
| 863 |
|
| 864 |
while (m->dns_queries) |
| 865 |
dns_query_free(m->dns_queries); |
| 866 |
|
| 867 |
m->stub_queries_by_packet = hashmap_free(m->stub_queries_by_packet); |
| 868 |
m->unicast_scope = dns_scope_free(m->unicast_scope); |
| 869 |
|
| 870 |
/* At this point only orphaned streams should remain. All others should have been freed already by their |
| 871 |
* owners */ |
| 872 |
while (m->dns_streams) |
| 873 |
dns_stream_unref(m->dns_streams); |
| 874 |
|
| 875 |
#if ENABLE_DNS_OVER_TLS |
| 876 |
dnstls_manager_free(m); |
| 877 |
#endif |
| 878 |
|
| 879 |
set_free(m->refuse_record_types); |
| 880 |
hashmap_free(m->links); |
| 881 |
hashmap_free(m->dns_transactions); |
| 882 |
|
| 883 |
sd_event_source_unref(m->network_event_source); |
| 884 |
sd_network_monitor_unref(m->network_monitor); |
| 885 |
|
| 886 |
sd_netlink_slot_unref(m->netlink_new_route_slot); |
| 887 |
sd_netlink_slot_unref(m->netlink_del_route_slot); |
| 888 |
sd_netlink_unref(m->rtnl); |
| 889 |
sd_event_source_unref(m->rtnl_event_source); |
| 890 |
sd_event_source_unref(m->clock_change_event_source); |
| 891 |
|
| 892 |
sd_json_variant_unref(m->dns_configuration_json); |
| 893 |
|
| 894 |
manager_llmnr_stop(m); |
| 895 |
manager_mdns_stop(m); |
| 896 |
manager_dns_stub_stop(m); |
| 897 |
manager_varlink_done(m); |
| 898 |
|
| 899 |
set_free(m->varlink_query_results_subscription); |
| 900 |
set_free(m->varlink_dns_configuration_subscription); |
| 901 |
|
| 902 |
manager_socket_graveyard_clear(m); |
| 903 |
|
| 904 |
ordered_set_free(m->dns_extra_stub_listeners); |
| 905 |
|
| 906 |
hashmap_free(m->polkit_registry); |
| 907 |
|
| 908 |
sd_bus_flush_close_unref(m->bus); |
| 909 |
|
| 910 |
dns_resource_key_unref(m->llmnr_host_ipv4_key); |
| 911 |
dns_resource_key_unref(m->llmnr_host_ipv6_key); |
| 912 |
dns_resource_key_unref(m->mdns_host_ipv4_key); |
| 913 |
dns_resource_key_unref(m->mdns_host_ipv6_key); |
| 914 |
|
| 915 |
sd_event_source_unref(m->hostname_event_source); |
| 916 |
safe_close(m->hostname_fd); |
| 917 |
|
| 918 |
sd_event_unref(m->event); |
| 919 |
|
| 920 |
free(m->full_hostname); |
| 921 |
free(m->llmnr_hostname); |
| 922 |
free(m->mdns_hostname); |
| 923 |
|
| 924 |
while ((s = hashmap_first(m->dnssd_registered_services))) |
| 925 |
dnssd_registered_service_free(s); |
| 926 |
hashmap_free(m->dnssd_registered_services); |
| 927 |
|
| 928 |
dns_trust_anchor_flush(&m->trust_anchor); |
| 929 |
manager_etc_hosts_flush(m); |
| 930 |
manager_static_records_flush(m); |
| 931 |
|
| 932 |
while ((sb = hashmap_first(m->dns_service_browsers))) |
| 933 |
dns_service_browser_free(sb); |
| 934 |
hashmap_free(m->dns_service_browsers); |
| 935 |
|
| 936 |
hashmap_free(m->hooks); |
| 937 |
|
| 938 |
return mfree(m); |
| 939 |
} |
| 940 |
|
| 941 |
int manager_recv(Manager *m, int fd, DnsProtocol protocol, DnsPacket **ret) { |
| 942 |
_cleanup_(dns_packet_unrefp) DnsPacket *p = NULL; |
| 943 |
CMSG_BUFFER_TYPE(CMSG_SPACE(MAXSIZE(struct in_pktinfo, struct in6_pktinfo)) |
| 944 |
+ CMSG_SPACE(int) /* ttl/hoplimit */ |
| 945 |
+ EXTRA_CMSG_SPACE /* kernel appears to require extra buffer space */) control; |
| 946 |
union sockaddr_union sa; |
| 947 |
struct iovec iov; |
| 948 |
struct msghdr mh = { |
| 949 |
.msg_name = &sa.sa, |
| 950 |
.msg_namelen = sizeof(sa), |
| 951 |
.msg_iov = &iov, |
| 952 |
.msg_iovlen = 1, |
| 953 |
.msg_control = &control, |
| 954 |
.msg_controllen = sizeof(control), |
| 955 |
}; |
| 956 |
struct cmsghdr *cmsg; |
| 957 |
ssize_t ms, l; |
| 958 |
int r; |
| 959 |
|
| 960 |
assert(m); |
| 961 |
assert(fd >= 0); |
| 962 |
assert(ret); |
| 963 |
|
| 964 |
ms = next_datagram_size_fd(fd); |
| 965 |
if (ms < 0) |
| 966 |
return ms; |
| 967 |
|
| 968 |
r = dns_packet_new(&p, protocol, ms, DNS_PACKET_SIZE_MAX); |
| 969 |
if (r < 0) |
| 970 |
return r; |
| 971 |
|
| 972 |
iov = IOVEC_MAKE(DNS_PACKET_DATA(p), p->allocated); |
| 973 |
|
| 974 |
l = recvmsg_safe(fd, &mh, 0); |
| 975 |
if (ERRNO_IS_NEG_TRANSIENT(l)) |
| 976 |
return 0; |
| 977 |
if (l <= 0) |
| 978 |
return l; |
| 979 |
|
| 980 |
p->size = (size_t) l; |
| 981 |
|
| 982 |
p->family = sa.sa.sa_family; |
| 983 |
p->ipproto = IPPROTO_UDP; |
| 984 |
if (p->family == AF_INET) { |
| 985 |
p->sender.in = sa.in.sin_addr; |
| 986 |
p->sender_port = be16toh(sa.in.sin_port); |
| 987 |
} else if (p->family == AF_INET6) { |
| 988 |
p->sender.in6 = sa.in6.sin6_addr; |
| 989 |
p->sender_port = be16toh(sa.in6.sin6_port); |
| 990 |
p->ifindex = sa.in6.sin6_scope_id; |
| 991 |
} else |
| 992 |
return -EAFNOSUPPORT; |
| 993 |
|
| 994 |
p->timestamp = now(CLOCK_BOOTTIME); |
| 995 |
|
| 996 |
CMSG_FOREACH(cmsg, &mh) { |
| 997 |
|
| 998 |
if (cmsg->cmsg_level == IPPROTO_IPV6) { |
| 999 |
assert(p->family == AF_INET6); |
| 1000 |
|
| 1001 |
switch (cmsg->cmsg_type) { |
| 1002 |
|
| 1003 |
case IPV6_PKTINFO: { |
| 1004 |
struct in6_pktinfo *i = CMSG_TYPED_DATA(cmsg, struct in6_pktinfo); |
| 1005 |
|
| 1006 |
if (p->ifindex <= 0) |
| 1007 |
p->ifindex = i->ipi6_ifindex; |
| 1008 |
|
| 1009 |
p->destination.in6 = i->ipi6_addr; |
| 1010 |
break; |
| 1011 |
} |
| 1012 |
|
| 1013 |
case IPV6_HOPLIMIT: |
| 1014 |
p->ttl = *CMSG_TYPED_DATA(cmsg, int); |
| 1015 |
break; |
| 1016 |
|
| 1017 |
case IPV6_RECVFRAGSIZE: |
| 1018 |
p->fragsize = *CMSG_TYPED_DATA(cmsg, int); |
| 1019 |
break; |
| 1020 |
} |
| 1021 |
} else if (cmsg->cmsg_level == IPPROTO_IP) { |
| 1022 |
assert(p->family == AF_INET); |
| 1023 |
|
| 1024 |
switch (cmsg->cmsg_type) { |
| 1025 |
|
| 1026 |
case IP_PKTINFO: { |
| 1027 |
struct in_pktinfo *i = CMSG_TYPED_DATA(cmsg, struct in_pktinfo); |
| 1028 |
|
| 1029 |
if (p->ifindex <= 0) |
| 1030 |
p->ifindex = i->ipi_ifindex; |
| 1031 |
|
| 1032 |
p->destination.in = i->ipi_addr; |
| 1033 |
break; |
| 1034 |
} |
| 1035 |
|
| 1036 |
case IP_TTL: |
| 1037 |
p->ttl = *CMSG_TYPED_DATA(cmsg, int); |
| 1038 |
break; |
| 1039 |
|
| 1040 |
case IP_RECVFRAGSIZE: |
| 1041 |
p->fragsize = *CMSG_TYPED_DATA(cmsg, int); |
| 1042 |
break; |
| 1043 |
} |
| 1044 |
} |
| 1045 |
} |
| 1046 |
|
| 1047 |
/* The Linux kernel sets the interface index to the loopback |
| 1048 |
* device if the packet came from the local host since it |
| 1049 |
* avoids the routing table in such a case. Let's unset the |
| 1050 |
* interface index in such a case. */ |
| 1051 |
if (p->ifindex == LOOPBACK_IFINDEX) |
| 1052 |
p->ifindex = 0; |
| 1053 |
|
| 1054 |
if (protocol != DNS_PROTOCOL_DNS) { |
| 1055 |
/* If we don't know the interface index still, we look for the |
| 1056 |
* first local interface with a matching address. Yuck! */ |
| 1057 |
if (p->ifindex <= 0) |
| 1058 |
p->ifindex = manager_find_ifindex(m, p->family, &p->destination); |
| 1059 |
} |
| 1060 |
|
| 1061 |
log_debug("Received %s UDP packet of size %zu, ifindex=%i, ttl=%u, fragsize=%zu, sender=%s, destination=%s", |
| 1062 |
dns_protocol_to_string(protocol), p->size, p->ifindex, p->ttl, p->fragsize, |
| 1063 |
IN_ADDR_TO_STRING(p->family, &p->sender), |
| 1064 |
IN_ADDR_TO_STRING(p->family, &p->destination)); |
| 1065 |
|
| 1066 |
*ret = TAKE_PTR(p); |
| 1067 |
return 1; |
| 1068 |
} |
| 1069 |
|
| 1070 |
int sendmsg_loop(int fd, struct msghdr *mh, int flags) { |
| 1071 |
usec_t end; |
| 1072 |
int r; |
| 1073 |
|
| 1074 |
assert(fd >= 0); |
| 1075 |
assert(mh); |
| 1076 |
|
| 1077 |
end = usec_add(now(CLOCK_MONOTONIC), SEND_TIMEOUT_USEC); |
| 1078 |
|
| 1079 |
for (;;) { |
| 1080 |
if (sendmsg(fd, mh, flags) >= 0) |
| 1081 |
return 0; |
| 1082 |
if (errno == EINTR) |
| 1083 |
continue; |
| 1084 |
if (errno != EAGAIN) |
| 1085 |
return -errno; |
| 1086 |
|
| 1087 |
r = fd_wait_for_event(fd, POLLOUT, LESS_BY(end, now(CLOCK_MONOTONIC))); |
| 1088 |
if (ERRNO_IS_NEG_TRANSIENT(r)) |
| 1089 |
continue; |
| 1090 |
if (r < 0) |
| 1091 |
return r; |
| 1092 |
if (r == 0) |
| 1093 |
return -ETIMEDOUT; |
| 1094 |
} |
| 1095 |
} |
| 1096 |
|
| 1097 |
static int write_loop(int fd, void *message, size_t length) { |
| 1098 |
usec_t end; |
| 1099 |
int r; |
| 1100 |
|
| 1101 |
assert(fd >= 0); |
| 1102 |
assert(message); |
| 1103 |
|
| 1104 |
end = usec_add(now(CLOCK_MONOTONIC), SEND_TIMEOUT_USEC); |
| 1105 |
|
| 1106 |
for (;;) { |
| 1107 |
if (write(fd, message, length) >= 0) |
| 1108 |
return 0; |
| 1109 |
if (errno == EINTR) |
| 1110 |
continue; |
| 1111 |
if (errno != EAGAIN) |
| 1112 |
return -errno; |
| 1113 |
|
| 1114 |
r = fd_wait_for_event(fd, POLLOUT, LESS_BY(end, now(CLOCK_MONOTONIC))); |
| 1115 |
if (ERRNO_IS_NEG_TRANSIENT(r)) |
| 1116 |
continue; |
| 1117 |
if (r < 0) |
| 1118 |
return r; |
| 1119 |
if (r == 0) |
| 1120 |
return -ETIMEDOUT; |
| 1121 |
} |
| 1122 |
} |
| 1123 |
|
| 1124 |
int manager_write(Manager *m, int fd, DnsPacket *p) { |
| 1125 |
int r; |
| 1126 |
|
| 1127 |
log_debug("Sending %s%s packet with id %" PRIu16 " of size %zu.", |
| 1128 |
DNS_PACKET_TC(p) ? "truncated (!) " : "", |
| 1129 |
DNS_PACKET_QR(p) ? "response" : "query", |
| 1130 |
DNS_PACKET_ID(p), |
| 1131 |
p->size); |
| 1132 |
|
| 1133 |
r = write_loop(fd, DNS_PACKET_DATA(p), p->size); |
| 1134 |
if (r < 0) |
| 1135 |
return r; |
| 1136 |
|
| 1137 |
return 0; |
| 1138 |
} |
| 1139 |
|
| 1140 |
static int manager_ipv4_send( |
| 1141 |
Manager *m, |
| 1142 |
int fd, |
| 1143 |
int ifindex, |
| 1144 |
const struct in_addr *destination, |
| 1145 |
uint16_t port, |
| 1146 |
const struct in_addr *source, |
| 1147 |
DnsPacket *p) { |
| 1148 |
|
| 1149 |
CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct in_pktinfo))) control = {}; |
| 1150 |
union sockaddr_union sa; |
| 1151 |
struct iovec iov; |
| 1152 |
struct msghdr mh = { |
| 1153 |
.msg_iov = &iov, |
| 1154 |
.msg_iovlen = 1, |
| 1155 |
.msg_name = &sa.sa, |
| 1156 |
.msg_namelen = sizeof(sa.in), |
| 1157 |
}; |
| 1158 |
|
| 1159 |
assert(m); |
| 1160 |
assert(fd >= 0); |
| 1161 |
assert(destination); |
| 1162 |
assert(port > 0); |
| 1163 |
assert(p); |
| 1164 |
|
| 1165 |
iov = IOVEC_MAKE(DNS_PACKET_DATA(p), p->size); |
| 1166 |
|
| 1167 |
sa = (union sockaddr_union) { |
| 1168 |
.in.sin_family = AF_INET, |
| 1169 |
.in.sin_addr = *destination, |
| 1170 |
.in.sin_port = htobe16(port), |
| 1171 |
}; |
| 1172 |
|
| 1173 |
if (ifindex > 0) { |
| 1174 |
struct cmsghdr *cmsg; |
| 1175 |
struct in_pktinfo *pi; |
| 1176 |
|
| 1177 |
mh.msg_control = &control; |
| 1178 |
mh.msg_controllen = sizeof(control); |
| 1179 |
|
| 1180 |
cmsg = CMSG_FIRSTHDR(&mh); |
| 1181 |
cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo)); |
| 1182 |
cmsg->cmsg_level = IPPROTO_IP; |
| 1183 |
cmsg->cmsg_type = IP_PKTINFO; |
| 1184 |
|
| 1185 |
pi = CMSG_TYPED_DATA(cmsg, struct in_pktinfo); |
| 1186 |
pi->ipi_ifindex = ifindex; |
| 1187 |
|
| 1188 |
if (source) |
| 1189 |
pi->ipi_spec_dst = *source; |
| 1190 |
} |
| 1191 |
|
| 1192 |
return sendmsg_loop(fd, &mh, 0); |
| 1193 |
} |
| 1194 |
|
| 1195 |
static int manager_ipv6_send( |
| 1196 |
Manager *m, |
| 1197 |
int fd, |
| 1198 |
int ifindex, |
| 1199 |
const struct in6_addr *destination, |
| 1200 |
uint16_t port, |
| 1201 |
const struct in6_addr *source, |
| 1202 |
DnsPacket *p) { |
| 1203 |
|
| 1204 |
CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct in6_pktinfo))) control = {}; |
| 1205 |
union sockaddr_union sa; |
| 1206 |
struct iovec iov; |
| 1207 |
struct msghdr mh = { |
| 1208 |
.msg_iov = &iov, |
| 1209 |
.msg_iovlen = 1, |
| 1210 |
.msg_name = &sa.sa, |
| 1211 |
.msg_namelen = sizeof(sa.in6), |
| 1212 |
}; |
| 1213 |
|
| 1214 |
assert(m); |
| 1215 |
assert(fd >= 0); |
| 1216 |
assert(destination); |
| 1217 |
assert(port > 0); |
| 1218 |
assert(p); |
| 1219 |
|
| 1220 |
iov = IOVEC_MAKE(DNS_PACKET_DATA(p), p->size); |
| 1221 |
|
| 1222 |
sa = (union sockaddr_union) { |
| 1223 |
.in6.sin6_family = AF_INET6, |
| 1224 |
.in6.sin6_addr = *destination, |
| 1225 |
.in6.sin6_port = htobe16(port), |
| 1226 |
.in6.sin6_scope_id = ifindex, |
| 1227 |
}; |
| 1228 |
|
| 1229 |
if (ifindex > 0) { |
| 1230 |
struct cmsghdr *cmsg; |
| 1231 |
struct in6_pktinfo *pi; |
| 1232 |
|
| 1233 |
mh.msg_control = &control; |
| 1234 |
mh.msg_controllen = sizeof(control); |
| 1235 |
|
| 1236 |
cmsg = CMSG_FIRSTHDR(&mh); |
| 1237 |
cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo)); |
| 1238 |
cmsg->cmsg_level = IPPROTO_IPV6; |
| 1239 |
cmsg->cmsg_type = IPV6_PKTINFO; |
| 1240 |
|
| 1241 |
pi = CMSG_TYPED_DATA(cmsg, struct in6_pktinfo); |
| 1242 |
pi->ipi6_ifindex = ifindex; |
| 1243 |
|
| 1244 |
if (source) |
| 1245 |
pi->ipi6_addr = *source; |
| 1246 |
} |
| 1247 |
|
| 1248 |
return sendmsg_loop(fd, &mh, 0); |
| 1249 |
} |
| 1250 |
|
| 1251 |
static int dns_question_to_json(DnsQuestion *q, sd_json_variant **ret) { |
| 1252 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *l = NULL; |
| 1253 |
DnsResourceKey *key; |
| 1254 |
int r; |
| 1255 |
|
| 1256 |
assert(ret); |
| 1257 |
|
| 1258 |
DNS_QUESTION_FOREACH(key, q) { |
| 1259 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 1260 |
|
| 1261 |
r = dns_resource_key_to_json(key, &v); |
| 1262 |
if (r < 0) |
| 1263 |
return r; |
| 1264 |
|
| 1265 |
r = sd_json_variant_append_array(&l, v); |
| 1266 |
if (r < 0) |
| 1267 |
return r; |
| 1268 |
} |
| 1269 |
|
| 1270 |
*ret = TAKE_PTR(l); |
| 1271 |
return 0; |
| 1272 |
} |
| 1273 |
|
| 1274 |
int manager_monitor_send(Manager *m, DnsQuery *q) { |
| 1275 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *jquestion = NULL, *jcollected_questions = NULL, *janswer = NULL; |
| 1276 |
_cleanup_(dns_question_unrefp) DnsQuestion *merged = NULL; |
| 1277 |
DnsAnswerItem *rri; |
| 1278 |
int r; |
| 1279 |
|
| 1280 |
assert(m); |
| 1281 |
|
| 1282 |
if (set_isempty(m->varlink_query_results_subscription)) |
| 1283 |
return 0; |
| 1284 |
|
| 1285 |
/* Merge all questions into one */ |
| 1286 |
r = dns_question_merge(q->question_idna, q->question_utf8, &merged); |
| 1287 |
if (r < 0) |
| 1288 |
return log_error_errno(r, "Failed to merge UTF8/IDNA questions: %m"); |
| 1289 |
|
| 1290 |
if (q->question_bypass) { |
| 1291 |
_cleanup_(dns_question_unrefp) DnsQuestion *merged2 = NULL; |
| 1292 |
|
| 1293 |
r = dns_question_merge(merged, q->question_bypass->question, &merged2); |
| 1294 |
if (r < 0) |
| 1295 |
return log_error_errno(r, "Failed to merge UTF8/IDNA questions and DNS packet question: %m"); |
| 1296 |
|
| 1297 |
dns_question_unref(merged); |
| 1298 |
merged = TAKE_PTR(merged2); |
| 1299 |
} |
| 1300 |
|
| 1301 |
/* Convert the current primary question to JSON */ |
| 1302 |
r = dns_question_to_json(merged, &jquestion); |
| 1303 |
if (r < 0) |
| 1304 |
return log_error_errno(r, "Failed to convert question to JSON: %m"); |
| 1305 |
|
| 1306 |
/* Generate a JSON array of the questions preceding the current one in the CNAME chain */ |
| 1307 |
r = dns_question_to_json(q->collected_questions, &jcollected_questions); |
| 1308 |
if (r < 0) |
| 1309 |
return log_error_errno(r, "Failed to convert question to JSON: %m"); |
| 1310 |
|
| 1311 |
DNS_ANSWER_FOREACH_ITEM(rri, q->answer) { |
| 1312 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 1313 |
|
| 1314 |
r = dns_resource_record_to_json(rri->rr, &v); |
| 1315 |
if (r < 0) |
| 1316 |
return log_error_errno(r, "Failed to convert answer resource record to JSON: %m"); |
| 1317 |
|
| 1318 |
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 */ |
| 1319 |
if (r < 0) |
| 1320 |
return log_error_errno(r, "Failed to generate RR wire format: %m"); |
| 1321 |
|
| 1322 |
r = sd_json_variant_append_arraybo( |
| 1323 |
&janswer, |
| 1324 |
SD_JSON_BUILD_PAIR_CONDITION(!!v, "rr", SD_JSON_BUILD_VARIANT(v)), |
| 1325 |
SD_JSON_BUILD_PAIR_BASE64("raw", rri->rr->wire_format, rri->rr->wire_format_size), |
| 1326 |
SD_JSON_BUILD_PAIR_CONDITION(rri->ifindex > 0, "ifindex", SD_JSON_BUILD_INTEGER(rri->ifindex))); |
| 1327 |
if (r < 0) |
| 1328 |
return log_debug_errno(r, "Failed to append notification entry to array: %m"); |
| 1329 |
} |
| 1330 |
|
| 1331 |
r = varlink_many_notifybo( |
| 1332 |
m->varlink_query_results_subscription, |
| 1333 |
SD_JSON_BUILD_PAIR_STRING("state", dns_transaction_state_to_string(q->state)), |
| 1334 |
SD_JSON_BUILD_PAIR_CONDITION(q->state == DNS_TRANSACTION_DNSSEC_FAILED, |
| 1335 |
"result", SD_JSON_BUILD_STRING(dnssec_result_to_string(q->answer_dnssec_result))), |
| 1336 |
SD_JSON_BUILD_PAIR_CONDITION(q->state == DNS_TRANSACTION_RCODE_FAILURE, |
| 1337 |
"rcode", SD_JSON_BUILD_INTEGER(q->answer_rcode)), |
| 1338 |
SD_JSON_BUILD_PAIR_CONDITION(q->state == DNS_TRANSACTION_ERRNO, |
| 1339 |
"errno", SD_JSON_BUILD_INTEGER(q->answer_errno)), |
| 1340 |
SD_JSON_BUILD_PAIR_CONDITION(IN_SET(q->state, |
| 1341 |
DNS_TRANSACTION_DNSSEC_FAILED, |
| 1342 |
DNS_TRANSACTION_RCODE_FAILURE) && |
| 1343 |
q->answer_ede_rcode >= 0, |
| 1344 |
"extendedDNSErrorCode", SD_JSON_BUILD_INTEGER(q->answer_ede_rcode)), |
| 1345 |
SD_JSON_BUILD_PAIR_CONDITION(IN_SET(q->state, |
| 1346 |
DNS_TRANSACTION_DNSSEC_FAILED, |
| 1347 |
DNS_TRANSACTION_RCODE_FAILURE) && |
| 1348 |
q->answer_ede_rcode >= 0 && !isempty(q->answer_ede_msg), |
| 1349 |
"extendedDNSErrorMessage", SD_JSON_BUILD_STRING(q->answer_ede_msg)), |
| 1350 |
SD_JSON_BUILD_PAIR_VARIANT("question", jquestion), |
| 1351 |
SD_JSON_BUILD_PAIR_CONDITION(!!jcollected_questions, |
| 1352 |
"collectedQuestions", SD_JSON_BUILD_VARIANT(jcollected_questions)), |
| 1353 |
SD_JSON_BUILD_PAIR_CONDITION(!!janswer, |
| 1354 |
"answer", SD_JSON_BUILD_VARIANT(janswer))); |
| 1355 |
if (r < 0) |
| 1356 |
log_debug_errno(r, "Failed to send monitor event, ignoring: %m"); |
| 1357 |
|
| 1358 |
return 0; |
| 1359 |
} |
| 1360 |
|
| 1361 |
int manager_send( |
| 1362 |
Manager *m, |
| 1363 |
int fd, |
| 1364 |
int ifindex, |
| 1365 |
int family, |
| 1366 |
const union in_addr_union *destination, |
| 1367 |
uint16_t port, |
| 1368 |
const union in_addr_union *source, |
| 1369 |
DnsPacket *p) { |
| 1370 |
|
| 1371 |
assert(m); |
| 1372 |
assert(fd >= 0); |
| 1373 |
assert(destination); |
| 1374 |
assert(port > 0); |
| 1375 |
assert(p); |
| 1376 |
|
| 1377 |
/* For mDNS, it is natural that the packet have truncated flag when we have many known answers. */ |
| 1378 |
bool truncated = DNS_PACKET_TC(p) && (p->protocol != DNS_PROTOCOL_MDNS || !p->more); |
| 1379 |
|
| 1380 |
log_debug("Sending %s%s packet with id %" PRIu16 " on interface %i/%s of size %zu.", |
| 1381 |
truncated ? "truncated (!) " : "", |
| 1382 |
DNS_PACKET_QR(p) ? "response" : "query", |
| 1383 |
DNS_PACKET_ID(p), |
| 1384 |
ifindex, af_to_name(family), |
| 1385 |
p->size); |
| 1386 |
|
| 1387 |
if (family == AF_INET) |
| 1388 |
return manager_ipv4_send(m, fd, ifindex, &destination->in, port, source ? &source->in : NULL, p); |
| 1389 |
if (family == AF_INET6) |
| 1390 |
return manager_ipv6_send(m, fd, ifindex, &destination->in6, port, source ? &source->in6 : NULL, p); |
| 1391 |
|
| 1392 |
return -EAFNOSUPPORT; |
| 1393 |
} |
| 1394 |
|
| 1395 |
uint32_t manager_find_mtu(Manager *m) { |
| 1396 |
uint32_t mtu = 0; |
| 1397 |
Link *l; |
| 1398 |
|
| 1399 |
/* If we don't know on which link a DNS packet would be delivered, let's find the largest MTU that |
| 1400 |
* works on all interfaces we know of that have an IP address associated */ |
| 1401 |
|
| 1402 |
HASHMAP_FOREACH(l, m->links) { |
| 1403 |
/* Let's filter out links without IP addresses (e.g. AF_CAN links and suchlike) */ |
| 1404 |
if (!l->addresses) |
| 1405 |
continue; |
| 1406 |
|
| 1407 |
/* Safety check: MTU shorter than what we need for the absolutely shortest DNS request? Then |
| 1408 |
* let's ignore this link. */ |
| 1409 |
if (l->mtu < MIN(UDP4_PACKET_HEADER_SIZE + DNS_PACKET_HEADER_SIZE, |
| 1410 |
UDP6_PACKET_HEADER_SIZE + DNS_PACKET_HEADER_SIZE)) |
| 1411 |
continue; |
| 1412 |
|
| 1413 |
if (mtu <= 0 || l->mtu < mtu) |
| 1414 |
mtu = l->mtu; |
| 1415 |
} |
| 1416 |
|
| 1417 |
if (mtu == 0) /* found nothing? then let's assume the typical Ethernet MTU for lack of anything more precise */ |
| 1418 |
return 1500; |
| 1419 |
|
| 1420 |
return mtu; |
| 1421 |
} |
| 1422 |
|
| 1423 |
int manager_find_ifindex(Manager *m, int family, const union in_addr_union *in_addr) { |
| 1424 |
LinkAddress *a; |
| 1425 |
|
| 1426 |
assert(m); |
| 1427 |
|
| 1428 |
if (!IN_SET(family, AF_INET, AF_INET6)) |
| 1429 |
return 0; |
| 1430 |
|
| 1431 |
if (!in_addr) |
| 1432 |
return 0; |
| 1433 |
|
| 1434 |
a = manager_find_link_address(m, family, in_addr); |
| 1435 |
if (a) |
| 1436 |
return a->link->ifindex; |
| 1437 |
|
| 1438 |
return 0; |
| 1439 |
} |
| 1440 |
|
| 1441 |
void manager_refresh_rrs(Manager *m) { |
| 1442 |
Link *l; |
| 1443 |
DnssdRegisteredService *s; |
| 1444 |
|
| 1445 |
assert(m); |
| 1446 |
|
| 1447 |
m->llmnr_host_ipv4_key = dns_resource_key_unref(m->llmnr_host_ipv4_key); |
| 1448 |
m->llmnr_host_ipv6_key = dns_resource_key_unref(m->llmnr_host_ipv6_key); |
| 1449 |
m->mdns_host_ipv4_key = dns_resource_key_unref(m->mdns_host_ipv4_key); |
| 1450 |
m->mdns_host_ipv6_key = dns_resource_key_unref(m->mdns_host_ipv6_key); |
| 1451 |
|
| 1452 |
HASHMAP_FOREACH(l, m->links) |
| 1453 |
link_add_rrs(l, true); |
| 1454 |
|
| 1455 |
if (m->mdns_support == RESOLVE_SUPPORT_YES) |
| 1456 |
HASHMAP_FOREACH(s, m->dnssd_registered_services) |
| 1457 |
if (dnssd_update_rrs(s) < 0) |
| 1458 |
log_warning("Failed to refresh DNS-SD service '%s'", s->id); |
| 1459 |
|
| 1460 |
HASHMAP_FOREACH(l, m->links) |
| 1461 |
link_add_rrs(l, false); |
| 1462 |
} |
| 1463 |
|
| 1464 |
static int manager_next_random_name(const char *old, char **ret_new) { |
| 1465 |
const char *p; |
| 1466 |
uint64_t u, a; |
| 1467 |
char *n; |
| 1468 |
|
| 1469 |
assert(ret_new); |
| 1470 |
|
| 1471 |
p = strchr(old, 0); |
| 1472 |
assert(p); |
| 1473 |
|
| 1474 |
while (p > old) { |
| 1475 |
if (!ascii_isdigit(p[-1])) |
| 1476 |
break; |
| 1477 |
|
| 1478 |
p--; |
| 1479 |
} |
| 1480 |
|
| 1481 |
if (*p == 0 || safe_atou64(p, &u) < 0 || u <= 0) |
| 1482 |
u = 1; |
| 1483 |
|
| 1484 |
/* Add a random number to the old value. This way we can avoid |
| 1485 |
* that two hosts pick the same hostname, win on IPv4 and lose |
| 1486 |
* on IPv6 (or vice versa), and pick the same hostname |
| 1487 |
* replacement hostname, ad infinitum. We still want the |
| 1488 |
* numbers to go up monotonically, hence we just add a random |
| 1489 |
* value 1..10 */ |
| 1490 |
|
| 1491 |
random_bytes(&a, sizeof(a)); |
| 1492 |
u += 1 + a % 10; |
| 1493 |
|
| 1494 |
if (asprintf(&n, "%.*s%" PRIu64, (int) (p - old), old, u) < 0) |
| 1495 |
return -ENOMEM; |
| 1496 |
|
| 1497 |
*ret_new = n; |
| 1498 |
|
| 1499 |
return 0; |
| 1500 |
} |
| 1501 |
|
| 1502 |
int manager_next_hostname(Manager *m) { |
| 1503 |
_cleanup_free_ char *h = NULL, *k = NULL; |
| 1504 |
int r; |
| 1505 |
|
| 1506 |
assert(m); |
| 1507 |
|
| 1508 |
r = manager_next_random_name(m->llmnr_hostname, &h); |
| 1509 |
if (r < 0) |
| 1510 |
return r; |
| 1511 |
|
| 1512 |
r = dns_name_concat(h, "local", 0, &k); |
| 1513 |
if (r < 0) |
| 1514 |
return r; |
| 1515 |
|
| 1516 |
log_info("Hostname conflict, changing published hostname from '%s' to '%s'.", m->llmnr_hostname, h); |
| 1517 |
|
| 1518 |
free_and_replace(m->llmnr_hostname, h); |
| 1519 |
free_and_replace(m->mdns_hostname, k); |
| 1520 |
|
| 1521 |
manager_refresh_rrs(m); |
| 1522 |
(void) manager_send_changed(m, "LLMNRHostname"); |
| 1523 |
|
| 1524 |
return 0; |
| 1525 |
} |
| 1526 |
|
| 1527 |
LinkAddress* manager_find_link_address(Manager *m, int family, const union in_addr_union *in_addr) { |
| 1528 |
Link *l; |
| 1529 |
|
| 1530 |
assert(m); |
| 1531 |
|
| 1532 |
if (!IN_SET(family, AF_INET, AF_INET6)) |
| 1533 |
return NULL; |
| 1534 |
|
| 1535 |
if (!in_addr) |
| 1536 |
return NULL; |
| 1537 |
|
| 1538 |
HASHMAP_FOREACH(l, m->links) { |
| 1539 |
LinkAddress *a; |
| 1540 |
|
| 1541 |
a = link_find_address(l, family, in_addr); |
| 1542 |
if (a) |
| 1543 |
return a; |
| 1544 |
} |
| 1545 |
|
| 1546 |
return NULL; |
| 1547 |
} |
| 1548 |
|
| 1549 |
bool manager_packet_from_local_address(Manager *m, DnsPacket *p) { |
| 1550 |
assert(m); |
| 1551 |
assert(p); |
| 1552 |
|
| 1553 |
/* Let's see if this packet comes from an IP address we have on any local interface */ |
| 1554 |
|
| 1555 |
return !!manager_find_link_address(m, p->family, &p->sender); |
| 1556 |
} |
| 1557 |
|
| 1558 |
bool manager_packet_from_our_transaction(Manager *m, DnsPacket *p) { |
| 1559 |
DnsTransaction *t; |
| 1560 |
|
| 1561 |
assert(m); |
| 1562 |
assert(p); |
| 1563 |
|
| 1564 |
/* Let's see if we have a transaction with a query message with the exact same binary contents as the |
| 1565 |
* one we just got. If so, it's almost definitely a packet loop of some kind. */ |
| 1566 |
|
| 1567 |
t = hashmap_get(m->dns_transactions, UINT_TO_PTR(DNS_PACKET_ID(p))); |
| 1568 |
if (!t) |
| 1569 |
return false; |
| 1570 |
|
| 1571 |
return t->sent && dns_packet_equal(t->sent, p); |
| 1572 |
} |
| 1573 |
|
| 1574 |
DnsScope* manager_find_scope_from_protocol(Manager *m, int ifindex, DnsProtocol protocol, int family) { |
| 1575 |
Link *l; |
| 1576 |
|
| 1577 |
assert(m); |
| 1578 |
|
| 1579 |
l = hashmap_get(m->links, INT_TO_PTR(ifindex)); |
| 1580 |
if (!l) |
| 1581 |
return NULL; |
| 1582 |
|
| 1583 |
switch (protocol) { |
| 1584 |
case DNS_PROTOCOL_LLMNR: |
| 1585 |
if (family == AF_INET) |
| 1586 |
return l->llmnr_ipv4_scope; |
| 1587 |
else if (family == AF_INET6) |
| 1588 |
return l->llmnr_ipv6_scope; |
| 1589 |
|
| 1590 |
break; |
| 1591 |
|
| 1592 |
case DNS_PROTOCOL_MDNS: |
| 1593 |
if (family == AF_INET) |
| 1594 |
return l->mdns_ipv4_scope; |
| 1595 |
else if (family == AF_INET6) |
| 1596 |
return l->mdns_ipv6_scope; |
| 1597 |
|
| 1598 |
break; |
| 1599 |
|
| 1600 |
default: |
| 1601 |
; |
| 1602 |
} |
| 1603 |
|
| 1604 |
return NULL; |
| 1605 |
} |
| 1606 |
|
| 1607 |
void manager_verify_all(Manager *m) { |
| 1608 |
assert(m); |
| 1609 |
|
| 1610 |
LIST_FOREACH(scopes, s, m->dns_scopes) |
| 1611 |
dns_zone_verify_all(&s->zone); |
| 1612 |
} |
| 1613 |
|
| 1614 |
int manager_is_own_hostname(Manager *m, const char *name) { |
| 1615 |
int r; |
| 1616 |
|
| 1617 |
assert(m); |
| 1618 |
assert(name); |
| 1619 |
|
| 1620 |
if (m->llmnr_hostname) { |
| 1621 |
r = dns_name_equal(name, m->llmnr_hostname); |
| 1622 |
if (r != 0) |
| 1623 |
return r; |
| 1624 |
} |
| 1625 |
|
| 1626 |
if (m->mdns_hostname) { |
| 1627 |
r = dns_name_equal(name, m->mdns_hostname); |
| 1628 |
if (r != 0) |
| 1629 |
return r; |
| 1630 |
} |
| 1631 |
|
| 1632 |
if (m->full_hostname) |
| 1633 |
return dns_name_equal(name, m->full_hostname); |
| 1634 |
|
| 1635 |
return 0; |
| 1636 |
} |
| 1637 |
|
| 1638 |
int manager_compile_dns_servers(Manager *m, OrderedSet **servers) { |
| 1639 |
Link *l; |
| 1640 |
int r; |
| 1641 |
|
| 1642 |
assert(m); |
| 1643 |
assert(servers); |
| 1644 |
|
| 1645 |
r = ordered_set_ensure_allocated(servers, &dns_server_hash_ops); |
| 1646 |
if (r < 0) |
| 1647 |
return r; |
| 1648 |
|
| 1649 |
/* First add the system-wide servers and domains */ |
| 1650 |
LIST_FOREACH(servers, s, m->dns_servers) { |
| 1651 |
r = ordered_set_put(*servers, s); |
| 1652 |
if (r == -EEXIST) |
| 1653 |
continue; |
| 1654 |
if (r < 0) |
| 1655 |
return r; |
| 1656 |
} |
| 1657 |
|
| 1658 |
/* Then, add the per-link servers */ |
| 1659 |
HASHMAP_FOREACH(l, m->links) |
| 1660 |
LIST_FOREACH(servers, s, l->dns_servers) { |
| 1661 |
r = ordered_set_put(*servers, s); |
| 1662 |
if (r == -EEXIST) |
| 1663 |
continue; |
| 1664 |
if (r < 0) |
| 1665 |
return r; |
| 1666 |
} |
| 1667 |
|
| 1668 |
/* Third, add the delegate servers and domains */ |
| 1669 |
DnsDelegate *d; |
| 1670 |
HASHMAP_FOREACH(d, m->delegates) |
| 1671 |
LIST_FOREACH(servers, s, d->dns_servers) { |
| 1672 |
r = ordered_set_put(*servers, s); |
| 1673 |
if (r == -EEXIST) |
| 1674 |
continue; |
| 1675 |
if (r < 0) |
| 1676 |
return r; |
| 1677 |
} |
| 1678 |
|
| 1679 |
/* If we found nothing, add the fallback servers */ |
| 1680 |
if (ordered_set_isempty(*servers)) { |
| 1681 |
LIST_FOREACH(servers, s, m->fallback_dns_servers) { |
| 1682 |
r = ordered_set_put(*servers, s); |
| 1683 |
if (r == -EEXIST) |
| 1684 |
continue; |
| 1685 |
if (r < 0) |
| 1686 |
return r; |
| 1687 |
} |
| 1688 |
} |
| 1689 |
|
| 1690 |
return 0; |
| 1691 |
} |
| 1692 |
|
| 1693 |
/* filter_route is a tri-state: |
| 1694 |
* < 0: no filtering |
| 1695 |
* = 0 or false: return only domains which should be used for searching |
| 1696 |
* > 0 or true: return only domains which are for routing only |
| 1697 |
*/ |
| 1698 |
int manager_compile_search_domains(Manager *m, OrderedSet **domains, int filter_route) { |
| 1699 |
int r; |
| 1700 |
|
| 1701 |
assert(m); |
| 1702 |
assert(domains); |
| 1703 |
|
| 1704 |
r = ordered_set_ensure_allocated(domains, &dns_name_hash_ops); |
| 1705 |
if (r < 0) |
| 1706 |
return r; |
| 1707 |
|
| 1708 |
LIST_FOREACH(domains, d, m->search_domains) { |
| 1709 |
|
| 1710 |
if (filter_route >= 0 && |
| 1711 |
d->route_only != !!filter_route) |
| 1712 |
continue; |
| 1713 |
|
| 1714 |
r = ordered_set_put(*domains, d->name); |
| 1715 |
if (r == -EEXIST) |
| 1716 |
continue; |
| 1717 |
if (r < 0) |
| 1718 |
return r; |
| 1719 |
} |
| 1720 |
|
| 1721 |
DnsDelegate *delegate; |
| 1722 |
HASHMAP_FOREACH(delegate, m->delegates) |
| 1723 |
LIST_FOREACH(domains, d, delegate->search_domains) { |
| 1724 |
|
| 1725 |
if (filter_route >= 0 && |
| 1726 |
d->route_only != !!filter_route) |
| 1727 |
continue; |
| 1728 |
|
| 1729 |
r = ordered_set_put(*domains, d->name); |
| 1730 |
if (r == -EEXIST) |
| 1731 |
continue; |
| 1732 |
if (r < 0) |
| 1733 |
return r; |
| 1734 |
} |
| 1735 |
|
| 1736 |
Link *l; |
| 1737 |
HASHMAP_FOREACH(l, m->links) |
| 1738 |
LIST_FOREACH(domains, d, l->search_domains) { |
| 1739 |
|
| 1740 |
if (filter_route >= 0 && |
| 1741 |
d->route_only != !!filter_route) |
| 1742 |
continue; |
| 1743 |
|
| 1744 |
r = ordered_set_put(*domains, d->name); |
| 1745 |
if (r == -EEXIST) |
| 1746 |
continue; |
| 1747 |
if (r < 0) |
| 1748 |
return r; |
| 1749 |
} |
| 1750 |
|
| 1751 |
return 0; |
| 1752 |
} |
| 1753 |
|
| 1754 |
DnssecMode manager_get_dnssec_mode(Manager *m) { |
| 1755 |
assert(m); |
| 1756 |
|
| 1757 |
if (m->dnssec_mode != _DNSSEC_MODE_INVALID) |
| 1758 |
return m->dnssec_mode; |
| 1759 |
|
| 1760 |
return DNSSEC_NO; |
| 1761 |
} |
| 1762 |
|
| 1763 |
bool manager_dnssec_supported(Manager *m) { |
| 1764 |
DnsServer *server; |
| 1765 |
Link *l; |
| 1766 |
|
| 1767 |
assert(m); |
| 1768 |
|
| 1769 |
if (manager_get_dnssec_mode(m) == DNSSEC_NO) |
| 1770 |
return false; |
| 1771 |
|
| 1772 |
server = manager_get_dns_server(m); |
| 1773 |
if (server && !dns_server_dnssec_supported(server)) |
| 1774 |
return false; |
| 1775 |
|
| 1776 |
HASHMAP_FOREACH(l, m->links) |
| 1777 |
if (!link_dnssec_supported(l)) |
| 1778 |
return false; |
| 1779 |
|
| 1780 |
return true; |
| 1781 |
} |
| 1782 |
|
| 1783 |
DnsOverTlsMode manager_get_dns_over_tls_mode(Manager *m) { |
| 1784 |
assert(m); |
| 1785 |
|
| 1786 |
if (m->dns_over_tls_mode != _DNS_OVER_TLS_MODE_INVALID) |
| 1787 |
return m->dns_over_tls_mode; |
| 1788 |
|
| 1789 |
return DNS_OVER_TLS_NO; |
| 1790 |
} |
| 1791 |
|
| 1792 |
void manager_dnssec_verdict(Manager *m, DnssecVerdict verdict, const DnsResourceKey *key) { |
| 1793 |
|
| 1794 |
assert(verdict >= 0); |
| 1795 |
assert(verdict < _DNSSEC_VERDICT_MAX); |
| 1796 |
|
| 1797 |
if (DEBUG_LOGGING) { |
| 1798 |
char s[DNS_RESOURCE_KEY_STRING_MAX]; |
| 1799 |
|
| 1800 |
log_debug("Found verdict for lookup %s: %s", |
| 1801 |
dns_resource_key_to_string(key, s, sizeof s), |
| 1802 |
dnssec_verdict_to_string(verdict)); |
| 1803 |
} |
| 1804 |
|
| 1805 |
m->n_dnssec_verdict[verdict]++; |
| 1806 |
} |
| 1807 |
|
| 1808 |
bool manager_routable(Manager *m) { |
| 1809 |
Link *l; |
| 1810 |
|
| 1811 |
assert(m); |
| 1812 |
|
| 1813 |
/* Returns true if the host has at least one interface with a routable address (regardless if IPv4 or IPv6) */ |
| 1814 |
|
| 1815 |
HASHMAP_FOREACH(l, m->links) |
| 1816 |
if (link_relevant(l, AF_UNSPEC, false)) |
| 1817 |
return true; |
| 1818 |
|
| 1819 |
return false; |
| 1820 |
} |
| 1821 |
|
| 1822 |
void manager_flush_caches(Manager *m, int log_level) { |
| 1823 |
assert(m); |
| 1824 |
|
| 1825 |
LIST_FOREACH(scopes, scope, m->dns_scopes) |
| 1826 |
dns_cache_flush(&scope->cache); |
| 1827 |
|
| 1828 |
dns_browse_services_purge(m, AF_UNSPEC); /* Clear records of DNS service browse subscriber, since caches are flushed */ |
| 1829 |
dns_browse_services_restart(m); |
| 1830 |
|
| 1831 |
log_full(log_level, "Flushed all caches."); |
| 1832 |
} |
| 1833 |
|
| 1834 |
void manager_reset_server_features(Manager *m) { |
| 1835 |
|
| 1836 |
dns_server_reset_features_all(m->dns_servers); |
| 1837 |
dns_server_reset_features_all(m->fallback_dns_servers); |
| 1838 |
|
| 1839 |
Link *l; |
| 1840 |
HASHMAP_FOREACH(l, m->links) |
| 1841 |
dns_server_reset_features_all(l->dns_servers); |
| 1842 |
|
| 1843 |
DnsDelegate *d; |
| 1844 |
HASHMAP_FOREACH(d, m->delegates) |
| 1845 |
dns_server_reset_features_all(d->dns_servers); |
| 1846 |
|
| 1847 |
log_info("Resetting learnt feature levels on all servers."); |
| 1848 |
} |
| 1849 |
|
| 1850 |
void manager_cleanup_saved_user(Manager *m) { |
| 1851 |
_cleanup_closedir_ DIR *d = NULL; |
| 1852 |
|
| 1853 |
assert(m); |
| 1854 |
|
| 1855 |
/* Clean up all saved per-link files in /run/systemd/resolve/netif/ that don't have a matching interface |
| 1856 |
* anymore. These files are created to persist settings pushed in by the user via the bus, so that resolved can |
| 1857 |
* be restarted without losing this data. */ |
| 1858 |
|
| 1859 |
d = opendir("/run/systemd/resolve/netif/"); |
| 1860 |
if (!d) { |
| 1861 |
if (errno == ENOENT) |
| 1862 |
return; |
| 1863 |
|
| 1864 |
log_warning_errno(errno, "Failed to open interface directory: %m"); |
| 1865 |
return; |
| 1866 |
} |
| 1867 |
|
| 1868 |
FOREACH_DIRENT_ALL(de, d, log_error_errno(errno, "Failed to read interface directory: %m")) { |
| 1869 |
int ifindex; |
| 1870 |
Link *l; |
| 1871 |
|
| 1872 |
if (!IN_SET(de->d_type, DT_UNKNOWN, DT_REG)) |
| 1873 |
continue; |
| 1874 |
|
| 1875 |
if (dot_or_dot_dot(de->d_name)) |
| 1876 |
continue; |
| 1877 |
|
| 1878 |
ifindex = parse_ifindex(de->d_name); |
| 1879 |
if (ifindex < 0) /* Probably some temporary file from a previous run. Delete it */ |
| 1880 |
goto rm; |
| 1881 |
|
| 1882 |
l = hashmap_get(m->links, INT_TO_PTR(ifindex)); |
| 1883 |
if (!l) /* link vanished */ |
| 1884 |
goto rm; |
| 1885 |
|
| 1886 |
if (l->is_managed) /* now managed by networkd, hence the bus settings are useless */ |
| 1887 |
goto rm; |
| 1888 |
|
| 1889 |
continue; |
| 1890 |
|
| 1891 |
rm: |
| 1892 |
if (unlinkat(dirfd(d), de->d_name, 0) < 0) |
| 1893 |
log_warning_errno(errno, "Failed to remove left-over interface configuration file '%s', ignoring: %m", de->d_name); |
| 1894 |
} |
| 1895 |
} |
| 1896 |
|
| 1897 |
bool manager_next_dnssd_names(Manager *m) { |
| 1898 |
DnssdRegisteredService *s; |
| 1899 |
bool tried = false; |
| 1900 |
int r; |
| 1901 |
|
| 1902 |
assert(m); |
| 1903 |
|
| 1904 |
HASHMAP_FOREACH(s, m->dnssd_registered_services) { |
| 1905 |
_cleanup_free_ char * new_name = NULL; |
| 1906 |
|
| 1907 |
if (!s->withdrawn) |
| 1908 |
continue; |
| 1909 |
|
| 1910 |
r = manager_next_random_name(s->name_template, &new_name); |
| 1911 |
if (r < 0) { |
| 1912 |
log_warning_errno(r, "Failed to get new name for service '%s': %m", s->id); |
| 1913 |
continue; |
| 1914 |
} |
| 1915 |
|
| 1916 |
free_and_replace(s->name_template, new_name); |
| 1917 |
|
| 1918 |
s->withdrawn = false; |
| 1919 |
|
| 1920 |
tried = true; |
| 1921 |
} |
| 1922 |
|
| 1923 |
if (tried) |
| 1924 |
manager_refresh_rrs(m); |
| 1925 |
|
| 1926 |
return tried; |
| 1927 |
} |
| 1928 |
|
| 1929 |
bool manager_server_is_stub(Manager *m, DnsServer *s) { |
| 1930 |
DnsStubListenerExtra *l; |
| 1931 |
|
| 1932 |
assert(m); |
| 1933 |
assert(s); |
| 1934 |
|
| 1935 |
/* Safety check: we generally already skip the main stub when parsing configuration. But let's be |
| 1936 |
* extra careful, and check here again */ |
| 1937 |
if (s->family == AF_INET && |
| 1938 |
s->address.in.s_addr == htobe32(INADDR_DNS_STUB) && |
| 1939 |
dns_server_port(s) == 53) |
| 1940 |
return true; |
| 1941 |
|
| 1942 |
/* Main reason to call this is to check server data against the extra listeners, and filter things |
| 1943 |
* out. */ |
| 1944 |
ORDERED_SET_FOREACH(l, m->dns_extra_stub_listeners) |
| 1945 |
if (s->family == l->family && |
| 1946 |
in_addr_equal(s->family, &s->address, &l->address) && |
| 1947 |
dns_server_port(s) == dns_stub_listener_extra_port(l)) |
| 1948 |
return true; |
| 1949 |
|
| 1950 |
return false; |
| 1951 |
} |
| 1952 |
|
| 1953 |
int socket_disable_pmtud(int fd, int af) { |
| 1954 |
int r; |
| 1955 |
|
| 1956 |
assert(fd >= 0); |
| 1957 |
|
| 1958 |
if (af == AF_UNSPEC) { |
| 1959 |
af = socket_get_family(fd); |
| 1960 |
if (af < 0) |
| 1961 |
return af; |
| 1962 |
} |
| 1963 |
|
| 1964 |
switch (af) { |
| 1965 |
|
| 1966 |
case AF_INET: { |
| 1967 |
/* Turn off path MTU discovery, let's rather fragment on the way than to open us up against |
| 1968 |
* PMTU forgery vulnerabilities. |
| 1969 |
* |
| 1970 |
* There appears to be no documentation about IP_PMTUDISC_OMIT, but it has the effect that |
| 1971 |
* the "Don't Fragment" bit in the IPv4 header is turned off, thus enforcing fragmentation if |
| 1972 |
* our datagram size exceeds the MTU of a router in the path, and turning off path MTU |
| 1973 |
* discovery. |
| 1974 |
* |
| 1975 |
* This helps mitigating the PMTUD vulnerability described here: |
| 1976 |
* |
| 1977 |
* https://blog.apnic.net/2019/07/12/its-time-to-consider-avoiding-ip-fragmentation-in-the-dns/ |
| 1978 |
* |
| 1979 |
* Similar logic is in place in most DNS servers. |
| 1980 |
* |
| 1981 |
* There are multiple conflicting goals: we want to allow the largest datagrams possible (for |
| 1982 |
* efficiency reasons), but not have fragmentation (for security reasons), nor use PMTUD (for |
| 1983 |
* security reasons, too). Our strategy to deal with this is: use large packets, turn off |
| 1984 |
* PMTUD, but watch fragmentation taking place, and then size our packets to the max of the |
| 1985 |
* fragments seen — and if we need larger packets always go to TCP. |
| 1986 |
*/ |
| 1987 |
|
| 1988 |
r = setsockopt_int(fd, IPPROTO_IP, IP_MTU_DISCOVER, IP_PMTUDISC_OMIT); |
| 1989 |
if (r < 0) |
| 1990 |
return r; |
| 1991 |
|
| 1992 |
return 0; |
| 1993 |
} |
| 1994 |
|
| 1995 |
case AF_INET6: { |
| 1996 |
/* On IPv6 fragmentation only is done by the sender — never by routers on the path. PMTUD is |
| 1997 |
* mandatory. If we want to turn off PMTUD, the only way is by sending with minimal MTU only, |
| 1998 |
* so that we apply maximum fragmentation locally already, and thus PMTUD doesn't happen |
| 1999 |
* because there's nothing that could be fragmented further anymore. */ |
| 2000 |
|
| 2001 |
r = setsockopt_int(fd, IPPROTO_IPV6, IPV6_MTU, IPV6_MIN_MTU); |
| 2002 |
if (r < 0) |
| 2003 |
return r; |
| 2004 |
|
| 2005 |
return 0; |
| 2006 |
} |
| 2007 |
|
| 2008 |
default: |
| 2009 |
return -EAFNOSUPPORT; |
| 2010 |
} |
| 2011 |
} |
| 2012 |
|
| 2013 |
int dns_manager_dump_statistics_json(Manager *m, sd_json_variant **ret) { |
| 2014 |
uint64_t size = 0, hit = 0, miss = 0; |
| 2015 |
|
| 2016 |
assert(m); |
| 2017 |
assert(ret); |
| 2018 |
|
| 2019 |
LIST_FOREACH(scopes, s, m->dns_scopes) { |
| 2020 |
size += dns_cache_size(&s->cache); |
| 2021 |
hit += s->cache.n_hit; |
| 2022 |
miss += s->cache.n_miss; |
| 2023 |
} |
| 2024 |
|
| 2025 |
return sd_json_buildo(ret, |
| 2026 |
SD_JSON_BUILD_PAIR("transactions", SD_JSON_BUILD_OBJECT( |
| 2027 |
SD_JSON_BUILD_PAIR_UNSIGNED("currentTransactions", hashmap_size(m->dns_transactions)), |
| 2028 |
SD_JSON_BUILD_PAIR_UNSIGNED("totalTransactions", m->n_transactions_total), |
| 2029 |
SD_JSON_BUILD_PAIR_UNSIGNED("totalTimeouts", m->n_timeouts_total), |
| 2030 |
SD_JSON_BUILD_PAIR_UNSIGNED("totalTimeoutsServedStale", m->n_timeouts_served_stale_total), |
| 2031 |
SD_JSON_BUILD_PAIR_UNSIGNED("totalFailedResponses", m->n_failure_responses_total), |
| 2032 |
SD_JSON_BUILD_PAIR_UNSIGNED("totalFailedResponsesServedStale", m->n_failure_responses_served_stale_total) |
| 2033 |
)), |
| 2034 |
SD_JSON_BUILD_PAIR("cache", SD_JSON_BUILD_OBJECT( |
| 2035 |
SD_JSON_BUILD_PAIR_UNSIGNED("size", size), |
| 2036 |
SD_JSON_BUILD_PAIR_UNSIGNED("hits", hit), |
| 2037 |
SD_JSON_BUILD_PAIR_UNSIGNED("misses", miss) |
| 2038 |
)), |
| 2039 |
SD_JSON_BUILD_PAIR("dnssec", SD_JSON_BUILD_OBJECT( |
| 2040 |
SD_JSON_BUILD_PAIR_UNSIGNED("secure", m->n_dnssec_verdict[DNSSEC_SECURE]), |
| 2041 |
SD_JSON_BUILD_PAIR_UNSIGNED("insecure", m->n_dnssec_verdict[DNSSEC_INSECURE]), |
| 2042 |
SD_JSON_BUILD_PAIR_UNSIGNED("bogus", m->n_dnssec_verdict[DNSSEC_BOGUS]), |
| 2043 |
SD_JSON_BUILD_PAIR_UNSIGNED("indeterminate", m->n_dnssec_verdict[DNSSEC_INDETERMINATE]) |
| 2044 |
))); |
| 2045 |
} |
| 2046 |
|
| 2047 |
void dns_manager_reset_statistics(Manager *m) { |
| 2048 |
|
| 2049 |
assert(m); |
| 2050 |
|
| 2051 |
LIST_FOREACH(scopes, s, m->dns_scopes) |
| 2052 |
s->cache.n_hit = s->cache.n_miss = 0; |
| 2053 |
|
| 2054 |
m->n_transactions_total = 0; |
| 2055 |
m->n_timeouts_total = 0; |
| 2056 |
m->n_timeouts_served_stale_total = 0; |
| 2057 |
m->n_failure_responses_total = 0; |
| 2058 |
m->n_failure_responses_served_stale_total = 0; |
| 2059 |
zero(m->n_dnssec_verdict); |
| 2060 |
} |
| 2061 |
|
| 2062 |
static int dns_configuration_json_append( |
| 2063 |
const char *ifname, |
| 2064 |
int ifindex, |
| 2065 |
const char *delegate, |
| 2066 |
int default_route, |
| 2067 |
DnsServer *current_dns_server, |
| 2068 |
DnsServer *dns_servers, |
| 2069 |
DnsServer *fallback_dns_servers, |
| 2070 |
DnsSearchDomain *search_domains, |
| 2071 |
Set *negative_trust_anchors, |
| 2072 |
Set *dns_scopes, |
| 2073 |
DnssecMode dnssec_mode, |
| 2074 |
bool dnssec_supported, |
| 2075 |
DnsOverTlsMode dns_over_tls_mode, |
| 2076 |
ResolveSupport llmnr_support, |
| 2077 |
ResolveSupport mdns_support, |
| 2078 |
ResolvConfMode resolv_conf_mode, |
| 2079 |
sd_json_variant **configuration) { |
| 2080 |
|
| 2081 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *dns_servers_json = NULL, |
| 2082 |
*fallback_dns_servers_json = NULL, |
| 2083 |
*search_domains_json = NULL, |
| 2084 |
*current_dns_server_json = NULL, |
| 2085 |
*scopes_json = NULL; |
| 2086 |
DnsScope *scope; |
| 2087 |
int r; |
| 2088 |
|
| 2089 |
assert(configuration); |
| 2090 |
|
| 2091 |
if (current_dns_server) { |
| 2092 |
r = dns_server_dump_configuration_to_json(current_dns_server, ¤t_dns_server_json); |
| 2093 |
if (r < 0) |
| 2094 |
return r; |
| 2095 |
} |
| 2096 |
|
| 2097 |
SET_FOREACH(scope, dns_scopes) { |
| 2098 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 2099 |
|
| 2100 |
r = dns_scope_to_json(scope, /* with_cache= */ false, &v); |
| 2101 |
if (r < 0) |
| 2102 |
return r; |
| 2103 |
|
| 2104 |
r = sd_json_variant_append_array(&scopes_json, v); |
| 2105 |
if (r < 0) |
| 2106 |
return r; |
| 2107 |
} |
| 2108 |
|
| 2109 |
LIST_FOREACH(servers, s, dns_servers) { |
| 2110 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 2111 |
|
| 2112 |
r = dns_server_dump_configuration_to_json(s, &v); |
| 2113 |
if (r < 0) |
| 2114 |
return r; |
| 2115 |
|
| 2116 |
r = sd_json_variant_append_array(&dns_servers_json, v); |
| 2117 |
if (r < 0) |
| 2118 |
return r; |
| 2119 |
} |
| 2120 |
|
| 2121 |
LIST_FOREACH(domains, d, search_domains) { |
| 2122 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 2123 |
|
| 2124 |
r = dns_search_domain_dump_to_json(d, &v); |
| 2125 |
if (r < 0) |
| 2126 |
return r; |
| 2127 |
|
| 2128 |
r = sd_json_variant_append_array(&search_domains_json, v); |
| 2129 |
if (r < 0) |
| 2130 |
return r; |
| 2131 |
} |
| 2132 |
|
| 2133 |
LIST_FOREACH(servers, s, fallback_dns_servers) { |
| 2134 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 2135 |
|
| 2136 |
r = dns_server_dump_configuration_to_json(s, &v); |
| 2137 |
if (r < 0) |
| 2138 |
return r; |
| 2139 |
|
| 2140 |
r = sd_json_variant_append_array(&fallback_dns_servers_json, v); |
| 2141 |
if (r < 0) |
| 2142 |
return r; |
| 2143 |
} |
| 2144 |
|
| 2145 |
return sd_json_variant_append_arraybo( |
| 2146 |
configuration, |
| 2147 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("ifname", ifname), |
| 2148 |
SD_JSON_BUILD_PAIR_CONDITION(ifindex > 0, "ifindex", SD_JSON_BUILD_UNSIGNED(ifindex)), |
| 2149 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("delegate", delegate), |
| 2150 |
JSON_BUILD_PAIR_TRISTATE_NON_NULL("defaultRoute", default_route), |
| 2151 |
JSON_BUILD_PAIR_VARIANT_NON_NULL("currentServer", current_dns_server_json), |
| 2152 |
JSON_BUILD_PAIR_VARIANT_NON_NULL("servers", dns_servers_json), |
| 2153 |
JSON_BUILD_PAIR_VARIANT_NON_NULL("fallbackServers", fallback_dns_servers_json), |
| 2154 |
JSON_BUILD_PAIR_VARIANT_NON_NULL("searchDomains", search_domains_json), |
| 2155 |
SD_JSON_BUILD_PAIR_CONDITION(!set_isempty(negative_trust_anchors), |
| 2156 |
"negativeTrustAnchors", |
| 2157 |
JSON_BUILD_STRING_SET(negative_trust_anchors)), |
| 2158 |
JSON_BUILD_PAIR_CONDITION_BOOLEAN(dnssec_mode >= 0, "dnssecSupported", dnssec_supported), |
| 2159 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("dnssec", dnssec_mode_to_string(dnssec_mode)), |
| 2160 |
JSON_BUILD_PAIR_STRING_NON_EMPTY("dnsOverTLS", dns_over_tls_mode_to_string(dns_over_tls_mode)), |
| 2161 |
JSON_BUILD_PAIR_STRING_NON_EMPTY_UNDERSCORIFY("llmnr", resolve_support_to_string(llmnr_support)), |
| 2162 |
JSON_BUILD_PAIR_STRING_NON_EMPTY_UNDERSCORIFY("mDNS", resolve_support_to_string(mdns_support)), |
| 2163 |
JSON_BUILD_PAIR_STRING_NON_EMPTY_UNDERSCORIFY("resolvConfMode", resolv_conf_mode_to_string(resolv_conf_mode)), |
| 2164 |
JSON_BUILD_PAIR_VARIANT_NON_NULL("scopes", scopes_json)); |
| 2165 |
} |
| 2166 |
|
| 2167 |
static int global_dns_configuration_json_append(Manager *m, sd_json_variant **configuration) { |
| 2168 |
_cleanup_set_free_ Set *scopes = NULL; |
| 2169 |
int r; |
| 2170 |
|
| 2171 |
assert(m); |
| 2172 |
assert(configuration); |
| 2173 |
|
| 2174 |
r = set_ensure_put(&scopes, NULL, m->unicast_scope); |
| 2175 |
if (r < 0) |
| 2176 |
return r; |
| 2177 |
|
| 2178 |
return dns_configuration_json_append( |
| 2179 |
/* ifname= */ NULL, |
| 2180 |
/* ifindex= */ 0, |
| 2181 |
/* delegate= */ NULL, |
| 2182 |
/* default_route= */ -1, |
| 2183 |
manager_get_dns_server(m), |
| 2184 |
m->dns_servers, |
| 2185 |
m->fallback_dns_servers, |
| 2186 |
m->search_domains, |
| 2187 |
m->trust_anchor.negative_by_name, |
| 2188 |
scopes, |
| 2189 |
manager_get_dnssec_mode(m), |
| 2190 |
manager_dnssec_supported(m), |
| 2191 |
manager_get_dns_over_tls_mode(m), |
| 2192 |
m->llmnr_support, |
| 2193 |
m->mdns_support, |
| 2194 |
resolv_conf_mode(), |
| 2195 |
configuration); |
| 2196 |
} |
| 2197 |
|
| 2198 |
static int link_dns_configuration_json_append(Link *l, sd_json_variant **configuration) { |
| 2199 |
_cleanup_set_free_ Set *scopes = NULL; |
| 2200 |
int r; |
| 2201 |
|
| 2202 |
assert(l); |
| 2203 |
assert(configuration); |
| 2204 |
|
| 2205 |
if (l->unicast_scope) { |
| 2206 |
r = set_ensure_put(&scopes, NULL, l->unicast_scope); |
| 2207 |
if (r < 0) |
| 2208 |
return r; |
| 2209 |
} |
| 2210 |
|
| 2211 |
if (l->llmnr_ipv4_scope) { |
| 2212 |
r = set_ensure_put(&scopes, NULL, l->llmnr_ipv4_scope); |
| 2213 |
if (r < 0) |
| 2214 |
return r; |
| 2215 |
} |
| 2216 |
|
| 2217 |
if (l->llmnr_ipv6_scope) { |
| 2218 |
r = set_ensure_put(&scopes, NULL, l->llmnr_ipv6_scope); |
| 2219 |
if (r < 0) |
| 2220 |
return r; |
| 2221 |
} |
| 2222 |
|
| 2223 |
if (l->mdns_ipv4_scope) { |
| 2224 |
r = set_ensure_put(&scopes, NULL, l->mdns_ipv4_scope); |
| 2225 |
if (r < 0) |
| 2226 |
return r; |
| 2227 |
} |
| 2228 |
|
| 2229 |
if (l->mdns_ipv6_scope) { |
| 2230 |
r = set_ensure_put(&scopes, NULL, l->mdns_ipv6_scope); |
| 2231 |
if (r < 0) |
| 2232 |
return r; |
| 2233 |
} |
| 2234 |
|
| 2235 |
return dns_configuration_json_append( |
| 2236 |
l->ifname, |
| 2237 |
l->ifindex, |
| 2238 |
/* delegate= */ NULL, |
| 2239 |
link_get_default_route(l), |
| 2240 |
link_get_dns_server(l), |
| 2241 |
l->dns_servers, |
| 2242 |
/* fallback_dns_servers= */ NULL, |
| 2243 |
l->search_domains, |
| 2244 |
l->dnssec_negative_trust_anchors, |
| 2245 |
scopes, |
| 2246 |
link_get_dnssec_mode(l), |
| 2247 |
link_dnssec_supported(l), |
| 2248 |
link_get_dns_over_tls_mode(l), |
| 2249 |
link_get_llmnr_support(l), |
| 2250 |
link_get_mdns_support(l), |
| 2251 |
/* resolv_conf_mode= */ _RESOLV_CONF_MODE_INVALID, |
| 2252 |
configuration); |
| 2253 |
} |
| 2254 |
|
| 2255 |
static int delegate_dns_configuration_json_append(DnsDelegate *d, sd_json_variant **configuration) { |
| 2256 |
_cleanup_set_free_ Set *scopes = NULL; |
| 2257 |
int r; |
| 2258 |
|
| 2259 |
assert(d); |
| 2260 |
assert(configuration); |
| 2261 |
|
| 2262 |
r = set_ensure_put(&scopes, NULL, d->scope); |
| 2263 |
if (r < 0) |
| 2264 |
return r; |
| 2265 |
|
| 2266 |
return dns_configuration_json_append( |
| 2267 |
/* ifname= */ NULL, |
| 2268 |
/* ifindex= */ 0, |
| 2269 |
d->id, |
| 2270 |
d->default_route > 0, /* Defaults to false. See dns_scope_is_default_route(). */ |
| 2271 |
dns_delegate_get_dns_server(d), |
| 2272 |
d->dns_servers, |
| 2273 |
/* fallback_dns_servers= */ NULL, |
| 2274 |
d->search_domains, |
| 2275 |
/* negative_trust_anchors= */ NULL, |
| 2276 |
scopes, |
| 2277 |
/* dnssec_mode= */ _DNSSEC_MODE_INVALID, |
| 2278 |
/* dnssec_supported= */ false, |
| 2279 |
/* dns_over_tls_mode= */ _DNS_OVER_TLS_MODE_INVALID, |
| 2280 |
/* llmnr_support= */ _RESOLVE_SUPPORT_INVALID, |
| 2281 |
/* mdns_support= */ _RESOLVE_SUPPORT_INVALID, |
| 2282 |
/* resolv_conf_mode= */ _RESOLV_CONF_MODE_INVALID, |
| 2283 |
configuration); |
| 2284 |
} |
| 2285 |
|
| 2286 |
int manager_dump_dns_configuration_json(Manager *m, sd_json_variant **ret) { |
| 2287 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *configuration = NULL; |
| 2288 |
Link *l; |
| 2289 |
DnsDelegate *d; |
| 2290 |
int r; |
| 2291 |
|
| 2292 |
assert(m); |
| 2293 |
assert(ret); |
| 2294 |
|
| 2295 |
/* Global DNS configuration */ |
| 2296 |
r = global_dns_configuration_json_append(m, &configuration); |
| 2297 |
if (r < 0) |
| 2298 |
return r; |
| 2299 |
|
| 2300 |
/* Append configuration for each link */ |
| 2301 |
HASHMAP_FOREACH(l, m->links) { |
| 2302 |
r = link_dns_configuration_json_append(l, &configuration); |
| 2303 |
if (r < 0) |
| 2304 |
return r; |
| 2305 |
} |
| 2306 |
|
| 2307 |
/* Append configuration for each delegate */ |
| 2308 |
HASHMAP_FOREACH(d, m->delegates) { |
| 2309 |
r = delegate_dns_configuration_json_append(d, &configuration); |
| 2310 |
if (r < 0) |
| 2311 |
return r; |
| 2312 |
} |
| 2313 |
|
| 2314 |
return sd_json_buildo(ret, SD_JSON_BUILD_PAIR_VARIANT("configuration", configuration)); |
| 2315 |
} |
| 2316 |
|
| 2317 |
int manager_send_dns_configuration_changed(Manager *m, Link *l, bool reset) { |
| 2318 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *configuration = NULL; |
| 2319 |
int r; |
| 2320 |
|
| 2321 |
assert(m); |
| 2322 |
|
| 2323 |
if (set_isempty(m->varlink_dns_configuration_subscription)) |
| 2324 |
return 0; |
| 2325 |
|
| 2326 |
if (reset) { |
| 2327 |
dns_server_reset_accessible_all(m->dns_servers); |
| 2328 |
|
| 2329 |
if (l) |
| 2330 |
dns_server_reset_accessible_all(l->dns_servers); |
| 2331 |
} |
| 2332 |
|
| 2333 |
r = manager_dump_dns_configuration_json(m, &configuration); |
| 2334 |
if (r < 0) |
| 2335 |
return log_warning_errno(r, "Failed to dump DNS configuration json: %m"); |
| 2336 |
|
| 2337 |
if (sd_json_variant_equal(configuration, m->dns_configuration_json)) |
| 2338 |
return 0; |
| 2339 |
|
| 2340 |
json_variant_unref_and_replace(m->dns_configuration_json, configuration); |
| 2341 |
|
| 2342 |
r = varlink_many_notify(m->varlink_dns_configuration_subscription, m->dns_configuration_json); |
| 2343 |
if (r < 0) |
| 2344 |
return log_warning_errno(r, "Failed to send DNS configuration event: %m"); |
| 2345 |
|
| 2346 |
return 0; |
| 2347 |
} |
| 2348 |
|
| 2349 |
int manager_start_dns_configuration_monitor(Manager *m) { |
| 2350 |
Link *l; |
| 2351 |
int r; |
| 2352 |
|
| 2353 |
assert(m); |
| 2354 |
assert(!m->dns_configuration_json); |
| 2355 |
assert(!m->netlink_new_route_slot); |
| 2356 |
assert(!m->netlink_del_route_slot); |
| 2357 |
|
| 2358 |
dns_server_reset_accessible_all(m->dns_servers); |
| 2359 |
|
| 2360 |
HASHMAP_FOREACH(l, m->links) |
| 2361 |
dns_server_reset_accessible_all(l->dns_servers); |
| 2362 |
|
| 2363 |
r = manager_dump_dns_configuration_json(m, &m->dns_configuration_json); |
| 2364 |
if (r < 0) |
| 2365 |
return r; |
| 2366 |
|
| 2367 |
r = sd_netlink_add_match(m->rtnl, &m->netlink_new_route_slot, RTM_NEWROUTE, manager_process_route, NULL, m, "resolve-NEWROUTE"); |
| 2368 |
if (r < 0) |
| 2369 |
return r; |
| 2370 |
|
| 2371 |
r = sd_netlink_add_match(m->rtnl, &m->netlink_del_route_slot, RTM_DELROUTE, manager_process_route, NULL, m, "resolve-DELROUTE"); |
| 2372 |
if (r < 0) |
| 2373 |
return r; |
| 2374 |
|
| 2375 |
return 0; |
| 2376 |
} |
| 2377 |
|
| 2378 |
void manager_stop_dns_configuration_monitor(Manager *m) { |
| 2379 |
assert(m); |
| 2380 |
|
| 2381 |
m->dns_configuration_json = sd_json_variant_unref(m->dns_configuration_json); |
| 2382 |
m->netlink_new_route_slot = sd_netlink_slot_unref(m->netlink_new_route_slot); |
| 2383 |
m->netlink_del_route_slot = sd_netlink_slot_unref(m->netlink_del_route_slot); |
| 2384 |
} |