1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 #include <netinet/in.h>
12 #include "alloc-util.h"
13 #include "bus-polkit.h"
14 #include "dirent-util.h"
15 #include "dns-domain.h"
16 #include "event-util.h"
19 #include "hostname-util.h"
22 #include "iovec-util.h"
23 #include "memstream-util.h"
24 #include "missing_network.h"
25 #include "missing_socket.h"
26 #include "netlink-util.h"
27 #include "ordered-set.h"
28 #include "parse-util.h"
29 #include "random-util.h"
30 #include "resolved-bus.h"
31 #include "resolved-conf.h"
32 #include "resolved-dns-stub.h"
33 #include "resolved-dnssd.h"
34 #include "resolved-etc-hosts.h"
35 #include "resolved-llmnr.h"
36 #include "resolved-manager.h"
37 #include "resolved-mdns.h"
38 #include "resolved-resolv-conf.h"
39 #include "resolved-util.h"
40 #include "resolved-varlink.h"
41 #include "socket-util.h"
42 #include "string-table.h"
43 #include "string-util.h"
46 #define SEND_TIMEOUT_USEC (200 * USEC_PER_MSEC)
48 static int manager_process_link(sd_netlink
*rtnl
, sd_netlink_message
*mm
, void *userdata
) {
49 Manager
*m
= ASSERT_PTR(userdata
);
57 r
= sd_netlink_message_get_type(mm
, &type
);
61 r
= sd_rtnl_message_link_get_ifindex(mm
, &ifindex
);
65 l
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
73 r
= link_new(m
, &l
, ifindex
);
78 r
= link_process_rtnl(l
, mm
);
87 log_debug("Found new link %i/%s", ifindex
, l
->ifname
);
94 log_debug("Removing link %i/%s", l
->ifindex
, l
->ifname
);
105 log_warning_errno(r
, "Failed to process RTNL link message: %m");
109 static int manager_process_address(sd_netlink
*rtnl
, sd_netlink_message
*mm
, void *userdata
) {
110 Manager
*m
= ASSERT_PTR(userdata
);
111 union in_addr_union address
, broadcast
= {};
113 int r
, ifindex
, family
;
120 r
= sd_netlink_message_get_type(mm
, &type
);
124 r
= sd_rtnl_message_addr_get_ifindex(mm
, &ifindex
);
128 l
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
132 r
= sd_rtnl_message_addr_get_family(mm
, &family
);
139 sd_netlink_message_read_in_addr(mm
, IFA_BROADCAST
, &broadcast
.in
);
140 r
= sd_netlink_message_read_in_addr(mm
, IFA_LOCAL
, &address
.in
);
142 r
= sd_netlink_message_read_in_addr(mm
, IFA_ADDRESS
, &address
.in
);
150 r
= sd_netlink_message_read_in6_addr(mm
, IFA_LOCAL
, &address
.in6
);
152 r
= sd_netlink_message_read_in6_addr(mm
, IFA_ADDRESS
, &address
.in6
);
163 a
= link_find_address(l
, family
, &address
);
170 r
= link_address_new(l
, &a
, family
, &address
, &broadcast
);
175 r
= link_address_update_rtnl(a
, mm
);
182 link_address_free(a
);
189 log_warning_errno(r
, "Failed to process RTNL address message: %m");
193 static int manager_rtnl_listen(Manager
*m
) {
194 _cleanup_(sd_netlink_message_unrefp
) sd_netlink_message
*req
= NULL
, *reply
= NULL
;
199 /* First, subscribe to interfaces coming and going */
200 r
= sd_netlink_open(&m
->rtnl
);
204 r
= sd_netlink_attach_event(m
->rtnl
, m
->event
, SD_EVENT_PRIORITY_IMPORTANT
);
208 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_NEWLINK
, manager_process_link
, NULL
, m
, "resolve-NEWLINK");
212 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_DELLINK
, manager_process_link
, NULL
, m
, "resolve-DELLINK");
216 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_NEWADDR
, manager_process_address
, NULL
, m
, "resolve-NEWADDR");
220 r
= sd_netlink_add_match(m
->rtnl
, NULL
, RTM_DELADDR
, manager_process_address
, NULL
, m
, "resolve-DELADDR");
224 /* Then, enumerate all links */
225 r
= sd_rtnl_message_new_link(m
->rtnl
, &req
, RTM_GETLINK
, 0);
229 r
= sd_netlink_message_set_request_dump(req
, true);
233 r
= sd_netlink_call(m
->rtnl
, req
, 0, &reply
);
237 for (sd_netlink_message
*i
= reply
; i
; i
= sd_netlink_message_next(i
)) {
238 r
= manager_process_link(m
->rtnl
, i
, m
);
243 req
= sd_netlink_message_unref(req
);
244 reply
= sd_netlink_message_unref(reply
);
246 /* Finally, enumerate all addresses, too */
247 r
= sd_rtnl_message_new_addr(m
->rtnl
, &req
, RTM_GETADDR
, 0, AF_UNSPEC
);
251 r
= sd_netlink_message_set_request_dump(req
, true);
255 r
= sd_netlink_call(m
->rtnl
, req
, 0, &reply
);
259 for (sd_netlink_message
*i
= reply
; i
; i
= sd_netlink_message_next(i
)) {
260 r
= manager_process_address(m
->rtnl
, i
, m
);
268 static int on_network_event(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
269 Manager
*m
= ASSERT_PTR(userdata
);
273 sd_network_monitor_flush(m
->network_monitor
);
275 HASHMAP_FOREACH(l
, m
->links
) {
278 log_warning_errno(r
, "Failed to update monitor information for %i: %m", l
->ifindex
);
281 (void) manager_write_resolv_conf(m
);
282 (void) manager_send_changed(m
, "DNS");
287 static int manager_network_monitor_listen(Manager
*m
) {
292 r
= sd_network_monitor_new(&m
->network_monitor
, NULL
);
296 fd
= sd_network_monitor_get_fd(m
->network_monitor
);
300 events
= sd_network_monitor_get_events(m
->network_monitor
);
304 r
= sd_event_add_io(m
->event
, &m
->network_event_source
, fd
, events
, &on_network_event
, m
);
308 r
= sd_event_source_set_priority(m
->network_event_source
, SD_EVENT_PRIORITY_IMPORTANT
+5);
312 (void) sd_event_source_set_description(m
->network_event_source
, "network-monitor");
317 static int manager_clock_change_listen(Manager
*m
);
319 static int on_clock_change(sd_event_source
*source
, int fd
, uint32_t revents
, void *userdata
) {
320 Manager
*m
= ASSERT_PTR(userdata
);
322 /* The clock has changed, let's flush all caches. Why that? That's because DNSSEC validation takes
323 * the system clock into consideration, and if the clock changes the old validations might have been
324 * wrong. Let's redo all validation with the new, correct time.
326 * (Also, this is triggered after system suspend, which is also a good reason to drop caches, since
327 * we might be connected to a different network now without this being visible in a dropped link
330 log_info("Clock change detected. Flushing caches.");
331 manager_flush_caches(m
, LOG_DEBUG
/* downgrade the functions own log message, since we already logged here at LOG_INFO level */);
333 /* The clock change timerfd is unusable after it triggered once, create a new one. */
334 return manager_clock_change_listen(m
);
337 static int manager_clock_change_listen(Manager
*m
) {
342 m
->clock_change_event_source
= sd_event_source_disable_unref(m
->clock_change_event_source
);
344 r
= event_add_time_change(m
->event
, &m
->clock_change_event_source
, on_clock_change
, m
);
346 return log_error_errno(r
, "Failed to create clock change event source: %m");
351 static int determine_hostnames(char **full_hostname
, char **llmnr_hostname
, char **mdns_hostname
) {
352 _cleanup_free_
char *h
= NULL
, *n
= NULL
;
355 assert(full_hostname
);
356 assert(llmnr_hostname
);
357 assert(mdns_hostname
);
359 r
= resolve_system_hostname(&h
, &n
);
363 r
= dns_name_concat(n
, "local", 0, mdns_hostname
);
365 return log_error_errno(r
, "Failed to determine mDNS hostname: %m");
367 *llmnr_hostname
= TAKE_PTR(n
);
368 *full_hostname
= TAKE_PTR(h
);
373 static char* fallback_hostname(void) {
375 /* Determine the fall back hostname. For exposing this system to the outside world, we cannot have it
376 * to be "localhost" even if that's the default hostname. In this case, let's revert to "linux"
379 _cleanup_free_
char *n
= get_default_hostname();
384 return strdup("linux");
389 static int make_fallback_hostnames(char **full_hostname
, char **llmnr_hostname
, char **mdns_hostname
) {
390 _cleanup_free_
char *h
= NULL
, *n
= NULL
, *m
= NULL
;
391 char label
[DNS_LABEL_MAX
+1];
395 assert(full_hostname
);
396 assert(llmnr_hostname
);
397 assert(mdns_hostname
);
399 p
= h
= fallback_hostname();
403 r
= dns_label_unescape(&p
, label
, sizeof label
, 0);
405 return log_error_errno(r
, "Failed to unescape fallback hostname: %m");
407 assert(r
> 0); /* The fallback hostname must have at least one label */
409 r
= dns_label_escape_new(label
, r
, &n
);
411 return log_error_errno(r
, "Failed to escape fallback hostname: %m");
413 r
= dns_name_concat(n
, "local", 0, &m
);
415 return log_error_errno(r
, "Failed to concatenate mDNS hostname: %m");
417 *llmnr_hostname
= TAKE_PTR(n
);
418 *mdns_hostname
= TAKE_PTR(m
);
419 *full_hostname
= TAKE_PTR(h
);
424 static int on_hostname_change(sd_event_source
*es
, int fd
, uint32_t revents
, void *userdata
) {
425 _cleanup_free_
char *full_hostname
= NULL
, *llmnr_hostname
= NULL
, *mdns_hostname
= NULL
;
426 Manager
*m
= ASSERT_PTR(userdata
);
427 bool llmnr_hostname_changed
;
430 r
= determine_hostnames(&full_hostname
, &llmnr_hostname
, &mdns_hostname
);
432 log_warning_errno(r
, "Failed to determine the local hostname and LLMNR/mDNS names, ignoring: %m");
433 return 0; /* ignore invalid hostnames */
436 llmnr_hostname_changed
= !streq(llmnr_hostname
, m
->llmnr_hostname
);
437 if (streq(full_hostname
, m
->full_hostname
) &&
438 !llmnr_hostname_changed
&&
439 streq(mdns_hostname
, m
->mdns_hostname
))
442 log_info("System hostname changed to '%s'.", full_hostname
);
444 free_and_replace(m
->full_hostname
, full_hostname
);
445 free_and_replace(m
->llmnr_hostname
, llmnr_hostname
);
446 free_and_replace(m
->mdns_hostname
, mdns_hostname
);
448 manager_refresh_rrs(m
);
449 (void) manager_send_changed(m
, "LLMNRHostname");
454 static int manager_watch_hostname(Manager
*m
) {
459 m
->hostname_fd
= open("/proc/sys/kernel/hostname",
460 O_RDONLY
|O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
);
461 if (m
->hostname_fd
< 0) {
462 log_warning_errno(errno
, "Failed to watch hostname: %m");
466 r
= sd_event_add_io(m
->event
, &m
->hostname_event_source
, m
->hostname_fd
, 0, on_hostname_change
, m
);
469 /* kernels prior to 3.2 don't support polling this file. Ignore the failure. */
470 m
->hostname_fd
= safe_close(m
->hostname_fd
);
472 return log_error_errno(r
, "Failed to add hostname event source: %m");
475 (void) sd_event_source_set_description(m
->hostname_event_source
, "hostname");
477 r
= determine_hostnames(&m
->full_hostname
, &m
->llmnr_hostname
, &m
->mdns_hostname
);
479 _cleanup_free_
char *d
= NULL
;
481 d
= fallback_hostname();
485 log_info("Defaulting to hostname '%s'.", d
);
487 r
= make_fallback_hostnames(&m
->full_hostname
, &m
->llmnr_hostname
, &m
->mdns_hostname
);
491 log_info("Using system hostname '%s'.", m
->full_hostname
);
496 static int manager_sigusr1(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
497 _cleanup_(memstream_done
) MemStream ms
= {};
498 Manager
*m
= ASSERT_PTR(userdata
);
505 f
= memstream_init(&ms
);
509 LIST_FOREACH(scopes
, scope
, m
->dns_scopes
)
510 dns_scope_dump(scope
, f
);
512 LIST_FOREACH(servers
, server
, m
->dns_servers
)
513 dns_server_dump(server
, f
);
514 LIST_FOREACH(servers
, server
, m
->fallback_dns_servers
)
515 dns_server_dump(server
, f
);
516 HASHMAP_FOREACH(l
, m
->links
)
517 LIST_FOREACH(servers
, server
, l
->dns_servers
)
518 dns_server_dump(server
, f
);
520 return memstream_dump(LOG_INFO
, &ms
);
523 static int manager_sigusr2(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
524 Manager
*m
= ASSERT_PTR(userdata
);
529 manager_flush_caches(m
, LOG_INFO
);
534 static int manager_sigrtmin1(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
535 Manager
*m
= ASSERT_PTR(userdata
);
540 manager_reset_server_features(m
);
544 static int manager_memory_pressure(sd_event_source
*s
, void *userdata
) {
545 Manager
*m
= ASSERT_PTR(userdata
);
547 log_info("Under memory pressure, flushing caches.");
549 manager_flush_caches(m
, LOG_INFO
);
550 sd_event_trim_memory();
555 static int manager_memory_pressure_listen(Manager
*m
) {
560 r
= sd_event_add_memory_pressure(m
->event
, NULL
, manager_memory_pressure
, m
);
562 log_full_errno(ERRNO_IS_NOT_SUPPORTED(r
) || ERRNO_IS_PRIVILEGE(r
) || (r
== -EHOSTDOWN
)? LOG_DEBUG
: LOG_NOTICE
, r
,
563 "Failed to install memory pressure event source, ignoring: %m");
568 int manager_new(Manager
**ret
) {
569 _cleanup_(manager_freep
) Manager
*m
= NULL
;
579 .llmnr_ipv4_udp_fd
= -EBADF
,
580 .llmnr_ipv6_udp_fd
= -EBADF
,
581 .llmnr_ipv4_tcp_fd
= -EBADF
,
582 .llmnr_ipv6_tcp_fd
= -EBADF
,
583 .mdns_ipv4_fd
= -EBADF
,
584 .mdns_ipv6_fd
= -EBADF
,
585 .hostname_fd
= -EBADF
,
587 .llmnr_support
= DEFAULT_LLMNR_MODE
,
588 .mdns_support
= DEFAULT_MDNS_MODE
,
589 .dnssec_mode
= DEFAULT_DNSSEC_MODE
,
590 .dns_over_tls_mode
= DEFAULT_DNS_OVER_TLS_MODE
,
591 .enable_cache
= DNS_CACHE_MODE_YES
,
592 .dns_stub_listener_mode
= DNS_STUB_LISTENER_YES
,
593 .read_resolv_conf
= true,
594 .need_builtin_fallbacks
= true,
595 .etc_hosts_last
= USEC_INFINITY
,
596 .read_etc_hosts
= true,
598 .sigrtmin18_info
.memory_pressure_handler
= manager_memory_pressure
,
599 .sigrtmin18_info
.memory_pressure_userdata
= m
,
602 r
= dns_trust_anchor_load(&m
->trust_anchor
);
606 r
= manager_parse_config_file(m
);
608 log_warning_errno(r
, "Failed to parse configuration file: %m");
610 #if ENABLE_DNS_OVER_TLS
611 r
= dnstls_manager_init(m
);
616 r
= sd_event_default(&m
->event
);
620 (void) sd_event_add_signal(m
->event
, NULL
, SIGTERM
, NULL
, NULL
);
621 (void) sd_event_add_signal(m
->event
, NULL
, SIGINT
, NULL
, NULL
);
623 (void) sd_event_set_watchdog(m
->event
, true);
625 r
= manager_watch_hostname(m
);
631 log_warning_errno(r
, "Failed to load DNS-SD configuration files: %m");
633 r
= dns_scope_new(m
, &m
->unicast_scope
, NULL
, DNS_PROTOCOL_DNS
, AF_UNSPEC
);
637 r
= manager_network_monitor_listen(m
);
641 r
= manager_rtnl_listen(m
);
645 r
= manager_clock_change_listen(m
);
649 r
= manager_memory_pressure_listen(m
);
653 r
= manager_connect_bus(m
);
657 (void) sd_event_add_signal(m
->event
, &m
->sigusr1_event_source
, SIGUSR1
, manager_sigusr1
, m
);
658 (void) sd_event_add_signal(m
->event
, &m
->sigusr2_event_source
, SIGUSR2
, manager_sigusr2
, m
);
659 (void) sd_event_add_signal(m
->event
, &m
->sigrtmin1_event_source
, SIGRTMIN
+1, manager_sigrtmin1
, m
);
660 (void) sd_event_add_signal(m
->event
, NULL
, SIGRTMIN
+18, sigrtmin18_handler
, &m
->sigrtmin18_info
);
662 manager_cleanup_saved_user(m
);
669 int manager_start(Manager
*m
) {
674 r
= manager_dns_stub_start(m
);
678 r
= manager_varlink_init(m
);
685 Manager
*manager_free(Manager
*m
) {
692 dns_server_unlink_all(m
->dns_servers
);
693 dns_server_unlink_all(m
->fallback_dns_servers
);
694 dns_search_domain_unlink_all(m
->search_domains
);
696 while ((l
= hashmap_first(m
->links
)))
699 while (m
->dns_queries
)
700 dns_query_free(m
->dns_queries
);
702 m
->stub_queries_by_packet
= hashmap_free(m
->stub_queries_by_packet
);
704 dns_scope_free(m
->unicast_scope
);
706 /* At this point only orphaned streams should remain. All others should have been freed already by their
708 while (m
->dns_streams
)
709 dns_stream_unref(m
->dns_streams
);
711 #if ENABLE_DNS_OVER_TLS
712 dnstls_manager_free(m
);
715 hashmap_free(m
->links
);
716 hashmap_free(m
->dns_transactions
);
718 sd_event_source_unref(m
->network_event_source
);
719 sd_network_monitor_unref(m
->network_monitor
);
721 sd_netlink_unref(m
->rtnl
);
722 sd_event_source_unref(m
->rtnl_event_source
);
723 sd_event_source_unref(m
->clock_change_event_source
);
725 manager_llmnr_stop(m
);
726 manager_mdns_stop(m
);
727 manager_dns_stub_stop(m
);
728 manager_varlink_done(m
);
730 manager_socket_graveyard_clear(m
);
732 ordered_set_free(m
->dns_extra_stub_listeners
);
734 hashmap_free(m
->polkit_registry
);
736 sd_bus_flush_close_unref(m
->bus
);
738 sd_event_source_unref(m
->sigusr1_event_source
);
739 sd_event_source_unref(m
->sigusr2_event_source
);
740 sd_event_source_unref(m
->sigrtmin1_event_source
);
742 dns_resource_key_unref(m
->llmnr_host_ipv4_key
);
743 dns_resource_key_unref(m
->llmnr_host_ipv6_key
);
744 dns_resource_key_unref(m
->mdns_host_ipv4_key
);
745 dns_resource_key_unref(m
->mdns_host_ipv6_key
);
747 sd_event_source_unref(m
->hostname_event_source
);
748 safe_close(m
->hostname_fd
);
750 sd_event_unref(m
->event
);
752 free(m
->full_hostname
);
753 free(m
->llmnr_hostname
);
754 free(m
->mdns_hostname
);
756 while ((s
= hashmap_first(m
->dnssd_services
)))
757 dnssd_service_free(s
);
758 hashmap_free(m
->dnssd_services
);
760 dns_trust_anchor_flush(&m
->trust_anchor
);
761 manager_etc_hosts_flush(m
);
766 int manager_recv(Manager
*m
, int fd
, DnsProtocol protocol
, DnsPacket
**ret
) {
767 _cleanup_(dns_packet_unrefp
) DnsPacket
*p
= NULL
;
768 CMSG_BUFFER_TYPE(CMSG_SPACE(MAXSIZE(struct in_pktinfo
, struct in6_pktinfo
))
769 + CMSG_SPACE(int) /* ttl/hoplimit */
770 + EXTRA_CMSG_SPACE
/* kernel appears to require extra buffer space */) control
;
771 union sockaddr_union sa
;
775 .msg_namelen
= sizeof(sa
),
778 .msg_control
= &control
,
779 .msg_controllen
= sizeof(control
),
781 struct cmsghdr
*cmsg
;
789 ms
= next_datagram_size_fd(fd
);
793 r
= dns_packet_new(&p
, protocol
, ms
, DNS_PACKET_SIZE_MAX
);
797 iov
= IOVEC_MAKE(DNS_PACKET_DATA(p
), p
->allocated
);
799 l
= recvmsg_safe(fd
, &mh
, 0);
800 if (ERRNO_IS_NEG_TRANSIENT(l
))
805 assert(!(mh
.msg_flags
& MSG_TRUNC
));
807 p
->size
= (size_t) l
;
809 p
->family
= sa
.sa
.sa_family
;
810 p
->ipproto
= IPPROTO_UDP
;
811 if (p
->family
== AF_INET
) {
812 p
->sender
.in
= sa
.in
.sin_addr
;
813 p
->sender_port
= be16toh(sa
.in
.sin_port
);
814 } else if (p
->family
== AF_INET6
) {
815 p
->sender
.in6
= sa
.in6
.sin6_addr
;
816 p
->sender_port
= be16toh(sa
.in6
.sin6_port
);
817 p
->ifindex
= sa
.in6
.sin6_scope_id
;
819 return -EAFNOSUPPORT
;
821 p
->timestamp
= now(CLOCK_BOOTTIME
);
823 CMSG_FOREACH(cmsg
, &mh
) {
825 if (cmsg
->cmsg_level
== IPPROTO_IPV6
) {
826 assert(p
->family
== AF_INET6
);
828 switch (cmsg
->cmsg_type
) {
831 struct in6_pktinfo
*i
= CMSG_TYPED_DATA(cmsg
, struct in6_pktinfo
);
834 p
->ifindex
= i
->ipi6_ifindex
;
836 p
->destination
.in6
= i
->ipi6_addr
;
841 p
->ttl
= *CMSG_TYPED_DATA(cmsg
, int);
844 case IPV6_RECVFRAGSIZE
:
845 p
->fragsize
= *CMSG_TYPED_DATA(cmsg
, int);
848 } else if (cmsg
->cmsg_level
== IPPROTO_IP
) {
849 assert(p
->family
== AF_INET
);
851 switch (cmsg
->cmsg_type
) {
854 struct in_pktinfo
*i
= CMSG_TYPED_DATA(cmsg
, struct in_pktinfo
);
857 p
->ifindex
= i
->ipi_ifindex
;
859 p
->destination
.in
= i
->ipi_addr
;
864 p
->ttl
= *CMSG_TYPED_DATA(cmsg
, int);
867 case IP_RECVFRAGSIZE
:
868 p
->fragsize
= *CMSG_TYPED_DATA(cmsg
, int);
874 /* The Linux kernel sets the interface index to the loopback
875 * device if the packet came from the local host since it
876 * avoids the routing table in such a case. Let's unset the
877 * interface index in such a case. */
878 if (p
->ifindex
== LOOPBACK_IFINDEX
)
881 if (protocol
!= DNS_PROTOCOL_DNS
) {
882 /* If we don't know the interface index still, we look for the
883 * first local interface with a matching address. Yuck! */
885 p
->ifindex
= manager_find_ifindex(m
, p
->family
, &p
->destination
);
888 log_debug("Received %s UDP packet of size %zu, ifindex=%i, ttl=%u, fragsize=%zu, sender=%s, destination=%s",
889 dns_protocol_to_string(protocol
), p
->size
, p
->ifindex
, p
->ttl
, p
->fragsize
,
890 IN_ADDR_TO_STRING(p
->family
, &p
->sender
),
891 IN_ADDR_TO_STRING(p
->family
, &p
->destination
));
897 static int sendmsg_loop(int fd
, struct msghdr
*mh
, int flags
) {
904 end
= usec_add(now(CLOCK_MONOTONIC
), SEND_TIMEOUT_USEC
);
907 if (sendmsg(fd
, mh
, flags
) >= 0)
914 r
= fd_wait_for_event(fd
, POLLOUT
, LESS_BY(end
, now(CLOCK_MONOTONIC
)));
915 if (ERRNO_IS_NEG_TRANSIENT(r
))
924 static int write_loop(int fd
, void *message
, size_t length
) {
931 end
= usec_add(now(CLOCK_MONOTONIC
), SEND_TIMEOUT_USEC
);
934 if (write(fd
, message
, length
) >= 0)
941 r
= fd_wait_for_event(fd
, POLLOUT
, LESS_BY(end
, now(CLOCK_MONOTONIC
)));
942 if (ERRNO_IS_NEG_TRANSIENT(r
))
951 int manager_write(Manager
*m
, int fd
, DnsPacket
*p
) {
954 log_debug("Sending %s%s packet with id %" PRIu16
" of size %zu.",
955 DNS_PACKET_TC(p
) ? "truncated (!) " : "",
956 DNS_PACKET_QR(p
) ? "response" : "query",
960 r
= write_loop(fd
, DNS_PACKET_DATA(p
), p
->size
);
967 static int manager_ipv4_send(
971 const struct in_addr
*destination
,
973 const struct in_addr
*source
,
976 CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct in_pktinfo
))) control
= {};
977 union sockaddr_union sa
;
983 .msg_namelen
= sizeof(sa
.in
),
992 iov
= IOVEC_MAKE(DNS_PACKET_DATA(p
), p
->size
);
994 sa
= (union sockaddr_union
) {
995 .in
.sin_family
= AF_INET
,
996 .in
.sin_addr
= *destination
,
997 .in
.sin_port
= htobe16(port
),
1001 struct cmsghdr
*cmsg
;
1002 struct in_pktinfo
*pi
;
1004 mh
.msg_control
= &control
;
1005 mh
.msg_controllen
= sizeof(control
);
1007 cmsg
= CMSG_FIRSTHDR(&mh
);
1008 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct in_pktinfo
));
1009 cmsg
->cmsg_level
= IPPROTO_IP
;
1010 cmsg
->cmsg_type
= IP_PKTINFO
;
1012 pi
= CMSG_TYPED_DATA(cmsg
, struct in_pktinfo
);
1013 pi
->ipi_ifindex
= ifindex
;
1016 pi
->ipi_spec_dst
= *source
;
1019 return sendmsg_loop(fd
, &mh
, 0);
1022 static int manager_ipv6_send(
1026 const struct in6_addr
*destination
,
1028 const struct in6_addr
*source
,
1031 CMSG_BUFFER_TYPE(CMSG_SPACE(sizeof(struct in6_pktinfo
))) control
= {};
1032 union sockaddr_union sa
;
1034 struct msghdr mh
= {
1038 .msg_namelen
= sizeof(sa
.in6
),
1043 assert(destination
);
1047 iov
= IOVEC_MAKE(DNS_PACKET_DATA(p
), p
->size
);
1049 sa
= (union sockaddr_union
) {
1050 .in6
.sin6_family
= AF_INET6
,
1051 .in6
.sin6_addr
= *destination
,
1052 .in6
.sin6_port
= htobe16(port
),
1053 .in6
.sin6_scope_id
= ifindex
,
1057 struct cmsghdr
*cmsg
;
1058 struct in6_pktinfo
*pi
;
1060 mh
.msg_control
= &control
;
1061 mh
.msg_controllen
= sizeof(control
);
1063 cmsg
= CMSG_FIRSTHDR(&mh
);
1064 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct in6_pktinfo
));
1065 cmsg
->cmsg_level
= IPPROTO_IPV6
;
1066 cmsg
->cmsg_type
= IPV6_PKTINFO
;
1068 pi
= CMSG_TYPED_DATA(cmsg
, struct in6_pktinfo
);
1069 pi
->ipi6_ifindex
= ifindex
;
1072 pi
->ipi6_addr
= *source
;
1075 return sendmsg_loop(fd
, &mh
, 0);
1078 static int dns_question_to_json(DnsQuestion
*q
, JsonVariant
**ret
) {
1079 _cleanup_(json_variant_unrefp
) JsonVariant
*l
= NULL
;
1080 DnsResourceKey
*key
;
1085 DNS_QUESTION_FOREACH(key
, q
) {
1086 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
1088 r
= dns_resource_key_to_json(key
, &v
);
1092 r
= json_variant_append_array(&l
, v
);
1101 int manager_monitor_send(
1106 DnsQuestion
*question_idna
,
1107 DnsQuestion
*question_utf8
,
1108 DnsPacket
*question_bypass
,
1109 DnsQuestion
*collected_questions
,
1110 DnsAnswer
*answer
) {
1112 _cleanup_(json_variant_unrefp
) JsonVariant
*jquestion
= NULL
, *jcollected_questions
= NULL
, *janswer
= NULL
;
1113 _cleanup_(dns_question_unrefp
) DnsQuestion
*merged
= NULL
;
1114 Varlink
*connection
;
1120 if (set_isempty(m
->varlink_subscription
))
1123 /* Merge all questions into one */
1124 r
= dns_question_merge(question_idna
, question_utf8
, &merged
);
1126 return log_error_errno(r
, "Failed to merge UTF8/IDNA questions: %m");
1128 if (question_bypass
) {
1129 _cleanup_(dns_question_unrefp
) DnsQuestion
*merged2
= NULL
;
1131 r
= dns_question_merge(merged
, question_bypass
->question
, &merged2
);
1133 return log_error_errno(r
, "Failed to merge UTF8/IDNA questions and DNS packet question: %m");
1135 dns_question_unref(merged
);
1136 merged
= TAKE_PTR(merged2
);
1139 /* Convert the current primary question to JSON */
1140 r
= dns_question_to_json(merged
, &jquestion
);
1142 return log_error_errno(r
, "Failed to convert question to JSON: %m");
1144 /* Generate a JSON array of the questions preceding the current one in the CNAME chain */
1145 r
= dns_question_to_json(collected_questions
, &jcollected_questions
);
1147 return log_error_errno(r
, "Failed to convert question to JSON: %m");
1149 DNS_ANSWER_FOREACH_ITEM(rri
, answer
) {
1150 _cleanup_(json_variant_unrefp
) JsonVariant
*v
= NULL
;
1152 r
= dns_resource_record_to_json(rri
->rr
, &v
);
1154 return log_error_errno(r
, "Failed to convert answer resource record to JSON: %m");
1156 r
= dns_resource_record_to_wire_format(rri
->rr
, /* canonical= */ false); /* don't use DNSSEC canonical format, since it removes casing, but we want that for DNS_SD compat */
1158 return log_error_errno(r
, "Failed to generate RR wire format: %m");
1160 r
= json_variant_append_arrayb(
1163 JSON_BUILD_PAIR_CONDITION(v
, "rr", JSON_BUILD_VARIANT(v
)),
1164 JSON_BUILD_PAIR("raw", JSON_BUILD_BASE64(rri
->rr
->wire_format
, rri
->rr
->wire_format_size
)),
1165 JSON_BUILD_PAIR_CONDITION(rri
->ifindex
> 0, "ifindex", JSON_BUILD_INTEGER(rri
->ifindex
))));
1167 return log_debug_errno(r
, "Failed to append notification entry to array: %m");
1170 SET_FOREACH(connection
, m
->varlink_subscription
) {
1171 r
= varlink_notifyb(connection
,
1172 JSON_BUILD_OBJECT(JSON_BUILD_PAIR("state", JSON_BUILD_STRING(dns_transaction_state_to_string(state
))),
1173 JSON_BUILD_PAIR_CONDITION(state
== DNS_TRANSACTION_RCODE_FAILURE
, "rcode", JSON_BUILD_INTEGER(rcode
)),
1174 JSON_BUILD_PAIR_CONDITION(state
== DNS_TRANSACTION_ERRNO
, "errno", JSON_BUILD_INTEGER(error
)),
1175 JSON_BUILD_PAIR("question", JSON_BUILD_VARIANT(jquestion
)),
1176 JSON_BUILD_PAIR_CONDITION(jcollected_questions
, "collectedQuestions", JSON_BUILD_VARIANT(jcollected_questions
)),
1177 JSON_BUILD_PAIR_CONDITION(janswer
, "answer", JSON_BUILD_VARIANT(janswer
))));
1179 log_debug_errno(r
, "Failed to send monitor event, ignoring: %m");
1190 const union in_addr_union
*destination
,
1192 const union in_addr_union
*source
,
1197 assert(destination
);
1201 /* For mDNS, it is natural that the packet have truncated flag when we have many known answers. */
1202 bool truncated
= DNS_PACKET_TC(p
) && (p
->protocol
!= DNS_PROTOCOL_MDNS
|| !p
->more
);
1204 log_debug("Sending %s%s packet with id %" PRIu16
" on interface %i/%s of size %zu.",
1205 truncated
? "truncated (!) " : "",
1206 DNS_PACKET_QR(p
) ? "response" : "query",
1208 ifindex
, af_to_name(family
),
1211 if (family
== AF_INET
)
1212 return manager_ipv4_send(m
, fd
, ifindex
, &destination
->in
, port
, source
? &source
->in
: NULL
, p
);
1213 if (family
== AF_INET6
)
1214 return manager_ipv6_send(m
, fd
, ifindex
, &destination
->in6
, port
, source
? &source
->in6
: NULL
, p
);
1216 return -EAFNOSUPPORT
;
1219 uint32_t manager_find_mtu(Manager
*m
) {
1223 /* If we don't know on which link a DNS packet would be delivered, let's find the largest MTU that
1224 * works on all interfaces we know of that have an IP address associated */
1226 HASHMAP_FOREACH(l
, m
->links
) {
1227 /* Let's filter out links without IP addresses (e.g. AF_CAN links and suchlike) */
1231 /* Safety check: MTU shorter than what we need for the absolutely shortest DNS request? Then
1232 * let's ignore this link. */
1233 if (l
->mtu
< MIN(UDP4_PACKET_HEADER_SIZE
+ DNS_PACKET_HEADER_SIZE
,
1234 UDP6_PACKET_HEADER_SIZE
+ DNS_PACKET_HEADER_SIZE
))
1237 if (mtu
<= 0 || l
->mtu
< mtu
)
1241 if (mtu
== 0) /* found nothing? then let's assume the typical Ethernet MTU for lack of anything more precise */
1247 int manager_find_ifindex(Manager
*m
, int family
, const union in_addr_union
*in_addr
) {
1252 if (!IN_SET(family
, AF_INET
, AF_INET6
))
1258 a
= manager_find_link_address(m
, family
, in_addr
);
1260 return a
->link
->ifindex
;
1265 void manager_refresh_rrs(Manager
*m
) {
1271 m
->llmnr_host_ipv4_key
= dns_resource_key_unref(m
->llmnr_host_ipv4_key
);
1272 m
->llmnr_host_ipv6_key
= dns_resource_key_unref(m
->llmnr_host_ipv6_key
);
1273 m
->mdns_host_ipv4_key
= dns_resource_key_unref(m
->mdns_host_ipv4_key
);
1274 m
->mdns_host_ipv6_key
= dns_resource_key_unref(m
->mdns_host_ipv6_key
);
1276 HASHMAP_FOREACH(l
, m
->links
)
1277 link_add_rrs(l
, true);
1279 if (m
->mdns_support
== RESOLVE_SUPPORT_YES
)
1280 HASHMAP_FOREACH(s
, m
->dnssd_services
)
1281 if (dnssd_update_rrs(s
) < 0)
1282 log_warning("Failed to refresh DNS-SD service '%s'", s
->name
);
1284 HASHMAP_FOREACH(l
, m
->links
)
1285 link_add_rrs(l
, false);
1288 static int manager_next_random_name(const char *old
, char **ret_new
) {
1297 if (!ascii_isdigit(p
[-1]))
1303 if (*p
== 0 || safe_atou64(p
, &u
) < 0 || u
<= 0)
1306 /* Add a random number to the old value. This way we can avoid
1307 * that two hosts pick the same hostname, win on IPv4 and lose
1308 * on IPv6 (or vice versa), and pick the same hostname
1309 * replacement hostname, ad infinitum. We still want the
1310 * numbers to go up monotonically, hence we just add a random
1313 random_bytes(&a
, sizeof(a
));
1316 if (asprintf(&n
, "%.*s%" PRIu64
, (int) (p
- old
), old
, u
) < 0)
1324 int manager_next_hostname(Manager
*m
) {
1325 _cleanup_free_
char *h
= NULL
, *k
= NULL
;
1330 r
= manager_next_random_name(m
->llmnr_hostname
, &h
);
1334 r
= dns_name_concat(h
, "local", 0, &k
);
1338 log_info("Hostname conflict, changing published hostname from '%s' to '%s'.", m
->llmnr_hostname
, h
);
1340 free_and_replace(m
->llmnr_hostname
, h
);
1341 free_and_replace(m
->mdns_hostname
, k
);
1343 manager_refresh_rrs(m
);
1344 (void) manager_send_changed(m
, "LLMNRHostname");
1349 LinkAddress
* manager_find_link_address(Manager
*m
, int family
, const union in_addr_union
*in_addr
) {
1354 if (!IN_SET(family
, AF_INET
, AF_INET6
))
1360 HASHMAP_FOREACH(l
, m
->links
) {
1363 a
= link_find_address(l
, family
, in_addr
);
1371 bool manager_packet_from_local_address(Manager
*m
, DnsPacket
*p
) {
1375 /* Let's see if this packet comes from an IP address we have on any local interface */
1377 return !!manager_find_link_address(m
, p
->family
, &p
->sender
);
1380 bool manager_packet_from_our_transaction(Manager
*m
, DnsPacket
*p
) {
1386 /* Let's see if we have a transaction with a query message with the exact same binary contents as the
1387 * one we just got. If so, it's almost definitely a packet loop of some kind. */
1389 t
= hashmap_get(m
->dns_transactions
, UINT_TO_PTR(DNS_PACKET_ID(p
)));
1393 return t
->sent
&& dns_packet_equal(t
->sent
, p
);
1396 DnsScope
* manager_find_scope(Manager
*m
, DnsPacket
*p
) {
1402 l
= hashmap_get(m
->links
, INT_TO_PTR(p
->ifindex
));
1406 switch (p
->protocol
) {
1407 case DNS_PROTOCOL_LLMNR
:
1408 if (p
->family
== AF_INET
)
1409 return l
->llmnr_ipv4_scope
;
1410 else if (p
->family
== AF_INET6
)
1411 return l
->llmnr_ipv6_scope
;
1415 case DNS_PROTOCOL_MDNS
:
1416 if (p
->family
== AF_INET
)
1417 return l
->mdns_ipv4_scope
;
1418 else if (p
->family
== AF_INET6
)
1419 return l
->mdns_ipv6_scope
;
1430 void manager_verify_all(Manager
*m
) {
1433 LIST_FOREACH(scopes
, s
, m
->dns_scopes
)
1434 dns_zone_verify_all(&s
->zone
);
1437 int manager_is_own_hostname(Manager
*m
, const char *name
) {
1443 if (m
->llmnr_hostname
) {
1444 r
= dns_name_equal(name
, m
->llmnr_hostname
);
1449 if (m
->mdns_hostname
) {
1450 r
= dns_name_equal(name
, m
->mdns_hostname
);
1455 if (m
->full_hostname
)
1456 return dns_name_equal(name
, m
->full_hostname
);
1461 int manager_compile_dns_servers(Manager
*m
, OrderedSet
**dns
) {
1468 r
= ordered_set_ensure_allocated(dns
, &dns_server_hash_ops
);
1472 /* First add the system-wide servers and domains */
1473 LIST_FOREACH(servers
, s
, m
->dns_servers
) {
1474 r
= ordered_set_put(*dns
, s
);
1481 /* Then, add the per-link servers */
1482 HASHMAP_FOREACH(l
, m
->links
) {
1483 LIST_FOREACH(servers
, s
, l
->dns_servers
) {
1484 r
= ordered_set_put(*dns
, s
);
1492 /* If we found nothing, add the fallback servers */
1493 if (ordered_set_isempty(*dns
)) {
1494 LIST_FOREACH(servers
, s
, m
->fallback_dns_servers
) {
1495 r
= ordered_set_put(*dns
, s
);
1506 /* filter_route is a tri-state:
1508 * = 0 or false: return only domains which should be used for searching
1509 * > 0 or true: return only domains which are for routing only
1511 int manager_compile_search_domains(Manager
*m
, OrderedSet
**domains
, int filter_route
) {
1518 r
= ordered_set_ensure_allocated(domains
, &dns_name_hash_ops
);
1522 LIST_FOREACH(domains
, d
, m
->search_domains
) {
1524 if (filter_route
>= 0 &&
1525 d
->route_only
!= !!filter_route
)
1528 r
= ordered_set_put(*domains
, d
->name
);
1535 HASHMAP_FOREACH(l
, m
->links
) {
1537 LIST_FOREACH(domains
, d
, l
->search_domains
) {
1539 if (filter_route
>= 0 &&
1540 d
->route_only
!= !!filter_route
)
1543 r
= ordered_set_put(*domains
, d
->name
);
1554 DnssecMode
manager_get_dnssec_mode(Manager
*m
) {
1557 if (m
->dnssec_mode
!= _DNSSEC_MODE_INVALID
)
1558 return m
->dnssec_mode
;
1563 bool manager_dnssec_supported(Manager
*m
) {
1569 if (manager_get_dnssec_mode(m
) == DNSSEC_NO
)
1572 server
= manager_get_dns_server(m
);
1573 if (server
&& !dns_server_dnssec_supported(server
))
1576 HASHMAP_FOREACH(l
, m
->links
)
1577 if (!link_dnssec_supported(l
))
1583 DnsOverTlsMode
manager_get_dns_over_tls_mode(Manager
*m
) {
1586 if (m
->dns_over_tls_mode
!= _DNS_OVER_TLS_MODE_INVALID
)
1587 return m
->dns_over_tls_mode
;
1589 return DNS_OVER_TLS_NO
;
1592 void manager_dnssec_verdict(Manager
*m
, DnssecVerdict verdict
, const DnsResourceKey
*key
) {
1594 assert(verdict
>= 0);
1595 assert(verdict
< _DNSSEC_VERDICT_MAX
);
1597 if (DEBUG_LOGGING
) {
1598 char s
[DNS_RESOURCE_KEY_STRING_MAX
];
1600 log_debug("Found verdict for lookup %s: %s",
1601 dns_resource_key_to_string(key
, s
, sizeof s
),
1602 dnssec_verdict_to_string(verdict
));
1605 m
->n_dnssec_verdict
[verdict
]++;
1608 bool manager_routable(Manager
*m
) {
1613 /* Returns true if the host has at least one interface with a routable address (regardless if IPv4 or IPv6) */
1615 HASHMAP_FOREACH(l
, m
->links
)
1616 if (link_relevant(l
, AF_UNSPEC
, false))
1622 void manager_flush_caches(Manager
*m
, int log_level
) {
1625 LIST_FOREACH(scopes
, scope
, m
->dns_scopes
)
1626 dns_cache_flush(&scope
->cache
);
1628 log_full(log_level
, "Flushed all caches.");
1631 void manager_reset_server_features(Manager
*m
) {
1634 dns_server_reset_features_all(m
->dns_servers
);
1635 dns_server_reset_features_all(m
->fallback_dns_servers
);
1637 HASHMAP_FOREACH(l
, m
->links
)
1638 dns_server_reset_features_all(l
->dns_servers
);
1640 log_info("Resetting learnt feature levels on all servers.");
1643 void manager_cleanup_saved_user(Manager
*m
) {
1644 _cleanup_closedir_
DIR *d
= NULL
;
1648 /* Clean up all saved per-link files in /run/systemd/resolve/netif/ that don't have a matching interface
1649 * anymore. These files are created to persist settings pushed in by the user via the bus, so that resolved can
1650 * be restarted without losing this data. */
1652 d
= opendir("/run/systemd/resolve/netif/");
1654 if (errno
== ENOENT
)
1657 log_warning_errno(errno
, "Failed to open interface directory: %m");
1661 FOREACH_DIRENT_ALL(de
, d
, log_error_errno(errno
, "Failed to read interface directory: %m")) {
1662 _cleanup_free_
char *p
= NULL
;
1666 if (!IN_SET(de
->d_type
, DT_UNKNOWN
, DT_REG
))
1669 if (dot_or_dot_dot(de
->d_name
))
1672 ifindex
= parse_ifindex(de
->d_name
);
1673 if (ifindex
< 0) /* Probably some temporary file from a previous run. Delete it */
1676 l
= hashmap_get(m
->links
, INT_TO_PTR(ifindex
));
1677 if (!l
) /* link vanished */
1680 if (l
->is_managed
) /* now managed by networkd, hence the bus settings are useless */
1686 p
= path_join("/run/systemd/resolve/netif", de
->d_name
);
1696 bool manager_next_dnssd_names(Manager
*m
) {
1703 HASHMAP_FOREACH(s
, m
->dnssd_services
) {
1704 _cleanup_free_
char * new_name
= NULL
;
1709 r
= manager_next_random_name(s
->name_template
, &new_name
);
1711 log_warning_errno(r
, "Failed to get new name for service '%s': %m", s
->name
);
1715 free_and_replace(s
->name_template
, new_name
);
1717 s
->withdrawn
= false;
1723 manager_refresh_rrs(m
);
1728 bool manager_server_is_stub(Manager
*m
, DnsServer
*s
) {
1729 DnsStubListenerExtra
*l
;
1734 /* Safety check: we generally already skip the main stub when parsing configuration. But let's be
1735 * extra careful, and check here again */
1736 if (s
->family
== AF_INET
&&
1737 s
->address
.in
.s_addr
== htobe32(INADDR_DNS_STUB
) &&
1738 dns_server_port(s
) == 53)
1741 /* Main reason to call this is to check server data against the extra listeners, and filter things
1743 ORDERED_SET_FOREACH(l
, m
->dns_extra_stub_listeners
)
1744 if (s
->family
== l
->family
&&
1745 in_addr_equal(s
->family
, &s
->address
, &l
->address
) &&
1746 dns_server_port(s
) == dns_stub_listener_extra_port(l
))
1752 int socket_disable_pmtud(int fd
, int af
) {
1757 if (af
== AF_UNSPEC
) {
1758 af
= socket_get_family(fd
);
1766 /* Turn off path MTU discovery, let's rather fragment on the way than to open us up against
1767 * PMTU forgery vulnerabilities.
1769 * There appears to be no documentation about IP_PMTUDISC_OMIT, but it has the effect that
1770 * the "Don't Fragment" bit in the IPv4 header is turned off, thus enforcing fragmentation if
1771 * our datagram size exceeds the MTU of a router in the path, and turning off path MTU
1774 * This helps mitigating the PMTUD vulnerability described here:
1776 * https://blog.apnic.net/2019/07/12/its-time-to-consider-avoiding-ip-fragmentation-in-the-dns/
1778 * Similar logic is in place in most DNS servers.
1780 * There are multiple conflicting goals: we want to allow the largest datagrams possible (for
1781 * efficiency reasons), but not have fragmentation (for security reasons), nor use PMTUD (for
1782 * security reasons, too). Our strategy to deal with this is: use large packets, turn off
1783 * PMTUD, but watch fragmentation taking place, and then size our packets to the max of the
1784 * fragments seen — and if we need larger packets always go to TCP.
1787 r
= setsockopt_int(fd
, IPPROTO_IP
, IP_MTU_DISCOVER
, IP_PMTUDISC_OMIT
);
1795 /* On IPv6 fragmentation only is done by the sender — never by routers on the path. PMTUD is
1796 * mandatory. If we want to turn off PMTUD, the only way is by sending with minimal MTU only,
1797 * so that we apply maximum fragmentation locally already, and thus PMTUD doesn't happen
1798 * because there's nothing that could be fragmented further anymore. */
1800 r
= setsockopt_int(fd
, IPPROTO_IPV6
, IPV6_MTU
, IPV6_MIN_MTU
);
1808 return -EAFNOSUPPORT
;
1812 int dns_manager_dump_statistics_json(Manager
*m
, JsonVariant
**ret
) {
1813 uint64_t size
= 0, hit
= 0, miss
= 0;
1818 LIST_FOREACH(scopes
, s
, m
->dns_scopes
) {
1819 size
+= dns_cache_size(&s
->cache
);
1820 hit
+= s
->cache
.n_hit
;
1821 miss
+= s
->cache
.n_miss
;
1824 return json_build(ret
,
1826 JSON_BUILD_PAIR("transactions", JSON_BUILD_OBJECT(
1827 JSON_BUILD_PAIR_UNSIGNED("currentTransactions", hashmap_size(m
->dns_transactions
)),
1828 JSON_BUILD_PAIR_UNSIGNED("totalTransactions", m
->n_transactions_total
),
1829 JSON_BUILD_PAIR_UNSIGNED("totalTimeouts", m
->n_timeouts_total
),
1830 JSON_BUILD_PAIR_UNSIGNED("totalTimeoutsServedStale", m
->n_timeouts_served_stale_total
),
1831 JSON_BUILD_PAIR_UNSIGNED("totalFailedResponses", m
->n_failure_responses_total
),
1832 JSON_BUILD_PAIR_UNSIGNED("totalFailedResponsesServedStale", m
->n_failure_responses_served_stale_total
)
1834 JSON_BUILD_PAIR("cache", JSON_BUILD_OBJECT(
1835 JSON_BUILD_PAIR_UNSIGNED("size", size
),
1836 JSON_BUILD_PAIR_UNSIGNED("hits", hit
),
1837 JSON_BUILD_PAIR_UNSIGNED("misses", miss
)
1839 JSON_BUILD_PAIR("dnssec", JSON_BUILD_OBJECT(
1840 JSON_BUILD_PAIR_UNSIGNED("secure", m
->n_dnssec_verdict
[DNSSEC_SECURE
]),
1841 JSON_BUILD_PAIR_UNSIGNED("insecure", m
->n_dnssec_verdict
[DNSSEC_INSECURE
]),
1842 JSON_BUILD_PAIR_UNSIGNED("bogus", m
->n_dnssec_verdict
[DNSSEC_BOGUS
]),
1843 JSON_BUILD_PAIR_UNSIGNED("indeterminate", m
->n_dnssec_verdict
[DNSSEC_INDETERMINATE
])
1847 void dns_manager_reset_statistics(Manager
*m
) {
1851 LIST_FOREACH(scopes
, s
, m
->dns_scopes
)
1852 s
->cache
.n_hit
= s
->cache
.n_miss
= 0;
1854 m
->n_transactions_total
= 0;
1855 m
->n_timeouts_total
= 0;
1856 m
->n_timeouts_served_stale_total
= 0;
1857 m
->n_failure_responses_total
= 0;
1858 m
->n_failure_responses_served_stale_total
= 0;
1859 zero(m
->n_dnssec_verdict
);