resolved-manager.c revision ec2c5e4398f9d65e5dfe61530f2556224733d1e6
/*-*- 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 "resolved.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;
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;
}
int r;
assert(m);
int family;
union in_addr_union addr;
if (r < 0) {
continue;
}
/* filter out duplicates */
continue;
if (r < 0)
return r;
}
/* do not warn about state here, since probably systemd already did */
return 0;
}
int config_parse_dnsv(
const char *unit,
const char *filename,
unsigned line,
const char *section,
unsigned section_line,
const char *lvalue,
int ltype,
const char *rvalue,
void *data,
void *userdata) {
int r;
assert(m);
/* Empty assignment means clear the list */
while (m->dns_servers)
return 0;
}
r = parse_dns_server_string(m, rvalue);
if (r < 0) {
log_error("Failed to parse DNS server string");
return r;
}
return 0;
}
int manager_parse_config_file(Manager *m) {
assert(m);
"Resolve\0",
false, false, true, m);
}
int r;
if (!m)
return -ENOMEM;
m->use_llmnr = true;
r = parse_dns_server_string(m, DNS_SERVERS);
if (r < 0)
return r;
m->hostname = gethostname_malloc();
if (!m->hostname)
return -ENOMEM;
r = sd_event_default(&m->event);
if (r < 0)
return r;
sd_event_set_watchdog(m->event, true);
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;
r = manager_llmnr_ipv4_udp_fd(m);
if (r < 0)
return r;
r = manager_llmnr_ipv6_udp_fd(m);
if (r < 0)
return r;
r = manager_llmnr_ipv4_tcp_fd(m);
if (r < 0)
return r;
r = manager_llmnr_ipv6_tcp_fd(m);
if (r < 0)
return r;
*ret = m;
m = NULL;
return 0;
}
Link *l;
if (!m)
return NULL;
while (m->dns_queries)
while ((l = hashmap_first(m->links)))
link_free(l);
hashmap_free(m->links);
while (m->dns_servers)
safe_close(m->dns_ipv4_fd);
safe_close(m->dns_ipv6_fd);
sd_bus_unref(m->bus);
sd_event_unref(m->event);
free(m);
return NULL;
}
_cleanup_free_ char *t = NULL;
int r;
assert(s);
assert(f);
if (r < 0) {
log_warning("Invalid DNS address. Ignoring.");
return;
}
fputs("# Too many DNS servers configured, the following entries may be ignored\n", f);
fprintf(f, "nameserver %s\n", t);
(*count) ++;
}
int manager_write_resolv_conf(Manager *m) {
const char *path = "/run/systemd/resolve/resolv.conf";
unsigned count = 0;
DnsServer *s;
Iterator i;
Link *l;
int r;
assert(m);
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);
HASHMAP_FOREACH(l, m->links, i)
write_resolve_conf_server(s, f, &count);
write_resolve_conf_server(s, f, &count);
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 NULL;
}
assert(m);
if (!m->current_dns_server)
m->current_dns_server = m->dns_servers;
return m->current_dns_server;
}
void manager_next_dns_server(Manager *m) {
assert(m);
if (!m->current_dns_server) {
m->current_dns_server = m->dns_servers;
return;
}
if (!m->current_dns_server)
return;
if (m->current_dns_server->servers_next) {
return;
}
m->current_dns_server = 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;
if (dns_packet_validate_reply(p) > 0) {
if (!t)
return 0;
} else if (dns_packet_validate_query(p) > 0) {
Link *l;
if (l) {
scope = l->llmnr_ipv4_scope;
scope = l->llmnr_ipv6_scope;
if (scope)
}
} else
log_debug("Invalid LLMNR 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 (dns_packet_validate_query(s->read_packet) > 0) {
Link *l;
if (l) {
scope = l->llmnr_ipv4_scope;
scope = l->llmnr_ipv6_scope;
if (scope) {
/* If no reply packet was set, we free the stream */
if (s->write_packet)
return 0;
}
}
}
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;
}
int manager_next_hostname(Manager *m) {
const char *p;
Iterator i;
uint64_t u;
char *h;
Link *l;
assert(m);
assert(p);
while (p > m->hostname) {
break;
p--;
}
if (*p == 0 || safe_atou64(p, &u) < 0 || u <= 0)
u = 1;
u++;
return -ENOMEM;
m->hostname = h;
HASHMAP_FOREACH(l, m->links, i) {
link_add_rrs(l, true);
link_add_rrs(l, false);
}
return 0;
}
Iterator i;
Link *l;
assert(m);
HASHMAP_FOREACH(l, m->links, i) {
LinkAddress *a;
if (a)
return a;
}
return NULL;
}
assert(m);
assert(p);
}