HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/resolve/resolved-manager.c

82,564 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 529912ff5267cc02c1e62a798e59c89544d7d165

Lines 2001–2387 of 2,387

                        return r;
 
                return 0;
        }
 
        default:
                return -EAFNOSUPPORT;
        }
}
 
int dns_manager_dump_statistics_json(Manager *m, sd_json_variant **ret) {
        uint64_t size = 0, hit = 0, miss = 0;
 
        assert(m);
        assert(ret);
 
        LIST_FOREACH(scopes, s, m->dns_scopes) {
                size += dns_cache_size(&s->cache);
                hit += s->cache.n_hit;
                miss += s->cache.n_miss;
        }
 
        return sd_json_buildo(ret,
                              SD_JSON_BUILD_PAIR("transactions", SD_JSON_BUILD_OBJECT(
                                                                 SD_JSON_BUILD_PAIR_UNSIGNED("currentTransactions", hashmap_size(m->dns_transactions)),
                                                                 SD_JSON_BUILD_PAIR_UNSIGNED("totalTransactions", m->n_transactions_total),
                                                                 SD_JSON_BUILD_PAIR_UNSIGNED("totalTimeouts", m->n_timeouts_total),
                                                                 SD_JSON_BUILD_PAIR_UNSIGNED("totalTimeoutsServedStale", m->n_timeouts_served_stale_total),
                                                                 SD_JSON_BUILD_PAIR_UNSIGNED("totalFailedResponses", m->n_failure_responses_total),
                                                                 SD_JSON_BUILD_PAIR_UNSIGNED("totalFailedResponsesServedStale", m->n_failure_responses_served_stale_total)
                                                 )),
                              SD_JSON_BUILD_PAIR("cache", SD_JSON_BUILD_OBJECT(
                                                                 SD_JSON_BUILD_PAIR_UNSIGNED("size", size),
                                                                 SD_JSON_BUILD_PAIR_UNSIGNED("hits", hit),
                                                                 SD_JSON_BUILD_PAIR_UNSIGNED("misses", miss)
                                                 )),
                              SD_JSON_BUILD_PAIR("dnssec", SD_JSON_BUILD_OBJECT(
                                                                 SD_JSON_BUILD_PAIR_UNSIGNED("secure", m->n_dnssec_verdict[DNSSEC_SECURE]),
                                                                 SD_JSON_BUILD_PAIR_UNSIGNED("insecure", m->n_dnssec_verdict[DNSSEC_INSECURE]),
                                                                 SD_JSON_BUILD_PAIR_UNSIGNED("bogus", m->n_dnssec_verdict[DNSSEC_BOGUS]),
                                                                 SD_JSON_BUILD_PAIR_UNSIGNED("indeterminate", m->n_dnssec_verdict[DNSSEC_INDETERMINATE])
                                                 )));
}
 
void dns_manager_reset_statistics(Manager *m) {
 
        assert(m);
 
        LIST_FOREACH(scopes, s, m->dns_scopes)
                s->cache.n_hit = s->cache.n_miss = 0;
 
        m->n_transactions_total = 0;
        m->n_timeouts_total = 0;
        m->n_timeouts_served_stale_total = 0;
        m->n_failure_responses_total = 0;
        m->n_failure_responses_served_stale_total = 0;
        zero(m->n_dnssec_verdict);
}
 
static int dns_configuration_json_append(
                const char *ifname,
                int ifindex,
                const char *delegate,
                int default_route,
                DnsServer *current_dns_server,
                DnsServer *dns_servers,
                DnsServer *fallback_dns_servers,
                DnsSearchDomain *search_domains,
                Set *negative_trust_anchors,
                Set *dns_scopes,
                DnssecMode dnssec_mode,
                bool dnssec_supported,
                DnsOverTlsMode dns_over_tls_mode,
                ResolveSupport llmnr_support,
                ResolveSupport mdns_support,
                ResolvConfMode resolv_conf_mode,
                sd_json_variant **configuration) {
 
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *dns_servers_json = NULL,
                                                          *fallback_dns_servers_json = NULL,
                                                          *search_domains_json = NULL,
                                                          *current_dns_server_json = NULL,
                                                          *scopes_json = NULL;
        DnsScope *scope;
        int r;
 
        assert(configuration);
 
        if (current_dns_server) {
                r = dns_server_dump_configuration_to_json(current_dns_server, &current_dns_server_json);
                if (r < 0)
                        return r;
        }
 
        SET_FOREACH(scope, dns_scopes) {
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
 
                r = dns_scope_to_json(scope, /* with_cache= */ false, &v);
                if (r < 0)
                        return r;
 
                r = sd_json_variant_append_array(&scopes_json, v);
                if (r < 0)
                        return r;
        }
 
        LIST_FOREACH(servers, s, dns_servers) {
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
 
                r = dns_server_dump_configuration_to_json(s, &v);
                if (r < 0)
                        return r;
 
                r = sd_json_variant_append_array(&dns_servers_json, v);
                if (r < 0)
                        return r;
        }
 
        LIST_FOREACH(domains, d, search_domains) {
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
 
                r = dns_search_domain_dump_to_json(d, &v);
                if (r < 0)
                        return r;
 
                r = sd_json_variant_append_array(&search_domains_json, v);
                if (r < 0)
                        return r;
        }
 
        LIST_FOREACH(servers, s, fallback_dns_servers) {
                _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
 
                r = dns_server_dump_configuration_to_json(s, &v);
                if (r < 0)
                        return r;
 
                r = sd_json_variant_append_array(&fallback_dns_servers_json, v);
                if (r < 0)
                        return r;
        }
 
        return sd_json_variant_append_arraybo(
                        configuration,
                        JSON_BUILD_PAIR_STRING_NON_EMPTY("ifname", ifname),
                        SD_JSON_BUILD_PAIR_CONDITION(ifindex > 0, "ifindex", SD_JSON_BUILD_UNSIGNED(ifindex)),
                        JSON_BUILD_PAIR_STRING_NON_EMPTY("delegate", delegate),
                        JSON_BUILD_PAIR_TRISTATE_NON_NULL("defaultRoute", default_route),
                        JSON_BUILD_PAIR_VARIANT_NON_NULL("currentServer", current_dns_server_json),
                        JSON_BUILD_PAIR_VARIANT_NON_NULL("servers", dns_servers_json),
                        JSON_BUILD_PAIR_VARIANT_NON_NULL("fallbackServers", fallback_dns_servers_json),
                        JSON_BUILD_PAIR_VARIANT_NON_NULL("searchDomains", search_domains_json),
                        SD_JSON_BUILD_PAIR_CONDITION(!set_isempty(negative_trust_anchors),
                                                     "negativeTrustAnchors",
                                                     JSON_BUILD_STRING_SET(negative_trust_anchors)),
                        JSON_BUILD_PAIR_CONDITION_BOOLEAN(dnssec_mode >= 0, "dnssecSupported", dnssec_supported),
                        JSON_BUILD_PAIR_STRING_NON_EMPTY("dnssec", dnssec_mode_to_string(dnssec_mode)),
                        JSON_BUILD_PAIR_STRING_NON_EMPTY("dnsOverTLS", dns_over_tls_mode_to_string(dns_over_tls_mode)),
                        JSON_BUILD_PAIR_STRING_NON_EMPTY_UNDERSCORIFY("llmnr", resolve_support_to_string(llmnr_support)),
                        JSON_BUILD_PAIR_STRING_NON_EMPTY_UNDERSCORIFY("mDNS", resolve_support_to_string(mdns_support)),
                        JSON_BUILD_PAIR_STRING_NON_EMPTY_UNDERSCORIFY("resolvConfMode", resolv_conf_mode_to_string(resolv_conf_mode)),
                        JSON_BUILD_PAIR_VARIANT_NON_NULL("scopes", scopes_json));
}
 
