resolved-manager.c revision 636e813dc98ea40c58c6c85bc5e7e3c9f0904ea2
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2014 Tom Gundersen <teg@jklm.no>
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <poll.h>
#include <resolv.h>
#include "af-list.h"
#include "alloc-util.h"
#include "dns-domain.h"
#include "fd-util.h"
#include "fileio-label.h"
#include "hostname-util.h"
#include "io-util.h"
#include "netlink-util.h"
#include "network-internal.h"
#include "ordered-set.h"
#include "parse-util.h"
#include "random-util.h"
#include "resolved-bus.h"
#include "resolved-conf.h"
#include "resolved-llmnr.h"
#include "resolved-manager.h"
#include "socket-util.h"
#include "string-table.h"
#include "string-util.h"
#include "utf8.h"
Link *l;
int ifindex, r;
assert(m);
if (r < 0)
goto fail;
if (r < 0)
goto fail;
switch (type) {
case RTM_NEWLINK:{
bool is_new = !l;
if (!l) {
if (r < 0)
goto fail;
}
r = link_update_rtnl(l, mm);
if (r < 0)
goto fail;
r = link_update_monitor(l);
if (r < 0)
goto fail;
if (is_new)
break;
}
case RTM_DELLINK:
if (l) {
link_free(l);
}
break;
}
return 0;
fail:
log_warning_errno(r, "Failed to process RTNL link message: %m");
return 0;
}
union in_addr_union address;
LinkAddress *a;
Link *l;
assert(m);
if (r < 0)
goto fail;
if (r < 0)
goto fail;
if (!l)
return 0;
if (r < 0)
goto fail;
switch (family) {
case AF_INET:
if (r < 0) {
if (r < 0)
goto fail;
}
break;
case AF_INET6:
if (r < 0) {
if (r < 0)
goto fail;
}
break;
default:
return 0;
}
switch (type) {
case RTM_NEWADDR:
if (!a) {
if (r < 0)
return r;
}
r = link_address_update_rtnl(a, mm);
if (r < 0)
return r;
break;
case RTM_DELADDR:
break;
}
return 0;
fail:
log_warning_errno(r, "Failed to process RTNL address message: %m");
return 0;
}
static int manager_rtnl_listen(Manager *m) {
int r;
assert(m);
/* First, subscribe to interfaces coming and going */
r = sd_netlink_open(&m->rtnl);
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
/* Then, enumerate all links */
if (r < 0)
return r;
r = sd_netlink_message_request_dump(req, true);
if (r < 0)
return r;
if (r < 0)
return r;
for (i = reply; i; i = sd_netlink_message_next(i)) {
r = manager_process_link(m->rtnl, i, m);
if (r < 0)
return r;
}
/* Finally, enumerate all addresses, too */
if (r < 0)
return r;
r = sd_netlink_message_request_dump(req, true);
if (r < 0)
return r;
if (r < 0)
return r;
for (i = reply; i; i = sd_netlink_message_next(i)) {
r = manager_process_address(m->rtnl, i, m);
if (r < 0)
return r;
}
return r;
}
Iterator i;
Link *l;
int r;
assert(m);
HASHMAP_FOREACH(l, m->links, i) {
r = link_update_monitor(l);
if (r < 0)
}
r = manager_write_resolv_conf(m);
if (r < 0)
log_warning_errno(r, "Could not update resolv.conf: %m");
return 0;
}
static int manager_network_monitor_listen(Manager *m) {
assert(m);
if (r < 0)
return r;
if (fd < 0)
return fd;
if (events < 0)
return events;
if (r < 0)
return r;
return 0;
}
char label[DNS_LABEL_MAX];
const char *p;
int r, k;
/* Extract and normalize the first label of the locally
* configured hostname, and check it's not "localhost". */
h = gethostname_malloc();
if (!h)
return log_oom();
p = h;
if (r < 0)
return log_error_errno(r, "Failed to unescape host name: %m");
if (r == 0) {
log_error("Couldn't find a single label in hosntame.");
return -EINVAL;
}
if (k < 0)
return log_error_errno(k, "Failed to undo IDNA: %m");
if (k > 0)
r = k;
if (!utf8_is_valid(label)) {
log_error("System hostname is not UTF-8 clean.");
return -EINVAL;
}
r = dns_label_escape(label, r, &n);
if (r < 0)
return log_error_errno(r, "Failed to escape host name: %m");
if (is_localhost(n)) {
log_debug("System hostname is 'localhost', ignoring.");
return -EINVAL;
}
if (r < 0)
return log_error_errno(r, "Failed to determine mDNS hostname: %m");
*llmnr_hostname = n;
n = NULL;
return 0;
}
int r;
assert(m);
if (r < 0)
return 0; /* ignore invalid hostnames */
return 0;
free(m->llmnr_hostname);
free(m->mdns_hostname);
m->llmnr_hostname = llmnr_hostname;
m->mdns_hostname = mdns_hostname;
return 0;
}
static int manager_watch_hostname(Manager *m) {
int r;
assert(m);
if (m->hostname_fd < 0) {
return 0;
}
if (r < 0) {
if (r == -EPERM)
/* kernels prior to 3.2 don't support polling this file. Ignore the failure. */
else
return log_error_errno(r, "Failed to add hostname event source: %m");
}
if (r < 0) {
log_info("Defaulting to hostname 'linux'.");
if (!m->llmnr_hostname)
return log_oom();
if (!m->mdns_hostname)
return log_oom();
} else
return 0;
}
assert(s);
assert(m);
if (!f)
return log_oom();
dns_scope_dump(scope, f);
if (fflush_and_check(f) < 0)
return log_oom();
return 0;
}
int r;
if (!m)
return -ENOMEM;
m->hostname_fd = -1;
m->llmnr_support = SUPPORT_YES;
m->read_resolv_conf = true;
if (r < 0)
return r;
r = sd_event_default(&m->event);
if (r < 0)
return r;
sd_event_set_watchdog(m->event, true);
r = manager_watch_hostname(m);
if (r < 0)
return r;
if (r < 0)
return r;
r = manager_network_monitor_listen(m);
if (r < 0)
return r;
r = manager_rtnl_listen(m);
if (r < 0)
return r;
r = manager_connect_bus(m);
if (r < 0)
return r;
*ret = m;
m = NULL;
return 0;
}
int manager_start(Manager *m) {
int r;
assert(m);
r = manager_llmnr_start(m);
if (r < 0)
return r;
return 0;
}
Link *l;
if (!m)
return NULL;
while ((l = hashmap_first(m->links)))
link_free(l);
while (m->dns_queries)
hashmap_free(m->links);
sd_netlink_unref(m->rtnl);
sd_bus_unref(m->bus);
sd_event_unref(m->event);
safe_close(m->hostname_fd);
free(m->llmnr_hostname);
free(m->mdns_hostname);
free(m);
return NULL;
}
int manager_read_resolv_conf(Manager *m) {
DnsServer *s;
usec_t t;
int r;
assert(m);
/* Reads the system /etc/resolv.conf, if it exists and is not
* symlinked to our own resolv.conf instance */
if (!m->read_resolv_conf)
return 0;
if (r < 0) {
r = 0;
else
goto clear;
}
/* Have we already seen the file? */
if (t == m->resolv_conf_mtime)
return 0;
m->resolv_conf_mtime = t;
/* Is it symlinked to our own file? */
r = 0;
goto clear;
}
if (!f) {
r = 0;
else
goto clear;
}
goto clear;
}
_cleanup_strv_free_ char **d = NULL;
const char *a;
char *l;
if (*l == '#' || *l == ';')
continue;
a = first_word(l, "nameserver");
if (a) {
r = manager_add_dns_server_by_string(m, DNS_SERVER_SYSTEM, a);
if (r < 0)
log_warning_errno(r, "Failed to parse DNS server address '%s', ignoring.", a);
continue;
}
}
/* Whenever /etc/resolv.conf changes, start using the first
* DNS server of it. This is useful to deal with broken
* network managing implementations (like NetworkManager),
* that when connecting to a VPN place both the VPN DNS
* servers and the local ones in /etc/resolv.conf. Without
* resetting the DNS server to use back to the first entry we
* will continue to use the local one thus being unable to
* resolve VPN domains. */
manager_set_dns_server(m, m->dns_servers);
return 0;
while (m->dns_servers) {
s = m->dns_servers;
dns_server_unref(s);
}
return r;
}
_cleanup_free_ char *t = NULL;
int r;
assert(s);
assert(f);
if (r < 0) {
log_warning_errno(r, "Invalid DNS address. Ignoring: %m");
return;
}
fputs("# Too many DNS servers configured, the following entries may be ignored.\n", f);
(*count) ++;
fprintf(f, "nameserver %s\n", t);
}
static void write_resolv_conf_search(
const char *domain,
FILE *f,
unsigned *count,
unsigned *length) {
assert(f);
fputs(" # Too many search domains configured, remaining ones ignored.", f);
if (*length <= 256)
fputs(" # Total length of all search domains is too long, remaining ones ignored.", f);
return;
}
(*count) ++;
fputc(' ', f);
}
Iterator i;
fputs("# This file is managed by systemd-resolved(8). Do not edit.\n#\n"
"# Third party programs must not access this file directly, but\n"
"# only through the symlink at /etc/resolv.conf. To manage\n"
"# resolv.conf(5) in a different way, replace the symlink by a\n"
"# static file or a different symlink.\n\n", f);
if (ordered_set_isempty(dns))
fputs("# No DNS servers known.\n", f);
else {
unsigned count = 0;
DnsServer *s;
ORDERED_SET_FOREACH(s, dns, i)
write_resolv_conf_server(s, f, &count);
}
if (!ordered_set_isempty(domains)) {
char *domain;
fputs("search", f);
fputs("\n", f);
}
return fflush_and_check(f);
}
int manager_write_resolv_conf(Manager *m) {
static const char path[] = "/run/systemd/resolve/resolv.conf";
DnsServer *s;
Iterator i;
Link *l;
int r;
assert(m);
/* Read the system /etc/resolv.conf first */
/* Add the full list to a set, to filter out duplicates */
if (!dns)
return -ENOMEM;
if (!domains)
return -ENOMEM;
/* First add the system-wide servers and domains */
r = ordered_set_put(dns, s);
if (r == -EEXIST)
continue;
if (r < 0)
return r;
}
/* Then, add the per-link servers and domains */
HASHMAP_FOREACH(l, m->links, i) {
char **domain;
r = ordered_set_put(dns, s);
if (r == -EEXIST)
continue;
if (r < 0)
return r;
}
if (!l->unicast_scope)
continue;
if (r == -EEXIST)
continue;
if (r < 0)
return r;
}
}
/* If we found nothing, add the fallback servers */
if (ordered_set_isempty(dns)) {
r = ordered_set_put(dns, s);
if (r == -EEXIST)
continue;
if (r < 0)
return r;
}
}
if (r < 0)
return r;
if (r < 0)
goto fail;
r = -errno;
goto fail;
}
return 0;
fail:
return r;
}
union {
+ CMSG_SPACE(int) /* ttl/hoplimit */
+ EXTRA_CMSG_SPACE /* kernel appears to require extra buffer space */];
} control;
union sockaddr_union sa;
int ms = 0, r;
ssize_t l;
assert(m);
if (r < 0)
return -errno;
if (ms < 0)
return -EIO;
if (r < 0)
return r;
if (l < 0) {
return 0;
return -errno;
}
if (l <= 0)
return -EIO;
p->ipproto = IPPROTO_UDP;
} else
return -EAFNOSUPPORT;
case IPV6_PKTINFO: {
if (p->ifindex <= 0)
p->ifindex = i->ipi6_ifindex;
break;
}
case IPV6_HOPLIMIT:
break;
}
case IP_PKTINFO: {
if (p->ifindex <= 0)
p->ifindex = i->ipi_ifindex;
break;
}
case IP_TTL:
break;
}
}
}
/* The Linux kernel sets the interface index to the loopback
* device if the packet came from the local host since it
* avoids the routing table in such a case. Let's unset the
* interface index in such a case. */
if (p->ifindex == LOOPBACK_IFINDEX)
p->ifindex = 0;
if (protocol != DNS_PROTOCOL_DNS) {
/* If we don't know the interface index still, we look for the
* first local interface with a matching address. Yuck! */
if (p->ifindex <= 0)
}
*ret = p;
p = NULL;
return 1;
}
int r;
for (;;) {
return 0;
continue;
return -errno;
if (r < 0)
return r;
if (r == 0)
return -ETIMEDOUT;
}
}
int r;
for (;;) {
return 0;
continue;
return -errno;
if (r < 0)
return r;
if (r == 0)
return -ETIMEDOUT;
}
}
int r;
log_debug("Sending %s packet with id %u", DNS_PACKET_QR(p) ? "response" : "query", DNS_PACKET_ID(p));
if (r < 0)
return r;
return 0;
}
static int manager_ipv4_send(Manager *m, int fd, int ifindex, const struct in_addr *addr, uint16_t port, DnsPacket *p) {
union sockaddr_union sa = {
};
union {
} control;
assert(m);
assert(p);
if (ifindex > 0) {
struct in_pktinfo *pi;
}
}
static int manager_ipv6_send(Manager *m, int fd, int ifindex, const struct in6_addr *addr, uint16_t port, DnsPacket *p) {
union sockaddr_union sa = {
};
union {
} control;
assert(m);
assert(p);
if (ifindex > 0) {
struct in6_pktinfo *pi;
}
}
int manager_send(Manager *m, int fd, int ifindex, int family, const union in_addr_union *addr, uint16_t port, DnsPacket *p) {
assert(m);
assert(p);
log_debug("Sending %s packet with id %u on interface %i/%s", DNS_PACKET_QR(p) ? "response" : "query", DNS_PACKET_ID(p), ifindex, af_to_name(family));
return -EAFNOSUPPORT;
}
DnsServer *s;
assert(m);
return s;
return s;
return NULL;
}
assert(m);
if (m->current_dns_server == s)
return s;
if (s) {
}
m->current_dns_server = s;
if (m->unicast_scope)
return s;
}
Link *l;
assert(m);
/* Try to read updates resolv.conf */
if (!m->current_dns_server)
manager_set_dns_server(m, m->dns_servers);
if (!m->current_dns_server) {
bool found = false;
Iterator i;
/* No DNS servers configured, let's see if there are
* any on any links. If not, we use the fallback
* servers */
HASHMAP_FOREACH(l, m->links, i)
if (l->dns_servers) {
found = true;
break;
}
if (!found)
}
return m->current_dns_server;
}
void manager_next_dns_server(Manager *m) {
assert(m);
/* If there's currently no DNS server set, then the next
* manager_get_dns_server() will find one */
if (!m->current_dns_server)
return;
/* Change to the next one */
if (m->current_dns_server->servers_next) {
return;
}
/* If there was no next one, then start from the beginning of
* the list */
else
manager_set_dns_server(m, m->dns_servers);
}
Link *l;
Iterator i;
/* If we don't know on which link a DNS packet would be
* delivered, let's find the largest MTU that works on all
* interfaces we know of */
HASHMAP_FOREACH(l, m->links, i) {
if (l->mtu <= 0)
continue;
}
return mtu;
}
LinkAddress *a;
assert(m);
if (a)
return 0;
}
void manager_refresh_rrs(Manager *m) {
Iterator i;
Link *l;
assert(m);
HASHMAP_FOREACH(l, m->links, i) {
link_add_rrs(l, true);
link_add_rrs(l, false);
}
}
int manager_next_hostname(Manager *m) {
const char *p;
uint64_t u, a;
char *h, *k;
int r;
assert(m);
p = strchr(m->llmnr_hostname, 0);
assert(p);
while (p > m->llmnr_hostname) {
break;
p--;
}
if (*p == 0 || safe_atou64(p, &u) < 0 || u <= 0)
u = 1;
/* Add a random number to the old value. This way we can avoid
* that two hosts pick the same hostname, win on IPv4 and lose
* on IPv6 (or vice versa), and pick the same hostname
* replacement hostname, ad infinitum. We still want the
* numbers to go up monotonically, hence we just add a random
* value 1..10 */
random_bytes(&a, sizeof(a));
u += 1 + a % 10;
return -ENOMEM;
r = dns_name_concat(h, "local", &k);
if (r < 0) {
free(h);
return r;
}
log_info("Hostname conflict, changing published hostname from '%s' to '%s'.", m->llmnr_hostname, h);
free(m->llmnr_hostname);
m->llmnr_hostname = h;
free(m->mdns_hostname);
m->mdns_hostname = k;
return 0;
}
LinkAddress* manager_find_link_address(Manager *m, int family, const union in_addr_union *in_addr) {
Iterator i;
Link *l;
assert(m);
HASHMAP_FOREACH(l, m->links, i) {
LinkAddress *a;
if (a)
return a;
}
return NULL;
}
assert(m);
assert(p);
}
Link *l;
assert(m);
assert(p);
if (!l)
return NULL;
if (p->protocol == DNS_PROTOCOL_LLMNR) {
return l->llmnr_ipv4_scope;
return l->llmnr_ipv6_scope;
}
return NULL;
}
void manager_verify_all(Manager *m) {
DnsScope *s;
assert(m);
dns_zone_verify_all(&s->zone);
}
int r;
assert(m);
if (m->llmnr_hostname) {
if (r != 0)
return r;
}
if (m->mdns_hostname)
return 0;
}
static const char* const support_table[_SUPPORT_MAX] = {
[SUPPORT_NO] = "no",
[SUPPORT_YES] = "yes",
[SUPPORT_RESOLVE] = "resolve",
};