/***
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 "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-etc-hosts.h"
#include "resolved-llmnr.h"
#include "resolved-manager.h"
#include "resolved-mdns.h"
#include "resolved-resolv-conf.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;
}
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;
}
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)
return 0;
}
assert(m);
if (r < 0)
return r;
if (fd < 0)
return fd;
if (events < 0)
return events;
if (r < 0)
return r;
if (r < 0)
return r;
return 0;
}
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_new(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;
}
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->dnssec_mode = DNSSEC_NO;
m->read_resolv_conf = true;
m->need_builtin_fallbacks = true;
r = dns_trust_anchor_load(&m->trust_anchor);
if (r < 0)
return r;
r = manager_parse_config_file(m);
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 r;
assert(m);
r = manager_llmnr_start(m);
if (r < 0)
return r;
r = manager_mdns_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;
}
union {
+ CMSG_SPACE(int) /* ttl/hoplimit */
+ EXTRA_CMSG_SPACE /* kernel appears to require extra buffer space */];
} control;
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 %" PRIu16 ".", 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 {
} control;
assert(m);
assert(p);
if (ifindex > 0) {
}
}
static int manager_ipv6_send(Manager *m, int fd, int ifindex, const struct in6_addr *addr, uint16_t port, DnsPacket *p) {
};
union {
} control;
assert(m);
assert(p);
if (ifindex > 0) {
}
}
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 %" PRIu16 " on interface %i/%s.", DNS_PACKET_QR(p) ? "response" : "query", DNS_PACKET_ID(p), ifindex, af_to_name(family));
return -EAFNOSUPPORT;
}
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;
}
Iterator i;
Link *l;
assert(m);
HASHMAP_FOREACH(l, m->links, i) {
link_add_rrs(l, true);
link_add_rrs(l, false);
}
}
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;
switch (p->protocol) {
case DNS_PROTOCOL_LLMNR:
return l->llmnr_ipv4_scope;
return l->llmnr_ipv6_scope;
break;
case DNS_PROTOCOL_MDNS:
return l->mdns_ipv4_scope;
return l->mdns_ipv6_scope;
break;
default:
break;
}
return NULL;
}
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;
}
DnsServer *s;
Iterator i;
Link *l;
int r;
assert(m);
if (r < 0)
return r;
/* 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 */
HASHMAP_FOREACH(l, m->links, i) {
r = ordered_set_put(*dns, s);
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;
}
}
return 0;
}
DnsSearchDomain *d;
Iterator i;
Link *l;
int r;
assert(m);
if (r < 0)
return r;
if (r == -EEXIST)
continue;
if (r < 0)
return r;
}
HASHMAP_FOREACH(l, m->links, i) {
if (r == -EEXIST)
continue;
if (r < 0)
return r;
}
}
return 0;
}
assert(m);
if (m->dnssec_mode != _DNSSEC_MODE_INVALID)
return m->dnssec_mode;
return DNSSEC_NO;
}
Iterator i;
Link *l;
assert(m);
if (manager_get_dnssec_mode(m) == DNSSEC_NO)
return false;
server = manager_get_dns_server(m);
return false;
HASHMAP_FOREACH(l, m->links, i)
if (!link_dnssec_supported(l))
return false;
return true;
}
if (log_get_max_level() >= LOG_DEBUG) {
_cleanup_free_ char *s = NULL;
(void) dns_resource_key_to_string(key, &s);
log_debug("Found verdict for lookup %s: %s", s ? strstrip(s) : "n/a", dnssec_verdict_to_string(verdict));
}
m->n_dnssec_verdict[verdict]++;
}
Iterator i;
Link *l;
assert(m);
/* Returns true if the host has at least one interface with a routable address of the specified type */
HASHMAP_FOREACH(l, m->links, i)
if (link_relevant(l, family, false))
return true;
return false;
}