resolved-dns-scope.c revision 28b9b7640603f88cb49f95609331fa5072715f15
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek This file is part of systemd.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Copyright 2014 Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek under the terms of the GNU Lesser General Public License as published by
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2.1 of the License, or
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek (at your option) any later version.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Lesser General Public License for more details.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek You should have received a copy of the GNU Lesser General Public License
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#define MULTICAST_RATELIMIT_INTERVAL_USEC (1*USEC_PER_SEC)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#define MULTICAST_RATELIMIT_BURST 1000
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering/* After how much time to repeat LLMNR requests, see RFC 4795 Section 7 */
0dec689b098cf62b948d43dc78bde859665056ecTom Gundersen#define MULTICAST_RESEND_TIMEOUT_MIN_USEC (100 * USEC_PER_MSEC)
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering#define MULTICAST_RESEND_TIMEOUT_MAX_USEC (1 * USEC_PER_SEC)
f4f15635ec05293ffcc83a5b39f624bbabbd8fd0Lennart Poetteringint dns_scope_new(Manager *m, DnsScope **ret, Link *l, DnsProtocol protocol, int family) {
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering s->resend_timeout = MULTICAST_RESEND_TIMEOUT_MIN_USEC;
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering log_debug("New scope on link %s, protocol %s, family %s", l ? l->name : "*", dns_protocol_to_string(protocol), family == AF_UNSPEC ? "*" : af_to_name(family));
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering /* Enforce ratelimiting for the multicast protocols */
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering RATELIMIT_INIT(s->ratelimit, MULTICAST_RATELIMIT_INTERVAL_USEC, MULTICAST_RATELIMIT_BURST);
7f1ad696a273703789b624fe0b209fb63e953016Lennart Poetteringstatic void dns_scope_abort_transactions(DnsScope *s) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek DnsTransaction *t = s->transactions;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering /* Abort the transaction, but make sure it is not
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * freed while we still look at it */
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering dns_transaction_complete(t, DNS_TRANSACTION_ABORTED);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering log_debug("Removing scope on link %s, protocol %s, family %s", s->link ? s->link->name : "*", dns_protocol_to_string(s->protocol), s->family == AF_UNSPEC ? "*" : af_to_name(s->family));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek dns_scope_abort_transactions(s);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering dns_query_candidate_free(s->query_candidates);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering while ((rr = ordered_hashmap_steal_first(s->conflict_queue)))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek ordered_hashmap_free(s->conflict_queue);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering sd_event_source_unref(s->conflict_event_source);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering LIST_REMOVE(scopes, s->manager->dns_scopes, s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-SzmekDnsServer *dns_scope_get_dns_server(DnsScope *s) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->protocol != DNS_PROTOCOL_DNS)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return manager_get_dns_server(s->manager);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekvoid dns_scope_next_dns_server(DnsScope *s) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->protocol != DNS_PROTOCOL_DNS)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringvoid dns_scope_packet_received(DnsScope *s, usec_t rtt) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering s->resend_timeout = MIN(MAX(MULTICAST_RESEND_TIMEOUT_MIN_USEC, s->max_rtt * 2), MULTICAST_RESEND_TIMEOUT_MAX_USEC);
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmekvoid dns_scope_packet_lost(DnsScope *s, usec_t usec) {
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek s->resend_timeout = MIN(s->resend_timeout * 2, MULTICAST_RESEND_TIMEOUT_MAX_USEC);
da2e288bbc4d8cebaa1d38a80f6eec8cde3e9cceLennart Poetteringint dns_scope_emit(DnsScope *s, int fd, DnsServer *server, DnsPacket *p) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert((s->protocol == DNS_PROTOCOL_DNS) != (fd < 0));
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (server->possible_features >= DNS_SERVER_FEATURE_LEVEL_EDNS0) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering edns_do = server->possible_features >= DNS_SERVER_FEATURE_LEVEL_DO;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (server->possible_features >= DNS_SERVER_FEATURE_LEVEL_LARGE)
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek packet_size = DNS_PACKET_UNICAST_SIZE_LARGE_MAX;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering packet_size = server->received_udp_packet_max;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = dns_packet_append_opt_rr(p, packet_size, edns_do, &saved_size);
0fb398316c6705dfc168733361650fdb6824896dLennart Poettering DNS_PACKET_HEADER(p)->arcount = htobe16(be16toh(DNS_PACKET_HEADER(p)->arcount) + 1);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (p->size > DNS_PACKET_UNICAST_SIZE_MAX)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (p->size + UDP_PACKET_HEADER_SIZE > mtu)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek dns_packet_truncate(p, saved_size);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek DNS_PACKET_HEADER(p)->arcount = htobe16(be16toh(DNS_PACKET_HEADER(p)->arcount) - 1);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!ratelimit_test(&s->ratelimit))
23ad4dd8844c582929115a11ed2830a1371568d6Jan Alexander Steffens (heftig) addr.in = LLMNR_MULTICAST_IPV4_ADDRESS;
23ad4dd8844c582929115a11ed2830a1371568d6Jan Alexander Steffens (heftig) fd = manager_llmnr_ipv4_udp_fd(s->manager);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else if (family == AF_INET6) {
23ad4dd8844c582929115a11ed2830a1371568d6Jan Alexander Steffens (heftig) addr.in6 = LLMNR_MULTICAST_IPV6_ADDRESS;
23ad4dd8844c582929115a11ed2830a1371568d6Jan Alexander Steffens (heftig) fd = manager_llmnr_ipv6_udp_fd(s->manager);
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt r = manager_send(s->manager, fd, ifindex, family, &addr, port, p);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int dns_scope_socket(DnsScope *s, int type, int family, const union in_addr_union *address, uint16_t port, DnsServer **server) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert((family == AF_UNSPEC) == !address);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek srv = dns_scope_get_dns_server(s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek srv->possible_features = dns_server_possible_features(srv);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (type == SOCK_DGRAM && srv->possible_features < DNS_SERVER_FEATURE_LEVEL_UDP)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sa.in.sin_addr = srv->address.in;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else if (srv->family == AF_INET6) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sa.in6.sin6_port = htobe16(port);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sa.in6.sin6_addr = srv->address.in6;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sa.in6.sin6_scope_id = s->link ? s->link->ifindex : 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else if (family == AF_INET6) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sa.in6.sin6_port = htobe16(port);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sa.in6.sin6_addr = address->in6;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sa.in6.sin6_scope_id = s->link ? s->link->ifindex : 0;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering fd = socket(sa.sa.sa_family, type|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek r = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek uint32_t ifindex = htobe32(s->link->ifindex);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (sa.sa.sa_family == AF_INET) {
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering r = setsockopt(fd, IPPROTO_IP, IP_UNICAST_IF, &ifindex, sizeof(ifindex));
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering } else if (sa.sa.sa_family == AF_INET6) {
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek r = setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_IF, &ifindex, sizeof(ifindex));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->protocol == DNS_PROTOCOL_LLMNR) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* RFC 4795, section 2.5 requires the TTL to be set to 1 */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (sa.sa.sa_family == AF_INET) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = setsockopt(fd, IPPROTO_IP, IP_TTL, &one, sizeof(one));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else if (sa.sa.sa_family == AF_INET6) {
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt r = setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &one, sizeof(one));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0 && errno != EINPROGRESS)
65089b82401cd395786a7987c470056ff3f01151Lennart Poetteringint dns_scope_udp_dns_socket(DnsScope *s, DnsServer **server) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk return dns_scope_socket(s, SOCK_DGRAM, AF_UNSPEC, NULL, 53, server);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchukint dns_scope_tcp_socket(DnsScope *s, int family, const union in_addr_union *address, uint16_t port, DnsServer **server) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk return dns_scope_socket(s, SOCK_STREAM, family, address, port, server);
26687bf8a907009dedcff79346860ed41511405eOleksii ShevchukDnsScopeMatch dns_scope_good_domain(DnsScope *s, int ifindex, uint64_t flags, const char *domain) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk /* Checks if the specified domain is something to look up on
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk * this scope. Note that this accepts non-qualified hostnames,
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk * i.e. those without any search path prefixed yet. */
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering if (ifindex != 0 && (!s->link || s->link->ifindex != ifindex))
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if ((SD_RESOLVED_FLAGS_MAKE(s->protocol, s->family) & flags) == 0)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering /* Never resolve any loopback hostname or IP address via DNS,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * LLMNR or mDNS. Instead, always rely on synthesized RRs for
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering dns_name_endswith(domain, "127.in-addr.arpa") > 0 ||
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek dns_name_equal(domain, "1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa") > 0)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering /* Never respond to some of the domains listed in RFC6303 */
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (dns_name_endswith(domain, "0.in-addr.arpa") > 0 ||
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering dns_name_equal(domain, "255.255.255.255.in-addr.arpa") > 0 ||
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering dns_name_equal(domain, "0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa") > 0)
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek /* Always honour search domains for routing queries. Note that
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * we return DNS_SCOPE_YES here, rather than just
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * DNS_SCOPE_MAYBE, which means wildcard scopes won't be
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * considered anymore. */
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering LIST_FOREACH(domains, d, dns_scope_get_search_domains(s))
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (dns_name_endswith(domain, d->name) > 0)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering /* Exclude link-local IP ranges */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (dns_name_endswith(domain, "254.169.in-addr.arpa") == 0 &&
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek dns_name_endswith(domain, "8.e.f.ip6.arpa") == 0 &&
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek dns_name_endswith(domain, "9.e.f.ip6.arpa") == 0 &&
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek dns_name_endswith(domain, "a.e.f.ip6.arpa") == 0 &&
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek dns_name_endswith(domain, "b.e.f.ip6.arpa") == 0)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if ((s->family == AF_INET && dns_name_endswith(domain, "in-addr.arpa") > 0) ||
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek (s->family == AF_INET6 && dns_name_endswith(domain, "ip6.arpa") > 0) ||
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering (dns_name_endswith(domain, "local") > 0 && /* only resolve names ending in .local via mDNS */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek dns_name_equal(domain, "local") == 0 && /* but not the single-label "local" name itself */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek manager_is_own_hostname(s->manager, domain) <= 0)) /* never resolve the local hostname via mDNS */
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if ((s->family == AF_INET && dns_name_endswith(domain, "in-addr.arpa") > 0) ||
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering (s->family == AF_INET6 && dns_name_endswith(domain, "ip6.arpa") > 0) ||
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering (dns_name_is_single_label(domain) && /* only resolve single label names via LLMNR */
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering !is_gateway_hostname(domain) && /* don't resolve "gateway" with LLMNR, let nss-myhostname handle this */
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering manager_is_own_hostname(s->manager, domain) <= 0)) /* never resolve the local hostname via LLMNR */
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering assert_not_reached("Unknown scope protocol");
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poetteringint dns_scope_good_key(DnsScope *s, DnsResourceKey *key) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering /* Check if it makes sense to resolve the specified key on
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering * this scope. Note that this call assumes as fully qualified
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering * name, i.e. the search suffixes already appended. */
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering /* On classic DNS, lookin up non-address RRs is always
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering * fine. (Specifically, we want to permit looking up
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering * DNSKEY and DS records on the root and top-level
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering * domains.) */
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering /* However, we refuse to look up A and AAAA RRs on the
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering * root and single-label domains, under the assumption
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering * that those should be resolved via LLMNR or search
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering * path only, and should not be leaked onto the
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering * internet. */
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return !(dns_name_is_single_label(DNS_RESOURCE_KEY_NAME(key)) ||
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering dns_name_is_root(DNS_RESOURCE_KEY_NAME(key)));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* On mDNS and LLMNR, send A and AAAA queries only on the
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * respective scopes */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->family == AF_INET && key->class == DNS_CLASS_IN && key->type == DNS_TYPE_AAAA)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->family == AF_INET6 && key->class == DNS_CLASS_IN && key->type == DNS_TYPE_A)
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poetteringint dns_scope_llmnr_membership(DnsScope *s, bool b) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->protocol != DNS_PROTOCOL_LLMNR)
fa6ac76083b8ffc1309876459f54f9f0e2843731Lennart Poettering .imr_multiaddr = LLMNR_MULTICAST_IPV4_ADDRESS,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fd = manager_llmnr_ipv4_udp_fd(s->manager);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Always first try to drop membership before we add
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * one. This is necessary on some devices, such as
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek (void) setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreqn, sizeof(mreqn));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (setsockopt(fd, IPPROTO_IP, b ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP, &mreqn, sizeof(mreqn)) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else if (s->family == AF_INET6) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek .ipv6mr_multiaddr = LLMNR_MULTICAST_IPV6_ADDRESS,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek .ipv6mr_interface = s->link->ifindex,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fd = manager_llmnr_ipv6_udp_fd(s->manager);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek (void) setsockopt(fd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (setsockopt(fd, IPPROTO_IPV6, b ? IPV6_ADD_MEMBERSHIP : IPV6_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
8266e1c04db8cabe3c68510a0c1f07c09ecdb2e8Lennart Poettering if ((!q || q->n_keys <= 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = dns_packet_new(&p, s->protocol, 0);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek DNS_PACKET_HEADER(p)->flags = htobe16(DNS_PACKET_MAKE_FLAGS(
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering 0 /* opcode */,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek for (i = 0; i < q->n_keys; i++) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = dns_packet_append_key(p, q->keys[i], NULL);
82499507b369fea3033a74c22813bf423301aef4Lennart Poettering DNS_PACKET_HEADER(p)->qdcount = htobe16(q->n_keys);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek for (i = 0; i < answer->n_rrs; i++) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = dns_packet_append_rr(p, answer->items[i].rr, NULL, NULL);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering DNS_PACKET_HEADER(p)->ancount = htobe16(answer->n_rrs);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek for (i = 0; i < soa->n_rrs; i++) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = dns_packet_append_rr(p, soa->items[i].rr, NULL, NULL);
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek DNS_PACKET_HEADER(p)->arcount = htobe16(soa->n_rrs);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void dns_scope_verify_conflicts(DnsScope *s, DnsPacket *p) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek for (n = 0; n < p->question->n_keys; n++)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek dns_zone_verify_conflicts(&s->zone, p->question->keys[n]);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek for (n = 0; n < p->answer->n_rrs; n++)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek dns_zone_verify_conflicts(&s->zone, p->answer->items[n].rr->key);
9bdbc2e2ec523dbefe1c1c7e164b5544aff0b185Lukas Nykrynvoid dns_scope_process_query(DnsScope *s, DnsStream *stream, DnsPacket *p) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_(dns_packet_unrefp) DnsPacket *reply = NULL;
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL, *soa = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (p->ipproto == IPPROTO_UDP) {
0a20e3c10761378869af7bbef2733e3ae879d0f1Holger Hans Peter Freyther /* Don't accept UDP queries directed to anything but
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * the LLMNR multicast addresses. See RFC 4795,
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering * section 2.5. */
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering if (p->family == AF_INET && !in_addr_equal(AF_INET, &p->destination, (union in_addr_union*) &LLMNR_MULTICAST_IPV4_ADDRESS))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (p->family == AF_INET6 && !in_addr_equal(AF_INET6, &p->destination, (union in_addr_union*) &LLMNR_MULTICAST_IPV6_ADDRESS))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug_errno(r, "Failed to extract resources from incoming packet: %m");
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering /* Somebody notified us about a possible conflict */
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek dns_scope_verify_conflicts(s, p);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering r = dns_zone_lookup(&s->zone, key, &answer, &soa, &tentative);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering log_debug_errno(r, "Failed to lookup key: %m");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering dns_answer_order_by_scope(answer, in_addr_is_link_local(p->family, &p->sender) > 0);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering r = dns_scope_make_reply_packet(s, DNS_PACKET_ID(p), DNS_RCODE_SUCCESS, p->question, answer, soa, tentative, &reply);
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering log_debug_errno(r, "Failed to build reply packet: %m");
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering r = dns_stream_write_packet(stream, reply);
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering fd = manager_llmnr_ipv4_udp_fd(s->manager);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering fd = manager_llmnr_ipv6_udp_fd(s->manager);
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering log_debug_errno(fd, "Failed to get reply socket: %m");
2d43b190901902dbd98ccea77c1d1ddc9e2a9955Dan McGee /* Note that we always immediately reply to all LLMNR
ef1673d16907726d83bdff2e57b5261997a85020Mirco Tischler * requests, and do not wait any time, since we
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * verified uniqueness for all records. Also see RFC
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * 4795, Section 2.7 */
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering r = manager_send(s->manager, fd, p->ifindex, p->family, &p->sender, p->sender_port, reply);
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering log_debug_errno(r, "Failed to send reply packet: %m");
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart PoetteringDnsTransaction *dns_scope_find_transaction(DnsScope *scope, DnsResourceKey *key, bool cache_ok) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Try to find an ongoing transaction that is a equal to the
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * specified question */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek t = hashmap_get(scope->transactions_by_key, key);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek /* Refuse reusing transactions that completed based on cached
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek * data instead of a real packet, if that's requested. */
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek IN_SET(t->state, DNS_TRANSACTION_SUCCESS, DNS_TRANSACTION_FAILURE) &&
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek t->answer_source != DNS_TRANSACTION_NETWORK)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmekstatic int dns_scope_make_conflict_packet(
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = dns_packet_new(&p, s->protocol, 0);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek DNS_PACKET_HEADER(p)->flags = htobe16(DNS_PACKET_MAKE_FLAGS(
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering 0 /* opcode */,
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek random_bytes(&DNS_PACKET_HEADER(p)->id, sizeof(uint16_t));
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek DNS_PACKET_HEADER(p)->qdcount = htobe16(1);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek DNS_PACKET_HEADER(p)->arcount = htobe16(1);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = dns_packet_append_key(p, rr->key, NULL);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = dns_packet_append_rr(p, rr, NULL, NULL);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poetteringstatic int on_conflict_dispatch(sd_event_source *es, usec_t usec, void *userdata) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek scope->conflict_event_source = sd_event_source_unref(scope->conflict_event_source);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering rr = ordered_hashmap_steal_first(scope->conflict_queue);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering r = dns_scope_make_conflict_packet(scope, rr, &p);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_error_errno(r, "Failed to make conflict packet: %m");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = dns_scope_emit(scope, -1, NULL, p);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_debug_errno(r, "Failed to send conflict packet: %m");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekint dns_scope_notify_conflict(DnsScope *scope, DnsResourceRecord *rr) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering /* We don't send these queries immediately. Instead, we queue
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering * them, and send them after some jitter delay. */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = ordered_hashmap_ensure_allocated(&scope->conflict_queue, &dns_resource_key_hash_ops);
da4993920cdf5527b8528f0a483b54ab3cbc1971Kay Sievers /* We only place one RR per key in the conflict
40adcda869bda55f44b57fd3a2bd71d006dfb51bLennart Poettering * messages, not all of them. That should be enough to
759c945a43577d56e85a927f15e7d9aaa94a4e4aColin Walters * indicate where there might be a conflict */
82499507b369fea3033a74c22813bf423301aef4Lennart Poettering r = ordered_hashmap_put(scope->conflict_queue, rr->key, rr);
edc3797f7cd9e37c24e5241cac3263e7c918f732Lennart Poettering if (r == -EEXIST || r == 0)
edc3797f7cd9e37c24e5241cac3263e7c918f732Lennart Poettering return log_debug_errno(r, "Failed to queue conflicting RR: %m");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek random_bytes(&jitter, sizeof(jitter));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek jitter %= LLMNR_JITTER_INTERVAL_USEC;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_event_add_time(scope->manager->event,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek now(clock_boottime_or_monotonic()) + jitter,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return log_debug_errno(r, "Failed to add conflict dispatch event: %m");
b6fa25552e538eca207072e12d223e3523b21a19Evgeny Vereshchaginvoid dns_scope_check_conflicts(DnsScope *scope, DnsPacket *p) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (p->protocol != DNS_PROTOCOL_LLMNR)
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering if (manager_our_packet(scope->manager, p))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug_errno(r, "Failed to extract packet: %m");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug("Checking for conflicts...");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek for (i = 0; i < p->answer->n_rrs; i++) {
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering /* Check for conflicts against the local zone. If we
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer * found one, we won't check any further */
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering r = dns_zone_check_conflicts(&scope->zone, p->answer->items[i].rr);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Check for conflicts against the local cache. If so,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek * send out an advisory query, to inform everybody */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = dns_cache_check_conflicts(&scope->cache, p->answer->items[i].rr, p->family, &p->sender);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek dns_scope_notify_conflict(scope, p->answer->items[i].rr);
2f5df74a5ec135ab2baebf26af6f088e5b4b8205Holger Hans Peter Freythervoid dns_scope_dump(DnsScope *s, FILE *f) {
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering fputs(dns_protocol_to_string(s->protocol), f);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fputs(af_to_name(s->family), f);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!dns_zone_is_empty(&s->zone)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-SzmekDnsSearchDomain *dns_scope_get_search_domains(DnsScope *s) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->protocol != DNS_PROTOCOL_DNS)
caa2f4c0c9613b2e02aafa308c8fb092576014a9Zbigniew Jędrzejewski-Szmek return s->manager->search_domains;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekbool dns_scope_name_needs_search_domain(DnsScope *s, const char *name) {
caa2f4c0c9613b2e02aafa308c8fb092576014a9Zbigniew Jędrzejewski-Szmek if (s->protocol != DNS_PROTOCOL_DNS)