static int global_dns_configuration_json_append(Manager *m, sd_json_variant **configuration) {
        _cleanup_set_free_ Set *scopes = NULL;
        int r;
 
        assert(m);
        assert(configuration);
 
        r = set_ensure_put(&scopes, NULL, m->unicast_scope);
        if (r < 0)
                return r;
 
        /* This may re-read /etc/resolv.conf and replace m->dns_servers and m->search_domains, hence call it
         * before they are passed below, as the order of evaluation of the arguments is unspecified. */
        DnsServer *current_dns_server = manager_get_dns_server(m);
 
        return dns_configuration_json_append(
                        /* ifname= */ NULL,
                        /* ifindex= */ 0,
                        /* delegate= */ NULL,
                        /* default_route= */ -1,
                        current_dns_server,
                        m->dns_servers,
                        m->fallback_dns_servers,
                        m->search_domains,
                        m->trust_anchor.negative_by_name,
                        scopes,
                        manager_get_dnssec_mode(m),
                        manager_dnssec_supported(m),
                        manager_get_dns_over_tls_mode(m),
                        m->llmnr_support,
                        m->mdns_support,
                        resolv_conf_mode(),
                        configuration);
}
 
static int link_dns_configuration_json_append(Link *l, sd_json_variant **configuration) {
        _cleanup_set_free_ Set *scopes = NULL;
        int r;
 
        assert(l);
        assert(configuration);
 
        if (l->unicast_scope) {
                r = set_ensure_put(&scopes, NULL, l->unicast_scope);
                if (r < 0)
                        return r;
        }
 
        if (l->llmnr_ipv4_scope) {
                r = set_ensure_put(&scopes, NULL, l->llmnr_ipv4_scope);
                if (r < 0)
                        return r;
        }
 
        if (l->llmnr_ipv6_scope) {
                r = set_ensure_put(&scopes, NULL, l->llmnr_ipv6_scope);
                if (r < 0)
                        return r;
        }
 
        if (l->mdns_ipv4_scope) {
                r = set_ensure_put(&scopes, NULL, l->mdns_ipv4_scope);
                if (r < 0)
                        return r;
        }
 
        if (l->mdns_ipv6_scope) {
                r = set_ensure_put(&scopes, NULL, l->mdns_ipv6_scope);
                if (r < 0)
                        return r;
        }
 
        return dns_configuration_json_append(
                        l->ifname,
                        l->ifindex,
                        /* delegate= */ NULL,
                        link_get_default_route(l),
                        link_get_dns_server(l),
                        l->dns_servers,
                        /* fallback_dns_servers= */ NULL,
                        l->search_domains,
                        l->dnssec_negative_trust_anchors,
                        scopes,
                        link_get_dnssec_mode(l),
                        link_dnssec_supported(l),
                        link_get_dns_over_tls_mode(l),
                        link_get_llmnr_support(l),
                        link_get_mdns_support(l),
                        /* resolv_conf_mode= */ _RESOLV_CONF_MODE_INVALID,
                        configuration);
}
 
static int delegate_dns_configuration_json_append(DnsDelegate *d, sd_json_variant **configuration) {
        _cleanup_set_free_ Set *scopes = NULL;
        int r;
 
        assert(d);
        assert(configuration);
 
        r = set_ensure_put(&scopes, NULL, d->scope);
        if (r < 0)
                return r;
 
        return dns_configuration_json_append(
                        /* ifname= */ NULL,
                        /* ifindex= */ 0,
                        d->id,
                        d->default_route > 0, /* Defaults to false. See dns_scope_is_default_route(). */
                        dns_delegate_get_dns_server(d),
                        d->dns_servers,
                        /* fallback_dns_servers= */ NULL,
                        d->search_domains,
                        /* negative_trust_anchors= */ NULL,
                        scopes,
                        /* dnssec_mode= */ _DNSSEC_MODE_INVALID,
                        /* dnssec_supported= */ false,
                        /* dns_over_tls_mode= */ _DNS_OVER_TLS_MODE_INVALID,
                        /* llmnr_support= */ _RESOLVE_SUPPORT_INVALID,
                        /* mdns_support= */ _RESOLVE_SUPPORT_INVALID,
                        /* resolv_conf_mode= */ _RESOLV_CONF_MODE_INVALID,
                        configuration);
}
 
int manager_dump_dns_configuration_json(Manager *m, sd_json_variant **ret) {
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *configuration = NULL;
        Link *l;
        DnsDelegate *d;
        int r;
 
        assert(m);
        assert(ret);
 
        /* Global DNS configuration */
        r = global_dns_configuration_json_append(m, &configuration);
        if (r < 0)
                return r;
 
        /* Append configuration for each link */
        HASHMAP_FOREACH(l, m->links) {
                r = link_dns_configuration_json_append(l, &configuration);
                if (r < 0)
                        return r;
        }
 
        /* Append configuration for each delegate */
        HASHMAP_FOREACH(d, m->delegates) {
                r = delegate_dns_configuration_json_append(d, &configuration);
                if (r < 0)
                        return r;
        }
 
        return sd_json_buildo(ret, SD_JSON_BUILD_PAIR_VARIANT("configuration", configuration));
}
 
int manager_send_dns_configuration_changed(Manager *m, Link *l, bool reset) {
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *configuration = NULL;
        int r;
 
        assert(m);
 
        if (set_isempty(m->varlink_dns_configuration_subscription))
                return 0;
 
        if (reset) {
                dns_server_reset_accessible_all(m->dns_servers);
 
                if (l)
                        dns_server_reset_accessible_all(l->dns_servers);
        }
 
        r = manager_dump_dns_configuration_json(m, &configuration);
        if (r < 0)
                return log_warning_errno(r, "Failed to dump DNS configuration json: %m");
 
        if (sd_json_variant_equal(configuration, m->dns_configuration_json))
                return 0;
 
        json_variant_unref_and_replace(m->dns_configuration_json, configuration);
 
        r = varlink_many_notify(m->varlink_dns_configuration_subscription, m->dns_configuration_json);
        if (r < 0)
                return log_warning_errno(r, "Failed to send DNS configuration event: %m");
 
        return 0;
}
 
int manager_start_dns_configuration_monitor(Manager *m) {
        Link *l;
        int r;
 
        assert(m);
        assert(!m->dns_configuration_json);
        assert(!m->netlink_new_route_slot);
        assert(!m->netlink_del_route_slot);
 
        dns_server_reset_accessible_all(m->dns_servers);
 
        HASHMAP_FOREACH(l, m->links)
                dns_server_reset_accessible_all(l->dns_servers);
 
        r = manager_dump_dns_configuration_json(m, &m->dns_configuration_json);
        if (r < 0)
                return r;
 
        r = sd_netlink_add_match(m->rtnl, &m->netlink_new_route_slot, RTM_NEWROUTE, manager_process_route, NULL, m, "resolve-NEWROUTE");
        if (r < 0)
                return r;
 
        r = sd_netlink_add_match(m->rtnl, &m->netlink_del_route_slot, RTM_DELROUTE, manager_process_route, NULL, m, "resolve-DELROUTE");
        if (r < 0)
                return r;
 
        return 0;
}
 
void manager_stop_dns_configuration_monitor(Manager *m) {
        assert(m);
 
        m->dns_configuration_json = sd_json_variant_unref(m->dns_configuration_json);
        m->netlink_new_route_slot = sd_netlink_slot_unref(m->netlink_new_route_slot);
        m->netlink_del_route_slot = sd_netlink_slot_unref(m->netlink_del_route_slot);
}