resolved-manager.c revision bf32f99f65ec42539af65736b057c9e09d3987b9
/*-*- 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 <resolv.h>
#include "rtnl-util.h"
#include "event-util.h"
#include "network-util.h"
#include "network-internal.h"
#include "conf-parser.h"
#include "socket-util.h"
#include "af-list.h"
#include "utf8.h"
#include "resolved-dns-domain.h"
#include "resolved-conf.h"
#include "resolved-bus.h"
#include "resolved-manager.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:
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:
if (a)
break;
}
return 0;
fail:
return 0;
}
static int manager_rtnl_listen(Manager *m) {
sd_rtnl_message *i;
int r;
assert(m);
/* First, subscibe to interfaces coming and going */
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_rtnl_message_request_dump(req, true);
if (r < 0)
return r;
if (r < 0)
return r;
for (i = reply; i; i = sd_rtnl_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_rtnl_message_request_dump(req, true);
if (r < 0)
return r;
if (r < 0)
return r;
for (i = reply; i; i = sd_rtnl_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)
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;
}
static int determine_hostname(char **ret) {
int r;
h = gethostname_malloc();
if (!h)
return log_oom();
if (!utf8_is_valid(h)) {
log_error("System hostname is not UTF-8 clean.");
return -EINVAL;
}
r = dns_name_normalize(h, &n);
if (r < 0) {
log_error("System hostname '%s' cannot be normalized.", h);
return r;
}
*ret = n;
n = NULL;
return 0;
}
_cleanup_free_ char *h = NULL;
int r;
assert(m);
r = determine_hostname(&h);
if (r < 0)
return 0; /* ignore invalid hostnames */
return 0;
log_info("System hostname changed to '%s'.", h);
m->hostname = h;
h = NULL;
return 0;
}
static int manager_watch_hostname(Manager *m) {
int r;
assert(m);
if (m->hostname_fd < 0) {
log_warning("Failed to watch hostname: %m");
return 0;
}
if (r < 0) {
if (r == -EPERM)
/* kernels prior to 3.2 don't support polling this file. Ignore the failure. */
else {
return r;
}
}
r = determine_hostname(&m->hostname);
if (r < 0) {
log_info("Defaulting to hostname 'linux'.");
if (!m->hostname)
return log_oom();
} else
return 0;
}
static void manager_llmnr_stop(Manager *m) {
assert(m);
}
static int manager_llmnr_start(Manager *m) {
int r;
assert(m);
if (m->llmnr_support == SUPPORT_NO)
return 0;
r = manager_llmnr_ipv4_udp_fd(m);
if (r == -EADDRINUSE)
goto eaddrinuse;
if (r < 0)
return r;
r = manager_llmnr_ipv4_tcp_fd(m);
if (r == -EADDRINUSE)
goto eaddrinuse;
if (r < 0)
return r;
if (socket_ipv6_is_supported()) {
r = manager_llmnr_ipv6_udp_fd(m);
if (r == -EADDRINUSE)
goto eaddrinuse;
if (r < 0)
return r;
r = manager_llmnr_ipv6_tcp_fd(m);
if (r == -EADDRINUSE)
goto eaddrinuse;
if (r < 0)
return r;
}
return 0;
log_warning("There appears to be another LLMNR respondering running. Turning off LLMNR support.");
m->llmnr_support = SUPPORT_NO;
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);
safe_close(m->dns_ipv4_fd);
safe_close(m->dns_ipv6_fd);
sd_bus_unref(m->bus);
sd_event_unref(m->event);
safe_close(m->hostname_fd);
free(m);
return NULL;
}
int manager_read_resolv_conf(Manager *m) {
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) {
log_warning("Failed to open /etc/resolv.conf: %m");
r = -errno;
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) {
log_warning("Failed to open /etc/resolv.conf: %m");
r = -errno;
goto clear;
}
log_error("Failed to stat open file: %m");
r = -errno;
goto clear;
}
s->marked = true;
union in_addr_union address;
int family;
char *l;
const char *a;
if (*l == '#' || *l == ';')
continue;
a = first_word(l, "nameserver");
if (!a)
continue;
if (r < 0) {
log_warning("Failed to parse name server %s.", a);
continue;
}
break;
if (s)
s->marked = false;
else {
if (r < 0)
goto clear;
}
}
if (s->marked)
dns_server_free(s);
return 0;
while (m->dns_servers)
return r;
}
_cleanup_free_ char *t = NULL;
int r;
assert(s);
assert(f);
if (r < 0) {
return;
}
fputs("# Too many DNS servers configured, the following entries may be ignored.\n", f);
fprintf(f, "nameserver %s\n", t);
(*count) ++;
}
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) ++;
}
int manager_write_resolv_conf(Manager *m) {
static const char path[] = "/run/systemd/resolve/resolv.conf";
unsigned count = 0;
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 */
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;
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 (set_isempty(dns)) {
if (r == -EEXIST)
continue;
if (r < 0)
return r;
}
}
if (r < 0)
return r;
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 (set_isempty(dns))
fputs("# No DNS servers known.\n", f);
else {
SET_FOREACH(s, dns, i)
write_resolv_conf_server(s, f, &count);
}
if (!set_isempty(domains)) {
unsigned length = 0;
char *domain;
count = 0;
fputs("search", f);
fputs("\n", f);
}
r = fflush_and_check(f);
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. */
p->ifindex = 0;
/* 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;
}
DnsTransaction *t = NULL;
int r;
if (r <= 0)
return r;
if (dns_packet_validate_reply(p) > 0) {
if (!t)
return 0;
} else
log_debug("Invalid DNS packet.");
return 0;
}
int manager_dns_ipv4_fd(Manager *m) {
const int one = 1;
int r;
assert(m);
if (m->dns_ipv4_fd >= 0)
return m->dns_ipv4_fd;
if (m->dns_ipv4_fd < 0)
return -errno;
if (r < 0) {
r = -errno;
goto fail;
}
r = sd_event_add_io(m->event, &m->dns_ipv4_event_source, m->dns_ipv4_fd, EPOLLIN, on_dns_packet, m);
if (r < 0)
goto fail;
return m->dns_ipv4_fd;
fail:
return r;
}
int manager_dns_ipv6_fd(Manager *m) {
const int one = 1;
int r;
assert(m);
if (m->dns_ipv6_fd >= 0)
return m->dns_ipv6_fd;
if (m->dns_ipv6_fd < 0)
return -errno;
if (r < 0) {
r = -errno;
goto fail;
}
r = sd_event_add_io(m->event, &m->dns_ipv6_event_source, m->dns_ipv6_fd, EPOLLIN, on_dns_packet, m);
if (r < 0)
goto fail;
return m->dns_ipv6_fd;
fail:
return r;
}
int r;
for (;;) {
return 0;
continue;
return -errno;
if (r < 0)
return r;
if (r == 0)
return -ETIMEDOUT;
}
}
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;
}
DnsTransaction *t = NULL;
int r;
if (r <= 0)
return r;
scope = manager_find_scope(m, p);
if (!scope) {
log_warning("Got LLMNR UDP packet on unknown scope. Ignoring.");
return 0;
}
if (dns_packet_validate_reply(p) > 0) {
if (t)
} else if (dns_packet_validate_query(p) > 0) {
} else
log_debug("Invalid LLMNR UDP packet.");
return 0;
}
int manager_llmnr_ipv4_udp_fd(Manager *m) {
union sockaddr_union sa = {
};
int r;
assert(m);
if (m->llmnr_ipv4_udp_fd >= 0)
return m->llmnr_ipv4_udp_fd;
if (m->llmnr_ipv4_udp_fd < 0)
return -errno;
/* RFC 4795, section 2.5 recommends setting the TTL of UDP packets to 255. */
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
/* Disable Don't-Fragment bit in the IP header */
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
r = sd_event_add_io(m->event, &m->llmnr_ipv4_udp_event_source, m->llmnr_ipv4_udp_fd, EPOLLIN, on_llmnr_packet, m);
if (r < 0)
goto fail;
return m->llmnr_ipv4_udp_fd;
fail:
return r;
}
int manager_llmnr_ipv6_udp_fd(Manager *m) {
union sockaddr_union sa = {
};
int r;
assert(m);
if (m->llmnr_ipv6_udp_fd >= 0)
return m->llmnr_ipv6_udp_fd;
if (m->llmnr_ipv6_udp_fd < 0)
return -errno;
if (r < 0) {
r = -errno;
goto fail;
}
/* RFC 4795, section 2.5 recommends setting the TTL of UDP packets to 255. */
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
r = sd_event_add_io(m->event, &m->llmnr_ipv6_udp_event_source, m->llmnr_ipv6_udp_fd, EPOLLIN, on_llmnr_packet, m);
if (r < 0) {
r = -errno;
goto fail;
}
return m->llmnr_ipv6_udp_fd;
fail:
return r;
}
static int on_llmnr_stream_packet(DnsStream *s) {
assert(s);
if (!scope) {
log_warning("Got LLMNR TCP packet on unknown scope. Ignroing.");
return 0;
}
if (dns_packet_validate_query(s->read_packet) > 0) {
/* If no reply packet was set, we free the stream */
if (s->write_packet)
return 0;
} else
log_debug("Invalid LLMNR TCP packet.");
dns_stream_free(s);
return 0;
}
int cfd, r;
if (cfd < 0) {
return 0;
return -errno;
}
if (r < 0) {
return r;
}
return 0;
}
int manager_llmnr_ipv4_tcp_fd(Manager *m) {
union sockaddr_union sa = {
};
int r;
assert(m);
if (m->llmnr_ipv4_tcp_fd >= 0)
return m->llmnr_ipv4_tcp_fd;
if (m->llmnr_ipv4_tcp_fd < 0)
return -errno;
/* RFC 4795, section 2.5. requires setting the TTL of TCP streams to 1 */
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
/* Disable Don't-Fragment bit in the IP header */
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
r = sd_event_add_io(m->event, &m->llmnr_ipv4_tcp_event_source, m->llmnr_ipv4_tcp_fd, EPOLLIN, on_llmnr_stream, m);
if (r < 0)
goto fail;
return m->llmnr_ipv4_tcp_fd;
fail:
return r;
}
int manager_llmnr_ipv6_tcp_fd(Manager *m) {
union sockaddr_union sa = {
};
static const int one = 1;
int r;
assert(m);
if (m->llmnr_ipv6_tcp_fd >= 0)
return m->llmnr_ipv6_tcp_fd;
if (m->llmnr_ipv6_tcp_fd < 0)
return -errno;
/* RFC 4795, section 2.5. requires setting the TTL of TCP streams to 1 */
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
if (r < 0) {
r = -errno;
goto fail;
}
r = sd_event_add_io(m->event, &m->llmnr_ipv6_tcp_event_source, m->llmnr_ipv6_tcp_fd, EPOLLIN, on_llmnr_stream, m);
if (r < 0) {
r = -errno;
goto fail;
}
return m->llmnr_ipv6_tcp_fd;
fail:
return r;
}
Link *l;
assert(m);
if (ifindex <= 0)
return -EINVAL;
if (l->flags & IFF_LOOPBACK)
return 1;
return 0;
}
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;
assert(m);
assert(p);
while (p > m->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;
m->hostname = h;
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);
}
assert(m);
if (t == DNS_SERVER_SYSTEM)
while (m->dns_servers)
if (t == DNS_SERVER_FALLBACK)
while (m->fallback_dns_servers)
}
static const char* const support_table[_SUPPORT_MAX] = {
[SUPPORT_NO] = "no",
[SUPPORT_YES] = "yes",
[SUPPORT_RESOLVE] = "resolve",
};