resolved-dns-scope.c revision 28b9b7640603f88cb49f95609331fa5072715f15
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/***
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek This file is part of systemd.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Copyright 2014 Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
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
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
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/>.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek***/
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering#include <netinet/tcp.h>
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering#include "af-list.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "alloc-util.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "dns-domain.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "fd-util.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "hostname-util.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "missing.h"
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering#include "random-util.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "resolved-dns-scope.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "resolved-llmnr.h"
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering#include "socket-util.h"
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poettering#include "strv.h"
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#define MULTICAST_RATELIMIT_INTERVAL_USEC (1*USEC_PER_SEC)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#define MULTICAST_RATELIMIT_BURST 1000
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
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)
958b66ea16deddd794b3a52643bd44633e165eadLennart Poettering
f4f15635ec05293ffcc83a5b39f624bbabbd8fd0Lennart Poetteringint dns_scope_new(Manager *m, DnsScope **ret, Link *l, DnsProtocol protocol, int family) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering DnsScope *s;
958b66ea16deddd794b3a52643bd44633e165eadLennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert(m);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert(ret);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek s = new0(DnsScope, 1);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (!s)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -ENOMEM;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering s->manager = m;
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering s->link = l;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering s->protocol = protocol;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering s->family = family;
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering s->resend_timeout = MULTICAST_RESEND_TIMEOUT_MIN_USEC;
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
6bedfcbb2970e06a4d3280c8fb62083d252ede73Lennart Poettering LIST_PREPEND(scopes, m->dns_scopes, s);
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering dns_scope_llmnr_membership(s, true);
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
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));
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
8b43440b7ef4b81c69c31de7ff820dc07a780254Lennart Poettering /* Enforce ratelimiting for the multicast protocols */
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering RATELIMIT_INIT(s->ratelimit, MULTICAST_RATELIMIT_INTERVAL_USEC, MULTICAST_RATELIMIT_BURST);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *ret = s;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk}
7f1ad696a273703789b624fe0b209fb63e953016Lennart Poettering
7f1ad696a273703789b624fe0b209fb63e953016Lennart Poetteringstatic void dns_scope_abort_transactions(DnsScope *s) {
e150e82097211f09b911c7784a89ef9efed713caMichał Bartoszkiewicz assert(s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering while (s->transactions) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek DnsTransaction *t = s->transactions;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering /* Abort the transaction, but make sure it is not
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * freed while we still look at it */
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering t->block_gc++;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering dns_transaction_complete(t, DNS_TRANSACTION_ABORTED);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering t->block_gc--;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering dns_transaction_free(t);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering }
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering}
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart PoetteringDnsScope* dns_scope_free(DnsScope *s) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering DnsResourceRecord *rr;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!s)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return NULL;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
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));
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering dns_scope_llmnr_membership(s, false);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek dns_scope_abort_transactions(s);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek while (s->query_candidates)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering dns_query_candidate_free(s->query_candidates);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering hashmap_free(s->transactions_by_key);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering while ((rr = ordered_hashmap_steal_first(s->conflict_queue)))
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering dns_resource_record_unref(rr);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek ordered_hashmap_free(s->conflict_queue);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering sd_event_source_unref(s->conflict_event_source);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering dns_cache_flush(&s->cache);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek dns_zone_flush(&s->zone);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering LIST_REMOVE(scopes, s->manager->dns_scopes, s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek free(s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-SzmekDnsServer *dns_scope_get_dns_server(DnsScope *s) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->protocol != DNS_PROTOCOL_DNS)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (s->link)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return link_get_dns_server(s->link);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek else
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return manager_get_dns_server(s->manager);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekvoid dns_scope_next_dns_server(DnsScope *s) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->protocol != DNS_PROTOCOL_DNS)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (s->link)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering link_next_dns_server(s->link);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering else
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering manager_next_dns_server(s->manager);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poetteringvoid dns_scope_packet_received(DnsScope *s, usec_t rtt) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert(s);
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (rtt <= s->max_rtt)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return;
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering s->max_rtt = rtt;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering s->resend_timeout = MIN(MAX(MULTICAST_RESEND_TIMEOUT_MIN_USEC, s->max_rtt * 2), MULTICAST_RESEND_TIMEOUT_MAX_USEC);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmekvoid dns_scope_packet_lost(DnsScope *s, usec_t usec) {
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek assert(s);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek if (s->resend_timeout <= usec)
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek s->resend_timeout = MIN(s->resend_timeout * 2, MULTICAST_RESEND_TIMEOUT_MAX_USEC);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering}
da2e288bbc4d8cebaa1d38a80f6eec8cde3e9cceLennart Poettering
da2e288bbc4d8cebaa1d38a80f6eec8cde3e9cceLennart Poetteringint dns_scope_emit(DnsScope *s, int fd, DnsServer *server, DnsPacket *p) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering union in_addr_union addr;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering int ifindex = 0, r;
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek int family;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering uint16_t port;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering uint32_t mtu;
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek size_t saved_size = 0;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert(s);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert(p);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert(p->protocol == s->protocol);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert((s->protocol == DNS_PROTOCOL_DNS) != (fd < 0));
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (s->link) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering mtu = s->link->mtu;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering ifindex = s->link->ifindex;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering } else
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering mtu = manager_find_mtu(s->manager);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering switch (s->protocol) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering case DNS_PROTOCOL_DNS:
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering assert(server);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (DNS_PACKET_QDCOUNT(p) > 1)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return -EOPNOTSUPP;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (server->possible_features >= DNS_SERVER_FEATURE_LEVEL_EDNS0) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering bool edns_do;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering size_t packet_size;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering edns_do = server->possible_features >= DNS_SERVER_FEATURE_LEVEL_DO;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (server->possible_features >= DNS_SERVER_FEATURE_LEVEL_LARGE)
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek packet_size = DNS_PACKET_UNICAST_SIZE_LARGE_MAX;
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek else
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering packet_size = server->received_udp_packet_max;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = dns_packet_append_opt_rr(p, packet_size, edns_do, &saved_size);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
0fb398316c6705dfc168733361650fdb6824896dLennart Poettering DNS_PACKET_HEADER(p)->arcount = htobe16(be16toh(DNS_PACKET_HEADER(p)->arcount) + 1);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (p->size > DNS_PACKET_UNICAST_SIZE_MAX)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -EMSGSIZE;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (p->size + UDP_PACKET_HEADER_SIZE > mtu)
4608af4333d0f7f5f8e3bc632801b04ef07d246dLennart Poettering return -EMSGSIZE;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
65089b82401cd395786a7987c470056ff3f01151Lennart Poettering r = manager_write(s->manager, fd, p);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
34c10968cbe3b5591b3c0ce225b8694edd9709d0Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (saved_size > 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek dns_packet_truncate(p, saved_size);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek DNS_PACKET_HEADER(p)->arcount = htobe16(be16toh(DNS_PACKET_HEADER(p)->arcount) - 1);
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek break;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek case DNS_PROTOCOL_LLMNR:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (DNS_PACKET_QDCOUNT(p) > 1)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -EOPNOTSUPP;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!ratelimit_test(&s->ratelimit))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -EBUSY;
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt
0fb398316c6705dfc168733361650fdb6824896dLennart Poettering family = s->family;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek port = LLMNR_PORT;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (family == AF_INET) {
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 } else
0fb398316c6705dfc168733361650fdb6824896dLennart Poettering return -EAFNOSUPPORT;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (fd < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return fd;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt r = manager_send(s->manager, fd, ifindex, family, &addr, port, p);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek break;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
ed375bebf46c1251f4baa170b39ee93761dbdb19Zbigniew Jędrzejewski-Szmek default:
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -EAFNOSUPPORT;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 1;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
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 DnsServer *srv = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_close_ int fd = -1;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek union sockaddr_union sa = {};
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek socklen_t salen;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek static const int one = 1;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int ret, r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
f7dc3ab9f43b67abcbd34062b9352ab42debec49Lennart Poettering assert(s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert((family == AF_UNSPEC) == !address);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (family == AF_UNSPEC) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek srv = dns_scope_get_dns_server(s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!srv)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -ESRCH;
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek srv->possible_features = dns_server_possible_features(srv);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (type == SOCK_DGRAM && srv->possible_features < DNS_SERVER_FEATURE_LEVEL_UDP)
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek return -EAGAIN;
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sa.sa.sa_family = srv->family;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (srv->family == AF_INET) {
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt sa.in.sin_port = htobe16(port);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sa.in.sin_addr = srv->address.in;
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt salen = sizeof(sa.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;
cbd671772c9ce053a7050ddd29de170eb9efac7eZbigniew Jędrzejewski-Szmek salen = sizeof(sa.in6);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -EAFNOSUPPORT;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sa.sa.sa_family = family;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt if (family == AF_INET) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sa.in.sin_port = htobe16(port);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek sa.in.sin_addr = address->in;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek salen = sizeof(sa.in);
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;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek salen = sizeof(sa.in6);
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering } else
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering return -EAFNOSUPPORT;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering }
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering fd = socket(sa.sa.sa_family, type|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering if (fd < 0)
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering return -errno;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (type == SOCK_STREAM) {
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek r = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (r < 0)
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek return -errno;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek }
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (s->link) {
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek uint32_t ifindex = htobe32(s->link->ifindex);
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (sa.sa.sa_family == AF_INET) {
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering r = setsockopt(fd, IPPROTO_IP, IP_UNICAST_IF, &ifindex, sizeof(ifindex));
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (r < 0)
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek return -errno;
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));
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (r < 0)
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek return -errno;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
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
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));
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (r < 0)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return -errno;
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));
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek if (r < 0)
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek return -errno;
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt }
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek }
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt r = connect(fd, &sa.sa, salen);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0 && errno != EINPROGRESS)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -errno;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk if (server)
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk *server = srv;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk ret = fd;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk fd = -1;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk return ret;
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk}
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
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 Shevchuk}
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt
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);
65089b82401cd395786a7987c470056ff3f01151Lennart Poettering}
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
26687bf8a907009dedcff79346860ed41511405eOleksii ShevchukDnsScopeMatch dns_scope_good_domain(DnsScope *s, int ifindex, uint64_t flags, const char *domain) {
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering DnsSearchDomain *d;
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering assert(s);
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt assert(domain);
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering
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. */
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering if (ifindex != 0 && (!s->link || s->link->ifindex != ifindex))
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering return DNS_SCOPE_NO;
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if ((SD_RESOLVED_FLAGS_MAKE(s->protocol, s->family) & flags) == 0)
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering return DNS_SCOPE_NO;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering /* Never resolve any loopback hostname or IP address via DNS,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering * LLMNR or mDNS. Instead, always rely on synthesized RRs for
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering * these. */
ea69bd41c5923f4f278a09bb7d8cb1abcfa122e1Lennart Poettering if (is_localhost(domain) ||
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)
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek return DNS_SCOPE_NO;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
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 return DNS_SCOPE_NO;
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek
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 return DNS_SCOPE_YES;
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering switch (s->protocol) {
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek case DNS_PROTOCOL_DNS:
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
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 return DNS_SCOPE_MAYBE;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return DNS_SCOPE_NO;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering case DNS_PROTOCOL_MDNS:
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 return DNS_SCOPE_MAYBE;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return DNS_SCOPE_NO;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering case DNS_PROTOCOL_LLMNR:
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 return DNS_SCOPE_MAYBE;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return DNS_SCOPE_NO;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering default:
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering assert_not_reached("Unknown scope protocol");
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering }
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering}
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poetteringint dns_scope_good_key(DnsScope *s, DnsResourceKey *key) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering assert(s);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering assert(key);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
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
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (s->protocol == DNS_PROTOCOL_DNS) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
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 if (!dns_resource_key_is_address(key))
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering return true;
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering
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)));
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering }
8531ae707d4d0203e83304d4af948b8169a5fce1Lennart Poettering
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
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->family == AF_INET && key->class == DNS_CLASS_IN && key->type == DNS_TYPE_AAAA)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return false;
fa6ac76083b8ffc1309876459f54f9f0e2843731Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->family == AF_INET6 && key->class == DNS_CLASS_IN && key->type == DNS_TYPE_A)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return false;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return true;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poetteringint dns_scope_llmnr_membership(DnsScope *s, bool b) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int fd;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->protocol != DNS_PROTOCOL_LLMNR)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(s->link);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->family == AF_INET) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct ip_mreqn mreqn = {
fa6ac76083b8ffc1309876459f54f9f0e2843731Lennart Poettering .imr_multiaddr = LLMNR_MULTICAST_IPV4_ADDRESS,
fa6ac76083b8ffc1309876459f54f9f0e2843731Lennart Poettering .imr_ifindex = s->link->ifindex,
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering };
2678031a179a9b91fc799f8ef951a548c66c4b49Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fd = manager_llmnr_ipv4_udp_fd(s->manager);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (fd < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return fd;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
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
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering * veth. */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (b)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek (void) setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreqn, sizeof(mreqn));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (setsockopt(fd, IPPROTO_IP, b ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP, &mreqn, sizeof(mreqn)) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -errno;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else if (s->family == AF_INET6) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek struct ipv6_mreq mreq = {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek .ipv6mr_multiaddr = LLMNR_MULTICAST_IPV6_ADDRESS,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek .ipv6mr_interface = s->link->ifindex,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek };
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fd = manager_llmnr_ipv6_udp_fd(s->manager);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (fd < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return fd;
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (b)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek (void) setsockopt(fd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (setsockopt(fd, IPPROTO_IPV6, b ? IPV6_ADD_MEMBERSHIP : IPV6_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -errno;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek } else
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -EAFNOSUPPORT;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk return 0;
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchukstatic int dns_scope_make_reply_packet(
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek DnsScope *s,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek uint16_t id,
8266e1c04db8cabe3c68510a0c1f07c09ecdb2e8Lennart Poettering int rcode,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek DnsQuestion *q,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek DnsAnswer *answer,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek DnsAnswer *soa,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek bool tentative,
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering DnsPacket **ret) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek unsigned i;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(ret);
8266e1c04db8cabe3c68510a0c1f07c09ecdb2e8Lennart Poettering
8266e1c04db8cabe3c68510a0c1f07c09ecdb2e8Lennart Poettering if ((!q || q->n_keys <= 0)
8266e1c04db8cabe3c68510a0c1f07c09ecdb2e8Lennart Poettering && (!answer || answer->n_rrs <= 0)
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering && (!soa || soa->n_rrs <= 0))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return -EINVAL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = dns_packet_new(&p, s->protocol, 0);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering DNS_PACKET_HEADER(p)->id = id;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek DNS_PACKET_HEADER(p)->flags = htobe16(DNS_PACKET_MAKE_FLAGS(
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek 1 /* qr */,
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering 0 /* opcode */,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek 0 /* c */,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek 0 /* tc */,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek tentative,
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering 0 /* (ra) */,
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering 0 /* (ad) */,
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering 0 /* (cd) */,
d378991747d67fff1d4dc39e7fb2bc8f49f1b561Mirco Tischler rcode));
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (q) {
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);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (r < 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
82499507b369fea3033a74c22813bf423301aef4Lennart Poettering DNS_PACKET_HEADER(p)->qdcount = htobe16(q->n_keys);
82499507b369fea3033a74c22813bf423301aef4Lennart Poettering }
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (answer) {
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);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (r < 0)
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering return r;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering }
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering DNS_PACKET_HEADER(p)->ancount = htobe16(answer->n_rrs);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (soa) {
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);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek DNS_PACKET_HEADER(p)->arcount = htobe16(soa->n_rrs);
c2457105d76e3daf159f554a9bafb9751b23d756Holger Hans Peter Freyther }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek *ret = p;
c2457105d76e3daf159f554a9bafb9751b23d756Holger Hans Peter Freyther p = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek return 0;
c2457105d76e3daf159f554a9bafb9751b23d756Holger Hans Peter Freyther}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void dns_scope_verify_conflicts(DnsScope *s, DnsPacket *p) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek unsigned n;
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(s);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert(p);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (p->question)
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]);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering if (p->answer)
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);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
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 DnsResourceKey *key = NULL;
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden bool tentative = false;
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden int r, fd;
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden assert(s);
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden assert(p);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden if (p->protocol != DNS_PROTOCOL_LLMNR)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
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. */
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering if (p->family == AF_INET && !in_addr_equal(AF_INET, &p->destination, (union in_addr_union*) &LLMNR_MULTICAST_IPV4_ADDRESS))
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering return;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (p->family == AF_INET6 && !in_addr_equal(AF_INET6, &p->destination, (union in_addr_union*) &LLMNR_MULTICAST_IPV6_ADDRESS))
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering return;
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek }
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = dns_packet_extract(p);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering if (r < 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug_errno(r, "Failed to extract resources from incoming packet: %m");
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering return;
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering }
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (DNS_PACKET_LLMNR_C(p)) {
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering /* Somebody notified us about a possible conflict */
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek dns_scope_verify_conflicts(s, p);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering }
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering assert(p->question->n_keys == 1);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering key = p->question->keys[0];
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering r = dns_zone_lookup(&s->zone, key, &answer, &soa, &tentative);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering if (r < 0) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering log_debug_errno(r, "Failed to lookup key: %m");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering return;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek if (r == 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (answer)
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering dns_answer_order_by_scope(answer, in_addr_is_link_local(p->family, &p->sender) > 0);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering r = dns_scope_make_reply_packet(s, DNS_PACKET_ID(p), DNS_RCODE_SUCCESS, p->question, answer, soa, tentative, &reply);
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering if (r < 0) {
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering log_debug_errno(r, "Failed to build reply packet: %m");
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering return;
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering }
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering if (stream)
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering r = dns_stream_write_packet(stream, reply);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering else {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering if (!ratelimit_test(&s->ratelimit))
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return;
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering if (p->family == AF_INET)
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering fd = manager_llmnr_ipv4_udp_fd(s->manager);
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering else if (p->family == AF_INET6)
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering fd = manager_llmnr_ipv6_udp_fd(s->manager);
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering else {
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering log_debug("Unknown protocol");
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering return;
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering }
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering if (fd < 0) {
0a244b8ecb6dfcb381fe831dc2aa9bacb2c12975Lennart Poettering log_debug_errno(fd, "Failed to get reply socket: %m");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering return;
2d43b190901902dbd98ccea77c1d1ddc9e2a9955Dan McGee }
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering
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 */
6baa7db00812437bbc87e73faa1a11b6cf631958Lennart Poettering
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering r = manager_send(s->manager, fd, p->ifindex, p->family, &p->sender, p->sender_port, reply);
f8294e4175918117ca6c131720bcf287eadcd029Josh Triplett }
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering if (r < 0) {
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering log_debug_errno(r, "Failed to send reply packet: %m");
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering return;
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart PoetteringDnsTransaction *dns_scope_find_transaction(DnsScope *scope, DnsResourceKey *key, bool cache_ok) {
e7ff4e7fe9f3abd2297e4ef7b95dcb2804e051c3Greg Kroah-Hartman DnsTransaction *t;
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering assert(scope);
d682b3a7e7c7c2941a4d3e193f1e330dbc9fae89Lennart Poettering assert(key);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
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 if (!t)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return NULL;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
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. */
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek if (!cache_ok &&
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-Szmek return NULL;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return t;
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek}
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmekstatic int dns_scope_make_conflict_packet(
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek DnsScope *s,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek DnsResourceRecord *rr,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek DnsPacket **ret) {
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek int r;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert(s);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert(rr);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert(ret);
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = dns_packet_new(&p, s->protocol, 0);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (r < 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return r;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek DNS_PACKET_HEADER(p)->flags = htobe16(DNS_PACKET_MAKE_FLAGS(
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek 0 /* qr */,
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering 0 /* opcode */,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek 1 /* conflict */,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek 0 /* tc */,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek 0 /* t */,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek 0 /* (ra) */,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek 0 /* (ad) */,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek 0 /* (cd) */,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek 0));
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
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = dns_packet_append_key(p, rr->key, NULL);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (r < 0)
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek return r;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = dns_packet_append_rr(p, rr, NULL, NULL);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (r < 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return r;
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek *ret = p;
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering p = NULL;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return 0;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek}
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poetteringstatic int on_conflict_dispatch(sd_event_source *es, usec_t usec, void *userdata) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek DnsScope *scope = userdata;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek int r;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert(es);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek assert(scope);
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek scope->conflict_event_source = sd_event_source_unref(scope->conflict_event_source);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek for (;;) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering _cleanup_(dns_packet_unrefp) DnsPacket *p = NULL;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering rr = ordered_hashmap_steal_first(scope->conflict_queue);
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering if (!rr)
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering break;
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering
63c372cb9df3bee01e3bf8cd7f96f336bddda846Lennart Poettering r = dns_scope_make_conflict_packet(scope, rr, &p);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (r < 0) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_error_errno(r, "Failed to make conflict packet: %m");
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering return 0;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek }
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = dns_scope_emit(scope, -1, NULL, p);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (r < 0)
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_debug_errno(r, "Failed to send conflict packet: %m");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering return 0;
a569398925430de1f8479262e8ab39502054f2e9Lennart Poettering}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekint dns_scope_notify_conflict(DnsScope *scope, DnsResourceRecord *rr) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek usec_t jitter;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering assert(scope);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering assert(rr);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
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);
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering if (r < 0) {
0c24bb2346b6b6232d67aacd5236b56ea4989de4Lennart Poettering log_oom();
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
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 0;
edc3797f7cd9e37c24e5241cac3263e7c918f732Lennart Poettering if (r < 0)
edc3797f7cd9e37c24e5241cac3263e7c918f732Lennart Poettering return log_debug_errno(r, "Failed to queue conflicting RR: %m");
7517e17443225fafea86b21e42a36af69feb1dbcTorstein Husebø
8a0889dfdafa3054c894e54852d8a9e3a7e8390bLennart Poettering dns_resource_record_ref(rr);
da4993920cdf5527b8528f0a483b54ab3cbc1971Kay Sievers
da4993920cdf5527b8528f0a483b54ab3cbc1971Kay Sievers if (scope->conflict_event_source)
759c945a43577d56e85a927f15e7d9aaa94a4e4aColin Walters return 0;
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek random_bytes(&jitter, sizeof(jitter));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek jitter %= LLMNR_JITTER_INTERVAL_USEC;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = sd_event_add_time(scope->manager->event,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek &scope->conflict_event_source,
b6fa25552e538eca207072e12d223e3523b21a19Evgeny Vereshchagin clock_boottime_or_monotonic(),
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek now(clock_boottime_or_monotonic()) + jitter,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek LLMNR_JITTER_INTERVAL_USEC,
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek on_conflict_dispatch, scope);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return log_debug_errno(r, "Failed to add conflict dispatch event: %m");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return 0;
b6fa25552e538eca207072e12d223e3523b21a19Evgeny Vereshchagin}
b6fa25552e538eca207072e12d223e3523b21a19Evgeny Vereshchagin
b6fa25552e538eca207072e12d223e3523b21a19Evgeny Vereshchaginvoid dns_scope_check_conflicts(DnsScope *scope, DnsPacket *p) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek unsigned i;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek int r;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(scope);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(p);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (p->protocol != DNS_PROTOCOL_LLMNR)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (DNS_PACKET_RRCOUNT(p) <= 0)
e2cc6eca73cd1df8be552d7c23f9ff3d69c06f1eLennart Poettering return;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (DNS_PACKET_LLMNR_C(p) != 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (DNS_PACKET_LLMNR_T(p) != 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering if (manager_our_packet(scope->manager, p))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = dns_packet_extract(p);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug_errno(r, "Failed to extract packet: %m");
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering return;
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_debug("Checking for conflicts...");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek for (i = 0; i < p->answer->n_rrs; i++) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
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);
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek if (r != 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek continue;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
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 if (r <= 0)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek continue;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek dns_scope_notify_conflict(scope, p->answer->items[i].rr);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
2f5df74a5ec135ab2baebf26af6f088e5b4b8205Holger Hans Peter Freyther}
2f5df74a5ec135ab2baebf26af6f088e5b4b8205Holger Hans Peter Freyther
2f5df74a5ec135ab2baebf26af6f088e5b4b8205Holger Hans Peter Freythervoid dns_scope_dump(DnsScope *s, FILE *f) {
2f5df74a5ec135ab2baebf26af6f088e5b4b8205Holger Hans Peter Freyther assert(s);
2f5df74a5ec135ab2baebf26af6f088e5b4b8205Holger Hans Peter Freyther
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!f)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek f = stdout;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
e9174f29c7e3ee45137537b126458718913a3ec5Lennart Poettering fputs("[Scope protocol=", f);
7027ff61a34a12487712b382a061c654acc3a679Lennart Poettering fputs(dns_protocol_to_string(s->protocol), f);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->link) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fputs(" interface=", f);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fputs(s->link->name, f);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->family != AF_UNSPEC) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fputs(" family=", f);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fputs(af_to_name(s->family), f);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fputs("]\n", f);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!dns_zone_is_empty(&s->zone)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fputs("ZONE:\n", f);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek dns_zone_dump(&s->zone, f);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering if (!dns_cache_is_empty(&s->cache)) {
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering fputs("CACHE:\n", f);
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek dns_cache_dump(&s->cache, f);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek }
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-SzmekDnsSearchDomain *dns_scope_get_search_domains(DnsScope *s) {
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek assert(s);
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->protocol != DNS_PROTOCOL_DNS)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return NULL;
d07f7b9ef2835c290d6beadebd17d15308608eeaLennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (s->link)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return s->link->search_domains;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
caa2f4c0c9613b2e02aafa308c8fb092576014a9Zbigniew Jędrzejewski-Szmek return s->manager->search_domains;
84267e4043cf88bf540b5bf9cd65e194670a4ffaLennart Poettering}
09eba4d46b1119c758fba38a520a38c8d19af739Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekbool dns_scope_name_needs_search_domain(DnsScope *s, const char *name) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert(s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
caa2f4c0c9613b2e02aafa308c8fb092576014a9Zbigniew Jędrzejewski-Szmek if (s->protocol != DNS_PROTOCOL_DNS)
caa2f4c0c9613b2e02aafa308c8fb092576014a9Zbigniew Jędrzejewski-Szmek return false;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return dns_name_is_single_label(name);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek}
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek